propertyeditor.cpp 34.7 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

#include "propertyeditorvalue.h"
44
45
46
47
48
49
50
51
#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
52
#include "siblingcombobox.h"
53
#include "propertyeditortransaction.h"
54

55
56
#include <coreplugin/sidebar.h>

57
58
59
60
61
#include <QtCore/QCoreApplication>
#include <QtCore/QDir>
#include <QtCore/QFileSystemWatcher>
#include <QtCore/QFileInfo>
#include <QtCore/QDebug>
62
63
#include <QtDeclarative/QDeclarativeView>
#include <QtDeclarative/QDeclarativeContext>
64
65
66
#include <QtGui/QVBoxLayout>
#include <QtGui/QShortcut>
#include <QtGui/QStackedWidget>
67
68
#include <QDeclarativeEngine>
#include <private/qdeclarativemetatype_p.h>
69
#include <QMessageBox>
70
#include <QApplication>
71
72
#include <QGraphicsOpacityEffect>
#include <QToolBar>
73
74
75
76
77

enum {
    debug = false
};

78
79
const int collapseButtonOffset = 114;

80
81
namespace QmlDesigner {

82
PropertyEditor::NodeType::NodeType(PropertyEditor *propertyEditor) :
83
        m_view(new QDeclarativeView), m_propertyEditorTransaction(new PropertyEditorTransaction(propertyEditor)), m_dummyPropertyEditorValue(new PropertyEditorValue())
84
85
86
{
    Q_ASSERT(QFileInfo(":/images/button_normal.png").exists());

87
    m_view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
88
89
90
    QDeclarativeContext *ctxt = m_view->rootContext();
    m_dummyPropertyEditorValue->setValue("#000000");
    ctxt->setContextProperty("dummyBackendValue", m_dummyPropertyEditorValue.data());
91
92
93
94
95
96
97
98

    connect(&m_backendValuesPropertyMap, SIGNAL(valueChanged(const QString&)), propertyEditor, SLOT(changeValue(const QString&)));
}

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

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

116
117
}

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

132
    if (fxObjectNode.propertyAffectedByCurrentState(name) && !(fxObjectNode.modelNode().property(name).isBindingProperty())) {
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
        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());
    }
}

148
void PropertyEditor::NodeType::setValue(const QmlObjectNode & fxObjectNode, const QString &name, const QVariant &value)
149
{
150
151
    QString propertyName = name;
    propertyName.replace(QLatin1Char('.'), QLatin1Char('_'));
152
    PropertyEditorValue *propertyValue = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(m_backendValuesPropertyMap.value(propertyName)));
153
    if (propertyValue) {
154
        propertyValue->setValue(value);
155
156
157
        if (!fxObjectNode.hasBindingProperty(name))
            propertyValue->setExpression(value.toString());
    }
158
159
}

160
void PropertyEditor::NodeType::setup(const QmlObjectNode &fxObjectNode, const QString &stateName, const QUrl &qmlSpecificsFile, PropertyEditor *propertyEditor)
161
162
163
164
{
    if (!fxObjectNode.isValid())
        return;

165
    QDeclarativeContext *ctxt = m_view->rootContext();
166
167
168

    if (fxObjectNode.isValid()) {
        foreach (const QString &propertyName, fxObjectNode.modelNode().metaInfo().properties(true).keys())
169
            createPropertyEditorValue(fxObjectNode, propertyName, fxObjectNode.instanceValue(propertyName), &m_backendValuesPropertyMap, propertyEditor);
170
171

        // className
172
        PropertyEditorValue *valueObject = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(m_backendValuesPropertyMap.value("className")));
173
174
        if (!valueObject)
            valueObject = new PropertyEditorValue(&m_backendValuesPropertyMap);
175
176
177
178
        valueObject->setName("className");
        valueObject->setModelNode(fxObjectNode.modelNode());
        valueObject->setValue(fxObjectNode.modelNode().simplifiedTypeName());
        QObject::connect(valueObject, SIGNAL(valueChanged(QString)), &m_backendValuesPropertyMap, SIGNAL(valueChanged(QString)));
179
        m_backendValuesPropertyMap.insert("className", QVariant::fromValue(valueObject));
180
181

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

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

        ctxt->setContextProperty("anchorBackend", &m_backendAnchorBinding);
194
        QApplication::processEvents();
195
        ctxt->setContextProperty("transaction", m_propertyEditorTransaction.data());
196
        QApplication::processEvents();
197
        ctxt->setContextProperty("backendValues", &m_backendValuesPropertyMap);
198
        QApplication::processEvents();
199
        ctxt->setContextProperty("specificsUrl", QVariant(qmlSpecificsFile));
200
        QApplication::processEvents();
201
        ctxt->setContextProperty("stateName", QVariant(stateName));
202
        QApplication::processEvents();
203
204
        if (!fxObjectNode.isValid())
            return;
205
206
        ctxt->setContextProperty("propertyCount", QVariant(fxObjectNode.modelNode().properties().count()));
        ctxt->setContextProperty("isBaseState", QVariant(fxObjectNode.isInBaseState()));
207
        ctxt->setContextProperty("selectionChanged", QVariant(false));
208
209
210
211
212
    } else {
        qWarning() << "PropertyEditor: invalid node for setup";
    }
}

