Debian Codegraph @

avogadro/qtplugins/symmetry/symmetrywidget.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 "symmetrywidget.h"
7 #include "symmetryutil.h"
8 #include "ui_symmetrywidget.h"
9
10 #include <avogadro/qtgui/molecule.h>
11 #include <avogadro/qtgui/richtextdelegate.h>
12
13 #include <QtCore/QDebug>
14
15 #include <QtWidgets/QPlainTextEdit>
16
17 using Avogadro::QtGui::Molecule;
18 using Avogadro::QtGui::RichTextDelegate;
19
20 using namespace msym;
21 using namespace Avogadro::QtPlugins::SymmetryUtil;
22
23 namespace Avogadro::QtPlugins {
24
25 msym_thresholds_t tight_thresholds = { // all defaults
26 /*.zero =*/1.0e-3,
27 /*.geometry =*/1.0e-3,
28 /*.angle =*/1.0e-3,
29 /*.equivalence =*/5.0e-4,
30 /*.eigfact =*/1.0e-3,
31 /*.permutation =*/5.0e-3,
32 /*.orthogonalization =*/0.1
33 };
34
35 msym_thresholds_t medium_thresholds = {
36 /*.zero =*/1.0e-2,
37 /*.geometry =*/1.0e-2,
38 /*.angle =*/1.0e-2,
39 /*.equivalence =*/6.3e-3,
40 /*.eigfact =*/1.0e-3,
41 /*.permutation =*/1.58e-2,
42 /*.orthogonalization =*/0.1
43 };
44
45 msym_thresholds_t loose_thresholds = {
46 /*.zero =*/0.06,
47 /*.geometry =*/0.1,
48 /*.angle =*/0.06,
49 /*.equivalence =*/0.025,
50 /*.eigfact =*/1.0e-3,
51 /*.permutation =*/1.0e-1,
52 /*.orthogonalization =*/0.1
53 };
54
55 msym_thresholds_t sloppy_thresholds = {
56 /*.zero =*/0.1,
57 /*.geometry =*/0.1,
58 /*.angle =*/0.1,
59 /*.equivalence =*/0.075,
60 /*.eigfact =*/1.0e-3,
61 /*.permutation =*/1.0e-1,
62 /*.orthogonalization =*/0.1
63 };
64
65 SymmetryWidget::SymmetryWidget(QWidget* parent_)
66 : QWidget(parent_), m_ui(new Ui::SymmetryWidget),
67 m_equivalenceTreeModel(new QStandardItemModel(this)),
68 m_operationsTableModel(new OperationsTableModel(this)),
69 m_subgroupsTreeModel(new QStandardItemModel(this)), m_molecule(nullptr),
70 m_es(nullptr), m_sops(nullptr), m_sg(nullptr), m_sopsl(0), m_sgl(0),
71 m_radius(0.0)
72 {
73 setWindowFlags(Qt::Dialog);
74 m_ui->setupUi(this);
75
76 m_ui->equivalenceTree->setModel(m_equivalenceTreeModel);
77
78 m_ui->operationsTable->setModel(m_operationsTableModel);
79 m_ui->operationsTable->setItemDelegateForColumn(
80 OperationsTableModel::ColumnType, new RichTextDelegate(this));
81
82 m_ui->subgroupsTree->setModel(m_subgroupsTreeModel);
83 m_ui->subgroupsTree->setItemDelegateForColumn(0, new RichTextDelegate(this));
84
85 connect(m_ui->detectSymmetryButton, SIGNAL(clicked()),
86 SIGNAL(detectSymmetry()));
87 connect(m_ui->symmetrizeMoleculeButton, SIGNAL(clicked()),
88 SIGNAL(symmetrizeMolecule()));
89
90 connect(
91 m_ui->equivalenceTree->selectionModel(),
92 SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
93 SLOT(equivalenceSelectionChanged(const QItemSelection&,
94 const QItemSelection&)));
95
96 connect(
97 m_ui->operationsTable->selectionModel(),
98 SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
99 SLOT(operationsSelectionChanged(const QItemSelection&,
100 const QItemSelection&)));
101 connect(
102 m_ui->subgroupsTree->selectionModel(),
103 SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
104 SLOT(
105 subgroupsSelectionChanged(const QItemSelection&, const QItemSelection&)));
106 }
107
108 SymmetryWidget::~SymmetryWidget()
109 {
110 delete m_ui;
111 }
112
113 void SymmetryWidget::setMolecule(QtGui::Molecule* molecule)
114 {
115 if (molecule != m_molecule) {
116 if (m_molecule)
117 m_molecule->disconnect(this);
118
119 m_molecule = molecule;
120
121 if (m_molecule) {
122 connect(m_molecule, SIGNAL(changed(uint)), SLOT(moleculeChanged(uint)));
123 }
124 }
125 }
126
127 void SymmetryWidget::moleculeChanged([[maybe_unused]] unsigned int changes)
128 {
129 /*
130 if (changes & Molecule::UnitCell)
131 revert();*/
132 }
133
134 void SymmetryWidget::operationsSelectionChanged(
135 [[maybe_unused]] const QItemSelection& selected,
136 [[maybe_unused]] const QItemSelection& deselected)
137 {
138
139 if (!m_molecule)
140 return;
141 if (m_ui->tabWidget->currentWidget() != m_ui->subgroupsTab) {
142 // qDebug() << "subgroupsTab not selected";
143 m_ui->subgroupsTree->selectionModel()->reset();
144 }
145
146 QModelIndexList selection =
147 m_ui->operationsTable->selectionModel()->selectedRows();
148
149 // qDebug() << "operations changed";
150
151 // qDebug() << "selection " << selection.size();
152
153 QVariantList reflectionVariantList;
154 QVariantList properRotationVariantList;
155 QVariantList improperRotationVariantList;
156
157 m_molecule->setProperty("SymmetryOrigo", QVariant());
158 m_molecule->setProperty("SymmetryRadius", QVariant());
159 m_molecule->setProperty("SymmetryInversion", QVariant());
160 m_molecule->setProperty("SymmetryProperRotationVariantList", QVariant());
161 m_molecule->setProperty("SymmetryImproperRotationVariantList", QVariant());
162 m_molecule->setProperty("SymmetryReflectionVariantList", QVariant());
163
164 // qDebug() << "cleared elements";
165
166 if (m_sopsl > 0 && selection.size() > 0) {
167 m_molecule->setProperty("SymmetryOrigo", m_cm);
168 m_molecule->setProperty("SymmetryRadius", m_radius);
169 }
170
171 foreach (QModelIndex i, selection) {
172 int row = i.row();
173 if (!i.isValid() || row >= m_sopsl)
174 continue;
175 float x = m_sops[row].v[0], y = m_sops[row].v[1], z = m_sops[row].v[2];
176 switch (m_sops[row].type) {
177 case IDENTITY:
178 break;
179 case PROPER_ROTATION:
180 properRotationVariantList.append(QVector3D(x, y, z));
181 break;
182 case IMPROPER_ROTATION:
183 improperRotationVariantList.append(QVector3D(x, y, z));
184 break;
185 case REFLECTION:
186 reflectionVariantList.append(QVector3D(x, y, z));
187 break;
188 case INVERSION:
189 m_molecule->setProperty("SymmetryInversion", m_cm);
190 break;
191 default:
192 break;
193 }
194 }
195 if (properRotationVariantList.size() > 0)
196 m_molecule->setProperty("SymmetryProperRotationVariantList",
197 properRotationVariantList);
198 if (improperRotationVariantList.size() > 0)
199 m_molecule->setProperty("SymmetryImproperRotationVariantList",
200 improperRotationVariantList);
201 if (reflectionVariantList.size() > 0)
202 m_molecule->setProperty("SymmetryReflectionVariantList",
203 reflectionVariantList);
204
205 /* A little bit ugly, but it'll do for now */
206 m_molecule->emitChanged(QtGui::Molecule::Atoms);
207 }
208
209 void SymmetryWidget::subgroupsSelectionChanged(
210 [[maybe_unused]] const QItemSelection& selected,
211 [[maybe_unused]] const QItemSelection& deselected)
212 {
213 // QModelIndexList selection =
214 // m_ui->subgroupsTree->selectionModel()->selectedIndexes();
215 QModelIndex i =
216 m_ui->subgroupsTree->selectionModel()->selectedIndexes().first();
217 // qDebug() << "subgroupsSelectionChanged";
218 if (!i.isValid())
219 return;
220 // qDebug() << "valid";
221 int sgi = i.data(Qt::UserRole).value<int>();
222 // qDebug() << "index " << sgi;
223 if (sgi < 0 || sgi >= m_sgl)
224 return;
225 const msym::msym_subgroup_t* sg = &m_sg[sgi];
226 // m_ui->operationsTable->selectionModel()->clear();
227
228 QItemSelectionModel* selectionModel = m_ui->operationsTable->selectionModel();
229 // selectionModel->clear();
230
231 QItemSelection selection;
232
233 for (int j = 0; j < sg->order; j++) {
234 int row = static_cast<int>(sg->sops[j] - m_sops);
235 QModelIndex left = m_operationsTableModel->index(row, 0);
236 QModelIndex right = m_operationsTableModel->index(
237 row, m_operationsTableModel->columnCount(left) - 1);
238 // if (!left.isValid() || !right.isValid())
239 // qDebug() << "invalid index " << j;
240 QItemSelection sel(left, right);
241
242 selection.merge(sel, QItemSelectionModel::Select);
243 }
244
245 QModelIndexList tmp = selection.indexes();
246 // foreach (QModelIndex j, tmp) {
247 // qDebug() << "selecting " << j.row() << " " << j.column();
248 // }
249
250 selectionModel->select(selection, QItemSelectionModel::ClearAndSelect);
251 }
252
253 void SymmetryWidget::equivalenceSelectionChanged(
254 [[maybe_unused]] const QItemSelection& selected,
255 [[maybe_unused]] const QItemSelection& deselected)
256 {
257 QModelIndex i =
258 m_ui->equivalenceTree->selectionModel()->selectedIndexes().first();
259 // qDebug() << "equivalenceSelectionChanged";
260 if (!i.isValid())
261 return;
262 int atomInGroup = i.data(Qt::UserRole).value<int>();
263 QModelIndex g = i.parent();
264 if (!g.isValid())
265 return;
266 int group = g.data(Qt::UserRole).value<int>();
267
268 // qDebug() << "valid " << group << atomInGroup;
269 if (group < 0 || group >= m_esl)
270 return;
271
272 // TODO: okay, now we have to find the atoms and select them
273 if (!m_molecule)
274 return;
275
276 const msym_equivalence_set_t* smes = &m_es[group];
277 if (smes == nullptr)
278 return;
279 const msym_element_t* a = smes->elements[atomInGroup];
280 if (a == nullptr)
281 return;
282
283 Index length = m_molecule->atomCount();
284 // unselect all the atoms
285 for (Index iat = 0; iat < length; ++iat) {
286 m_molecule->setAtomSelected(iat, false);
287 }
288 // this is yucky, but libmsym uses <void*> for id
289 auto selectedAtom = reinterpret_cast<Index>(a->id);
290 m_molecule->setAtomSelected(selectedAtom, true);
291
292 m_molecule->emitChanged(QtGui::Molecule::Atoms);
293 }
294
295 void SymmetryWidget::setRadius(double radius)
296 {
297 m_radius = radius;
298 }
299
300 void SymmetryWidget::setCenterOfMass(double cm[3])
301 {
302 m_cm = QVector3D(cm[0], cm[1], cm[2]);
303 }
304
305 void SymmetryWidget::setPointGroupSymbol(QString pg)
306 {
307 m_ui->pointGroupLabel->setText(pg);
308 m_molecule->setData("pointgroup", pg.toStdString());
309 }
310
311 void SymmetryWidget::setSymmetryOperations(
312 int sopsl, const msym::msym_symmetry_operation_t* sops)
313 {
314 m_sops = sops;
315 m_sopsl = sopsl;
316 m_operationsTableModel->setOperations(sopsl, sops);
317 m_molecule->setProperty("SymmetryOrigo", QVariant());
318 m_molecule->setProperty("SymmetryRadius", QVariant());
319 m_molecule->setProperty("SymmetryInversion", QVariant());
320 m_molecule->setProperty("SymmetryProperRotationVariantList", QVariant());
321 m_molecule->setProperty("SymmetryImproperRotationVariantList", QVariant());
322 m_molecule->setProperty("SymmetryReflectionVariantList", QVariant());
323 /* need another change event */
324 m_molecule->emitChanged(QtGui::Molecule::Atoms);
325 }
326
327 void SymmetryWidget::setEquivalenceSets(int esl,
328 const msym::msym_equivalence_set_t* es)
329 {
330 m_esl = esl;
331 m_es = es;
332 m_equivalenceTreeModel->clear();
333 for (int i = 0; i < esl; i++) {
334 auto* const parent = new QStandardItem;
335 QString label = tr("Group %1").arg(QString::number(i + 1));
336 parent->setText(label);
337 parent->setData(i, Qt::UserRole);
338 m_equivalenceTreeModel->appendRow(parent);
339 const msym_equivalence_set_t* smes = &es[i];
340 for (int j = 0; j < smes->length; j++) {
341 auto* const child = new QStandardItem;
342 label =
343 tr("%1 %2").arg(smes->elements[j]->name).arg(QString::number(j + 1));
344 child->setText(label);
345 child->setData(j, Qt::UserRole);
346 parent->appendRow(child);
347 }
348 }
349 }
350
351 void SymmetryWidget::setSubgroups(int sgl, const msym::msym_subgroup_t* sg)
352 {
353 m_sg = sg;
354 m_sgl = sgl;
355 m_subgroupsTreeModel->clear();
356 for (int i = 0; i < sgl; i++) {
357 if (sg[i].order <= 2)
358 continue;
359 auto* const parent = new QStandardItem;
360 parent->setText(pointGroupSymbol(sg[i].name));
361 parent->setData(i, Qt::UserRole);
362 m_subgroupsTreeModel->appendRow(parent);
363 for (auto generator : sg[i].generators) {
364 if (generator == nullptr)
365 continue;
366 // qDebug() << "child " << sg[i].generators[j] - m_sg << " "
367 // << sg[i].generators[j] << " " << m_sg;
368 auto* const child = new QStandardItem;
369 child->setText(pointGroupSymbol(generator->name));
370
371 child->setData(static_cast<int>(generator - m_sg), Qt::UserRole);
372 parent->appendRow(child);
373 }
374 }
375 }
376
377 msym_thresholds_t* SymmetryWidget::getThresholds() const
378 {
379 msym_thresholds_t* thresholds = nullptr;
380 switch (m_ui->toleranceCombo->currentIndex()) {
381 case 3: // sloppy
382 thresholds = &sloppy_thresholds;
383 break;
384 case 2: // loose
385 thresholds = &loose_thresholds;
386 break;
387 case 1: // normal
388 thresholds = &medium_thresholds;
389 break;
390 case 0: // tight
391 default:
392 thresholds = &tight_thresholds;
393 }
394 return thresholds;
395 }
396
397 } // namespace Avogadro::QtPlugins