stateseditorwidget.cpp 4.79 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8
9
10
11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12
13
14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16
17
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
18
19
20
21
22
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
con's avatar
con committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
25
26
27
28

#include "stateseditorwidget.h"
#include "stateseditormodel.h"
#include "stateseditorview.h"
29
#include "stateseditorimageprovider.h"
30

31
32
#include <theming.h>

33
34
#include <invalidqmlsourceexception.h>

35
#include <coreplugin/icore.h>
36

37
#include <utils/qtcassert.h>
38
#include <utils/stylehelper.h>
39

40
41
42
43
#include <QApplication>

#include <QFileInfo>
#include <QShortcut>
44
#include <QBoxLayout>
45
#include <QKeySequence>
46

47
48
49
#include <QQmlContext>
#include <QQmlEngine>
#include <QQuickItem>
50
51
52
53
54
55
56

enum {
    debug = false
};

namespace QmlDesigner {

57
int StatesEditorWidget::currentStateInternalId() const
58
{
59
60
    Q_ASSERT(rootObject());
    Q_ASSERT(rootObject()->property("currentStateInternalId").isValid());
61

62
    return rootObject()->property("currentStateInternalId").toInt();
63
64
}

65
void StatesEditorWidget::setCurrentStateInternalId(int internalId)
66
{
67
    rootObject()->setProperty("currentStateInternalId", internalId);
68
69
}

70
void StatesEditorWidget::setNodeInstanceView(NodeInstanceView *nodeInstanceView)
71
{
72
73
    m_imageProvider->setNodeInstanceView(nodeInstanceView);
}
74

75
76
void StatesEditorWidget::showAddNewStatesButton(bool showAddNewStatesButton)
{
77
    rootContext()->setContextProperty(QLatin1String("canAddNewStates"), showAddNewStatesButton);
78
79
}

80
StatesEditorWidget::StatesEditorWidget(StatesEditorView *statesEditorView, StatesEditorModel *statesEditorModel)
81
    : QQuickWidget(),
82
83
84
      m_statesEditorView(statesEditorView),
      m_imageProvider(0),
      m_qmlSourceUpdateShortcut(0)
85
86
87
{
    m_imageProvider = new Internal::StatesEditorImageProvider;
    m_imageProvider->setNodeInstanceView(statesEditorView->nodeInstanceView());
88

89
90
    engine()->addImageProvider(QStringLiteral("qmldesigner_stateseditor"), m_imageProvider);
    engine()->addImportPath(qmlSourcesPath());
91

92
93
94
    m_qmlSourceUpdateShortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_F4), this);
    connect(m_qmlSourceUpdateShortcut, SIGNAL(activated()), this, SLOT(reloadQmlSource()));

95
96
    setResizeMode(QQuickWidget::SizeRootObjectToView);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
97

98
    rootContext()->setContextProperty(QStringLiteral("statesEditorModel"), statesEditorModel);
99

100
    rootContext()->setContextProperty(QLatin1String("canAddNewStates"), true);
101

102
103
104
    Theming::insertTheme(&m_themeProperties);
    rootContext()->setContextProperty(QLatin1String("creatorTheme"), &m_themeProperties);

105
    Theming::registerIconProvider(engine());
106

107
    setWindowTitle(tr("States", "Title of Editor widget"));
108
109
110

    // init the first load of the QML UI elements
    reloadQmlSource();
111
112
113
114
115
116
}

StatesEditorWidget::~StatesEditorWidget()
{
}

117
QString StatesEditorWidget::qmlSourcesPath() {
118
119
    return Core::ICore::resourcePath() + QStringLiteral("/qmldesigner/statesEditorQmlSources");
}
120

121
122
void StatesEditorWidget::reloadQmlSource()
{
123
    QString statesListQmlFilePath = qmlSourcesPath() + QStringLiteral("/StatesList.qml");
124
    QTC_ASSERT(QFileInfo::exists(statesListQmlFilePath), return);
125
126
    engine()->clearComponentCache();
    setSource(QUrl::fromLocalFile(statesListQmlFilePath));
127

128
129
130
131
    QTC_ASSERT(rootObject(), return);
    connect(rootObject(), SIGNAL(currentStateInternalIdChanged()), m_statesEditorView.data(), SLOT(synchonizeCurrentStateFromWidget()));
    connect(rootObject(), SIGNAL(createNewState()), m_statesEditorView.data(), SLOT(createNewState()));
    connect(rootObject(), SIGNAL(deleteState(int)), m_statesEditorView.data(), SLOT(removeState(int)));
132
    m_statesEditorView.data()->synchonizeCurrentStateFromWidget();
133
    setFixedHeight(initialSize().height());
134

135
    connect(rootObject(), SIGNAL(expandedChanged()), this, SLOT(changeHeight()));
136
137
138
139
}

void StatesEditorWidget::changeHeight()
{
140
    setFixedHeight(rootObject()->height());
141
}
142
}