-
-
Notifications
You must be signed in to change notification settings - Fork 165
/
scenepluginmodel.cpp
175 lines (151 loc) · 4.35 KB
/
scenepluginmodel.cpp
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
/******************************************************************************
This source file is part of the Avogadro project.
This source code is released under the 3-Clause BSD License, (see "LICENSE").
******************************************************************************/
#include "scenepluginmodel.h"
#include "sceneplugin.h"
namespace Avogadro::QtGui {
ScenePluginModel::ScenePluginModel(QObject* parent_)
: QAbstractItemModel(parent_)
{
}
QModelIndex ScenePluginModel::parent(const QModelIndex&) const
{
return QModelIndex();
}
int ScenePluginModel::rowCount(const QModelIndex& parent_) const
{
if (parent_.isValid())
return 0;
else
return m_scenePlugins.size();
}
int ScenePluginModel::columnCount(const QModelIndex&) const
{
return 2;
}
Qt::ItemFlags ScenePluginModel::flags(const QModelIndex& index_) const
{
if (index_.column() == 0)
return Qt::ItemIsEditable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
else
return Qt::ItemIsEnabled;
}
bool ScenePluginModel::setData(const QModelIndex& index_, const QVariant& value,
int role)
{
if (!index_.isValid() || index_.column() > 1)
return false;
auto* item =
qobject_cast<ScenePlugin*>(static_cast<QObject*>(index_.internalPointer()));
if (!item)
return false;
switch (role) {
case Qt::CheckStateRole:
if (value == Qt::Checked && !item->isActiveLayerEnabled()) {
item->setEnabled(true);
emit pluginStateChanged(item);
} else if (value == Qt::Unchecked && item->isActiveLayerEnabled()) {
item->setEnabled(false);
emit pluginStateChanged(item);
}
emit dataChanged(index_, index_);
return true;
}
return false;
}
QVariant ScenePluginModel::data(const QModelIndex& index_, int role) const
{
if (!index_.isValid() || index_.column() > 2)
return QVariant();
auto* object = static_cast<QObject*>(index_.internalPointer());
auto* item = qobject_cast<ScenePlugin*>(object);
if (!item)
return QVariant();
// Simple lambda to convert QFlags to variant as in Qt 6 this needs help.
auto toVariant = [&](auto flags) {
return static_cast<Qt::Alignment::Int>(flags);
};
// check if setupWidget() returns something
if (index_.column() == 1) {
switch (role) {
case Qt::DisplayRole:
case Qt::EditRole:
return (item->hasSetupWidget()) ? "•••" : " ";
case Qt::ToolTipRole:
case Qt::WhatsThisRole:
return tr("Settings");
case Qt::TextAlignmentRole:
return toVariant(Qt::AlignLeft | Qt::AlignVCenter);
default:
return QVariant();
}
}
if (index_.column() == 0) {
switch (role) {
case Qt::DisplayRole:
case Qt::EditRole:
return item->name();
case Qt::CheckStateRole:
if (item->isActiveLayerEnabled())
return Qt::Checked;
else
return Qt::Unchecked;
case Qt::ToolTipRole:
case Qt::WhatsThisRole:
return item->description();
case Qt::TextAlignmentRole:
return toVariant(Qt::AlignLeft);
default:
return QVariant();
}
}
return QVariant();
}
QModelIndex ScenePluginModel::index(int row, int column,
const QModelIndex& parent_) const
{
if (!parent_.isValid() && row >= 0 && row < m_scenePlugins.size())
return createIndex(row, column, m_scenePlugins[row]);
else
return QModelIndex();
}
void ScenePluginModel::clear()
{
m_scenePlugins.clear();
}
QList<ScenePlugin*> ScenePluginModel::scenePlugins() const
{
return m_scenePlugins;
}
QList<ScenePlugin*> ScenePluginModel::activeScenePlugins() const
{
QList<ScenePlugin*> result;
foreach (ScenePlugin* plugin, m_scenePlugins) {
if (plugin->isEnabled())
result << plugin;
}
return result;
}
void ScenePluginModel::addItem(ScenePlugin* item)
{
if (!m_scenePlugins.contains(item)) {
m_scenePlugins.append(item);
item->setParent(this);
connect(item, SIGNAL(drawablesChanged()), SIGNAL(pluginConfigChanged()));
}
}
void ScenePluginModel::removeItem(ScenePlugin* item)
{
m_scenePlugins.removeAll(item);
}
void ScenePluginModel::itemChanged()
{
auto* item = qobject_cast<ScenePlugin*>(sender());
if (item) {
int row = m_scenePlugins.indexOf(item);
if (row >= 0)
emit dataChanged(createIndex(row, 0), createIndex(row, 0));
}
}
} // namespace Avogadro::QtGui