propertyeditor.cpp 35.2 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6
7
8
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
con's avatar
con committed
9
** No Commercial Usage
10
**
con's avatar
con committed
11
12
13
14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
15
16
17
18
19
20
21
22
23
24
**
** 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.
**
con's avatar
con committed
25
26
27
28
29
30
** 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.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
31
32
33
34
35
36
**
**************************************************************************/

#include "propertyeditor.h"

#include <nodemetainfo.h>
37
#include <metainfo.h>
38
39
40
41

#include <propertymetainfo.h>

#include <invalididexception.h>
42
#include <rewritingexception.h>
43
44
#include <invalidnodestateexception.h>
#include <variantproperty.h>
45
#include <propertymetainfo.h>
46

47
48
49
#include <bindingproperty.h>

#include <nodeabstractproperty.h>
50
#include <rewriterview.h>
51

52
#include "propertyeditorvalue.h"
53
54
55
56
57
58
59
60
#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
61
#include "siblingcombobox.h"
62
#include "propertyeditortransaction.h"
63
#include "originwidget.h"
64
65
66
67
68
69

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

enum {
    debug = false
};

86
87
const int collapseButtonOffset = 114;

88
89
namespace QmlDesigner {

90
PropertyEditor::NodeType::NodeType(PropertyEditor *propertyEditor) :
91
        m_view(new DeclarativeWidgetView), m_propertyEditorTransaction(new PropertyEditorTransaction(propertyEditor)), m_dummyPropertyEditorValue(new PropertyEditorValue()),
92
        m_contextObject(new PropertyEditorContextObject())
93
94
95
{
    Q_ASSERT(QFileInfo(":/images/button_normal.png").exists());

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

103
    connect(&m_backendValuesPropertyMap, SIGNAL(valueChanged(const QString&, const QVariant&)), propertyEditor, SLOT(changeValue(const QString&)));
104
105
106
107
108
109
}

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

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

127
128
}

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

143
    if (fxObjectNode.propertyAffectedByCurrentState(name) && !(fxObjectNode.modelNode().property(name).isBindingProperty())) {
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
        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());
    }
}

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

173
void PropertyEditor::NodeType::setup(const QmlObjectNode &fxObjectNode, const QString &stateName, const QUrl &qmlSpecificsFile, PropertyEditor *propertyEditor)
174
{
175
    if (!fxObjectNode.isValid()) {
176
        return;
177
    }
178

179
    QDeclarativeContext *ctxt = m_view->rootContext();
180
181
182

    if (fxObjectNode.isValid()) {
        foreach (const QString &propertyName, fxObjectNode.modelNode().metaInfo().properties(true).keys())
183
            createPropertyEditorValue(fxObjectNode, propertyName, fxObjectNode.instanceValue(propertyName), &m_backendValuesPropertyMap, propertyEditor);
184
185

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

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

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

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

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

        m_contextObject->setSelectionChanged(false);
223
224
225
226
227
    } else {
        qWarning() << "PropertyEditor: invalid node for setup";
    }
}

228
229
void PropertyEditor::NodeType::initialSetup(const QString &typeName, const QUrl &qmlSpecificsFile, PropertyEditor *propertyEditor)
{
230
    QDeclarativeContext *ctxt = m_view->rootContext();
231

232
    NodeMetaInfo metaInfo = propertyEditor->model()->metaInfo().nodeMetaInfo(typeName, 4, 7);
233
234

    foreach (const QString &propertyName, metaInfo.properties(true).keys())
235
        setupPropertyEditorValue(propertyName, &m_backendValuesPropertyMap, propertyEditor, metaInfo.property(propertyName, true).type());
236

237
    PropertyEditorValue *valueObject = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(m_backendValuesPropertyMap.value("className")));
238
239
240
241
242
    if (!valueObject)
        valueObject = new PropertyEditorValue(&m_backendValuesPropertyMap);
    valueObject->setName("className");

    valueObject->setValue(typeName);
243
    QObject::connect(valueObject, SIGNAL(valueChanged(QString, const QVariant&)), &m_backendValuesPropertyMap, SIGNAL(valueChanged(QString, const QVariant&)));
244
245
246
    m_backendValuesPropertyMap.insert("className", QVariant::fromValue(valueObject));

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

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

