1.12.42
[gnumeric.git] / src / dialogs / dialog-analysis-tool-normality.c
blob97a3d68fd5d37814ce2b683ce1961e86a7e7b4ae
1 /*
2 * dialog-analysis-tool-normality.c:
4 * Authors:
5 * Andreas J. Guelzow <aguelzow@taliesin.ca>
7 * (C) Copyright 2009 by Andreas J. Guelzow <aguelzow@pyrshep.ca>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <https://www.gnu.org/licenses/>.
23 #include <gnumeric-config.h>
24 #include <glib/gi18n-lib.h>
25 #include <gnumeric.h>
26 #include <dialogs/dialogs.h>
27 #include <tools/analysis-normality.h>
28 #include <tools/analysis-tools.h>
30 #include <workbook.h>
31 #include <workbook-control.h>
32 #include <wbc-gtk.h>
33 #include <workbook-view.h>
34 #include <gui-util.h>
35 #include <parse-util.h>
36 #include <gnm-format.h>
37 #include <dialogs/tool-dialogs.h>
38 #include <dialogs/dao-gui-utils.h>
39 #include <sheet.h>
40 #include <expr.h>
41 #include <number-match.h>
42 #include <ranges.h>
43 #include <selection.h>
44 #include <value.h>
45 #include <commands.h>
46 #include <dialogs/help.h>
48 #include <widgets/gnm-dao.h>
49 #include <widgets/gnm-expr-entry.h>
51 #include <string.h>
53 #define NORMALITY_KEY "analysistools-normality-dialog"
55 typedef struct {
56 GnmGenericToolState base;
57 GtkWidget *alpha_entry;
58 } NormalityTestsToolState;
61 static char const * const grouped_by_group[] = {
62 "grouped_by_row",
63 "grouped_by_col",
64 "grouped_by_area",
65 NULL
68 static char const * const test_group[] = {
69 "andersondarling",
70 "cramervonmises",
71 "lilliefors",
72 "shapirofrancia",
73 NULL
76 /**
77 * normality_tool_update_sensitivity_cb:
78 * @dummy:
79 * @state:
81 * Update the dialog widgets sensitivity
82 **/
83 static void
84 normality_tool_update_sensitivity_cb (G_GNUC_UNUSED GtkWidget *dummy,
85 NormalityTestsToolState *state)
87 gnm_float alpha;
88 GSList *input_range;
90 input_range = gnm_expr_entry_parse_as_list (
91 GNM_EXPR_ENTRY (state->base.input_entry),
92 state->base.sheet);
93 if (input_range == NULL) {
94 gtk_label_set_text (GTK_LABEL (state->base.warning),
95 _("The input range is invalid."));
96 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
97 return;
98 } else
99 range_list_destroy (input_range);
101 /* Checking Alpha*/
102 alpha = gtk_spin_button_get_value
103 (GTK_SPIN_BUTTON (state->alpha_entry));
104 if (!(alpha > 0 && alpha < 1)) {
105 gtk_label_set_text (GTK_LABEL (state->base.warning),
106 _("The alpha value should "
107 "be a number between 0 and 1."));
108 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
109 return;
112 /* Checking Output Page */
113 if (!gnm_dao_is_ready (GNM_DAO (state->base.gdao))) {
114 gtk_label_set_text (GTK_LABEL (state->base.warning),
115 _("The output specification "
116 "is invalid."));
117 gtk_widget_set_sensitive (state->base.ok_button, FALSE);
118 return;
121 gtk_label_set_text (GTK_LABEL (state->base.warning), "");
122 gtk_widget_set_sensitive (state->base.ok_button, TRUE);
128 * normality_tool_ok_clicked_cb:
129 * @button:
130 * @state:
132 * Retrieve the information from the dialog and call the normality_tool.
133 * Note that we assume that the ok_button is only active if the entry fields
134 * contain sensible data.
136 static void
137 normality_tool_ok_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
138 NormalityTestsToolState *state)
140 data_analysis_output_t *dao;
141 GtkWidget *w;
142 analysis_tools_data_normality_t *data;
144 data = g_new0 (analysis_tools_data_normality_t, 1);
145 dao = parse_output ((GnmGenericToolState *)state, NULL);
147 data->base.input = gnm_expr_entry_parse_as_list (
148 GNM_EXPR_ENTRY (state->base.input_entry), state->base.sheet);
149 data->base.group_by = gnm_gui_group_value (state->base.gui, grouped_by_group);
151 w = go_gtk_builder_get_widget (state->base.gui, "labels_button");
152 data->base.labels = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w));
153 data->alpha = gtk_spin_button_get_value
154 (GTK_SPIN_BUTTON (state->alpha_entry));
156 data->type = gnm_gui_group_value (state->base.gui, test_group);
158 w = go_gtk_builder_get_widget (state->base.gui, "normalprobabilityplot");
159 data->graph = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w));
161 if (!cmd_analysis_tool (GNM_WBC (state->base.wbcg),
162 state->base.sheet,
163 dao, data, analysis_tool_normality_engine,
164 TRUE))
165 gtk_widget_destroy (state->base.dialog);
167 return;
171 * dialog_normality_tool:
172 * @wbcg:
173 * @sheet:
175 * Show the dialog (guru).
179 dialog_normality_tool (WBCGtk *wbcg, Sheet *sheet)
181 NormalityTestsToolState *state;
182 char const * plugins[] = { "Gnumeric_fnstat",
183 "Gnumeric_fnlogical",
184 NULL};
186 if ((wbcg == NULL) ||
187 gnm_check_for_plugins_missing (plugins, wbcg_toplevel (wbcg)))
188 return 1;
190 /* Only pop up one copy per workbook */
191 if (gnm_dialog_raise_if_exists (wbcg, NORMALITY_KEY))
192 return 0;
194 state = g_new0 (NormalityTestsToolState, 1);
196 if (dialog_tool_init (&state->base, wbcg, sheet,
197 GNUMERIC_HELP_LINK_NORMALITY,
198 "res:ui/normality-tests.ui", "Normality-Tests",
199 _("Could not create the Normality Test Tool dialog."),
200 NORMALITY_KEY,
201 G_CALLBACK (normality_tool_ok_clicked_cb), NULL,
202 G_CALLBACK (normality_tool_update_sensitivity_cb),
205 g_free(state);
206 return 0;
210 state->alpha_entry = tool_setup_update
211 (&state->base, "alpha-entry",
212 G_CALLBACK (normality_tool_update_sensitivity_cb),
213 state);
215 float_to_entry (GTK_ENTRY (state->alpha_entry), 0.05);
217 gnm_dao_set_put (GNM_DAO (state->base.gdao), TRUE, TRUE);
218 normality_tool_update_sensitivity_cb (NULL, state);
219 tool_load_selection ((GnmGenericToolState *)state, TRUE);
221 return 0;