propertyeditor.cpp 37.5 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
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
8
9
10
11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12
13
14
15
16
17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21
22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23
24
25
26
27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
29
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
30
31
32
33
34
35
**
**************************************************************************/

#include "propertyeditor.h"

#include <nodemetainfo.h>
36
#include <metainfo.h>
37
38

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

43
44
45
#include <bindingproperty.h>

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

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

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

enum {
    debug = false
};

84
85
const int collapseButtonOffset = 114;

86
87
namespace QmlDesigner {

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

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

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

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

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

125
126
}

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

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

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

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

177
    QDeclarativeContext *ctxt = m_view->rootContext();
178
179

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

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

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

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

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

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

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

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

230
    NodeMetaInfo metaInfo = propertyEditor->model()->metaInfo(typeName, 4, 7);
231

Marco Bubke's avatar
Marco Bubke committed
232
    foreach (const QString &propertyName, metaInfo.propertyNames())
233
        setupPropertyEditorValue(propertyName, &m_backendValuesPropertyMap, propertyEditor, metaInfo.propertyTypeName(propertyName));
234

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

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

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

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

256
257
258
259
260
261
262
263
264
    m_contextObject->setSpecificsUrl(qmlSpecificsFile);

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

    m_contextObject->setIsBaseState(true);

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

    m_contextObject->setGlobalBaseUrl(QUrl());
265
266
}

267
PropertyEditor::PropertyEditor(QWidget *parent) :
268
269
270
271
        QmlModelView(parent),
        m_parent(parent),
        m_updateShortcut(0),
        m_timerId(0),
272
        m_stackedWidget(new StackedWidget(parent)),
273
        m_currentType(0),
274
        m_locked(false),
275
276
        m_setupCompleted(false),
        m_singleShotTimer(new QTimer(this))
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
    m_stackedWidget->move(0, 0);
287
    connect(m_stackedWidget, SIGNAL(resized()), this, SLOT(updateSize()));
288
289
290

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

291
292
293
294
295
296
297
298

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

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

316
317
318
static inline QString fixTypeNameForPanes(const QString &typeName)
{
    QString fixedTypeName = typeName;
319
    fixedTypeName.replace('.', '/');
320
321
322
323
    fixedTypeName.replace("QtQuick/", "Qt/");
    return fixedTypeName;
}

324
325
326
void PropertyEditor::setupPane(const QString &typeName)
{

327
    QUrl qmlFile = fileToUrl(locateQmlFile(QLatin1String("Qt/ItemPane.qml")));
328
329
    QUrl qmlSpecificsFile;

330
    qmlSpecificsFile = fileToUrl(locateQmlFile(fixTypeNameForPanes(typeName) + "Specifics.qml"));
331
    NodeType *type = m_typeHash.value(qmlFile.toString());
332

333
    if (!type) {
334
        type = new NodeType(this);
335

336
        QDeclarativeContext *ctxt = type->m_view->rootContext();
337
338
        ctxt->setContextProperty("finishedNotify", QVariant(false) );
        type->initialSetup(typeName, qmlSpecificsFile, this);
339
        type->m_view->setSource(qmlFile);
340
        ctxt->setContextProperty("finishedNotify", QVariant(true) );
341

342
343
344
        m_stackedWidget->addWidget(type->m_view);
        m_typeHash.insert(qmlFile.toString(), type);
    } else {
345
        QDeclarativeContext *ctxt = type->m_view->rootContext();
346
        ctxt->setContextProperty("finishedNotify", QVariant(false) );
Thomas Hartmann's avatar
Thomas Hartmann committed
347

348
        type->initialSetup(typeName, qmlSpecificsFile, this);
349
350
        ctxt->setContextProperty("finishedNotify", QVariant(true) );
    }
351
352
}

