propertyeditor.cpp 34.9 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
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
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "propertyeditor.h"

#include <nodemetainfo.h>
33
#include <metainfo.h>
34
35
36
37

#include <propertymetainfo.h>

#include <invalididexception.h>
38
#include <rewritingexception.h>
39
40
#include <invalidnodestateexception.h>
#include <variantproperty.h>
41
#include <propertymetainfo.h>
42

43
44
45
#include <bindingproperty.h>

#include <nodeabstractproperty.h>
46
#include <rewriterview.h>
47

48
#include "propertyeditorvalue.h"
49
50
51
52
53
54
55
56
#include "basiclayouts.h"
#include "basicwidgets.h"
#include "resetwidget.h"
#include "qlayoutobject.h"
#include "colorwidget.h"
#include "behaviordialog.h"
#include "qproxylayoutitem.h"
#include "fontwidget.h"
Thomas Hartmann's avatar
Thomas Hartmann committed
57
#include "siblingcombobox.h"
58
#include "propertyeditortransaction.h"
59
#include "originwidget.h"
60
61
62
63
64
65

#include <QtCore/QCoreApplication>
#include <QtCore/QDir>
#include <QtCore/QFileSystemWatcher>
#include <QtCore/QFileInfo>
#include <QtCore/QDebug>
66
67
#include <QtDeclarative/QDeclarativeView>
#include <QtDeclarative/QDeclarativeContext>
68
69
70
#include <QtGui/QVBoxLayout>
#include <QtGui/QShortcut>
#include <QtGui/QStackedWidget>
71
72
#include <QDeclarativeEngine>
#include <private/qdeclarativemetatype_p.h>
73
#include <QMessageBox>
74
#include <QApplication>
75
76
#include <QGraphicsOpacityEffect>
#include <QToolBar>
77
78
79
80
81

enum {
    debug = false
};

82
83
const int collapseButtonOffset = 114;

84
85
namespace QmlDesigner {

86
PropertyEditor::NodeType::NodeType(PropertyEditor *propertyEditor) :
87
        m_view(new DeclarativeWidgetView), m_propertyEditorTransaction(new PropertyEditorTransaction(propertyEditor)), m_dummyPropertyEditorValue(new PropertyEditorValue()),
88
        m_contextObject(new PropertyEditorContextObject())
89
90
91
{
    Q_ASSERT(QFileInfo(":/images/button_normal.png").exists());

92
    QDeclarativeContext *ctxt = m_view->rootContext();
93
    m_view->engine()->setOutputWarningsToStandardError(debug);
94
95
    m_dummyPropertyEditorValue->setValue("#000000");
    ctxt->setContextProperty("dummyBackendValue", m_dummyPropertyEditorValue.data());
96
97
    m_contextObject->setBackendValues(&m_backendValuesPropertyMap);
    ctxt->setContextObject(m_contextObject.data());
98

99
    connect(&m_backendValuesPropertyMap, SIGNAL(valueChanged(const QString&, const QVariant&)), propertyEditor, SLOT(changeValue(const QString&)));
100
101
102
103
104
105
}

PropertyEditor::NodeType::~NodeType()
{
}

106
void setupPropertyEditorValue(const QString &name, QDeclarativePropertyMap *propertyMap, PropertyEditor *propertyEditor, const QString &type)
107
108
109
{
    QString propertyName(name);
    propertyName.replace(QLatin1Char('.'), QLatin1Char('_'));
110
    PropertyEditorValue *valueObject = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(propertyMap->value(propertyName)));
111
112
    if (!valueObject) {
        valueObject = new PropertyEditorValue(propertyMap);
113
        QObject::connect(valueObject, SIGNAL(valueChanged(QString, const QVariant&)), propertyMap, SIGNAL(valueChanged(QString, const QVariant&)));
114
115
116
117
        QObject::connect(valueObject, SIGNAL(expressionChanged(QString)), propertyEditor, SLOT(changeExpression(QString)));
        propertyMap->insert(propertyName, QVariant::fromValue(valueObject));
    }
    valueObject->setName(propertyName);
118
119
120
121
122
    if (type == "QColor")
        valueObject->setValue(QVariant("#000000"));
    else
        valueObject->setValue(QVariant(1));

123
124
}

125
void createPropertyEditorValue(const QmlObjectNode &fxObjectNode, const QString &name, const QVariant &value, QDeclarativePropertyMap *propertyMap, PropertyEditor *propertyEditor)
126
127
{
    QString propertyName(name);
128
    propertyName.replace(QLatin1Char('.'), QLatin1Char('_'));
129
    PropertyEditorValue *valueObject = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(propertyMap->value(propertyName)));
130
131
    if (!valueObject) {
        valueObject = new PropertyEditorValue(propertyMap);
132
        QObject::connect(valueObject, SIGNAL(valueChanged(QString, const QVariant&)), propertyMap, SIGNAL(valueChanged(QString, const QVariant&)));
133
        QObject::connect(valueObject, SIGNAL(expressionChanged(QString)), propertyEditor, SLOT(changeExpression(QString)));
134
        propertyMap->insert(propertyName, QVariant::fromValue(valueObject));
135
    }
