dialog-analysis-tool-normality.c 5.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * dialog-analysis-tool-normality.c:
 *
 * Authors:
  *  Andreas J. Guelzow  <aguelzow@taliesin.ca>
 *
 * (C) Copyright 2009 by Andreas J. Guelzow  <aguelzow@pyrshep.ca>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
22 23 24 25 26 27
 */

#include <gnumeric-config.h>
#include <glib/gi18n-lib.h>
#include <gnumeric.h>
#include "dialogs.h"
28 29
#include "tools/analysis-normality.h"
#include "tools/analysis-tools.h"
30 31 32 33 34 35 36 37

#include <workbook.h>
#include <workbook-control.h>
#include <wbc-gtk.h>
#include <workbook-view.h>
#include <gui-util.h>
#include <parse-util.h>
#include <gnm-format.h>
38 39
#include <dialogs/tool-dialogs.h>
#include <dialogs/dao-gui-utils.h>
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include <sheet.h>
#include <expr.h>
#include <number-match.h>
#include <ranges.h>
#include <selection.h>
#include <value.h>
#include <commands.h>
#include "help.h"

#include <widgets/gnm-dao.h>
#include <widgets/gnumeric-expr-entry.h>

#include <string.h>

#define NORMALITY_KEY      "analysistools-normality-dialog"

typedef struct {
57
	GnmGenericToolState base;
58 59 60 61 62 63 64 65 66 67 68
	GtkWidget *alpha_entry;
} NormalityTestsToolState;


static char const * const grouped_by_group[] = {
	"grouped_by_row",
	"grouped_by_col",
	"grouped_by_area",
	NULL
};

69 70 71 72 73 74 75 76
static char const * const test_group[] = {
	"andersondarling",
	"cramervonmises",
	"lilliefors",
	"shapirofrancia",
	NULL
};

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
/**
 * normality_tool_update_sensitivity_cb:
 * @dummy:
 * @state:
 *
 * Update the dialog widgets sensitivity
 **/
static void
normality_tool_update_sensitivity_cb (G_GNUC_UNUSED GtkWidget *dummy,
				      NormalityTestsToolState *state)
{
	gnm_float alpha;
        GSList *input_range;

        input_range = gnm_expr_entry_parse_as_list (
		GNM_EXPR_ENTRY (state->base.input_entry),
		state->base.sheet);
	if (input_range == NULL) {
		gtk_label_set_text (GTK_LABEL (state->base.warning),
				    _("The input range is invalid."));
		gtk_widget_set_sensitive (state->base.ok_button, FALSE);
		return;
	} else
		range_list_destroy (input_range);

	/* Checking Alpha*/
	alpha = gtk_spin_button_get_value
		(GTK_SPIN_BUTTON (state->alpha_entry));
	if (!(alpha > 0 && alpha < 1)) {
		gtk_label_set_text (GTK_LABEL (state->base.warning),
				    _("The alpha value should "
				      "be a number between 0 and 1."));
		gtk_widget_set_sensitive (state->base.ok_button, FALSE);
		return;
	}

	/* Checking Output Page */
	if (!gnm_dao_is_ready (GNM_DAO (state->base.gdao))) {
		gtk_label_set_text (GTK_LABEL (state->base.warning),
				    _("The output specification "
				      "is invalid."));
		gtk_widget_set_sensitive (state->base.ok_button, FALSE);
		return;
	}

	gtk_label_set_text (GTK_LABEL (state->base.warning), "");
	gtk_widget_set_sensitive (state->base.ok_button, TRUE);

}


/**
 * normality_tool_ok_clicked_cb:
 * @button:
 * @state:
 *
 * Retrieve the information from the dialog and call the normality_tool.
 * Note that we assume that the ok_button is only active if the entry fields
 * contain sensible data.
 **/
static void
normality_tool_ok_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
			      NormalityTestsToolState *state)
{
	data_analysis_output_t  *dao;
	GtkWidget *w;
	analysis_tools_data_normality_t *data;

	data = g_new0 (analysis_tools_data_normality_t, 1);
146
	dao  = parse_output ((GnmGenericToolState *)state, NULL);
147 148 149

	data->base.input = gnm_expr_entry_parse_as_list (
		GNM_EXPR_ENTRY (state->base.input_entry), state->base.sheet);
150
	data->base.group_by = gnm_gui_group_value (state->base.gui, grouped_by_group);
151

Morten Welinder's avatar
Morten Welinder committed
152
	w = go_gtk_builder_get_widget (state->base.gui, "labels_button");
153 154 155 156
        data->base.labels = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w));
	data->alpha = gtk_spin_button_get_value
		(GTK_SPIN_BUTTON (state->alpha_entry));

157
	data->type = gnm_gui_group_value (state->base.gui, test_group);
158

Morten Welinder's avatar
Morten Welinder committed
159
	w = go_gtk_builder_get_widget (state->base.gui, "normalprobabilityplot");
160
	data->graph = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w));
161

Morten Welinder's avatar
Morten Welinder committed
162
	if (!cmd_analysis_tool (GNM_WBC (state->base.wbcg),
163
				state->base.sheet,
Morten Welinder's avatar
Morten Welinder committed
164
				dao, data, analysis_tool_normality_engine,
165
				TRUE))
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
		gtk_widget_destroy (state->base.dialog);

	return;
}

/**
 * dialog_normality_tool:
 * @wbcg:
 * @sheet:
 *
 * Show the dialog (guru).
 *
 **/
int
dialog_normality_tool (WBCGtk *wbcg, Sheet *sheet)
{
        NormalityTestsToolState *state;
	char const * plugins[] = { "Gnumeric_fnstat",
				   "Gnumeric_fnlogical",
				   NULL};

	if ((wbcg == NULL) ||
	    gnm_check_for_plugins_missing (plugins, wbcg_toplevel (wbcg)))
		return 1;

	/* Only pop up one copy per workbook */
Morten Welinder's avatar
Morten Welinder committed
192
	if (gnm_dialog_raise_if_exists (wbcg, NORMALITY_KEY))
193 194 195 196 197 198
		return 0;

	state = g_new0 (NormalityTestsToolState, 1);

	if (dialog_tool_init (&state->base, wbcg, sheet,
			      GNUMERIC_HELP_LINK_NORMALITY,
199
			      "res:ui/normality-tests.ui", "Normality-Tests",
200 201 202 203 204
			      _("Could not create the Normality Test Tool dialog."),
			      NORMALITY_KEY,
			      G_CALLBACK (normality_tool_ok_clicked_cb), NULL,
			      G_CALLBACK (normality_tool_update_sensitivity_cb),
			      0))
205 206
	{
		g_free(state);
207
		return 0;
208
	}
209 210


Morten Welinder's avatar
Morten Welinder committed
211
	state->alpha_entry = tool_setup_update
212
		(&state->base, "alpha-entry",
Morten Welinder's avatar
Morten Welinder committed
213
		 G_CALLBACK (normality_tool_update_sensitivity_cb),
214 215
		 state);

216 217 218 219
	float_to_entry (GTK_ENTRY (state->alpha_entry), 0.05);

	gnm_dao_set_put (GNM_DAO (state->base.gdao), TRUE, TRUE);
	normality_tool_update_sensitivity_cb (NULL, state);
220
	tool_load_selection ((GnmGenericToolState *)state, TRUE);
221 222 223

        return 0;
}