353
void PropertyEditor::changeValue(const QString &propertyName)
354
{
355
    if (propertyName.isNull())
356
        return;
357
358
359
360

    if (m_locked)
        return;

361
    if (propertyName == "type")
362
363
        return;

364
365
366
    if (!m_selectedNode.isValid())
        return;

367
    if (propertyName == "id") {
368
        PropertyEditorValue *value = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(m_currentType->m_backendValuesPropertyMap.value(propertyName)));
369
370
        const QString newId = value->value().toString();

371
372
373
374
        if (newId == m_selectedNode.id())
            return;

        if (m_selectedNode.isValidId(newId)  && !modelNodeForId(newId).isValid() ) {
375
376
377
378
            if (m_selectedNode.id().isEmpty() || newId.isEmpty()) { //no id
                try {
                    m_selectedNode.setId(newId);
                } catch (InvalidIdException &e) { //better save then sorry
379
                    m_locked = true;
380
                    value->setValue(m_selectedNode.id());
381
                    m_locked = false;
382
383
384
385
386
387
                    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);
            }
388
        } else {
389
            m_locked = true;
390
            value->setValue(m_selectedNode.id());
391
392
393
394
395
            m_locked = false;
            if (!m_selectedNode.isValidId(newId))
                QMessageBox::warning(0, tr("Invalid Id"),  tr("%1 is an invalid id").arg(newId));
            else
                QMessageBox::warning(0, tr("Invalid Id"),  tr("%1 already exists").arg(newId));
396
397
398
399
        }
        return;
    }

400
401
402
    //.replace(QLatin1Char('.'), QLatin1Char('_'))
    QString underscoreName(propertyName);
    underscoreName.replace(QLatin1Char('.'), QLatin1Char('_'));
403
    PropertyEditorValue *value = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(m_currentType->m_backendValuesPropertyMap.value(underscoreName)));
404
405
406
407
408
409
410
411
412

    if (value ==0) {
        return;
    }

    QmlObjectNode fxObjectNode(m_selectedNode);

    QVariant castedValue;

Marco Bubke's avatar
Marco Bubke committed
413
    if (fxObjectNode.modelNode().metaInfo().isValid() && fxObjectNode.modelNode().metaInfo().hasProperty(propertyName)) {
414
        castedValue = fxObjectNode.modelNode().metaInfo().propertyCastedValue(propertyName, value->value());
415
    } else {
416
        qWarning() << "PropertyEditor:" <<propertyName << "cannot be casted (metainfo)";
417
418
419
420
        return ;
    }

    if (value->value().isValid() && !castedValue.isValid()) {
421
        qWarning() << "PropertyEditor:" << propertyName << "not properly casted (metainfo)";
422
423
424
        return ;
    }

Marco Bubke's avatar
Marco Bubke committed
425
    if (fxObjectNode.modelNode().metaInfo().isValid() && fxObjectNode.modelNode().metaInfo().hasProperty(propertyName))
426
        if (fxObjectNode.modelNode().metaInfo().propertyTypeName(propertyName) == QLatin1String("QUrl")) { //turn absolute local file paths into relative paths
427
428
429
430
431
432
433
        QString filePath = castedValue.toUrl().toString();
        if (QFileInfo(filePath).exists() && QFileInfo(filePath).isAbsolute()) {
            QDir fileDir(QFileInfo(model()->fileUrl().toLocalFile()).absolutePath());
            castedValue = QUrl(fileDir.relativeFilePath(filePath));
        }
    }

434
435
436
437
438
439
440
        if (castedValue.type() == QVariant::Color) {
            QColor color = castedValue.value<QColor>();
            QColor newColor = QColor(color.name());
            newColor.setAlpha(color.alpha());
            castedValue = QVariant(newColor);
        }

441
442
443
444
        try {
            if (!value->value().isValid()) { //reset
                fxObjectNode.removeVariantProperty(propertyName);
            } else {
445
                if (castedValue.isValid() && !castedValue.isNull()) {
446
                    m_locked = true;
447
448
449
                    fxObjectNode.setVariantProperty(propertyName, castedValue);
                    m_locked = false;
                }
450
451
452
453
            }
        }
        catch (RewritingException &e) {
            QMessageBox::warning(0, "Error", e.description());
454
455
456
457
458
        }
}

