2 Copyright (C) 2013-2024 Ben Kibbey <bjk@luxsci.net>
4 This file is part of qpwmc.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library 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 GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
21 #include <QApplication>
22 #include <QMessageBox>
23 #include "applicationFormXmlStream.h"
25 ApplicationFormXmlStream::ApplicationFormXmlStream (const QString
&filename
,
29 currentAppString
= AppStringNone
;
31 currentElement
= ElementNone
;
32 currentWidget
= nullptr;
34 QFile
file (filename
);
35 file
.open(QIODevice::ReadOnly
| QIODevice::Text
);
36 stream
= new QXmlStreamReader (&file
);
39 QXmlStreamReader::TokenType type
= stream
->readNext ();
40 QString name
= stream
->name ().toUtf8().data ();
44 case QXmlStreamReader::Invalid
:
45 case QXmlStreamReader::StartDocument
:
46 case QXmlStreamReader::EndDocument
:
47 case QXmlStreamReader::Comment
:
48 case QXmlStreamReader::DTD
:
49 case QXmlStreamReader::EntityReference
:
50 case QXmlStreamReader::ProcessingInstruction
:
51 case QXmlStreamReader::NoToken
:
53 case QXmlStreamReader::StartElement
:
54 if (name
== "formRow")
60 currentAppString
= AppStringNone
;
61 currentWidget
= new ApplicationFormWidget ();
66 if (name
== "PwmdApplicationForm")
68 else if (name
== "name")
69 currentAppString
= AppStringName
;
70 else if (name
== "shortDescription")
71 currentAppString
= AppStringShortDescription
;
72 else if (name
== "description")
73 currentAppString
= AppStringDescription
;
80 currentElement
= ElementType
;
82 else if (name
== "elementSelector")
84 currentWidget
->setElementSelector ();
85 currentElement
= ElementSelector
;
87 else if (name
== "fileSelector")
89 currentWidget
->setFileSelector ();
90 currentElement
= ElementFileSelector
;
92 else if (name
== "dateSelector")
94 currentWidget
->setDateSelector ();
95 currentElement
= ElementDateSelector
;
97 else if (name
== "passwordGenerator")
99 currentWidget
->setPasswordGenerator ();
100 currentElement
= ElementPasswordGenerator
;
102 else if (name
== "id")
104 currentElement
= ElementId
;
106 else if (name
== "childOf")
108 currentElement
= ElementChildOf
;
110 else if (name
== "value")
112 currentElement
= ElementValue
;
114 else if (name
== "element")
116 currentElement
= ElementPwmdName
;
118 else if (name
== "label")
120 currentElement
= ElementLabel
;
122 else if (name
== "placeholderText")
124 currentElement
= ElementPlaceholderText
;
126 else if (name
== "radio")
128 currentElement
= ElementRadio
;
130 else if (name
== "hiddenInput")
132 currentWidget
->setHidden ();
133 currentElement
= ElementHiddenInput
;
135 else if (name
== "expiry")
137 currentWidget
->setExpiry ();
138 currentElement
= ElementExpiry
;
140 else if (name
== "required")
142 currentWidget
->setRequired ();
143 currentElement
= ElementRequired
;
145 else if (name
== "whatsThis")
147 currentElement
= ElementWhatsThis
;
150 case QXmlStreamReader::EndElement
:
151 if (name
== "formRow")
154 if (wizard
->pwm
&& (currentWidget
->id () == "pwmdSocket"
155 || currentWidget
->id () == "pwmdFilename"))
156 delete currentWidget
;
158 _widgets
.append (currentWidget
);
159 currentWidget
= nullptr;
162 case QXmlStreamReader::Characters
:
163 QString chars
= stream
->text ().toUtf8 ().data ();
164 if (chars
.trimmed ().isEmpty ())
167 switch (currentAppString
)
170 wizard
->ui
.wz_start
->setTitle (chars
);
172 case AppStringShortDescription
:
173 wizard
->ui
.wz_start
->setSubTitle (chars
);
175 case AppStringDescription
:
176 wizard
->ui
.wz_startTextEdit
->document ()->setHtml (chars
);
182 switch (currentElement
)
185 if (chars
== "radioButton")
187 currentWidget
->setType (ApplicationFormWidget::AppWidgetRadioButton
);
189 else if (chars
== "spinBox")
191 currentWidget
->setType (ApplicationFormWidget::AppWidgetSpinBox
);
193 else if (chars
== "lineEdit")
195 if (currentWidget
->dateSelector ())
196 currentWidget
->setType (ApplicationFormWidget::AppWidgetDateSelector
);
198 currentWidget
->setType (ApplicationFormWidget::AppWidgetLineEdit
);
200 else if (chars
== "checkBox")
202 currentWidget
->setType (ApplicationFormWidget::AppWidgetCheckBox
);
206 currentWidget
->setId (QString (chars
));
209 currentWidget
->setChildOf (chars
);
212 currentWidget
->setValue (chars
);
214 case ElementPwmdName
:
215 currentWidget
->setPwmdName (chars
);
218 currentWidget
->setLabel (chars
);
220 case ElementPlaceholderText
:
221 currentWidget
->setPlaceholderText (chars
);
224 currentWidget
->addRadioButton (chars
);
226 case ElementHiddenInput
:
227 currentWidget
->setHidden ();
230 currentWidget
->setExpiry ();
232 case ElementRequired
:
233 currentWidget
->setRequired ();
235 case ElementWhatsThis
:
236 currentWidget
->setWhatsThis (chars
);
243 } while (!stream
->atEnd ());
245 currentWidget
= new ApplicationFormWidget ();
246 currentWidget
->setType (ApplicationFormWidget::AppWidgetInteralOverwriteCheckBox
);
247 currentWidget
->setWhatsThis (QApplication::tr ("Allow overwriting of existing element content."));
248 currentWidget
->setLabel (QApplication::tr ("Overwrite content:"));
249 _widgets
.append (currentWidget
);
250 currentWidget
= nullptr;
252 if (stream
->error () != QXmlStreamReader::NoError
)
255 m
.setText (file
.fileName ());
256 m
.setInformativeText (QString ("%1\n\nLine %2, position %3.").arg (stream
->errorString ()).arg (stream
->lineNumber ()).arg (stream
->columnNumber ()));
257 m
.setIcon (QMessageBox::Critical
);
263 ApplicationFormXmlStream::~ApplicationFormXmlStream ()
265 while (_widgets
.count ())
267 ApplicationFormWidget
*w
= _widgets
.takeFirst ();
275 QList
<ApplicationFormWidget
*>
276 ApplicationFormXmlStream::widgets ()
281 QXmlStreamReader::Error
282 ApplicationFormXmlStream::error ()
284 return stream
->error ();