136
    valueObject->setName(name);
137
    valueObject->setModelNode(fxObjectNode);
138

139
    if (fxObjectNode.propertyAffectedByCurrentState(name) && !(fxObjectNode.modelNode().property(name).isBindingProperty())) {
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
        valueObject->setValue(fxObjectNode.modelValue(name));

    } else {
        valueObject->setValue(value);
    }

    if (propertyName != QLatin1String("id") &&
        fxObjectNode.currentState().isBaseState() &&
        fxObjectNode.modelNode().property(propertyName).isBindingProperty()) {
        valueObject->setExpression(fxObjectNode.modelNode().bindingProperty(propertyName).expression());
    } else {
        valueObject->setExpression(fxObjectNode.instanceValue(name).toString());
    }
}

155
void PropertyEditor::NodeType::setValue(const QmlObjectNode & fxObjectNode, const QString &name, const QVariant &value)
156
{
157
158
    QString propertyName = name;
    propertyName.replace(QLatin1Char('.'), QLatin1Char('_'));
159
    PropertyEditorValue *propertyValue = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(m_backendValuesPropertyMap.value(propertyName)));
160
    if (propertyValue) {
161
        propertyValue->setValue(value);
162
163
        if (!fxObjectNode.hasBindingProperty(name))
            propertyValue->setExpression(value.toString());
164
165
        else
            propertyValue->setExpression(fxObjectNode.expression(name));
166
    }
167
168
}

169
void PropertyEditor::NodeType::setup(const QmlObjectNode &fxObjectNode, const QString &stateName, const QUrl &qmlSpecificsFile, PropertyEditor *propertyEditor)
170
{
171
    if (!fxObjectNode.isValid()) {
172
        return;
173
    }
174

175
    QDeclarativeContext *ctxt = m_view->rootContext();
176
177

    if (fxObjectNode.isValid()) {
Marco Bubke's avatar
Marco Bubke committed
178
        foreach (const QString &propertyName, fxObjectNode.modelNode().metaInfo().propertyNames())
179
            createPropertyEditorValue(fxObjectNode, propertyName, fxObjectNode.instanceValue(propertyName), &m_backendValuesPropertyMap, propertyEditor);
180
181

        // className
182
        PropertyEditorValue *valueObject = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(m_backendValuesPropertyMap.value("className")));
183
184
        if (!valueObject)
            valueObject = new PropertyEditorValue(&m_backendValuesPropertyMap);
185
186
187
        valueObject->setName("className");
        valueObject->setModelNode(fxObjectNode.modelNode());
        valueObject->setValue(fxObjectNode.modelNode().simplifiedTypeName());
188
        QObject::connect(valueObject, SIGNAL(valueChanged(QString, const QVariant&)), &m_backendValuesPropertyMap, SIGNAL(valueChanged(QString, const QVariant&)));
189
        m_backendValuesPropertyMap.insert("className", QVariant::fromValue(valueObject));
190
191

        // id
192
        valueObject = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(m_backendValuesPropertyMap.value("id")));
193
194
        if (!valueObject)
            valueObject = new PropertyEditorValue(&m_backendValuesPropertyMap);
195
        valueObject->setName("id");
196
        valueObject->setValue(fxObjectNode.id());
197
        QObject::connect(valueObject, SIGNAL(valueChanged(QString, const QVariant&)), &m_backendValuesPropertyMap, SIGNAL(valueChanged(QString, const QVariant&)));
198
        m_backendValuesPropertyMap.insert("id", QVariant::fromValue(valueObject));
199
200
201
202
203

        // anchors
        m_backendAnchorBinding.setup(QmlItemNode(fxObjectNode.modelNode()));

        ctxt->setContextProperty("anchorBackend", &m_backendAnchorBinding);
204
        
205
        ctxt->setContextProperty("transaction", m_propertyEditorTransaction.data());
206
207
208
209
        
        m_contextObject->setSpecificsUrl(qmlSpecificsFile);
        
        m_contextObject->setStateName(stateName);
210
        QApplication::processEvents();
211
212
        if (!fxObjectNode.isValid())
            return;
213
        ctxt->setContextProperty("propertyCount", QVariant(fxObjectNode.modelNode().properties().count()));
214
215
216
217
218

        m_contextObject->setIsBaseState(fxObjectNode.isInBaseState());
        m_contextObject->setSelectionChanged(false);

        m_contextObject->setSelectionChanged(false);
219
220
221
222
223
    } else {
        qWarning() << "PropertyEditor: invalid node for setup";
    }
}

224
225
void PropertyEditor::NodeType::initialSetup(const QString &typeName, const QUrl &qmlSpecificsFile, PropertyEditor *propertyEditor)
{
226
    QDeclarativeContext *ctxt = m_view->rootContext();
227

228
    NodeMetaInfo metaInfo = propertyEditor->model()->metaInfo().nodeMetaInfo(typeName, 4, 7);
229

Marco Bubke's avatar
Marco Bubke committed
230
231
    foreach (const QString &propertyName, metaInfo.propertyNames())
        setupPropertyEditorValue(propertyName, &m_backendValuesPropertyMap, propertyEditor, metaInfo.propertyType(propertyName));
232

233
    PropertyEditorValue *valueObject = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(m_backendValuesPropertyMap.value("className")));
