Debian Codegraph @

avogadro/qtgui/layermodel.cpp

raw
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 "layermodel.h"
7 #include "molecule.h"
8 #include "rwmolecule.h"
9
10 #include <QtCore/QDebug>
11 #include <QtCore/QFileInfo>
12 #include <QtGui/QColor>
13 #include <QtGui/QFont>
14 #include <QtGui/QIcon>
15 #include <QtGui/QPalette>
16
17 namespace Avogadro::QtGui {
18
19 using Core::LayerManager;
20
21 namespace {
22 const int QTTY_COLUMNS = 6;
23 }
24
25 LayerModel::LayerModel(QObject* p) : QAbstractItemModel(p), m_item(0)
26 {
27 // determine if need dark mode or light mode icons
28 // e.g. https://www.qt.io/blog/dark-mode-on-windows-11-with-qt-6.5
29 const QPalette defaultPalette;
30 // i.e., the text is lighter than the background
31 bool darkMode = (defaultPalette.color(QPalette::WindowText).lightness() >
32 defaultPalette.color(QPalette::Window).lightness());
33 loadIcons(darkMode);
34 }
35
36 void LayerModel::loadIcons(bool darkMode)
37 {
38 QString iconPath = ":icons/fallback/32x32/";
39
40 QString dashedPreviewIconPath = iconPath + "dashed-preview-light.png";
41 QString dotsIconPath = iconPath + "dots-light.png";
42 QString lockIconPath = iconPath + "lock-light.png";
43 QString openLockIconPath = iconPath + "lock-open-light.png";
44 QString plusIconPath = iconPath + "plus-light.png";
45 QString previewIconPath = iconPath + "preview-light.png";
46
47 if (darkMode) {
48 dashedPreviewIconPath = iconPath + "dashed-preview-dark.png";
49 dotsIconPath = iconPath + "dots-dark.png";
50 lockIconPath = iconPath + "lock-dark.png";
51 openLockIconPath = iconPath + "lock-open-dark.png";
52 plusIconPath = iconPath + "plus-dark.png";
53 previewIconPath = iconPath + "preview-dark.png";
54 }
55
56 m_plusIcon = QIcon::fromTheme("list-add", QIcon(plusIconPath));
57 m_removeIcon = QIcon::fromTheme(
58 "list-remove", QIcon(":/icons/fallback/32x32/edit-delete.png"));
59 m_dotsIcon = QIcon::fromTheme("view-more", QIcon(dotsIconPath));
60 m_previewIcon = QIcon::fromTheme("view-visible", QIcon(previewIconPath));
61 m_previewDashedIcon =
62 QIcon::fromTheme("view-hidden", QIcon(dashedPreviewIconPath));
63 m_lockIcon = QIcon::fromTheme("lock", QIcon(lockIconPath));
64 m_openLockIcon = QIcon::fromTheme("unlock", QIcon(openLockIconPath));
65 }
66
67 QModelIndex LayerModel::parent(const QModelIndex&) const
68 {
69 return QModelIndex();
70 }
71
72 int LayerModel::rowCount(const QModelIndex& p) const
73 {
74 if (p.isValid())
75 return 0;
76 else
77 return m_item;
78 }
79
80 int LayerModel::columnCount(const QModelIndex&) const
81 {
82 return QTTY_COLUMNS;
83 }
84
85 Qt::ItemFlags LayerModel::flags(const QModelIndex&) const
86 {
87 return Qt::ItemIsEnabled;
88 }
89
90 bool LayerModel::setData(const QModelIndex&, const QVariant&, int)
91 {
92 return false;
93 }
94
95 QVariant LayerModel::data(const QModelIndex& idx, int role) const
96 {
97 if (!idx.isValid() || idx.column() > QTTY_COLUMNS)
98 return QVariant();
99 auto names = activeMoleculeNames();
100 if (idx.row() == static_cast<int>(names.size())) {
101 if (idx.column() == 0) {
102 switch (role) {
103 case Qt::DecorationRole:
104 return m_plusIcon;
105 default:
106 return QVariant();
107 }
108 }
109 return QVariant();
110 }
111 auto name = names[idx.row()].second;
112 auto layer = names[idx.row()].first;
113 bool isLayer = (name == "Layer");
114 if (isLayer) {
115 if (idx.column() == ColumnType::Name) {
116 switch (role) {
117 case Qt::DisplayRole: {
118 return QString(tr("Layer %1"))
119 .arg(layer + 1); // count starts at 0 internally
120 }
121 case Qt::FontRole:
122 if (layer == getMoleculeLayer().activeLayer()) {
123 QFont font;
124 font.setBold(true);
125 return font;
126 } else {
127 return QVariant();
128 }
129 default:
130 return QVariant();
131 }
132 } else if (idx.column() == ColumnType::Menu) {
133 if (role == Qt::DecorationRole)
134 return m_dotsIcon;
135 } else if (idx.column() == ColumnType::Visible) {
136 if (role == Qt::DecorationRole) {
137 if (visible(layer))
138 return m_previewIcon;
139 else
140 return m_previewDashedIcon;
141 }
142 } else if (idx.column() == ColumnType::Lock) {
143 if (role == Qt::DecorationRole) {
144 if (locked(layer))
145 return m_lockIcon;
146 else
147 return m_openLockIcon;
148 }
149 } else if (idx.column() == ColumnType::Remove) {
150 if (role == Qt::DecorationRole)
151 return m_removeIcon;
152 }
153 } else {
154 if (idx.column() == ColumnType::Name) {
155 switch (role) {
156 case Qt::DisplayRole: {
157 return " " + getTranslatedName(name); // should already be translated
158 }
159 }
160 }
161 }
162
163 return QVariant();
164 }
165
166 QString LayerModel::getTranslatedName(const std::string& name) const
167 {
168 // This is a bad hack, but whatever..
169 // Put all the strings that show up as layer options
170
171 if (name == "Ball and Stick")
172 return tr("Ball and Stick");
173 else if (name == "Cartoons")
174 return tr("Cartoons", "protein ribbon / cartoon rendering");
175 else if (name == "Close Contacts")
176 return tr("Close Contacts", "rendering of non-covalent close contacts");
177 else if (name == "Crystal Lattice")
178 return tr("Crystal Lattice");
179 else if (name == "Force")
180 return tr("Force");
181 else if (name == "Labels")
182 return tr("Labels");
183 else if (name == "Licorice")
184 return tr("Licorice", "stick / licorice rendering");
185 else if (name == "Surfaces")
186 return tr("Surfaces");
187 else if (name == "Non-Covalent")
188 return tr("Non-Covalent");
189 else if (name == "QTAIM")
190 return tr("QTAIM", "quantum theory of atoms in molecules");
191 else if (name == "Symmetry Elements")
192 return tr("Symmetry Elements");
193 else if (name == "Van der Waals")
194 return tr("Van der Waals");
195 else if (name == "Van der Waals (AO)")
196 return tr("Van der Waals (AO)", "ambient occlusion");
197 else if (name == "Volumes")
198 return tr("Volumes", "volumetric clouds");
199 else if (name == "Wireframe")
200 return tr("Wireframe");
201 else if (name == "Reference Axes")
202 return tr("Reference Axes");
203
204 qDebug() << "LayerModel: name didn't match: " << name.c_str();
205
206 return QString(name.c_str());
207 }
208
209 QModelIndex LayerModel::index(int row, int column, const QModelIndex& p) const
210 {
211 if (!p.isValid())
212 if (row >= 0 && row <= static_cast<int>(m_item))
213 return createIndex(row, column);
214 return QModelIndex();
215 }
216
217 void LayerModel::addLayer(RWMolecule* rwmolecule)
218 {
219 addItem();
220 RWLayerManager::addLayer(rwmolecule);
221 }
222
223 void LayerModel::addItem()
224 {
225 beginInsertRows(QModelIndex(), m_item, m_item);
226 endInsertRows();
227 ++m_item;
228 }
229
230 void LayerModel::updateRows()
231 {
232 while (m_item > activeMoleculeNames().size()) {
233 beginRemoveRows(QModelIndex(), m_item, m_item);
234 endRemoveRows();
235 --m_item;
236 }
237 while (m_item <= activeMoleculeNames().size()) {
238 addItem();
239 }
240 emit dataChanged(createIndex(0, 0), createIndex(m_item, 0));
241 }
242
243 void LayerModel::addMolecule(const Molecule* mol)
244 {
245 RWLayerManager::addMolecule(mol);
246 m_item = 0;
247 updateRows();
248
249 connect(mol, &Molecule::changed, this, &LayerModel::updateRows);
250 }
251
252 void LayerModel::setActiveLayer(int index, RWMolecule* rwmolecule)
253 {
254 auto names = activeMoleculeNames();
255 assert(index < static_cast<int>(names.size()));
256 RWLayerManager::setActiveLayer(names[index].first, rwmolecule);
257 updateRows();
258 }
259 void LayerModel::removeItem(int row, RWMolecule* rwmolecule)
260 {
261 if (row <= static_cast<int>(m_item)) {
262 auto names = activeMoleculeNames();
263 removeLayer(static_cast<size_t>(names[row].first), rwmolecule);
264 updateRows();
265 }
266 }
267
268 size_t LayerModel::items() const
269 {
270 return m_item;
271 }
272
273 void LayerModel::flipVisible(size_t row)
274 {
275 auto names = activeMoleculeNames();
276 auto layer = names[row].first;
277 RWLayerManager::flipVisible(layer);
278 }
279 void LayerModel::flipLocked(size_t row)
280 {
281 auto names = activeMoleculeNames();
282 auto layer = names[row].first;
283 RWLayerManager::flipLocked(layer);
284 }
285
286 size_t LayerModel::layerCount() const
287 {
288 return LayerManager::layerCount();
289 }
290
291 } // namespace Avogadro::QtGui