258
259
260
261
262
263
264
265
266
    m_contextObject->setSpecificsUrl(qmlSpecificsFile);

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

    m_contextObject->setIsBaseState(true);

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

    m_contextObject->setGlobalBaseUrl(QUrl());
267
268
}

269
PropertyEditor::PropertyEditor(QWidget *parent) :
270
271
272
273
        QmlModelView(parent),
        m_parent(parent),
        m_updateShortcut(0),
        m_timerId(0),
274
        m_stackedWidget(new StackedWidget(parent)),
275
276
        m_currentType(0),
        m_locked(false)
277
278
279
280
281
282
283
284
{
    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);
285
    m_stackedWidget->setMinimumWidth(300);
286
    connect(m_stackedWidget, SIGNAL(resized()), this, SLOT(updateSize()));
287
288
289

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

290
291
292
293
294
295
296
297
298
299
300
301
302

    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
303
        SiblingComboBox::registerDeclarativeTypes();
304
        OriginWidget::registerDeclarativeType();
305
    }
306
307
308
309
310
311
312
313
}

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

314
315
316
void PropertyEditor::setupPane(const QString &typeName)
{

317
    QUrl qmlFile = fileToUrl(locateQmlFile(QLatin1String("Qt/ItemPane.qml")));
318
319
320
    QUrl qmlSpecificsFile;

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

323
    if (!type) {
324
        type = new NodeType(this);
325

326
        QDeclarativeContext *ctxt = type->m_view->rootContext();
327
328
        ctxt->setContextProperty("finishedNotify", QVariant(false) );
        type->initialSetup(typeName, qmlSpecificsFile, this);
329
        type->m_view->setSource(qmlFile);
330
        ctxt->setContextProperty("finishedNotify", QVariant(true) );
331

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

338
        type->initialSetup(typeName, qmlSpecificsFile, this);
339
340
        ctxt->setContextProperty("finishedNotify", QVariant(true) );
    }
341
    m_stackedWidget->setCurrentWidget(type->m_view);
342
343
}

344
void PropertyEditor::changeValue(const QString &propertyName)
345
{
346
    if (propertyName.isNull())
347
        return;
348
349
350
351

    if (m_locked)
        return;

352
    if (propertyName == "type")
353
354
        return;

355
356
357
    if (!m_selectedNode.isValid())
        return;

358
    if (propertyName == "id") {
359
        PropertyEditorValue *value = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(m_currentType->m_backendValuesPropertyMap.value(propertyName)));
360
361
        const QString newId = value->value().toString();

362
        if (m_selectedNode.isValidId(newId)) {
363
364
365
366
367
368
369
370
371
372
            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);
            }
373
        } else {
374
            value->setValue(m_selectedNode.id());
Friedemann Kleint's avatar
Friedemann Kleint committed
375
            QMessageBox::warning(0, tr("Invalid Id"),  tr("%1 is an invalid id").arg(newId));
376
377
378
379
        }
        return;
    }

380
381
382
    //.replace(QLatin1Char('.'), QLatin1Char('_'))
    QString underscoreName(propertyName);
    underscoreName.replace(QLatin1Char('.'), QLatin1Char('_'));
383
    PropertyEditorValue *value = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(m_currentType->m_backendValuesPropertyMap.value(underscoreName)));
384
385
386
387
388
389
390
391
392
393
394
395

    if (value ==0) {
        return;
    }

    QmlObjectNode fxObjectNode(m_selectedNode);

    QVariant castedValue;

    if (fxObjectNode.modelNode().metaInfo().isValid() && fxObjectNode.modelNode().metaInfo().property(propertyName, true).isValid()) {
        castedValue = fxObjectNode.modelNode().metaInfo().property(propertyName, true).castedValue(value->value());
    } else {
396
        qWarning() << "PropertyEditor:" <<propertyName << "cannot be casted (metainfo)";
397
398
399
400
        return ;
    }

    if (value->value().isValid() && !castedValue.isValid()) {
401
        qWarning() << "PropertyEditor:" << propertyName << "not properly casted (metainfo)";
402
403
404
405
406
407
408
409
410
411
412
413
        return ;
    }

    if (fxObjectNode.modelNode().metaInfo().isValid() && fxObjectNode.modelNode().metaInfo().property(propertyName).isValid())
        if (fxObjectNode.modelNode().metaInfo().property(propertyName).type() == QLatin1String("QUrl")) { //turn absolute local file paths into relative paths
        QString filePath = castedValue.toUrl().toString();
        if (QFileInfo(filePath).exists() && QFileInfo(filePath).isAbsolute()) {
            QDir fileDir(QFileInfo(model()->fileUrl().toLocalFile()).absolutePath());
            castedValue = QUrl(fileDir.relativeFilePath(filePath));
        }
    }