234
235
236
237
238
    if (!valueObject)
        valueObject = new PropertyEditorValue(&m_backendValuesPropertyMap);
    valueObject->setName("className");

    valueObject->setValue(typeName);
239
    QObject::connect(valueObject, SIGNAL(valueChanged(QString, const QVariant&)), &m_backendValuesPropertyMap, SIGNAL(valueChanged(QString, const QVariant&)));
240
241
242
    m_backendValuesPropertyMap.insert("className", QVariant::fromValue(valueObject));

    // id
243
    valueObject = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(m_backendValuesPropertyMap.value("id")));
244
245
246
247
    if (!valueObject)
        valueObject = new PropertyEditorValue(&m_backendValuesPropertyMap);
    valueObject->setName("id");
    valueObject->setValue("id");
248
    QObject::connect(valueObject, SIGNAL(valueChanged(QString, const QVariant&)), &m_backendValuesPropertyMap, SIGNAL(valueChanged(QString, const QVariant&)));
249
250
251
    m_backendValuesPropertyMap.insert("id", QVariant::fromValue(valueObject));

    ctxt->setContextProperty("anchorBackend", &m_backendAnchorBinding);
252
    ctxt->setContextProperty("transaction", m_propertyEditorTransaction.data());
253

254
255
256
257
258
259
260
261
262
    m_contextObject->setSpecificsUrl(qmlSpecificsFile);

    m_contextObject->setStateName(QLatin1String("basestate"));

    m_contextObject->setIsBaseState(true);

    m_contextObject->setSpecificQmlData(QLatin1String(""));

    m_contextObject->setGlobalBaseUrl(QUrl());
263
264
}

265
PropertyEditor::PropertyEditor(QWidget *parent) :
266
267
268
269
        QmlModelView(parent),
        m_parent(parent),
        m_updateShortcut(0),
        m_timerId(0),
270
        m_stackedWidget(new StackedWidget(parent)),
271
272
        m_currentType(0),
        m_locked(false)
273
274
275
276
277
278
279
280
{
    m_updateShortcut = new QShortcut(QKeySequence("F5"), m_stackedWidget);
    connect(m_updateShortcut, SIGNAL(activated()), this, SLOT(reloadQml()));

    QFile file(":/qmldesigner/stylesheet.css");
    file.open(QFile::ReadOnly);
    QString styleSheet = QLatin1String(file.readAll());
    m_stackedWidget->setStyleSheet(styleSheet);
281
    m_stackedWidget->setMinimumWidth(300);
282
    connect(m_stackedWidget, SIGNAL(resized()), this, SLOT(updateSize()));
283
284
285

    m_stackedWidget->insertWidget(0, new QWidget(m_stackedWidget));

286
287
288
289
290
291
292
293
294
295
296
297
298

    static bool declarativeTypesRegistered = false;
    if (!declarativeTypesRegistered) {
        declarativeTypesRegistered = true;
        BasicWidgets::registerDeclarativeTypes();
        BasicLayouts::registerDeclarativeTypes();
        ResetWidget::registerDeclarativeType();
        QLayoutObject::registerDeclarativeType();
        ColorWidget::registerDeclarativeTypes();
        BehaviorDialog::registerDeclarativeType();
        QProxyLayoutItem::registerDeclarativeTypes();
        PropertyEditorValue::registerDeclarativeTypes();
        FontWidget::registerDeclarativeTypes();
Thomas Hartmann's avatar
Thomas Hartmann committed
299
        SiblingComboBox::registerDeclarativeTypes();
300
        OriginWidget::registerDeclarativeType();
301
    }
302
303
304
305
306
307
308
309
}

PropertyEditor::~PropertyEditor()
{
    delete m_stackedWidget;
    qDeleteAll(m_typeHash);
}

310
311
312
void PropertyEditor::setupPane(const QString &typeName)
{

313
    QUrl qmlFile = fileToUrl(locateQmlFile(QLatin1String("Qt/ItemPane.qml")));
314
315
316
    QUrl qmlSpecificsFile;

    qmlSpecificsFile = fileToUrl(locateQmlFile(typeName + "Specifics.qml"));
317
    NodeType *type = m_typeHash.value(qmlFile.toString());
318

319
    if (!type) {
320
        type = new NodeType(this);
321

322
        QDeclarativeContext *ctxt = type->m_view->rootContext();
323
324
        ctxt->setContextProperty("finishedNotify", QVariant(false) );
        type->initialSetup(typeName, qmlSpecificsFile, this);
325
        type->m_view->setSource(qmlFile);
326
        ctxt->setContextProperty("finishedNotify", QVariant(true) );
327

328
329
330
        m_stackedWidget->addWidget(type->m_view);
        m_typeHash.insert(qmlFile.toString(), type);
    } else {
331
        QDeclarativeContext *ctxt = type->m_view->rootContext();
332
        ctxt->setContextProperty("finishedNotify", QVariant(false) );
Thomas Hartmann's avatar
Thomas Hartmann committed
333

334
        type->initialSetup(typeName, qmlSpecificsFile, this);
335
336
        ctxt->setContextProperty("finishedNotify", QVariant(true) );
    }
337
    m_stackedWidget->setCurrentWidget(type->m_view);
338
339
}

