dialog-analysis-tool-principal-components.c 5.01 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-principal-components.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-principal-components.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 57 58 59 60 61 62 63 64
#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 PRINCIPAL_COMPONENTS_KEY "analysistools-principal-components-dialog"

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

static void
principal_components_tool_update_sensitivity_cb (G_GNUC_UNUSED GtkWidget *dummy,
65
						 GnmGenericToolState *state)
66 67 68 69 70 71 72 73 74 75 76 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
{
        GSList *input_range;

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

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

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

	return;
}

/**
 * principal_components_tool_ok_clicked_cb:
 * @button:
 * @state:
 *
 **/
static void
principal_components_tool_ok_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
103
			GnmGenericToolState *state)
104 105 106 107 108 109 110 111 112 113 114 115 116 117
{
	data_analysis_output_t  *dao;
	analysis_tools_data_generic_t  *data;

	GtkWidget *w;

	if (state->warning_dialog != NULL)
		gtk_widget_destroy (state->warning_dialog);

	data = g_new0 (analysis_tools_data_generic_t, 1);
	dao  = parse_output (state, NULL);

	data->input = gnm_expr_entry_parse_as_list (
		GNM_EXPR_ENTRY (state->input_entry), state->sheet);
118
	data->group_by = gnm_gui_group_value (state->gui, grouped_by_group);
119

Morten Welinder's avatar
Morten Welinder committed
120
	w = go_gtk_builder_get_widget (state->gui, "labels_button");
121 122
        data->labels = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w));

Morten Welinder's avatar
Morten Welinder committed
123
	if (cmd_analysis_tool (GNM_WBC (state->wbcg), state->sheet,
Morten Welinder's avatar
Morten Welinder committed
124
			       dao, data,
125 126 127
			       analysis_tool_principal_components_engine,
			       TRUE)) {
		char   *text;
128
		text = g_strdup_printf (
129
			_("An unexpected error has occurred."));
130
		error_in_entry ((GnmGenericToolState *) state,
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
				GTK_WIDGET (state->input_entry), text);
		g_free (text);
	} else
		gtk_widget_destroy (state->dialog);
	return;
}



/**
 * dialog_principal_components_tool:
 * @wbcg:
 * @sheet:
 *
 * Show the dialog (guru).
 *
 **/
int
dialog_principal_components_tool (WBCGtk *wbcg, Sheet *sheet)
{
151
        GnmGenericToolState *state;
Morten Welinder's avatar
Morten Welinder committed
152
	char const * plugins[] = { "Gnumeric_fnstat",
153 154 155 156 157 158 159 160 161
				   "Gnumeric_fnmath",
				   "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
162
	if (gnm_dialog_raise_if_exists (wbcg, PRINCIPAL_COMPONENTS_KEY))
163 164
		return 0;

165
	state = g_new0 (GnmGenericToolState, 1);
166 167 168

	if (dialog_tool_init (state, wbcg, sheet,
			      GNUMERIC_HELP_LINK_PRINCIPAL_COMPONENTS,
169
			      "res:ui/principal-components.ui", "PrincipalComponents",
170 171 172 173 174 175 176 177 178
			      _("Could not create the Principal Components Analysis Tool dialog."),
			      PRINCIPAL_COMPONENTS_KEY,
			      G_CALLBACK (principal_components_tool_ok_clicked_cb), NULL,
			      G_CALLBACK (principal_components_tool_update_sensitivity_cb),
			      GNM_EE_SINGLE_RANGE))
		return 0;

	gnm_dao_set_put (GNM_DAO (state->gdao), TRUE, TRUE);
	principal_components_tool_update_sensitivity_cb (NULL, state);
179
	tool_load_selection ((GnmGenericToolState *)state, TRUE);
180 181 182

        return 0;
}