[gnumeric] scenario: cleanups.
- From: Morten Welinder <mortenw src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [gnumeric] scenario: cleanups.
- Date: Wed, 25 Nov 2009 16:51:32 +0000 (UTC)
commit 6096b2d1a748caa3dbb283b7dbf1c15c3244b7de
Author: Morten Welinder <terra gnome org>
Date: Wed Nov 25 11:51:16 2009 -0500
scenario: cleanups.
src/commands.c | 41 ++++++------------
src/commands.h | 4 +-
src/dialogs/dialog-scenarios.c | 16 +------
src/dialogs/dialog-solver.c | 2 +-
src/sheet.c | 2 +-
src/tools/scenarios.c | 92 ++++++++++++++++++++++++++++------------
src/tools/scenarios.h | 13 ++----
src/xml-sax-write.c | 2 +-
8 files changed, 91 insertions(+), 81 deletions(-)
---
diff --git a/src/commands.c b/src/commands.c
index 1c0af5e..16b5cf1 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -6031,7 +6031,8 @@ cmd_scenario_add (WorkbookControl *wbc, GnmScenario *s, Sheet *sheet)
typedef struct {
GnmCommand cmd;
- scenario_cmd_t *sc;
+ GnmScenario *sc;
+ GOUndo *undo;
} CmdScenarioMngr;
MAKE_GNM_COMMAND (CmdScenarioMngr, cmd_scenario_mngr, NULL)
@@ -6040,13 +6041,7 @@ static gboolean
cmd_scenario_mngr_redo (GnmCommand *cmd, WorkbookControl *wbc)
{
CmdScenarioMngr *me = CMD_SCENARIO_MNGR (cmd);
- data_analysis_output_t dao;
-
- dao_init_new_sheet (&dao);
- dao.sheet = me->cmd.sheet;
- g_object_unref (me->sc->undo);
- me->sc->undo = scenario_show (me->sc->redo, NULL, &dao);
-
+ me->undo = gnm_scenario_apply (me->sc);
return FALSE;
}
@@ -6055,14 +6050,9 @@ cmd_scenario_mngr_undo (GnmCommand *cmd,
G_GNUC_UNUSED WorkbookControl *wbc)
{
CmdScenarioMngr *me = CMD_SCENARIO_MNGR (cmd);
- GnmScenario *tmp;
- data_analysis_output_t dao;
-
- dao_init_new_sheet (&dao);
- dao.sheet = me->cmd.sheet;
- tmp = gnm_scenario_dup (me->sc->undo, dao.sheet);
- scenario_show (NULL, tmp, &dao);
-
+ go_undo_undo_with_data (me->undo, GO_CMD_CONTEXT (wbc));
+ g_object_unref (me->undo);
+ me->undo = NULL;
return FALSE;
}
@@ -6071,33 +6061,28 @@ cmd_scenario_mngr_finalize (GObject *cmd)
{
CmdScenarioMngr *me = CMD_SCENARIO_MNGR (cmd);
- g_object_unref (me->sc->undo);
- g_object_unref (me->sc->redo);
- g_free (me->sc);
+ g_object_unref (me->sc);
+ if (me->undo)
+ g_object_unref (me->undo);
gnm_command_finalize (cmd);
}
gboolean
-cmd_scenario_mngr (WorkbookControl *wbc, scenario_cmd_t *sc, Sheet *sheet)
+cmd_scenario_mngr (WorkbookControl *wbc, GnmScenario *sc)
{
CmdScenarioMngr *me;
- data_analysis_output_t dao;
g_return_val_if_fail (IS_WORKBOOK_CONTROL (wbc), TRUE);
- g_return_val_if_fail (IS_SHEET (sheet), TRUE);
+ g_return_val_if_fail (GNM_IS_SCENARIO (wbc), TRUE);
me = g_object_new (CMD_SCENARIO_MNGR_TYPE, NULL);
- me->sc = sc;
- me->cmd.sheet = sheet;
+ me->sc = g_object_ref (sc);
+ me->cmd.sheet = sc->sheet;
me->cmd.size = 1;
me->cmd.cmd_descriptor = g_strdup (_("Scenario Show"));
- dao_init_new_sheet (&dao);
- dao.sheet = me->cmd.sheet;
- me->sc->redo = scenario_show (me->sc->undo, NULL, &dao);
-
return gnm_command_push_undo (wbc, G_OBJECT (me));
}
diff --git a/src/commands.h b/src/commands.h
index b9356d5..2bf21f0 100644
--- a/src/commands.h
+++ b/src/commands.h
@@ -114,8 +114,8 @@ gboolean cmd_remove_name (WorkbookControl *wbc, GnmNamedExpr *nexpr);
/* FIXME: figure out how to resolve this better. */
struct _scenario_cmd_t;
-gboolean cmd_scenario_add (WorkbookControl *wbc, struct GnmScenario_ *s, Sheet *sheet);
-gboolean cmd_scenario_mngr (WorkbookControl *wbc, struct _scenario_cmd_t *sc, Sheet *sheet);
+gboolean cmd_scenario_add (WorkbookControl *wbc, GnmScenario *s, Sheet *sheet);
+gboolean cmd_scenario_mngr (WorkbookControl *wbc, GnmScenario *sc);
/* FIXME: figure out how to resolve this better. */
struct _data_shuffling_t;
diff --git a/src/dialogs/dialog-scenarios.c b/src/dialogs/dialog-scenarios.c
index 63837ee..3beb9e7 100644
--- a/src/dialogs/dialog-scenarios.c
+++ b/src/dialogs/dialog-scenarios.c
@@ -417,7 +417,7 @@ scenario_add_ok_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
sc = gnm_sheet_scenario_new (state->base.sheet, name);
if (comment && comment[0])
gnm_scenario_set_comment (sc, comment);
- gnm_scenario_add_area (sc, &sr, TRUE);
+ gnm_scenario_add_area (sc, &sr);
cmd_scenario_add (wbc, sc, state->base.sheet);
@@ -647,19 +647,9 @@ static void
scenarios_ok_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
ScenariosState *state)
{
- data_analysis_output_t dao;
- WorkbookControl *wbc;
- scenario_cmd_t *cmd = g_new (scenario_cmd_t, 1);
-
if (state->scenario_state->current) {
- dao_init_new_sheet (&dao);
- dao.sheet = state->base.sheet;
- wbc = WORKBOOK_CONTROL (state->base.wbcg);
-
- cmd->redo = state->scenario_state->current;
- cmd->undo = state->scenario_state->old_values;
-
- cmd_scenario_mngr (wbc, cmd, state->base.sheet);
+ WorkbookControl *wbc = WORKBOOK_CONTROL (state->base.wbcg);
+ cmd_scenario_mngr (wbc, state->scenario_state->current);
}
scenario_manager_ok (state->base.sheet);
diff --git a/src/dialogs/dialog-solver.c b/src/dialogs/dialog-solver.c
index 1c589d9..beedf35 100644
--- a/src/dialogs/dialog-solver.c
+++ b/src/dialogs/dialog-solver.c
@@ -798,7 +798,7 @@ solver_add_scenario (SolverState *state, GnmSolverResult *res, gchar const *name
default:
break;
}
- gnm_scenario_add_area (sc, &sr, TRUE);
+ gnm_scenario_add_area (sc, &sr);
cmd_scenario_add (wbc, sc, sc->sheet);
}
diff --git a/src/sheet.c b/src/sheet.c
index e595187..b8f8bcf 100644
--- a/src/sheet.c
+++ b/src/sheet.c
@@ -5810,7 +5810,7 @@ gnm_sheet_scenario_new (Sheet *sheet, const char *name)
for (j = 1; ; j++) {
g_string_printf (str, "%s [%d]", tmp, j);
- if (!gnm_sheet_scenario_find (sheet, name)) {
+ if (!gnm_sheet_scenario_find (sheet, str->str)) {
actual_name = g_string_free (str, FALSE);
str = NULL;
break;
diff --git a/src/tools/scenarios.c b/src/tools/scenarios.c
index f6113fa..f63fd17 100644
--- a/src/tools/scenarios.c
+++ b/src/tools/scenarios.c
@@ -34,6 +34,7 @@
#include "scenarios.h"
#include "dao.h"
+#include "clipboard.h"
#include "parse-util.h"
#include <goffice/goffice.h>
@@ -61,8 +62,9 @@ void
gnm_scenario_item_set_range (GnmScenarioItem *sci, const GnmSheetRange *sr)
{
if (sr) {
- GnmValue *v = value_new_cellrange_r (sr->sheet,
- &sr->range);
+ GnmValue *v = value_new_cellrange_r
+ (sr->sheet != sci->dep.sheet ? sr->sheet : NULL,
+ &sr->range);
GnmExprTop const *texpr = gnm_expr_top_new_constant (v);
dependent_managed_set_expr (&sci->dep, texpr);
gnm_expr_top_unref (texpr);
@@ -78,11 +80,22 @@ gnm_scenario_item_set_value (GnmScenarioItem *sci, const GnmValue *v)
}
gboolean
-gnm_scenario_item_valid (const GnmScenarioItem *sci)
+gnm_scenario_item_valid (const GnmScenarioItem *sci, GnmSheetRange *sr)
{
- GnmExprTop const *texpr = sci ? sci->dep.texpr : NULL;
- GnmValue const *vr = texpr ? gnm_expr_top_get_constant (texpr) : NULL;
- return vr && vr->type == VALUE_CELLRANGE;
+ GnmExprTop const *texpr;
+ GnmValue const *vr;
+
+ if (!sci || !((texpr = sci->dep.texpr)))
+ return FALSE;
+
+ vr = gnm_expr_top_get_constant (texpr);
+ if (!vr || vr->type != VALUE_CELLRANGE)
+ return FALSE;
+
+ if (sr)
+ gnm_sheet_range_from_value
+ (sr, gnm_expr_top_get_constant (texpr));
+ return TRUE;
}
/* ------------------------------------------------------------------------- */
@@ -184,9 +197,7 @@ cb_save_cells (GnmCellIter const *iter, gpointer user)
/* FIXME: Think about arrays. */
- sr.sheet = (cell->base.sheet == pdata->sc->sheet)
- ? NULL
- : cell->base.sheet;
+ sr.sheet = cell->base.sheet;
sr.range.start = sr.range.end = iter->pp.eval;
gnm_scenario_item_set_range (sci, &sr);
gnm_scenario_item_set_value (sci, cell->value);
@@ -198,34 +209,61 @@ cb_save_cells (GnmCellIter const *iter, gpointer user)
void
-gnm_scenario_add_area (GnmScenario *sc, const GnmSheetRange *sr,
- gboolean add_content)
+gnm_scenario_add_area (GnmScenario *sc, const GnmSheetRange *sr)
{
GnmScenarioItem *sci;
- GnmSheetRange sr2;
+ struct cb_save_cells data;
g_return_if_fail (GNM_IS_SCENARIO (sc));
g_return_if_fail (sr != NULL);
- sr2 = *sr;
- if (!sr2.sheet)
- sr2.sheet = sc->sheet;
sci = gnm_scenario_item_new (sc->sheet);
- gnm_scenario_item_set_range (sci, &sr2);
+ gnm_scenario_item_set_range (sci, sr);
sc->items = g_slist_prepend (sc->items, sci);
- if (add_content) {
- struct cb_save_cells data;
- data.items = NULL;
- data.sc = sc;
- sheet_foreach_cell_in_range
- (sr2.sheet, CELL_ITER_IGNORE_NONEXISTENT,
- sr2.range.start.col, sr2.range.start.row,
- sr2.range.end.col, sr2.range.end.row,
- cb_save_cells, &data);
- sc->items = g_slist_concat (sc->items,
- g_slist_reverse (data.items));
+ data.items = NULL;
+ data.sc = sc;
+ sheet_foreach_cell_in_range
+ (eval_sheet (sr->sheet, sc->sheet),
+ CELL_ITER_IGNORE_NONEXISTENT,
+ sr->range.start.col, sr->range.start.row,
+ sr->range.end.col, sr->range.end.row,
+ cb_save_cells, &data);
+ sc->items = g_slist_concat (sc->items,
+ g_slist_reverse (data.items));
+}
+
+GOUndo *
+gnm_scenario_apply (GnmScenario *sc)
+{
+ GOUndo *undo = NULL;
+ GSList *l;
+
+ g_return_val_if_fail (GNM_IS_SCENARIO (sc), NULL);
+
+ for (l = sc->items; l; l = l->next) {
+ GnmScenarioItem *sci = l->data;
+ GnmValue const *val = sci->value;
+ GnmSheetRange sr;
+
+ if (!gnm_scenario_item_valid (sci, &sr))
+ continue;
+
+ if (val) {
+ /* FIXME: think about arrays. */
+ GnmCell *cell = sheet_cell_fetch
+ (eval_sheet (sr.sheet, sc->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,
+ &sr.range);
+ undo = go_undo_combine (undo, u);
+ }
}
+
+ return undo;
}
/* ------------------------------------------------------------------------- */
diff --git a/src/tools/scenarios.h b/src/tools/scenarios.h
index ffbde80..4b3819e 100644
--- a/src/tools/scenarios.h
+++ b/src/tools/scenarios.h
@@ -17,7 +17,8 @@ void gnm_scenario_item_set_range (GnmScenarioItem *sci,
const GnmSheetRange *sr);
void gnm_scenario_item_set_value (GnmScenarioItem *sci,
const GnmValue *v);
-gboolean gnm_scenario_item_valid (const GnmScenarioItem *sci);
+gboolean gnm_scenario_item_valid (const GnmScenarioItem *sci,
+ GnmSheetRange *sr);
/* ------------------------------------------------------------------------- */
@@ -53,15 +54,11 @@ GnmScenario *gnm_scenario_dup (GnmScenario *s, Sheet *new_sheet);
void gnm_scenario_set_comment (GnmScenario *sc, const char *comment);
-void gnm_scenario_add_area (GnmScenario *sc, const GnmSheetRange *sr,
- gboolean add_content);
+void gnm_scenario_add_area (GnmScenario *sc, const GnmSheetRange *sr);
-/* ------------------------------------------------------------------------- */
+GOUndo *gnm_scenario_apply (GnmScenario *sc);
-typedef struct _scenario_cmd_t {
- GnmScenario *redo;
- GnmScenario *undo;
-} scenario_cmd_t;
+/* ------------------------------------------------------------------------- */
GnmScenario *scenario_show (GnmScenario *scenario,
GnmScenario *old_values,
diff --git a/src/xml-sax-write.c b/src/xml-sax-write.c
index 571f0c9..dac3340 100644
--- a/src/xml-sax-write.c
+++ b/src/xml-sax-write.c
@@ -1062,7 +1062,7 @@ xml_write_scenario (GnmOutputXML *state, GnmScenario const *sc)
GString *str;
GnmConventionsOut out;
- if (!gnm_scenario_item_valid (sci))
+ if (!gnm_scenario_item_valid (sci, NULL))
continue;
str = g_string_new (NULL);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]