-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlImageBrowserCreator.cc
348 lines (284 loc) · 11.9 KB
/
htmlImageBrowserCreator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
65
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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// htmlImageBrowserCreator
#include "htmlImageBrowserCreator.h"
// c++
#include <fstream>
HtmlVariable::HtmlVariable(string tit, string filename)
{
title = tit;
ifstream in(filename.c_str());
if(!in) {
cerr << " !! Error: can't open input file " << filename << ". Check your spelling. Exiting. " << endl;
exit(1);
} else if(tit != "mauri") {
cout << " > Scanning file " << filename << " for variable >" << tit << "<" << endl;
while (!in.eof()) {
string imageName;
in >> imageName ;
// checking if variable exist
if(imageName.find(tit) != string::npos) {
string thisFieldStart = imageName.substr(imageName.find(tit));
// getting upper limit: underscore or .png
string thisFieldEnd = "";
if(thisFieldStart.find("_") != string::npos) {
thisFieldEnd = thisFieldStart.substr(0, thisFieldStart.find("_"));
} else if(thisFieldStart.find(".png") != string::npos) {
thisFieldEnd = thisFieldStart.substr(0, thisFieldStart.find(".png"));
}
// getting lower limit: dash. This won't pass if thisFieldEnd is empty
if(thisFieldEnd.find("-") != string::npos) {
string newField = thisFieldEnd.substr(thisFieldEnd.find("-")+1);
// inserting it only if it's not present already
auto notPresent = find(present.begin(), present.end(), newField);
if(notPresent == present.cend()) {
present.push_back(newField);
cout << " found " << tit << ": " << newField << endl;
}
}
}
}
in.close();
}
}
// these needs more documenting
map<string, GOption> Page::defineOptions()
{
map<string, GOption> optionsMap;
optionsMap["addRowTableVariable"] = GOption("Row Variable Description", "mauri");
optionsMap["addColumnTableVariable"] = GOption("Column Variable Description", "mauri");
optionsMap["addSelectableVariable"] = GOption("QuantityVariable Description", "mauri", "general", true); // can be repeated
optionsMap["f"] = GOption("Filename", "imageslist.txt");
optionsMap["d"] = GOption("Image Directory", "img");
optionsMap["imagesSize"] = GOption("(w, h) image size", "1000 800");
optionsMap["defaultPlot"] = GOption("Default Plot at page loading", "default.png");
optionsMap["pageTitle"] = GOption("Page Title", "Mauri");
optionsMap["tdGrouping"] = GOption("Table scheme", 0 ); // 0: rows and columns are independent. Any other number will print the value on td and group the tds
return optionsMap;
}
Page::Page(GOptions* gopt)
{
string filename = gopt->getString("f");
for(auto &tv: gopt->getStrings("addRowTableVariable")) {
if(tv != "mauri")
rows.push_back(HtmlVariable(tv, filename));
}
for(auto &tv: gopt->getStrings("addColumnTableVariable")) {
if(tv != "mauri")
columns.push_back(HtmlVariable(tv, filename));
}
for(auto &tv: gopt->getStrings("addSelectableVariable")) {
if(tv != "mauri")
selections.push_back(HtmlVariable(tv, filename));
}
tdGrouping = gopt->getInt("tdGrouping");
pageTitle = gopt->getString("pageTitle");
defaultPlot = gopt->getString("defaultPlot");
imgDir = gopt->getString("d");
vector<string> ress = gopt->getStringVectorValue("imagesSize");
if(ress.size() == 2) {
// not sure why I have to add 50.
xres = stod(ress[0]) + 50;
yres = stod(ress[1]) + 50;
}
hf.open("page.html");
writeTopHtml();
writeBody();
writeBottomHtml();
hf.close();
cout << " tdGrouping set to " << tdGrouping << endl;
}
void Page::writeTopHtml()
{
hf << "<html lang=\"en\">" << endl;
hf << "\t<head>" << endl;
// css style: table
hf << "\t\t<style>" << endl;
hf << "\t\t\t#indextable { " << endl;
hf << "\t\t\t\tfont-family: \"Trebuchet MS\", Arial, Helvetica, sans-serif; " << endl;
hf << "\t\t\t\tborder-collapse: collapse; " << endl;
hf << "\t\t\t}" << endl;
hf << "\t\t\t#indextable td { " << endl;
hf << "\t\t\t\tborder: 1px solid #ddd; " << endl;
hf << "\t\t\t\tpadding: 6px; " << endl;
hf << "\t\t\t}" << endl;
hf << "\t\t\t#indextable tr:nth-child(even){background-color: #f2f2f2;} " << endl;
hf << "\t\t\t#indextable tr:hover {background-color: #ddd;}" << endl;
hf << "\t\t\t#indextable td:hover {background-color: #119; color: white;}" << endl;
hf << "\t\t\t.button { " << endl;
hf << "\t\t\t\t\tbackground-color: white;" << endl;
hf << "\t\t\t\t\tcolor: black;" << endl;
hf << "\t\t\t\t\tborder: 2px solid #555555;" << endl;
hf << "\t\t\t\t\tborder-radius: 10%;" << endl;
hf << "\t\t\t\t\tpadding: 15px 32px; /* Button size */" << endl;
hf << "\t\t\t\t\ttext-align: center; /* Center text */" << endl;
hf << "\t\t\t\t\ttext-decoration: none; /* No underline */" << endl;
hf << "\t\t\t\t\tdisplay: inline-block; /* Inline block */" << endl;
hf << "\t\t\t\t\tfont-size: 16px; /* Text size */" << endl;
hf << "\t\t\t\t\tmargin: 4px 2px; /* Small margins */" << endl;
hf << "\t\t\t\t\tcursor: pointer; /* Pointer cursor */" << endl;
hf << "\t\t\t\t\ttransition: background-color 0.3s ease; /* Smooth background transition */" << endl;
hf << "\t\t\t}" << endl;
hf << "\t\t\t " << endl;
hf << "\t\t\t/* Hover effect */" << endl;
hf << "\t\t\tbutton:hover {" << endl;
hf << "\t\t\t\tbackground-color: #45a049; /* Darker green on hover */" << endl;
hf << "\t\t\t}"<< endl;
hf << "\t\t\t/* Active state (when clicked) */" << endl;
hf << "\t\t\tbutton:active {" << endl;
hf << "\t\t\t\ttransform: scale(0.98); /* Slight shrink effect on click */" << endl;
hf << "\t\t\t}" << endl;
hf << "\t\t\t/* Selected (stay highlighted) */" << endl;
hf << "\t\t\tbutton.selected {" << endl;
hf << "\t\t\t\tbackground-color: #323fd5; /* Green when selected */" << endl;
hf << "\t\t\t\tcolor: white; /* White text when selected */" << endl;
hf << "\t\t\t\tbox-shadow: 0 5px #666; /* Shadow for depth */" << endl;
hf << "\t\t\t}" << endl;
hf << "\t\t\t/* Optional: Focus state (when keyboard focused) */" << endl;
hf << "\t\t\tbutton:focus {" << endl;
hf << "\t\t\t\toutline: none; /* Remove default focus outline */" << endl;
hf << "\t\t\t\tbox-shadow: 0 0 10px #719ECE; /* Custom blue glow */" << endl;
hf << "\t\t\t}" << endl;
hf << "\t\t</style>" << endl;
hf << "\t\t<title>Mauri's plots</title>" << endl;
// script
hf << "\t\t<script>" << endl;
// showPic function
hf << "\t\t\tfunction popPic() {picDisplayed = window.open(document.DIST.src, document.DIST.src,";
hf << "'statusbar=no, scrollbars=auto, status=no, resizable=no, width=" << xres << ", height=" << yres << "');}" << endl;
// selectable variables
for(auto &sv: selections) {
hf << "\t\t\tvar " << sv.title << " = \"" << sv.present[0] << "\";" << endl;
}
// changeBin function
hf << "\t\t\tfunction showPic(row, col) {" << endl;
// row
hf << "\t\t\t\tvar dist = \"" << imgDir << "/\"" << endl;
hf << "\t\t\t\tdist += \"" << rows[0].title << "-\"" << endl;
hf << "\t\t\t\tdist += row;" << endl;
// column if necessary
if(tdGrouping == 0) {
hf << "\t\t\t\tdist += \"_\"" << endl;
hf << "\t\t\t\tdist += \"" << columns[0].title << "-\"" << endl;
hf << "\t\t\t\tdist += col;" << endl;
}
// selecteables
for(auto &sv: selections) {
hf << "\t\t\t\tdist += \"_" << sv.title << "-\";" << endl;
hf << "\t\t\t\tdist += " << sv.title << ";" << endl;
}
// format
hf << "\t\t\t\tdist += \".png\"" << endl;
hf << "\t\t\t\tdocument.DIST.src = dist" << endl;
hf << "\t\t\t}" << endl;
// for each selectables create the function that change the title
for(auto &sv: selections) {
string functionTitle = "change" + sv.title;
string btitle = sv.title + "Buttons";
string ctitle = sv.title + "Title";
hf << "\t\t\tfunction " << functionTitle << "(selectedButton) {" << endl;
hf << "\t\t\t\tvar " << ctitle << " = \"" << sv.title << " \" + " << sv.title << ";" << endl;
hf << "\t\t\t\tdocument.getElementById(\"" << btitle << "\").innerHTML = " << ctitle << ";" << endl;
// Remove 'selected' class from all buttons in the weight row
hf << "\t\t\t\tvar buttons = selectedButton.closest('tr').querySelectorAll('button');" << endl;
hf << "\t\t\t\tbuttons.forEach(button => button.classList.remove(\"selected\"));" << endl;
// Add 'selected' class to the clicked button
hf << "\t\t\t\tselectedButton.classList.add(\"selected\");" << endl;
hf << "\t\t\t}" << endl;
}
hf << "\t\t\tfunction selectFirstButton() {" << endl;
hf << "\t\t\t\tvar buttons = document.querySelectorAll('button'); " << endl;
hf << "\t\t\t\tbuttons.forEach(button => {if (button.textContent == \"0\") button.click();}); " << endl;
hf << "\t\t\t}" << endl;
hf << "\t\t\twindow.onload = function() {" << endl;
hf << "\t\t\t\tselectFirstButton(); " << endl;
hf << "\t\t\t};" << endl;
hf << "\t\t </script>" << endl;
hf << "\t </head>" << endl;
}
void Page::writeBody()
{
hf << "\t <body>" << endl;
hf << "\t\t <center>" << endl;
// title
if(pageTitle != "Mauri") hf << "\t\t\t <h2> " << pageTitle << " </h2>" << endl;
// table. Left: data tables. Right: plot
hf << "\t\t\t <table> " << endl;
// selectables quantities
writeSelectables();
hf << "\t\t\t\t <tr> " << endl;
// Left: data tables
hf << "\t\t\t\t\t <td> " << endl;
hf << "\t\t\t\t\t\t <table id=\"indextable\" class=\"indextable\"> " << endl;
// data rows
if(tdGrouping == 0) {
writeSingleTDs();
} else if(tdGrouping > 0) {
writeGroupedTDs();
}
hf << "\t\t\t\t\t\t </table> " << endl;
hf << "\t\t\t\t\t </td> " << endl;
// Right: plot
hf << "\t\t\t\t\t <td> " << endl;
hf << "\t\t\t\t\t\t <img name = \"DIST\" width=\"650px\" src=\"" << defaultPlot << "\"/> " << endl;
hf << "\t\t\t\t\t </td> " << endl;
hf << "\t\t\t\t </tr> " << endl;
hf << "\t\t\t </table> " << endl;
hf << "\t\t </center>" << endl;
hf << "\t </body>" << endl;
}
void Page::writeBottomHtml()
{
hf << "</html>" << endl;
}
void Page::writeGroupedTDs()
{
// tds are grouped
int gIndex = 0;
for(auto &ri: rows[0].present) {
if(gIndex == 0) hf << "\t\t\t\t\t\t\t <tr> " << endl;
hf << "\t\t\t\t\t\t\t\t <td onclick=\"popPic()\" onmouseover=\"showPic('" << ri << "', 'na')\" >" << ri << "</td>" << endl;
gIndex++;
if(gIndex == tdGrouping) {
hf << "\t\t\t\t\t\t\t </tr> " << endl;
gIndex = 0;
}
}
}
void Page::writeSingleTDs()
{
// first row: columns titles
hf << "\t\t\t\t\t\t\t <tr> " << endl;
hf << "\t\t\t\t\t\t\t\t <td> <sub> " << rows[0].title << "</sub> <sup> " << columns[0].title << "</sup> </td>" << endl;
for(auto &ct : columns[0].present) {
hf << "\t\t\t\t\t\t\t\t <td>" << ct << "</td>" << endl;
}
hf << "\t\t\t\t\t\t\t </tr> " << endl;
// all remaining rows: row content
for(auto &ri: rows[0].present) {
hf << "\t\t\t\t\t\t\t <tr> " << endl;
hf << "\t\t\t\t\t\t\t\t <td>" << ri << "</td>" << endl;
for(auto &ct : columns[0].present) {
hf << "\t\t\t\t\t\t\t\t <td onclick=\"popPic()\" onmouseover=\"showPic('" << ri << "', '" << ct << "')\" />" << endl;
}
hf << "\t\t\t\t\t\t\t </tr> " << endl;
}
}
void Page::writeSelectables()
{
for(auto &sv: selections) {
// selectable quantities - onlt left TD
hf << "\t\t\t\t <tr> " << endl;
// colspan 2 so buttons include the plot space too
hf << "\t\t\t\t\t <td colspan=2> " << endl;
hf << "\t\t\t\t\t\t <table> <tr>" << endl;
string functionTitle = "change" + sv.title;
string btitle = sv.title + "Buttons";
hf << "\t\t\t\t\t\t\t <td id=\"" << btitle << "\" align=center colspan=" << sv.present.size() << ">" << sv.title << " " << sv.present[0] << "</td></tr><tr>" << endl;
for(auto &ps: sv.present) {
hf << "\t\t\t\t\t\t\t <td><button class=\"button\" onclick=\"javascript:" ;
hf << sv.title << "=\'" << ps << "\' ; " << functionTitle << "(this); \">" << ps <<"</button></td>" << endl;
}
hf << "\t\t\t\t\t\t </tr></table>" << endl;
hf << "\t\t\t\t\t </td> " << endl;
hf << "\t\t\t\t </tr> " << endl;
}
}