Debian Codegraph @

avogadro/qtplugins/templatetool/templatetoolwidget.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 "templatetoolwidget.h"
7 #include "ui_templatetoolwidget.h"
8
9 #include <avogadro/core/elements.h>
10
11 #include <avogadro/qtgui/insertfragmentdialog.h>
12 #include <avogadro/qtgui/periodictableview.h>
13
14 #include <algorithm>
15
16 #include <QtCore/QDebug>
17 #include <QtCore/QList>
18 #include <QtCore/QSettings>
19
20 #include <QtGui/QClipboard>
21
22 namespace {
23 // The ItemData of the "Other" entry in the combo box
24 const int ELEMENT_SELECTOR_TAG = 255;
25 } // namespace
26
27 namespace Avogadro {
28 namespace QtPlugins {
29
30 enum TabType
31 {
32 Elements = 0,
33 Ligands = 1,
34 FunctionalGroups = 2
35 };
36
37 enum LigandType
38 {
39 Monodentate = 0,
40 Bidentate = 1,
41 Tridentate = 2,
42 Tetradentate = 3,
43 Hexadentate = 4,
44 Haptic = 5,
45 Clipboard = 6
46 };
47
48 TemplateToolWidget::TemplateToolWidget(QWidget* parent_)
49 : QWidget(parent_), m_ui(new Ui::TemplateToolWidget),
50 m_fragmentDialog(nullptr), m_elementSelector(nullptr), m_currentElement(26)
51 {
52 m_ui->setupUi(this);
53
54 buildElements();
55
56 // Get default options
57 QSettings settings;
58 settings.beginGroup("templatetool");
59 unsigned int currentElement = settings.value("element", 26).toUInt();
60 selectElement(currentElement);
61
62 // In the same order of the coordinationComboBox
63 // append ".svg" for the icon and ".cjson" for the template
64 m_centers << "1-lin"
65 << "2-lin"
66 << "3-tpl"
67 << "4-tet"
68 << "4-sqp"
69 << "5-tbp"
70 << "5-spy"
71 << "6-oct"
72 << "6-tpr"
73 << "7-pbp"
74 << "8-sqa";
75
76 m_groups << "amide"
77 << "carboxylate"
78 << "ester"
79 << "ethylene"
80 << "ethyne"
81 << "nitro"
82 << "phenyl"
83 << "phosphate"
84 << "sulfonate" << tr("Other…");
85
86 connect(m_ui->elementComboBox, SIGNAL(currentIndexChanged(int)), this,
87 SLOT(elementChanged(int)));
88
89 connect(m_ui->coordinationComboBox, SIGNAL(currentIndexChanged(int)), this,
90 SLOT(coordinationChanged(int)));
91
92 connect(m_ui->typeComboBox, SIGNAL(currentIndexChanged(int)), this,
93 SLOT(typeChanged(int)));
94
95 connect(m_ui->ligandComboBox, SIGNAL(currentIndexChanged(int)), this,
96 SLOT(ligandChanged(int)));
97
98 connect(m_ui->groupComboBox, SIGNAL(currentIndexChanged(int)), this,
99 SLOT(groupChanged(int)));
100
101 // default coordination = octahedral
102 QString currentCoord = settings.value("coordination", "6-oct").toString();
103 int index = m_centers.indexOf(currentCoord);
104 if (index < 0)
105 index = 7; // octahedral
106 m_ui->coordinationComboBox->setCurrentIndex(index);
107
108 // update the preview icon
109 QString iconPath = QString(":/icons/centers/%1.svg").arg(currentCoord);
110 m_ui->centerPreview->setIcon(QIcon(iconPath));
111
112 unsigned int ligandType = settings.value("ligandType", 0).toUInt();
113 m_ui->typeComboBox->setCurrentIndex(ligandType);
114 // update the ligand combo box
115 typeChanged(ligandType);
116 groupChanged(0);
117 }
118
119 TemplateToolWidget::~TemplateToolWidget()
120 {
121 delete m_ui;
122 }
123
124 void TemplateToolWidget::setAtomicNumber(unsigned char atomicNum)
125 {
126 selectElement(atomicNum);
127
128 if (m_elementSelector)
129 m_elementSelector->setElement(static_cast<int>(atomicNum));
130 }
131
132 unsigned char TemplateToolWidget::atomicNumber() const
133 {
134 int curIndex = m_ui->elementComboBox->currentIndex();
135 QVariant itemData = m_ui->elementComboBox->itemData(curIndex);
136 if (!itemData.isValid())
137 return 0;
138
139 unsigned char atomicNum = static_cast<unsigned char>(itemData.toUInt());
140
141 // "Other..." selected....
142 if (atomicNum == 0 && m_elementSelector)
143 atomicNum = static_cast<unsigned char>(m_elementSelector->element());
144
145 return atomicNum;
146 }
147
148 signed char TemplateToolWidget::formalCharge() const
149 {
150 return m_ui->chargeSpinBox->value();
151 }
152
153 void TemplateToolWidget::setFormalCharge(int charge)
154 {
155 QSpinBox* spinBox = m_ui->chargeSpinBox;
156 if (charge < spinBox->minimum() || charge > spinBox->maximum()) {
157 return;
158 }
159 spinBox->setValue(charge);
160 }
161
162 void TemplateToolWidget::setCoordination(unsigned char order)
163 {
164 if (order < m_ui->coordinationComboBox->count())
165 m_ui->coordinationComboBox->setCurrentIndex(static_cast<int>(order));
166 }
167
168 unsigned char TemplateToolWidget::coordination() const
169 {
170 return static_cast<unsigned char>(m_ui->coordinationComboBox->currentIndex());
171 }
172
173 QString TemplateToolWidget::coordinationString() const
174 {
175 return m_centers.at(m_ui->coordinationComboBox->currentIndex());
176 }
177
178 int TemplateToolWidget::currentTab() const
179 {
180 return m_ui->tabWidget->currentIndex();
181 }
182
183 void TemplateToolWidget::setCurrentTab(int index)
184 {
185 if (index < 0)
186 index = m_ui->tabWidget->count() - 1;
187 else if (index >= m_ui->tabWidget->count())
188 index = 0;
189
190 m_ui->tabWidget->setCurrentIndex(index);
191 }
192
193 void TemplateToolWidget::setGroup(const QString& groupName)
194 {
195 // Check if it's in the groups list (excluding "Other" at the end)
196 int index = -1;
197 for (int i = 0; i < m_groups.size() - 1; ++i) {
198 if (m_groups[i] == groupName) {
199 index = i;
200 break;
201 }
202 }
203
204 if (index >= 0) {
205 m_ui->groupComboBox->setCurrentIndex(index);
206 // groupChanged will be called automatically and update the preview
207 } else {
208 // Not in list, set as custom ligand path
209 m_ligandPath = ":/templates/ligands/" + groupName + ".cjson";
210 // Set to "Other" without triggering the dialog
211 m_ui->groupComboBox->blockSignals(true);
212 m_ui->groupComboBox->setCurrentIndex(m_ui->groupComboBox->count() - 1);
213 m_ui->groupComboBox->blockSignals(false);
214 m_denticity = 1;
215 // Update the preview icon
216 m_ui->groupPreview->setIcon(QIcon(":/icons/ligands/" + groupName + ".svg"));
217 }
218 }
219
220 void TemplateToolWidget::setLigand(const QString& ligandName)
221 {
222 // Determine the ligand type from the prefix
223 int ligandType = LigandType::Monodentate;
224 if (ligandName.startsWith("eta") || ligandName.startsWith("e")) {
225 ligandType = LigandType::Haptic;
226 } else if (ligandName.startsWith("6-")) {
227 ligandType = LigandType::Hexadentate;
228 } else if (ligandName.startsWith("4-")) {
229 ligandType = LigandType::Tetradentate;
230 } else if (ligandName.startsWith("3-")) {
231 ligandType = LigandType::Tridentate;
232 } else if (ligandName.startsWith("2-")) {
233 ligandType = LigandType::Bidentate;
234 }
235
236 // Set the type combo box (this triggers typeChanged and populates m_ligands)
237 if (m_ui->typeComboBox->currentIndex() != ligandType) {
238 m_ui->typeComboBox->setCurrentIndex(ligandType);
239 }
240
241 // Find the ligand in the list
242 int index = m_ligands.indexOf(ligandName);
243 if (index >= 0 && index < m_ligands.size() - 1) {
244 m_ui->ligandComboBox->setCurrentIndex(index);
245 } else {
246 // Not in list, set as custom ligand path
247 m_ligandPath = ":/templates/ligands/" + ligandName + ".cjson";
248 // Set to "Other" without triggering the dialog
249 m_ui->ligandComboBox->blockSignals(true);
250 m_ui->ligandComboBox->setCurrentIndex(m_ui->ligandComboBox->count() - 1);
251 m_ui->ligandComboBox->blockSignals(false);
252 // Update the preview icon
253 m_ui->ligandPreview->setIcon(
254 QIcon(":/icons/ligands/" + ligandName + ".svg"));
255 }
256 }
257
258 unsigned char TemplateToolWidget::ligand() const
259 {
260 return static_cast<unsigned char>(m_ui->ligandComboBox->currentIndex());
261 }
262
263 QString TemplateToolWidget::ligandString() const
264 {
265 // first check which tab is open
266 int tabIndex = m_ui->tabWidget->currentIndex();
267
268 if (tabIndex == TabType::FunctionalGroups) {
269 int index = m_ui->groupComboBox->currentIndex();
270 // check if it's "other"
271 if (index == m_ui->groupComboBox->count() - 1)
272 return m_ligandPath;
273 else
274 return m_groups[index];
275 }
276
277 // tell us if we are using the clipboard
278 if (m_ui->typeComboBox->currentIndex() == LigandType::Clipboard)
279 return "Clipboard";
280 // check if it's "other"
281 if (m_ligands.at(m_ui->ligandComboBox->currentIndex()).endsWith("other"))
282 return m_ligandPath;
283
284 return m_ligands.at(m_ui->ligandComboBox->currentIndex());
285 }
286
287 void TemplateToolWidget::coordinationChanged(int index)
288 {
289 if (index < 0 || index > m_ui->coordinationComboBox->count())
290 return;
291
292 // get the icon name
293 QString iconName = m_centers[index];
294 QSettings settings;
295 settings.setValue("templatetool/coordination", iconName);
296
297 m_ui->centerPreview->setIcon(QIcon(":/icons/centers/" + iconName + ".svg"));
298 }
299
300 void TemplateToolWidget::groupChanged(int index)
301 {
302 // get the current name from the text
303 int i = m_ui->groupComboBox->currentIndex();
304 const QString& groupName = m_groups[i];
305 const QString& iconName = groupName;
306 m_denticity = 1;
307
308 // check if it's "other"
309 if (index == m_ui->groupComboBox->count() - 1) {
310 QString path = "fragments/groups";
311
312 if (m_fragmentDialog != nullptr)
313 m_fragmentDialog->deleteLater();
314
315 m_fragmentDialog = new QtGui::InsertFragmentDialog(this, path);
316 connect(m_fragmentDialog, SIGNAL(performInsert(const QString&, bool)), this,
317 SLOT(otherLigandInsert(const QString&, bool)));
318 m_fragmentDialog->show();
319 m_fragmentDialog->raise();
320 m_fragmentDialog->activateWindow();
321 return;
322 }
323
324 m_ui->groupPreview->setIcon(QIcon(":/icons/ligands/" + iconName + ".svg"));
325 }
326
327 void TemplateToolWidget::ligandChanged(int index)
328 {
329 // we need to check if it's "other"
330 if (index < 0 || index > m_ui->ligandComboBox->count() - 1)
331 return;
332
333 // get the icon name
334 QString iconName = m_ligands[index];
335
336 // check if it's "other"
337 if (iconName.endsWith("other")) {
338
339 // figure out the ligand type and the resulting path
340 // to the fragment files
341 int ligandType = m_ui->typeComboBox->currentIndex();
342 QString path = "fragments";
343
344 switch (ligandType) {
345 case LigandType::Monodentate:
346 path += "/ligands/monodentate";
347 break;
348 case LigandType::Bidentate:
349 path += "/ligands/bidentate";
350 break;
351 case LigandType::Tridentate:
352 path += "/ligands/tridentate";
353 break;
354 case LigandType::Tetradentate:
355 path += "/ligands/tetradentate";
356 break;
357 case LigandType::Hexadentate:
358 path += "/ligands/hexadentate";
359 break;
360 case LigandType::Haptic:
361 path += "/ligands/haptic";
362 break;
363 }
364
365 if (m_fragmentDialog != nullptr)
366 m_fragmentDialog->deleteLater();
367
368 m_fragmentDialog = new QtGui::InsertFragmentDialog(this, path);
369 connect(m_fragmentDialog, SIGNAL(performInsert(const QString&, bool)), this,
370 SLOT(otherLigandInsert(const QString&, bool)));
371 m_fragmentDialog->show();
372 m_fragmentDialog->raise();
373 m_fragmentDialog->activateWindow();
374 return;
375 }
376
377 m_ui->ligandPreview->setIcon(QIcon(":/icons/ligands/" + iconName + ".svg"));
378 }
379
380 void TemplateToolWidget::otherLigandInsert(const QString& fileName,
381 [[maybe_unused]] bool crystal)
382 {
383 if (m_fragmentDialog == nullptr)
384 return;
385
386 // get the ligand name
387 QString ligandName = m_fragmentDialog->fileName();
388 m_ligandPath = ligandName;
389
390 m_fragmentDialog->hide();
391 // it will be deleted later
392
393 // update the icon from the filename (so check for .svg)
394 QString iconName = fileName;
395 if (iconName.endsWith(".cjson"))
396 iconName.chop(6);
397 iconName += ".svg";
398
399 // check which tab is active
400
401 m_ui->ligandPreview->setIcon(QIcon(iconName));
402 }
403
404 void TemplateToolWidget::typeChanged(int index)
405 {
406 QSettings settings;
407 settings.beginGroup("templatetool");
408 settings.setValue("ligandType", index);
409
410 m_selectedUIDs.clear();
411 m_ui->ligandComboBox->clear();
412 m_ligands = QStringList();
413 QStringList ligandNames;
414 switch (index) {
415 case LigandType::Monodentate: // Monodentate
416 ligandNames << "ammine"
417 << "aqua"
418 << "carbonyl"
419 << "cyano"
420 << "phosphine"
421 << "thiol" << tr("Other…");
422 m_ligands << "1-ammine"
423 << "1-aqua"
424 << "1-carbonyl"
425 << "1-cyano"
426 << "1-phosphine"
427 << "1-thiol"
428 << "1-other";
429 m_denticity = 1;
430 break;
431 case LigandType::Bidentate: // Bidentate
432 ligandNames << "acetylacetonate"
433 << "bipyridine"
434 << "ethylenediamine" << tr("Other…");
435 m_ligands << "2-acetylacetonate"
436 << "2-bipyridine"
437 << "2-ethylenediamine"
438 << "2-other";
439 m_denticity = 2;
440 break;
441 case LigandType::Tridentate: // Tridentate
442 ligandNames << "terpyridine" << tr("Other…");
443 m_ligands << "3-terpyridine"
444 << "3-other";
445 m_denticity = 3;
446 break;
447 case LigandType::Tetradentate: // Tetradentate
448 ligandNames << "phthalocyanine"
449 << "porphin"
450 << "salen" << tr("Other…");
451 m_ligands << "4-phthalocyanine"
452 << "4-porphin"
453 << "4-salen"
454 << "4-other";
455 m_denticity = 4;
456 break;
457 case LigandType::Hexadentate: // Hexadentate
458 ligandNames << "edta" << tr("Other…");
459 m_ligands << "6-edta"
460 << "6-other";
461 m_denticity = 6;
462 break;
463 case LigandType::Haptic: // Haptic
464 ligandNames << "η2-ethylene"
465 << "η5-cyclopentyl"
466 << "η6-benzene" << tr("Other…");
467 m_ligands << "eta2-ethylene"
468 << "eta5-cyclopentyl"
469 << "eta6-benzene"
470 << "eta-other";
471 m_denticity = 1;
472 break;
473 case LigandType::Clipboard: // Clipboard
474 ligandNames << "clipboard";
475 m_ligands = ligandNames;
476 // technically, we should check the clipboard
477 m_denticity = 1;
478 break;
479 }
480 m_ui->ligandComboBox->addItems(ligandNames);
481 ligandChanged(0);
482 }
483
484 void TemplateToolWidget::elementChanged(int index)
485 {
486 QVariant itemData = m_ui->elementComboBox->itemData(index);
487 if (itemData.isValid()) {
488 if (itemData.toInt() == ELEMENT_SELECTOR_TAG) {
489 if (!m_elementSelector) {
490 m_elementSelector = new QtGui::PeriodicTableView(this);
491 connect(m_elementSelector, SIGNAL(elementChanged(int)), this,
492 SLOT(elementSelectedFromTable(int)));
493 }
494 m_elementSelector->setElement(m_currentElement);
495 m_elementSelector->show();
496 m_elementSelector->raise();
497 m_elementSelector->activateWindow();
498 } else {
499 if (m_elementSelector)
500 m_elementSelector->setElement(itemData.toInt());
501 m_currentElement = static_cast<unsigned char>(itemData.toInt());
502 }
503 }
504
505 QSettings settings;
506 settings.setValue("templatetool/element", m_currentElement);
507 }
508
509 void TemplateToolWidget::updateElementCombo()
510 {
511 // Build set of all elements:
512 QList<unsigned char> allElements;
513 allElements << m_defaultElements;
514 allElements << m_userElements;
515 std::sort(allElements.begin(), allElements.end());
516
517 // Cache selected atomic number for later
518 QVariant selectedData;
519 int curIndex = m_ui->elementComboBox->currentIndex();
520 if (curIndex >= 0)
521 selectedData = m_ui->elementComboBox->itemData(curIndex);
522
523 // Clear and repopulate combo
524 m_ui->elementComboBox->clear();
525 foreach (unsigned char atomicNum, allElements) {
526 m_ui->elementComboBox->addItem(
527 QString("%1 (%2)").arg(Core::Elements::name(atomicNum)).arg(atomicNum),
528 atomicNum);
529 }
530 m_ui->elementComboBox->insertSeparator(m_ui->elementComboBox->count());
531 m_ui->elementComboBox->addItem(tr("Other…"), ELEMENT_SELECTOR_TAG);
532
533 // Reset the element if it still exists
534 selectElement(static_cast<unsigned char>(
535 selectedData.isValid() ? selectedData.toInt() : -1));
536 }
537
538 void TemplateToolWidget::addUserElement(unsigned char element)
539 {
540 // Never add any of the common elements to the user list.
541 if (m_defaultElements.contains(element))
542 return;
543
544 // If the element is already in the user list, move it to the back of the
545 // list.
546 if (m_userElements.removeOne(element)) {
547 m_userElements << element;
548 return;
549 }
550
551 m_userElements << element;
552
553 // Limit the number of user elements
554 /// @todo Make this number of user elements configurable.
555 while (m_userElements.size() > 15)
556 m_userElements.pop_front();
557
558 updateElementCombo();
559 saveElements();
560 }
561
562 void TemplateToolWidget::elementSelectedFromTable(int element)
563 {
564 addUserElement(static_cast<unsigned char>(element));
565 selectElement(static_cast<unsigned char>(element));
566 }
567
568 void TemplateToolWidget::selectElement(unsigned char element)
569 {
570 int curIndex = element > 0 ? m_ui->elementComboBox->findData(element) : -1;
571 if (curIndex >= 0)
572 m_ui->elementComboBox->setCurrentIndex(curIndex);
573 else {
574 addUserElement(element);
575 curIndex = m_ui->elementComboBox->findData(element);
576 if (curIndex >= 0)
577 m_ui->elementComboBox->setCurrentIndex(curIndex);
578 // if we can't find it after adding it, something is very wrong!
579 }
580 }
581
582 void TemplateToolWidget::buildElements()
583 {
584 // Common elements that are always shown in the combo box.
585 if (m_defaultElements.isEmpty()) {
586 m_defaultElements.append(15); // Phosphorus
587 m_defaultElements.append(16); // Sulfur
588 m_defaultElements.append(22); // Ti
589 m_defaultElements.append(23); // V
590 m_defaultElements.append(24); // Cr
591 m_defaultElements.append(25); // Mn
592 m_defaultElements.append(26); // Fe
593 m_defaultElements.append(27); // Co
594 m_defaultElements.append(28); // Ni
595 m_defaultElements.append(29); // Cu
596 m_defaultElements.append(30); // Zn
597 }
598
599 // User-added elements
600 QVariantList userElementsVar =
601 QSettings().value("templatetool/userElements").toList();
602 foreach (const QVariant& var, userElementsVar)
603 m_userElements << static_cast<unsigned char>(var.toUInt());
604
605 updateElementCombo();
606 }
607
608 void TemplateToolWidget::saveElements()
609 {
610 QVariantList atomicNums;
611 for (int i = 0; i < m_userElements.size(); ++i)
612 atomicNums << QVariant(m_userElements[i]);
613
614 QSettings().setValue("templatetool/userElements", atomicNums);
615 }
616
617 int TemplateToolWidget::denticity() const
618 {
619 return m_denticity;
620 }
621
622 std::vector<size_t>& TemplateToolWidget::selectedUIDs()
623 {
624 return m_selectedUIDs;
625 }
626
627 } // namespace QtPlugins
628 } // namespace Avogadro