r15364 - gnucash/branches/sx-cleanup/src - Move since-last-run model variable setting into sx-instance-model.

Josh Sled jsled at cvs.gnucash.org
Sat Jan 13 18:45:29 EST 2007


Author: jsled
Date: 2007-01-13 18:45:29 -0500 (Sat, 13 Jan 2007)
New Revision: 15364
Trac: http://svn.gnucash.org/trac/changeset/15364

Modified:
   gnucash/branches/sx-cleanup/src/app-utils/gnc-sx-instance-model.c
   gnucash/branches/sx-cleanup/src/app-utils/gnc-sx-instance-model.h
   gnucash/branches/sx-cleanup/src/doc/sx.rst
   gnucash/branches/sx-cleanup/src/gnome/dialog-sx-editor.c
   gnucash/branches/sx-cleanup/src/gnome/dialog-sx-since-last-run.c
Log:
Move since-last-run model variable setting into sx-instance-model.
Fix variable handling in sx editor; start to move into sx-instance-model.


Modified: gnucash/branches/sx-cleanup/src/app-utils/gnc-sx-instance-model.c
===================================================================
--- gnucash/branches/sx-cleanup/src/app-utils/gnc-sx-instance-model.c	2007-01-13 23:27:04 UTC (rev 15363)
+++ gnucash/branches/sx-cleanup/src/app-utils/gnc-sx-instance-model.c	2007-01-13 23:45:29 UTC (rev 15364)
@@ -23,6 +23,7 @@
 #include "config.h"
 #include <glib.h>
 #include <glib-object.h>
+#include <stdlib.h>
 
 #include "Account.h"
 #include "SX-book.h"
@@ -99,13 +100,14 @@
                         gnc_numeric *result)
 {
      gnc_numeric num;
-     char *errLoc;
+     char *errLoc = NULL;
      int toRet = 0;
      GHashTable *parser_vars;
 
      // convert var_hash -> variables for the parser.
      parser_vars = gnc_sx_instance_get_variables_for_parser(var_hash);
 
+     num = gnc_numeric_zero();
      if (!gnc_exp_parser_parse_separate_vars(formula, &num, &errLoc, parser_vars))
      {
           toRet = -1;
@@ -247,6 +249,27 @@
 }
 
 static void
+_set_var_to_random_value(gchar *key, GncSxVariable *var, gpointer unused_user_data)
+{
+     var->value = double_to_gnc_numeric(rand() + 2, 1,
+                                        GNC_NUMERIC_RND_MASK
+                                        | GNC_RND_FLOOR);
+}
+
+void
+gnc_sx_variable_free(GncSxVariable *var)
+{
+     // g_free(var->name);
+     g_free(var);
+}
+
+void
+randomize_variables(GHashTable *vars)
+{
+     g_hash_table_foreach(vars, (GHFunc)_set_var_to_random_value, NULL);
+}
+
+static void
 _clone_sx_var_hash_entry(gpointer key, gpointer value, gpointer user_data)
 {
      GHashTable *to = (GHashTable*)user_data;
@@ -1122,3 +1145,57 @@
 
      g_signal_emit_by_name(model, "updated", (gpointer)instance->parent->sx);
 }
+
+void
+gnc_sx_instance_model_set_variable(GncSxInstanceModel *model,
+                                   GncSxInstance *instance,
+                                   GncSxVariable *variable,
+                                   gnc_numeric *new_value)
+{
+
+     if (gnc_numeric_equal(variable->value, *new_value))
+          return;
+     variable->value = *new_value;
+     g_signal_emit_by_name(model, "updated", (gpointer)instance->parent->sx);
+}
+
+static void
+_list_from_hash_elts(gpointer key, gpointer value, GList **result_list)
+{
+     *result_list = g_list_append(*result_list, value);
+}
+
+GList*
+gnc_sx_instance_model_check_variables(GncSxInstanceModel *model)
+{
+     GList *rtn = NULL;
+     GList *sx_iter, *inst_iter, *var_list = NULL, *var_iter;
+
+     for (sx_iter = model->sx_instance_list; sx_iter != NULL; sx_iter = sx_iter->next)
+     {
+          GncSxInstances *instances = (GncSxInstances*)sx_iter->data;
+          for (inst_iter = instances->list; inst_iter != NULL; inst_iter = inst_iter->next)
+          {
+               GncSxInstance *inst = (GncSxInstance*)inst_iter->data;
+
+               if (inst->state != SX_INSTANCE_STATE_TO_CREATE)
+                    continue;
+
+               g_hash_table_foreach(inst->variable_bindings, (GHFunc)_list_from_hash_elts, &var_list);
+               for (var_iter = var_list; var_iter != NULL; var_iter = var_iter->next)
+               {
+                    GncSxVariable *var = (GncSxVariable*)var_iter->data;
+                    if (gnc_numeric_check(var->value) != GNC_ERROR_OK)
+                    {
+                         GncSxVariableNeeded *need = g_new0(GncSxVariableNeeded, 1);
+                         need->instance = inst;
+                         need->variable = var;
+                         rtn = g_list_append(rtn, need);
+                    }
+               }
+               g_list_free(var_list);
+               var_list = NULL;
+          }
+     }
+     return rtn;
+}

Modified: gnucash/branches/sx-cleanup/src/app-utils/gnc-sx-instance-model.h
===================================================================
--- gnucash/branches/sx-cleanup/src/app-utils/gnc-sx-instance-model.h	2007-01-13 23:27:04 UTC (rev 15363)
+++ gnucash/branches/sx-cleanup/src/app-utils/gnc-sx-instance-model.h	2007-01-13 23:45:29 UTC (rev 15364)
@@ -104,6 +104,12 @@
      GHashTable *variable_bindings; /**< variable bindings. **/
 } GncSxInstance;
 
