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
|
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at info@qt.nokia.com.
**
**************************************************************************/
#include "formtemplatewizardpage.h"
#include "formeditorw.h"
#include "designerconstants.h"
#if QT_VERSION >= 0x050000
# include <QtDesigner/QDesignerNewFormWidgetInterface>
#else
# include "qt_private/abstractnewformwidget_p.h"
#endif
#include <QtCore/QDebug>
#include <QtCore/QXmlStreamReader>
#include <QtCore/QXmlStreamAttributes>
#include <QtCore/QByteArray>
#include <QtCore/QBuffer>
#include <QtGui/QVBoxLayout>
#include <QtGui/QMessageBox>
#include <QtGui/QAbstractButton>
#ifdef USE_XSLT
# include <QtXmlPatterns/QXmlQuery>
#else
# include <QtXml/QDomDocument>
#endif
namespace Designer {
namespace Internal {
// ----------------- FormTemplateWizardPage
FormTemplateWizardPage::FormTemplateWizardPage(QWidget * parent) :
QWizardPage(parent),
m_newFormWidget(QDesignerNewFormWidgetInterface::createNewFormWidget(FormEditorW::instance()->designerEditor())),
m_templateSelected(m_newFormWidget->hasCurrentTemplate())
{
setTitle(tr("Choose a Form Template"));
QVBoxLayout *layout = new QVBoxLayout;
connect(m_newFormWidget, SIGNAL(currentTemplateChanged(bool)),
this, SLOT(slotCurrentTemplateChanged(bool)));
connect(m_newFormWidget, SIGNAL(templateActivated()),
this, SIGNAL(templateActivated()));
layout->addWidget(m_newFormWidget);
setLayout(layout);
}
bool FormTemplateWizardPage::isComplete() const
{
return m_templateSelected;
}
void FormTemplateWizardPage::slotCurrentTemplateChanged(bool templateSelected)
{
if (m_templateSelected == templateSelected)
return;
m_templateSelected = templateSelected;
emit completeChanged();
}
bool FormTemplateWizardPage::validatePage()
{
QString errorMessage;
m_templateContents = m_newFormWidget->currentTemplate(&errorMessage);
if (m_templateContents.isEmpty()) {
QMessageBox::critical(this, tr("%1 - Error").arg(title()), errorMessage);
return false;
}
return true;
}
QString FormTemplateWizardPage::stripNamespaces(const QString &className)
{
QString rc = className;
const int namespaceIndex = rc.lastIndexOf(QLatin1String("::"));
if (namespaceIndex != -1)
rc.remove(0, namespaceIndex + 2);
return rc;
}
bool FormTemplateWizardPage::getUIXmlData(const QString &uiXml,
QString *formBaseClass,
QString *uiClassName)
{
// Parse UI xml to determine
// 1) The ui class name from "<class>Designer::Internal::FormClassWizardPage</class>"
// 2) the base class from: widget class="QWizardPage"...
QXmlStreamReader reader(uiXml);
while (!reader.atEnd()) {
if (reader.readNext() == QXmlStreamReader::StartElement) {
if (reader.name() == QLatin1String("class")) {
*uiClassName = reader.readElementText();
} else {
if (reader.name() == QLatin1String("widget")) {
const QXmlStreamAttributes attrs = reader.attributes();
*formBaseClass = reader.attributes().value(QLatin1String("class")).toString();
return !uiClassName->isEmpty() && !formBaseClass->isEmpty();
}
}
}
}
return false;
}
#ifdef USE_XSLT
// Change the UI class name in UI xml: This occurs several times, as contents
// of the <class> element, as name of the first <widget> element, and possibly
// in the signal/slot connections
static const char *classNameChangingSheetFormatC =
"<?xml version=\"1.0\"?>\n"
"<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"2.0\">\n"
"<xsl:output method=\"xml\" indent=\"yes\" encoding=\"UTF-8\" />\n"
"\n"
"<!-- Grab old class name to be replaced by evaluating <ui><class> -->\n"
"<xsl:param name=\"oldClassName\"><xsl:value-of select=\"/ui/class\"/></xsl:param>\n"
"\n"
"<!-- heavy wizardry to copy nodes and attributes (emulating the default rules) -->\n"
"<xsl:template match=\"@*|node()\">\n"
" <xsl:copy>\n"
" <xsl:apply-templates select=\"@*|node()\"/>\n"
" </xsl:copy>\n"
"</xsl:template>\n"
"\n"
"<!-- Change <ui><class> tag -->\n"
"<xsl:template match=\"class\">\n"
" <xsl:element name=\"class\">\n"
" <xsl:text>%1</xsl:text>\n"
" </xsl:element>\n"
"</xsl:template>\n"
"\n"
"<!-- Change first <widget> tag -->\n"
"<xsl:template match=\"/ui/widget/@name\">\n"
"<xsl:attribute name=\"name\">\n"
"<xsl:text>%1</xsl:text>\n"
"</xsl:attribute>\n"
"</xsl:template>\n"
"\n"
"<!-- Change <sender>/<receiver> elements (for pre-wired QDialog signals) -->\n"
"<xsl:template match=\"receiver[.='Dialog']\">\n"
" <xsl:element name=\"receiver\">\n"
" <xsl:text>%1</xsl:text>\n"
" </xsl:element>\n"
"</xsl:template>\n"
"<xsl:template match=\"sender[.='Dialog']\">\n"
" <xsl:element name=\"sender\">\n"
" <xsl:text>%1</xsl:text>\n"
" </xsl:element>\n"
"</xsl:template>\n"
"</xsl:stylesheet>\n";
QString FormTemplateWizardPage::changeUiClassName(const QString &uiXml, const QString &newUiClassName)
{
// Prepare I/O: Sheet
const QString xsltSheet = QString::fromLatin1(classNameChangingSheetFormatC).arg(newUiClassName);
QByteArray xsltSheetBA = xsltSheet.toUtf8();
QBuffer xsltSheetBuffer(&xsltSheetBA);
xsltSheetBuffer.open(QIODevice::ReadOnly);
// Prepare I/O: Xml
QByteArray xmlBA = uiXml.toUtf8();
QBuffer xmlBuffer(&xmlBA);
xmlBuffer.open(QIODevice::ReadOnly);
// Prepare I/O: output
QBuffer outputBuffer;
outputBuffer.open(QIODevice::WriteOnly);
// Run query
QXmlQuery query(QXmlQuery::XSLT20);
query.setFocus(&xmlBuffer);
query.setQuery(&xsltSheetBuffer);
if (!query.evaluateTo(&outputBuffer)) {
qWarning("Unable to change the ui class name in a form template.\n%s\nUsing:\n%s\n",
xmlBA.constData(), xsltSheetBA.constData());
return uiXml;
}
outputBuffer.close();
return QString::fromUtf8(outputBuffer.data());
}
#else
// Change the contents of a DOM element to a new value if it matches
// a predicate
template <class Predicate>
bool changeDomElementContents(const QDomElement &element,
Predicate p,
const QString &newValue,
QString *ptrToOldValue = 0)
{
// Find text in "<element>text</element>"
const QDomNodeList children = element.childNodes();
if (children.size() != 1)
return false;
const QDomNode first = children.at(0);
if (first.nodeType() != QDomNode::TextNode)
return false;
QDomCharacterData data = first.toCharacterData();
const QString oldValue = data.data();
if (p(oldValue)) {
if (ptrToOldValue)
*ptrToOldValue = oldValue;
data.setData(newValue);
return true;
}
return false;
}
namespace {
bool truePredicate(const QString &) { return true; }
// Predicate that matches a string value
class MatchPredicate {
public:
MatchPredicate(const QString &m) : m_match(m) {}
bool operator()(const QString &s) const { return s == m_match; }
private:
const QString m_match;
};
// Change <sender> and <receiver> in a Dom UI <connections> list
// if they match the class name passed on
void changeDomConnectionList(const QDomElement &connectionsNode,
const QString &oldClassName,
const QString &newClassName)
{
const MatchPredicate oldClassPredicate(oldClassName);
const QString senderTag = QLatin1String("sender");
const QString receiverTag = QLatin1String("receiver");
const QDomNodeList connections = connectionsNode.childNodes();
const int connectionsCount = connections.size();
// Loop <connection>
for (int c = 0; c < connectionsCount; c++) {
const QDomNodeList connectionElements = connections.at(c).childNodes();
const int connectionElementCount = connectionElements.count();
// Loop <sender>, <receiver>, <signal>, <slot>
for (int ce = 0; ce < connectionElementCount; ce++) {
const QDomNode connectionElementNode = connectionElements.at(ce);
if (connectionElementNode.isElement()) {
const QDomElement connectionElement = connectionElementNode.toElement();
const QString tagName = connectionElement.tagName();
if (tagName == senderTag || tagName == receiverTag)
changeDomElementContents(connectionElement, oldClassPredicate, newClassName);
}
}
}
}
}
// Change the UI class name in UI xml: This occurs several times, as contents
// of the <class> element, as name of the first <widget> element, and possibly
// in the signal/slot connections
QString FormTemplateWizardPage::changeUiClassName(const QString &uiXml, const QString &newUiClassName)
{
if (Designer::Constants::Internal::debug)
qDebug() << '>' << Q_FUNC_INFO << newUiClassName;
QDomDocument domUi;
if (!domUi.setContent(uiXml)) {
qWarning("Failed to parse:\n%s", uiXml.toUtf8().constData());
return uiXml;
}
bool firstWidgetElementFound = false;
QString oldClassName;
// Loop first level children. First child is <ui>
const QDomNodeList children = domUi.firstChildElement().childNodes();
const QString classTag = QLatin1String("class");
const QString widgetTag = QLatin1String("widget");
const QString connectionsTag = QLatin1String("connections");
const int count = children.size();
for (int i = 0; i < count; i++) {
const QDomNode node = children.at(i);
if (node.isElement()) {
// Replace <class> element text
QDomElement element = node.toElement();
const QString name = element.tagName();
if (name == classTag) {
if (!changeDomElementContents(element, truePredicate, newUiClassName, &oldClassName)) {
qWarning("Unable to change the <class> element:\n%s", uiXml.toUtf8().constData());
return uiXml;
}
} else {
// Replace first <widget> element name attribute
if (!firstWidgetElementFound && name == widgetTag) {
firstWidgetElementFound = true;
const QString nameAttribute = QLatin1String("name");
if (element.hasAttribute(nameAttribute))
element.setAttribute(nameAttribute, newUiClassName);
} else {
// Replace <sender>, <receiver> tags of dialogs.
if (name == connectionsTag)
changeDomConnectionList(element, oldClassName, newUiClassName);
}
}
}
}
const QString rc = domUi.toString();
if (Designer::Constants::Internal::debug > 1)
qDebug() << '<' << Q_FUNC_INFO << newUiClassName << rc;
return rc;
}
#endif // USE_XSLT
} // namespace Internal
} // namespace Designer
|