213
214
void PropertyEditor::NodeType::initialSetup(const QString &typeName, const QUrl &qmlSpecificsFile, PropertyEditor *propertyEditor)
{
215
    QDeclarativeContext *ctxt = m_view->rootContext();
216
217
218
219

    NodeMetaInfo metaInfo = propertyEditor->model()->metaInfo().nodeMetaInfo(typeName, 4, 6);

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

222
    PropertyEditorValue *valueObject = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(m_backendValuesPropertyMap.value("className")));
223
224
225
226
227
228
229
230
231
    if (!valueObject)
        valueObject = new PropertyEditorValue(&m_backendValuesPropertyMap);
    valueObject->setName("className");

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

    // id
232
    valueObject = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(m_backendValuesPropertyMap.value("id")));
233
234
235
236
237
238
239
240
    if (!valueObject)
        valueObject = new PropertyEditorValue(&m_backendValuesPropertyMap);
    valueObject->setName("id");
    valueObject->setValue("id");
    QObject::connect(valueObject, SIGNAL(valueChanged(QString)), &m_backendValuesPropertyMap, SIGNAL(valueChanged(QString)));
    m_backendValuesPropertyMap.insert("id", QVariant::fromValue(valueObject));

    ctxt->setContextProperty("anchorBackend", &m_backendAnchorBinding);
241
    ctxt->setContextProperty("transaction", m_propertyEditorTransaction.data());
242
243
244
245
246
247
248
    ctxt->setContextProperty("backendValues", &m_backendValuesPropertyMap);

    ctxt->setContextProperty("specificsUrl", QVariant(qmlSpecificsFile));
    ctxt->setContextProperty("stateName", QVariant(QLatin1String("basestate")));
    ctxt->setContextProperty("isBaseState", QVariant(true));
}

249
PropertyEditor::PropertyEditor(QWidget *parent) :
250
251
252
253
        QmlModelView(parent),
        m_parent(parent),
        m_updateShortcut(0),
        m_timerId(0),
254
255
        m_stackedWidget(new StackedWidget(parent)),
        m_collapseButton(new CollapseButton(m_stackedWidget)),
256
257
        m_currentType(0),
        m_locked(false)
258
259
260
261
262
263
264
265
{
    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);
266
    m_stackedWidget->setMinimumWidth(300);
267
268
269
270
271
272
273
274
    m_collapseButton->raise();
    m_collapseButton->show();
    connect(m_collapseButton, SIGNAL(expand()), this, SLOT(expand()));
    connect(m_collapseButton, SIGNAL(collapse()), this, SLOT(collapse()));
    connect(m_stackedWidget, SIGNAL(resized()), this, SLOT(updateCollapseButton()));

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

275
276
277
278
279
280
281
282
283
284
285
286
287

    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
288
        SiblingComboBox::registerDeclarativeTypes();
289
    }
290
291
292
293
294
295
296
297
}

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