340
void PropertyEditor::changeValue(const QString &propertyName)
341
{
342
    if (propertyName.isNull())
343
        return;
344
345
346
347

    if (m_locked)
        return;

348
    if (propertyName == "type")
349
350
        return;

351
352
353
    if (!m_selectedNode.isValid())
        return;

354
    if (propertyName == "id") {
355
        PropertyEditorValue *value = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(m_currentType->m_backendValuesPropertyMap.value(propertyName)));
356
357
        const QString newId = value->value().toString();

358
        if (m_selectedNode.isValidId(newId)) {
359
360
361
362
363
364
365
366
367
368
            if (m_selectedNode.id().isEmpty() || newId.isEmpty()) { //no id
                try {
                    m_selectedNode.setId(newId);
                } catch (InvalidIdException &e) { //better save then sorry
                    QMessageBox::warning(0, tr("Invalid Id"), e.description());
                }
            } else { //there is already an id, so we refactor
                if (rewriterView())
                    rewriterView()->renameId(m_selectedNode.id(), newId);
            }
369
        } else {
370
            value->setValue(m_selectedNode.id());
Friedemann Kleint's avatar
Friedemann Kleint committed
371
            QMessageBox::warning(0, tr("Invalid Id"),  tr("%1 is an invalid id").arg(newId));
372
373
374
375
        }
        return;
    }

376
377
378
    //.replace(QLatin1Char('.'), QLatin1Char('_'))
    QString underscoreName(propertyName);
    underscoreName.replace(QLatin1Char('.'), QLatin1Char('_'));
379
    PropertyEditorValue *value = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(m_currentType->m_backendValuesPropertyMap.value(underscoreName)));
380
381
382
383
384
385
386
387
388

    if (value ==0) {
        return;
    }

    QmlObjectNode fxObjectNode(m_selectedNode);

    QVariant castedValue;

Marco Bubke's avatar
Marco Bubke committed
389
390
    if (fxObjectNode.modelNode().metaInfo().isValid() && fxObjectNode.modelNode().metaInfo().hasProperty(propertyName)) {
        castedValue = fxObjectNode.modelNode().metaInfo().nativePropertyValue(propertyName, value->value());
391
    } else {
392
        qWarning() << "PropertyEditor:" <<propertyName << "cannot be casted (metainfo)";
393
394
395
396
        return ;
    }

    if (value->value().isValid() && !castedValue.isValid()) {
397
        qWarning() << "PropertyEditor:" << propertyName << "not properly casted (metainfo)";
398
399
400
        return ;
    }

Marco Bubke's avatar
Marco Bubke committed
401
402
    if (fxObjectNode.modelNode().metaInfo().isValid() && fxObjectNode.modelNode().metaInfo().hasProperty(propertyName))
        if (fxObjectNode.modelNode().metaInfo().propertyType(propertyName) == QLatin1String("QUrl")) { //turn absolute local file paths into relative paths
403
404
405
406
407
408
409
        QString filePath = castedValue.toUrl().toString();
        if (QFileInfo(filePath).exists() && QFileInfo(filePath).isAbsolute()) {
            QDir fileDir(QFileInfo(model()->fileUrl().toLocalFile()).absolutePath());
            castedValue = QUrl(fileDir.relativeFilePath(filePath));
        }
    }

410
411
412
413
414
415
416
        if (castedValue.type() == QVariant::Color) {
            QColor color = castedValue.value<QColor>();
            QColor newColor = QColor(color.name());
            newColor.setAlpha(color.alpha());
            castedValue = QVariant(newColor);
        }

417
418
419
420
        try {
            if (!value->value().isValid()) { //reset
                fxObjectNode.removeVariantProperty(propertyName);
            } else {
421
                if (castedValue.isValid() && !castedValue.isNull()) {
422
                    m_locked = true;
423
424
425
                    fxObjectNode.setVariantProperty(propertyName, castedValue);
                    m_locked = false;
                }
426
427
428
429
            }
        }
        catch (RewritingException &e) {
            QMessageBox::warning(0, "Error", e.description());
430
431
432
433
434
        }
}

void PropertyEditor::changeExpression(const QString &name)
{
435
436
437
438
439
440
    if (name.isNull())
        return;

    if (m_locked)
        return;

441
442
    RewriterTransaction transaction = beginRewriterTransaction();

443
444
445
    QString underscoreName(name);
    underscoreName.replace(QLatin1Char('.'), QLatin1Char('_'));

446
    QmlObjectNode fxObjectNode(m_selectedNode);
447
448
    PropertyEditorValue *value = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(m_currentType->m_backendValuesPropertyMap.value(underscoreName)));