414
415
416
417
418
419
420
        if (castedValue.type() == QVariant::Color) {
            QColor color = castedValue.value<QColor>();
            QColor newColor = QColor(color.name());
            newColor.setAlpha(color.alpha());
            castedValue = QVariant(newColor);
        }

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

void PropertyEditor::changeExpression(const QString &name)
{
439
440
441
442
443
444
    if (name.isNull())
        return;

    if (m_locked)
        return;

445
446
    RewriterTransaction transaction = beginRewriterTransaction();

447
448
449
    QString underscoreName(name);
    underscoreName.replace(QLatin1Char('.'), QLatin1Char('_'));

450
    QmlObjectNode fxObjectNode(m_selectedNode);
451
452
    PropertyEditorValue *value = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(m_currentType->m_backendValuesPropertyMap.value(underscoreName)));

Kai Koehne's avatar
Kai Koehne committed
453
    if (fxObjectNode.modelNode().metaInfo().isValid() && fxObjectNode.modelNode().metaInfo().property(name).isValid()) {
454
455
456
457
458
459
460
461
462
463
464
465
466
        if (fxObjectNode.modelNode().metaInfo().property(name).type() == QLatin1String("QColor")) {
            if (QColor(value->expression().remove('"')).isValid()) {
                fxObjectNode.setVariantProperty(name, QColor(value->expression().remove('"')));
                return;
            }
        } else if (fxObjectNode.modelNode().metaInfo().property(name).type() == QLatin1String("bool")) {
            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;
            }
467
468
469
470
471
472
473
474
475
476
477
478
479
480
        } else if (fxObjectNode.modelNode().metaInfo().property(name).type() == QLatin1String("int")) {
            bool ok;
            int intValue = value->expression().toInt(&ok);
            if (ok) {
                fxObjectNode.setVariantProperty(name, intValue);
                return;
            }
        } else if (fxObjectNode.modelNode().metaInfo().property(name).type() == QLatin1String("qreal")) {
            bool ok;
            qreal realValue = value->expression().toFloat(&ok);
            if (ok) {
                fxObjectNode.setVariantProperty(name, realValue);
                return;
            }
481
        }
Kai Koehne's avatar
Kai Koehne committed
482
    }
483

484
485
486
487
488
    if (!value) {
        qWarning() << "PropertyEditor::changeExpression no value for " << underscoreName;
        return;
    }

489
    try {
490
491
        if (fxObjectNode.expression(name) != value->expression() || !fxObjectNode.propertyAffectedByCurrentState(name))
            fxObjectNode.setBindingProperty(name, value->expression());
492
493
    }

494
    catch (RewritingException &e) {
495
        QMessageBox::warning(0, "Error", e.description());
496
497
498
    }
}

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

508
void PropertyEditor::otherPropertyChanged(const QmlObjectNode &fxObjectNode, const QString &propertyName)
509
{
510
511
    QmlModelView::otherPropertyChanged(fxObjectNode, propertyName);

512
    if (fxObjectNode.isValid() && m_currentType && fxObjectNode == m_selectedNode && fxObjectNode.currentState().isValid()) {
513
514
515
        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))
516
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).instanceValue(property.name()));
517
            else
518
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).modelValue(property.name()));
519
520
521
522
        }
    }
}

523
void PropertyEditor::transformChanged(const QmlObjectNode &fxObjectNode, const QString &propertyName)
524
{
525
526
    QmlModelView::transformChanged(fxObjectNode, propertyName);

527
    if (fxObjectNode.isValid() && m_currentType && fxObjectNode == m_selectedNode && fxObjectNode.currentState().isValid()) {
528
529
530
        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))
531
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).instanceValue(property.name()));
532
            else
533
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).modelValue(property.name()));
534
535
536
537
        }
    }
}

