source: trunk/tools/designer/src/lib/shared/qdesigner_widgetbox.cpp@ 235

Last change on this file since 235 was 2, checked in by Dmitry A. Kuminov, 17 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 6.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the Qt Designer of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*
43TRANSLATOR qdesigner_internal::QDesignerWidgetBox
44*/
45
46#include "qdesigner_widgetbox_p.h"
47#include "qdesigner_utils_p.h"
48
49#include <ui4_p.h>
50
51#include <QtCore/QRegExp>
52#include <QtCore/QDebug>
53#include <QtCore/QXmlStreamReader>
54
55QT_BEGIN_NAMESPACE
56
57namespace qdesigner_internal {
58QDesignerWidgetBox::QDesignerWidgetBox(QWidget *parent, Qt::WindowFlags flags)
59 : QDesignerWidgetBoxInterface(parent, flags),
60 m_loadMode(LoadMerge)
61{
62
63}
64
65QDesignerWidgetBox::LoadMode QDesignerWidgetBox::loadMode() const
66{
67 return m_loadMode;
68}
69
70void QDesignerWidgetBox::setLoadMode(LoadMode lm)
71{
72 m_loadMode = lm;
73}
74
75// Convenience to find a widget by class name
76bool QDesignerWidgetBox::findWidget(const QDesignerWidgetBoxInterface *wbox,
77 const QString &className,
78 const QString &category,
79 Widget *widgetData)
80{
81 // Note that entry names do not necessarily match the class name
82 // (at least, not for the standard widgets), so,
83 // look in the XML for the class name of the first widget to appear
84 const QString widgetTag = QLatin1String("<widget");
85 QString pattern = QLatin1String("^<widget\\s+class\\s*=\\s*\"");
86 pattern += className;
87 pattern += QLatin1String("\".*$");
88 const QRegExp regexp(pattern);
89 Q_ASSERT(regexp.isValid());
90 const int catCount = wbox->categoryCount();
91 for (int c = 0; c < catCount; c++) {
92 const Category cat = wbox->category(c);
93 if (category.isEmpty() || cat.name() == category) {
94 const int widgetCount = cat.widgetCount();
95 for (int w = 0; w < widgetCount; w++) {
96 const Widget widget = cat.widget(w);
97 QString xml = widget.domXml(); // Erase the <ui> tag that can be present starting from 4.4
98 const int widgetTagIndex = xml.indexOf(widgetTag);
99 if (widgetTagIndex != -1) {
100 xml.remove(0, widgetTagIndex);
101 if (regexp.exactMatch(xml)) {
102 *widgetData = widget;
103 return true;
104 }
105 }
106 }
107 }
108 }
109 return false;
110}
111
112// Convenience to create a Dom Widget from widget box xml code.
113DomUI *QDesignerWidgetBox::xmlToUi(const QString &name, const QString &xml, bool insertFakeTopLevel,
114 QString *errorMessage)
115{
116 QXmlStreamReader reader(xml);
117 DomUI *ui = 0;
118
119 // The xml description must either contain a root element "ui" with a child element "widget"
120 // or "widget" as the root element (4.3 legacy)
121 const QString widgetTag = QLatin1String("widget");
122
123 while (!reader.atEnd()) {
124 if (reader.readNext() == QXmlStreamReader::StartElement) {
125 const QStringRef name = reader.name();
126 if (ui) {
127 reader.raiseError(tr("Unexpected element <%1>").arg(name.toString()));
128 continue;
129 }
130
131 if (name.compare(QLatin1String("widget"), Qt::CaseInsensitive) == 0) { // 4.3 legacy, wrap into DomUI
132 ui = new DomUI;
133 DomWidget *widget = new DomWidget;
134 widget->read(reader);
135 ui->setElementWidget(widget);
136 } else if (name.compare(QLatin1String("ui"), Qt::CaseInsensitive) == 0) { // 4.4
137 ui = new DomUI;
138 ui->read(reader);
139 } else {
140 reader.raiseError(tr("Unexpected element <%1>").arg(name.toString()));
141 }
142 }
143 }
144
145 if (reader.hasError()) {
146 delete ui;
147 *errorMessage = tr("A parse error occurred at line %1, column %2 of the XML code "
148 "specified for the widget %3: %4\n%5")
149 .arg(reader.lineNumber()).arg(reader.columnNumber()).arg(name)
150 .arg(reader.errorString()).arg(xml);
151 return 0;
152 }
153
154 if (!ui || !ui->elementWidget()) {
155 delete ui;
156 *errorMessage = tr("The XML code specified for the widget %1 does not contain "
157 "any widget elements.\n%2").arg(name).arg(xml);
158 return 0;
159 }
160
161 if (insertFakeTopLevel) {
162 DomWidget *fakeTopLevel = new DomWidget;
163 fakeTopLevel->setAttributeClass(QLatin1String("QWidget"));
164 QList<DomWidget *> children;
165 children.push_back(ui->takeElementWidget());
166 fakeTopLevel->setElementWidget(children);
167 ui->setElementWidget(fakeTopLevel);
168 }
169
170 return ui;
171}
172
173// Convenience to create a Dom Widget from widget box xml code.
174DomUI *QDesignerWidgetBox::xmlToUi(const QString &name, const QString &xml, bool insertFakeTopLevel)
175{
176 QString errorMessage;
177 DomUI *rc = xmlToUi(name, xml, insertFakeTopLevel, &errorMessage);
178 if (!rc)
179 qdesigner_internal::designerWarning(errorMessage);
180 return rc;
181}
182
183} // namespace qdesigner_internal
184
185QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.