Marco Bubke's avatar
Marco Bubke committed
449
450
    if (fxObjectNode.modelNode().metaInfo().isValid() && fxObjectNode.modelNode().metaInfo().hasProperty(name)) {
        if (fxObjectNode.modelNode().metaInfo().propertyType(name) == QLatin1String("QColor")) {
451
452
453
454
            if (QColor(value->expression().remove('"')).isValid()) {
                fxObjectNode.setVariantProperty(name, QColor(value->expression().remove('"')));
                return;
            }
Marco Bubke's avatar
Marco Bubke committed
455
        } else if (fxObjectNode.modelNode().metaInfo().propertyType(name) == QLatin1String("bool")) {
456
457
458
459
460
461
462
            if (value->expression().compare("false", Qt::CaseInsensitive) == 0 || value->expression().compare("true", Qt::CaseInsensitive) == 0) {
                if (value->expression().compare("true", Qt::CaseInsensitive) == 0)
                    fxObjectNode.setVariantProperty(name, true);
                else
                    fxObjectNode.setVariantProperty(name, false);
                return;
            }
Marco Bubke's avatar
Marco Bubke committed
463
        } else if (fxObjectNode.modelNode().metaInfo().propertyType(name) == QLatin1String("int")) {
464
465
466
467
468
469
            bool ok;
            int intValue = value->expression().toInt(&ok);
            if (ok) {
                fxObjectNode.setVariantProperty(name, intValue);
                return;
            }
Marco Bubke's avatar
Marco Bubke committed
470
        } else if (fxObjectNode.modelNode().metaInfo().propertyType(name) == QLatin1String("qreal")) {
471
472
473
474
475
476
            bool ok;
            qreal realValue = value->expression().toFloat(&ok);
            if (ok) {
                fxObjectNode.setVariantProperty(name, realValue);
                return;
            }
477
        }
Kai Koehne's avatar
Kai Koehne committed
478
    }
479

480
481
482
483
484
    if (!value) {
        qWarning() << "PropertyEditor::changeExpression no value for " << underscoreName;
        return;
    }

485
    try {
486
487
        if (fxObjectNode.expression(name) != value->expression() || !fxObjectNode.propertyAffectedByCurrentState(name))
            fxObjectNode.setBindingProperty(name, value->expression());
488
489
    }

490
    catch (RewritingException &e) {
491
        QMessageBox::warning(0, "Error", e.description());
492
493
494
    }
}

495
void PropertyEditor::updateSize()
496
{
497
498
499
500
501
    if (!m_currentType)
        return;
    QWidget* frame = m_currentType->m_view->findChild<QWidget*>("propertyEditorFrame");
    if (frame)
        frame->resize(m_stackedWidget->size());
502
503
}

504
void PropertyEditor::otherPropertyChanged(const QmlObjectNode &fxObjectNode, const QString &propertyName)
505
{
506
507
    QmlModelView::otherPropertyChanged(fxObjectNode, propertyName);

508
    if (fxObjectNode.isValid() && m_currentType && fxObjectNode == m_selectedNode && fxObjectNode.currentState().isValid()) {
509
510
511
        AbstractProperty property = fxObjectNode.modelNode().property(propertyName);
        if (fxObjectNode == m_selectedNode || QmlObjectNode(m_selectedNode).propertyChangeForCurrentState() == fxObjectNode) {
            if ( m_selectedNode.property(property.name()).isBindingProperty() || !m_selectedNode.hasProperty(propertyName))
512
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).instanceValue(property.name()));
513
            else
514
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).modelValue(property.name()));
515
516
517
518
        }
    }
}

519
void PropertyEditor::transformChanged(const QmlObjectNode &fxObjectNode, const QString &propertyName)
520
{
521
522
    QmlModelView::transformChanged(fxObjectNode, propertyName);

523
    if (fxObjectNode.isValid() && m_currentType && fxObjectNode == m_selectedNode && fxObjectNode.currentState().isValid()) {
524
525
526
        AbstractProperty property = fxObjectNode.modelNode().property(propertyName);
        if (fxObjectNode == m_selectedNode || QmlObjectNode(m_selectedNode).propertyChangeForCurrentState() == fxObjectNode) {
            if ( m_selectedNode.property(property.name()).isBindingProperty() || !m_selectedNode.hasProperty(propertyName))
527
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).instanceValue(property.name()));
528
            else
529
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).modelValue(property.name()));
530
531
532
533
        }
    }
}

534
535
536
537
538
539
540
541
542
543
544
545
void PropertyEditor::setQmlDir(const QString &qmlDir)
{
    m_qmlDir = qmlDir;

    QFileSystemWatcher *watcher = new QFileSystemWatcher(this);
    watcher->addPath(m_qmlDir);
    connect(watcher, SIGNAL(directoryChanged(QString)), this, SLOT(reloadQml()));
}

void PropertyEditor::delayedResetView()
{
    if (m_timerId == 0)
546
        m_timerId = startTimer(200);
547
548
549
550
551
552
553
554
555
}

void PropertyEditor::timerEvent(QTimerEvent *timerEvent)
{
    if (m_timerId == timerEvent->timerId()) {
        resetView();
    }
}