+typedef struct _GncSxVariableNeeded
+{
+     GncSxInstance *instance;
+     GncSxVariable *variable;
+} GncSxVariableNeeded;
+
 GType gnc_sx_instance_model_get_type(void);
 
 GncSxInstanceModel* gnc_sx_get_instances(GDate *range_end);
@@ -128,11 +134,22 @@
 GHashTable* gnc_sx_instance_get_variables_for_parser(GHashTable *instance_var_hash);
 
 GncSxVariable* gnc_sx_variable_new_full(gchar *name, gnc_numeric value, gboolean editable);
+void gnc_sx_variable_free(GncSxVariable *var);
 
 void gnc_sx_instance_model_change_instance_state(GncSxInstanceModel *model,
                                                  GncSxInstance *instance,
                                                  GncSxInstanceState new_state);
 
+void gnc_sx_instance_model_set_variable(GncSxInstanceModel *model,
+                                        GncSxInstance *instance,
+                                        GncSxVariable *variable,
+                                        gnc_numeric *new_value);
+
+/**
+ * @return List<GncSxVariableNeeded> of unbound {instance,variable} pairs;
+ * the caller owns the list and the items.
+ **/
+GList* gnc_sx_instance_model_check_variables(GncSxInstanceModel *model);
 void gnc_sx_instance_model_effect_change(GncSxInstanceModel *model,
                                          gboolean auto_create_only,
                                          GList **created_transaction_guids,
@@ -141,6 +158,7 @@
 /* @@fixme names. */
 void sxsl_get_sx_vars(SchedXaction *sx, GHashTable *var_hash);
 int parse_vars_from_formula(const char *formula, GHashTable *var_hash, gnc_numeric *result);
+void randomize_variables(GHashTable *vars);
 
 G_END_DECLS
 

Modified: gnucash/branches/sx-cleanup/src/doc/sx.rst
===================================================================
--- gnucash/branches/sx-cleanup/src/doc/sx.rst	2007-01-13 23:27:04 UTC (rev 15363)
+++ gnucash/branches/sx-cleanup/src/doc/sx.rst	2007-01-13 23:45:29 UTC (rev 15364)
@@ -37,10 +37,15 @@
   - [ ] rename, re-home gnc-sx-instance-model:sxsl_get_sx_vars
   - [ ] rename, re-home gnc-sx-instance-model:parse_vars_from_formula
 
-- sx editor page
+- sx list page
   - [ ] make into split panel
   - [ ] {0, 1, 2, 4, 8, 12} month selection for dense calendar
 
+- sx editor
+  - [ ] clean up, reformat
+  - [ ] model-ize
+    - (check_consistent, especially...)
+
 - gnc_dense_cal
   - [ ] fix static variables that should be instance fields.
   - [ ] change number-of-month properties to display-named properties (width, length)
@@ -59,9 +64,11 @@
   - [ ] XML migration, handling
 
 - since-last-run
+  - [ ] rewrite adapter (re-)population logic
   - [x] move "effect_change" up to app-utils/, test.
   - [x] move state-change up to app-utils
-  - [ ] move variable-setting up to app-utils
+  - [x] move variable-setting up to app-utils
+  - [ ] move summarization up to app-utils
   - [x] add reminders, postponed to SxInstanceModel
   - [x] add mutation support to sx instance model
     - [x] state machine

Modified: gnucash/branches/sx-cleanup/src/gnome/dialog-sx-editor.c
===================================================================
--- gnucash/branches/sx-cleanup/src/gnome/dialog-sx-editor.c	2007-01-13 23:27:04 UTC (rev 15363)
+++ gnucash/branches/sx-cleanup/src/gnome/dialog-sx-editor.c	2007-01-13 23:45:29 UTC (rev 15364)
@@ -157,9 +157,6 @@
 static void advance_toggle( GtkButton *b, GncSxEditorDialog *sxed );
 static gboolean gnc_sxed_check_consistent( GncSxEditorDialog *sxed );
 static gboolean gnc_sxed_check_changed( GncSxEditorDialog *sxed );
-static void free_keys_and_numerics_ea( gpointer key,
-                                       gpointer value,
-                                       gpointer user_data );
 static void gnc_sxed_save_sx( GncSxEditorDialog *sxed );
 static void gnc_sxed_freq_changed( GNCFrequency *gf, gpointer ud );
 static void sxed_excal_update_adapt( GtkObject *o, gpointer ud );
@@ -238,31 +235,8 @@
 	gnc_gnome_help(HF_HELP, HL_SXEDITOR);
 }
 
