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

« back to all changes in this revision

Viewing changes to tools/designer/src/lib/shared/qdesigner_propertysheet.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 "qdesigner_propertysheet_p.h"
 
30
#include "qdesigner_utils_p.h"
 
31
 
 
32
#include <QtDesigner/QDesignerFormWindowInterface>
 
33
 
 
34
#include <QtCore/QVariant>
 
35
#include <QtCore/QMetaObject>
 
36
#include <QtCore/QMetaProperty>
 
37
#include <QtGui/QImage>
 
38
#include <QtGui/QPixmap>
 
39
#include <QtCore/qdebug.h>
 
40
 
 
41
static const QMetaObject *introducedBy(const QMetaObject *meta, int index)
 
42
{
 
43
    if (index >= meta->propertyOffset())
 
44
        return meta;
 
45
 
 
46
    if (meta->superClass())
 
47
        return introducedBy(meta->superClass(), index);
 
48
 
 
49
    return 0;
 
50
}
 
51
 
 
52
QDesignerPropertySheet::QDesignerPropertySheet(QObject *object, QObject *parent)
 
53
    : QObject(parent),
 
54
      m_object(object),
 
55
      meta(object->metaObject())
 
56
{
 
57
    const QMetaObject *baseMeta = meta;
 
58
 
 
59
    while (baseMeta && QString::fromUtf8(baseMeta->className()).startsWith(QLatin1String("QDesigner"))) {
 
60
        baseMeta = baseMeta->superClass();
 
61
    }
 
62
    Q_ASSERT(baseMeta != 0);
 
63
 
 
64
    // ### hack
 
65
    for (int index=0; index<count(); ++index) {
 
66
        QMetaProperty p = meta->property(index);
 
67
        setVisible(index, p.isDesignable(m_object));
 
68
 
 
69
        if (p.type() == QVariant::KeySequence)
 
70
            createFakeProperty(QString::fromUtf8(p.name()));
 
71
 
 
72
        QString pgroup = QString::fromUtf8(baseMeta->className());
 
73
 
 
74
        if (const QMetaObject *pmeta = introducedBy(baseMeta, index)) {
 
75
            pgroup = QString::fromUtf8(pmeta->className());
 
76
        }
 
77
 
 
78
        setPropertyGroup(index, pgroup);
 
79
    }
 
80
 
 
81
    if (object->isWidgetType()) {
 
82
        createFakeProperty(QLatin1String("focusPolicy"));
 
83
        createFakeProperty(QLatin1String("cursor"));
 
84
        createFakeProperty(QLatin1String("toolTip"));
 
85
        createFakeProperty(QLatin1String("whatsThis"));
 
86
        createFakeProperty(QLatin1String("acceptDrops"));
 
87
        createFakeProperty(QLatin1String("dragEnabled"));
 
88
        createFakeProperty(QLatin1String("modal"));
 
89
    }
 
90
}
 
91
 
 
92
QDesignerPropertySheet::~QDesignerPropertySheet()
 
93
{
 
94
}
 
95
 
 
96
void QDesignerPropertySheet::createFakeProperty(const QString &propertyName, const QVariant &value)
 
97
{
 
98
    // fake properties
 
99
    int index = meta->indexOfProperty(propertyName.toUtf8());
 
100
    if (index != -1) {
 
101
        setVisible(index, false);
 
102
        QVariant v = value.isValid() ? value : metaProperty(index);
 
103
        m_fakeProperties.insert(index, v);
 
104
    } else if (value.isValid()) { // additional properties
 
105
        int index = count();
 
106
        m_addIndex.insert(propertyName, index);
 
107
        m_addProperties.insert(index, value);
 
108
    }
 
109
}
 
110
 
 
111
bool QDesignerPropertySheet::isAdditionalProperty(int index) const
 
112
{
 
113
    return m_addProperties.contains(index);
 
114
}
 
115
 
 
116
bool QDesignerPropertySheet::isFakeProperty(int index) const
 
117
{
 
118
    // additional properties must be fake
 
119
    return (m_fakeProperties.contains(index) || isAdditionalProperty(index));
 
120
}
 
121
 
 
122
int QDesignerPropertySheet::count() const
 
123
{
 
124
    return meta->propertyCount() + m_addProperties.count();
 
125
}
 
126
 
 
127
int QDesignerPropertySheet::indexOf(const QString &name) const
 
128
{
 
129
    int index = meta->indexOfProperty(name.toUtf8());
 
130
 
 
131
    if (index == -1)
 
132
        index = m_addIndex.value(name, -1);
 
133
 
 
134
    return index;
 
135
}
 
136
 
 
137
QString QDesignerPropertySheet::propertyName(int index) const
 
138
{
 
139
    if (isAdditionalProperty(index))
 
140
        return m_addIndex.key(index);
 
141
 
 
142
    return QString::fromUtf8(meta->property(index).name());
 
143
}
 
