[hkl] remove a bunch of warnings.
[hkl.git] / gui / hkl-gui-pseudoaxes.c
blob007ec6df242fd7a9a49a5c390216fff56f1c13dd
1 /* This file is part of the hkl library.
3 * The hkl library is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * The hkl library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with the hkl library. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright (C) 2003-2013 Synchrotron SOLEIL
17 * L'Orme des Merisiers Saint-Aubin
18 * BP 48 91192 GIF-sur-YVETTE CEDEX
20 * Authors: Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr>
23 #include "hkl-gui-macros.h"
24 #include "hkl-gui-pseudoaxes.h"
25 #include "hkl.h"
26 #include <gtk/gtk.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <float.h>
30 #include <math.h>
32 G_DEFINE_TYPE (HklGuiEngine, hkl_gui_engine, G_TYPE_OBJECT);
34 enum {
35 PROP_0,
37 PROP_ENGINE,
38 PROP_LISTSTORE,
40 N_PROPERTIES
43 /* Keep a pointer to the properties definition */
44 static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
46 enum {
47 CHANGED,
49 N_SIGNALS
52 static guint signals[N_SIGNALS] = { 0 };
54 struct _HklGuiEnginePrivate {
55 /* Properties */
56 HklEngine* engine;
57 /* Properties */
59 GtkBuilder *builder;
60 GtkFrame* frame1;
61 GtkLabel* label2;
62 GtkComboBox* combobox1;
63 GtkExpander* expander1;
64 GtkTreeView* treeview1;
65 GtkButton* button1;
66 GtkButton* button2;
67 GtkListStore* store_mode;
68 GtkListStore* store_pseudo;
69 GtkListStore* store_mode_parameter;
72 #define HKL_GUI_ENGINE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), HKL_GUI_TYPE_ENGINE, HklGuiEnginePrivate))
74 static void
75 set_property (GObject *object, guint prop_id,
76 const GValue *value, GParamSpec *pspec)
78 HklGuiEngine *self = HKL_GUI_ENGINE (object);
80 switch (prop_id) {
81 case PROP_ENGINE:
82 hkl_gui_engine_set_engine(self, g_value_get_pointer (value));
83 break;
84 default:
85 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
86 break;
90 static void
91 get_property (GObject *object, guint prop_id,
92 GValue *value, GParamSpec *pspec)
94 HklGuiEngine *self = HKL_GUI_ENGINE (object);
96 switch (prop_id)
98 case PROP_ENGINE:
99 g_value_set_pointer (value, hkl_gui_engine_get_engine (self));
100 break;
101 case PROP_LISTSTORE:
102 g_value_set_object(value, hkl_gui_engine_get_liststore (self));
103 break;
104 default:
105 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
106 break;
110 static void
111 finalize (GObject* object)
113 HklGuiEnginePrivate *priv = HKL_GUI_ENGINE_GET_PRIVATE(object);
115 g_object_unref(priv->builder);
117 G_OBJECT_CLASS (hkl_gui_engine_parent_class)->finalize (object);
121 HklGuiEngine*
122 hkl_gui_engine_new (HklEngine* engine)
124 return g_object_new (HKL_GUI_TYPE_ENGINE,
125 "engine", engine,
126 NULL);
130 HklEngine*
131 hkl_gui_engine_get_engine (HklGuiEngine *self)
133 HklGuiEnginePrivate *priv = HKL_GUI_ENGINE_GET_PRIVATE(self);
135 return priv->engine;
139 GtkListStore*
140 hkl_gui_engine_get_liststore (HklGuiEngine *self)
142 HklGuiEnginePrivate *priv = HKL_GUI_ENGINE_GET_PRIVATE(self);
144 return priv->store_pseudo;
147 GtkFrame*
148 hkl_gui_engine_get_frame(HklGuiEngine *self)
150 HklGuiEnginePrivate *priv = HKL_GUI_ENGINE_GET_PRIVATE(self);
152 return priv->frame1;
155 static void
156 update_pseudo_axis (HklGuiEngine* self)
158 HklGuiEnginePrivate *priv = HKL_GUI_ENGINE_GET_PRIVATE(self);
159 GtkTreeIter iter = {0};
160 const darray_string *parameters = hkl_engine_pseudo_axis_names_get (priv->engine);
161 unsigned int n_values = darray_size(*parameters);
162 double values[n_values];
163 GError *error = NULL;
164 unsigned int i;
166 g_return_if_fail (self != NULL);
168 gtk_list_store_clear (priv->store_pseudo);
169 if(!hkl_engine_pseudo_axis_values_get(priv->engine, values, n_values, HKL_UNIT_USER, &error)){
170 /* TODO check for the error */
171 g_clear_error(&error);
172 return;
175 for(i=0; i<n_values; ++i){
176 gtk_list_store_append (priv->store_pseudo,
177 &iter);
178 gtk_list_store_set (priv->store_pseudo,
179 &iter,
180 PSEUDO_COL_NAME, darray_item(*parameters, i),
181 PSEUDO_COL_IDX, i,
182 PSEUDO_COL_VALUE, values[i],
183 -1);
188 static void
189 update_mode (HklGuiEngine* self)
191 HklGuiEnginePrivate *priv = HKL_GUI_ENGINE_GET_PRIVATE(self);
192 GtkTreeIter iter = {0};
193 GtkTreeIter current = {0};
194 const darray_string *modes;
195 const char **mode;
197 g_return_if_fail (self != NULL);
199 modes = hkl_engine_modes_names_get(priv->engine);
200 gtk_list_store_clear (priv->store_mode);
201 darray_foreach(mode, *modes){
202 gtk_list_store_append (priv->store_mode,
203 &iter);
204 gtk_list_store_set (priv->store_mode,
205 &iter,
206 MODE_COL_NAME, *mode,
207 -1);
208 if(*mode == hkl_engine_current_mode_get(priv->engine))
209 current = iter;
212 /* now set the active row with the current mode */
213 gtk_combo_box_set_active_iter(priv->combobox1, &current);
217 static void
218 update_mode_parameters (HklGuiEngine* self)
220 HklGuiEnginePrivate *priv = HKL_GUI_ENGINE_GET_PRIVATE(self);
221 const darray_string *parameters = hkl_engine_parameters_names_get(priv->engine);
222 unsigned int n_values = darray_size(*parameters);
223 double values[n_values];
224 unsigned int i;
226 if(n_values){
227 GtkTreeIter iter = {0};
229 gtk_list_store_clear (priv->store_mode_parameter);
230 hkl_engine_parameters_values_get(priv->engine, values, n_values, HKL_UNIT_USER);
231 for(i=0; i<n_values; ++i){
232 gtk_list_store_append (priv->store_mode_parameter, &iter);
233 gtk_list_store_set (priv->store_mode_parameter,
234 &iter,
235 PSEUDO_COL_NAME, darray_item(*parameters, i),
236 PSEUDO_COL_IDX, i,
237 PSEUDO_COL_VALUE, values[i],
238 -1);
240 gtk_expander_set_expanded (priv->expander1, TRUE);
241 gtk_widget_show (GTK_WIDGET (priv->expander1));
242 }else
243 gtk_widget_hide (GTK_WIDGET (priv->expander1));
247 void
248 hkl_gui_engine_update (HklGuiEngine* self)
250 HklGuiEnginePrivate *priv = HKL_GUI_ENGINE_GET_PRIVATE(self);
251 GtkTreeViewColumn* col;
252 GList* cells;
254 g_return_if_fail (self != NULL);
256 update_pseudo_axis (self);
258 col = gtk_tree_view_get_column(priv->treeview1, 1);
259 cells = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(col));
260 g_object_set (G_OBJECT(cells->data), "background", NULL, NULL);
261 g_list_free(cells);
265 void hkl_gui_engine_set_engine (HklGuiEngine *self,
266 HklEngine *engine)
268 HklGuiEnginePrivate *priv = HKL_GUI_ENGINE_GET_PRIVATE(self);
270 g_return_if_fail (self != NULL);
271 g_return_if_fail (engine != NULL);
273 priv->engine = engine;
275 gtk_label_set_label (priv->label2,
276 hkl_engine_name_get(priv->engine));
278 update_pseudo_axis (self);
279 update_mode (self);
280 update_mode_parameters (self);
282 hkl_gui_engine_update(self);
285 static void
286 combobox1_changed_cb (GtkComboBox* combobox, HklGuiEngine* self)
288 HklGuiEnginePrivate *priv = HKL_GUI_ENGINE_GET_PRIVATE(self);
289 gchar *mode;
290 GtkTreeIter iter = {0};
292 g_return_if_fail (self != NULL);
293 g_return_if_fail (combobox != NULL);
295 if(gtk_combo_box_get_active_iter(combobox, &iter)){
296 gtk_tree_model_get(GTK_TREE_MODEL(priv->store_mode),
297 &iter,
298 MODE_COL_NAME, &mode,
299 -1);
300 if(!hkl_engine_current_mode_set(priv->engine, mode, NULL))
301 return;
302 update_mode_parameters(self);
306 static void
307 button1_clicked_cb (GtkButton* button, HklGuiEngine* self)
309 g_signal_emit(self, signals[CHANGED], 0);
313 static void
314 button2_clicked_cb (GtkButton* button, HklGuiEngine* self)
316 HklGuiEnginePrivate *priv = HKL_GUI_ENGINE_GET_PRIVATE(self);
318 if (HKL_ENGINE_CAPABILITIES_INITIALIZABLE & hkl_engine_capabilities_get(priv->engine)){
319 if(hkl_engine_initialized_set(priv->engine, TRUE, NULL)){
320 /* some init method update the parameters */
321 update_mode_parameters(self);
327 static void
328 cell_tree_view_pseudo_axis_value_edited_cb (GtkCellRendererText* renderer,
329 const gchar* path,
330 const gchar* new_text,
331 HklGuiEngine* self)
333 HklGuiEnginePrivate *priv = HKL_GUI_ENGINE_GET_PRIVATE(self);
334 GtkTreeIter iter = {0};
336 if (gtk_tree_model_get_iter_from_string (GTK_TREE_MODEL(priv->store_pseudo),
337 &iter, path)) {
338 gdouble value = 0.0;
340 value = atof(new_text);
341 g_object_set (G_OBJECT(renderer), "background", "red", NULL, NULL);
342 gtk_list_store_set (priv->store_pseudo,
343 &iter,
344 PSEUDO_COL_VALUE, value,
345 -1);
349 static void hkl_gui_engine_class_init (HklGuiEngineClass * class)
351 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
353 g_type_class_add_private (class, sizeof (HklGuiEnginePrivate));
355 /* virtual methods */
356 gobject_class->finalize = finalize;
357 gobject_class->set_property = set_property;
358 gobject_class->get_property = get_property;
360 /* properties */
361 obj_properties[PROP_ENGINE] =
362 g_param_spec_pointer ("engine",
363 "Engine",
364 "The Hkl Engine used underneath",
365 G_PARAM_CONSTRUCT_ONLY |
366 G_PARAM_READWRITE |
367 G_PARAM_STATIC_STRINGS);
369 obj_properties[PROP_LISTSTORE] =
370 g_param_spec_object ("liststore",
371 "Liststore",
372 "The liststore contaning all the pseudo axes values",
373 GTK_TYPE_LIST_STORE,
374 G_PARAM_READABLE |
375 G_PARAM_STATIC_STRINGS);
377 g_object_class_install_properties (gobject_class,
378 N_PROPERTIES,
379 obj_properties);
381 /* signals */
382 signals[CHANGED] =
383 g_signal_new ("changed",
384 HKL_GUI_TYPE_ENGINE,
385 G_SIGNAL_RUN_LAST,
386 0, /* class offset */
387 NULL, /* accumulator */
388 NULL, /* accu_data */
389 g_cclosure_marshal_VOID__VOID,
390 G_TYPE_NONE, /* return_type */
395 static void _connect_renderer(gpointer data, gpointer user_data)
397 GtkCellRenderer *renderer = GTK_CELL_RENDERER(data);
398 HklGuiEngine *self = HKL_GUI_ENGINE(user_data);
400 g_signal_connect_object (renderer,
401 "edited",
402 (GCallback) cell_tree_view_pseudo_axis_value_edited_cb,
403 self, 0);
406 static void hkl_gui_engine_init (HklGuiEngine * self)
408 HklGuiEnginePrivate *priv = HKL_GUI_ENGINE_GET_PRIVATE(self);
409 GtkBuilder *builder;
410 GtkTreeViewColumn* col;
411 GList* cells;
413 priv->engine = NULL;
414 priv->builder = builder = gtk_builder_new ();
416 get_ui(builder, "pseudo.ui");
418 get_object(builder, GTK_FRAME, priv, frame1);
419 get_object(builder, GTK_LABEL, priv, label2);
420 get_object(builder, GTK_COMBO_BOX, priv, combobox1);
421 get_object(builder, GTK_EXPANDER, priv, expander1);
422 get_object(builder, GTK_TREE_VIEW, priv, treeview1);
423 get_object(builder, GTK_BUTTON, priv, button1);
424 get_object(builder, GTK_BUTTON, priv, button2);
426 get_object(builder, GTK_LIST_STORE, priv, store_mode);
427 get_object(builder, GTK_LIST_STORE, priv, store_pseudo);
428 get_object(builder, GTK_LIST_STORE, priv, store_mode_parameter);
430 gtk_builder_connect_signals (builder, self);
432 g_signal_connect_object (priv->combobox1,
433 "changed",
434 (GCallback) combobox1_changed_cb,
435 self, 0);
437 g_signal_connect_object (priv->button1,
438 "clicked",
439 (GCallback) button1_clicked_cb,
440 self, 0);
442 g_signal_connect_object (priv->button2,
443 "clicked",
444 (GCallback) button2_clicked_cb,
445 self, 0);
447 col = gtk_tree_view_get_column (priv->treeview1, 1);
448 cells = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(col));
449 g_list_foreach(cells, _connect_renderer, self);
450 g_list_free(cells);