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"
32 G_DEFINE_TYPE (HklGuiEngine
, hkl_gui_engine
, G_TYPE_OBJECT
);
43 /* Keep a pointer to the properties definition */
44 static GParamSpec
*obj_properties
[N_PROPERTIES
] = { NULL
, };
52 static guint signals
[N_SIGNALS
] = { 0 };
54 struct _HklGuiEnginePrivate
{
62 GtkComboBox
* combobox1
;
63 GtkExpander
* expander1
;
64 GtkTreeView
* treeview1
;
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))
75 set_property (GObject
*object
, guint prop_id
,
76 const GValue
*value
, GParamSpec
*pspec
)
78 HklGuiEngine
*self
= HKL_GUI_ENGINE (object
);
82 hkl_gui_engine_set_engine(self
, g_value_get_pointer (value
));
85 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
91 get_property (GObject
*object
, guint prop_id
,
92 GValue
*value
, GParamSpec
*pspec
)
94 HklGuiEngine
*self
= HKL_GUI_ENGINE (object
);
99 g_value_set_pointer (value
, hkl_gui_engine_get_engine (self
));
102 g_value_set_object(value
, hkl_gui_engine_get_liststore (self
));
105 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
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
);
122 hkl_gui_engine_new (HklEngine
* engine
)
124 return g_object_new (HKL_GUI_TYPE_ENGINE
,
131 hkl_gui_engine_get_engine (HklGuiEngine
*self
)
133 HklGuiEnginePrivate
*priv
= HKL_GUI_ENGINE_GET_PRIVATE(self
);
140 hkl_gui_engine_get_liststore (HklGuiEngine
*self
)
142 HklGuiEnginePrivate
*priv
= HKL_GUI_ENGINE_GET_PRIVATE(self
);
144 return priv
->store_pseudo
;
148 hkl_gui_engine_get_frame(HklGuiEngine
*self
)
150 HklGuiEnginePrivate
*priv
= HKL_GUI_ENGINE_GET_PRIVATE(self
);
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
;
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
);
175 for(i
=0; i
<n_values
; ++i
){
176 gtk_list_store_append (priv
->store_pseudo
,
178 gtk_list_store_set (priv
->store_pseudo
,
180 PSEUDO_COL_NAME
, darray_item(*parameters
, i
),
182 PSEUDO_COL_VALUE
, values
[i
],
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
;
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
,
204 gtk_list_store_set (priv
->store_mode
,
206 MODE_COL_NAME
, *mode
,
208 if(*mode
== hkl_engine_current_mode_get(priv
->engine
))
212 /* now set the active row with the current mode */
213 gtk_combo_box_set_active_iter(priv
->combobox1
, ¤t
);
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
];
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
,
235 PSEUDO_COL_NAME
, darray_item(*parameters
, i
),
237 PSEUDO_COL_VALUE
, values
[i
],
240 gtk_expander_set_expanded (priv
->expander1
, TRUE
);
241 gtk_widget_show (GTK_WIDGET (priv
->expander1
));
243 gtk_widget_hide (GTK_WIDGET (priv
->expander1
));
248 hkl_gui_engine_update (HklGuiEngine
* self
)
250 HklGuiEnginePrivate
*priv
= HKL_GUI_ENGINE_GET_PRIVATE(self
);
251 GtkTreeViewColumn
* col
;
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
);
265 void hkl_gui_engine_set_engine (HklGuiEngine
*self
,
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
);
280 update_mode_parameters (self
);
282 hkl_gui_engine_update(self
);
286 combobox1_changed_cb (GtkComboBox
* combobox
, HklGuiEngine
* self
)
288 HklGuiEnginePrivate
*priv
= HKL_GUI_ENGINE_GET_PRIVATE(self
);
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
),
298 MODE_COL_NAME
, &mode
,
300 if(!hkl_engine_current_mode_set(priv
->engine
, mode
, NULL
))
302 update_mode_parameters(self
);
307 button1_clicked_cb (GtkButton
* button
, HklGuiEngine
* self
)
309 g_signal_emit(self
, signals
[CHANGED
], 0);
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
);
328 cell_tree_view_pseudo_axis_value_edited_cb (GtkCellRendererText
* renderer
,
330 const gchar
* new_text
,
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
),
340 value
= atof(new_text
);
341 g_object_set (G_OBJECT(renderer
), "background", "red", NULL
, NULL
);
342 gtk_list_store_set (priv
->store_pseudo
,
344 PSEUDO_COL_VALUE
, value
,
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
;
361 obj_properties
[PROP_ENGINE
] =
362 g_param_spec_pointer ("engine",
364 "The Hkl Engine used underneath",
365 G_PARAM_CONSTRUCT_ONLY
|
367 G_PARAM_STATIC_STRINGS
);
369 obj_properties
[PROP_LISTSTORE
] =
370 g_param_spec_object ("liststore",
372 "The liststore contaning all the pseudo axes values",
375 G_PARAM_STATIC_STRINGS
);
377 g_object_class_install_properties (gobject_class
,
383 g_signal_new ("changed",
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
,
402 (GCallback
) cell_tree_view_pseudo_axis_value_edited_cb
,
406 static void hkl_gui_engine_init (HklGuiEngine
* self
)
408 HklGuiEnginePrivate
*priv
= HKL_GUI_ENGINE_GET_PRIVATE(self
);
410 GtkTreeViewColumn
* col
;
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
,
434 (GCallback
) combobox1_changed_cb
,
437 g_signal_connect_object (priv
->button1
,
439 (GCallback
) button1_clicked_cb
,
442 g_signal_connect_object (priv
->button2
,
444 (GCallback
) button2_clicked_cb
,
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
);