File: hkl-gui-sample-reflection.c

package info (click to toggle)
hkl 5.1.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,992 kB
  • sloc: ansic: 57,080; haskell: 5,996; sh: 4,991; cpp: 1,846; makefile: 1,129; python: 925; xml: 76; lisp: 55
file content (334 lines) | stat: -rw-r--r-- 8,807 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* This file is part of the hkl library.
 *
 * The hkl library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * The hkl library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the hkl library.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright (C) 2003-2019, 2022, 2024, 2025 Synchrotron SOLEIL
 *                         L'Orme des Merisiers Saint-Aubin
 *                         BP 48 91192 GIF-sur-YVETTE CEDEX
 *
 * Authors: Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr>
 */

#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <math.h>
#include <stdio.h>

#include <glib.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>

#include "hkl.h"
#include "hkl-gui.h"
#include "hkl-gui-macros.h"

/**********/
/* Sample */
/**********/

enum {
	PROP_0,

	PROP_H,
	PROP_K,
	PROP_L,
	PROP_REFLECTION,

	NUM_PROPERTIES,
};

static GParamSpec *props[NUM_PROPERTIES] = { NULL, };

typedef struct _HklGuiSampleReflection HklGuiSampleReflection;

struct _HklGuiSampleReflection {
	GObject parent_instance;

	/* instance members */
	HklSampleReflection *reflection;
	HklGuiGeometry *geometry;
};

G_DEFINE_FINAL_TYPE(HklGuiSampleReflection, hkl_gui_sample_reflection, G_TYPE_OBJECT);