void PropertyEditor::changeExpression(const QString &name)
{
459
460
461
462
463
464
    if (name.isNull())
        return;

    if (m_locked)
        return;

465
466
    RewriterTransaction transaction = beginRewriterTransaction();

467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
    try {
        QString underscoreName(name);
        underscoreName.replace(QLatin1Char('.'), QLatin1Char('_'));

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

        if (fxObjectNode.modelNode().metaInfo().isValid() && fxObjectNode.modelNode().metaInfo().hasProperty(name)) {
            if (fxObjectNode.modelNode().metaInfo().propertyTypeName(name) == QLatin1String("QColor")) {
                if (QColor(value->expression().remove('"')).isValid()) {
                    fxObjectNode.setVariantProperty(name, QColor(value->expression().remove('"')));
                    transaction.commit(); //committing in the try block
                    return;
                }
            } else if (fxObjectNode.modelNode().metaInfo().propertyTypeName(name) == 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);
                    transaction.commit(); //committing in the try block
                    return;
                }
            } else if (fxObjectNode.modelNode().metaInfo().propertyTypeName(name) == QLatin1String("int")) {
                bool ok;
                int intValue = value->expression().toInt(&ok);
                if (ok) {
                    fxObjectNode.setVariantProperty(name, intValue);
                    transaction.commit(); //committing in the try block
                    return;
                }
            } else if (fxObjectNode.modelNode().metaInfo().propertyTypeName(name) == QLatin1String("qreal")) {
                bool ok;
                qreal realValue = value->expression().toFloat(&ok);
                if (ok) {
                    fxObjectNode.setVariantProperty(name, realValue);
                    transaction.commit(); //committing in the try block
                    return;
                }
506
            }
507
508
        }

509
510
511
512
        if (!value) {
            qWarning() << "PropertyEditor::changeExpression no value for " << underscoreName;
            return;
        }
513

514
515
516
        if (value->expression().isEmpty())
            return;

517
518
        if (fxObjectNode.expression(name) != value->expression() || !fxObjectNode.propertyAffectedByCurrentState(name))
            fxObjectNode.setBindingProperty(name, value->expression());
519
520

        transaction.commit(); //committing in the try block
521
522
    }

523
    catch (RewritingException &e) {
524
        QMessageBox::warning(0, "Error", e.description());
525
526
527
    }
}

528
void PropertyEditor::updateSize()
529
{
530
531
532
533
534
    if (!m_currentType)
        return;
    QWidget* frame = m_currentType->m_view->findChild<QWidget*>("propertyEditorFrame");
    if (frame)
        frame->resize(m_stackedWidget->size());
535
536
}

537
538
539
void PropertyEditor::setupPanes()
{
    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
540
541
    setupPane("QtQuick.Rectangle");
    setupPane("QtQuick.Text");
542
543
544
545
546
    resetView();
    m_setupCompleted = true;
    QApplication::restoreOverrideCursor();
}

547
void PropertyEditor::otherPropertyChanged(const QmlObjectNode &fxObjectNode, const QString &propertyName)
548
{
549
550
    QmlModelView::otherPropertyChanged(fxObjectNode, propertyName);

551
552
553
    if (!m_selectedNode.isValid())
        return;

554
555
    m_locked = true;

556
    if (fxObjectNode.isValid() && m_currentType && fxObjectNode == m_selectedNode && fxObjectNode.currentState().isValid()) {
557
558
        AbstractProperty property = fxObjectNode.modelNode().property(propertyName);
        if (fxObjectNode == m_selectedNode || QmlObjectNode(m_selectedNode).propertyChangeForCurrentState() == fxObjectNode) {
559
            if ( !m_selectedNode.hasProperty(propertyName) || m_selectedNode.property(property.name()).isBindingProperty() )
560
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).instanceValue(property.name()));
561
            else
562
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).modelValue(property.name()));
563
564
        }
    }
565
566

    m_locked = false;
567
568
}

569
void PropertyEditor::transformChanged(const QmlObjectNode &fxObjectNode, const QString &propertyName)
570
{
571
572
    QmlModelView::transformChanged(fxObjectNode, propertyName);

573
574
575
    if (!m_selectedNode.isValid())
        return;

576
    if (fxObjectNode.isValid() && m_currentType && fxObjectNode == m_selectedNode && fxObjectNode.currentState().isValid()) {
577
578
579
        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))
580
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).instanceValue(property.name()));
581
            else
582
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).modelValue(property.name()));
583
584
585
586
        }
    }
}