298
299
300
301
302
303
304
void PropertyEditor::setupPane(const QString &typeName)
{

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

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

307
    if (!type) {
308
        type = new NodeType(this);
309

310
        QDeclarativeContext *ctxt = type->m_view->rootContext();
311
312
        ctxt->setContextProperty("finishedNotify", QVariant(false) );
        type->initialSetup(typeName, qmlSpecificsFile, this);
313
        type->m_view->setSource(qmlFile);
314
        ctxt->setContextProperty("finishedNotify", QVariant(true) );
315

316
317
318
        m_stackedWidget->addWidget(type->m_view);
        m_typeHash.insert(qmlFile.toString(), type);
    } else {
319
        QDeclarativeContext *ctxt = type->m_view->rootContext();
320
        ctxt->setContextProperty("finishedNotify", QVariant(false) );
Thomas Hartmann's avatar
Thomas Hartmann committed
321

322
        type->initialSetup(typeName, qmlSpecificsFile, this);
323
324
        ctxt->setContextProperty("finishedNotify", QVariant(true) );
    }
325
    m_stackedWidget->setCurrentWidget(type->m_view);
326
327
}

328
void PropertyEditor::changeValue(const QString &propertyName)
329
{
330
    if (propertyName.isNull())
331
        return;
332
333
334
335

    if (m_locked)
        return;

336
    if (propertyName == "type")
337
338
        return;

339
340
341
    if (!m_selectedNode.isValid())
        return;

342
    if (propertyName == "id") {
343
        PropertyEditorValue *value = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(m_currentType->m_backendValuesPropertyMap.value(propertyName)));
344
345
346
347
348
349
350
351
352
353
354
355
356
357
        const QString newId = value->value().toString();

        try {
            if (ModelNode::isValidId(newId))
                m_selectedNode.setId(newId);
            else
                value->setValue(m_selectedNode.id());
        } catch (InvalidIdException &) {
            value->setValue(m_selectedNode.id());
        }

        return;
    }

358
359
360
    //.replace(QLatin1Char('.'), QLatin1Char('_'))
    QString underscoreName(propertyName);
    underscoreName.replace(QLatin1Char('.'), QLatin1Char('_'));
361
    PropertyEditorValue *value = qobject_cast<PropertyEditorValue*>(QDeclarativeMetaType::toQObject(m_currentType->m_backendValuesPropertyMap.value(underscoreName)));
362
363

    if (value ==0) {
364
        qWarning() << "PropertyEditor:" <<propertyName << " - value is null";
365
366
367
368
369
370
371
372
373
374
        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 {
375
        qWarning() << "PropertyEditor:" <<propertyName << "cannot be casted (metainfo)";
376
377
378
379
        return ;
    }

    if (value->value().isValid() && !castedValue.isValid()) {
380
        qWarning() << "PropertyEditor:" << propertyName << "not properly casted (metainfo)";
381
382
383
384
385
386
387
388
389
390
391
392
        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));
        }
    }

393
    try {
394
        if (!value->value().isValid()) { //reset
395
396
397
            fxObjectNode.removeVariantProperty(propertyName);
        } else {
            if (castedValue.isValid() && !castedValue.isNull())
398
                m_locked = true;
399
                fxObjectNode.setVariantProperty(propertyName, castedValue);
400
                m_locked = false;
401
        }
402
    }
403
    catch (RewritingException &e) {
404
405
        QMessageBox::warning(0, "Error", e.description());
    }
406
407
408
409
}

void PropertyEditor::changeExpression(const QString &name)
{
410
411
412
413
414
415
    if (name.isNull())
        return;

    if (m_locked)
        return;

416
417
418
    QString underscoreName(name);
    underscoreName.replace(QLatin1Char('.'), QLatin1Char('_'));

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

Kai Koehne's avatar
Kai Koehne committed
422
    if (fxObjectNode.modelNode().metaInfo().isValid() && fxObjectNode.modelNode().metaInfo().property(name).isValid()) {
423
424
425
426
427
428
429
430
431
432
433
434
435
        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;
            }
436
437
438
439
440
441
442
443
444
445
446
447
448
449
        } 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;
            }
450
        }
Kai Koehne's avatar
Kai Koehne committed
451
    }
452

453
454
455
456
457
    if (!value) {
        qWarning() << "PropertyEditor::changeExpression no value for " << underscoreName;
        return;
    }

