aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/eventlist/eventlist.cpp
blob: 33c64e61103ad614b997b8efd881daf4cf58b767 (plain)
1
2
3
4
5
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
#include "eventlist.h"
#include "eventlistpluginview.h"
#include "eventlistview.h"
#include "nodelistview.h"

#include "bindingproperty.h"
#include "metainfo.h"
#include "projectexplorer/project.h"
#include "projectexplorer/session.h"
#include "qmldesignerplugin.h"
#include "signalhandlerproperty.h"
#include "utils/fileutils.h"
#include "utils/qtcassert.h"
#include "variantproperty.h"

#include <QDirIterator>
#include <QStandardItemModel>

namespace QmlDesigner {

Utils::FilePath projectFilePath()
{
    if (auto *doc = QmlDesignerPlugin::instance()->documentManager().currentDesignDocument()) {
        if (auto *proj = ProjectExplorer::SessionManager::projectForFile(doc->fileName()))
            return proj->projectDirectory();
    }
    return Utils::FilePath();
}

static Utils::FilePath findFile(const Utils::FilePath &path, const QString &fileName)
{
    QDirIterator it(path.toString(), QDirIterator::Subdirectories);

    while (it.hasNext()) {
        QFileInfo file(it.next());
        if (file.isDir())
            continue;

        if (file.fileName() == fileName)
            return Utils::FilePath::fromFileInfo(file);
    }
    return {};
}


NodeListView *EventList::st_nodeView = nullptr;

void EventList::setNodeProperties(AbstractView *view)
{
    st_nodeView = new NodeListView(view);
}

void EventList::selectNode(int internalId)
{
    if (st_nodeView)
        st_nodeView->selectNode(internalId);
}

int EventList::currentNode()
{
    if (st_nodeView)
        return st_nodeView->currentNode();

    return -1;
}

bool EventList::hasEventListModel()
{
    Utils::FilePath projectPath = projectFilePath();
    if (projectPath.isEmpty())
        return false;

    Utils::FilePath path = findFile(projectPath, "EventListModel.qml");
    return path.exists();
}

void EventList::addEventIdToCurrent(const QString &eventId)
{
    int iid = currentNode();
    if (st_nodeView && iid >= 0)
        st_nodeView->addEventId(iid, eventId);
}

void EventList::removeEventIdFromCurrent(const QString &eventId)
{
    int iid = currentNode();
    if (st_nodeView && iid >= 0)
        st_nodeView->removeEventIds(iid, {eventId});
}

QString EventList::setNodeId(int internalId, const QString &id)
{
    if (st_nodeView)
        return st_nodeView->setNodeId(internalId, id);

    return QString();
}

QStandardItemModel *EventList::nodeModel()
{
    if (st_nodeView)
        return st_nodeView->itemModel();

    return nullptr;
}

NodeListView *EventList::nodeListView()
{
    return st_nodeView;
}

ModelNode EventList::modelNode(const QString &id)
{
    if (st_nodeView)
        return st_nodeView->modelNodeForId(id);

    return ModelNode();
}

void EventList::setSignalSource(SignalHandlerProperty &prop, const QString &source)
{
    if (st_nodeView) {
        QmlDesigner::Import import =
            QmlDesigner::Import::createLibraryImport("QtQuick.Studio.EventSystem", "1.0");

        if (!st_nodeView->model()->hasImport(import, true, true)) {
            try {
                st_nodeView->model()->changeImports({import}, {});
            } catch (const QmlDesigner::Exception &) {
                QTC_ASSERT(false, return );
            }
        }

        if (source == "{}") {
            if (ModelNode node = prop.parentModelNode(); node.isValid()) {
                st_nodeView->executeInTransaction("EventList::removeProperty",
                    [&]() { node.removeProperty(prop.name()); });
            }
        }
        else {
            st_nodeView->executeInTransaction("EventList::setSource",
                [&]() { prop.setSource(source); });
        }
    }
}

EventList::EventList()
    : m_model(nullptr)
    , m_eventView(nullptr)
    , m_path()

{}

EventList::~EventList() = default;

Model *EventList::model() const
{
    return m_model.get();
}

EventListView *EventList::view() const
{
    return m_eventView.get();
}

QString EventList::read() const
{
    if (!m_path.exists())
        return QString();

    Utils::FileReader reader;
    QTC_ASSERT(reader.fetch(m_path), return QString());

    return QString::fromUtf8(reader.data());
}


void EventList::initialize(EventListPluginView *parent)
{
    Utils::FilePath projectPath = projectFilePath();
    QTC_ASSERT(!projectPath.isEmpty(), return );
    m_path = findFile(projectPath, "EventListModel.qml");

    if (!m_model) {
        QByteArray unqualifiedTypeName = "ListModel";
        auto metaInfo = parent->model()->metaInfo(unqualifiedTypeName);

        QByteArray fullTypeName = metaInfo.typeName();
        int minorVersion = metaInfo.minorVersion();
        int majorVersion = metaInfo.majorVersion();

        m_model = Model::create(fullTypeName, majorVersion, minorVersion);
        m_model->setParent(parent);
    }

    if (!m_eventView) {
        m_eventView = std::make_unique<EventListView>(m_model.get());
        m_model->attachView(m_eventView.get());
    }
}

void EventList::write(const QString &text)
{
    if (!m_path.exists())
        return;

    Utils::FileSaver writer(m_path);
    writer.write(text.toUtf8());
    writer.finalize();
}

} // namespace QmlDesigner.