CEBL  2.1
WidgetPanel.cpp
Go to the documentation of this file.
1 
8 #include "WidgetPanel.hpp"
9 #include <vector>
10 #include <string>
11 #include <sstream>
12 #include <iostream>
13 
14 using namespace std;
15 using namespace CEBL;
16 
18 {
19  container = gtk_vbox_new(false,0);
20  table = gtk_table_new(1,3,false);
21  nrows = 1;
22 
23  gtk_box_pack_start(GTK_BOX(container),table,false,false,0);
24 }
25 WidgetPanel::WidgetPanel(map<std::string, CEBL::Param> params)
26 {
27  container = gtk_vbox_new(false,0);
28  table = gtk_table_new(1,3,false);
29  nrows = 1;
30 
31  gtk_box_pack_start(GTK_BOX(container),table,false,false,0);
32  createByParams(params);
33 }
34 
36 {
37 
38 }
39 
40 //adds a string widget
41 void WidgetPanel::add(string parameter,
42  string description,
43  string default_value)
44 {
45  CEBL::Param temp(parameter, description,default_value);
46  add(temp);
47 }
48 
49 //adds a CEBL::Param widget
51 {
52  //Text stuff that is the same for any data type
53  parameters.push_back(param.name);
54  descriptions.push_back(param.description);
55  GtkWidget *label1 = gtk_label_new(param.name.c_str());
56  gtk_label_set_line_wrap(GTK_LABEL(label1),true);
57  gtk_widget_set_size_request(label1,100,-1);
58 
59  GtkWidget *label2 = gtk_label_new(param.description.c_str());
60  gtk_label_set_line_wrap(GTK_LABEL(label2),true);
61  gtk_widget_set_size_request(label1,100,-1);
62 
63  GtkWidget *descript_align = gtk_alignment_new(0,0,1,1);
64  gtk_container_add(GTK_CONTAINER(descript_align), label2);
65 
66  //Create widget based on data type
67  GtkWidget *value;
68 
69  //boolean
70  if(param==PARAM_BOOLEAN)
71  {
72  value = gtk_check_button_new();
73  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(value),param.getBool());
74  }
75  //integer
76  else if(param==PARAM_INTEGER)
77  {
78  value = gtk_spin_button_new_with_range(param.getMin(),
79  param.getMax(),
80  param.getStep());
81  gtk_spin_button_set_value(GTK_SPIN_BUTTON(value),param.getInt());
82 
83  }
84  //double
85  else if(param==PARAM_DOUBLE)
86  {
87  value = gtk_spin_button_new_with_range(param.getMin(),
88  param.getMax(),
89  param.getStep());
90  gtk_spin_button_set_value(GTK_SPIN_BUTTON(value),param.getDouble());
91  }
92  //string
93  else if(param==PARAM_STRING)
94  {
95  value = gtk_entry_new();
96  gtk_entry_set_text(GTK_ENTRY(value),param.getString().c_str());
97  }
98  else
99  {
100  value = gtk_entry_new();
101  std::cerr << "No Param Type Identified";
102  }
103 
104  parameter_widgets.push_back(label1);
105  description_widgets.push_back(label2);
106  value_widgets.push_back(value);
107 
108  //add to table
109  gtk_table_resize(GTK_TABLE(table),nrows++,3);
110  gtk_table_attach(GTK_TABLE(table)
111  ,label1
112  ,0,1,nrows,nrows+1,
113  GTK_SHRINK,
114  GTK_SHRINK,
115  10,0);
116 
117  //GtkWidget *align = gtk_alignment_new(0,0,1,1);
118  gtk_table_attach(GTK_TABLE(table)
119  ,value
120  ,1,2,nrows,nrows+1,
121  GTK_SHRINK,
122  GTK_SHRINK,
123  5,0);
124  gtk_table_attach(GTK_TABLE(table)
125  ,descript_align
126  ,2,3,nrows,nrows+1,
127  GTK_SHRINK,
128  GTK_SHRINK,
129  10,0);
130 
131 }
132 
133 void WidgetPanel::createByParams(map<std::string, CEBL::Param> params)
134 {
135  this->params = params;
136  map<std::string, CEBL::Param>::iterator it;
137  for(it = params.begin(); it!=params.end(); ++it)
138  {
139  add(it->second);
140  }
141 }
142 
143 
144 map<std::string, CEBL::Param> WidgetPanel::getParams()
145 {
146  GtkWidget *widget;
147  map<std::string, CEBL::Param>::iterator it;
148  int i;
149  for(it = params.begin(), i = 0; it!=params.end(); ++it,++i)
150  {
151  widget = getWidget(i);
152  if((it->second)==PARAM_BOOLEAN)
153  {
154  (it->second).setBool(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)));
155  }
156  else if((it->second)==PARAM_INTEGER)
157  {
158  (it->second).setInt(int(gtk_spin_button_get_value(GTK_SPIN_BUTTON(widget))));
159  }
160  else if((it->second)==PARAM_DOUBLE)
161  {
162  (it->second).setDouble(gtk_spin_button_get_value(GTK_SPIN_BUTTON(widget)));
163  }
164  else if((it->second)==PARAM_STRING)
165  {
166  (it->second).setString(gtk_entry_get_text(GTK_ENTRY(widget)));
167  }
168  }
169  return params;
170 }
171 
172 
173 
174 //return a widget number
175 GtkWidget *WidgetPanel::getWidget(int i)
176 {
177  return value_widgets.at(i);
178 }
179