CEBL  2.1
InterfaceConfigurationWindow.cpp
Go to the documentation of this file.
1 
9 #include "CEBLViewGTK.hpp"
10 #include "WidgetPanel.hpp"
11 #include "InterfaceCombo.hpp"
13 #include "../Exceptions.hpp"
14 
15 //----------------------------------------------------------------------
16 // Constructors / Destructors
17 
19 {
20  this->interface_parameters_panel = NULL;
21  this->view = view;
22  this->window_created = false;
23  this->window_open = false;
24  this->updating_view = false;
25 }
26 
28 {
29 
30 }
31 
32 //----------------------------------------------------------------------
33 
34 
36 {
37  GtkWidget* btn = gtk_button_new_with_label("Open Interface Configuration");
38  this->open_close_buttons.push_back(btn);
39  g_signal_connect(G_OBJECT(btn),
40  "clicked",
41  G_CALLBACK(CB_openCloseWindow),
42  (gpointer) this);
43  return btn;
44 }
45 
46 
47 
48 //----------------------------------------------------------------------
49 
50 void InterfaceConfigurationWindow::createWindow()
51 {
52  if(!this->window_created)
53  {
54  int num_classes = this->view->getModel()->trainingGetNumClasses();
55  EEGInterface * interface = this->view->getInterfaceCombo()->getInterface();
56  GtkWidget * main_window = this->view->getMainWindow();
57  std::vector<string> class_labels = this->view->getModel()->trainingGetClassLabels();
58 
59  // create window
60  this->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
61 
62  // set window options
63  gtk_window_set_title(GTK_WINDOW(window), "Configure Interface");
64  gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER_ON_PARENT);
65  gtk_window_set_transient_for(GTK_WINDOW(window),GTK_WINDOW(main_window));
66  gtk_window_set_destroy_with_parent(GTK_WINDOW(window), true);
67  gtk_window_set_default_size(GTK_WINDOW(window),200,0);
68 
69  //main container
70  GtkWidget *container = gtk_vbox_new(false, 0);
71  gtk_container_add(GTK_CONTAINER(window), container);
72 
73  g_signal_connect(G_OBJECT(window), "delete_event",
74  G_CALLBACK(CB_windowDelete), this);
75  //Top Decoration
76  GtkWidget *params_label_box = gtk_hbox_new(false, 0);
77  GtkWidget *classes_label_box = gtk_hbox_new(false, 0);
78 
79  //preferences icon
80  GtkWidget *pref_image;
81  pref_image = gtk_image_new_from_stock(GTK_STOCK_PREFERENCES,GTK_ICON_SIZE_LARGE_TOOLBAR);
82  gtk_box_pack_start(GTK_BOX(params_label_box), pref_image, false, true, 0);
83 
84  //--------------------------------------------------
85  //PARAMETERS
86  GtkWidget *param_text;
87  param_text = gtk_label_new("");
88  gtk_label_set_markup(GTK_LABEL(param_text),"<b><big>Interface Parameters</big></b>");
89  gtk_box_pack_start(GTK_BOX(params_label_box), param_text, false, true, 0);
90 
91  //add boxes
92  gtk_box_pack_start(GTK_BOX(container), params_label_box, false, false, 0);
93  gtk_box_pack_start(GTK_BOX(container), gtk_hseparator_new(), false, false, 0);
94 
95  //-------------------------------------------------
96  //config
97  gtk_box_pack_start(GTK_BOX(container),gtk_hseparator_new(), false, false, 0);
98  gtk_box_pack_start(GTK_BOX(container),gtk_label_new("Interface Params"), false, false, 0);
99 
100  //Add parameters if there is a feature extractor
101  GtkWidget *panel_container = gtk_hbox_new(false,0);
102  if(interface != NULL)
103  {
104  if(interface_parameters_panel!=NULL)
105  delete interface_parameters_panel;
106  std::map<std::string, CEBL::Param> params = interface->getParamsList();
107  interface_parameters_panel = new WidgetPanel(params);
108 
109  if(params.size() > 0)
110  panel_container = interface_parameters_panel->getContainer();
111  else
112  panel_container = gtk_label_new("No options for selected interface.");
113  }
114  gtk_box_pack_start(GTK_BOX(container),panel_container, false, false, 0);
115 
116  //--------------------------------------------------
117  //CLASSES
118  GtkWidget *pref_text;
119  pref_text = gtk_label_new("");
120  gtk_label_set_markup(GTK_LABEL(pref_text),"<b><big>Classes</big></b>");
121  gtk_box_pack_start(GTK_BOX(classes_label_box), pref_text, false, true, 0);
122 
123  //add boxes
124  gtk_box_pack_start(GTK_BOX(container), classes_label_box, false, false, 0);
125  gtk_box_pack_start(GTK_BOX(container), gtk_hseparator_new(), false, false, 0);
126 
127  //ENTRY BOXES
128 
129  //vector of gtk widgets
130  class_labels_entries.resize(num_classes);
131 
132  //loop through each class
133  for(int i=0; i<num_classes;i++)
134  {
135  GtkWidget *box = gtk_hbutton_box_new();
136  gtk_button_box_set_layout(GTK_BUTTON_BOX(box),GTK_BUTTONBOX_START);
137 
138  std::stringstream l;
139  l << "Class " << (i);
140  std::string label = l.str();
141  gtk_box_pack_start(GTK_BOX(box),gtk_label_new(label.c_str()), false, false, 0);
142  //create entry
143  class_labels_entries[i] = gtk_entry_new();
144  gtk_entry_set_text(GTK_ENTRY(class_labels_entries[i]), class_labels[i].c_str());
145  gtk_box_pack_start(GTK_BOX(box),class_labels_entries[i], true, true, 0);
146 
147  gtk_box_pack_start(GTK_BOX(container),box, true, true, 0);
148  }
149 
150 
151  //OK BUTTON
152  GtkWidget *btn_ok = gtk_button_new_from_stock(GTK_STOCK_OK);
153  g_signal_connect(G_OBJECT(btn_ok), "clicked",
154  G_CALLBACK(CB_windowClose), this);
155  gtk_box_pack_start(GTK_BOX(container), btn_ok, false, false, 0);
156 
157  //SHOW WINDOW
158  this->window_created = true;
159  }
160 }
161 
162 
163 void InterfaceConfigurationWindow::closeWindow()
164 {
165  if(this->window_open && this->window_created)
166  {
167  gtk_widget_hide(this->window);
168  this->window_open = false;
169  this->applyConfig();
170  }
171 }
172 
173 void InterfaceConfigurationWindow::openWindow()
174 {
175  this->updateView();
176  if(!this->window_open)
177  {
178  if(!this->window_created)
179  {
180  this->createWindow();
181  }
182  gtk_widget_show_all(this->window);
183  this->window_open = true;
184  }
185 }
186 
187 
188 //----------------------------------------------------------------------
189 
190 void InterfaceConfigurationWindow::applyConfig()
191 {
192  int num_classes = this->view->getModel()->trainingGetNumClasses();
193  EEGInterface * interface = this->view->getInterfaceCombo()->getInterface();
194 
195  this->class_labels.resize(num_classes);
196  for(unsigned i=0; i<class_labels_entries.size();i++)
197  {
198  if(int(i) < num_classes)
199  class_labels[i] = gtk_entry_get_text(GTK_ENTRY(class_labels_entries[i]));
200  }
201 
202  //update model
203  this->view->getModel()->trainingSetClassLabels(class_labels);
204 
205  //update interface
206  if(interface!=NULL)
207  {
208  if(interface_parameters_panel!=NULL)
209  interface->setParamsList(interface_parameters_panel->getParams());
210  interface->setClassLabels(class_labels);
211  }
212 
213 
214 }
215 
216 //----------------------------------------------------------------------
217 
219 {
220  this->updating_view = true;
221  if(this->window_created)
222  {
223  gtk_widget_destroy(this->window);
224  this->window = NULL;
225  this->window_created = false;
226  this->createWindow();
227  if(this->window_open)
228  {
229  gtk_widget_show_all(this->window);
230  }
231  }
232  this->updating_view = false;
233 }
234 
235 //----------------------------------------------------------------------
236 // CALLBACKS
237 
238 void InterfaceConfigurationWindow::CB_openCloseWindow(GtkWidget *widget, gpointer data)
239 {
241 
242  if(iw->window_open)
243  {
244  iw->closeWindow();
245  }
246  else
247  {
248  iw->openWindow();
249  }
250 }
251 
252 gboolean InterfaceConfigurationWindow::CB_windowDelete(GtkWidget *widget, GdkEvent *event, gpointer data)
253 {
255  iw->closeWindow();
256  return true;
257 }
258 
259 void InterfaceConfigurationWindow::CB_windowClose(GtkWidget *widget, gpointer data)
260 {
262  iw->closeWindow();
263 }
264