summaryrefslogtreecommitdiffstats
path: root/scilab/modules
diff options
context:
space:
mode:
authorSamuel GOUGEON <sgougeon@free.fr>2021-01-09 02:01:10 +0100
committerClément David <clement.david@esi-group.com>2021-04-12 11:01:26 +0200
commit8c60701b51050b48aa565965188c3f4a06ccf479 (patch)
treedaf08555997a4d70303fa5d16db0650a8c2a14e1 /scilab/modules
parent7efe1ce40d3bc08d29036ea36d6e352e1835e0e6 (diff)
downloadscilab-8c60701b51050b48aa565965188c3f4a06ccf479.zip
scilab-8c60701b51050b48aa565965188c3f4a06ccf479.tar.gz
* Bug 16627: importgui => uiSpreadsheet. Internals => private. [fileio] => [ui_data]
http://bugzilla.scilab.org/16627 Change-Id: Ied3bec6e52cd343fa3f64d22d0a2824027057aec
Diffstat (limited to 'scilab/modules')
-rw-r--r--scilab/modules/fileio/etc/fileio.start1
-rw-r--r--scilab/modules/fileio/macros/importgui/importgui.sci173
-rw-r--r--scilab/modules/fileio/macros/importgui/importgui_analyze.sci32
-rw-r--r--scilab/modules/fileio/macros/importgui/importgui_cbselect.sci23
-rw-r--r--scilab/modules/fileio/macros/importgui/importgui_function.sci54
-rw-r--r--scilab/modules/fileio/macros/importgui/importgui_gethead.sci3
-rw-r--r--scilab/modules/fileio/macros/importgui/importgui_preview.sci130
-rw-r--r--scilab/modules/fileio/macros/importgui/importgui_progress.sci5
-rw-r--r--scilab/modules/fileio/macros/importgui/importgui_variable.sci43
-rw-r--r--scilab/modules/ui_data/macros/uiSpreadsheet.sci529
10 files changed, 531 insertions, 462 deletions
diff --git a/scilab/modules/fileio/etc/fileio.start b/scilab/modules/fileio/etc/fileio.start
index 18e8183..ec95d73 100644
--- a/scilab/modules/fileio/etc/fileio.start
+++ b/scilab/modules/fileio/etc/fileio.start
@@ -13,7 +13,6 @@
13//Load functions libraries 13//Load functions libraries
14// ============================================================================= 14// =============================================================================
15load('SCI/modules/fileio/macros/lib'); 15load('SCI/modules/fileio/macros/lib');
16load('SCI/modules/fileio/macros/importgui/lib');
17 16
18// Initialize CURL environment 17// Initialize CURL environment
19// ============================================================================= 18// =============================================================================
diff --git a/scilab/modules/fileio/macros/importgui/importgui.sci b/scilab/modules/fileio/macros/importgui/importgui.sci
index 9090c6f..87e0eef 100644
--- a/scilab/modules/fileio/macros/importgui/importgui.sci
+++ b/scilab/modules/fileio/macros/importgui/importgui.sci
@@ -1,174 +1,5 @@
1function importgui() 1function importgui()
2 win = get("importgui"); 2 warnobsolete("uiSpreadsheet", "6.2.0")
3 if ~isempty(win) then
4 //set visible
5 set("win", "visible", "on");
6 //set on top
7 set("win");
8 return;
9 end
10
11 w = 800;
12 h = 600;
13
14 fig = figure(...
15 "figure_name", _("Import data"), ...
16 "dockable", "off", ...
17 "axes_size", [w h], ...
18 "infobar_visible", "off", ...
19 "toolbar_visible", "off", ...
20 "menubar_visible", "off", ...
21 "default_axes", "off", ...
22 "background", -2, ...
23 "resize", "off", ...
24 "layout", "border", ...
25 "tag", "importgui", ...
26 "visible", "off");
27 3
28 //Border frame 4 uiSpreadsheet()
29 t = uicontrol(fig, ...
30 "style", "frame", ...
31 "constraints", createConstraints("border", "top", [0 90]));
32
33 c = uicontrol(fig, ...
34 "style", "frame", ...
35 "layout", "border", ...
36 "constraints", createConstraints("border", "center"));
37
38 uicontrol(c, ...
39 "style", "frame", ...
40 "tag", "importgui_preview", ...
41 "scrollable", "on", ...
42 "layout", "gridbag", ...
43 "constraints", createConstraints("border", "center"));
44
45 b = uicontrol(fig, ...
46 "style", "frame", ...
47 "constraints", createConstraints("border", "bottom", [0 45]));
48
49 //frame file
50 fr1 = uicontrol(t, ...
51 "style", "frame", ...
52 "position", [0 0 800 90]);
53
54 uicontrol(fr1, ...
55 "style", "text", ...
56 "position", [20 50 560 30], ...
57 "horizontalalignment", "center", ...
58 "relief", "solid", ...
59 "fontsize", 13, ...
60 "fontweight", "bold", ...
61 "tag", "importgui_filename")
62
63 uicontrol(fr1, ...
64 "style", "pushbutton", ...
65 "string", _("Select a file"), ...
66 "callback", "importgui_cbselect", ...
67 "position", [600 50 180 30]);
68
69 uicontrol(fr1, ...
70 "style", "checkbox", ...
71 "string", _("File has a header"), ...
72 "tag", "importgui_header", ...
73 "callback", "importgui_preview", ...
74 "position", [20 20 130 25]);
75
76 uicontrol(fr1, ...
77 "style", "checkbox", ...
78 "string", _("Show all columns"), ...
79 "tooltip", _("It can take a long time"), ...
80 "tag", "importgui_showcol", ...
81 "callback", "importgui_preview", ...
82 "position", [20 0 130 25]);
83
84 uicontrol(fr1, ...
85 "style", "text", ...
86 "string", _("Separator"), ...
87 "horizontalalignment", "center", ...
88 "position", [180 10 60 25]);
89
90 uicontrol(fr1, ...
91 "style", "popupmenu", ...
92 "string", [_("Comma"), _("Space"), _("Tab"), _("Semicolon"), _("Pipe"), _("Colon")], ...
93 "userdata", [",", " ", ascii(9), ";", "|", ":"], ...
94 "value", 4, ...
95 "tag", "importgui_delim", ...
96 "callback", "importgui_preview", ...
97 "position", [240 10 120 25]);
98
99 uicontrol(fr1, ...
100 "style", "text", ...
101 "string", _("Decimal"), ...
102 "horizontalalignment", "center", ...
103 "position", [390 10 60 25]);
104
105 uicontrol(fr1, ...
106 "style", "popupmenu", ...
107 "string", [_("Point"), _("Comma")], ...
108 "userdata", [".", ","], ...
109 "value", 1, ...
110 "tag", "importgui_decimal", ...
111 "callback", "importgui_preview", ...
112 "position", [450 10 120 25]);
113
114 uicontrol(fr1, ...
115 "style", "text", ...
116 "string", _("Conversion"), ...
117 "horizontalalignment", "center", ...
118 "position", [600 10 60 25]);
119
120 uicontrol(fr1, ...
121 "style", "popupmenu", ...
122 "string", [_("Keep as strings"), _("Convert to double")], ...
123 "userdata", ["string", "double"], ...
124 "value", 1, ...
125 "tag", "importgui_conversion", ...
126 "callback", "importgui_preview", ...
127 "position", [660 10 120 25]);
128
129
130 //footer
131 outer = uicontrol(b, ...
132 "style", "frame", ...
133 "position", [0 40 800 5], ...
134 "border", createBorder("line", "#dddddd"));
135
136 uicontrol(outer, ...
137 "style", "frame", ...
138 "backgroundcolor", [0 120 215]./255, ...
139 "tag", "importgui_progressbar", ...
140 "position", [0 0 0 5]);
141
142 uicontrol(b, ...
143 "style", "pushbutton", ...
144 "string", _("Import as variable"), ...
145 "callback", "importgui_variable", ...
146 "enable", "off", ...
147 "tag", "importgui_btnvarialble", ...
148 "position", [50 5 200 30]);
149
150 uicontrol(b, ...
151 "style", "pushbutton", ...
152 "string", _("Quit"), ...
153 "callback", "delete(get(""importgui""));", ...
154 "position", [300 5 200 30]);
155
156 uicontrol(b, ...
157 "style", "pushbutton", ...
158 "string", _("Copy function to clipboard"), ...
159 "callback", "importgui_function", ...
160 "enable", "off", ...
161 "tag", "importgui_btnfunction", ...
162 "position", [550 5 200 30]);
163
164 sw = getsystemmetrics("SM_CXSCREEN");
165 sh = getsystemmetrics("SM_CYSCREEN");
166
167 s = fig.figure_size;
168 w = (sw - s(1)) / 2
169 h = (sh - s(2)) / 2
170 fig.figure_position = [w h];
171
172 fig.visible = "on";
173 importgui_cbselect();
174endfunction 5endfunction
diff --git a/scilab/modules/fileio/macros/importgui/importgui_analyze.sci b/scilab/modules/fileio/macros/importgui/importgui_analyze.sci
deleted file mode 100644
index a968f20..0000000
--- a/scilab/modules/fileio/macros/importgui/importgui_analyze.sci
+++ /dev/null
@@ -1,32 +0,0 @@
1function delim = importgui_analyze(f, n)
2
3 if ~exists("n", "local") then
4 n = 10;
5 end
6
7 head = mgetl(f, n);
8 n = size(head, "r");
9
10 delimiters = get("importgui_delim", "userdata");
11 count = zeros(delimiters);
12
13 for i = 1:n
14 l = head(i);
15 for j = 1:size(delimiters, "*")
16 r = strindex(l, delimiters(j));
17 if size(r, "*") > 1 then
18 count(j) = count(j) + 1;
19 end
20 end
21 end
22
23 [m, k] = max(count);
24
25 if m == n then
26 delim = k;
27 else
28 //unable to find good delimiter.
29 delim = 0;
30 return;
31 end
32endfunction
diff --git a/scilab/modules/fileio/macros/importgui/importgui_cbselect.sci b/scilab/modules/fileio/macros/importgui/importgui_cbselect.sci
deleted file mode 100644
index 265e7d8..0000000
--- a/scilab/modules/fileio/macros/importgui/importgui_cbselect.sci
+++ /dev/null
@@ -1,23 +0,0 @@
1function importgui_cbselect()
2
3 path = pwd();
4 f = get("importgui_filename", "string");
5 if ~isempty(f) then
6 path = fileparts(f);
7 end
8
9
10 path = uigetfile(["*.txt" "Text files";"*.csv" "CSV files"], path, "Choose a file", %f);
11 if ~isempty(path) then
12 set("importgui_filename", "string", path);
13 delim = importgui_analyze(path, 10);
14 if delim then
15 set("importgui_delim", "value", delim);
16 if delim == 1 then
17 set("importgui_decimal", "value", 1);
18 end
19 end
20
21 importgui_preview();
22 end
23endfunction
diff --git a/scilab/modules/fileio/macros/importgui/importgui_function.sci b/scilab/modules/fileio/macros/importgui/importgui_function.sci
deleted file mode 100644
index d9f47e9..0000000
--- a/scilab/modules/fileio/macros/importgui/importgui_function.sci
+++ /dev/null
@@ -1,54 +0,0 @@
1function importgui_function()
2
3 hasHeader = get("importgui_header", "value");
4 delim = get("importgui_delim", "userdata")(get("importgui_delim", "value"));
5 decimal = get("importgui_decimal", "userdata")(get("importgui_decimal", "value"));
6 conversion = get("importgui_conversion", "userdata")(get("importgui_conversion", "value"));
7
8 path = get("importgui_filename", "string");
9
10 importHeader = %f;
11
12 if hasHeader then
13 x = x_mdialog("Create function", ["Function name";"Import header"], ["importdata";"%F"]);
14 if isempty(x) then
15 return;
16 end
17
18 importHeader = evstr(x(2));
19 else
20 x = x_mdialog("Create function", ["Function name"], ["importdata"]);
21 if isempty(x) then
22 return;
23 end
24 end
25
26
27 str = [];
28 str($+1) = sprintf("clear %s;", x(1));
29 str($+1) = "";
30 if importHeader then
31 str($+1) = sprintf("function [header, data] = %s(filename)", x(1));
32 str($+1) = " header = mgetl(filename, 1);";
33 str($+1) = sprintf(" header = csvTextScan(header, ""%s"", ""%s"", ""string"");", delim, decimal);
34 else
35 str($+1) = sprintf("function [data] = %s(filename)", x(1));
36 end
37
38 if hasHeader then
39 str($+1) = sprintf(" data = csvRead(filename, ""%s"", ""%s"", ""%s"", [], [], [], 1);", delim, decimal, conversion);
40 else
41 str($+1) = sprintf(" data = csvRead(filename, ""%s"", ""%s"", ""%s"");", delim, decimal, conversion);
42 end
43
44 str($+1) = "endfunction";
45 str($+1) = "";
46
47 if importHeader then
48 str($+1) = sprintf("[header, data] = importdata(""%s"");", path);
49 else
50 str($+1) = sprintf("[data] = importdata(""%s"");", path);
51 end
52
53 clipboard("copy", str);
54endfunction
diff --git a/scilab/modules/fileio/macros/importgui/importgui_gethead.sci b/scilab/modules/fileio/macros/importgui/importgui_gethead.sci
deleted file mode 100644
index 6f37966..0000000
--- a/scilab/modules/fileio/macros/importgui/importgui_gethead.sci
+++ /dev/null
@@ -1,3 +0,0 @@
1function head = importgui_gethead(f, n)
2 head = mgetl(f, n);
3endfunction
diff --git a/scilab/modules/fileio/macros/importgui/importgui_preview.sci b/scilab/modules/fileio/macros/importgui/importgui_preview.sci
deleted file mode 100644
index 651c6ec..0000000
--- a/scilab/modules/fileio/macros/importgui/importgui_preview.sci
+++ /dev/null
@@ -1,130 +0,0 @@
1function importgui_preview()
2 global %importgui_cancel;
3 //get window information
4 delim = get("importgui_delim", "value")
5 if delim == 1 then
6 set("importgui_decimal", "value", 1);
7 end
8
9 set("importgui_btnvarialble", "enable", "off");
10 set("importgui_btnfunction", "enable", "off");
11
12 hasHeader = get("importgui_header", "value");
13 delim = get("importgui_delim", "userdata")(delim);
14 decimal = get("importgui_decimal", "userdata")(get("importgui_decimal", "value"));
15 conversion = get("importgui_conversion", "userdata")(get("importgui_conversion", "value"));
16
17 path = get("importgui_filename", "string");
18
19 if isempty(path) || ~isfile(path) then
20 return;
21 end
22
23 c = get("importgui_preview");
24 c.visible = "off";
25 delete(c.children);
26
27 parent = c.parent;
28 fr = uicontrol(parent, ...
29 "style", "frame", ...
30 "layout", "gridbag", ...
31 "tag", "importgui_cancel", ...
32 "constraints", createConstraints("border", "center"));
33
34 uicontrol(fr, ...
35 "style", "pushbutton", ...
36 "string", _("Cancel preview"), ...
37 "callback_type", 10, ...
38 "callback", "global %importgui_cancel;%importgui_cancel=%t;", ...
39 "constraints", createConstraints("gridbag", [1 1 1 1], [1, 1]));
40
41 txt = mgetl(path, 20 + hasHeader);
42 if hasHeader then
43 header = txt(1);
44 txt(1) = [];
45
46 //process header
47 try
48 header = csvTextScan(header, delim, decimal, "string");
49 catch
50 delete(fr);
51 c.visible = "on";
52 return;
53 end
54 end
55
56 try
57 x = csvTextScan(txt, delim, decimal, conversion);
58 catch
59 delete(fr);
60 c.visible = "on";
61 return;
62 end
63
64 if conversion == "double" then
65 //must convert double to string to display data
66 x = string(x);
67 end
68
69 //frame data
70 limit = min(10, size(x, "c"));
71 show = get("importgui_showcol", "value");
72 if show then
73 limit = size(x, "c");
74 end
75
76 for i = 1:limit
77 if %importgui_cancel then
78 break;
79 end
80
81 progress = i / limit;
82 constraints = createConstraints("gridbag", [i 1 1 1], [1, 0], "horizontal", "upper", [5, 5]);
83
84 if hasHeader then
85 h = header(i);
86 else
87 h = sprintf("col%d", i);
88 end
89
90 uicontrol(c, ...
91 "style", "text", ...
92 "string", h, ...
93 "horizontalalignment", "center", ...
94 "relief", "solid", ...
95 "constraints", constraints)
96
97 for j = 1:size(x, "r")
98 constraints = createConstraints("gridbag", [i j+1 1 1], [1, 0], "horizontal", "upper", [7 7]);
99
100 uicontrol(c, ...
101 "style", "text", ...
102 "horizontalalignment", "center", ...
103 "string", x(j,i), ...
104 "constraints", constraints);
105 end
106
107 importgui_progress(progress);
108 end
109
110 //to push lines to top
111 constraints = createConstraints("gridbag", [1 size(x, "r")+1 size(x, "c") 1], [1, 1], "both", "upper");
112 uicontrol(c, ...
113 "style", "frame", ...
114 "constraints", constraints);
115
116 //delete cancel frame
117 delete(fr);
118
119 if %importgui_cancel then
120 //delete created children to keep interface clean
121 delete(c.children)
122 else
123 set("importgui_btnvarialble", "enable", "on");
124 set("importgui_btnfunction", "enable", "on");
125 end
126
127 clearglobal %importgui_cancel;
128 c.visible = "on";
129 importgui_progress(0);
130endfunction
diff --git a/scilab/modules/fileio/macros/importgui/importgui_progress.sci b/scilab/modules/fileio/macros/importgui/importgui_progress.sci
deleted file mode 100644
index 88ac58e..0000000
--- a/scilab/modules/fileio/macros/importgui/importgui_progress.sci
+++ /dev/null
@@ -1,5 +0,0 @@
1function importgui_progress(val)
2 pos = get("importgui_progressbar", "position");
3 pos(3) = 800 * val;
4 set("importgui_progressbar", "position", pos);
5endfunction
diff --git a/scilab/modules/fileio/macros/importgui/importgui_variable.sci b/scilab/modules/fileio/macros/importgui/importgui_variable.sci
deleted file mode 100644
index 7c3cd3c..0000000
--- a/scilab/modules/fileio/macros/importgui/importgui_variable.sci
+++ /dev/null
@@ -1,43 +0,0 @@
1function importgui_variable()
2 hasHeader = get("importgui_header", "value");
3 delim = get("importgui_delim", "userdata")(get("importgui_delim", "value"));
4 decimal = get("importgui_decimal", "userdata")(get("importgui_decimal", "value"));
5 conversion = get("importgui_conversion", "userdata")(get("importgui_conversion", "value"));
6
7 path = get("importgui_filename", "string");
8 if isempty(path) || ~isfile(path) then
9 return;
10 end
11
12 importHeader = %f;
13
14 if hasHeader then
15 x = x_mdialog("Import Data", ["Variable name";"Import header";"Header name"], ["data";"%F";"header"]);
16 if isempty(x) then
17 return;
18 end
19
20 importHeader = evstr(x(2));
21 if importHeader then
22 header = mgetl(path, 1);
23 header = csvTextScan(header, delim, decimal, "string");
24 str = sprintf("[%s, %s] = resume(data, header);", x(1), x(3));
25 else
26 str = sprintf("[%s] = resume(data);", x(1));
27 end
28
29 dataName = x(1);
30 data = csvRead(path, delim, decimal, conversion, [], [], [], 1);
31 else
32 x = x_mdialog("Import Data", ["Variable name"], ["data"]);
33 if isempty(x) then
34 return;
35 end
36
37 str = sprintf("[%s] = resume(data);", x(1));
38 dataName = x(1);
39 data = csvRead(path, delim, decimal, conversion);
40 end
41
42 execstr(str);
43endfunction
diff --git a/scilab/modules/ui_data/macros/uiSpreadsheet.sci b/scilab/modules/ui_data/macros/uiSpreadsheet.sci
new file mode 100644
index 0000000..576c51a
--- /dev/null
+++ b/scilab/modules/ui_data/macros/uiSpreadsheet.sci
@@ -0,0 +1,529 @@
1// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2// Copyright (C) 2021 - ESI Group - Antoine ELIAS
3// Copyright (C) 2021 - Samuel Gougeon
4//
5// Copyright (C) 2012 - 2016 - Scilab Enterprises
6//
7// This file is hereby licensed under the terms of the GNU GPL v2.0,
8// pursuant to article 5.3.4 of the CeCILL v.2.1.
9// This file was originally licensed under the terms of the CeCILL v2.1,
10// and continues to be available under such terms.
11// For more information, see the COPYING file which you should have received
12// along with this program.
13
14function uiSpreadsheet(action)
15 // action: private internal argument
16
17 if isdef("action","l")
18 select action
19 case "cbselect"
20 uispreadsheet_cbselect()
21
22 case "preview"
23 uispreadsheet_preview()
24
25 case "variable"
26 uispreadsheet_variable()
27 if isdef("names","l")
28 if isdef("header","l")
29 str = msprintf("[%s] = resume(data,header)\n", names)
30 else
31 str = msprintf("[%s] = resume(data)\n", names)
32 end
33 execstr(str)
34 end
35
36 case "function"
37 uispreadsheet_function()
38 end
39 return
40 end
41 // ------------------------------
42
43 win = get("uispreadsheet");
44 if ~isempty(win) then
45 //set visible
46 set("win", "visible", "on");
47 //set on top
48 set("win");
49 return;
50 end
51
52 w = 800;
53 h = 600;
54
55 fig = figure(...
56 "figure_name", _("Import data"), ...
57 "dockable", "off", ...
58 "axes_size", [w h], ...
59 "infobar_visible", "off", ...
60 "toolbar_visible", "off", ...
61 "menubar_visible", "off", ...
62 "default_axes", "off", ...
63 "background", -2, ...
64 "resize", "off", ...
65 "layout", "border", ...
66 "tag", "uispreadsheet", ...
67 "visible", "off");
68
69 //Border frame
70 t = uicontrol(fig, ...
71 "style", "frame", ...
72 "constraints", createConstraints("border", "top", [0 90]));
73
74 c = uicontrol(fig, ...
75 "style", "frame", ...
76 "layout", "border", ...
77 "constraints", createConstraints("border", "center"));
78
79 uicontrol(c, ...
80 "style", "frame", ...
81 "tag", "uispreadsheet_preview", ...
82 "scrollable", "on", ...
83 "layout", "gridbag", ...
84 "constraints", createConstraints("border", "center"));
85
86 b = uicontrol(fig, ...
87 "style", "frame", ...
88 "constraints", createConstraints("border", "bottom", [0 45]));
89
90 //frame file
91 fr1 = uicontrol(t, ...
92 "style", "frame", ...
93 "position", [0 0 800 90]);
94
95 uicontrol(fr1, ...
96 "style", "text", ...
97 "position", [20 50 560 30], ...
98 "horizontalalignment", "center", ...
99 "relief", "solid", ...
100 "fontsize", 13, ...
101 "fontweight", "bold", ...
102 "tag", "uispreadsheet_filename")
103
104 uicontrol(fr1, ...
105 "style", "pushbutton", ...
106 "string", _("Select a file"), ...
107 "callback", "uiSpreadsheet(""cbselect"")", ...
108 "position", [600 50 180 30]);
109
110 uicontrol(fr1, ...
111 "style", "checkbox", ...
112 "string", _("File has a header"), ...
113 "tag", "uispreadsheet_header", ...
114 "callback", "uiSpreadsheet(""preview"")", ...
115 "position", [20 20 130 25]);
116
117 uicontrol(fr1, ...
118 "style", "checkbox", ...
119 "string", _("Show all columns"), ...
120 "tooltip", _("It can take a long time"), ...
121 "tag", "uispreadsheet_showcol", ...
122 "callback", "uiSpreadsheet(""preview"")", ...
123 "position", [20 0 130 25]);
124
125 uicontrol(fr1, ...
126 "style", "text", ...
127 "string", _("Separator"), ...
128 "horizontalalignment", "center", ...
129 "position", [180 10 60 25]);
130
131 uicontrol(fr1, ...
132 "style", "popupmenu", ...
133 "string", [_("Comma"), _("Space"), _("Tab"), _("Semicolon"), _("Pipe"), _("Colon")], ...
134 "userdata", [",", " ", ascii(9), ";", "|", ":"], ...
135 "value", 4, ...
136 "tag", "uispreadsheet_delim", ...
137 "callback", "uiSpreadsheet(""preview"")", ...
138 "position", [240 10 120 25]);
139
140 uicontrol(fr1, ...
141 "style", "text", ...
142 "string", _("Decimal"), ...
143 "horizontalalignment", "center", ...
144 "position", [390 10 60 25]);
145
146 uicontrol(fr1, ...
147 "style", "popupmenu", ...
148 "string", [_("Point"), _("Comma")], ...
149 "userdata", [".", ","], ...
150 "value", 1, ...
151 "tag", "uispreadsheet_decimal", ...
152 "callback", "uiSpreadsheet(""preview"")", ...
153 "position", [450 10 120 25]);
154
155 uicontrol(fr1, ...
156 "style", "text", ...
157 "string", _("Conversion"), ...
158 "horizontalalignment", "center", ...
159 "position", [600 10 60 25]);
160
161 uicontrol(fr1, ...
162 "style", "popupmenu", ...
163 "string", [_("Keep as strings"), _("Convert to double")], ...
164 "userdata", ["string", "double"], ...
165 "value", 1, ...
166 "tag", "uispreadsheet_conversion", ...
167 "callback", "uiSpreadsheet(""preview"")", ...
168 "position", [660 10 120 25]);
169
170
171 //footer
172 outer = uicontrol(b, ...
173 "style", "frame", ...
174 "position", [0 40 800 5], ...
175 "border", createBorder("line", "#dddddd"));
176
177 uicontrol(outer, ...
178 "style", "frame", ...
179 "backgroundcolor", [0 120 215]./255, ...
180 "tag", "uispreadsheet_progressbar", ...
181 "position", [0 0 0 5]);
182
183 uicontrol(b, ...
184 "style", "pushbutton", ...
185 "string", _("Import as variable"), ...
186 "callback", "uiSpreadsheet(""variable"")", ...
187 "enable", "off", ...
188 "tag", "uispreadsheet_btnvarialble", ...
189 "position", [50 5 200 30]);
190
191 uicontrol(b, ...
192 "style", "pushbutton", ...
193 "string", _("Quit"), ...
194 "callback", "delete(get(""uispreadsheet""));", ...
195 "position", [300 5 200 30]);
196
197 uicontrol(b, ...
198 "style", "pushbutton", ...
199 "string", _("Copy function to clipboard"), ...
200 "callback", "uiSpreadsheet(""function"")", ...
201 "enable", "off", ...
202 "tag", "uispreadsheet_btnfunction", ...
203 "position", [550 5 200 30]);
204
205 sw = get(0,"screensize_px")(3);
206 sh = get(0,"screensize_px")(4);
207
208 s = fig.figure_size;
209 w = (sw - s(1)) / 2
210 h = (sh - s(2)) / 2
211 fig.figure_position = [w h];
212
213 fig.visible = "on";
214 uispreadsheet_cbselect();
215endfunction
216
217// -----------------------------------------------------------------------------
218
219function head = uispreadsheet_gethead(f, n)
220 head = mgetl(f, n);
221endfunction
222
223// -----------------------------------------------------------------------------
224
225function uispreadsheet_progress(val)
226 pos = get("uispreadsheet_progressbar", "position");
227 pos(3) = 800 * val;
228 set("uispreadsheet_progressbar", "position", pos);
229endfunction
230
231// -----------------------------------------------------------------------------
232
233function uispreadsheet_variable()
234 hasHeader = get("uispreadsheet_header", "value");
235 delim = get("uispreadsheet_delim", "userdata")(get("uispreadsheet_delim", "value"));
236 decimal = get("uispreadsheet_decimal", "userdata")(get("uispreadsheet_decimal", "value"));
237 conversion = get("uispreadsheet_conversion", "userdata")(get("uispreadsheet_conversion", "value"));
238
239 path = get("uispreadsheet_filename", "string");
240 if isempty(path) || ~isfile(path) then
241 return;
242 end
243
244 importHeader = %f;
245
246 if hasHeader then
247 x = x_mdialog("Import Data", ["Variable name";"Import header";"Header name"], ["data";"%F";"header"]);
248 if isempty(x) then
249 return;
250 end
251
252 importHeader = evstr(x(2));
253 if importHeader then
254 header = mgetl(path, 1);
255 header = csvTextScan(header, delim, decimal, "string");
256 names = strcat([x(1) x(3)],",")
257 str = "[data, header, names] = resume(data, header, names)";
258 else
259 names = x(1)
260 str = "[data, names] = resume(data, names)";
261 end
262
263 dataName = x(1);
264 data = csvRead(path, delim, decimal, conversion, [], [], [], 1);
265 else
266 x = x_mdialog("Import Data", ["Variable name"], ["data"]);
267 if isempty(x) then
268 return;
269 end
270
271 names = x(1)
272 str = "[data, names] = resume(data, names)";
273 dataName = x(1);
274 data = csvRead(path, delim, decimal, conversion);
275 end
276
277 execstr(str);
278endfunction
279
280// -----------------------------------------------------------------------------
281
282function uispreadsheet_cbselect()
283
284 path = pwd();
285 f = get("uispreadsheet_filename", "string");
286 if ~isempty(f) then
287 path = fileparts(f);
288 end
289
290
291 path = uigetfile(["*.txt" "Text files";"*.csv" "CSV files"], path, "Choose a file", %f);
292 if ~isempty(path) then
293 set("uispreadsheet_filename", "string", path);
294 delim = uispreadsheet_analyze(path, 10);
295 if delim then
296 set("uispreadsheet_delim", "value", delim);
297 if delim == 1 then
298 set("uispreadsheet_decimal", "value", 1);
299 end
300 end
301
302 uispreadsheet_preview();
303 end
304endfunction
305
306// -----------------------------------------------------------------------------
307
308function delim = uispreadsheet_analyze(f, n)
309
310 if ~exists("n", "local") then
311 n = 10;
312 end
313
314 head = mgetl(f, n);
315 n = size(head, "r");
316
317 delimiters = get("uispreadsheet_delim", "userdata");
318 count = zeros(delimiters);
319
320 for i = 1:n
321 l = head(i);
322 for j = 1:size(delimiters, "*")
323 r = strindex(l, delimiters(j));
324 if size(r, "*") > 1 then
325 count(j) = count(j) + 1;
326 end
327 end
328 end
329
330 [m, k] = max(count);
331
332 if m == n then
333 delim = k;
334 else
335 //unable to find good delimiter.
336 delim = 0;
337 return;
338 end
339endfunction
340
341// -----------------------------------------------------------------------------
342
343function uispreadsheet_function()
344
345 hasHeader = get("uispreadsheet_header", "value");
346 delim = get("uispreadsheet_delim", "userdata")(get("uispreadsheet_delim", "value"));
347 decimal = get("uispreadsheet_decimal", "userdata")(get("uispreadsheet_decimal", "value"));
348 conversion = get("uispreadsheet_conversion", "userdata")(get("uispreadsheet_conversion", "value"));
349
350 path = get("uispreadsheet_filename", "string");
351
352 importHeader = %f;
353
354 if hasHeader then
355 x = x_mdialog("Create function", ["Function name";"Import header"], ["importdata";"%F"]);
356 if isempty(x) then
357 return;
358 end
359
360 importHeader = evstr(x(2));
361 else
362 x = x_mdialog("Create function", ["Function name"], ["importdata"]);
363 if isempty(x) then
364 return;
365 end
366 end
367
368
369 str = [];
370 str($+1) = sprintf("clear %s;", x(1));
371 str($+1) = "";
372 if importHeader then
373 str($+1) = sprintf("function [header, data] = %s(filename)", x(1));
374 str($+1) = " header = mgetl(filename, 1);";
375 str($+1) = sprintf(" header = csvTextScan(header, ""%s"", ""%s"", ""string"");", delim, decimal);
376 else
377 str($+1) = sprintf("function [data] = %s(filename)", x(1));
378 end
379
380 if hasHeader then
381 str($+1) = sprintf(" data = csvRead(filename, ""%s"", ""%s"", ""%s"", [], [], [], 1);", delim, decimal, conversion);
382 else
383 str($+1) = sprintf(" data = csvRead(filename, ""%s"", ""%s"", ""%s"");", delim, decimal, conversion);
384 end
385
386 str($+1) = "endfunction";
387 str($+1) = "";
388
389 if importHeader then
390 str($+1) = sprintf("[header, data] = importdata(""%s"");", path);
391 else
392 str($+1) = sprintf("[data] = importdata(""%s"");", path);
393 end
394
395 clipboard("copy", str);
396endfunction
397
398// -----------------------------------------------------------------------------
399
400function uispreadsheet_preview()
401 global %uispreadsheet_cancel;
402 //get window information
403 delim = get("uispreadsheet_delim", "value")
404 if delim == 1 then
405 set("uispreadsheet_decimal", "value", 1);
406 end
407
408 set("uispreadsheet_btnvarialble", "enable", "off");
409 set("uispreadsheet_btnfunction", "enable", "off");
410
411 hasHeader = get("uispreadsheet_header", "value");
412 delim = get("uispreadsheet_delim", "userdata")(delim);
413 decimal = get("uispreadsheet_decimal", "userdata")(get("uispreadsheet_decimal", "value"));
414 conversion = get("uispreadsheet_conversion", "userdata")(get("uispreadsheet_conversion", "value"));
415
416 path = get("uispreadsheet_filename", "string");
417
418 if isempty(path) || ~isfile(path) then
419 return;
420 end
421
422 c = get("uispreadsheet_preview");
423 c.visible = "off";
424 delete(c.children);
425
426 parent = c.parent;
427 fr = uicontrol(parent, ...
428 "style", "frame", ...
429 "layout", "gridbag", ...
430 "tag", "uispreadsheet_cancel", ...
431 "constraints", createConstraints("border", "center"));
432
433 uicontrol(fr, ...
434 "style", "pushbutton", ...
435 "string", _("Cancel preview"), ...
436 "callback_type", 10, ...
437 "callback", "global %uispreadsheet_cancel;%uispreadsheet_cancel=%t;", ...
438 "constraints", createConstraints("gridbag", [1 1 1 1], [1, 1]));
439
440 txt = mgetl(path, 20 + hasHeader);
441 if hasHeader then
442 header = txt(1);
443 txt(1) = [];
444
445 //process header
446 try
447 header = csvTextScan(header, delim, decimal, "string");
448 catch
449 delete(fr);
450 c.visible = "on";
451 return;
452 end
453 end
454
455 try
456 x = csvTextScan(txt, delim, decimal, conversion);
457 catch
458 delete(fr);
459 c.visible = "on";
460 return;
461 end
462
463 if conversion == "double" then
464 //must convert double to string to display data
465 x = string(x);
466 end
467
468 //frame data
469 limit = min(10, size(x, "c"));
470 show = get("uispreadsheet_showcol", "value");
471 if show then
472 limit = size(x, "c");
473 end
474
475 for i = 1:limit
476 if %uispreadsheet_cancel then
477 break;
478 end
479
480 progress = i / limit;
481 constraints = createConstraints("gridbag", [i 1 1 1], [1, 0], "horizontal", "upper", [5, 5]);
482
483 if hasHeader then
484 h = header(i);
485 else
486 h = sprintf("col%d", i);
487 end
488
489 uicontrol(c, ...
490 "style", "text", ...
491 "string", h, ...
492 "horizontalalignment", "center", ...
493 "relief", "solid", ...
494 "constraints", constraints)
495
496 for j = 1:size(x, "r")
497 constraints = createConstraints("gridbag", [i j+1 1 1], [1, 0], "horizontal", "upper", [7 7]);
498
499 uicontrol(c, ...
500 "style", "text", ...
501 "horizontalalignment", "center", ...
502 "string", x(j,i), ...
503 "constraints", constraints);
504 end
505
506 uispreadsheet_progress(progress);
507 end
508
509 //to push lines to top
510 constraints = createConstraints("gridbag", [1 size(x, "r")+1 size(x, "c") 1], [1, 1], "both", "upper");
511 uicontrol(c, ...
512 "style", "frame", ...
513 "constraints", constraints);
514
515 //delete cancel frame
516 delete(fr);
517
518 if %uispreadsheet_cancel then
519 //delete created children to keep interface clean
520 delete(c.children)
521 else
522 set("uispreadsheet_btnvarialble", "enable", "on");
523 set("uispreadsheet_btnfunction", "enable", "on");
524 end
525
526 clearglobal %uispreadsheet_cancel;
527 c.visible = "on";
528 uispreadsheet_progress(0);
529endfunction