Debian Codegraph @

avogadro/qtgui/moleculemodel.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 "moleculemodel.h"
7 #include "molecule.h"
8
9 #include <QtCore/QFileInfo>
10 #include <QtGui/QColor>
11 #include <QtGui/QFont>
12 #include <QtGui/QIcon>
13 #include <QtGui/QPalette>
14
15 namespace Avogadro::QtGui {
16
17 MoleculeModel::MoleculeModel(QObject* p)
18 : QAbstractItemModel(p), m_activeMolecule(nullptr)
19 {
20 const QPalette defaultPalette;
21 bool darkMode = (defaultPalette.color(QPalette::WindowText).lightness() >
22 defaultPalette.color(QPalette::Window).lightness());
23 loadIcons(darkMode);
24 }
25
26 QModelIndex MoleculeModel::parent(const QModelIndex&) const
27 {
28 return QModelIndex();
29 }
30
31 void MoleculeModel::loadIcons(bool darkMode)
32 {
33 QString iconPath = ":icons/fallback/32x32/";
34 QString plusIconPath =
35 iconPath + (darkMode ? "plus-dark.png" : "plus-light.png");
36 QString closeIconPath =
37 iconPath + (darkMode ? "cross-dark.png" : "cross-light.png");
38
39 m_plusIcon = QIcon(plusIconPath);
40 m_closeIcon = QIcon(closeIconPath);
41 }
42
43 int MoleculeModel::rowCount(const QModelIndex& p) const
44 {
45 if (p.isValid())
46 return 0;
47 else
48 return m_molecules.size() + 1;
49 }
50
51 int MoleculeModel::columnCount(const QModelIndex&) const
52 {
53 return 2;
54 }
55
56 Qt::ItemFlags MoleculeModel::flags(const QModelIndex& idx) const
57 {
58
59 if (idx.row() == m_molecules.size())
60 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
61 if (idx.column() == 0)
62 return static_cast<Qt::ItemFlags>(Qt::ItemIsEditable | Qt::ItemIsEnabled);
63 return Qt::ItemIsEnabled;
64 }
65
66 bool MoleculeModel::setData(const QModelIndex& idx, const QVariant& value,
67 int role)
68 {
69 if (!idx.isValid() || idx.column() > 2)
70 return false;
71
72 auto* object = static_cast<QObject*>(idx.internalPointer());
73 auto* mol = qobject_cast<Molecule*>(object);
74 if (!mol)
75 return false;
76
77 switch (role) {
78 case Qt::CheckStateRole:
79 m_activeMolecule = mol;
80 if (value == Qt::Checked /*&& !item->isEnabled()*/) {
81 // item->setEnabled(true);
82 emit moleculeStateChanged(mol);
83 } else if (value == Qt::Unchecked /*&& item->isEnabled()*/) {
84 // item->setEnabled(false);
85 emit moleculeStateChanged(mol);
86 }
87 emit dataChanged(idx, idx);
88 return true;
89 case Qt::EditRole:
90 if (!value.toString().isEmpty()) {
91 // don't set an empty name
92 mol->setData("name", std::string(value.toString().toLatin1()));
93 emit dataChanged(idx, idx);
94 }
95 return true;
96 }
97 return false;
98 }
99
100 QVariant MoleculeModel::data(const QModelIndex& idx, int role) const
101 {
102 if (!idx.isValid() || idx.column() > 2)
103 return QVariant();
104
105 auto* object = static_cast<QObject*>(idx.internalPointer());
106 auto* mol = qobject_cast<Molecule*>(object);
107
108 if (idx.row() == m_molecules.size()) {
109 if (idx.column() == 0 && role == Qt::DecorationRole) {
110 return m_plusIcon;
111 }
112 return QVariant();
113 }
114
115 if (!mol)
116 return QVariant();
117
118 if (idx.column() == 0) {
119 switch (role) {
120 case Qt::DisplayRole: {
121 std::string name = tr("Untitled").toStdString();
122 if (mol && mol->hasData("name") &&
123 !(mol->data("name").toString().empty())) {
124 // don't set an empty name
125 name = mol->data("name").toString();
126 } else if (mol && mol->hasData("fileName")) {
127 name = QFileInfo(mol->data("fileName").toString().c_str())
128 .fileName()
129 .toStdString();
130 }
131 if (mol)
132 return QString("%1 (%2)")
133 .arg(QString::fromStdString(name))
134 .arg(mol->formattedFormula());
135 else
136 return tr("Edit molecule");
137 }
138 case Qt::EditRole:
139 return mol->data("name").toString().c_str();
140 case Qt::ToolTipRole:
141 if (mol->hasData("fileName"))
142 return mol->data("fileName").toString().c_str();
143 return tr("Not saved");
144 case Qt::WhatsThisRole:
145 return mol->formula().c_str();
146 case Qt::FontRole:
147 if (mol == m_activeMolecule) {
148 QFont font;
149 font.setBold(true);
150 return font;
151 }
152 return QVariant();
153 default:
154 return QVariant();
155 }
156 } else if (idx.column() == 1) {
157 if (role == Qt::DecorationRole)
158 return m_closeIcon;
159 }
160 return QVariant();
161 }
162
163 QModelIndex MoleculeModel::index(int row, int column,
164 const QModelIndex& p) const
165 {
166 if (!p.isValid())
167 if (row >= 0 && row < m_molecules.size()) {
168 return createIndex(row, column, m_molecules[row]);
169 }
170 if (row == m_molecules.size()) {
171 return createIndex(row, column, nullptr);
172 }
173 return QModelIndex();
174 }
175
176 void MoleculeModel::clear()
177 {
178 m_molecules.clear();
179 }
180
181 QList<Molecule*> MoleculeModel::molecules() const
182 {
183 return m_molecules;
184 }
185
186 QList<Molecule*> MoleculeModel::activeMolecules() const
187 {
188 QList<Molecule*> result;
189 foreach (Molecule* mol, m_molecules) {
190 if (true)
191 result << mol;
192 }
193 return result;
194 }
195
196 void MoleculeModel::setActiveMolecule(QObject* active)
197 {
198 if (m_activeMolecule == active)
199 return;
200 m_activeMolecule = active;
201 emit dataChanged(createIndex(0, 0), createIndex(m_molecules.size(), 0));
202 }
203
204 void MoleculeModel::addItem(Molecule* item)
205 {
206 if (!m_molecules.contains(item)) {
207 int row = m_molecules.size();
208 beginInsertRows(QModelIndex(), row, row);
209 m_molecules.append(item);
210 item->setParent(this);
211 endInsertRows();
212 }
213 }
214
215 void MoleculeModel::removeItem(Molecule* item)
216 {
217 if (m_molecules.contains(item)) {
218 int row = m_molecules.indexOf(item);
219 beginRemoveRows(QModelIndex(), row, row);
220 m_molecules.removeAt(row);
221 // Do we want strong ownership of molecules?
222 item->deleteLater();
223 endRemoveRows();
224 }
225 }
226
227 void MoleculeModel::itemChanged()
228 {
229 auto* item = qobject_cast<Molecule*>(sender());
230 if (item) {
231 int row = m_molecules.indexOf(item);
232 if (row >= 0)
233 emit dataChanged(createIndex(row, 0), createIndex(row, 0));
234 }
235 }
236
237 } // namespace Avogadro::QtGui