1 /*****************************************************************************
2 * *
3 * Elmer, A Finite Element Software for Multiphysical Problems *
4 * *
5 * Copyright 1st April 1995 - , CSC - IT Center for Science Ltd., Finland *
6 * *
7 * This program is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU General Public License *
9 * as published by the Free Software Foundation; either version 2 *
10 * of the License, or (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program (in file fem/GPL-2); if not, write to the *
19 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
20 * Boston, MA 02110-1301, USA. *
21 * *
22 *****************************************************************************/
23
24 /*****************************************************************************
25 * *
26 * ElmerGUI projectio *
27 * *
28 *****************************************************************************
29 * *
30 * Authors: Mikko Lyly, Juha Ruokolainen and Peter R�back *
31 * Email: Juha.Ruokolainen@csc.fi *
32 * Web: http://www.csc.fi/elmer *
33 * Address: CSC - IT Center for Science Ltd. *
34 * Keilaranta 14 *
35 * 02101 Espoo, Finland *
36 * *
37 * Original Date: 15 Mar 2008 *
38 * *
39 *****************************************************************************/
40
41 #include <QtGui>
42 #include <iostream>
43 #include "projectio.h"
44
45 #if WITH_QT5
46 #include <QtWidgets>
47 #endif
48
49 using namespace std;
50
ProjectIO(QWidget * parent)51 ProjectIO::ProjectIO(QWidget *parent)
52 : QDialog(parent)
53 {
54 parentWidget = parent;
55 }
56
~ProjectIO()57 ProjectIO::~ProjectIO()
58 {
59 }
60
appendToProject(QDomDocument * projectDoc,QDomElement * item)61 void ProjectIO::appendToProject(QDomDocument *projectDoc, QDomElement *item)
62 {
63 // Radio buttons:
64 //----------------
65 QList<QRadioButton *> allRadioButtons = parentWidget->findChildren<QRadioButton *>();
66
67 for(int i = 0; i < allRadioButtons.size(); i++) {
68 QRadioButton *rb = allRadioButtons.at(i);
69
70 if(!rb)
71 continue;
72
73 QString rbObjectName = rb->objectName();
74 QString rbValue = QString::number(rb->isChecked());
75
76 if(rbObjectName.isEmpty())
77 continue;
78
79 QDomElement widget = projectDoc->createElement("widget");
80 widget.setAttribute("type", "RadioButton");
81 item->appendChild(widget);
82
83 QDomElement objectName = projectDoc->createElement("objectName");
84 QDomText objectNameValue = projectDoc->createTextNode(rbObjectName);
85 objectName.appendChild(objectNameValue);
86 widget.appendChild(objectName);
87
88 QDomElement isChecked = projectDoc->createElement("isChecked");
89 QDomText isCheckedValue = projectDoc->createTextNode(rbValue);
90 isChecked.appendChild(isCheckedValue);
91 widget.appendChild(isChecked);
92 }
93
94 // Check boxes:
95 //--------------
96 QList<QCheckBox *> allCheckBoxes = parentWidget->findChildren<QCheckBox *>();
97
98 for(int i = 0; i < allCheckBoxes.size(); i++) {
99 QCheckBox *cb = allCheckBoxes.at(i);
100
101 if(!cb)
102 continue;
103
104 QString cbObjectName = cb->objectName();
105 QString cbValue = QString::number(cb->isChecked());
106
107 if(cbObjectName.isEmpty())
108 continue;
109
110 QDomElement widget = projectDoc->createElement("widget");
111 widget.setAttribute("type", "CheckBox");
112 item->appendChild(widget);
113
114 QDomElement objectName = projectDoc->createElement("objectName");
115 QDomText objectNameValue = projectDoc->createTextNode(cbObjectName);
116 objectName.appendChild(objectNameValue);
117 widget.appendChild(objectName);
118
119 QDomElement isChecked = projectDoc->createElement("isChecked");
120 QDomText isCheckedValue = projectDoc->createTextNode(cbValue);
121 isChecked.appendChild(isCheckedValue);
122 widget.appendChild(isChecked);
123 }
124
125 // Line edits:
126 //-------------
127 QList<QLineEdit *> allLineEdits = parentWidget->findChildren<QLineEdit *>();
128
129 for(int i = 0; i < allLineEdits.size(); i++) {
130 QLineEdit *le = allLineEdits.at(i);
131
132 if(!le)
133 continue;
134
135 QString leObjectName = le->objectName();
136 QString leValue = le->text().trimmed();
137
138 if(leObjectName.isEmpty())
139 continue;
140
141 QDomElement widget = projectDoc->createElement("widget");
142 widget.setAttribute("type", "LineEdit");
143 item->appendChild(widget);
144
145 QDomElement objectName = projectDoc->createElement("objectName");
146 QDomText objectNameValue = projectDoc->createTextNode(leObjectName);
147 objectName.appendChild(objectNameValue);
148 widget.appendChild(objectName);
149
150 QDomElement text = projectDoc->createElement("text");
151 QDomText textValue = projectDoc->createTextNode(leValue);
152 text.appendChild(textValue);
153 widget.appendChild(text);
154 }
155
156 // Text edits:
157 //-------------
158 QList<QTextEdit *> allTextEdits = parentWidget->findChildren<QTextEdit *>();
159
160 for(int i = 0; i < allTextEdits.size(); i++) {
161 QTextEdit *te = allTextEdits.at(i);
162
163 if(!te)
164 continue;
165
166 QString teObjectName = te->objectName();
167 QString teValue = te->toPlainText();
168
169 if(teObjectName.isEmpty())
170 continue;
171
172 QDomElement widget = projectDoc->createElement("widget");
173 widget.setAttribute("type", "TextEdit");
174 item->appendChild(widget);
175
176 QDomElement objectName = projectDoc->createElement("objectName");
177 QDomText objectNameValue = projectDoc->createTextNode(teObjectName);
178 objectName.appendChild(objectNameValue);
179 widget.appendChild(objectName);
180
181 QDomElement text = projectDoc->createElement("text");
182 QDomText textValue = projectDoc->createTextNode(teValue);
183 text.appendChild(textValue);
184 widget.appendChild(text);
185 }
186
187 // Combo boxes:
188 //--------------
189 QList<QComboBox *> allComboBoxes = parentWidget->findChildren<QComboBox *>();
190
191 for(int i = 0; i < allComboBoxes.size(); i++) {
192 QComboBox *cx = allComboBoxes.at(i);
193
194 if(!cx)
195 continue;
196
197 QString cxObjectName = cx->objectName();
198 QString cxValue = QString::number(cx->currentIndex());
199
200 if(cxObjectName.isEmpty())
201 continue;
202
203 QDomElement widget = projectDoc->createElement("widget");
204 widget.setAttribute("type", "ComboBox");
205 item->appendChild(widget);
206
207 QDomElement objectName = projectDoc->createElement("objectName");
208 QDomText objectNameValue = projectDoc->createTextNode(cxObjectName);
209 objectName.appendChild(objectNameValue);
210 widget.appendChild(objectName);
211
212 QDomElement currentIndex = projectDoc->createElement("currentIndex");
213 QDomText currentIndexValue = projectDoc->createTextNode(cxValue);
214 currentIndex.appendChild(currentIndexValue);
215 widget.appendChild(currentIndex);
216 }
217 }
218
readFromProject(QDomDocument * projectDoc,QDomElement * item)219 void ProjectIO::readFromProject(QDomDocument *projectDoc, QDomElement *item)
220 {
221 // Radio buttons:
222 //----------------
223 #if WITH_QT5
224 QList<QRadioButton *> allRadioButtons = parentWidget->findChildren<QRadioButton *>(QString());
225 #else
226 QList<QRadioButton *> allRadioButtons = parentWidget->findChildren<QRadioButton *>();
227 #endif
228
229 QList<QString> rbObjectNames;
230 for(int i = 0; i < allRadioButtons.size(); i++)
231 rbObjectNames.append(allRadioButtons.at(i)->objectName());
232
233 QDomElement widget = item->firstChildElement("widget");
234 for( ; !widget.isNull(); widget = widget.nextSiblingElement()) {
235
236 QString type = widget.attribute("type").trimmed();
237
238 if(type != "RadioButton")
239 continue;
240
241 QString objectName = widget.firstChildElement("objectName").text().trimmed();
242 bool isChecked = (widget.firstChildElement("isChecked").text().toInt() > 0);
243
244 if(objectName.isEmpty())
245 continue;
246
247 int index = rbObjectNames.indexOf(objectName);
248
249 if(index < 0) {
250 cout << "Load project: RadioButton: mismatch with object name" << endl;
251 #if WITH_QT5
252 cout << "*** " << string(objectName.toLatin1()) << " ***" << endl;
253 #else
254 cout << "*** " << string(objectName.toAscii()) << " ***" << endl;
255 #endif
256 return;
257 }
258
259 QRadioButton *rb = allRadioButtons.at(index);
260 rb->setChecked(isChecked);
261 }
262
263 // Check boxes:
264 //--------------
265 QList<QCheckBox *> allCheckBoxes = parentWidget->findChildren<QCheckBox *>();
266
267 QList<QString> cbObjectNames;
268 for(int i = 0; i < allCheckBoxes.size(); i++)
269 cbObjectNames.append(allCheckBoxes.at(i)->objectName());
270
271 widget = item->firstChildElement("widget");
272 for( ; !widget.isNull(); widget = widget.nextSiblingElement()) {
273
274 QString type = widget.attribute("type").trimmed();
275
276 if(type != "CheckBox")
277 continue;
278
279 QString objectName = widget.firstChildElement("objectName").text().trimmed();
280 bool isChecked = (widget.firstChildElement("isChecked").text().toInt() > 0);
281
282 if(objectName.isEmpty())
283 continue;
284
285 int index = cbObjectNames.indexOf(objectName);
286
287 if(index < 0) {
288 cout << "Load project: Check box: mismatch with object name" << endl;
289 #if WITH_QT5
290 cout << "*** " << string(objectName.toLatin1()) << " ***" << endl;
291 #else
292 cout << "*** " << string(objectName.toAscii()) << " ***" << endl;
293 #endif
294 return;
295 }
296
297 QCheckBox *cb = allCheckBoxes.at(index);
298 cb->setChecked(isChecked);
299 }
300
301 // Line edits:
302 //-------------
303 QList<QLineEdit *> allLineEdits = parentWidget->findChildren<QLineEdit *>();
304
305 QList<QString> leObjectNames;
306 for(int i = 0; i < allLineEdits.size(); i++)
307 leObjectNames.append(allLineEdits.at(i)->objectName());
308
309 widget = item->firstChildElement("widget");
310 for( ; !widget.isNull(); widget = widget.nextSiblingElement()) {
311
312 QString type = widget.attribute("type").trimmed();
313
314 if(type != "LineEdit")
315 continue;
316
317 QString objectName = widget.firstChildElement("objectName").text().trimmed();
318 QString text = widget.firstChildElement("text").text().trimmed();
319
320 if(objectName.isEmpty())
321 continue;
322
323 int index = leObjectNames.indexOf(objectName);
324
325 if(index < 0) {
326 cout << "Load project: LineEdit: mismatch with object name" << endl;
327 #if WITH_QT5
328 cout << "*** " << string(objectName.toLatin1()) << " ***" << endl;
329 #else
330 cout << "*** " << string(objectName.toAscii()) << " ***" << endl;
331 #endif
332 return;
333 }
334
335 QLineEdit *le = allLineEdits.at(index);
336 le->setText(text);
337 }
338
339 // Text edits:
340 //-------------
341 QList<QTextEdit *> allTextEdits = parentWidget->findChildren<QTextEdit *>();
342
343 QList<QString> teObjectNames;
344
345 for(int i = 0; i < allTextEdits.size(); i++)
346 teObjectNames.append(allTextEdits.at(i)->objectName());
347
348 widget = item->firstChildElement("widget");
349 for( ; !widget.isNull(); widget = widget.nextSiblingElement()) {
350
351 QString type = widget.attribute("type").trimmed();
352
353 if(type != "TextEdit")
354 continue;
355
356 QString objectName = widget.firstChildElement("objectName").text().trimmed();
357 QString text = widget.firstChildElement("text").text().trimmed();
358
359 if(objectName.isEmpty())
360 continue;
361
362 int index = teObjectNames.indexOf(objectName);
363
364 if(index < 0) {
365 cout << "Load project: TextEdit: mismatch with object name" << endl;
366 #if WITH_QT5
367 cout << "*** " << string(objectName.toLatin1()) << " ***" << endl;
368 #else
369 cout << "*** " << string(objectName.toAscii()) << " ***" << endl;
370 #endif
371 return;
372 }
373
374 QTextEdit *te = allTextEdits.at(index);
375 te->clear();
376 te->append(text);
377 }
378
379 // Combo boxes:
380 //--------------
381 #if WITH_QT5
382 QList<QComboBox *> allComboBoxes = parentWidget->findChildren<QComboBox *>(QString());
383 #else
384 QList<QComboBox *> allComboBoxes = parentWidget->findChildren<QComboBox *>();
385 #endif
386
387 QList<QString> cxObjectNames;
388 for(int i = 0; i < allComboBoxes.size(); i++)
389 cxObjectNames.append(allComboBoxes.at(i)->objectName());
390
391 widget = item->firstChildElement("widget");
392 for( ; !widget.isNull(); widget = widget.nextSiblingElement()) {
393
394 QString type = widget.attribute("type").trimmed();
395
396 if(type != "ComboBox")
397 continue;
398
399 QString objectName = widget.firstChildElement("objectName").text().trimmed();
400 int currentIndex = widget.firstChildElement("currentIndex").text().toInt();
401
402 if(objectName.isEmpty())
403 continue;
404
405 int index = cxObjectNames.indexOf(objectName);
406
407 if(index < 0) {
408 cout << "Load project: Combo box: mismatch with object name" << endl;
409 #if WITH_QT5
410 cout << "*** " << string(objectName.toLatin1()) << " ***" << endl;
411 #else
412 cout << "*** " << string(objectName.toAscii()) << " ***" << endl;
413 #endif
414 return;
415 }
416
417 QComboBox *cx = allComboBoxes.at(index);
418 cx->setCurrentIndex(currentIndex);
419 }
420 }
421