Path: blob/devel/ElmerGUI/Application/src/projectio.cpp
3203 views
/*****************************************************************************1* *2* Elmer, A Finite Element Software for Multiphysical Problems *3* *4* Copyright 1st April 1995 - , CSC - IT Center for Science Ltd., Finland *5* *6* This program is free software; you can redistribute it and/or *7* modify it under the terms of the GNU General Public License *8* as published by the Free Software Foundation; either version 2 *9* of the License, or (at your option) any later version. *10* *11* This program is distributed in the hope that it will be useful, *12* but WITHOUT ANY WARRANTY; without even the implied warranty of *13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *14* GNU General Public License for more details. *15* *16* You should have received a copy of the GNU General Public License *17* along with this program (in file fem/GPL-2); if not, write to the *18* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *19* Boston, MA 02110-1301, USA. *20* *21*****************************************************************************/2223/*****************************************************************************24* *25* ElmerGUI projectio *26* *27*****************************************************************************28* *29* Authors: Mikko Lyly, Juha Ruokolainen and Peter R�back *30* Email: [email protected] *31* Web: http://www.csc.fi/elmer *32* Address: CSC - IT Center for Science Ltd. *33* Keilaranta 14 *34* 02101 Espoo, Finland *35* *36* Original Date: 15 Mar 2008 *37* *38*****************************************************************************/3940#include <QtGui>41#include <iostream>42#include "projectio.h"4344#if WITH_QT5 || WITH_QT645#include <QtWidgets>46#endif4748using namespace std;4950ProjectIO::ProjectIO(QWidget *parent)51: QDialog(parent)52{53parentWidget = parent;54}5556ProjectIO::~ProjectIO()57{58}5960void ProjectIO::appendToProject(QDomDocument *projectDoc, QDomElement *item)61{62// Radio buttons:63//----------------64QList<QRadioButton *> allRadioButtons = parentWidget->findChildren<QRadioButton *>();6566for(int i = 0; i < allRadioButtons.size(); i++) {67QRadioButton *rb = allRadioButtons.at(i);6869if(!rb)70continue;7172QString rbObjectName = rb->objectName();73QString rbValue = QString::number(rb->isChecked());7475if(rbObjectName.isEmpty())76continue;7778QDomElement widget = projectDoc->createElement("widget");79widget.setAttribute("type", "RadioButton");80item->appendChild(widget);8182QDomElement objectName = projectDoc->createElement("objectName");83QDomText objectNameValue = projectDoc->createTextNode(rbObjectName);84objectName.appendChild(objectNameValue);85widget.appendChild(objectName);8687QDomElement isChecked = projectDoc->createElement("isChecked");88QDomText isCheckedValue = projectDoc->createTextNode(rbValue);89isChecked.appendChild(isCheckedValue);90widget.appendChild(isChecked);91}9293// Check boxes:94//--------------95QList<QCheckBox *> allCheckBoxes = parentWidget->findChildren<QCheckBox *>();9697for(int i = 0; i < allCheckBoxes.size(); i++) {98QCheckBox *cb = allCheckBoxes.at(i);99100if(!cb)101continue;102103QString cbObjectName = cb->objectName();104QString cbValue = QString::number(cb->isChecked());105106if(cbObjectName.isEmpty())107continue;108109QDomElement widget = projectDoc->createElement("widget");110widget.setAttribute("type", "CheckBox");111item->appendChild(widget);112113QDomElement objectName = projectDoc->createElement("objectName");114QDomText objectNameValue = projectDoc->createTextNode(cbObjectName);115objectName.appendChild(objectNameValue);116widget.appendChild(objectName);117118QDomElement isChecked = projectDoc->createElement("isChecked");119QDomText isCheckedValue = projectDoc->createTextNode(cbValue);120isChecked.appendChild(isCheckedValue);121widget.appendChild(isChecked);122}123124// Line edits:125//-------------126QList<QLineEdit *> allLineEdits = parentWidget->findChildren<QLineEdit *>();127128for(int i = 0; i < allLineEdits.size(); i++) {129QLineEdit *le = allLineEdits.at(i);130131if(!le)132continue;133134QString leObjectName = le->objectName();135QString leValue = le->text().trimmed();136137if(leObjectName.isEmpty())138continue;139140QDomElement widget = projectDoc->createElement("widget");141widget.setAttribute("type", "LineEdit");142item->appendChild(widget);143144QDomElement objectName = projectDoc->createElement("objectName");145QDomText objectNameValue = projectDoc->createTextNode(leObjectName);146objectName.appendChild(objectNameValue);147widget.appendChild(objectName);148149QDomElement text = projectDoc->createElement("text");150QDomText textValue = projectDoc->createTextNode(leValue);151text.appendChild(textValue);152widget.appendChild(text);153}154155// Text edits:156//-------------157QList<QTextEdit *> allTextEdits = parentWidget->findChildren<QTextEdit *>();158159for(int i = 0; i < allTextEdits.size(); i++) {160QTextEdit *te = allTextEdits.at(i);161162if(!te)163continue;164165QString teObjectName = te->objectName();166QString teValue = te->toPlainText();167168if(teObjectName.isEmpty())169continue;170171QDomElement widget = projectDoc->createElement("widget");172widget.setAttribute("type", "TextEdit");173item->appendChild(widget);174175QDomElement objectName = projectDoc->createElement("objectName");176QDomText objectNameValue = projectDoc->createTextNode(teObjectName);177objectName.appendChild(objectNameValue);178widget.appendChild(objectName);179180QDomElement text = projectDoc->createElement("text");181QDomText textValue = projectDoc->createTextNode(teValue);182text.appendChild(textValue);183widget.appendChild(text);184}185186// Combo boxes:187//--------------188QList<QComboBox *> allComboBoxes = parentWidget->findChildren<QComboBox *>();189190for(int i = 0; i < allComboBoxes.size(); i++) {191QComboBox *cx = allComboBoxes.at(i);192193if(!cx)194continue;195196QString cxObjectName = cx->objectName();197QString cxValue = QString::number(cx->currentIndex());198199if(cxObjectName.isEmpty())200continue;201202QDomElement widget = projectDoc->createElement("widget");203widget.setAttribute("type", "ComboBox");204item->appendChild(widget);205206QDomElement objectName = projectDoc->createElement("objectName");207QDomText objectNameValue = projectDoc->createTextNode(cxObjectName);208objectName.appendChild(objectNameValue);209widget.appendChild(objectName);210211QDomElement currentIndex = projectDoc->createElement("currentIndex");212QDomText currentIndexValue = projectDoc->createTextNode(cxValue);213currentIndex.appendChild(currentIndexValue);214widget.appendChild(currentIndex);215}216}217218void ProjectIO::readFromProject(QDomDocument *projectDoc, QDomElement *item)219{220// Radio buttons:221//----------------222#if WITH_QT5 || WITH_QT6223QList<QRadioButton *> allRadioButtons = parentWidget->findChildren<QRadioButton *>(QString());224#else225QList<QRadioButton *> allRadioButtons = parentWidget->findChildren<QRadioButton *>();226#endif227228QList<QString> rbObjectNames;229for(int i = 0; i < allRadioButtons.size(); i++)230rbObjectNames.append(allRadioButtons.at(i)->objectName());231232QDomElement widget = item->firstChildElement("widget");233for( ; !widget.isNull(); widget = widget.nextSiblingElement()) {234235QString type = widget.attribute("type").trimmed();236237if(type != "RadioButton")238continue;239240QString objectName = widget.firstChildElement("objectName").text().trimmed();241bool isChecked = (widget.firstChildElement("isChecked").text().toInt() > 0);242243if(objectName.isEmpty())244continue;245246int index = rbObjectNames.indexOf(objectName);247248if(index < 0) {249cout << "Load project: RadioButton: mismatch with object name" << endl;250#if WITH_QT5 || WITH_QT6251cout << "*** " << string(objectName.toLatin1()) << " ***" << endl;252#else253cout << "*** " << string(objectName.toAscii()) << " ***" << endl;254#endif255return;256}257258QRadioButton *rb = allRadioButtons.at(index);259rb->setChecked(isChecked);260}261262// Check boxes:263//--------------264QList<QCheckBox *> allCheckBoxes = parentWidget->findChildren<QCheckBox *>();265266QList<QString> cbObjectNames;267for(int i = 0; i < allCheckBoxes.size(); i++)268cbObjectNames.append(allCheckBoxes.at(i)->objectName());269270widget = item->firstChildElement("widget");271for( ; !widget.isNull(); widget = widget.nextSiblingElement()) {272273QString type = widget.attribute("type").trimmed();274275if(type != "CheckBox")276continue;277278QString objectName = widget.firstChildElement("objectName").text().trimmed();279bool isChecked = (widget.firstChildElement("isChecked").text().toInt() > 0);280281if(objectName.isEmpty())282continue;283284int index = cbObjectNames.indexOf(objectName);285286if(index < 0) {287cout << "Load project: Check box: mismatch with object name" << endl;288#if WITH_QT5 || WITH_QT6289cout << "*** " << string(objectName.toLatin1()) << " ***" << endl;290#else291cout << "*** " << string(objectName.toAscii()) << " ***" << endl;292#endif293return;294}295296QCheckBox *cb = allCheckBoxes.at(index);297cb->setChecked(isChecked);298}299300// Line edits:301//-------------302QList<QLineEdit *> allLineEdits = parentWidget->findChildren<QLineEdit *>();303304QList<QString> leObjectNames;305for(int i = 0; i < allLineEdits.size(); i++)306leObjectNames.append(allLineEdits.at(i)->objectName());307308widget = item->firstChildElement("widget");309for( ; !widget.isNull(); widget = widget.nextSiblingElement()) {310311QString type = widget.attribute("type").trimmed();312313if(type != "LineEdit")314continue;315316QString objectName = widget.firstChildElement("objectName").text().trimmed();317QString text = widget.firstChildElement("text").text().trimmed();318319if(objectName.isEmpty())320continue;321322int index = leObjectNames.indexOf(objectName);323324if(index < 0) {325cout << "Load project: LineEdit: mismatch with object name" << endl;326#if WITH_QT5 || WITH_QT6327cout << "*** " << string(objectName.toLatin1()) << " ***" << endl;328#else329cout << "*** " << string(objectName.toAscii()) << " ***" << endl;330#endif331return;332}333334QLineEdit *le = allLineEdits.at(index);335le->setText(text);336}337338// Text edits:339//-------------340QList<QTextEdit *> allTextEdits = parentWidget->findChildren<QTextEdit *>();341342QList<QString> teObjectNames;343344for(int i = 0; i < allTextEdits.size(); i++)345teObjectNames.append(allTextEdits.at(i)->objectName());346347widget = item->firstChildElement("widget");348for( ; !widget.isNull(); widget = widget.nextSiblingElement()) {349350QString type = widget.attribute("type").trimmed();351352if(type != "TextEdit")353continue;354355QString objectName = widget.firstChildElement("objectName").text().trimmed();356QString text = widget.firstChildElement("text").text().trimmed();357358if(objectName.isEmpty())359continue;360361int index = teObjectNames.indexOf(objectName);362363if(index < 0) {364cout << "Load project: TextEdit: mismatch with object name" << endl;365#if WITH_QT5 || WITH_QT6366cout << "*** " << string(objectName.toLatin1()) << " ***" << endl;367#else368cout << "*** " << string(objectName.toAscii()) << " ***" << endl;369#endif370return;371}372373QTextEdit *te = allTextEdits.at(index);374te->clear();375te->append(text);376}377378// Combo boxes:379//--------------380#if WITH_QT5 || WITH_QT6381QList<QComboBox *> allComboBoxes = parentWidget->findChildren<QComboBox *>(QString());382#else383QList<QComboBox *> allComboBoxes = parentWidget->findChildren<QComboBox *>();384#endif385386QList<QString> cxObjectNames;387for(int i = 0; i < allComboBoxes.size(); i++)388cxObjectNames.append(allComboBoxes.at(i)->objectName());389390widget = item->firstChildElement("widget");391for( ; !widget.isNull(); widget = widget.nextSiblingElement()) {392393QString type = widget.attribute("type").trimmed();394395if(type != "ComboBox")396continue;397398QString objectName = widget.firstChildElement("objectName").text().trimmed();399int currentIndex = widget.firstChildElement("currentIndex").text().toInt();400401if(objectName.isEmpty())402continue;403404int index = cxObjectNames.indexOf(objectName);405406if(index < 0) {407cout << "Load project: Combo box: mismatch with object name" << endl;408#if WITH_QT5 || WITH_QT6409cout << "*** " << string(objectName.toLatin1()) << " ***" << endl;410#else411cout << "*** " << string(objectName.toAscii()) << " ***" << endl;412#endif413return;414}415416QComboBox *cx = allComboBoxes.at(index);417cx->setCurrentIndex(currentIndex);418}419}420421422