587
588
589
590
591
592
593
594
595
596
597
598
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)
599
        m_timerId = startTimer(200);
600
601
602
603
604
605
606
607
608
}

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

609
QString templateGeneration(NodeMetaInfo type, NodeMetaInfo superType, const QmlObjectNode &objectNode)
610
{
611
    QString qmlTemplate = QLatin1String("import Qt 4.7\nimport Bauhaus 1.0\n");
612
613
614
615
    qmlTemplate += QLatin1String("GroupBox {\n");
    qmlTemplate += QString(QLatin1String("caption: \"%1\"\n")).arg(type.typeName());
    qmlTemplate += QLatin1String("layout: VerticalLayout {\n");

616
    QList<QString> orderedList;
Marco Bubke's avatar
Marco Bubke committed
617
    orderedList = type.propertyNames();
618
619
620
    qSort(orderedList);

    foreach (const QString &name, orderedList) {
621
622
623

        if (name.startsWith(QLatin1String("__")))
            continue; //private API
624
        QString properName = name;
625

626
        properName.replace('.', '_');
627

628
        QString typeName = type.propertyTypeName(name);
629
630
631
632
        //alias resolution only possible with instance
            if (typeName == QLatin1String("alias") && objectNode.isValid())
                typeName = objectNode.instanceType(name);

633
        if (!superType.hasProperty(name) && type.propertyIsWritable(name)) {
634
            if (typeName == "int") {
635
                qmlTemplate +=  QString(QLatin1String(
636
637
                "IntEditor { backendValue: backendValues.%2\n caption: \"%1\"\nbaseStateFlag: isBaseState\nslider: false\n}"
                )).arg(name).arg(properName);
638
            }
639
            if (typeName == "real" || typeName == "double" || typeName == "qreal") {
640
                qmlTemplate +=  QString(QLatin1String(
641
642
                "DoubleSpinBoxAlternate {\ntext: \"%1\"\nbackendValue: backendValues.%2\nbaseStateFlag: isBaseState\n}\n"
                )).arg(name).arg(properName);
643
            }
644
            if (typeName == "string" || typeName == "QString" || typeName == "QUrl" || typeName == "url") {
645
                 qmlTemplate +=  QString(QLatin1String(
646
647
                "QWidget {\nlayout: HorizontalLayout {\nLabel {\ntext: \"%1\"\ntoolTip: \"%1\"\n}\nLineEdit {\nbackendValue: backendValues.%2\nbaseStateFlag: isBaseState\n}\n}\n}\n"
                )).arg(name).arg(properName);
648
            }
649
            if (typeName == "bool") {
650
                 qmlTemplate +=  QString(QLatin1String(
651
652
                 "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);
653
            }
654
            if (typeName == "color" || typeName == "QColor") {
655
                qmlTemplate +=  QString(QLatin1String(
656
657
                "ColorGroupBox {\ncaption: \"%1\"\nfinished: finishedNotify\nbackendColor: backendValues.%2\n}\n\n"
                )).arg(name).arg(properName);
658
            }
659
660
661
662
663
664
665
666
        }
    }
    qmlTemplate += QLatin1String("}\n"); //VerticalLayout
    qmlTemplate += QLatin1String("}\n"); //GroupBox

    return qmlTemplate;
}

667
668
669
670
671
void PropertyEditor::resetView()
{
    if (model() == 0)
        return;

Thomas Hartmann's avatar
Thomas Hartmann committed
672
673
    m_locked = true;

674
675
676
677
678
679
680
681
682
    if (debug)
        qDebug() << "________________ RELOADING PROPERTY EDITOR QML _______________________";

    if (m_timerId)
        killTimer(m_timerId);

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

683
684
    QString specificsClassName;
    QUrl qmlFile(qmlForNode(m_selectedNode, specificsClassName));
685
686
    QUrl qmlSpecificsFile;
    if (m_selectedNode.isValid())
687
        qmlSpecificsFile = fileToUrl(locateQmlFile(fixTypeNameForPanes(m_selectedNode.type()) + "Specifics.qml"));
688

689
690
    QString specificQmlData;

691
    if (m_selectedNode.isValid() && !QFileInfo(qmlSpecificsFile.toLocalFile()).exists() && m_selectedNode.metaInfo().isValid()) {
692
        //do magic !!
693
        specificQmlData = templateGeneration(m_selectedNode.metaInfo(), model()->metaInfo(specificsClassName), m_selectedNode);
694
695
    }

696
    NodeType *type = m_typeHash.value(qmlFile.toString());
697

698
    if (!type) {
699
        type = new NodeType(this);
700
701

        m_stackedWidget->addWidget(type->m_view);
702
        m_typeHash.insert(qmlFile.toString(), type);
703
704
705
706
707
708

        QmlObjectNode fxObjectNode;
        if (m_selectedNode.isValid()) {
            fxObjectNode = QmlObjectNode(m_selectedNode);
            Q_ASSERT(fxObjectNode.isValid());
        }
709
        QDeclarativeContext *ctxt = type->m_view->rootContext();
710
        type->setup(fxObjectNode, currentState().name(), qmlSpecificsFile, this);
711
        ctxt->setContextProperty("finishedNotify", QVariant(false));
712
        if (specificQmlData.isEmpty())
713
714
715
716
            type->m_contextObject->setSpecificQmlData(specificQmlData);
            
        type->m_contextObject->setGlobalBaseUrl(qmlFile);
        type->m_contextObject->setSpecificQmlData(specificQmlData);
717
        type->m_view->setSource(qmlFile);
718
719
720
721
722
723
        ctxt->setContextProperty("finishedNotify", QVariant(true));
    } else {
        QmlObjectNode fxObjectNode;
        if (m_selectedNode.isValid()) {
            fxObjectNode = QmlObjectNode(m_selectedNode);
        }
Thomas Hartmann's avatar
Thomas Hartmann committed
724
        QDeclarativeContext *ctxt = type->m_view->rootContext();
725
        
726
        ctxt->setContextProperty("finishedNotify", QVariant(false));
727
        if (specificQmlData.isEmpty())
728
            type->m_contextObject->setSpecificQmlData(specificQmlData);
729
730
        QString currentStateName = currentState().isValid() ? currentState().name() : QLatin1String("invalid state");
        type->setup(fxObjectNode, currentStateName, qmlSpecificsFile, this);
731
732
        type->m_contextObject->setGlobalBaseUrl(qmlFile);
        type->m_contextObject->setSpecificQmlData(specificQmlData);
733
734
735
    }

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

737

Thomas Hartmann's avatar
Thomas Hartmann committed
738
    QDeclarativeContext *ctxt = type->m_view->rootContext();
739
    ctxt->setContextProperty("finishedNotify", QVariant(true));
740
    /*ctxt->setContextProperty("selectionChanged", QVariant(false));
741
    ctxt->setContextProperty("selectionChanged", QVariant(true));
742
743
    ctxt->setContextProperty("selectionChanged", QVariant(false));*/
    type->m_contextObject->triggerSelectionChanged();
744

745
746
    m_currentType = type;

747
748
    m_locked = false;

749
750
    if (m_timerId)
        m_timerId = 0;
751
752

    updateSize();
753
754
755
}

void PropertyEditor::selectedNodesChanged(const QList<ModelNode> &selectedNodeList,
756
                                          const QList<ModelNode> &lastSelectedNodeList)
757
758
759
{
    Q_UNUSED(lastSelectedNodeList);

760
    if (selectedNodeList.isEmpty() || selectedNodeList.count() > 1)
761
        select(ModelNode());
762
    else if (m_selectedNode != selectedNodeList.first())
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
        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
780
781
    m_locked = true;

782
    resetView();
783
784
785
786
787
788
    if (!m_setupCompleted) {
        m_singleShotTimer->setSingleShot(true);
        m_singleShotTimer->setInterval(1000);
        connect(m_singleShotTimer, SIGNAL(timeout()), this, SLOT(setupPanes()));
        m_singleShotTimer->start();
    }
Thomas Hartmann's avatar
Thomas Hartmann committed
789
790

    m_locked = false;
791
792
793
794
795
}

void PropertyEditor::modelAboutToBeDetached(Model *model)
{
    QmlModelView::modelAboutToBeDetached(model);
796
    m_currentType->m_propertyEditorTransaction->end();
797
798
799
800
801

    resetView();
}


802
void PropertyEditor::propertiesRemoved(const QList<AbstractProperty>& propertyList)
803
{
804
    QmlModelView::propertiesRemoved(propertyList);
805
806
807
808
809

    if (!m_selectedNode.isValid())
        return;

    foreach (const AbstractProperty &property, propertyList) {
810
811
812
        ModelNode node(property.parentModelNode());
        if (node == m_selectedNode || QmlObjectNode(m_selectedNode).propertyChangeForCurrentState() == node) {
            setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).instanceValue(property.name()));
813
814
            if (property.name().contains("anchor", Qt::CaseInsensitive))
                m_currentType->m_backendAnchorBinding.invalidate(m_selectedNode);
815
816
817
818
819
820
821
        }
    }
}


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

