| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "scenepluginmodel.h" |
| 7 |
|
| 8 |
#include "sceneplugin.h" |
| 9 |
|
| 10 |
namespace Avogadro::QtGui { |
| 11 |
|
| 12 |
ScenePluginModel::ScenePluginModel(QObject* parent_) |
| 13 |
: QAbstractItemModel(parent_) |
| 14 |
{ |
| 15 |
} |
| 16 |
|
| 17 |
QModelIndex ScenePluginModel::parent(const QModelIndex&) const |
| 18 |
{ |
| 19 |
return QModelIndex(); |
| 20 |
} |
| 21 |
|
| 22 |
int ScenePluginModel::rowCount(const QModelIndex& parent_) const |
| 23 |
{ |
| 24 |
if (parent_.isValid()) |
| 25 |
return 0; |
| 26 |
else |
| 27 |
return m_scenePlugins.size(); |
| 28 |
} |
| 29 |
|
| 30 |
int ScenePluginModel::columnCount(const QModelIndex&) const |
| 31 |
{ |
| 32 |
return 2; |
| 33 |
} |
| 34 |
|
| 35 |
Qt::ItemFlags ScenePluginModel::flags(const QModelIndex& index_) const |
| 36 |
{ |
| 37 |
auto* item = |
| 38 |
qobject_cast<ScenePlugin*>(static_cast<QObject*>(index_.internalPointer())); |
| 39 |
if (!item) |
| 40 |
return Qt::NoItemFlags; |
| 41 |
|
| 42 |
Qt::ItemFlags flags = Qt::ItemIsEnabled; |
| 43 |
if (!item->isApplicable()) |
| 44 |
flags = Qt::NoItemFlags; |
| 45 |
|
| 46 |
if (index_.column() == 0) |
| 47 |
return flags | Qt::ItemIsEditable | Qt::ItemIsUserCheckable; |
| 48 |
else |
| 49 |
return flags; |
| 50 |
} |
| 51 |
|
| 52 |
bool ScenePluginModel::setData(const QModelIndex& index_, const QVariant& value, |
| 53 |
int role) |
| 54 |
{ |
| 55 |
if (!index_.isValid() || index_.column() > 1) |
| 56 |
return false; |
| 57 |
|
| 58 |
auto* item = |
| 59 |
qobject_cast<ScenePlugin*>(static_cast<QObject*>(index_.internalPointer())); |
| 60 |
if (!item) |
| 61 |
return false; |
| 62 |
|
| 63 |
switch (role) { |
| 64 |
case Qt::CheckStateRole: |
| 65 |
if (value == Qt::Checked && !item->isActiveLayerEnabled()) { |
| 66 |
item->setEnabled(true); |
| 67 |
emit pluginStateChanged(item); |
| 68 |
} else if (value == Qt::Unchecked && item->isActiveLayerEnabled()) { |
| 69 |
item->setEnabled(false); |
| 70 |
emit pluginStateChanged(item); |
| 71 |
} |
| 72 |
emit dataChanged(index_, index_); |
| 73 |
return true; |
| 74 |
} |
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
QVariant ScenePluginModel::data(const QModelIndex& index_, int role) const |
| 79 |
{ |
| 80 |
if (!index_.isValid() || index_.column() > 2) |
| 81 |
return QVariant(); |
| 82 |
|
| 83 |
auto* object = static_cast<QObject*>(index_.internalPointer()); |
| 84 |
auto* item = qobject_cast<ScenePlugin*>(object); |
| 85 |
if (!item) |
| 86 |
return QVariant(); |
| 87 |
|
| 88 |
// Simple lambda to convert QFlags to variant as in Qt 6 this needs help. |
| 89 |
auto toVariant = [&](auto flags) { |
| 90 |
return static_cast<Qt::Alignment::Int>(flags); |
| 91 |
}; |
| 92 |
|
| 93 |
// check if setupWidget() returns something |
| 94 |
if (index_.column() == 1) { |
| 95 |
switch (role) { |
| 96 |
case Qt::DisplayRole: |
| 97 |
case Qt::EditRole: |
| 98 |
return (item->hasSetupWidget()) ? "•••" : " "; |
| 99 |
case Qt::ToolTipRole: |
| 100 |
case Qt::WhatsThisRole: |
| 101 |
return tr("Settings"); |
| 102 |
case Qt::TextAlignmentRole: |
| 103 |
return toVariant(Qt::AlignLeft | Qt::AlignVCenter); |
| 104 |
default: |
| 105 |
return QVariant(); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
if (index_.column() == 0) { |
| 110 |
switch (role) { |
| 111 |
case Qt::DisplayRole: |
| 112 |
case Qt::EditRole: |
| 113 |
return item->name(); |
| 114 |
case Qt::CheckStateRole: |
| 115 |
if (item->isActiveLayerEnabled()) |
| 116 |
return Qt::Checked; |
| 117 |
else |
| 118 |
return Qt::Unchecked; |
| 119 |
case Qt::ToolTipRole: |
| 120 |
case Qt::WhatsThisRole: |
| 121 |
return item->description(); |
| 122 |
case Qt::TextAlignmentRole: |
| 123 |
return toVariant(Qt::AlignLeft); |
| 124 |
default: |
| 125 |
return QVariant(); |
| 126 |
} |
| 127 |
} |
| 128 |
return QVariant(); |
| 129 |
} |
| 130 |
|
| 131 |
QModelIndex ScenePluginModel::index(int row, int column, |
| 132 |
const QModelIndex& parent_) const |
| 133 |
{ |
| 134 |
if (!parent_.isValid() && row >= 0 && row < m_scenePlugins.size()) |
| 135 |
return createIndex(row, column, m_scenePlugins[row]); |
| 136 |
else |
| 137 |
return QModelIndex(); |
| 138 |
} |
| 139 |
|
| 140 |
void ScenePluginModel::clear() |
| 141 |
{ |
| 142 |
m_scenePlugins.clear(); |
| 143 |
} |
| 144 |
|
| 145 |
ScenePlugin* ScenePluginModel::scenePlugin(const QModelIndex& index) const |
| 146 |
{ |
| 147 |
if (!index.isValid() || index.column() != 0) |
| 148 |
return nullptr; |
| 149 |
|
| 150 |
return m_scenePlugins[index.row()]; |
| 151 |
} |
| 152 |
|
| 153 |
ScenePlugin* ScenePluginModel::scenePlugin(int row) const |
| 154 |
{ |
| 155 |
if (row < 0 || row >= m_scenePlugins.size()) |
| 156 |
return nullptr; |
| 157 |
|
| 158 |
return m_scenePlugins[row]; |
| 159 |
} |
| 160 |
|
| 161 |
QList<ScenePlugin*> ScenePluginModel::scenePlugins() const |
| 162 |
{ |
| 163 |
return m_scenePlugins; |
| 164 |
} |
| 165 |
|
| 166 |
QList<ScenePlugin*> ScenePluginModel::activeScenePlugins() const |
| 167 |
{ |
| 168 |
QList<ScenePlugin*> result; |
| 169 |
foreach (ScenePlugin* plugin, m_scenePlugins) { |
| 170 |
if (plugin->isEnabled()) |
| 171 |
result << plugin; |
| 172 |
} |
| 173 |
return result; |
| 174 |
} |
| 175 |
|
| 176 |
void ScenePluginModel::addItem(ScenePlugin* item) |
| 177 |
{ |
| 178 |
if (!m_scenePlugins.contains(item)) { |
| 179 |
m_scenePlugins.append(item); |
| 180 |
item->setParent(this); |
| 181 |
connect(item, SIGNAL(drawablesChanged()), SIGNAL(pluginConfigChanged())); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
void ScenePluginModel::removeItem(ScenePlugin* item) |
| 186 |
{ |
| 187 |
m_scenePlugins.removeAll(item); |
| 188 |
} |
| 189 |
|
| 190 |
void ScenePluginModel::itemChanged() |
| 191 |
{ |
| 192 |
auto* item = qobject_cast<ScenePlugin*>(sender()); |
| 193 |
if (item) { |
| 194 |
int row = m_scenePlugins.indexOf(item); |
| 195 |
if (row >= 0) |
| 196 |
emit dataChanged(createIndex(row, 0), createIndex(row, 0)); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
} // namespace Avogadro::QtGui |