556
QString templateGeneration(NodeMetaInfo type, NodeMetaInfo superType, const QmlObjectNode &objectNode)
557
{
558
    QString qmlTemplate = QLatin1String("import Qt 4.7\nimport Bauhaus 1.0\n");
559
560
561
562
    qmlTemplate += QLatin1String("GroupBox {\n");
    qmlTemplate += QString(QLatin1String("caption: \"%1\"\n")).arg(type.typeName());
    qmlTemplate += QLatin1String("layout: VerticalLayout {\n");

563
    QList<QString> orderedList;
Marco Bubke's avatar
Marco Bubke committed
564
    orderedList = type.propertyNames();
565
566
567
    qSort(orderedList);

    foreach (const QString &name, orderedList) {
568
569
        QString properName = name;
        properName.replace(".", "_");
570

Marco Bubke's avatar
Marco Bubke committed
571
        QString typeName = type.propertyType(name);
572
573
574
575
        //alias resolution only possible with instance
            if (typeName == QLatin1String("alias") && objectNode.isValid())
                typeName = objectNode.instanceType(name);

Marco Bubke's avatar
Marco Bubke committed
576
        if (!superType.hasProperty(name)) {
577
            if (typeName == "int") {
578
                qmlTemplate +=  QString(QLatin1String(
579
580
                "IntEditor { backendValue: backendValues.%2\n caption: \"%1\"\nbaseStateFlag: isBaseState\nslider: false\n}"
                )).arg(name).arg(properName);
581
            }
582
            if (typeName == "real" || typeName == "double" || typeName == "qreal") {
583
                qmlTemplate +=  QString(QLatin1String(
584
585
                "DoubleSpinBoxAlternate {\ntext: \"%1\"\nbackendValue: backendValues.%2\nbaseStateFlag: isBaseState\n}\n"
                )).arg(name).arg(properName);
586
            }
587
            if (typeName == "string" || typeName == "QString" || typeName == "QUrl" || typeName == "url") {
588
                 qmlTemplate +=  QString(QLatin1String(
589
590
                "QWidget {\nlayout: HorizontalLayout {\nLabel {\ntext: \"%1\"\ntoolTip: \"%1\"\n}\nLineEdit {\nbackendValue: backendValues.%2\nbaseStateFlag: isBaseState\n}\n}\n}\n"
                )).arg(name).arg(properName);
591
            }
592
            if (typeName == "bool") {
593
                 qmlTemplate +=  QString(QLatin1String(
594
595
                 "QWidget {\nlayout: HorizontalLayout {\nLabel {\ntext: \"%1\"\ntoolTip: \"%1\"\n}\nCheckBox {text: backendValues.%2.value\nbackendValue: backendValues.%2\nbaseStateFlag: isBaseState\ncheckable: true\n}\n}\n}\n"
                 )).arg(name).arg(properName);
596
            }
597
            if (typeName == "color" || typeName == "QColor") {
598
                qmlTemplate +=  QString(QLatin1String(
599
600
                "ColorGroupBox {\ncaption: \"%1\"\nfinished: finishedNotify\nbackendColor: backendValues.%2\n}\n\n"
                )).arg(name).arg(properName);
601
            }
602
603
604
605
606
607
608
609
        }
    }
    qmlTemplate += QLatin1String("}\n"); //VerticalLayout
    qmlTemplate += QLatin1String("}\n"); //GroupBox

    return qmlTemplate;
}