-static void
-set_var_to_random_value( gpointer key, gpointer value, gpointer ud )
-{
-        if ( !value ) {
-                value = g_new0( gnc_numeric, 1 );
-        }
-        *(gnc_numeric*)value =
-                double_to_gnc_numeric( rand() + 2, 1,
-                                       GNC_NUMERIC_RND_MASK
-                                       | GNC_RND_FLOOR );
-        g_hash_table_insert( ud, key, value );
-}
-
 static
 void
-free_keys_and_numerics_ea( gpointer key, gpointer val, gpointer ud )
-{
-        g_assert( key );
-        g_assert( val );
-        g_free( (gchar*)key );
-        g_free( (gnc_numeric*)val );
-}
-
-static
-void
 editor_ok_button_clicked( GtkButton *b, GncSxEditorDialog *sxed )
 {
         GNCBook *book;
@@ -599,8 +573,7 @@
 
                 srand(time(NULL));
                 for ( i=0; i < numIters && !unbalanceable; i++ ) {
-                        g_hash_table_foreach( vars, set_var_to_random_value,
-                                              (gpointer)vars );
+                        randomize_variables(vars);
                         g_hash_table_foreach( txns, set_sums_to_zero, NULL );
                         tmp = gnc_numeric_zero();
 
@@ -702,16 +675,15 @@
                 }
 
                 /* Subtract out pre-defined vars */
-                if ( g_hash_table_lookup_extended( vars, "i",
-                                                   &unusedKey,
-                                                   &unusedValue ) ) {
-                        ttVarCount -= 1;
+                if (g_hash_table_lookup_extended(vars, "i",
+                                                 &unusedKey,
+                                                 &unusedValue))
+                {
+                     ttVarCount -= 1;
                 }
 
-                g_hash_table_foreach( vars,
-                                      free_keys_and_numerics_ea,
-                                      NULL );
-                g_hash_table_destroy( vars );
+                g_hash_table_foreach(vars, (GHFunc)gnc_sx_variable_free, NULL);
+                g_hash_table_destroy(vars);
 
                 g_hash_table_foreach( txns, free_sums, NULL );
                 g_hash_table_destroy( txns );

Modified: gnucash/branches/sx-cleanup/src/gnome/dialog-sx-since-last-run.c
===================================================================
--- gnucash/branches/sx-cleanup/src/gnome/dialog-sx-since-last-run.c	2007-01-13 23:27:04 UTC (rev 15363)
+++ gnucash/branches/sx-cleanup/src/gnome/dialog-sx-since-last-run.c	2007-01-13 23:45:29 UTC (rev 15364)
@@ -93,22 +93,10 @@
 
 void gnc_sx_slr_model_change_instance_state(GncSxSlrTreeModelAdapter *model, GncSxInstance *instance, GncSxInstanceState new_state);
 void gnc_sx_slr_model_change_variable(GncSxSlrTreeModelAdapter *model, GncSxInstance *instance, GncSxVariable *variable, gnc_numeric *new_value);
-
 void gnc_sx_slr_model_effect_change(GncSxSlrTreeModelAdapter *model, gboolean auto_create_only, GList **created_transaction_guids, GList **creation_errors);
 
 GtkTreeModel* gnc_sx_get_slr_state_model(void);
 
-typedef struct _GncSxSlrVariableNeeded
-{
-     GncSxInstance *instance;
-     GncSxVariable *variable;
-} GncSxSlrVariableNeeded;
-
-/**
- * @return Caller-owned GList<GncSxSlrVariableNeeded*>.
- **/
-GList* gnc_sx_slr_model_check_variables(GncSxSlrTreeModelAdapter *model);
-
 #define GNC_TYPE_SX_SLR_TREE_MODEL_ADAPTER	      (gnc_sx_slr_tree_model_adapter_get_type ())
 #define GNC_SX_SLR_TREE_MODEL_ADAPTER(obj)	      (G_TYPE_CHECK_INSTANCE_CAST ((obj), GNC_TYPE_SX_SLR_TREE_MODEL_ADAPTER, GncSxSlrTreeModelAdapter))
 #define GNC_SX_SLR_TREE_MODEL_ADAPTER_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GNC_TYPE_SX_SLR_TREE_MODEL_ADAPTER, GncSxSlrTreeModelAdapterClass))
@@ -674,23 +662,7 @@
 void
 gnc_sx_slr_model_change_variable(GncSxSlrTreeModelAdapter *model, GncSxInstance *instance, GncSxVariable *variable, gnc_numeric *new_value)
 {
-     GtkTreePath *path;
-     GtkTreeIter iter;
-     GString *tmp_str;
-
-     path = _get_path_for_variable(model, instance, variable);
-     if (path == NULL)
-          return;
-     gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path);
-     
-     variable->value = *new_value;
-
-     _var_numeric_to_string(&variable->value, &tmp_str);
-     gtk_tree_store_set(model->real, &iter,
-                        SLR_MODEL_COL_VARAIBLE_VALUE, tmp_str->str,
-                        -1);
-     g_string_free(tmp_str, TRUE);
-     gtk_tree_path_free(path);
+     gnc_sx_instance_model_set_variable(model->instances, instance, variable, new_value);
 }
 
 static void
@@ -1050,6 +1022,12 @@
      xaccFreeQuery(guid_query);
 }
 
