~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to tools/designer/src/lib/uilib/formbuilder.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the designer application of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include <QtDesigner/QtDesigner>
 
30
#include <QtGui/QtGui>
 
31
 
 
32
/*!
 
33
    \class QFormBuilder
 
34
    \inmodule QtDesigner
 
35
*/
 
36
 
 
37
/*!
 
38
    \fn QFormBuilder::QFormBuilder()
 
39
*/
 
40
 
 
41
QFormBuilder::QFormBuilder()
 
42
{
 
43
}
 
44
 
 
45
QWidget *QFormBuilder::create(DomWidget *ui_widget, QWidget *parentWidget)
 
46
{
 
47
    return QAbstractFormBuilder::create(ui_widget, parentWidget);
 
48
}
 
49
 
 
50
 
 
51
QWidget *QFormBuilder::createWidget(const QString &widgetName, QWidget *parentWidget, const QString &name)
 
52
{
 
53
    QWidget *w = 0;
 
54
 
 
55
    if (qobject_cast<QTabWidget*>(parentWidget)
 
56
            || qobject_cast<QStackedWidget*>(parentWidget)
 
57
            || qobject_cast<QToolBox*>(parentWidget))
 
58
        parentWidget = 0;
 
59
 
 
60
    if (widgetName == QLatin1String("Line"))
 
61
        w = new QFrame(parentWidget);
 
62
 
 
63
#define DECLARE_LAYOUT(L, C)
 
64
#define DECLARE_COMPAT_WIDGET(W, C) /*DECLARE_WIDGET(W, C)*/
 
65
#define DECLARE_WIDGET(W, C) else if (widgetName == QLatin1String(#W)) { Q_ASSERT(w == 0); w = new W(parentWidget); }
 
66
 
 
67
#include "widgets.table"
 
68
 
 
69
#undef DECLARE_COMPAT_WIDGET
 
70
#undef DECLARE_LAYOUT
 
71
#undef DECLARE_WIDGET
 
72
 
 
73
    if (w == 0) { // try with a registered custom widget
 
74
        QDesignerCustomWidgetInterface *factory = m_customWidgets.value(widgetName);
 
75
        if (factory != 0)
 
76
            w = factory->createWidget(parentWidget);
 
77
    }
 
78
 
 
79
    if (w == 0) { // nothing to do
 
80
        return 0;
 
81
    }
 
82
 
 
83
    w->setObjectName(name);
 
84
 
 
85
    if (qobject_cast<QDialog *>(w))
 
86
        w->setParent(parentWidget, 0);
 
87
 
 
88
    return w;
 
89
}
 
90
 
 
91
QLayout *QFormBuilder::createLayout(const QString &layoutName, QObject *parent, const QString &name)
 