458
459
    try {
        if (fxObjectNode.currentState().isBaseState()) {
460
461
            if (fxObjectNode.modelNode().bindingProperty(name).expression() != value->expression())
                fxObjectNode.modelNode().bindingProperty(name).setExpression(value->expression());
462
463
464
        }
    }

465
    catch (RewritingException &e) {
466
        QMessageBox::warning(0, "Error", e.description());
467
468
469
    }
}

470
471
472

void PropertyEditor::expand()
{
473
    m_stackedWidget->setMinimumWidth(300);
474
475
476
477
478
479
480
481
482
    m_stackedWidget->setMaximumWidth(1000);
    m_stackedWidget->parentWidget()->layout()->update();

    QWidget * parentWidget = m_stackedWidget->parentWidget();
    QWidget *childWidget = m_stackedWidget;

    while (parentWidget) {
        if (Core::SideBar *sideBar  = qobject_cast<Core::SideBar*>(parentWidget)) {
            childWidget->setMaximumWidth(2000);
483
484
485
486
487
            foreach (QObject* child, sideBar->children()) {
                if (QWidget* otherWidget = qobject_cast<QWidget*>(child))
                    if (otherWidget != childWidget && !qobject_cast<QComboBox*>(otherWidget))
                    otherWidget->show();
            }
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
            QList<QComboBox*> list = sideBar->findChildren<QComboBox*>();
            foreach (QComboBox* box, list) {
                if (qobject_cast<QToolBar*>(box->parentWidget()))
                    box->parentWidget()->show();
            }
            sideBar->refresh();
            sideBar->update();
            break;
        }
        childWidget = parentWidget;
        parentWidget = parentWidget->parentWidget();
    }
    m_collapseButton->move(0, m_stackedWidget->height() - collapseButtonOffset);
    resetView();
}

void PropertyEditor::collapse()
{
    m_stackedWidget->setCurrentIndex(0);
    m_stackedWidget->setMinimumWidth(0);

    QWidget * parentWidget = m_stackedWidget->parentWidget();
    QWidget *childWidget = m_stackedWidget;

    while (parentWidget) {
        if (Core::SideBar *sideBar  = qobject_cast<Core::SideBar*>(parentWidget)) {
            childWidget->setMaximumWidth(22);
515
516
517
518
519
520
            foreach (QObject* child, sideBar->children()) {
                if (QWidget* otherWidget = qobject_cast<QWidget*>(child))
                    if (otherWidget != childWidget && !qobject_cast<QComboBox*>(otherWidget))
                    otherWidget->hide();
            }

521
            QSplitter* parentSplitter = qobject_cast<QSplitter*>(sideBar->parentWidget());
522
523
            QList<int> s = parentSplitter->sizes();
            qDebug() << s;
524
525
            s.removeLast();
            s.append(25);
526
            qDebug() << s;
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
            parentSplitter->setSizes(s);
            parentSplitter->refresh();
            parentSplitter->update();
            QApplication::processEvents();
            QList<QComboBox*> list = sideBar->findChildren<QComboBox*>();
            foreach (QComboBox* box, list) {
                if (qobject_cast<QToolBar*>(box->parentWidget())) {
                    box->parentWidget()->hide();
                }
            }
            break;
        }
        childWidget = parentWidget;
        parentWidget = parentWidget->parentWidget();
    }
    m_collapseButton->raise();
    m_stackedWidget->parentWidget()->layout()->activate();
    m_collapseButton->move(0, m_stackedWidget->height() - collapseButtonOffset + 20);
}

void PropertyEditor::updateCollapseButton()
{
    m_collapseButton->move(0, m_stackedWidget->height() - collapseButtonOffset + 20);
    m_collapseButton->update();
}

553
void PropertyEditor::otherPropertyChanged(const QmlObjectNode &fxObjectNode, const QString &propertyName)
554
{
555
556
    QmlModelView::otherPropertyChanged(fxObjectNode, propertyName);

557
    if (fxObjectNode.isValid() && m_currentType && fxObjectNode == m_selectedNode && fxObjectNode.currentState().isValid()) {
558
559
560
        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))
561
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).instanceValue(property.name()));
562
            else