610
611
612
613
614
void PropertyEditor::resetView()
{
    if (model() == 0)
        return;

Thomas Hartmann's avatar
Thomas Hartmann committed
615
616
    m_locked = true;

617
618
619
620
621
622
623
624
625
    if (debug)
        qDebug() << "________________ RELOADING PROPERTY EDITOR QML _______________________";

    if (m_timerId)
        killTimer(m_timerId);

    if (m_selectedNode.isValid() && model() != m_selectedNode.model())
        m_selectedNode = ModelNode();

626
627
    QString specificsClassName;
    QUrl qmlFile(qmlForNode(m_selectedNode, specificsClassName));
628
629
630
631
    QUrl qmlSpecificsFile;
    if (m_selectedNode.isValid())
        qmlSpecificsFile = fileToUrl(locateQmlFile(m_selectedNode.type() + "Specifics.qml"));

632
633
    QString specificQmlData;

634
    if (m_selectedNode.isValid() && !QFileInfo(qmlSpecificsFile.toLocalFile()).exists() && m_selectedNode.metaInfo().isValid()) {
635
        //do magic !!
636
        specificQmlData = templateGeneration(m_selectedNode.metaInfo(), model()->metaInfo().nodeMetaInfo(specificsClassName), m_selectedNode);
637
638
    }

639
    NodeType *type = m_typeHash.value(qmlFile.toString());
640

641
    if (!type) {
642
        type = new NodeType(this);
643
644

        m_stackedWidget->addWidget(type->m_view);
645
        m_typeHash.insert(qmlFile.toString(), type);
646
647
648
649
650
651

        QmlObjectNode fxObjectNode;
        if (m_selectedNode.isValid()) {
            fxObjectNode = QmlObjectNode(m_selectedNode);
            Q_ASSERT(fxObjectNode.isValid());
        }
652
        QDeclarativeContext *ctxt = type->m_view->rootContext();
653
        type->setup(fxObjectNode, currentState().name(), qmlSpecificsFile, this);
654
        ctxt->setContextProperty("finishedNotify", QVariant(false));
655
        if (specificQmlData.isEmpty())
656
657
658
659
            type->m_contextObject->setSpecificQmlData(specificQmlData);
            
        type->m_contextObject->setGlobalBaseUrl(qmlFile);
        type->m_contextObject->setSpecificQmlData(specificQmlData);
660
        type->m_view->setSource(qmlFile);
661
662
663
664
665
666
        ctxt->setContextProperty("finishedNotify", QVariant(true));
    } else {
        QmlObjectNode fxObjectNode;
        if (m_selectedNode.isValid()) {
            fxObjectNode = QmlObjectNode(m_selectedNode);
        }
Thomas Hartmann's avatar
Thomas Hartmann committed
667
        QDeclarativeContext *ctxt = type->m_view->rootContext();
668
669
        type->m_contextObject->triggerSelectionChanged();
        
670
        ctxt->setContextProperty("finishedNotify", QVariant(false));
671
        if (specificQmlData.isEmpty())
672
673
            type->m_contextObject->setSpecificQmlData(specificQmlData);
        
674
        type->setup(fxObjectNode, currentState().name(), qmlSpecificsFile, this);
675
676
        type->m_contextObject->setGlobalBaseUrl(qmlFile);
        type->m_contextObject->setSpecificQmlData(specificQmlData);
677
678
679
    }

    m_stackedWidget->setCurrentWidget(type->m_view);
680

681

Thomas Hartmann's avatar
Thomas Hartmann committed
682
    QDeclarativeContext *ctxt = type->m_view->rootContext();
683
    ctxt->setContextProperty("finishedNotify", QVariant(true));
684
    /*ctxt->setContextProperty("selectionChanged", QVariant(false));
685
    ctxt->setContextProperty("selectionChanged", QVariant(true));
686
687
    ctxt->setContextProperty("selectionChanged", QVariant(false));*/
    type->m_contextObject->triggerSelectionChanged();
688

689
690
    m_currentType = type;

691
692
    m_locked = false;

693
694
    if (m_timerId)
        m_timerId = 0;
695
696

    updateSize();
697
698
699
}

void PropertyEditor::selectedNodesChanged(const QList<ModelNode> &selectedNodeList,
700
                                          const QList<ModelNode> &lastSelectedNodeList)
701
702
703
{
    Q_UNUSED(lastSelectedNodeList);

704
    if (selectedNodeList.isEmpty() || selectedNodeList.count() > 1)
705
        select(ModelNode());
706
    else if (m_selectedNode != selectedNodeList.first())
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
        select(selectedNodeList.first());
}

void PropertyEditor::nodeAboutToBeRemoved(const ModelNode &removedNode)
{
    QmlModelView::nodeAboutToBeRemoved(removedNode);
    if (m_selectedNode.isValid() && removedNode.isValid() && m_selectedNode == removedNode)
        select(m_selectedNode.parentProperty().parentModelNode());
}

void PropertyEditor::modelAttached(Model *model)
{
    QmlModelView::modelAttached(model);

    if (debug)
        qDebug() << Q_FUNC_INFO;

Thomas Hartmann's avatar
Thomas Hartmann committed
724
725
    m_locked = true;

726
727
728
729
    setupPane("Qt/Rectangle");
    setupPane("Qt/Text");
    setupPane("Qt/TextInput");
    setupPane("Qt/TextEdit");
730
    resetView();
Thomas Hartmann's avatar
Thomas Hartmann committed
731
732

    m_locked = false;
733
734
735
736
737
}

void PropertyEditor::modelAboutToBeDetached(Model *model)
{
    QmlModelView::modelAboutToBeDetached(model);
738
    m_currentType->m_propertyEditorTransaction->end();
739
740
741
742
743

    resetView();
}


744
void PropertyEditor::propertiesRemoved(const QList<AbstractProperty>& propertyList)
745
{
746
    QmlModelView::propertiesRemoved(propertyList);
747
748
749
750
751

    if (!m_selectedNode.isValid())
        return;

    foreach (const AbstractProperty &property, propertyList) {
752
753
754
        ModelNode node(property.parentModelNode());
        if (node == m_selectedNode || QmlObjectNode(m_selectedNode).propertyChangeForCurrentState() == node) {
            setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).instanceValue(property.name()));
755
756
            if (property.name().contains("anchor", Qt::CaseInsensitive))
                m_currentType->m_backendAnchorBinding.invalidate(m_selectedNode);
757
758
759
760
761
762
763
        }
    }
}


void PropertyEditor::variantPropertiesChanged(const QList<VariantProperty>& propertyList, PropertyChangeFlags propertyChange)
{
764

765
766
767
768
769
770
771
772
773
774
    QmlModelView::variantPropertiesChanged(propertyList, propertyChange);

    if (!m_selectedNode.isValid())
        return;

    foreach (const VariantProperty &property, propertyList) {
        ModelNode node(property.parentModelNode());

        if (node == m_selectedNode || QmlObjectNode(m_selectedNode).propertyChangeForCurrentState() == node) {
            if ( QmlObjectNode(m_selectedNode).modelNode().property(property.name()).isBindingProperty())
775
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).instanceValue(property.name()));
776
            else
777
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).modelValue(property.name()));
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
        }
    }
}

