aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/eventlist/assigneventdialog.cpp
blob: 7e7a4b1287e1190661273db633888a854c74f88b (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
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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
** 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.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** 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.
**
****************************************************************************/

#include "assigneventdialog.h"
#include "eventlist.h"
#include "eventlistdelegate.h"
#include "eventlistview.h"
#include "filterlinewidget.h"
#include "nodelistdelegate.h"
#include "nodelistview.h"
#include "nodeselectionmodel.h"
#include "eventlistutils.h"

#include <QHBoxLayout>
#include <QHeaderView>
#include <QMessageBox>
#include <QSortFilterProxyModel>
#include <QSplitter>
#include <QTableView>
#include <QVBoxLayout>

namespace QmlDesigner {

AssignEventDialog::AssignEventDialog(QWidget *parent)
    : QDialog(parent)
    , m_nodeTable(new QTableView)
    , m_eventTable(new QTableView)
    , m_nodeLine(new FilterLineWidget())
    , m_eventLine(new FilterLineWidget())
{
    setWindowFlag(Qt::Tool, true);
    setModal(true);

    auto *nodeFilterModel = new QSortFilterProxyModel;
    auto *nodeListDelegate = new NodeListDelegate(m_nodeTable);
    auto *nodeSelectionModel = new NodeSelectionModel(nodeFilterModel);
    m_nodeTable->installEventFilter(new TabWalker(this));
    m_nodeTable->setItemDelegate(nodeListDelegate);
    m_nodeTable->setModel(nodeFilterModel);
    m_nodeTable->setSelectionModel(nodeSelectionModel);
    m_nodeTable->setFocusPolicy(Qt::NoFocus);
    m_nodeTable->setSelectionMode(QAbstractItemView::SingleSelection);
    m_nodeTable->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_nodeTable->resizeColumnsToContents();
    m_nodeTable->horizontalHeader()->setStretchLastSection(true);
    m_nodeTable->verticalHeader()->hide();
    polishPalette(m_nodeTable, "#1f75cc");

    auto *eventFilterModel = new QSortFilterProxyModel;
    auto *eventListDelegate = new EventListDelegate(m_eventTable);
    m_eventTable->installEventFilter(new TabWalker(this));
    m_eventTable->setItemDelegate(eventListDelegate);
    m_eventTable->setFocusPolicy(Qt::NoFocus);
    m_eventTable->setSelectionMode(QAbstractItemView::NoSelection);
    m_eventTable->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_eventTable->setModel(eventFilterModel);
    m_eventTable->verticalHeader()->hide();
    polishPalette(m_eventTable, QColor("#d87b00"));

    auto *nodeBox = new QVBoxLayout;
    nodeBox->addWidget(m_nodeLine);
    nodeBox->addWidget(m_nodeTable);

    QWidget *nodeWidget = new QWidget;
    nodeWidget->setLayout(nodeBox);

    auto *eventBox = new QVBoxLayout;
    eventBox->addWidget(m_eventLine);
    eventBox->addWidget(m_eventTable);

    auto *eventWidget = new QWidget;
    eventWidget->setLayout(eventBox);

    auto *splitter = new QSplitter(Qt::Horizontal);
    splitter->addWidget(nodeWidget);
    splitter->addWidget(eventWidget);
    splitter->setStretchFactor(0, 1);
    splitter->setStretchFactor(1, 3);

    auto *box = new QHBoxLayout;
    box->addWidget(splitter);
    setLayout(box);

    connect(m_nodeLine, &FilterLineWidget::filterChanged, [this](const QString &str) {
        if (auto *sm = qobject_cast<NodeSelectionModel *>(m_nodeTable->selectionModel())) {
            sm->storeSelection();
            if (auto *fm = qobject_cast<QSortFilterProxyModel *>(m_nodeTable->model()))
                fm->setFilterFixedString(str);
            sm->reselect();
        }
    });

    connect(m_eventLine, &FilterLineWidget::filterChanged, [this](const QString &str) {
        if (auto *fm = qobject_cast<QSortFilterProxyModel *>(m_eventTable->model()))
            fm->setFilterFixedString(str);
    });

    connect(eventListDelegate, &EventListDelegate::connectClicked,
            [](const QString &id, bool connected) {
                if (connected)
                    EventList::addEventIdToCurrent(id);
                else
                    EventList::removeEventIdFromCurrent(id);
            });
}

void AssignEventDialog::initialize(EventList &events)
{
    m_nodeLine->clear();
    m_eventLine->clear();

    if (auto *fm = qobject_cast<QSortFilterProxyModel *>(m_nodeTable->model()))
        fm->setSourceModel(events.nodeModel());

    if (auto *fm = qobject_cast<QSortFilterProxyModel *>(m_eventTable->model()))
        fm->setSourceModel(events.view()->eventListModel());

    if (auto *sm = qobject_cast<NodeSelectionModel *>(m_nodeTable->selectionModel())) {
        if (m_connection)
            sm->disconnect(m_connection);

        auto updateEventListView = [this, &events](const QStringList &eventIds) {
            auto nonExistent = events.view()->eventListModel()->connectEvents(eventIds);

            if (!nonExistent.empty()) {
                QString header(tr("Nonexistent events discovered"));
                QString msg(tr("The Node references the following nonexistent events:\n"));
                for (auto &&id : nonExistent)
                    msg += id + ", ";
                msg.remove(msg.size() - 2, 2);
                msg += "\nDo you want to remove these references?";

                if (QMessageBox::Yes == QMessageBox::question(this, header, msg)) {
                    auto *view = events.nodeListView();
                    view->removeEventIds(view->currentNode(), nonExistent);
                    view->reset();
                    if (auto *sm = qobject_cast<NodeSelectionModel *>(m_nodeTable->selectionModel()))
                        sm->selectNode(view->currentNode());
                }
            }
            m_eventTable->update();
        };
        m_connection = connect(sm, &NodeSelectionModel::nodeSelected, updateEventListView);
    }

    m_nodeTable->setColumnHidden(NodeListModel::typeColumn, true);
    m_nodeTable->setColumnHidden(NodeListModel::fromColumn, true);
    m_nodeTable->setColumnHidden(NodeListModel::toColumn, true);

    if (QHeaderView *header = m_eventTable->horizontalHeader()) {
        header->setSectionResizeMode(EventListModel::idColumn, QHeaderView::Stretch);
        header->setSectionResizeMode(EventListModel::descriptionColumn, QHeaderView::Stretch);
        header->setSectionResizeMode(EventListModel::shortcutColumn, QHeaderView::Stretch);
        header->resizeSection(EventListModel::connectColumn, 120);
        header->setStretchLastSection(false);
    }
}

void AssignEventDialog::postShow()
{
    if (auto *sm = qobject_cast<NodeSelectionModel *>(m_nodeTable->selectionModel()))
        sm->selectNode(EventList::currentNode());

    resize(QSize(700, 300));
}

} // namespace QmlDesigner.