summaryrefslogtreecommitdiff
path: root/src/ld-diagram.c
blob: fb0255e131fc08f927ad9effdde45ab85b04027e (plain)
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
 * ld-diagram.c
 *
 * This file is a part of logdiag.
 * Copyright Přemysl Janouch 2010 - 2011. All rights reserved.
 *
 * See the file LICENSE for licensing information.
 *
 */

#include <gtk/gtk.h>
#include <json-glib/json-glib.h>

#include "config.h"

#include "ld-diagram-object.h"
#include "ld-diagram.h"


/**
 * SECTION:ld-diagram
 * @short_description: A diagram object.
 * @see_also: #LdCanvas
 *
 * #LdDiagram is a model for storing diagrams.
 */

/*
 * LdDiagramPrivate:
 * @modified: Whether the diagram has been modified.
 * @objects: All the objects in the diagram.
 * @selection: All currently selected objects.
 * @connections: Connections between objects.
 */
struct _LdDiagramPrivate
{
	gboolean modified;

	GSList *objects;
	GSList *selection;
	GSList *connections;
};

enum
{
	PROP_0,
	PROP_MODIFIED
};

static void ld_diagram_get_property (GObject *object, guint property_id,
	GValue *value, GParamSpec *pspec);
static void ld_diagram_set_property (GObject *object, guint property_id,
	const GValue *value, GParamSpec *pspec);
static void ld_diagram_dispose (GObject *gobject);
static void ld_diagram_finalize (GObject *gobject);

static void ld_diagram_real_changed (LdDiagram *self);
static void ld_diagram_clear_internal (LdDiagram *self);
static void ld_diagram_unselect_all_internal (LdDiagram *self);


G_DEFINE_TYPE (LdDiagram, ld_diagram, G_TYPE_OBJECT);

static void
ld_diagram_class_init (LdDiagramClass *klass)
{
	GObjectClass *object_class;
	GParamSpec *pspec;

	object_class = G_OBJECT_CLASS (klass);
	object_class->get_property = ld_diagram_get_property;
	object_class->set_property = ld_diagram_set_property;
	object_class->dispose = ld_diagram_dispose;
	object_class->finalize = ld_diagram_finalize;

	klass->changed = ld_diagram_real_changed;

/**
 * LdDiagram:modified:
 *
 * Whether the diagram has been modified.
 */
	pspec = g_param_spec_boolean ("modified", "Modified",
		"Whether the diagram has been modified.",
		FALSE, G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_MODIFIED, pspec);

/**
 * LdDiagram::changed:
 * @diagram: The diagram object.
 *
 * Contents of the diagram have changed.
 */
	klass->changed_signal = g_signal_new
		("changed", G_TYPE_FROM_CLASS (klass),
		G_SIGNAL_RUN_LAST,
		G_STRUCT_OFFSET (LdDiagramClass, changed), NULL, NULL,
		g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);

/**
 * LdDiagram::selection-changed:
 * @diagram: The diagram object.
 *
 * The current selection has changed.
 */
	klass->selection_changed_signal = g_signal_new
		("selection-changed", G_TYPE_FROM_CLASS (klass),
		G_SIGNAL_RUN_LAST,
		G_STRUCT_OFFSET (LdDiagramClass, selection_changed), NULL, NULL,
		g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);

	g_type_class_add_private (klass, sizeof (LdDiagramPrivate));
}

static void
ld_diagram_init (LdDiagram *self)
{
	self->priv = G_TYPE_INSTANCE_GET_PRIVATE
		(self, LD_TYPE_DIAGRAM, LdDiagramPrivate);
}

