1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2006, 2008, 2009, 2010, 2011, 2012,
3 2013, 2016, 2017 Free Software Foundation
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #define _(msgid) gettext (msgid)
23 #define P_(msgid) msgid
25 #include <data/datasheet.h>
26 #include <data/data-out.h>
27 #include <data/variable.h>
29 #include <ui/gui/psppire-marshal.h>
31 #include <pango/pango-context.h>
33 #include "psppire-data-store.h"
34 #include <libpspp/i18n.h>
37 #include <data/dictionary.h>
38 #include <data/missing-values.h>
39 #include <data/value-labels.h>
40 #include <data/data-in.h>
41 #include <data/format.h>
43 #include <math/sort.h>
47 #include "value-variant.h"
49 static void psppire_data_store_init (PsppireDataStore *data_store);
50 static void psppire_data_store_class_init (PsppireDataStoreClass *class);
52 static void psppire_data_store_finalize (GObject *object);
53 static void psppire_data_store_dispose (GObject *object);
55 static gboolean psppire_data_store_insert_case (PsppireDataStore *ds,
60 static gboolean psppire_data_store_data_in (PsppireDataStore *ds,
61 casenumber casenum, gint idx,
62 struct substring input,
63 const struct fmt_spec *fmt);
65 static GObjectClass *parent_class = NULL;
75 static guint signals [n_SIGNALS];
78 __tree_model_iter_n_children (GtkTreeModel *tree_model,
81 PsppireDataStore *store = PSPPIRE_DATA_STORE (tree_model);
83 if (store->datasheet == NULL)
86 gint n = datasheet_get_n_rows (store->datasheet);
91 static GtkTreeModelFlags
92 __tree_model_get_flags (GtkTreeModel *model)
94 g_return_val_if_fail (PSPPIRE_IS_DATA_STORE (model), (GtkTreeModelFlags) 0);
96 return GTK_TREE_MODEL_LIST_ONLY;
100 __tree_model_get_n_columns (GtkTreeModel *tree_model)
102 PsppireDataStore *store = PSPPIRE_DATA_STORE (tree_model);
104 return psppire_dict_get_var_cnt (store->dict);
109 __iter_nth_child (GtkTreeModel *tree_model,
114 PsppireDataStore *store = PSPPIRE_DATA_STORE (tree_model);
116 g_assert (parent == NULL);
117 g_return_val_if_fail (store, FALSE);
119 if (!store->datasheet || n >= datasheet_get_n_rows (store->datasheet))
122 iter->user_data = NULL;
126 iter->user_data = GINT_TO_POINTER (n);
127 iter->stamp = store->stamp;
132 /* Set the contents of OUT to reflect the information provided by IN, COL, and
133 ROW, for MODEL. Returns TRUE if successful. */
135 psppire_data_store_string_to_value (GtkTreeModel *model, gint col, gint row,
136 const gchar *in, GValue *out)
138 PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
140 while (col >= psppire_dict_get_var_cnt (store->dict))
142 const struct variable *var =
143 psppire_dict_insert_variable (store->dict,
144 psppire_dict_get_var_cnt (store->dict),
146 g_return_val_if_fail (var, FALSE);
149 const struct variable *variable = psppire_dict_get_variable (store->dict, col);
150 g_return_val_if_fail (variable, FALSE);
152 const struct fmt_spec *fmt = var_get_print_format (variable);
154 int width = var_get_width (variable);
157 value_init (&val, width);
159 data_in (ss_cstr (in), psppire_dict_encoding (store->dict),
160 fmt->type, &val, width, "UTF-8");
162 GVariant *vrnt = value_variant_new (&val, width);
163 value_destroy (&val, width);
165 g_value_init (out, G_TYPE_VARIANT);
166 g_value_set_variant (out, vrnt);
172 unlabeled_value (PsppireDataStore *store, const struct variable *variable, const union value *val)
174 const struct fmt_spec *fmt = var_get_print_format (variable);
175 return data_out (val, psppire_dict_encoding (store->dict), fmt);
179 psppire_data_store_value_to_string (gpointer unused, PsppireDataStore *store, gint col, gint row, const GValue *v)
181 const struct variable *variable = psppire_dict_get_variable (store->dict, col);
182 g_return_val_if_fail (variable, g_strdup ("???"));
184 GVariant *vrnt = g_value_get_variant (v);
185 g_return_val_if_fail (vrnt, g_strdup ("???"));
188 value_variant_get (&val, vrnt);
190 char *out = unlabeled_value (store, variable, &val);
192 value_destroy_from_variant (&val, vrnt);
198 psppire_data_store_value_to_string_with_labels (gpointer unused, PsppireDataStore *store, gint col, gint row, const GValue *v)
200 const struct variable *variable = psppire_dict_get_variable (store->dict, col);
201 g_return_val_if_fail (variable, g_strdup ("???"));
203 GVariant *vrnt = g_value_get_variant (v);
205 value_variant_get (&val, vrnt);
209 const struct val_labs *vls = var_get_value_labels (variable);
210 struct val_lab *vl = val_labs_lookup (vls, &val);
212 out = strdup (val_lab_get_label (vl));
214 out = unlabeled_value (store, variable, &val);
216 value_destroy_from_variant (&val, vrnt);
222 __get_value (GtkTreeModel *tree_model,
227 PsppireDataStore *store = PSPPIRE_DATA_STORE (tree_model);
229 g_return_if_fail (iter->stamp == store->stamp);
231 const struct variable *variable = psppire_dict_get_variable (store->dict, column);
232 if (NULL == variable)
235 gint row = GPOINTER_TO_INT (iter->user_data);
237 struct ccase *cc = datasheet_get_row (store->datasheet, row);
239 g_return_if_fail (cc);
241 g_value_init (value, G_TYPE_VARIANT);
243 const union value *val = case_data_idx (cc, var_get_case_index (variable));
245 GVariant *vv = value_variant_new (val, var_get_width (variable));
247 g_value_set_variant (value, vv);
254 __tree_model_init (GtkTreeModelIface *iface)
256 iface->get_flags = __tree_model_get_flags;
257 iface->get_n_columns = __tree_model_get_n_columns ;
258 iface->get_column_type = NULL;
259 iface->get_iter = NULL;
260 iface->iter_next = NULL;
261 iface->get_path = NULL;
262 iface->get_value = __get_value;
264 iface->iter_children = NULL;
265 iface->iter_has_child = NULL;
266 iface->iter_n_children = __tree_model_iter_n_children;
267 iface->iter_nth_child = __iter_nth_child;
268 iface->iter_parent = NULL;
273 psppire_data_store_get_type (void)
275 static GType data_store_type = 0;
277 if (!data_store_type)
279 static const GTypeInfo data_store_info =
281 sizeof (PsppireDataStoreClass),
282 NULL, /* base_init */
283 NULL, /* base_finalize */
284 (GClassInitFunc) psppire_data_store_class_init,
285 NULL, /* class_finalize */
286 NULL, /* class_data */
287 sizeof (PsppireDataStore),
289 (GInstanceInitFunc) psppire_data_store_init,
292 static const GInterfaceInfo tree_model_info = {
293 (GInterfaceInitFunc) __tree_model_init,
298 data_store_type = g_type_register_static (G_TYPE_OBJECT,
300 &data_store_info, 0);
302 g_type_add_interface_static (data_store_type, GTK_TYPE_TREE_MODEL,
306 return data_store_type;
311 psppire_data_store_class_init (PsppireDataStoreClass *class)
313 GObjectClass *object_class;
315 parent_class = g_type_class_peek_parent (class);
316 object_class = (GObjectClass*) class;
318 object_class->finalize = psppire_data_store_finalize;
319 object_class->dispose = psppire_data_store_dispose;
321 signals [ITEMS_CHANGED] =
322 g_signal_new ("items-changed",
323 G_TYPE_FROM_CLASS (class),
327 psppire_marshal_VOID__UINT_UINT_UINT,
330 G_TYPE_UINT, /* Index of the start of the change */
331 G_TYPE_UINT, /* The number of items deleted */
332 G_TYPE_UINT); /* The number of items inserted */
334 signals [CASE_CHANGED] =
335 g_signal_new ("case-changed",
336 G_TYPE_FROM_CLASS (class),
340 g_cclosure_marshal_VOID__INT,
349 psppire_data_store_get_case_count (const PsppireDataStore *store)
351 return datasheet_get_n_rows (store->datasheet);
355 psppire_data_store_get_value_count (const PsppireDataStore *store)
357 return psppire_dict_get_value_cnt (store->dict);
360 const struct caseproto *
361 psppire_data_store_get_proto (const PsppireDataStore *store)
363 return psppire_dict_get_proto (store->dict);
367 psppire_data_store_init (PsppireDataStore *data_store)
369 data_store->dict = NULL;
370 data_store->datasheet = NULL;
371 data_store->dispose_has_run = FALSE;
372 data_store->stamp = g_random_int ();
377 psppire_data_store_delete_value (PsppireDataStore *store, gint case_index)
379 g_return_if_fail (store->datasheet);
380 datasheet_delete_columns (store->datasheet, case_index, 1);
381 datasheet_insert_column (store->datasheet, NULL, -1, case_index);
386 A callback which occurs after a variable has been deleted.
389 delete_variable_callback (GObject *obj, const struct variable *var UNUSED,
390 gint dict_index, gint case_index,
393 PsppireDataStore *store = PSPPIRE_DATA_STORE (data);
395 psppire_data_store_delete_value (store, case_index);
398 struct resize_datum_aux
400 const struct dictionary *dict;
401 const struct variable *new_variable;
402 const struct variable *old_variable;
406 resize_datum (const union value *old, union value *new, const void *aux_)
408 const struct resize_datum_aux *aux = aux_;
409 int new_width = var_get_width (aux->new_variable);
410 const char *enc = dict_get_encoding (aux->dict);
411 const struct fmt_spec *newfmt = var_get_print_format (aux->new_variable);
412 char *s = data_out (old, enc, var_get_print_format (aux->old_variable));
413 enum fmt_type type = (fmt_usable_for_input (newfmt->type)
416 free (data_in (ss_cstr (s), enc, type, new, new_width, enc));
421 variable_changed_callback (GObject *obj, gint var_num, guint what, const struct variable *oldvar,
424 PsppireDataStore *store = PSPPIRE_DATA_STORE (data);
425 struct variable *variable = psppire_dict_get_variable (store->dict, var_num);
427 if (what & VAR_TRAIT_WIDTH)
429 int posn = var_get_case_index (variable);
430 struct resize_datum_aux aux;
431 aux.old_variable = oldvar;
432 aux.new_variable = variable;
433 aux.dict = store->dict->dict;
434 datasheet_resize_column (store->datasheet, posn, var_get_width (variable),
440 insert_variable_callback (GObject *obj, gint var_num, gpointer data)
442 struct variable *variable;
443 PsppireDataStore *store;
446 g_return_if_fail (data);
448 store = PSPPIRE_DATA_STORE (data);
450 variable = psppire_dict_get_variable (store->dict, var_num);
451 posn = var_get_case_index (variable);
452 psppire_data_store_insert_value (store, var_get_width (variable), posn);
456 * psppire_data_store_new:
457 * @dict: The dictionary for this data_store.
460 * Return value: a new #PsppireDataStore
463 psppire_data_store_new (PsppireDict *dict)
465 PsppireDataStore *retval;
467 retval = g_object_new (PSPPIRE_TYPE_DATA_STORE, NULL);
469 psppire_data_store_set_dictionary (retval, dict);
475 psppire_data_store_set_reader (PsppireDataStore *ds,
476 struct casereader *reader)
482 old_n = datasheet_get_n_rows (ds->datasheet);
483 datasheet_destroy (ds->datasheet);
486 ds->datasheet = datasheet_create (reader);
488 gint new_n = datasheet_get_n_rows (ds->datasheet);
491 for (i = 0 ; i < n_dict_signals; ++i)
493 if (ds->dict_handler_id [i] > 0)
495 g_signal_handler_unblock (ds->dict,
496 ds->dict_handler_id[i]);
500 g_signal_emit (ds, signals[ITEMS_CHANGED], 0, 0, old_n, new_n);
505 * psppire_data_store_replace_set_dictionary:
506 * @data_store: The variable store
507 * @dict: The dictionary to set
509 * If a dictionary is already associated with the data-store, then it will be
513 psppire_data_store_set_dictionary (PsppireDataStore *data_store, PsppireDict *dict)
517 /* Disconnect any existing handlers */
518 if (data_store->dict)
519 for (i = 0 ; i < n_dict_signals; ++i)
521 g_signal_handler_disconnect (data_store->dict,
522 data_store->dict_handler_id[i]);
525 data_store->dict = dict;
530 data_store->dict_handler_id [VARIABLE_INSERTED] =
531 g_signal_connect (dict, "variable-inserted",
532 G_CALLBACK (insert_variable_callback),
535 data_store->dict_handler_id [VARIABLE_DELETED] =
536 g_signal_connect (dict, "variable-deleted",
537 G_CALLBACK (delete_variable_callback),
540 data_store->dict_handler_id [VARIABLE_CHANGED] =
541 g_signal_connect (dict, "variable-changed",
542 G_CALLBACK (variable_changed_callback),
548 /* The entire model has changed */
550 if (data_store->dict)
551 for (i = 0 ; i < n_dict_signals; ++i)
553 if (data_store->dict_handler_id [i] > 0)
555 g_signal_handler_block (data_store->dict,
556 data_store->dict_handler_id[i]);
562 psppire_data_store_finalize (GObject *object)
564 PsppireDataStore *ds = PSPPIRE_DATA_STORE (object);
568 datasheet_destroy (ds->datasheet);
569 ds->datasheet = NULL;
573 (* parent_class->finalize) (object);
578 psppire_data_store_dispose (GObject *object)
580 PsppireDataStore *ds = PSPPIRE_DATA_STORE (object);
582 if (ds->dispose_has_run)
585 psppire_data_store_set_dictionary (ds, NULL);
588 (* parent_class->dispose) (object);
590 ds->dispose_has_run = TRUE;
595 /* Insert a blank case before POSN */
597 psppire_data_store_insert_new_case (PsppireDataStore *ds, casenumber posn)
600 const struct caseproto *proto;
602 g_return_val_if_fail (ds, FALSE);
604 proto = datasheet_get_proto (ds->datasheet);
605 g_return_val_if_fail (caseproto_get_n_widths (proto) > 0, FALSE);
606 g_return_val_if_fail (posn <= psppire_data_store_get_case_count (ds), FALSE);
608 cc = case_create (proto);
609 case_set_missing (cc);
611 result = psppire_data_store_insert_case (ds, cc, posn);
619 psppire_data_store_get_value (PsppireDataStore *store,
620 glong row, const struct variable *var,
623 g_return_val_if_fail (store != NULL, FALSE);
624 g_return_val_if_fail (store->datasheet != NULL, FALSE);
625 g_return_val_if_fail (var != NULL, FALSE);
627 if (row < 0 || row >= datasheet_get_n_rows (store->datasheet))
630 int width = var_get_width (var);
631 value_init (val, width);
632 datasheet_get_value (store->datasheet, row, var_get_case_index (var), val);
640 psppire_data_store_get_string (PsppireDataStore *store,
641 glong row, const struct variable *var,
642 bool use_value_label)
646 int width = var_get_width (var);
647 if (! psppire_data_store_get_value (store, row, var, &v))
653 const char *label = var_lookup_value_label (var, &v);
655 string = g_strdup (label);
658 string = value_to_text (v, var);
660 value_destroy (&v, width);
666 /* Attempts to update that part of the variable store which corresponds to VAR
667 within ROW with the value TEXT.
669 If USE_VALUE_LABEL is true, and TEXT is a value label for the column's
670 variable, then stores the value from that value label instead of the literal
673 Returns true if anything was updated, false otherwise. */
675 psppire_data_store_set_string (PsppireDataStore *store,
677 glong row, const struct variable *var,
678 gboolean use_value_label)
684 n_cases = psppire_data_store_get_case_count (store);
688 psppire_data_store_insert_new_case (store, row);
690 case_index = var_get_case_index (var);
693 const struct val_labs *vls = var_get_value_labels (var);
694 const union value *value = vls ? val_labs_find_value (vls, text) : NULL;
696 ok = datasheet_put_value (store->datasheet, row, case_index, value);
701 ok = psppire_data_store_data_in (store, row, case_index, ss_cstr (text),
702 var_get_print_format (var));
705 g_signal_emit (store, signals [CASE_CHANGED], 0, row);
712 psppire_data_store_clear (PsppireDataStore *ds)
714 datasheet_destroy (ds->datasheet);
715 ds->datasheet = NULL;
717 psppire_dict_clear (ds->dict);
719 g_signal_emit (ds, signals [ITEMS_CHANGED], 0, 0, -1, 0);
724 /* Return a casereader made from this datastore */
726 psppire_data_store_get_reader (PsppireDataStore *ds)
729 struct casereader *reader ;
732 for (i = 0 ; i < n_dict_signals; ++i)
734 g_signal_handler_block (ds->dict,
735 ds->dict_handler_id[i]);
738 reader = datasheet_make_reader (ds->datasheet);
740 /* We must not reference this again */
741 ds->datasheet = NULL;
746 /* Returns the CASENUMth case, or a null pointer on failure.
749 psppire_data_store_get_case (const PsppireDataStore *ds,
752 g_return_val_if_fail (ds, FALSE);
753 g_return_val_if_fail (ds->datasheet, FALSE);
755 return datasheet_get_row (ds->datasheet, casenum);
760 psppire_data_store_delete_cases (PsppireDataStore *ds, casenumber first,
763 g_return_val_if_fail (ds, FALSE);
764 g_return_val_if_fail (ds->datasheet, FALSE);
766 g_return_val_if_fail (first + n_cases <=
767 psppire_data_store_get_case_count (ds), FALSE);
770 datasheet_delete_rows (ds->datasheet, first, n_cases);
772 g_signal_emit (ds, signals[ITEMS_CHANGED], 0, first, n_cases, 0);
779 /* Insert case CC into the case file before POSN */
781 psppire_data_store_insert_case (PsppireDataStore *ds,
787 g_return_val_if_fail (ds, FALSE);
788 g_return_val_if_fail (ds->datasheet, FALSE);
791 result = datasheet_insert_rows (ds->datasheet, posn, &cc, 1);
795 g_signal_emit (ds, signals[ITEMS_CHANGED], 0, posn, 0, 1);
798 g_warning ("Cannot insert case at position %ld\n", posn);
804 /* Set the value of VAR in case CASENUM to V.
805 V must be the correct width for IDX.
806 Returns true if successful, false on I/O error. */
808 psppire_data_store_set_value (PsppireDataStore *ds, casenumber casenum,
809 const struct variable *var, const union value *v)
814 g_return_val_if_fail (ds, FALSE);
815 g_return_val_if_fail (ds->datasheet, FALSE);
817 n_cases = psppire_data_store_get_case_count (ds);
818 if (casenum > n_cases)
821 if (casenum == n_cases)
822 psppire_data_store_insert_new_case (ds, casenum);
824 ok = datasheet_put_value (ds->datasheet, casenum, var_get_case_index (var),
828 g_signal_emit (ds, signals [CASE_CHANGED], 0, casenum);
829 g_signal_emit (ds, signals [ITEMS_CHANGED], 0, casenum, 1, 1);
838 /* Set the IDXth value of case C using D_IN */
840 psppire_data_store_data_in (PsppireDataStore *ds, casenumber casenum, gint idx,
841 struct substring input, const struct fmt_spec *fmt)
849 g_return_val_if_fail (ds, FALSE);
850 g_return_val_if_fail (ds->datasheet, FALSE);
852 g_return_val_if_fail (idx < datasheet_get_n_columns (ds->datasheet), FALSE);
856 width = fmt_var_width (fmt);
857 g_return_val_if_fail (caseproto_get_width (
858 datasheet_get_proto (ds->datasheet), idx) == width,
860 value_init (&value, width);
861 ok = (datasheet_get_value (ds->datasheet, casenum, idx, &value)
862 && data_in_msg (input, UTF8, fmt->type, &value, width,
863 dict_get_encoding (dict->dict))
864 && datasheet_put_value (ds->datasheet, casenum, idx, &value));
865 value_destroy (&value, width);
870 /* Resize the cases in the casefile, by inserting a value of the
871 given WIDTH into every one of them at the position immediately
875 psppire_data_store_insert_value (PsppireDataStore *ds,
876 gint width, gint where)
880 g_return_val_if_fail (ds, FALSE);
882 g_assert (width >= 0);
885 ds->datasheet = datasheet_create (NULL);
887 value_init (&value, width);
888 value_set_missing (&value, width);
890 datasheet_insert_column (ds->datasheet, &value, width, where);
891 value_destroy (&value, width);
897 psppire_data_store_filtered (PsppireDataStore *ds,
902 const struct dictionary *dict;
903 const struct variable *filter;
905 if (row < 0 || row >= datasheet_get_n_rows (ds->datasheet))
908 dict = ds->dict->dict;
909 g_return_val_if_fail (dict, FALSE);
910 filter = dict_get_filter (dict);
914 g_return_val_if_fail (var_is_numeric (filter), FALSE);
915 value_init (&val, 0);
916 if (! datasheet_get_value (ds->datasheet, row,
917 var_get_case_index (filter),
921 return (val.f == 0.0);