538
539
540
541
542
543
544
545
546
547
548
549
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)
550
        m_timerId = startTimer(200);
551
552
553
554
555
556
557
558
559
}

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

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

567
    QList<QString> orderedList;
568
    orderedList = type.properties(true).keys();
569
570
571
    qSort(orderedList);

    foreach (const QString &name, orderedList) {
572
573
574
        const PropertyMetaInfo propertyMetaInfo(type.property(name, true));
        QString properName = name;
        properName.replace(".", "_");
575

576
577
578
579
580
        QString typeName = propertyMetaInfo.type();
        //alias resolution only possible with instance
            if (typeName == QLatin1String("alias") && objectNode.isValid())
                typeName = objectNode.instanceType(name);

581
        if (!superType.hasProperty(name, true)) {
582
            if (typeName == "int") {
583
                qmlTemplate +=  QString(QLatin1String(
584
585
                "IntEditor { backendValue: backendValues.%2\n caption: \"%1\"\nbaseStateFlag: isBaseState\nslider: false\n}"
                )).arg(name).arg(properName);
586
            }
587
            if (typeName == "real" || typeName == "double" || typeName == "qreal") {
588
                qmlTemplate +=  QString(QLatin1String(
589
590
                "DoubleSpinBoxAlternate {\ntext: \"%1\"\nbackendValue: backendValues.%2\nbaseStateFlag: isBaseState\n}\n"
                )).arg(name).arg(properName);
591
            }
592
            if (typeName == "string" || typeName == "QString" || typeName == "QUrl" || typeName == "url") {
593
                 qmlTemplate +=  QString(QLatin1String(
594
595
                "QWidget {\nlayout: HorizontalLayout {\nLabel {\ntext: \"%1\"\ntoolTip: \"%1\"\n}\nLineEdit {\nbackendValue: backendValues.%2\nbaseStateFlag: isBaseState\n}\n}\n}\n"
                )).arg(name).arg(properName);
596
            }
597
            if (typeName == "bool") {
598
                 qmlTemplate +=  QString(QLatin1String(
599
600
                 "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);
601
            }
602
            if (typeName == "color" || typeName == "QColor") {
603
                qmlTemplate +=  QString(QLatin1String(
604
605
                "ColorGroupBox {\ncaption: \"%1\"\nfinished: finishedNotify\nbackendColor: backendValues.%2\n}\n\n"
                )).arg(name).arg(properName);
606
            }
607
608
609
610
611
612
613
614
        }
    }
    qmlTemplate += QLatin1String("}\n"); //VerticalLayout
    qmlTemplate += QLatin1String("}\n"); //GroupBox

    return qmlTemplate;
}

615
616
617
618
619
void PropertyEditor::resetView()
{
    if (model() == 0)
        return;

Thomas Hartmann's avatar
Thomas Hartmann committed
620
621
    m_locked = true;

622
623
624
625
626
627
628
629
630
    if (debug)
        qDebug() << "________________ RELOADING PROPERTY EDITOR QML _______________________";

    if (m_timerId)
        killTimer(m_timerId);

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

631
632
    QString specificsClassName;
    QUrl qmlFile(qmlForNode(m_selectedNode, specificsClassName));
633
634
635
636
    QUrl qmlSpecificsFile;
    if (m_selectedNode.isValid())
        qmlSpecificsFile = fileToUrl(locateQmlFile(m_selectedNode.type() + "Specifics.qml"));

637
638
    QString specificQmlData;

639
    if (m_selectedNode.isValid() && !QFileInfo(qmlSpecificsFile.toLocalFile()).exists() && m_selectedNode.metaInfo().isValid()) {
640
        //do magic !!
641
        specificQmlData = templateGeneration(m_selectedNode.metaInfo(), model()->metaInfo().nodeMetaInfo(specificsClassName), m_selectedNode);
642
643
    }

644
    NodeType *type = m_typeHash.value(qmlFile.toString());
645

646
    if (!type) {
647
        type = new NodeType(this);
648
649

        m_stackedWidget->addWidget(type->m_view);
650
        m_typeHash.insert(qmlFile.toString(), type);
651
652
653
654
655
656

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

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

686

Thomas Hartmann's avatar
Thomas Hartmann committed
687
    QDeclarativeContext *ctxt = type->m_view->rootContext();
688
    ctxt->setContextProperty("finishedNotify", QVariant(true));
689
    /*ctxt->setContextProperty("selectionChanged", QVariant(false));
690
    ctxt->setContextProperty("selectionChanged", QVariant(true));
691
692
    ctxt->setContextProperty("selectionChanged", QVariant(false));*/
    type->m_contextObject->triggerSelectionChanged();
693

694
695
    m_currentType = type;

696
697
    m_locked = false;

698
699
    if (m_timerId)
        m_timerId = 0;
700
701

    updateSize();
702
703
704
}

void PropertyEditor::selectedNodesChanged(const QList<ModelNode> &selectedNodeList,
705
                                          const QList<ModelNode> &lastSelectedNodeList)
706
707
708
{
    Q_UNUSED(lastSelectedNodeList);

709
    if (selectedNodeList.isEmpty() || selectedNodeList.count() > 1)
710
        select(ModelNode());
711
    else if (m_selectedNode != selectedNodeList.first())
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
        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
729
730
    m_locked = true;

731
732
733
734
    setupPane("Qt/Rectangle");
    setupPane("Qt/Text");
    setupPane("Qt/TextInput");
    setupPane("Qt/TextEdit");
735
    resetView();
Thomas Hartmann's avatar
Thomas Hartmann committed
736
737

    m_locked = false;
738
739
740
741
742
}

void PropertyEditor::modelAboutToBeDetached(Model *model)
{
    QmlModelView::modelAboutToBeDetached(model);
743
    m_currentType->m_propertyEditorTransaction->end();
744
745
746
747
748

    resetView();
}


749
void PropertyEditor::propertiesRemoved(const QList<AbstractProperty>& propertyList)
750
{
751
    QmlModelView::propertiesRemoved(propertyList);
752
753
754
755
756

    if (!m_selectedNode.isValid())
        return;

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


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

770
771
772
773
774
775
776
777
778
779
    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())
780
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).instanceValue(property.name()));
781
            else