static void
ld_diagram_get_property (GObject *object, guint property_id,
	GValue *value, GParamSpec *pspec)
{
	LdDiagram *self;

	self = LD_DIAGRAM (object);
	switch (property_id)
	{
	case PROP_MODIFIED:
		g_value_set_boolean (value, ld_diagram_get_modified (self));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}

static void
ld_diagram_set_property (GObject *object, guint property_id,
	const GValue *value, GParamSpec *pspec)
{
	LdDiagram *self;

	self = LD_DIAGRAM (object);
	switch (property_id)
	{
	case PROP_MODIFIED:
		ld_diagram_set_modified (self, g_value_get_boolean (value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}

static void
ld_diagram_dispose (GObject *gobject)
{
	LdDiagram *self;

	self = LD_DIAGRAM (gobject);
	ld_diagram_clear_internal (self);

	/* Chain up to the parent class. */
	G_OBJECT_CLASS (ld_diagram_parent_class)->dispose (gobject);
}

static void
ld_diagram_finalize (GObject *gobject)
{
	/* Chain up to the parent class. */
	G_OBJECT_CLASS (ld_diagram_parent_class)->finalize (gobject);
}

static void
ld_diagram_real_changed (LdDiagram *self)
{
	g_return_if_fail (LD_IS_DIAGRAM (self));

	ld_diagram_set_modified (self, TRUE);
}


/**
 * ld_diagram_new:
 *
 * Create an instance.
 */
LdDiagram *
ld_diagram_new (void)
{
	return g_object_new (LD_TYPE_DIAGRAM, NULL);
}

/**
 * ld_diagram_clear:
 * @self: An #LdDiagram object.
 *
 * Clear the whole diagram with it's objects and selection.
 */
void
ld_diagram_clear (LdDiagram *self)
{
	g_return_if_fail (LD_IS_DIAGRAM (self));

	ld_diagram_clear_internal (self);

	g_signal_emit (self,
		LD_DIAGRAM_GET_CLASS (self)->changed_signal, 0);
}

/*
 * ld_diagram_clear_internal:
 * @self: An #LdDiagram object.
 *
 * Do the same what ld_diagram_clear() does but don't emit signals.
 */
static void
ld_diagram_clear_internal (LdDiagram *self)
{
	ld_diagram_unselect_all (self);

	g_slist_free (self->priv->connections);
	self->priv->connections = NULL;

	g_slist_foreach (self->priv->objects, (GFunc) g_object_unref, NULL);
	g_slist_free (self->priv->objects);
	self->priv->objects = NULL;
}

/**
 * ld_diagram_load_from_file:
 * @self: An #LdDiagram object.
 * @filename: A filename.
 * @error: Return location for a GError, or NULL.
 *
 * Load a file into the diagram.
 *
 * Return value: TRUE if the file could be loaded, FALSE otherwise.
 */
gboolean
ld_diagram_load_from_file (LdDiagram *self,
	const gchar *filename, GError **error)
{
	JsonParser *parser;
	GError *json_error;

	g_return_val_if_fail (LD_IS_DIAGRAM (self), FALSE);
	g_return_val_if_fail (filename != NULL, FALSE);

	/* TODO: Implement loading for real. This is just a stub. */
	parser = json_parser_new ();

	json_error = NULL;
	json_parser_load_from_file (parser, filename, &json_error);
	if (json_error)
	{
		g_propagate_error (error, json_error);
		g_object_unref (parser);
		return FALSE;
	}

	ld_diagram_clear (self);
	g_object_unref (parser);
	return TRUE;
}

/**
 * ld_diagram_save_to_file:
 * @self: An #LdDiagram object.
 * @filename: A filename.
 * @error: Return location for a GError, or NULL.
 *
 * Save the diagram into a file.
 *
 * Return value: TRUE if the diagram could be saved, FALSE otherwise.
 */
gboolean
ld_diagram_save_to_file (LdDiagram *self,
	const gchar *filename, GError **error)
{
	JsonGenerator *generator;
	JsonNode *root;
	GError *json_error;

	g_return_val_if_fail (LD_IS_DIAGRAM (self), FALSE);
	g_return_val_if_fail (filename != NULL, FALSE);

	/* TODO: Implement saving for real. This is just a stub. */
	generator = json_generator_new ();
	g_object_set (generator, "pretty", TRUE, NULL);

	/* XXX: json-glib dislikes empty objects. */
	root = json_node_new (JSON_NODE_OBJECT);
	json_generator_set_root (generator, root);
	json_node_free (root);

	json_error = NULL;
	json_generator_to_file (generator, filename, &json_error);
	if (json_error)
	{
		g_propagate_error (error, json_error);
		g_object_unref (generator);
		return FALSE;
	}
	g_object_unref (generator);
	return TRUE;
}

/**
 * ld_diagram_get_modified:
 * @self: An #LdDiagram object.
 *
 * Return value: The modification status of diagram.
 */
gboolean
ld_diagram_get_modified (LdDiagram *self)
{
	g_return_val_if_fail (LD_IS_DIAGRAM (self), FALSE);
	return self->priv->modified;
}

/**
 * ld_diagram_set_modified:
 * @self: An #LdDiagram object.
 * @value: Whether the diagram has been modified.
 *
 * Set the modification status of diagram.
 */
void
ld_diagram_set_modified (LdDiagram *self, gboolean value)
{
	g_return_if_fail (LD_IS_DIAGRAM (self));
	self->priv->modified = value;

	g_object_notify (G_OBJECT (self), "modified");
}

/**
 * ld_diagram_get_objects:
 * @self: An #LdDiagram object.
 *
 * Get a list of objects in the diagram.
 * You mustn't make any changes to the list.
 */
GSList *
ld_diagram_get_objects (LdDiagram *self)
{
	g_return_val_if_fail (LD_IS_DIAGRAM (self), NULL);
	return self->priv->objects;
}

/**
 * ld_diagram_insert_object:
 * @self: An #LdDiagram object.
 * @object: The object to be inserted.
 * @pos: The position at which the object is to be inserted.
 *       Negative values will append to the end.
 *
 * Insert an object into the diagram.
 */
void
ld_diagram_insert_object (LdDiagram *self, LdDiagramObject *object, gint pos)
{
	g_return_if_fail (LD_IS_DIAGRAM (self));
	g_return_if_fail (LD_IS_DIAGRAM_OBJECT (object));

	if (!g_slist_find (self->priv->objects, object))
	{
		self->priv->objects =
			g_slist_insert (self->priv->objects, object, pos);
		g_object_ref (object);

		g_signal_emit (self,
			LD_DIAGRAM_GET_CLASS (self)->changed_signal, 0);
	}
}

/**
 * ld_diagram_remove_object:
 * @self: An #LdDiagram object.
 * @object: The object to be removed.
 *
 * Remove an object from the diagram.
 */
void
ld_diagram_remove_object (LdDiagram *self, LdDiagramObject *object)
{
	g_return_if_fail (LD_IS_DIAGRAM (self));
	g_return_if_fail (LD_IS_DIAGRAM_OBJECT (object));

	if (g_slist_find (self->priv->objects, object))
	{
		ld_diagram_selection_remove (self, object);

		self->priv->objects = g_slist_remove (self->priv->objects, object);
		g_object_unref (object);

		g_signal_emit (self,
			LD_DIAGRAM_GET_CLASS (self)->changed_signal, 0);
	}
}

/**
 * ld_diagram_get_selection:
 * @self: An #LdDiagram object.
 *
 * Get a list of objects that are currently selected in the diagram.
 * You mustn't make any changes to the list.
 */
GSList *
ld_diagram_get_selection (LdDiagram *self)
{
	g_return_val_if_fail (LD_IS_DIAGRAM (self), NULL);
	return self->priv->selection;
}

/**
 * ld_diagram_selection_add:
 * @self: An #LdDiagram object.
 * @object: The object to be added to the selection.
 * @pos: The position at which the object is to be inserted.
 *       Negative values will append to the end.
 *
 * Add an object to selection.
 */
void
ld_diagram_selection_add (LdDiagram *self, LdDiagramObject *object, gint pos)
{
	g_return_if_fail (LD_IS_DIAGRAM (self));
	g_return_if_fail (LD_IS_DIAGRAM_OBJECT (object));

	g_return_if_fail (g_slist_find (self->priv->objects, object) != NULL);

	if (!g_slist_find (self->priv->selection, object))
	{
		self->priv->selection =
			g_slist_insert (self->priv->selection, object, pos);
		g_object_ref (object);

		g_signal_emit (self,
			LD_DIAGRAM_GET_CLASS (self)->selection_changed_signal, 0);
	}
}

/**
 * ld_diagram_selection_remove:
 * @self: An #LdDiagram object.
 * @object: The object to be removed from the selection.
 *
 * Remove an object from the selection.
 */
void
ld_diagram_selection_remove (LdDiagram *self, LdDiagramObject *object)
{
	g_return_if_fail (LD_IS_DIAGRAM (self));
	g_return_if_fail (LD_IS_DIAGRAM_OBJECT (object));

	if (g_slist_find (self->priv->selection, object))
	{
		self->priv->selection = g_slist_remove (self->priv->selection, object);
		g_object_unref (object);

		g_signal_emit (self,
			LD_DIAGRAM_GET_CLASS (self)->selection_changed_signal, 0);
	}
}

/**
 * ld_diagram_select_all:
 * @self: An #LdDiagram object.
 *
 * Include all objects in the document to the selection.
 */
void
ld_diagram_select_all (LdDiagram *self)
{
	g_return_if_fail (LD_IS_DIAGRAM (self));

	ld_diagram_unselect_all_internal (self);

	self->priv->selection = g_slist_copy (self->priv->objects);
	g_slist_foreach (self->priv->selection, (GFunc) g_object_ref, NULL);

	g_signal_emit (self,
		LD_DIAGRAM_GET_CLASS (self)->selection_changed_signal, 0);
}

/**
 * ld_diagram_unselect_all:
 * @self: An #LdDiagram object.
 *
 * Remove all objects from the current selection.
 */
void
ld_diagram_unselect_all (LdDiagram *self)
{
	g_return_if_fail (LD_IS_DIAGRAM (self));

	ld_diagram_unselect_all_internal (self);

	g_signal_emit (self,
		LD_DIAGRAM_GET_CLASS (self)->selection_changed_signal, 0);
}

static void
ld_diagram_unselect_all_internal (LdDiagram *self)
{
	g_slist_foreach (self->priv->selection, (GFunc) g_object_unref, NULL);
	g_slist_free (self->priv->selection);
	self->priv->selection = NULL;
}