GUI: Move .ui files from goffice resources to glib resources
[gnumeric.git] / src / dialogs / dialog-analysis-tool-principal-components.c
blob6b0e7941e484f2de80f82c2a04dc240bf7e56b83
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * dialog-analysis-tool-principal-components.c:
5 * Authors:
6 * Andreas J. Guelzow <aguelzow@taliesin.ca>
8 * (C) Copyright 2009 by Andreas J. Guelzow <aguelzow@pyrshep.ca>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * 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/>.
24 #include <gnumeric-config.h>
25 #include <glib/gi18n-lib.h>
26 #include <gnumeric.h>
27 #include "dialogs.h"
28 #include "analysis-principal-components.h"
29 #include "analysis-tools.h"
31 #include <workbook.h>
32 #include <workbook-control.h>
33 #include <wbc-gtk.h>
34 #include <workbook-view.h>
35 #include <gui-util.h>
36 #include <parse-util.h>
37 #include <gnm-format.h>
38 #include <tool-dialogs.h>
39 #include <dao-gui-utils.h>
40 #include <sheet.h>
41 #include <expr.h>
42 #include <number-match.h>
43 #include <ranges.h>
44 #include <selection.h>
45 #include <value.h>
46 #include <commands.h>
47 #include "help.h"
49 #include <widgets/gnm-dao.h>
50 #include <widgets/gnumeric-expr-entry.h>
52 #include <string.h>
53 #include <gtk/gtk.h>
55 #define PRINCIPAL_COMPONENTS_KEY "analysistools-principal-components-dialog"
57 static char const * const grouped_by_group[] = {
58 "grouped_by_row",
59 "grouped_by_col",
60 "grouped_by_area",
61 NULL
64 static void
65 principal_components_tool_update_sensitivity_cb (G_GNUC_UNUSED GtkWidget *dummy,
66 GenericToolState *state)
68 GSList *input_range;
70 /* Checking Input Range */
71 input_range = gnm_expr_entry_parse_as_list (
72 GNM_EXPR_ENTRY (state->input_entry), state->sheet);
73 if (input_range == NULL) {
74 gtk_label_set_text (GTK_LABEL (state->warning),
75 _("The input range is invalid."));
76 gtk_widget_set_sensitive (state->ok_button, FALSE);
77 return;
78 } else
79 range_list_destroy (input_range);
81 /* Checking Output Page */
82 if (!gnm_dao_is_ready (GNM_DAO (state->gdao))) {
83 gtk_label_set_text (GTK_LABEL (state->warning),
84 _("The output specification "
85 "is invalid."));
86 gtk_widget_set_sensitive (state->ok_button, FALSE);
87 return;
90 gtk_label_set_text (GTK_LABEL (state->warning), "");
91 gtk_widget_set_sensitive (state->ok_button, TRUE);
93 return;
96 /**
97 * principal_components_tool_ok_clicked_cb:
98 * @button:
99 * @state:
102 static void
103 principal_components_tool_ok_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
104 GenericToolState *state)
106 data_analysis_output_t *dao;
107 analysis_tools_data_generic_t *data;
109 GtkWidget *w;
111 if (state->warning_dialog != NULL)
112 gtk_widget_destroy (state->warning_dialog);
114 data = g_new0 (analysis_tools_data_generic_t, 1);
115 dao = parse_output (state, NULL);
117 data->input = gnm_expr_entry_parse_as_list (
118 GNM_EXPR_ENTRY (state->input_entry), state->sheet);
119 data->group_by = gnm_gui_group_value (state->gui, grouped_by_group);
121 w = go_gtk_builder_get_widget (state->gui, "labels_button");
122 data->labels = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w));
124 if (cmd_analysis_tool (GNM_WBC (state->wbcg), state->sheet,
125 dao, data,
126 analysis_tool_principal_components_engine,
127 TRUE)) {
128 char *text;
129 text = g_strdup_printf (
130 _("An unexpected error has occurred."));
131 error_in_entry ((GenericToolState *) state,
132 GTK_WIDGET (state->input_entry), text);
133 g_free (text);
134 } else
135 gtk_widget_destroy (state->dialog);
136 return;
142 * dialog_principal_components_tool:
143 * @wbcg:
144 * @sheet:
146 * Show the dialog (guru).
150 dialog_principal_components_tool (WBCGtk *wbcg, Sheet *sheet)
152 GenericToolState *state;
153 char const * plugins[] = { "Gnumeric_fnstat",
154 "Gnumeric_fnmath",
155 "Gnumeric_fnlogical",
156 NULL};
158 if ((wbcg == NULL) ||
159 gnm_check_for_plugins_missing (plugins, wbcg_toplevel (wbcg)))
160 return 1;
162 /* Only pop up one copy per workbook */
163 if (gnm_dialog_raise_if_exists (wbcg, PRINCIPAL_COMPONENTS_KEY))
164 return 0;
166 state = g_new0 (GenericToolState, 1);
168 if (dialog_tool_init (state, wbcg, sheet,
169 GNUMERIC_HELP_LINK_PRINCIPAL_COMPONENTS,
170 "res:ui/principal-components.ui", "PrincipalComponents",
171 _("Could not create the Principal Components Analysis Tool dialog."),
172 PRINCIPAL_COMPONENTS_KEY,
173 G_CALLBACK (principal_components_tool_ok_clicked_cb), NULL,
174 G_CALLBACK (principal_components_tool_update_sensitivity_cb),
175 GNM_EE_SINGLE_RANGE))
176 return 0;
178 gnm_dao_set_put (GNM_DAO (state->gdao), TRUE, TRUE);
179 principal_components_tool_update_sensitivity_cb (NULL, state);
180 tool_load_selection ((GenericToolState *)state, TRUE);
182 return 0;