782
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).modelValue(property.name()));
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
        }
    }
}

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) {
798
799
            if (property.name().contains("anchor", Qt::CaseInsensitive))
                m_currentType->m_backendAnchorBinding.invalidate(m_selectedNode);
800
            if ( QmlObjectNode(m_selectedNode).modelNode().property(property.name()).isBindingProperty())
801
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).instanceValue(property.name()));
802
            else
803
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).modelValue(property.name()));
804
805
806
807
808
809
810
811
        }
    }
}

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

812
    if (!m_selectedNode.isValid())
813
814
        return;

815
    if (node == m_selectedNode) {
816

817
        if (m_currentType) {
818
            setValue(node, "id", newId);
819
820
        }
    }
821
822
}

Marco Bubke's avatar
Marco Bubke committed
823
824
825
826
827
void PropertyEditor::scriptFunctionsChanged(const ModelNode &node, const QStringList &scriptFunctionList)
{
    QmlModelView::scriptFunctionsChanged(node, scriptFunctionList);
}

828
829
void PropertyEditor::select(const ModelNode &node)
{
830
    if (QmlItemNode(node).isValid())
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
        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();
}

853
854
855
856
857
858
859
void PropertyEditor::setValue(const QmlObjectNode &fxObjectNode, const QString &name, const QVariant &value)
{
    m_locked = true;
    m_currentType->setValue(fxObjectNode, name, value);
    m_locked = false;
}

860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
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;

883
    if (filePath.startsWith(QLatin1Char(':'))) {
884
885
886
887
888
889
890
891
892
893
894
        fileUrl.setScheme("qrc");
        QString path = filePath;
        path.remove(0, 1); // remove trailing ':'
        fileUrl.setPath(path);
    } else {
        fileUrl = QUrl::fromLocalFile(filePath);
    }

    return fileUrl;
}

895
QUrl PropertyEditor::qmlForNode(const ModelNode &modelNode, QString &className) const
896
897
898
899
900
901
902
903
{
    if (modelNode.isValid()) {
        QList<NodeMetaInfo> hierarchy;
        hierarchy << modelNode.metaInfo();
        hierarchy << modelNode.metaInfo().superClasses();

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

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();
}

926

927
928
} //QmlDesigner