[gnumeric] scenarios: even more cleanup.
- From: Morten Welinder <mortenw src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [gnumeric] scenarios: even more cleanup.
- Date: Tue, 24 Nov 2009 18:58:36 +0000 (UTC)
commit 66b7e05282f04f593af1d9bf4e7e779af343b5ce
Author: Morten Welinder <terra gnome org>
Date: Tue Nov 24 13:58:22 2009 -0500
scenarios: even more cleanup.
src/commands.c | 15 +-
src/dialogs/dialog-scenarios.c | 267 ++++++++++++++++++++++++++++++++-
src/dialogs/dialog-solver.c | 2 +-
src/sheet.c | 75 +++++++++-
src/sheet.h | 5 +-
src/tools/scenarios.c | 322 +---------------------------------------
src/tools/scenarios.h | 17 +--
7 files changed, 357 insertions(+), 346 deletions(-)
---
diff --git a/src/commands.c b/src/commands.c
index e8904c6..1c0af5e 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -5976,9 +5976,9 @@ static gboolean
cmd_scenario_add_redo (GnmCommand *cmd, WorkbookControl *wbc)
{
CmdScenarioAdd *me = CMD_SCENARIO_ADD (cmd);
+ GnmScenario *sc = gnm_scenario_dup (me->scenario, me->cmd.sheet);
- scenario_add (me->cmd.sheet,
- gnm_scenario_dup (me->scenario, me->cmd.sheet));
+ gnm_sheet_scenario_add (sc->sheet, sc);
return FALSE;
}
@@ -5988,8 +5988,11 @@ cmd_scenario_add_undo (GnmCommand *cmd,
G_GNUC_UNUSED WorkbookControl *wbc)
{
CmdScenarioAdd *me = CMD_SCENARIO_ADD (cmd);
+ GnmScenario *sc = gnm_sheet_scenario_find (me->cmd.sheet,
+ me->scenario->name);
- scenario_delete (me->cmd.sheet, me->scenario->name);
+ if (sc)
+ gnm_sheet_scenario_remove (sc->sheet, sc);
return FALSE;
}
@@ -6042,7 +6045,7 @@ cmd_scenario_mngr_redo (GnmCommand *cmd, WorkbookControl *wbc)
dao_init_new_sheet (&dao);
dao.sheet = me->cmd.sheet;
g_object_unref (me->sc->undo);
- me->sc->undo = scenario_show (wbc, me->sc->redo, NULL, &dao);
+ me->sc->undo = scenario_show (me->sc->redo, NULL, &dao);
return FALSE;
}
@@ -6058,7 +6061,7 @@ cmd_scenario_mngr_undo (GnmCommand *cmd,
dao_init_new_sheet (&dao);
dao.sheet = me->cmd.sheet;
tmp = gnm_scenario_dup (me->sc->undo, dao.sheet);
- scenario_show (wbc, NULL, tmp, &dao);
+ scenario_show (NULL, tmp, &dao);
return FALSE;
}
@@ -6093,7 +6096,7 @@ cmd_scenario_mngr (WorkbookControl *wbc, scenario_cmd_t *sc, Sheet *sheet)
dao_init_new_sheet (&dao);
dao.sheet = me->cmd.sheet;
- me->sc->redo = scenario_show (wbc, me->sc->undo, NULL, &dao);
+ me->sc->redo = scenario_show (me->sc->undo, NULL, &dao);
return gnm_command_push_undo (wbc, G_OBJECT (me));
}
diff --git a/src/dialogs/dialog-scenarios.c b/src/dialogs/dialog-scenarios.c
index 7a2159e..435800b 100644
--- a/src/dialogs/dialog-scenarios.c
+++ b/src/dialogs/dialog-scenarios.c
@@ -35,7 +35,9 @@
#include <sheet.h>
#include <dao-gui-utils.h>
#include <position.h>
+#include <ranges.h>
#include <value.h>
+#include <cell.h>
#include <dao.h>
#include "scenarios.h"
@@ -63,6 +65,254 @@ struct _scenario_state {
GnmScenario *current;
};
+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. */
+static void
+scenario_manager_ok (Sheet *sheet)
+{
+ GList *l, *scenarios = g_list_copy (sheet->scenarios);
+
+ /* Update scenarios (free the deleted ones). */
+ for (l = scenarios; l; l = l->next) {
+ GnmScenario *sc = l->data;
+ if (g_object_get_data (G_OBJECT (sc), "marked_deleted")) {
+ gnm_sheet_scenario_remove (sc->sheet, sc);
+ }
+ }
+ g_list_free (scenarios);
+
+ sheet_redraw_all (sheet, TRUE);
+}
+
+/* Cancel button pressed. */
+static void
+scenario_recover_all (Sheet *sheet)
+{
+ GList *l;
+
+ for (l = sheet->scenarios; l; l = l->next) {
+ GnmScenario *sc = l->data;
+ g_object_set_data (G_OBJECT (sc),
+ "marked_deleted", GUINT_TO_POINTER (FALSE));
+ }
+}
+
+/* Scenario: Create summary report ***************************************/
+
+static void
+rm_fun_cb (gpointer key, gpointer value, gpointer user_data)
+{
+ g_free (value);
+}
+
+typedef struct {
+ data_analysis_output_t dao;
+
+ Sheet *sheet;
+ GHashTable *names; /* A hash table for cell names->row. */
+ int col;
+ int row;
+ GSList *results;
+} summary_cb_t;
+
+static GnmValue *
+summary_cb (int col, int row, GnmValue *v, summary_cb_t *p)
+{
+ char *tmp = dao_find_name (p->sheet, col, row);
+ int *index;
+
+ /* Check if some of the previous scenarios already included that
+ * cell. If so, it's row will be put into *index. */
+ index = g_hash_table_lookup (p->names, tmp);
+ if (index != NULL) {
+ dao_set_cell_value (&p->dao, 2 + p->col, 3 + *index,
+ value_dup (v));
+
+ /* Set the colors. */
+ dao_set_colors (&p->dao, 2 + p->col, 3 + *index,
+ 2 + p->col, 3 + *index,
+ style_color_new_go (GO_COLOR_BLACK),
+ style_color_new_gdk (&gs_light_gray));
+
+ } else {
+ /* New cell. */
+ GnmCell *cell;
+ int *r;
+
+ /* Changing cell name. */
+ dao_set_cell (&p->dao, 0, 3 + p->row, tmp);
+
+ /* GnmValue of the cell in this scenario. */
+ dao_set_cell_value (&p->dao, 2 + p->col, 3 + p->row,
+ value_dup (v));
+
+ /* Current value of the cell. */
+ cell = sheet_cell_fetch (p->sheet, col, row);
+ dao_set_cell_value (&p->dao, 1, 3 + p->row,
+ value_dup (cell->value));
+
+ /* Set the colors. */
+ dao_set_colors (&p->dao, 2 + p->col, 3 + p->row,
+ 2 + p->col, 3 + p->row,
+ style_color_new_go (GO_COLOR_BLACK),
+ style_color_new_gdk (&gs_light_gray));
+
+ /* Insert row number into the hash table. */
+ r = g_new (int, 1);
+ *r = p->row;
+ g_hash_table_insert (p->names, tmp, r);
+
+ /* Increment the nbr of rows. */
+ p->row++;
+ }
+
+ return v;
+}
+
+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
+scenario_summary (WorkbookControl *wbc,
+ Sheet *sheet,
+ GSList *results,
+ Sheet **new_sheet)
+{
+ summary_cb_t cb;
+ GList *cur;
+ GList *scenarios = sheet->scenarios;
+
+ /* Initialize: Currently only new sheet output supported. */
+ dao_init_new_sheet (&cb.dao);
+ dao_prepare_output (wbc, &cb.dao, _("Scenario Summary"));
+
+ /* Titles. */
+ dao_set_cell (&cb.dao, 1, 1, _("Current Values"));
+ dao_set_cell (&cb.dao, 0, 2, _("Changing Cells:"));
+
+ /* Go through all scenarios. */
+ cb.row = 0;
+ cb.names = g_hash_table_new (g_str_hash, g_str_equal);
+ cb.sheet = sheet;
+ cb.results = results;
+ for (cb.col = 0, cur = scenarios; cur != NULL; cb.col++,
+ cur = cur->next) {
+ GnmScenario *s = cur->data;
+
+ /* Scenario name. */
+ dao_set_cell (&cb.dao, 2 + cb.col, 1, s->name);
+
+ scenario_for_each_value (s, (ScenarioValueCB) summary_cb, &cb);
+ }
+
+ /* Set the alignment of names of the changing cells to be right. */
+ dao_set_align (&cb.dao, 0, 3, 0, 2 + cb.row, HALIGN_RIGHT,
+ VALIGN_BOTTOM);
+
+ /* Result cells. */
+ if (results != NULL)
+ scenario_summary_res_cells (wbc, results, &cb);
+
+ /* Destroy the hash table. */
+ g_hash_table_foreach (cb.names, (GHFunc) rm_fun_cb, NULL);
+ g_hash_table_destroy (cb.names);
+
+ /* Clean up the report output. */
+ dao_set_bold (&cb.dao, 0, 0, 0, 2 + cb.row);
+ dao_autofit_columns (&cb.dao);
+ dao_set_cell (&cb.dao, 0, 0, _("Scenario Summary"));
+
+ dao_set_colors (&cb.dao, 0, 0, cb.col + 1, 1,
+ style_color_new_go (GO_COLOR_WHITE),
+ style_color_new_gdk (&gs_dark_gray));
+ dao_set_colors (&cb.dao, 0, 2, 0, 2 + cb.row,
+ style_color_new_go (GO_COLOR_BLACK),
+ style_color_new_gdk (&gs_light_gray));
+
+ dao_set_align (&cb.dao, 1, 1, cb.col + 1, 1, HALIGN_RIGHT,
+ VALIGN_BOTTOM);
+
+ *new_sheet = cb.dao.sheet;
+}
/********* Scenario Add UI **********************************************/
@@ -444,7 +694,7 @@ restore_old_values (ScenariosState *state)
wbc = WORKBOOK_CONTROL (state->base.wbcg);
dao_init_new_sheet (&dao);
dao.sheet = state->base.sheet;
- scenario_show (wbc, NULL,
+ scenario_show (NULL,
(GnmScenario *) state->scenario_state->old_values,
&dao);
state->scenario_state->current = NULL;
@@ -483,7 +733,7 @@ scenarios_cancel_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
}
/* Recover the deleted scenarios. */
- scenario_recover_all (state->base.sheet->scenarios);
+ scenario_recover_all (state->base.sheet);
scenario_manager_free (state);
gtk_widget_destroy (state->base.dialog);
@@ -513,10 +763,9 @@ scenarios_show_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
wbc = WORKBOOK_CONTROL (state->base.wbcg);
state->scenario_state->current =
- gnm_sheet_get_scenario (state->base.sheet, value);
+ gnm_sheet_scenario_find (state->base.sheet, value);
state->scenario_state->old_values =
- scenario_show (wbc,
- state->scenario_state->current,
+ scenario_show (state->scenario_state->current,
state->scenario_state->old_values,
&dao);
}
@@ -549,16 +798,18 @@ scenarios_delete_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
- sc = gnm_sheet_get_scenario (state->base.sheet, value);
+ sc = gnm_sheet_scenario_find (state->base.sheet, value);
if (sc)
- sc->marked_deleted = TRUE;
+ g_object_set_data (G_OBJECT (sc),
+ "marked_deleted", GUINT_TO_POINTER (TRUE));
set_selection_state (state, FALSE);
all_deleted = TRUE;
for (l = state->base.sheet->scenarios; l && all_deleted; l = l->next) {
GnmScenario *sc = l->data;
- all_deleted = sc->marked_deleted;
+ if (!g_object_get_data (G_OBJECT (sc), "marked_deleted"))
+ all_deleted = FALSE;
}
gtk_widget_set_sensitive
diff --git a/src/dialogs/dialog-solver.c b/src/dialogs/dialog-solver.c
index 7f525ac..c29745b 100644
--- a/src/dialogs/dialog-solver.c
+++ b/src/dialogs/dialog-solver.c
@@ -787,7 +787,7 @@ solver_add_scenario (SolverState *state, GnmSolverResult *res, gchar const *name
scenario_add_new (name, input_range,
value_peek_string (gnm_solver_param_get_input (param)),
comment, state->sheet, &scenario);
- scenario_add (state->sheet, scenario);
+ gnm_sheet_scenario_add (scenario->sheet, scenario);
value_release (input_range);
}
diff --git a/src/sheet.c b/src/sheet.c
index b8c1bf3..f269bb4 100644
--- a/src/sheet.c
+++ b/src/sheet.c
@@ -5778,8 +5778,59 @@ gnm_sheet_set_solver_params (Sheet *sheet, GnmSolverParameters *param)
sheet->solver_parameters = param;
}
+/* ------------------------------------------------------------------------- */
+
GnmScenario *
-gnm_sheet_get_scenario (Sheet *sheet, const char *name)
+gnm_sheet_scenario_new (Sheet *sheet, const char *name)
+{
+ GnmScenario *sc;
+ char *actual_name;
+
+ g_return_val_if_fail (IS_SHEET (sheet), NULL);
+ g_return_val_if_fail (name != NULL, NULL);
+
+ /* Check if a scenario having the same name already exists. */
+ if (gnm_sheet_scenario_find (sheet, name)) {
+ GString *str = g_string_new (NULL);
+ gchar *tmp;
+ int i, j, len;
+
+ len = strlen (name);
+ if (len > 1 && name [len - 1] == ']') {
+ for (i = len - 2; i > 0; i--) {
+ if (! g_ascii_isdigit (name [i]))
+ break;
+ }
+
+ tmp = g_strdup (name);
+ if (i > 0 && name [i] == '[')
+ tmp [i] = '\0';
+ } else
+ tmp = g_strdup (name);
+
+ for (j = 1; ; j++) {
+ g_string_printf (str, "%s [%d]", tmp, j);
+ if (!gnm_sheet_scenario_find (sheet, name)) {
+ actual_name = g_string_free (str, FALSE);
+ str = NULL;
+ break;
+ }
+ }
+ if (str)
+ g_string_free (str, TRUE);
+ g_free (tmp);
+ } else
+ actual_name = g_strdup (name);
+
+ sc = gnm_scenario_new (actual_name, NULL, sheet);
+
+ g_free (actual_name);
+
+ return sc;
+}
+
+GnmScenario *
+gnm_sheet_scenario_find (Sheet *sheet, const char *name)
{
GList *l;
@@ -5795,6 +5846,28 @@ gnm_sheet_get_scenario (Sheet *sheet, const char *name)
return NULL;
}
+void
+gnm_sheet_scenario_add (Sheet *sheet, GnmScenario *sc)
+{
+ g_return_if_fail (IS_SHEET (sheet));
+ g_return_if_fail (GNM_IS_SCENARIO (sc));
+
+ /* We take ownership of the ref. */
+ sheet->scenarios = g_list_append (sheet->scenarios, sc);
+}
+
+void
+gnm_sheet_scenario_remove (Sheet *sheet, GnmScenario *sc)
+{
+ g_return_if_fail (IS_SHEET (sheet));
+ g_return_if_fail (GNM_IS_SCENARIO (sc));
+
+ sheet->scenarios = g_list_remove (sheet->scenarios, sc);
+ g_object_unref (sc);
+}
+
+/* ------------------------------------------------------------------------- */
+
GHashTable *
gnm_sheet_get_sort_setups (Sheet *sheet)
{
diff --git a/src/sheet.h b/src/sheet.h
index 61ba6db..2178399 100644
--- a/src/sheet.h
+++ b/src/sheet.h
@@ -281,7 +281,10 @@ void gnm_sheet_foreach_name (Sheet const *sheet, GHFunc func, gpointer data);
void gnm_sheet_set_solver_params (Sheet *sheet, GnmSolverParameters *param);
-GnmScenario *gnm_sheet_get_scenario (Sheet *sheet, const char *name);
+GnmScenario *gnm_sheet_scenario_new (Sheet *sheet, const char *name);
+GnmScenario *gnm_sheet_scenario_find (Sheet *sheet, const char *name);
+void gnm_sheet_scenario_add (Sheet *sheet, GnmScenario *sc);
+void gnm_sheet_scenario_remove (Sheet *sheet, GnmScenario *sc);
GHashTable *gnm_sheet_get_sort_setups (Sheet *sheet);
void gnm_sheet_add_sort_setup (Sheet *sheet, char *key, gpointer setup);
diff --git a/src/tools/scenarios.c b/src/tools/scenarios.c
index 7a0b771..5074a37 100644
--- a/src/tools/scenarios.c
+++ b/src/tools/scenarios.c
@@ -119,55 +119,6 @@ GSF_CLASS (GnmScenario, gnm_scenario,
/* ------------------------------------------------------------------------- */
-static GnmScenario *
-scenario_new (Sheet *sheet, gchar const *name, gchar const *comment)
-{
- GnmScenario *sc;
- char *actual_name = g_strdup (name);
-
- /* Check if a scenario having the same name already exists. */
- if (gnm_sheet_get_scenario (sheet, name)) {
- GString *str = g_string_new (NULL);
- gchar *tmp;
- int i, j, len;
-
- len = strlen (name);
- if (len > 1 && name [len - 1] == ']') {
- for (i = len - 2; i > 0; i--) {
- if (! g_ascii_isdigit (name [i]))
- break;
- }
-
- tmp = g_strdup (name);
- if (i > 0 && name [i] == '[')
- tmp [i] = '\0';
- } else
- tmp = g_strdup (name);
-
- for (j = 1; j < 10000; j++) {
- g_string_printf (str, "%s [%d]", tmp, j);
- if (!gnm_sheet_get_scenario (sheet, name)) {
- actual_name = g_string_free (str, FALSE);
- str = NULL;
- break;
- }
- }
- if (str)
- g_string_free (str, TRUE);
- g_free (tmp);
- }
-
- sc = gnm_scenario_new (actual_name, comment, sheet);
- g_free (actual_name);
-
- sc->changing_cells = NULL;
- sc->cell_sel_str = NULL;
- sc->marked_deleted = FALSE;
-
- return sc;
-}
-
-
typedef struct {
gboolean expr_flag;
Sheet *sheet;
@@ -215,9 +166,10 @@ scenario_add_new (gchar const *name,
Sheet *sheet,
GnmScenario **new_scenario)
{
- GnmScenario *scenario = scenario_new (sheet, name, comment);
+ GnmScenario *scenario = gnm_sheet_scenario_new (sheet, name);
gboolean res = collect_values (sheet, scenario, (GnmValueRange *) changing_cells);
+ scenario->comment = g_strdup (comment);
scenario->cell_sel_str = g_strdup (cell_sel_str);
sheet_redraw_all (sheet, TRUE);
@@ -225,12 +177,6 @@ scenario_add_new (gchar const *name,
return res;
}
-void
-scenario_add (Sheet *sheet, GnmScenario *scenario)
-{
- sheet->scenarios = g_list_append (sheet->scenarios, scenario);
-}
-
/* Scenario: Duplicate sheet ***********************************************/
typedef struct {
@@ -273,23 +219,6 @@ gnm_scenario_dup (GnmScenario *src, Sheet *new_sheet)
return dst;
}
-/* Scenario: Remove sheet *************************************************/
-
-void
-scenario_delete (Sheet *sheet, gchar *name)
-{
- GnmScenario *sc;
-
- sc = gnm_sheet_get_scenario (sheet, name);
- if (sc) {
- sheet->scenarios = g_list_remove (sheet->scenarios, sc);
- g_object_unref (sc);
- }
-}
-
-/* Scenario: Show **********************************************************/
-
-
static GnmValue *
show_cb (int col, int row, GnmValue *v, data_analysis_output_t *dao)
{
@@ -299,8 +228,7 @@ show_cb (int col, int row, GnmValue *v, data_analysis_output_t *dao)
}
GnmScenario *
-scenario_show (WorkbookControl *wbc,
- GnmScenario *s,
+scenario_show (GnmScenario *s,
GnmScenario *old_values,
data_analysis_output_t *dao)
{
@@ -319,256 +247,20 @@ scenario_show (WorkbookControl *wbc,
return NULL;
/* Store values for recovery. */
- stored_values = scenario_new (dao->sheet, "", "");
+ stored_values = gnm_sheet_scenario_new (s->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;
+ cb.sheet = s->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 (wb_control_get_workbook (wbc));
- sheet_redraw_all (dao->sheet, TRUE);
+ workbook_recalc (s->sheet->workbook);
+ sheet_redraw_all (s->sheet, TRUE);
return stored_values;
}
-
-/* Scenario Manager: Ok/Cancel buttons************************************/
-
-/* Ok button pressed. */
-void
-scenario_manager_ok (Sheet *sheet)
-{
- GList *cur, *scenarios = sheet->scenarios;
- GList *list = NULL;
-
- /* Update scenarios (free the deleted ones). */
- for (cur = scenarios; cur != NULL; cur = cur->next) {
- GnmScenario *s = cur->data;
-
- if (s->marked_deleted)
- g_object_unref (s);
- else
- list = g_list_append (list, s);
- }
- g_list_free (scenarios);
- sheet->scenarios = list;
-
- sheet_redraw_all (sheet, TRUE);
-}
-
-/* Cancel button pressed. */
-void
-scenario_recover_all (GList *scenarios)
-{
- while (scenarios) {
- GnmScenario *s = scenarios->data;
-
- s->marked_deleted = FALSE;
- scenarios = scenarios->next;
- }
-}
-
-/* Scenario: Create summary report ***************************************/
-
-static void
-rm_fun_cb (gpointer key, gpointer value, gpointer user_data)
-{
- g_free (value);
-}
-
-typedef struct {
- data_analysis_output_t dao;
-
- Sheet *sheet;
- GHashTable *names; /* A hash table for cell names->row. */
- int col;
- int row;
- GSList *results;
-} summary_cb_t;
-
-static GnmValue *
-summary_cb (int col, int row, GnmValue *v, summary_cb_t *p)
-{
- char *tmp = dao_find_name (p->sheet, col, row);
- int *index;
-
- /* Check if some of the previous scenarios already included that
- * cell. If so, it's row will be put into *index. */
- index = g_hash_table_lookup (p->names, tmp);
- if (index != NULL) {
- dao_set_cell_value (&p->dao, 2 + p->col, 3 + *index,
- value_dup (v));
-
- /* Set the colors. */
- dao_set_colors (&p->dao, 2 + p->col, 3 + *index,
- 2 + p->col, 3 + *index,
- style_color_new_go (GO_COLOR_BLACK),
- style_color_new_gdk (&gs_light_gray));
-
- } else {
- /* New cell. */
- GnmCell *cell;
- int *r;
-
- /* Changing cell name. */
- dao_set_cell (&p->dao, 0, 3 + p->row, tmp);
-
- /* GnmValue of the cell in this scenario. */
- dao_set_cell_value (&p->dao, 2 + p->col, 3 + p->row,
- value_dup (v));
-
- /* Current value of the cell. */
- cell = sheet_cell_fetch (p->sheet, col, row);
- dao_set_cell_value (&p->dao, 1, 3 + p->row,
- value_dup (cell->value));
-
- /* Set the colors. */
- dao_set_colors (&p->dao, 2 + p->col, 3 + p->row,
- 2 + p->col, 3 + p->row,
- style_color_new_go (GO_COLOR_BLACK),
- style_color_new_gdk (&gs_light_gray));
-
- /* Insert row number into the hash table. */
- r = g_new (int, 1);
- *r = p->row;
- g_hash_table_insert (p->names, tmp, r);
-
- /* Increment the nbr of rows. */
- p->row++;
- }
-
- return v;
-}
-
-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 (wbc, 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 (wbc, 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);
-}
-
-void
-scenario_summary (WorkbookControl *wbc,
- Sheet *sheet,
- GSList *results,
- Sheet **new_sheet)
-{
- summary_cb_t cb;
- GList *cur;
- GList *scenarios = sheet->scenarios;
-
- /* Initialize: Currently only new sheet output supported. */
- dao_init_new_sheet (&cb.dao);
- dao_prepare_output (wbc, &cb.dao, _("Scenario Summary"));
-
- /* Titles. */
- dao_set_cell (&cb.dao, 1, 1, _("Current Values"));
- dao_set_cell (&cb.dao, 0, 2, _("Changing Cells:"));
-
- /* Go through all scenarios. */
- cb.row = 0;
- cb.names = g_hash_table_new (g_str_hash, g_str_equal);
- cb.sheet = sheet;
- cb.results = results;
- for (cb.col = 0, cur = scenarios; cur != NULL; cb.col++,
- cur = cur->next) {
- GnmScenario *s = cur->data;
-
- /* Scenario name. */
- dao_set_cell (&cb.dao, 2 + cb.col, 1, s->name);
-
- scenario_for_each_value (s, (ScenarioValueCB) summary_cb, &cb);
- }
-
- /* Set the alignment of names of the changing cells to be right. */
- dao_set_align (&cb.dao, 0, 3, 0, 2 + cb.row, HALIGN_RIGHT,
- VALIGN_BOTTOM);
-
- /* Result cells. */
- if (results != NULL)
- scenario_summary_res_cells (wbc, results, &cb);
-
- /* Destroy the hash table. */
- g_hash_table_foreach (cb.names, (GHFunc) rm_fun_cb, NULL);
- g_hash_table_destroy (cb.names);
-
- /* Clean up the report output. */
- dao_set_bold (&cb.dao, 0, 0, 0, 2 + cb.row);
- dao_autofit_columns (&cb.dao);
- dao_set_cell (&cb.dao, 0, 0, _("Scenario Summary"));
-
- dao_set_colors (&cb.dao, 0, 0, cb.col + 1, 1,
- style_color_new_go (GO_COLOR_WHITE),
- style_color_new_gdk (&gs_dark_gray));
- dao_set_colors (&cb.dao, 0, 2, 0, 2 + cb.row,
- style_color_new_go (GO_COLOR_BLACK),
- style_color_new_gdk (&gs_light_gray));
-
- dao_set_align (&cb.dao, 1, 1, cb.col + 1, 1, HALIGN_RIGHT,
- VALIGN_BOTTOM);
-
- *new_sheet = cb.dao.sheet;
-}
diff --git a/src/tools/scenarios.h b/src/tools/scenarios.h
index 6a9df5c..be4b64a 100644
--- a/src/tools/scenarios.h
+++ b/src/tools/scenarios.h
@@ -22,8 +22,6 @@ struct GnmScenario_ {
GnmRange range;
gchar *cell_sel_str;
-
- gboolean marked_deleted;
};
typedef struct {
@@ -49,22 +47,13 @@ typedef struct _scenario_cmd_t {
GnmScenario *scenario_by_name (GList *scenarios, const gchar *name,
gboolean *all_deleted);
-void scenario_manager_ok (Sheet *sheet);
-GnmScenario *scenario_show (WorkbookControl *wbc,
- GnmScenario *scenario,
- GnmScenario *old_values,
- data_analysis_output_t *dao);
+GnmScenario *scenario_show (GnmScenario *scenario,
+ GnmScenario *old_values,
+ data_analysis_output_t *dao);
gboolean scenario_add_new (gchar const *name,
GnmValue *changing_cells,
gchar const *cell_sel_str,
gchar const *comment,
Sheet *sheet, GnmScenario **new_scenario);
-void scenario_add (Sheet *sheet, GnmScenario *scenario);
-void scenario_delete (Sheet *sheet, gchar *name);
-void scenario_summary (WorkbookControl *wbc,
- Sheet *sheet,
- GSList *results,
- Sheet **new_sheet);
-void scenario_recover_all (GList *scenarios);
#endif
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]