Add help text for attribute S/R.
[qpwmc.git] / applicationFormXmlStream.cpp
blob165b9e5666585b9aa576915e5382c81d3f144b50
1 /*
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
19 USA
21 #include <QApplication>
22 #include <QMessageBox>
23 #include "applicationFormXmlStream.h"
25 ApplicationFormXmlStream::ApplicationFormXmlStream (const QString &filename,
26 ApplicationForm *w)
28 inWidget = 0;
29 currentAppString = AppStringNone;
30 wizard = w;
31 currentElement = ElementNone;
32 currentWidget = nullptr;
34 QFile file (filename);
35 file.open(QIODevice::ReadOnly | QIODevice::Text);
36 stream = new QXmlStreamReader (&file);
38 do {
39 QXmlStreamReader::TokenType type = stream->readNext ();
40 QString name = stream->name ().toUtf8().data ();
42 switch (type)
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:
52 break;
53 case QXmlStreamReader::StartElement:
54 if (name == "formRow")
56 if (inWidget)
57 break;
59 inWidget = true;
60 currentAppString = AppStringNone;
61 currentWidget = new ApplicationFormWidget ();
62 break;
64 else if (!inWidget)
66 if (name == "PwmdApplicationForm")
67 break;
68 else if (name == "name")
69 currentAppString = AppStringName;
70 else if (name == "shortDescription")
71 currentAppString = AppStringShortDescription;
72 else if (name == "description")
73 currentAppString = AppStringDescription;
75 break;
78 if (name == "type")
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;
149 break;
150 case QXmlStreamReader::EndElement:
151 if (name == "formRow")
153 inWidget = false;
154 if (wizard->pwm && (currentWidget->id () == "pwmdSocket"
155 || currentWidget->id () == "pwmdFilename"))
156 delete currentWidget;
157 else
158 _widgets.append (currentWidget);
159 currentWidget = nullptr;
161 break;
162 case QXmlStreamReader::Characters:
163 QString chars = stream->text ().toUtf8 ().data ();
164 if (chars.trimmed ().isEmpty ())
165 break;
167 switch (currentAppString)
169 case AppStringName:
170 wizard->ui.wz_start->setTitle (chars);
171 break;
172 case AppStringShortDescription:
173 wizard->ui.wz_start->setSubTitle (chars);
174 break;
175 case AppStringDescription:
176 wizard->ui.wz_startTextEdit->document ()->setHtml (chars);
177 break;
178 default:
179 break;
182 switch (currentElement)
184 case ElementType:
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);
197 else
198 currentWidget->setType (ApplicationFormWidget::AppWidgetLineEdit);
200 else if (chars == "checkBox")
202 currentWidget->setType (ApplicationFormWidget::AppWidgetCheckBox);
204 break;
205 case ElementId:
206 currentWidget->setId (QString (chars));
207 break;
208 case ElementChildOf:
209 currentWidget->setChildOf (chars);
210 break;
211 case ElementValue:
212 currentWidget->setValue (chars);
213 break;
214 case ElementPwmdName:
215 currentWidget->setPwmdName (chars);
216 break;
217 case ElementLabel:
218 currentWidget->setLabel (chars);
219 break;
220 case ElementPlaceholderText:
221 currentWidget->setPlaceholderText (chars);
222 break;
223 case ElementRadio:
224 currentWidget->addRadioButton (chars);
225 break;
226 case ElementHiddenInput:
227 currentWidget->setHidden ();
228 break;
229 case ElementExpiry:
230 currentWidget->setExpiry ();
231 break;
232 case ElementRequired:
233 currentWidget->setRequired ();
234 break;
235 case ElementWhatsThis:
236 currentWidget->setWhatsThis (chars);
237 break;
238 default:
239 break;
241 break;
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)
254 QMessageBox m;
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);
258 m.exec ();
259 return;
263 ApplicationFormXmlStream::~ApplicationFormXmlStream ()
265 while (_widgets.count ())
267 ApplicationFormWidget *w = _widgets.takeFirst ();
268 delete w;
271 _widgets.clear ();
272 delete stream;
275 QList<ApplicationFormWidget *>
276 ApplicationFormXmlStream::widgets ()
278 return _widgets;
281 QXmlStreamReader::Error
282 ApplicationFormXmlStream::error ()
284 return stream->error ();