563
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).modelValue(property.name()));
564
565
566
567
        }
    }
}

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

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

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

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

void PropertyEditor::resetView()
{
    if (model() == 0)
        return;

610
611
612
    if (m_collapseButton->isCollapsed())
        return;

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

    if (m_timerId)
        killTimer(m_timerId);

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

    QUrl qmlFile(qmlForNode(m_selectedNode));
    QUrl qmlSpecificsFile;
    if (m_selectedNode.isValid())
        qmlSpecificsFile = fileToUrl(locateQmlFile(m_selectedNode.type() + "Specifics.qml"));

627
628
    m_locked = true;

629
630
    NodeType *type = m_typeHash.value(qmlFile.toString());

631
    if (!type) {
632
        type = new NodeType(this);
633
634

        m_stackedWidget->addWidget(type->m_view);
635
        m_typeHash.insert(qmlFile.toString(), type);
636
637
638
639
640
641

        QmlObjectNode fxObjectNode;
        if (m_selectedNode.isValid()) {
            fxObjectNode = QmlObjectNode(m_selectedNode);
            Q_ASSERT(fxObjectNode.isValid());
        }
642
        type->setup(fxObjectNode, currentState().name(), qmlSpecificsFile, this);
643

644
        QDeclarativeContext *ctxt = type->m_view->rootContext();
645
        ctxt->setContextProperty("finishedNotify", QVariant(false));
646
        type->m_view->setSource(qmlFile);
647
648
649
650
651
652
        ctxt->setContextProperty("finishedNotify", QVariant(true));
    } else {
        QmlObjectNode fxObjectNode;
        if (m_selectedNode.isValid()) {
            fxObjectNode = QmlObjectNode(m_selectedNode);
        }
Thomas Hartmann's avatar
Thomas Hartmann committed
653
        QDeclarativeContext *ctxt = type->m_view->rootContext();
654
655
656
657
        ctxt->setContextProperty("selectionChanged", QVariant(false));
        ctxt->setContextProperty("selectionChanged", QVariant(true));
        ctxt->setContextProperty("selectionChanged", QVariant(false));
        ctxt->setContextProperty("finishedNotify", QVariant(false));
658
        type->setup(fxObjectNode, currentState().name(), qmlSpecificsFile, this);
659
660
661
    }

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

663
664
665
    m_collapseButton->raise();
    m_collapseButton->move(0, m_stackedWidget->height() - collapseButtonOffset);

Thomas Hartmann's avatar
Thomas Hartmann committed
666
    QDeclarativeContext *ctxt = type->m_view->rootContext();
667
668
669
670
671
    ctxt->setContextProperty("finishedNotify", QVariant(true));
    ctxt->setContextProperty("selectionChanged", QVariant(false));
    ctxt->setContextProperty("selectionChanged", QVariant(true));
    ctxt->setContextProperty("selectionChanged", QVariant(false));

672
673
    m_currentType = type;

674
675
    m_locked = false;

676
677
678
679
680
    if (m_timerId)
        m_timerId = 0;
}

void PropertyEditor::selectedNodesChanged(const QList<ModelNode> &selectedNodeList,
681
                                          const QList<ModelNode> &lastSelectedNodeList)
682
683
684
{
    Q_UNUSED(lastSelectedNodeList);

685
    if (selectedNodeList.isEmpty() || selectedNodeList.count() > 1)
686
        select(ModelNode());
687
    else if (m_selectedNode != selectedNodeList.first())
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
        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;

705
706
707
708
    setupPane("Qt/Rectangle");
    setupPane("Qt/Text");
    setupPane("Qt/TextInput");
    setupPane("Qt/TextEdit");
709
710
711
712
713
714
715
716
717
718
719
    resetView();
}

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

    resetView();
}


720
void PropertyEditor::propertiesRemoved(const QList<AbstractProperty>& propertyList)
721
{
722
    QmlModelView::propertiesRemoved(propertyList);
723
724
725
726
727

    if (!m_selectedNode.isValid())
        return;

    foreach (const AbstractProperty &property, propertyList) {
728
729
730
        ModelNode node(property.parentModelNode());
        if (node == m_selectedNode || QmlObjectNode(m_selectedNode).propertyChangeForCurrentState() == node) {
            setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).instanceValue(property.name()));
731
732
            if (property.name().contains("anchor", Qt::CaseInsensitive))
                m_currentType->m_backendAnchorBinding.invalidate(m_selectedNode);
733
734
735
736
737
738
739
        }
    }
}


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