823
824
825
826
827
828
829
830
831
832
    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())
833
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).instanceValue(property.name()));
834
            else
835
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).modelValue(property.name()));
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
        }
    }
}

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) {
851
852
            if (property.name().contains("anchor", Qt::CaseInsensitive))
                m_currentType->m_backendAnchorBinding.invalidate(m_selectedNode);
853
            if ( QmlObjectNode(m_selectedNode).modelNode().property(property.name()).isBindingProperty())
854
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).instanceValue(property.name()));
855
            else
856
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).modelValue(property.name()));
857
858
859
860
        }
    }
}

861
862
863
864
865
866

void PropertyEditor::instanceInformationsChange(const QVector<ModelNode> &nodeList)
{
    if (!m_selectedNode.isValid())
        return;

867
    m_locked = true;
868
869
    if (nodeList.contains(m_selectedNode))
        m_currentType->m_backendAnchorBinding.setup(QmlItemNode(m_selectedNode));
870
    m_locked = false;
871
872
}

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

877
    if (!m_selectedNode.isValid())
878
879
        return;

880
    if (node == m_selectedNode) {
881

882
        if (m_currentType) {
883
            setValue(node, "id", newId);
884
885
        }
    }
886
887
}

Marco Bubke's avatar
Marco Bubke committed
888
889
890
891
892
void PropertyEditor::scriptFunctionsChanged(const ModelNode &node, const QStringList &scriptFunctionList)
{
    QmlModelView::scriptFunctionsChanged(node, scriptFunctionList);
}