92
{
 
93
    QLayout *l = 0;
 
94
 
 
95
    QWidget *parentWidget = qobject_cast<QWidget*>(parent);
 
96
    QLayout *parentLayout = qobject_cast<QLayout*>(parent);
 
97
 
 
98
    Q_ASSERT(parentWidget || parentLayout);
 
99
 
 
100
#define DECLARE_WIDGET(W, C)
 
101
#define DECLARE_COMPAT_WIDGET(W, C)
 
102
 
 
103
#define DECLARE_LAYOUT(L, C) \
 
104
    if (layoutName == QLatin1String(#L)) { \
 
105
        Q_ASSERT(l == 0); \
 
106
        l = parentLayout \
 
107
            ? new L() \
 
108
            : new L(parentWidget); \
 
109
    }
 
110
 
 
111
#include "widgets.table"
 
112
 
 
113
#undef DECLARE_LAYOUT
 
114
#undef DECLARE_COMPAT_WIDGET
 
115
#undef DECLARE_WIDGET
 
116
 
 
117
    if (l) {
 
118
        l->setObjectName(name);
 
119
    } else {
 
120
        qWarning("layout `%s' not supported", layoutName.toUtf8().data());
 
121
    }
 
122
 
 
123
    return l;
 
124
}
 
125
 
 
126
bool QFormBuilder::addItem(DomLayoutItem *ui_item, QLayoutItem *item, QLayout *layout)
 
127
{
 
128
    return QAbstractFormBuilder::addItem(ui_item, item, layout);
 
129
}
 
130
 
 
131
bool QFormBuilder::addItem(DomWidget *ui_widget, QWidget *widget, QWidget *parentWidget)
 
132
{
 
133
    return QAbstractFormBuilder::addItem(ui_widget, widget, parentWidget);
 
134
}
 
135
 
 
136
QWidget *QFormBuilder::widgetByName(QWidget *topLevel, const QString &name)
 
137
{
 
138
    Q_ASSERT(topLevel);
 
139
    if (topLevel->objectName() == name)
 
140
        return topLevel;
 
141
 
 
142
    return qFindChild<QWidget*>(topLevel, name);
 
143
}
 
144
 
 
145
void QFormBuilder::createConnections(DomConnections *ui_connections, QWidget *widget)
 
146
{
 
147
    Q_ASSERT(widget != 0);
 
148
 
 
149
    if (ui_connections == 0)
 
150
        return;
 
151
 
 
152
    QList<DomConnection*> connections = ui_connections->elementConnection();
 
153
    foreach (DomConnection *c, connections) {
 
154
        QWidget *sender = widgetByName(widget, c->elementSender());
 
155
        QWidget *receiver = widgetByName(widget, c->elementReceiver());
 
156
        if (!sender || !receiver)
 
157
            continue;
 
158
 
 
159
        QByteArray sig = c->elementSignal().toUtf8();
 
160
        sig.prepend("2");
 
161
        QByteArray sl = c->elementSlot().toUtf8();
 
162
        sl.prepend("1");
 
163
 
 
164
        QObject::connect(sender, sig, receiver, sl);
 
165
    }
 
166
}
 
167
 
 
168
QWidget *QFormBuilder::create(DomUI *ui, QWidget *parentWidget)
 
169
{
 
170
    return QAbstractFormBuilder::create(ui, parentWidget);
 
171
}
 
172
 
 
173
QLayout *QFormBuilder::create(DomLayout *ui_layout, QLayout *layout, QWidget *parentWidget)
 
174
{
 
175
    return QAbstractFormBuilder::create(ui_layout, layout, parentWidget);
 
176
}
 
177
 
 
178
QLayoutItem *QFormBuilder::create(DomLayoutItem *ui_layoutItem, QLayout *layout, QWidget *parentWidget)
 
179
{
 
180
    return QAbstractFormBuilder::create(ui_layoutItem, layout, parentWidget);
 
181
}
 
182
 
 
183
QAction *QFormBuilder::create(DomAction *ui_action, QObject *parent)
 
184
{
 
185
    return QAbstractFormBuilder::create(ui_action, parent);
 
186
}
 
187
 
 
188
QActionGroup *QFormBuilder::create(DomActionGroup *ui_action_group, QObject *parent)
 
189
{
 
190
    return QAbstractFormBuilder::create(ui_action_group, parent);
 
191
}
 
192
 
 
193
/*!
 
194
*/
 
195
QStringList QFormBuilder::pluginPaths() const
 
196
{
 
197
    return m_pluginPaths;
 
198
}
 
199
 
 
200
/*!
 
201
*/
 
202
void QFormBuilder::clearPluginPaths()
 
203
{
 
204
    m_pluginPaths.clear();
 
205
    updateCustomWidgets();
 
206
}
 
207
 
 
208
/*!
 
209
*/
 
210
void QFormBuilder::addPluginPath(const QString &pluginPath)
 
211
{
 
212
    m_pluginPaths.append(pluginPath);
 
213
    updateCustomWidgets();
 
214
}
 
215
 
 
216
/*!
 
217
*/
 
218
void QFormBuilder::setPluginPath(const QStringList &pluginPaths)
 
219
{
 
220
    m_pluginPaths = pluginPaths;
 
221
    updateCustomWidgets();
 
222
}
 
223
 
 
224
void QFormBuilder::updateCustomWidgets()
 
225
{
 
226
    m_customWidgets.clear();
 
227
 
 
228
    foreach (QString path, m_pluginPaths) {
 
229
        QDir dir(path);
 
230
        QStringList candidates = dir.entryList(QDir::Files);
 
231
 
 
232
        foreach (QString plugin, candidates) {
 
233
            if (!QLibrary::isLibrary(plugin))
 
234
                continue;
 
235
 
 
236
            QPluginLoader loader(path + QLatin1String("/") + plugin);
 
237
            if (loader.load()) {
 
238
                // step 1) try with a normal plugin
 
239
                QDesignerCustomWidgetInterface *iface = 0;
 
240
                iface = qobject_cast<QDesignerCustomWidgetInterface *>(loader.instance());
 
241
                if (iface != 0) {
 
242
                    m_customWidgets.insert(iface->name(), iface);
 
243
                    continue;
 
244
                }
 
245
 
 
246
                // step 2) try with a collection of plugins
 
247
                QDesignerCustomWidgetCollectionInterface *c = 0;
 
248
                c = qobject_cast<QDesignerCustomWidgetCollectionInterface *>(loader.instance());
 
249
                if (c != 0) {
 
250
                    foreach (QDesignerCustomWidgetInterface *iface, c->customWidgets()) {
 
251
                        m_customWidgets.insert(iface->name(), iface);
 
252
                    }
 
253
                }
 
254
            }
 
255
        }
 
256
    }
 
257
}
 
258
 
 
259
/*!
 
260
    \fn QList<QDesignerCustomWidgetInterface*> QFormBuilder::customWidgets() const
 
261
*/
 
262
QList<QDesignerCustomWidgetInterface*> QFormBuilder::customWidgets() const
 
263
{
 
264
    return m_customWidgets.values();
 
265
}