144
 
 
145
QString QDesignerPropertySheet::propertyGroup(int index) const
 
146
{
 
147
    QString g = m_info.value(index).group;
 
148
 
 
149
    if (!g.isEmpty())
 
150
        return g;
 
151
 
 
152
    if (propertyName(index).startsWith(QLatin1String("accessible")))
 
153
        return QString::fromUtf8("Accessibility");
 
154
 
 
155
    if (isAdditionalProperty(index))
 
156
        return QString::fromUtf8(meta->className());
 
157
 
 
158
    return g;
 
159
}
 
160
 
 
161
void QDesignerPropertySheet::setPropertyGroup(int index, const QString &group)
 
162
{
 
163
    if (!m_info.contains(index))
 
164
        m_info.insert(index, Info());
 
165
 
 
166
    m_info[index].group = group;
 
167
}
 
168
 
 
169
QVariant QDesignerPropertySheet::property(int index) const
 
170
{
 
171
    if (isAdditionalProperty(index)) {
 
172
        return m_addProperties.value(index);
 
173
    }
 
174
 
 
175
    if (isFakeProperty(index)) {
 
176
        return m_fakeProperties.value(index);
 
177
    }
 
178
 
 
179
    QMetaProperty p = meta->property(index);
 
180
    QVariant v = p.read(m_object);
 
181
 
 
182
    if (p.isFlagType()) {
 
183
        FlagType e;
 
184
        e.value = v;
 
185
        QMetaEnum me = p.enumerator();
 
186
        for (int i=0; i<me.keyCount(); ++i) {
 
187
            QString k = QString::fromUtf8(me.scope());
 
188
            k += QString::fromUtf8("::");
 
189
            k += QLatin1String(me.key(i));
 
190
            e.items.insert(k, me.keyToValue(k.toUtf8()));
 
191
        }
 
192
 
 
193
        qVariantSetValue(v, e);
 
194
    } else if (p.isEnumType()) {
 
195
        EnumType e;
 
196
        e.value = v;
 
197
        QMetaEnum me = p.enumerator();
 
198
        for (int i=0; i<me.keyCount(); ++i) {
 
199
            QString k = QString::fromUtf8(me.scope());
 
200
            k += QString::fromUtf8("::");
 
201
            k += QLatin1String(me.key(i));
 
202
            e.items.insert(k, me.keyToValue(k.toUtf8()));
 
203
        }
 
204
 
 
205
        qVariantSetValue(v, e);
 
206
    }
 
207
 
 
208
    return v;
 
209
}
 
210
 
 
211
QVariant QDesignerPropertySheet::metaProperty(int index) const
 
212
{
 
213
    Q_ASSERT(!isFakeProperty(index));
 
214
 
 
215
    QMetaProperty p = meta->property(index);
 
216
    QVariant v = p.read(m_object);
 
217
 
 
218
    if (p.isFlagType()) {
 
219
        FlagType e;
 
220
        e.value = v;
 
221
        QMetaEnum me = p.enumerator();
 
222
        for (int i=0; i<me.keyCount(); ++i) {
 
223
            QString key;
 
224
            key += QLatin1String(me.scope());
 
225
            key += QLatin1String("::");
 
226
            key += QLatin1String(me.key(i));
 
227
 
 
228
            e.items.insert(key, me.keyToValue(key.toUtf8()));
 
229
        }
 
230
 
 
231
        qVariantSetValue(v, e);
 
232
    } else if (p.isEnumType()) {
 
233
        EnumType e;
 
234
        e.value = v;
 
235
        QMetaEnum me = p.enumerator();
 
236
        for (int i=0; i<me.keyCount(); ++i) {
 
237
            QString key;
 
238
            key += QLatin1String(me.scope());
 
239
            key += QLatin1String("::");
 
240
            key += QLatin1String(me.key(i));
 
241
 
 
242
            e.items.insert(key, me.keyToValue(key.toUtf8()));
 
243
        }
 
244
 
 
245
        qVariantSetValue(v, e);
 
246
    }
 
247
 
 
248
    return v;
 
249
}
 
250
 
 
251
QVariant QDesignerPropertySheet::resolvePropertyValue(const QVariant &value) const
 
252
{
 
253
    QVariant v;
 
254
    EnumType e;
 
255
    FlagType f;
 
256
 
 
257
    if (qVariantCanConvert<FlagType>(value))
 
258
        v = qvariant_cast<FlagType>(value).value;
 
259
    else if (qVariantCanConvert<EnumType>(value))
 
260
        v = qvariant_cast<EnumType>(value).value;
 
261
    else
 
262
        v = value;
 
263
 
 
264
    return v;
 
265
}
 