void PropertyEditor::bindingPropertiesChanged(const QList<BindingProperty>& propertyList, PropertyChangeFlags propertyChange)
{
    QmlModelView::bindingPropertiesChanged(propertyList, propertyChange);

    if (!m_selectedNode.isValid())
        return;

    foreach (const BindingProperty &property, propertyList) {
        ModelNode node(property.parentModelNode());

        if (node == m_selectedNode || QmlObjectNode(m_selectedNode).propertyChangeForCurrentState() == node) {
793
794
            if (property.name().contains("anchor", Qt::CaseInsensitive))
                m_currentType->m_backendAnchorBinding.invalidate(m_selectedNode);
795
            if ( QmlObjectNode(m_selectedNode).modelNode().property(property.name()).isBindingProperty())
796
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).instanceValue(property.name()));
797
            else
798
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).modelValue(property.name()));
799
800
801
802
803
804
805
806
        }
    }
}

void PropertyEditor::nodeIdChanged(const ModelNode& node, const QString& newId, const QString& oldId)
{
    QmlModelView::nodeIdChanged(node, newId, oldId);

807
    if (!m_selectedNode.isValid())
808
809
        return;

810
    if (node == m_selectedNode) {
811

812
        if (m_currentType) {
813
            setValue(node, "id", newId);
814
815
        }
    }
816
817
}

Marco Bubke's avatar
Marco Bubke committed
818
819
820
821
822
void PropertyEditor::scriptFunctionsChanged(const ModelNode &node, const QStringList &scriptFunctionList)
{
    QmlModelView::scriptFunctionsChanged(node, scriptFunctionList);
}

823
824
void PropertyEditor::select(const ModelNode &node)
{
825
    if (QmlItemNode(node).isValid())
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
        m_selectedNode = node;
    else
        m_selectedNode = ModelNode();

    delayedResetView();
}

QWidget *PropertyEditor::createPropertiesPage()
{
    delayedResetView();
    return m_stackedWidget;
}

void PropertyEditor::stateChanged(const QmlModelState &newQmlModelState, const QmlModelState &oldQmlModelState)
{
    QmlModelView::stateChanged(newQmlModelState, oldQmlModelState);
    Q_ASSERT(newQmlModelState.isValid());
    if (debug)
        qDebug() << Q_FUNC_INFO << newQmlModelState.name();
    delayedResetView();
}

848
849
850
851
852
853
854
void PropertyEditor::setValue(const QmlObjectNode &fxObjectNode, const QString &name, const QVariant &value)
{
    m_locked = true;
    m_currentType->setValue(fxObjectNode, name, value);
    m_locked = false;
}

855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
void PropertyEditor::reloadQml()
{
    m_typeHash.clear();
    while (QWidget *widget = m_stackedWidget->widget(0)) {
        m_stackedWidget->removeWidget(widget);
        delete widget;
    }
    m_currentType = 0;

    delayedResetView();
}

QString PropertyEditor::qmlFileName(const NodeMetaInfo &nodeInfo) const
{
    return nodeInfo.typeName() + QLatin1String("Pane.qml");
}

QUrl PropertyEditor::fileToUrl(const QString &filePath) const {
    QUrl fileUrl;

    if (filePath.isEmpty())
        return fileUrl;

878
    if (filePath.startsWith(QLatin1Char(':'))) {
879
880
881
882
883
884
885
886
887
888
889
        fileUrl.setScheme("qrc");
        QString path = filePath;
        path.remove(0, 1); // remove trailing ':'
        fileUrl.setPath(path);
    } else {
        fileUrl = QUrl::fromLocalFile(filePath);
    }

    return fileUrl;
}

890
QUrl PropertyEditor::qmlForNode(const ModelNode &modelNode, QString &className) const
891
892
893
894
895
896
897
898
{
    if (modelNode.isValid()) {
        QList<NodeMetaInfo> hierarchy;
        hierarchy << modelNode.metaInfo();
        hierarchy << modelNode.metaInfo().superClasses();

        foreach (const NodeMetaInfo &info, hierarchy) {
            QUrl fileUrl = fileToUrl(locateQmlFile(qmlFileName(info)));
899
900
            if (fileUrl.isValid()) {
                className = info.typeName();
901
                return fileUrl;
902
            }
903
904
        }
    }
905
    return fileToUrl(QDir(m_qmlDir).filePath("Qt/emptyPane.qml"));
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
}

QString PropertyEditor::locateQmlFile(const QString &relativePath) const
{
    QDir fileSystemDir(m_qmlDir);
    static QDir resourcesDir(":/propertyeditor");

    if (fileSystemDir.exists(relativePath))
        return fileSystemDir.absoluteFilePath(relativePath);
    if (resourcesDir.exists(relativePath))
        return resourcesDir.absoluteFilePath(relativePath);

    return QString();
}

921

922
923
} //QmlDesigner