893
894
void PropertyEditor::select(const ModelNode &node)
{
895
    if (QmlItemNode(node).isValid())
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
        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();
}

918
919
920
921
922
923
924
void PropertyEditor::setValue(const QmlObjectNode &fxObjectNode, const QString &name, const QVariant &value)
{
    m_locked = true;
    m_currentType->setValue(fxObjectNode, name, value);
    m_locked = false;
}

925
926
927
928
929
930
931
932
933
934
935
936
937
938
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
{
939
    if (nodeInfo.typeName().split('.').last() == "QDeclarativeItem")
940
        return "Qt/ItemPane.qml";
941
    const QString fixedTypeName = fixTypeNameForPanes(nodeInfo.typeName());
942
    return fixedTypeName + QLatin1String("Pane.qml");
943
944
945
946
947
948
949
950
}

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

    if (filePath.isEmpty())
        return fileUrl;

951
    if (filePath.startsWith(QLatin1Char(':'))) {
952
953
954
955
956
957
958
959
960
961
962
        fileUrl.setScheme("qrc");
        QString path = filePath;
        path.remove(0, 1); // remove trailing ':'
        fileUrl.setPath(path);
    } else {
        fileUrl = QUrl::fromLocalFile(filePath);
    }

    return fileUrl;
}

963
QUrl PropertyEditor::qmlForNode(const ModelNode &modelNode, QString &className) const
964
965
966
967
968
969
970
971
{
    if (modelNode.isValid()) {
        QList<NodeMetaInfo> hierarchy;
        hierarchy << modelNode.metaInfo();
        hierarchy << modelNode.metaInfo().superClasses();

        foreach (const NodeMetaInfo &info, hierarchy) {
            QUrl fileUrl = fileToUrl(locateQmlFile(qmlFileName(info)));