static void
hkl_gui_sample_reflection_set_property (GObject *object,
					guint prop_id,
					const GValue *value,
					GParamSpec *pspec)
{
	HklGuiSampleReflection *self = HKL_GUI_SAMPLE_REFLECTION (object);

	switch (prop_id)
	{
	case PROP_H:
		hkl_gui_sample_reflection_set_h (self, g_value_get_double (value));
		break;
	case PROP_K:
		hkl_gui_sample_reflection_set_k (self, g_value_get_double (value));
		break;
	case PROP_L:
		hkl_gui_sample_reflection_set_l (self, g_value_get_double (value));
		break;
	case PROP_REFLECTION:
		hkl_gui_sample_reflection_set_reflection (self, g_value_get_pointer (value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
hkl_gui_sample_reflection_get_property (GObject *object,
					guint prop_id,
					GValue *value,
					GParamSpec *pspec)
{
	HklGuiSampleReflection *self = HKL_GUI_SAMPLE_REFLECTION (object);

	switch (prop_id)
	{
	case PROP_H:
		g_value_set_double (value, hkl_gui_sample_reflection_get_h(self));
		break;
	case PROP_K:
		g_value_set_double (value, hkl_gui_sample_reflection_get_k(self));
		break;
	case PROP_L:
		g_value_set_double (value, hkl_gui_sample_reflection_get_l(self));
		break;
	case PROP_REFLECTION:
		g_value_set_pointer (value, hkl_gui_sample_reflection_get_reflection(self));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
hkl_gui_sample_reflection_dispose (GObject *gobject)
{
	/* HklGuiSampleReflection *self = HKL_GUI_SAMPLE_REFLECTION (gobject); */

	/* In dispose(), you are supposed to free all types referenced from this
	 * object which might themselves hold a reference to self. Generally,
	 * the most simple solution is to unref all members on which you own a
	 * reference.
	 */

	/* dispose() might be called multiple times, so we must guard against
	 * calling g_object_unref() on an invalid GObject by setting the member
	 * NULL; g_clear_object() does this for us.
	 */

	/* Always chain up to the parent class; there is no need to check if
	 * the parent class implements the dispose() virtual function: it is
	 * always guaranteed to do so
	 */
	G_OBJECT_CLASS (hkl_gui_sample_reflection_parent_class)->dispose (gobject);
}

static void
hkl_gui_sample_reflection_finalize (GObject *gobject)
{
	//HklGuiSampleReflection *self = HKL_GUI_SAMPLE_REFLECTION(gobject);

	/* Always chain up to the parent class; as with dispose(), finalize()
	 * is guaranteed to exist on the parent's class virtual function table
	 */
	G_OBJECT_CLASS (hkl_gui_sample_reflection_parent_class)->finalize (gobject);
}

static void
hkl_gui_sample_reflection_init(HklGuiSampleReflection *self)
{
	self->reflection = NULL;
	self->geometry = NULL;
}

static void
hkl_gui_sample_reflection_class_init (HklGuiSampleReflectionClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->dispose = hkl_gui_sample_reflection_dispose;
	object_class->finalize = hkl_gui_sample_reflection_finalize;
	object_class->get_property = hkl_gui_sample_reflection_get_property;
	object_class->set_property = hkl_gui_sample_reflection_set_property;

	props[PROP_H] =
		g_param_spec_double ("h",
				     "H",
				     "The h miller coordinate",
				     -G_MAXDOUBLE, G_MAXDOUBLE, 0,
				     G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);

	props[PROP_K] =
		g_param_spec_double ("k",
				     "K",
				     "The k miller coordinate ",
				     -G_MAXDOUBLE, G_MAXDOUBLE, 0,
				     G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);

	props[PROP_L] =
		g_param_spec_double ("l",
				     "L",
				     "The l miller coordinate",
				     -G_MAXDOUBLE, G_MAXDOUBLE, 0,
				     G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);

	props[PROP_REFLECTION] =
		g_param_spec_pointer ("reflection",
				      "Reflection",
				      "A HklSamplereflection",
				      G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);

	g_object_class_install_properties (object_class,
					   NUM_PROPERTIES,
					   props);
}

HklGuiSampleReflection *
hkl_gui_sample_reflection_new(HklSampleReflection *reflection)
{
	return g_object_new (HKL_GUI_TYPE_SAMPLE_REFLECTION,
			     "reflection", reflection,
			     NULL);
}

/* getters */

gdouble
hkl_gui_sample_reflection_get_h(HklGuiSampleReflection* self)
{
	gdouble h, k, l;

	hkl_sample_reflection_hkl_get(self->reflection, &h, &k, &l);

	return h;
}

gdouble
hkl_gui_sample_reflection_get_k(HklGuiSampleReflection* self)
{
	gdouble h, k, l;

	hkl_sample_reflection_hkl_get(self->reflection, &h, &k, &l);

	return k;
}

gdouble
hkl_gui_sample_reflection_get_l(HklGuiSampleReflection* self)
{
	gdouble h, k, l;

	hkl_sample_reflection_hkl_get(self->reflection, &h, &k, &l);

	return l;
}

HklSampleReflection *
hkl_gui_sample_reflection_get_reflection(HklGuiSampleReflection *self)
{
	return self->reflection;
}

/* setters */

void
hkl_gui_sample_reflection_set_h(HklGuiSampleReflection* self, gdouble h_new)
{
	gdouble h, k, l;

	/* TODO error */

	hkl_sample_reflection_hkl_get(self->reflection, &h, &k, &l);

	if (TRUE == hkl_sample_reflection_hkl_set(self->reflection, h_new, k ,l, NULL))
		g_object_notify_by_pspec (G_OBJECT (self), props[PROP_H]);
}

void
hkl_gui_sample_reflection_set_k(HklGuiSampleReflection* self, gdouble k_new)
{
	gdouble h, k, l;

	/* TODO error */

	hkl_sample_reflection_hkl_get(self->reflection, &h, &k, &l);

	if (TRUE == hkl_sample_reflection_hkl_set(self->reflection, h, k_new ,l, NULL))
		g_object_notify_by_pspec (G_OBJECT (self), props[PROP_K]);
}

void
hkl_gui_sample_reflection_set_l(HklGuiSampleReflection* self, gdouble l_new)
{
	gdouble h, k, l;

	/* TODO error */

	hkl_sample_reflection_hkl_get(self->reflection, &h, &k, &l);

	if (TRUE == hkl_sample_reflection_hkl_set(self->reflection, h, k ,l_new, NULL))
		g_object_notify_by_pspec (G_OBJECT (self), props[PROP_L]);
}

void
hkl_gui_sample_reflection_set_reflection(HklGuiSampleReflection *self, HklSampleReflection *reflection)
{
	self->reflection = reflection;
	self->geometry = hkl_gui_geometry_new(hkl_sample_reflection_geometry_get(reflection));

	g_object_notify_by_pspec (G_OBJECT (self), props[PROP_REFLECTION]);
}


/*********************/
/* List item factory */
/*********************/


static void
bind_label__sample_reflection_geometry_axis_value_cb (GtkListItemFactory *factory,
						      GtkListItem *list_item,
						      gpointer user_data)
{
	GtkWidget *label;
	HklGuiSampleReflection *self;
	gint idx = GPOINTER_TO_INT(user_data);

	label = gtk_list_item_get_child (list_item);
	self = gtk_list_item_get_item (list_item);

	g_return_if_fail(NULL != self->geometry);

	set_label_from_double(label, hkl_gui_geometry_get_axis_value(self->geometry, idx));
}


GtkListItemFactory *
hkl_gui_item_factory_new_label__sample_reflection_geometry_axis(gint idx)
{
	GtkListItemFactory *factory = gtk_signal_list_item_factory_new ();
	g_signal_connect (factory, "setup", G_CALLBACK (hkl_gui_setup_item_factory_label_cb), NULL);
	g_signal_connect (factory, "bind", G_CALLBACK (bind_label__sample_reflection_geometry_axis_value_cb), GINT_TO_POINTER(idx));

	return factory;
}