+static GList*
+gnc_sx_slr_model_check_variables(GncSxSlrTreeModelAdapter *editing_model)
+{
+     return gnc_sx_instance_model_check_variables(editing_model->instances);
+}
+
 static void
 dialog_response_cb(GtkDialog *dialog, gint response_id, GncSxSinceLastRunDialog *app_dialog)
 {
@@ -1068,13 +1046,13 @@
                if (g_list_length(unbound_variables) > 0)
                {
                     // focus first variable
-                    GncSxSlrVariableNeeded *first_unbound;
+                    GncSxVariableNeeded *first_unbound;
                     GtkTreePath *variable_path;
                     GtkTreeViewColumn *variable_col;
                     gint variable_view_column = 2;
                     gboolean start_editing = TRUE;
 
-                    first_unbound = (GncSxSlrVariableNeeded*)unbound_variables->data;
+                    first_unbound = (GncSxVariableNeeded*)unbound_variables->data;
                     variable_path = _get_path_for_variable(app_dialog->editing_model, first_unbound->instance, first_unbound->variable);
                     variable_col = gtk_tree_view_get_column(app_dialog->instance_view, variable_view_column);
 
@@ -1125,43 +1103,3 @@
      g_signal_handler_unblock(model->instances, model->updated_cb_id);
 }
 
-static void
-_list_from_hash_elts(gpointer key, gpointer value, GList **result_list)
-{
-     *result_list = g_list_append(*result_list, value);
-}
-
-GList*
-gnc_sx_slr_model_check_variables(GncSxSlrTreeModelAdapter *model)
-{
-     GList *rtn = NULL;
-     GList *sx_iter, *inst_iter, *var_list = NULL, *var_iter;
-
-     for (sx_iter = model->instances->sx_instance_list; sx_iter != NULL; sx_iter = sx_iter->next)
-     {
-          GncSxInstances *instances = (GncSxInstances*)sx_iter->data;
-          for (inst_iter = instances->list; inst_iter != NULL; inst_iter = inst_iter->next)
-          {
-               GncSxInstance *inst = (GncSxInstance*)inst_iter->data;
-
-               if (inst->state != SX_INSTANCE_STATE_TO_CREATE)
-                    continue;
-
-               g_hash_table_foreach(inst->variable_bindings, (GHFunc)_list_from_hash_elts, &var_list);
-               for (var_iter = var_list; var_iter != NULL; var_iter = var_iter->next)
-               {
-                    GncSxVariable *var = (GncSxVariable*)var_iter->data;
-                    if (gnc_numeric_check(var->value) != GNC_ERROR_OK)
-                    {
-                         GncSxSlrVariableNeeded *need = g_new0(GncSxSlrVariableNeeded, 1);
-                         need->instance = inst;
-                         need->variable = var;
-                         rtn = g_list_append(rtn, need);
-                    }
-               }
-               g_list_free(var_list);
-               var_list = NULL;
-          }
-     }
-     return rtn;
-}



More information about the gnucash-changes mailing list