[gnumeric] scenario: clean.
- From: Morten Welinder <mortenw src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [gnumeric] scenario: clean.
- Date: Fri, 27 Nov 2009 01:26:17 +0000 (UTC)
commit 792ff30edde1603c490510cdb0edc05f893f96c4
Author: Morten Welinder <terra gnome org>
Date: Thu Nov 26 20:25:53 2009 -0500
scenario: clean.
src/dialogs/dialog-scenarios.c | 183 +++++++++++-----------------------------
src/tools/gnm-solver.h | 2 +-
src/tools/scenarios.c | 136 ++++--------------------------
src/tools/scenarios.h | 9 +--
4 files changed, 70 insertions(+), 260 deletions(-)
---
diff --git a/src/dialogs/dialog-scenarios.c b/src/dialogs/dialog-scenarios.c
index fadd961..cb23299 100644
--- a/src/dialogs/dialog-scenarios.c
+++ b/src/dialogs/dialog-scenarios.c
@@ -49,38 +49,23 @@
typedef struct {
GenericToolState base;
- scenario_state_t *scenario_state;
- GtkWidget *name_entry;
-} ScenariosState;
-
-struct _scenario_state {
GtkWidget *show_button;
GtkWidget *delete_button;
GtkWidget *summary_button;
+ GtkWidget *name_entry;
GtkWidget *scenarios_treeview;
GSList *new_report_sheets;
- GnmScenario *old_values;
+ GOUndo *undo;
GnmScenario *current;
-};
+} ScenariosState;
typedef GnmValue * (*ScenarioValueCB) (int col, int row, GnmValue *v, gpointer data);
static void
scenario_for_each_value (GnmScenario *s, ScenarioValueCB fn, gpointer data)
{
- int i, j, cols, pos;
-
- cols = s->range.end.col - s->range.start.col + 1;
- for (i = s->range.start.row; i <= s->range.end.row; i++)
- for (j = s->range.start.col; j <= s->range.end.col; j++) {
- pos = j - s->range.start.col +
- (i - s->range.start.row) * cols;
- s->changing_cells [pos] = fn (j, i,
- s->changing_cells [pos],
- data);
- }
}
/* Ok button pressed. */
@@ -190,65 +175,6 @@ static void
scenario_summary_res_cells (WorkbookControl *wbc, GSList *results,
summary_cb_t *cb)
{
- data_analysis_output_t dao;
- int i, j, col, tmp_row = 4 + cb->row;
- GnmRange r;
-
- dao_init_new_sheet (&dao);
- dao.sheet = cb->sheet;
-
- dao_set_cell (&cb->dao, 0, 3 + cb->row++, _("Result Cells:"));
-
- while (results != NULL) {
- range_init_value (&r, (GnmValue *) results->data);
- for (i = r.start.col; i <= r.end.col; i++)
- for (j = r.start.row; j <= r.end.row; j++) {
- GnmScenario *ov = NULL;
- GnmCell *cell;
- GList *cur;
-
- cell = sheet_cell_fetch (cb->sheet, i, j);
-
- /* Names of the result cells. */
- dao_set_cell (&cb->dao, 0, 3 + cb->row,
- cell_name (cell));
-
- /* Current value. */
- dao_set_cell_value
- (&cb->dao, 1, 3 + cb->row,
- value_dup (cell->value));
-
- /* Evaluate and write the value of the cell
- * with all different scenario values. */
- col = 2;
- for (cur = cb->sheet->scenarios; cur != NULL;
- cur = cur->next) {
- GnmScenario *s = cur->data;
-
- ov = scenario_show (s, ov, &dao);
-
- cell = sheet_cell_fetch (cb->sheet,
- i, j);
-
- cell_queue_recalc (cell);
- gnm_cell_eval (cell);
- dao_set_cell_value (&cb->dao, col++,
- 3 + cb->row,
- value_dup
- (cell->value));
- }
- cb->row++;
-
- /* Use show to clean up 'ov'. */
- scenario_show (NULL, ov, &dao);
- ov = NULL;
- }
- results = results->next;
- }
-
- /* Set the alignment of names of result cells to be right. */
- dao_set_align (&cb->dao, 0, tmp_row, 0, 2 + cb->row,
- HALIGN_RIGHT, VALIGN_BOTTOM);
}
static void
@@ -525,8 +451,8 @@ static void
set_selection_state (ScenariosState *state, gboolean f)
{
/* Set the sensitivies to FALSE since no selections have been made */
- gtk_widget_set_sensitive (state->scenario_state->show_button, f);
- gtk_widget_set_sensitive (state->scenario_state->delete_button, f);
+ gtk_widget_set_sensitive (state->show_button, f);
+ gtk_widget_set_sensitive (state->delete_button, f);
if (f) {
GtkTreeSelection *selection;
@@ -538,12 +464,12 @@ set_selection_state (ScenariosState *state, gboolean f)
selection = gtk_tree_view_get_selection
(GTK_TREE_VIEW
- (state->scenario_state->scenarios_treeview));
+ (state->scenarios_treeview));
if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
return;
model = gtk_tree_view_get_model
(GTK_TREE_VIEW
- (state->scenario_state->scenarios_treeview));
+ (state->scenarios_treeview));
gtk_tree_model_get (GTK_TREE_MODEL (model), &iter,
0, &name, -1);
@@ -610,10 +536,13 @@ scenarios_update_sensitivity_cb (G_GNUC_UNUSED GtkWidget *dummy,
static void
scenario_manager_free (ScenariosState *state)
{
- g_slist_free (state->scenario_state->new_report_sheets);
- state->scenario_state->new_report_sheets = NULL;
+ g_slist_free (state->new_report_sheets);
+ state->new_report_sheets = NULL;
- g_free (state->scenario_state);
+ if (state->undo) {
+ g_object_unref (state->undo);
+ state->undo = NULL;
+ }
}
/**
@@ -627,9 +556,9 @@ static void
scenarios_ok_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
ScenariosState *state)
{
- if (state->scenario_state->current) {
+ if (state->current) {
WorkbookControl *wbc = WORKBOOK_CONTROL (state->base.wbcg);
- cmd_scenario_mngr (wbc, state->scenario_state->current);
+ cmd_scenario_mngr (wbc, state->current);
}
scenario_manager_ok (state->base.sheet);
@@ -643,19 +572,16 @@ scenarios_ok_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
static void
restore_old_values (ScenariosState *state)
{
- data_analysis_output_t dao;
- WorkbookControl *wbc;
+ GOCmdContext *cc;
- if (state->scenario_state->old_values == NULL)
+ if (state->undo == NULL)
return;
- wbc = WORKBOOK_CONTROL (state->base.wbcg);
- dao_init_new_sheet (&dao);
- dao.sheet = state->base.sheet;
- scenario_show (NULL,
- (GnmScenario *) state->scenario_state->old_values,
- &dao);
- state->scenario_state->current = NULL;
- state->scenario_state->old_values = NULL;
+
+ cc = GO_CMD_CONTEXT (state->base.wbcg);
+ go_undo_undo_with_data (state->undo, cc);
+ g_object_unref (state->undo);
+ state->undo = NULL;
+ state->current = NULL;
}
/**
@@ -677,7 +603,7 @@ scenarios_cancel_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
wbc = WORKBOOK_CONTROL (state->base.wbcg);
/* Remove report sheets created on this dialog session. */
- for (cur = state->scenario_state->new_report_sheets; cur != NULL;
+ for (cur = state->new_report_sheets; cur != NULL;
cur = cur->next) {
Sheet *sheet = (Sheet *) cur->data;
@@ -700,7 +626,6 @@ static void
scenarios_show_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
ScenariosState *state)
{
- data_analysis_output_t dao;
WorkbookControl *wbc;
GtkTreeSelection *selection;
GtkTreeIter iter;
@@ -708,23 +633,19 @@ scenarios_show_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
const gchar *value;
selection = gtk_tree_view_get_selection
- (GTK_TREE_VIEW (state->scenario_state->scenarios_treeview));
- dao_init_new_sheet (&dao);
- dao.sheet = state->base.sheet;
+ (GTK_TREE_VIEW (state->scenarios_treeview));
if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
return;
model = gtk_tree_view_get_model
- (GTK_TREE_VIEW (state->scenario_state->scenarios_treeview));
+ (GTK_TREE_VIEW (state->scenarios_treeview));
gtk_tree_model_get (GTK_TREE_MODEL (model), &iter, 0, &value, -1);
wbc = WORKBOOK_CONTROL (state->base.wbcg);
- state->scenario_state->current =
+ state->current =
gnm_sheet_scenario_find (state->base.sheet, value);
- state->scenario_state->old_values =
- scenario_show (state->scenario_state->current,
- state->scenario_state->old_values,
- &dao);
+ state->undo =
+ gnm_scenario_apply (state->current);
}
static void
@@ -743,13 +664,13 @@ scenarios_delete_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
restore_old_values (state);
selection = gtk_tree_view_get_selection
- (GTK_TREE_VIEW (state->scenario_state->scenarios_treeview));
+ (GTK_TREE_VIEW (state->scenarios_treeview));
dao_init_new_sheet (&dao);
dao.sheet = state->base.sheet;
if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
return;
model = gtk_tree_view_get_model
- (GTK_TREE_VIEW (state->scenario_state->scenarios_treeview));
+ (GTK_TREE_VIEW (state->scenarios_treeview));
gtk_tree_model_get (GTK_TREE_MODEL (model), &iter, 0, &value, -1);
@@ -770,7 +691,7 @@ scenarios_delete_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
}
gtk_widget_set_sensitive
- (state->scenario_state->summary_button, !all_deleted);
+ (state->summary_button, !all_deleted);
}
static void
@@ -796,8 +717,8 @@ scenarios_summary_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
scenario_summary (WORKBOOK_CONTROL (state->base.wbcg), state->base.sheet,
results, &new_sheet);
- state->scenario_state->new_report_sheets =
- g_slist_prepend (state->scenario_state->new_report_sheets,
+ state->new_report_sheets =
+ g_slist_prepend (state->new_report_sheets,
new_sheet);
if (results)
go_slist_free_custom (results, (GFreeFunc)value_release);
@@ -814,29 +735,29 @@ static gboolean
init_scenario_buttons (ScenariosState *state)
{
/* Show button */
- state->scenario_state->show_button =
+ state->show_button =
glade_xml_get_widget (state->base.gui, "show_button");
- if (state->scenario_state->show_button == NULL)
+ if (state->show_button == NULL)
return TRUE;
- g_signal_connect (G_OBJECT (state->scenario_state->show_button),
+ g_signal_connect (G_OBJECT (state->show_button),
"clicked",
G_CALLBACK (scenarios_show_clicked_cb), state);
/* Delete button */
- state->scenario_state->delete_button =
+ state->delete_button =
glade_xml_get_widget (state->base.gui, "delete_button");
- if (state->scenario_state->delete_button == NULL)
+ if (state->delete_button == NULL)
return TRUE;
- g_signal_connect (G_OBJECT (state->scenario_state->delete_button),
+ g_signal_connect (G_OBJECT (state->delete_button),
"clicked",
G_CALLBACK (scenarios_delete_clicked_cb), state);
/* Summary button */
- state->scenario_state->summary_button =
+ state->summary_button =
glade_xml_get_widget (state->base.gui, "summary_button");
- if (state->scenario_state->summary_button == NULL)
+ if (state->summary_button == NULL)
return TRUE;
- g_signal_connect (G_OBJECT (state->scenario_state->summary_button),
+ g_signal_connect (G_OBJECT (state->summary_button),
"clicked",
G_CALLBACK (scenarios_summary_clicked_cb),
state);
@@ -862,10 +783,9 @@ dialog_scenarios (WBCGtk *wbcg)
sheet = wb_control_cur_sheet (wbc);
state = g_new (ScenariosState, 1);
- state->scenario_state = g_new (scenario_state_t, 1);
- state->scenario_state->new_report_sheets = NULL;
- state->scenario_state->current = NULL;
- state->scenario_state->old_values = NULL;
+ state->new_report_sheets = NULL;
+ state->current = NULL;
+ state->undo = NULL;
state->base.wb = wb_control_get_workbook (wbc);
if (dialog_tool_init (&state->base, wbcg, sheet,
@@ -881,9 +801,9 @@ dialog_scenarios (WBCGtk *wbcg)
if (init_scenario_buttons (state))
goto error_out;
- state->scenario_state->scenarios_treeview = glade_xml_get_widget
+ state->scenarios_treeview = glade_xml_get_widget
(state->base.gui, "scenarios_treeview");
- if (state->scenario_state->scenarios_treeview == NULL)
+ if (state->scenarios_treeview == NULL)
goto error_out;
w = glade_xml_get_widget (state->base.gui, "changing_cells_entry");
@@ -898,12 +818,12 @@ dialog_scenarios (WBCGtk *wbcg)
if (state->base.sheet->scenarios == NULL)
gtk_widget_set_sensitive
- (state->scenario_state->summary_button, FALSE);
+ (state->summary_button, FALSE);
- update_scenarios_treeview (state->scenario_state->scenarios_treeview,
+ update_scenarios_treeview (state->scenarios_treeview,
sheet->scenarios);
select = gtk_tree_view_get_selection
- (GTK_TREE_VIEW (state->scenario_state->scenarios_treeview));
+ (GTK_TREE_VIEW (state->scenarios_treeview));
g_signal_connect (select, "changed",
G_CALLBACK (cb_selection_changed), state);
@@ -915,7 +835,6 @@ dialog_scenarios (WBCGtk *wbcg)
error_out:
go_gtk_notice_dialog (wbcg_toplevel (wbcg), GTK_MESSAGE_ERROR,
"%s", error_str);
- g_free (state->scenario_state);
g_free (state);
return;
diff --git a/src/tools/gnm-solver.h b/src/tools/gnm-solver.h
index 675fb91..964c30c 100644
--- a/src/tools/gnm-solver.h
+++ b/src/tools/gnm-solver.h
@@ -1,8 +1,8 @@
#ifndef _TOOLS_GNM_SOLVER_H_
#define _TOOLS_GNM_SOLVER_H_
-#include <glib-object.h>
#include <gnumeric.h>
+#include <glib-object.h>
#include <dependent.h>
#include <numbers.h>
diff --git a/src/tools/scenarios.c b/src/tools/scenarios.c
index cff33ea..db16e35 100644
--- a/src/tools/scenarios.c
+++ b/src/tools/scenarios.c
@@ -109,37 +109,6 @@ gnm_scenario_item_valid (const GnmScenarioItem *sci, GnmSheetRange *sr)
/* ------------------------------------------------------------------------- */
-typedef GnmValue * (*ScenarioValueCB) (int col, int row, GnmValue *v, gpointer data);
-
-static void
-scenario_for_each_value (GnmScenario *s, ScenarioValueCB fn, gpointer data)
-{
- int i, j, cols, pos;
-
- if (!s->changing_cells)
- return;
-
- cols = s->range.end.col - s->range.start.col + 1;
- for (i = s->range.start.row; i <= s->range.end.row; i++)
- for (j = s->range.start.col; j <= s->range.end.col; j++) {
- pos = j - s->range.start.col +
- (i - s->range.start.row) * cols;
- s->changing_cells [pos] = fn (j, i,
- s->changing_cells [pos],
- data);
- }
-}
-
-static GnmValue *
-cb_value_free (int col, int row, GnmValue *v, gpointer data)
-{
- value_release (v);
-
- return NULL;
-}
-
-/* ------------------------------------------------------------------------- */
-
static GObjectClass *gnm_scenario_parent_class;
static void
@@ -152,10 +121,6 @@ gnm_scenario_finalize (GObject *obj)
go_slist_free_custom (sc->items, (GFreeFunc)gnm_scenario_item_free);
- scenario_for_each_value (sc, cb_value_free, NULL);
-
- g_free (sc->changing_cells);
-
gnm_scenario_parent_class->finalize (obj);
}
@@ -181,6 +146,18 @@ gnm_scenario_new (char const *name, Sheet *sheet)
return sc;
}
+GnmScenario *
+gnm_scenario_dup (GnmScenario *src, Sheet *new_sheet)
+{
+ GnmScenario *dst;
+
+ dst = gnm_scenario_new (src->name, new_sheet);
+ gnm_scenario_set_comment (dst, src->comment);
+ dst->items = go_slist_map (src->items,
+ (GOMapFunc)gnm_scenario_item_dup);
+ return dst;
+}
+
void
gnm_scenario_set_comment (GnmScenario *sc, const char *comment)
{
@@ -252,20 +229,23 @@ gnm_scenario_apply (GnmScenario *sc)
GnmScenarioItem *sci = l->data;
GnmValue const *val = sci->value;
GnmSheetRange sr;
+ Sheet *sheet;
if (!gnm_scenario_item_valid (sci, &sr))
continue;
+ sheet = eval_sheet (sr.sheet, sc->sheet);
if (val) {
/* FIXME: think about arrays. */
GnmCell *cell = sheet_cell_fetch
- (eval_sheet (sr.sheet, sc->sheet),
+ (sheet,
sr.range.start.col,
sr.range.start.row);
sheet_cell_set_value (cell, value_dup (val));
} else {
- GOUndo *u = clipboard_copy_range_undo (sr.sheet,
+ GOUndo *u = clipboard_copy_range_undo (sheet,
&sr.range);
+ /* FIXME: Clear the range. */
undo = go_undo_combine (undo, u);
}
}
@@ -298,85 +278,3 @@ gnm_scenario_get_range_str (const GnmScenario *sc)
/* ------------------------------------------------------------------------- */
-/* Scenario: Duplicate sheet ***********************************************/
-
-GnmScenario *
-gnm_scenario_dup (GnmScenario *src, Sheet *new_sheet)
-{
- GnmScenario *dst;
- GSList *l;
-
- dst = gnm_scenario_new (src->name, new_sheet);
- gnm_scenario_set_comment (dst, src->comment);
- dst->range = src->range;
-
- for (l = src->items; l; l = l->next) {
- GnmScenarioItem *src_sci = l->data;
- GnmScenarioItem *dst_sci = gnm_scenario_item_dup (src_sci);
- dst->items = g_slist_prepend (dst->items, dst_sci);
- }
- dst->items = g_slist_reverse (dst->items);
-
- return dst;
-}
-
-static GnmValue *
-show_cb (int col, int row, GnmValue *v, data_analysis_output_t *dao)
-{
- dao_set_cell_value (dao, col, row, value_dup (v));
-
- return v;
-}
-
-typedef struct {
- gboolean expr_flag;
- Sheet *sheet;
-} collect_cb_t;
-
-static GnmValue *
-collect_cb (int col, int row, GnmValue *v, collect_cb_t *p)
-{
- GnmCell *cell = sheet_cell_fetch (p->sheet, col, row);
-
- p->expr_flag |= gnm_cell_has_expr (cell);
-
- return value_dup (cell->value);
-}
-
-GnmScenario *
-scenario_show (GnmScenario *s,
- GnmScenario *old_values,
- data_analysis_output_t *dao)
-{
- GnmScenario *stored_values;
- int rows, cols;
- collect_cb_t cb;
-
- /* Recover values of the previous show call. */
- if (old_values) {
- scenario_for_each_value (old_values, (ScenarioValueCB) show_cb,
- dao);
- g_object_unref (old_values);
- }
-
- if (s == NULL)
- return NULL;
-
- /* Store values for recovery. */
- stored_values = gnm_sheet_scenario_new (dao->sheet, "");
- stored_values->range = s->range;
- rows = s->range.end.row - s->range.start.row + 1;
- cols = s->range.end.col - s->range.start.col + 1;
- stored_values->changing_cells = g_new (GnmValue *, rows * cols);
-
- cb.sheet = dao->sheet;
- scenario_for_each_value (stored_values, (ScenarioValueCB) collect_cb,
- &cb);
-
- /* Show scenario and recalculate. */
- scenario_for_each_value (s, (ScenarioValueCB) show_cb, dao);
- workbook_recalc (dao->sheet->workbook);
- sheet_redraw_all (dao->sheet, TRUE);
-
- return stored_values;
-}
diff --git a/src/tools/scenarios.h b/src/tools/scenarios.h
index 7f08c3d..f854b29 100644
--- a/src/tools/scenarios.h
+++ b/src/tools/scenarios.h
@@ -2,7 +2,7 @@
#define __SCENARIOS_H__
#include <gnumeric.h>
-#include <tools/dao.h>
+#include <glib-object.h>
/* ------------------------------------------------------------------------- */
@@ -35,9 +35,6 @@ struct GnmScenario_ {
char *comment;
GSList *items;
-
- GnmValue **changing_cells;
- GnmRange range;
};
typedef struct {
@@ -60,8 +57,4 @@ char *gnm_scenario_get_range_str (const GnmScenario *sc);
/* ------------------------------------------------------------------------- */
-GnmScenario *scenario_show (GnmScenario *scenario,
- GnmScenario *old_values,
- data_analysis_output_t *dao);
-
#endif
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]