1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2006, 2008, 2009, 2010, 2011, 2012 Free Software Foundation
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #define _(msgid) gettext (msgid)
22 #define N_(msgid) msgid
24 #include <data/datasheet.h>
25 #include <data/data-out.h>
26 #include <data/variable.h>
28 #include <ui/gui/psppire-marshal.h>
30 #include <pango/pango-context.h>
32 #include "psppire-data-store.h"
33 #include <libpspp/i18n.h>
36 #include <data/dictionary.h>
37 #include <data/missing-values.h>
38 #include <data/value-labels.h>
39 #include <data/data-in.h>
40 #include <data/format.h>
42 #include <math/sort.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;
77 static guint signals [n_SIGNALS];
81 psppire_data_store_get_type (void)
83 static GType data_store_type = 0;
87 static const GTypeInfo data_store_info =
89 sizeof (PsppireDataStoreClass),
91 NULL, /* base_finalize */
92 (GClassInitFunc) psppire_data_store_class_init,
93 NULL, /* class_finalize */
94 NULL, /* class_data */
95 sizeof (PsppireDataStore),
97 (GInstanceInitFunc) psppire_data_store_init,
100 data_store_type = g_type_register_static (G_TYPE_OBJECT,
102 &data_store_info, 0);
105 return data_store_type;
110 psppire_data_store_class_init (PsppireDataStoreClass *class)
112 GObjectClass *object_class;
114 parent_class = g_type_class_peek_parent (class);
115 object_class = (GObjectClass*) class;
117 object_class->finalize = psppire_data_store_finalize;
118 object_class->dispose = psppire_data_store_dispose;
120 signals [BACKEND_CHANGED] =
121 g_signal_new ("backend-changed",
122 G_TYPE_FROM_CLASS (class),
126 g_cclosure_marshal_VOID__VOID,
130 signals [CASE_INSERTED] =
131 g_signal_new ("case-inserted",
132 G_TYPE_FROM_CLASS (class),
136 g_cclosure_marshal_VOID__INT,
142 signals [CASE_CHANGED] =
143 g_signal_new ("case-changed",
144 G_TYPE_FROM_CLASS (class),
148 g_cclosure_marshal_VOID__INT,
153 signals [CASES_DELETED] =
154 g_signal_new ("cases-deleted",
155 G_TYPE_FROM_CLASS (class),
159 psppire_marshal_VOID__INT_INT,
169 psppire_data_store_insert_value (PsppireDataStore *ds,
170 gint width, gint where);
173 psppire_data_store_get_case_count (const PsppireDataStore *store)
175 return datasheet_get_n_rows (store->datasheet);
179 psppire_data_store_get_value_count (const PsppireDataStore *store)
181 return psppire_dict_get_value_cnt (store->dict);
184 const struct caseproto *
185 psppire_data_store_get_proto (const PsppireDataStore *store)
187 return psppire_dict_get_proto (store->dict);
191 psppire_data_store_init (PsppireDataStore *data_store)
193 data_store->dict = NULL;
194 data_store->datasheet = NULL;
195 data_store->dispose_has_run = FALSE;
199 A callback which occurs after a variable has been deleted.
202 delete_variable_callback (GObject *obj, const struct variable *var UNUSED,
203 gint dict_index, gint case_index,
206 PsppireDataStore *store = PSPPIRE_DATA_STORE (data);
209 datasheet_delete_columns (store->datasheet, case_index, 1);
210 datasheet_insert_column (store->datasheet, NULL, -1, case_index);
213 struct resize_datum_aux
220 resize_datum (const union value *old, union value *new, const void *aux_)
222 const struct resize_datum_aux *aux = aux_;
224 if (aux->new_width == 0)
226 /* FIXME: try to parse string as number. */
229 else if (aux->old_width == 0)
231 /* FIXME: format number as string. */
232 value_set_missing (new, aux->new_width);
235 value_copy_rpad (new, aux->new_width, old, aux->old_width, ' ');
239 variable_changed_callback (GObject *obj, gint var_num, guint what, const struct variable *oldvar,
242 PsppireDataStore *store = PSPPIRE_DATA_STORE (data);
243 struct variable *variable = psppire_dict_get_variable (store->dict, var_num);
245 if (what & VAR_TRAIT_WIDTH)
247 int posn = var_get_case_index (variable);
248 struct resize_datum_aux aux;
249 aux.old_width = var_get_width (oldvar);
250 aux.new_width = var_get_width (variable);
251 datasheet_resize_column (store->datasheet, posn, aux.new_width,
257 insert_variable_callback (GObject *obj, gint var_num, gpointer data)
259 struct variable *variable;
260 PsppireDataStore *store;
263 g_return_if_fail (data);
265 store = PSPPIRE_DATA_STORE (data);
267 variable = psppire_dict_get_variable (store->dict, var_num);
268 posn = var_get_case_index (variable);
269 psppire_data_store_insert_value (store, var_get_width (variable), posn);
273 * psppire_data_store_new:
274 * @dict: The dictionary for this data_store.
277 * Return value: a new #PsppireDataStore
280 psppire_data_store_new (PsppireDict *dict)
282 PsppireDataStore *retval;
284 retval = g_object_new (PSPPIRE_TYPE_DATA_STORE, NULL);
286 psppire_data_store_set_dictionary (retval, dict);
292 psppire_data_store_set_reader (PsppireDataStore *ds,
293 struct casereader *reader)
298 datasheet_destroy (ds->datasheet);
300 ds->datasheet = datasheet_create (reader);
303 for (i = 0 ; i < n_dict_signals; ++i )
305 if ( ds->dict_handler_id [i] > 0)
307 g_signal_handler_unblock (ds->dict,
308 ds->dict_handler_id[i]);
312 g_signal_emit (ds, signals[BACKEND_CHANGED], 0);
317 * psppire_data_store_replace_set_dictionary:
318 * @data_store: The variable store
319 * @dict: The dictionary to set
321 * If a dictionary is already associated with the data-store, then it will be
325 psppire_data_store_set_dictionary (PsppireDataStore *data_store, PsppireDict *dict)
329 /* Disconnect any existing handlers */
330 if ( data_store->dict )
331 for (i = 0 ; i < n_dict_signals; ++i )
333 g_signal_handler_disconnect (data_store->dict,
334 data_store->dict_handler_id[i]);
337 data_store->dict = dict;
342 data_store->dict_handler_id [VARIABLE_INSERTED] =
343 g_signal_connect (dict, "variable-inserted",
344 G_CALLBACK (insert_variable_callback),
347 data_store->dict_handler_id [VARIABLE_DELETED] =
348 g_signal_connect (dict, "variable-deleted",
349 G_CALLBACK (delete_variable_callback),
352 data_store->dict_handler_id [VARIABLE_CHANGED] =
353 g_signal_connect (dict, "variable-changed",
354 G_CALLBACK (variable_changed_callback),
360 /* The entire model has changed */
362 if ( data_store->dict )
363 for (i = 0 ; i < n_dict_signals; ++i )
365 if ( data_store->dict_handler_id [i] > 0)
367 g_signal_handler_block (data_store->dict,
368 data_store->dict_handler_id[i]);
374 psppire_data_store_finalize (GObject *object)
376 PsppireDataStore *ds = PSPPIRE_DATA_STORE (object);
380 datasheet_destroy (ds->datasheet);
381 ds->datasheet = NULL;
385 (* parent_class->finalize) (object);
390 psppire_data_store_dispose (GObject *object)
392 PsppireDataStore *ds = PSPPIRE_DATA_STORE (object);
394 if (ds->dispose_has_run)
399 (* parent_class->dispose) (object);
401 ds->dispose_has_run = TRUE;
406 /* Insert a blank case before POSN */
408 psppire_data_store_insert_new_case (PsppireDataStore *ds, casenumber posn)
411 const struct caseproto *proto;
413 g_return_val_if_fail (ds, FALSE);
415 proto = datasheet_get_proto (ds->datasheet);
416 g_return_val_if_fail (caseproto_get_n_widths (proto) > 0, FALSE);
417 g_return_val_if_fail (posn <= psppire_data_store_get_case_count (ds), FALSE);
419 cc = case_create (proto);
420 case_set_missing (cc);
422 result = psppire_data_store_insert_case (ds, cc, posn);
430 psppire_data_store_get_string (PsppireDataStore *store,
431 glong row, const struct variable *var,
432 bool use_value_label)
438 g_return_val_if_fail (store != NULL, NULL);
439 g_return_val_if_fail (var != NULL, NULL);
441 if (row < 0 || row >= datasheet_get_n_rows (store->datasheet))
444 width = var_get_width (var);
445 value_init (&v, width);
446 datasheet_get_value (store->datasheet, row, var_get_case_index (var), &v);
451 const char *label = var_lookup_value_label (var, &v);
453 string = g_strdup (label);
456 string = value_to_text (v, var);
458 value_destroy (&v, width);
464 /* Attempts to update that part of the variable store which corresponds to VAR
465 within ROW with the value TEXT.
467 If USE_VALUE_LABEL is true, and TEXT is a value label for the column's
468 variable, then stores the value from that value label instead of the literal
471 Returns true if anything was updated, false otherwise. */
473 psppire_data_store_set_string (PsppireDataStore *store,
475 glong row, const struct variable *var,
476 gboolean use_value_label)
482 n_cases = psppire_data_store_get_case_count (store);
486 psppire_data_store_insert_new_case (store, row);
488 case_index = var_get_case_index (var);
491 const struct val_labs *vls = var_get_value_labels (var);
492 const union value *value = vls ? val_labs_find_value (vls, text) : NULL;
494 ok = datasheet_put_value (store->datasheet, row, case_index, value);
499 ok = psppire_data_store_data_in (store, row, case_index, ss_cstr (text),
500 var_get_print_format (var));
503 g_signal_emit (store, signals [CASE_CHANGED], 0, row);
510 psppire_data_store_clear (PsppireDataStore *ds)
512 datasheet_destroy (ds->datasheet);
513 ds->datasheet = NULL;
515 psppire_dict_clear (ds->dict);
517 g_signal_emit (ds, signals [CASES_DELETED], 0, 0, -1);
522 /* Return a casereader made from this datastore */
524 psppire_data_store_get_reader (PsppireDataStore *ds)
527 struct casereader *reader ;
530 for (i = 0 ; i < n_dict_signals; ++i )
532 g_signal_handler_block (ds->dict,
533 ds->dict_handler_id[i]);
536 reader = datasheet_make_reader (ds->datasheet);
538 /* We must not reference this again */
539 ds->datasheet = NULL;
546 /* Column related funcs */
549 static const gchar null_var_name[]=N_("var");
555 /* Returns the CASENUMth case, or a null pointer on failure.
558 psppire_data_store_get_case (const PsppireDataStore *ds,
561 g_return_val_if_fail (ds, FALSE);
562 g_return_val_if_fail (ds->datasheet, FALSE);
564 return datasheet_get_row (ds->datasheet, casenum);
569 psppire_data_store_delete_cases (PsppireDataStore *ds, casenumber first,
572 g_return_val_if_fail (ds, FALSE);
573 g_return_val_if_fail (ds->datasheet, FALSE);
575 g_return_val_if_fail (first + n_cases <=
576 psppire_data_store_get_case_count (ds), FALSE);
579 datasheet_delete_rows (ds->datasheet, first, n_cases);
581 g_signal_emit (ds, signals [CASES_DELETED], 0, first, n_cases);
588 /* Insert case CC into the case file before POSN */
590 psppire_data_store_insert_case (PsppireDataStore *ds,
596 g_return_val_if_fail (ds, FALSE);
597 g_return_val_if_fail (ds->datasheet, FALSE);
600 result = datasheet_insert_rows (ds->datasheet, posn, &cc, 1);
603 g_signal_emit (ds, signals [CASE_INSERTED], 0, posn);
605 g_warning ("Cannot insert case at position %ld\n", posn);
611 /* Set the value of VAR in case CASENUM to V.
612 V must be the correct width for IDX.
613 Returns true if successful, false on I/O error. */
615 psppire_data_store_set_value (PsppireDataStore *ds, casenumber casenum,
616 const struct variable *var, const union value *v)
621 g_return_val_if_fail (ds, FALSE);
622 g_return_val_if_fail (ds->datasheet, FALSE);
624 n_cases = psppire_data_store_get_case_count (ds);
625 if ( casenum > n_cases)
628 if (casenum == n_cases)
629 psppire_data_store_insert_new_case (ds, casenum);
631 ok = datasheet_put_value (ds->datasheet, casenum, var_get_case_index (var),
634 g_signal_emit (ds, signals [CASE_CHANGED], 0, casenum);
642 /* Set the IDXth value of case C using D_IN */
644 psppire_data_store_data_in (PsppireDataStore *ds, casenumber casenum, gint idx,
645 struct substring input, const struct fmt_spec *fmt)
653 g_return_val_if_fail (ds, FALSE);
654 g_return_val_if_fail (ds->datasheet, FALSE);
656 g_return_val_if_fail (idx < datasheet_get_n_columns (ds->datasheet), FALSE);
660 width = fmt_var_width (fmt);
661 g_return_val_if_fail (caseproto_get_width (
662 datasheet_get_proto (ds->datasheet), idx) == width,
664 value_init (&value, width);
665 ok = (datasheet_get_value (ds->datasheet, casenum, idx, &value)
666 && data_in_msg (input, UTF8, fmt->type, &value, width,
667 dict_get_encoding (dict->dict))
668 && datasheet_put_value (ds->datasheet, casenum, idx, &value));
669 value_destroy (&value, width);
674 /* Resize the cases in the casefile, by inserting a value of the
675 given WIDTH into every one of them at the position immediately
679 psppire_data_store_insert_value (PsppireDataStore *ds,
680 gint width, gint where)
684 g_return_val_if_fail (ds, FALSE);
686 g_assert (width >= 0);
688 if ( ! ds->datasheet )
689 ds->datasheet = datasheet_create (NULL);
691 value_init (&value, width);
692 value_set_missing (&value, width);
694 datasheet_insert_column (ds->datasheet, &value, width, where);
695 value_destroy (&value, width);
701 psppire_data_store_filtered (PsppireDataStore *ds,
706 const struct dictionary *dict;
707 const struct variable *filter;
709 if ( row < 0 || row >= datasheet_get_n_rows (ds->datasheet))
712 dict = ds->dict->dict;
713 g_return_val_if_fail (dict, FALSE);
714 filter = dict_get_filter (dict);
718 g_return_val_if_fail (var_is_numeric (filter), FALSE);
719 value_init (&val, 0);
720 if ( ! datasheet_get_value (ds->datasheet, row,
721 var_get_case_index (filter),
725 return (val.f == 0.0);