1 /*
2  * dialog-analysis-tool-wilcoxon-mann-whitney.c:
3  *
4  * Authors:
5   *  Andreas J. Guelzow  <aguelzow@pyrshep.ca>
6  *
7  * (C) Copyright 2010 by Andreas J. Guelzow  <aguelzow@pyrshep.ca>
8  *
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.
13  *
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.
18  *
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/>.
21  */
22 
23 #include <gnumeric-config.h>
24 #include <glib/gi18n-lib.h>
25 #include <gnumeric.h>
26 #include <dialogs/dialogs.h>
27 #include <tools/analysis-wilcoxon-mann-whitney.h>
28 #include <tools/analysis-tools.h>
29 
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>
47 
48 #include <widgets/gnm-dao.h>
49 #include <widgets/gnm-expr-entry.h>
50 
51 #include <string.h>
52 
53 #define WILCOXON_MANN_WHITNEY_KEY "analysistools-principal-components-dialog"
54 
55 #if 0
56 static char const * const grouped_by_group[] = {
57 	"grouped_by_row",
58 	"grouped_by_col",
59 	"grouped_by_area",
60 	NULL
61 };
62 #endif
63 
64 static void
wilcoxon_mann_whitney_tool_update_sensitivity_cb(G_GNUC_UNUSED GtkWidget * dummy,GnmGenericToolState * state)65 wilcoxon_mann_whitney_tool_update_sensitivity_cb (G_GNUC_UNUSED GtkWidget *dummy,
66 						 GnmGenericToolState *state)
67 {
68         GnmValue *input_range;
69         GnmValue *input_range_2;
70 	gboolean input_1_ready  = FALSE;
71 	gboolean input_2_ready  = FALSE;
72 
73 	/* Checking Input Ranges */
74         input_range = gnm_expr_entry_parse_as_value (
75 		GNM_EXPR_ENTRY (state->input_entry), state->sheet);
76 	input_range_2 = gnm_expr_entry_parse_as_value
77 		(GNM_EXPR_ENTRY (state->input_entry_2), state->sheet);
78 
79 	input_1_ready = (input_range != NULL);
80 	input_2_ready = ((state->input_entry_2 == NULL) || (input_range_2 != NULL));
81         value_release (input_range);
82         value_release (input_range_2);
83 
84 	if (!input_1_ready) {
85 		gtk_label_set_text (GTK_LABEL (state->warning),
86 				    _("The input range for variable 1 is invalid."));
87 		gtk_widget_set_sensitive (state->ok_button, FALSE);
88 		return;
89 	} else if (!input_2_ready) {
90 		gtk_label_set_text (GTK_LABEL (state->warning),
91 				    _("The input range for variable 2 is invalid."));
92 		gtk_widget_set_sensitive (state->ok_button, FALSE);
93 		return;
94 	}
95 
96 	/* Checking Output Page */
97 	if (!gnm_dao_is_ready (GNM_DAO (state->gdao))) {
98 		gtk_label_set_text (GTK_LABEL (state->warning),
99 				    _("The output specification "
100 				      "is invalid."));
101 		gtk_widget_set_sensitive (state->ok_button, FALSE);
102 		return;
103 	}
104 
105 	gtk_label_set_text (GTK_LABEL (state->warning), "");
106 	gtk_widget_set_sensitive (state->ok_button, TRUE);
107 
108 	return;
109 }
110 
111 /**
112  * wilcoxon_mann_whitney_tool_ok_clicked_cb:
113  * @button:
114  * @state:
115  *
116  **/
117 static void
wilcoxon_mann_whitney_tool_ok_clicked_cb(G_GNUC_UNUSED GtkWidget * button,GnmGenericToolState * state)118 wilcoxon_mann_whitney_tool_ok_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
119 			GnmGenericToolState *state)
120 {
121 	data_analysis_output_t  *dao;
122 	analysis_tools_data_generic_b_t  *data;
123 
124  	GtkWidget *w;
125 
126 	if (state->warning_dialog != NULL)
127 		gtk_widget_destroy (state->warning_dialog);
128 
129 	data = g_new0 (analysis_tools_data_generic_b_t, 1);
130 	dao  = parse_output (state, NULL);
131 
132 	data->wbc = GNM_WBC (state->wbcg);
133 
134 	data->range_1 = gnm_expr_entry_parse_as_value
135 		(GNM_EXPR_ENTRY (state->input_entry), state->sheet);
136 
137 	data->range_2 =  gnm_expr_entry_parse_as_value
138 		(GNM_EXPR_ENTRY (state->input_entry_2), state->sheet);
139 
140 	w = go_gtk_builder_get_widget (state->gui, "labels_button");
141         data->labels = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w));
142 
143 	if (cmd_analysis_tool (GNM_WBC (state->wbcg), state->sheet,
144 			       dao, data,
145 			       analysis_tool_wilcoxon_mann_whitney_engine, TRUE)) {
146 		char   *text;
147 		text = g_strdup_printf (
148 			_("An unexpected error has occurred."));
149 		error_in_entry (state, GTK_WIDGET (state->input_entry), text);
150 		g_free (text);
151 	} else
152 		gtk_widget_destroy (state->dialog);
153 	return;
154 }
155 
156 
157 
158 /**
159  * dialog_wilcoxon_mann_whitney_tool:
160  * @wbcg:
161  * @sheet:
162  *
163  * Show the dialog (guru).
164  *
165  **/
166 int
dialog_wilcoxon_m_w_tool(WBCGtk * wbcg,Sheet * sheet)167 dialog_wilcoxon_m_w_tool (WBCGtk *wbcg, Sheet *sheet)
168 {
169         GnmGenericToolState *state;
170 	char const * plugins[] = { "Gnumeric_fnstat",
171 				   "Gnumeric_fnmath",
172 				   "Gnumeric_fnlookup",
173 				   NULL};
174 
175 	if ((wbcg == NULL) ||
176 	    gnm_check_for_plugins_missing (plugins, wbcg_toplevel (wbcg)))
177 		return 1;
178 
179 	/* Only pop up one copy per workbook */
180 	if (gnm_dialog_raise_if_exists (wbcg, WILCOXON_MANN_WHITNEY_KEY))
181 		return 0;
182 
183 	state = g_new0 (GnmGenericToolState, 1);
184 
185 	if (dialog_tool_init (state, wbcg, sheet,
186 			      GNUMERIC_HELP_LINK_WILCOXON_MANN_WHITNEY,
187 			      "res:ui/wilcoxon-mann-whitney.ui", "WilcoxonMannWhitney",
188 			      _("Could not create the Wilcoxon-Mann-Whitney Analysis Tool dialog."),
189 			      WILCOXON_MANN_WHITNEY_KEY,
190 			      G_CALLBACK (wilcoxon_mann_whitney_tool_ok_clicked_cb), NULL,
191 			      G_CALLBACK (wilcoxon_mann_whitney_tool_update_sensitivity_cb),
192 			      GNM_EE_SINGLE_RANGE))
193 		return 0;
194 
195 	gnm_dao_set_put (GNM_DAO (state->gdao), TRUE, TRUE);
196 	wilcoxon_mann_whitney_tool_update_sensitivity_cb (NULL, state);
197 	tool_load_selection ((GnmGenericToolState *)state, TRUE);
198 
199         return 0;
200 }
201