266
 
 
267
void QDesignerPropertySheet::setFakeProperty(int index, const QVariant &value)
 
268
{
 
269
    Q_ASSERT(isFakeProperty(index));
 
270
 
 
271
    QVariant &v = m_fakeProperties[index];
 
272
 
 
273
    if (qVariantCanConvert<FlagType>(value) || qVariantCanConvert<EnumType>(value)) {
 
274
        v = value;
 
275
    } else if (qVariantCanConvert<FlagType>(v)) {
 
276
        FlagType f = qvariant_cast<FlagType>(v);
 
277
        f.value = value;
 
278
        qVariantSetValue(v, f);
 
279
        Q_ASSERT(f.value.type() == QVariant::Int);
 
280
    } else if (qVariantCanConvert<EnumType>(v)) {
 
281
        EnumType e = qvariant_cast<EnumType>(v);
 
282
        e.value = value;
 
283
        qVariantSetValue(v, e);
 
284
        Q_ASSERT(e.value.type() == QVariant::Int);
 
285
    } else {
 
286
        v = value;
 
287
    }
 
288
}
 
289
 
 
290
void QDesignerPropertySheet::setProperty(int index, const QVariant &value)
 
291
{
 
292
    if (isAdditionalProperty(index)) {
 
293
        m_addProperties[index] = value;
 
294
    } else if (isFakeProperty(index)) {
 
295
        setFakeProperty(index, value);
 
296
    } else {
 
297
        QMetaProperty p = meta->property(index);
 
298
        p.write(m_object, resolvePropertyValue(value));
 
299
    }
 
300
}
 
301
 
 
302
bool QDesignerPropertySheet::hasReset(int index) const
 
303
{
 
304
    if (isAdditionalProperty(index))
 
305
        return m_info.value(index).reset;
 
306
 
 
307
    return true;
 
308
}
 
309
 
 
310
bool QDesignerPropertySheet::reset(int index)
 
311
{
 
312
    if (isAdditionalProperty(index))
 
313
        return false;
 
314
    else if (isFakeProperty(index)) {
 
315
        QMetaProperty p = meta->property(index);
 
316
        bool result = p.reset(m_object);
 
317
        m_fakeProperties[index] = p.read(m_object);
 
318
        return result;
 
319
    }
 
320
 
 
321
    // ### TODO: reset for fake properties.
 
322
 
 
323
    QMetaProperty p = meta->property(index);
 
324
    return p.reset(m_object);
 
325
}
 
326
 
 
327
bool QDesignerPropertySheet::isChanged(int index) const
 
328
{
 
329
    return m_info.value(index).changed;
 
330
}
 
331
 
 
332
void QDesignerPropertySheet::setChanged(int index, bool changed)
 
333
{
 
334
    if (!m_info.contains(index))
 
335
        m_info.insert(index, Info());
 
336
 
 
337
    m_info[index].changed = changed;
 
338
}
 
339
 
 
340
bool QDesignerPropertySheet::isVisible(int index) const
 
341
{
 
342
    if (isAdditionalProperty(index))
 
343
        return m_info.value(index).visible;
 
344
 
 
345
    if (isFakeProperty(index))
 
346
        return true;
 
347
 
 
348
    QMetaProperty p = meta->property(index);
 
349
    return p.isWritable() && m_info.value(index).visible;
 
350
}
 
351
 
 
352
void QDesignerPropertySheet::setVisible(int index, bool visible)
 
353
{
 
354
    if (!m_info.contains(index))
 
355
        m_info.insert(index, Info());
 
356
 
 
357
    m_info[index].visible = visible;
 
358
}
 
359
 
 
360
bool QDesignerPropertySheet::isAttribute(int index) const
 
361
{
 
362
    if (isAdditionalProperty(index))
 
363
        return m_info.value(index).attribute;
 
364
 
 
365
    if (isFakeProperty(index))
 
366
        return false;
 
367
 
 
368
    return m_info.value(index).attribute;
 
369
}
 
370
 
 
371
void QDesignerPropertySheet::setAttribute(int index, bool attribute)
 
372
{
 
373
    if (!m_info.contains(index))
 
374
        m_info.insert(index, Info());
 
375
 
 
376
    m_info[index].attribute = attribute;
 
377
}
 
378
 
 
379
 
 
380
QDesignerPropertySheetFactory::QDesignerPropertySheetFactory(QExtensionManager *parent)
 
381
    : QExtensionFactory(parent)
 
382
{
 
383
}
 
384
 
 
385
QObject *QDesignerPropertySheetFactory::createExtension(QObject *object, const QString &iid, QObject *parent) const
 
386
{
 
387
    if (iid == Q_TYPEID(QDesignerPropertySheetExtension))
 
388
        return new QDesignerPropertySheet(object, parent);
 
389
 
 
390
    return 0;
 
391
}