-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoolbaraddwidget.cpp
More file actions
190 lines (169 loc) · 6.01 KB
/
toolbaraddwidget.cpp
File metadata and controls
190 lines (169 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "toolbaraddwidget.h"
#include "mol3dview.h"
#include "elementselector.h"
#include "element.h"
#include <QHBoxLayout>
#include <QPushButton>
#include <QDebug>
#include <QLabel>
static const QStringList names = {
"Default",
"32 Bent",
"33 Trigonal planar",
"41 Single",
"42 Bent",
"43 Trigonal pyramidal",
"44 Tetrahedral",
"52 Linear",
"53a T-Shaped (axial)",
"53r T-Shaped (radial)",
"54a Seesaw (axial)",
"54r Seesaw (radial)",
"55a Trigonal bipyramidal (axial)",
"55r Trigonal bipyramidal (radial)",
"64 Square planar",
"65a Square pyramidal (axial)",
"65r Square pyramidal (radial)",
"66 Octahedral",
};
static const QStringList geometries = {
"",
":/structure/32_bent.mol",
":/structure/33_trigonal_planar.mol",
":/structure/41_single.mol",
":/structure/42_bent.mol",
":/structure/43_trigonal_pyramidal.mol",
":/structure/44_tetrahedral.mol",
":/structure/52_linear.mol",
":/structure/53_T_shaped_(axial).mol",
":/structure/53_T_shaped_(radial).mol",
":/structure/54_seesaw_(axial).mol",
":/structure/54_seesaw_(radial).mol",
":/structure/55_trigonal_bipyramidal_(axial).mol",
":/structure/55_trigonal_bipyramidal_(radial).mol",
":/structure/64_square_planar.mol",
":/structure/65_square_pyramidal_(axial).mol",
":/structure/65_square_pyramidal_(radial).mol",
":/structure/66_octahedral.mol",
};
// https://en.wikipedia.org/wiki/Organometallic_chemistry
static const QMap<QString, QString> defaultGeometry = {
{"B", ":/structure/33_trigonal_planar.mol"},
{"C", ":/structure/44_tetrahedral.mol"},
{"N", ":/structure/43_trigonal_pyramidal.mol"},
{"O", ":/structure/42_bent.mol"},
{"Si", ":/structure/44_tetrahedral.mol"},
// P being 43 is also reasonable but you're probably trying to make a phosphate
{"P", ":/structure/55_trigonal_bipyramidal_(axial).mol"},
{"S", ":/structure/42_bent.mol"},
{"As", ":/structure/43_trigonal_pyramidal.mol"},
{"Se", ":/structure/42_bent.mol"},
{"Sn", ":/structure/44_tetrahedral.mol"},
};
namespace {
// Adjust the bond lenghts of the geometry's hydrogens to match the center element
void adjustValenceLength(MolStruct &mol, int center)
{
QList<int> bondedHydrogens;
for (auto &b: mol.bonds)
{
if ((b.from == center) && (mol.atoms[b.to].element == "H"))
bondedHydrogens.append(b.to);
else if ((b.to == center) && (mol.atoms[b.from].element == "H"))
bondedHydrogens.append(b.from);
}
QVector3D centerPos = mol.atoms.at(center).posToVector();
float bondLength = Element(1).estimateBondLength(Element(mol.atoms[center].element));
for (int i: bondedHydrogens)
{
QVector3D bondVector = mol.atoms[i].posToVector() - centerPos;
mol.atoms[i].setPos(bondVector.normalized() * bondLength + centerPos);
}
}
}
ToolbarAddWidget::ToolbarAddWidget(QWidget *parent, Mol3dView *v) : QWidget(parent), view(v)
{
auto l = new QHBoxLayout(this);
l->setContentsMargins(0,0,0,0);
l->setSpacing(4);
selector = new ElementSelector(this);
geometrySelector = new QComboBox(this);
l->addWidget(new QLabel("Element:"));
l->addWidget(selector);
l->addWidget(new QLabel("Geometry:"));
l->addWidget(geometrySelector);
l->insertStretch(-1);
geometrySelector->addItems(names);
for (int i = 0; i < names.size(); ++i)
{
// TODO: https://stackoverflow.com/questions/59054556/how-to-disable-or-shorten-the-tooltip-delay-in-qtableview
if (!geometries[i].isEmpty())
{
QString iconName = geometries[i];
iconName.replace(".mol", ".png");
geometrySelector->setItemData(i, QString("<img src='%1'>").arg(iconName), Qt::ToolTipRole);
}
}
connect(selector, &ElementSelector::elementChanged, this, [this]() {
QString abbr = selector->getSelectedElement();
if (abbr == QStringLiteral("H"))
{
view->setToolMode(Mol3dView::ToolModeAddValence);
QSignalBlocker blockerA(geometrySelector);
geometrySelector->setCurrentIndex(0);
geometrySelector->setEnabled(false);
}
else
{
view->setToolMode(Mol3dView::ToolModeAdd);
QSignalBlocker blockerA(geometrySelector);
geometrySelector->setCurrentIndex(0);
geometrySelector->setEnabled(true);
}
updateStructure();
});
// https://stackoverflow.com/questions/34603778/cannot-connect-qbuttongroup-buttonclicked-to-a-functor
connect(geometrySelector, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, [this](int idx) {
(void)idx;
updateStructure();
});
}
void ToolbarAddWidget::activate()
{
view->setToolMode(Mol3dView::ToolModeAdd);
updateStructure();
}
void ToolbarAddWidget::updateStructure()
{
QString abbr = selector->getSelectedElement();
int idx = geometrySelector->currentIndex();
MolStruct newGroup;
if (idx < 0 || idx >= geometries.size())
idx = 0;
QString structurePath;
if (idx == 0)
{
structurePath = defaultGeometry.value(abbr, ":/structure/41_single.mol");
QSignalBlocker blockerA(geometrySelector);
auto iter = std::find(geometries.begin(), geometries.end(), structurePath);
if (iter != geometries.end())
geometrySelector->setCurrentIndex(iter - geometries.begin());
else
geometrySelector->setCurrentIndex(3);
}
else
structurePath = geometries[idx];
try
{
newGroup = MolStruct::fromSDF(structurePath);
}
catch (QString err)
{
qCritical() << "Failed to open internal structure file!:" << structurePath;
qCritical() << err;
}
QList<int> replacedAtoms = newGroup.replaceElement("R0", abbr);
for (int i: replacedAtoms)
adjustValenceLength(newGroup, i);
view->setAddRGroup(newGroup);
}