741
742
743
744
745
746
747
748
749
750
    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())
751
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).instanceValue(property.name()));
752
            else
753
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).modelValue(property.name()));
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
        }
    }
}

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) {
769
770
            if (property.name().contains("anchor", Qt::CaseInsensitive))
                m_currentType->m_backendAnchorBinding.invalidate(m_selectedNode);
771
            if ( QmlObjectNode(m_selectedNode).modelNode().property(property.name()).isBindingProperty())
772
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).instanceValue(property.name()));
773
            else
774
                setValue(m_selectedNode, property.name(), QmlObjectNode(m_selectedNode).modelValue(property.name()));
775
776
777
778
779
780
781
782
        }
    }
}

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

783
    if (!m_selectedNode.isValid())
784
785
        return;

786
    if (node == m_selectedNode) {
787

788
        if (m_currentType) {
789
            setValue(node, "id", newId);
790
791
        }
    }
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
}

void PropertyEditor::select(const ModelNode &node)
{
    if (node.isValid())
        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();
}

819
820
821
822
823
824
825
void PropertyEditor::setValue(const QmlObjectNode &fxObjectNode, const QString &name, const QVariant &value)
{
    m_locked = true;
    m_currentType->setValue(fxObjectNode, name, value);
    m_locked = false;
}

826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
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;

849
    if (filePath.startsWith(QLatin1Char(':'))) {
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
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;
}

QUrl PropertyEditor::qmlForNode(const ModelNode &modelNode) const
{
    if (modelNode.isValid()) {
        QList<NodeMetaInfo> hierarchy;
        hierarchy << modelNode.metaInfo();
        hierarchy << modelNode.metaInfo().superClasses();

        foreach (const NodeMetaInfo &info, hierarchy) {
            QUrl fileUrl = fileToUrl(locateQmlFile(qmlFileName(info)));
            if (fileUrl.isValid())
                return fileUrl;
        }
    }
    return fileToUrl(QDir(m_qmlDir).filePath("Qt/emptyPane.qml"));
}

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

890
891
CollapseButton::CollapseButton(QWidget *parent) : QWidget(parent), m_collapsed(false), m_hovered(false),
                                  m_pixmap_normal(":/qmldesigner/images/collapse-button-normal.png"),
Lasse Holmstedt's avatar
Lasse Holmstedt committed
892
                                  m_pixmap_hover(":/qmldesigner/images/collapse-button-hover.png"),
893
                                  m_pixmap_normal_mirrored(":/qmldesigner/images/collapse-button-normal-mirrored.png"),
Lasse Holmstedt's avatar
Lasse Holmstedt committed
894
                                  m_pixmap_hover_mirrored(":/qmldesigner/images/collapse-button-hover-mirrored.png")
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
{
    setFixedWidth(20);
    setFixedHeight(81);
    setMouseTracking(true);
    setAttribute(Qt::WA_TranslucentBackground, true);
    QGraphicsOpacityEffect* graphicsOpacityEffect = new QGraphicsOpacityEffect(this);
    setGraphicsEffect(graphicsOpacityEffect);
    graphicsOpacityEffect->setOpacity(0.4);
    setToolTip(QLatin1String("Properties"));
}


void CollapseButton::setOpacity(qreal opacity)
{
    qobject_cast<QGraphicsOpacityEffect*>(graphicsEffect())->setOpacity(opacity);
}

void CollapseButton::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);
    QPainter p(this);

    if (m_hovered) {
        if (m_collapsed)
            p.drawPixmap(0,0, m_pixmap_hover_mirrored);
        else
            p.drawPixmap(0,0, m_pixmap_hover);
    } else {
        if (m_collapsed)
            p.drawPixmap(0,0, m_pixmap_normal_mirrored);
        else
            p.drawPixmap(0,0, m_pixmap_normal);
    }
}

930
931
} //QmlDesigner