-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelementselector.cpp
More file actions
157 lines (133 loc) · 3.83 KB
/
elementselector.cpp
File metadata and controls
157 lines (133 loc) · 3.83 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
#include "elementselector.h"
#include "element.h"
#include <QHBoxLayout>
#include <QPushButton>
#include <QDebug>
#include <QKeyEvent>
#include <QLabel>
class ElementSelectorPrivate
{
public:
ElementSelectorPrivate(ElementSelector *q) : q_ptr(q) {}
QString selectedElement;
QString pendingKeys;
QLabel *popupWidget = new QLabel();
void showPopup();
ElementSelector * const q_ptr;
Q_DECLARE_PUBLIC(ElementSelector)
};
void ElementSelectorPrivate::showPopup()
{
Q_Q(ElementSelector);
QPoint parentPos = q->mapToGlobal(QPoint(0, 0));
QSize popupSize = popupWidget->sizeHint();
int originX = parentPos.x() + (q->width() - popupSize.width()) / 2;
int originY = parentPos.y() + q->height() + 4;
popupWidget->show();
popupWidget->move(originX, originY);
}
ElementSelector::ElementSelector(QWidget *parent) : QPushButton(parent),
d_ptr(new ElementSelectorPrivate(this))
{
Q_D(ElementSelector);
d->selectedElement = "C";
setText(d->selectedElement);
setFocusPolicy(Qt::ClickFocus);
d->popupWidget->setText("<center>Enter an element abbreviation, for single<br>"
"letters type the abbreviation twice (e.g. CC).</center>");
d->popupWidget->setFrameStyle(QFrame::Box | QFrame::Plain);
d->popupWidget->setWindowFlags(Qt::ToolTip);
connect(this, &QPushButton::clicked, this, [this](){
this->setFocus();
});
}
ElementSelector::~ElementSelector()
{
}
QString ElementSelector::getSelectedElement()
{
Q_D(ElementSelector);
return d->selectedElement;
}
void ElementSelector::setSelectedElement(QString e)
{
Q_D(ElementSelector);
d->selectedElement = e;
setText(d->selectedElement);
}
void ElementSelector::focusInEvent(QFocusEvent *)
{
Q_D(ElementSelector);
d->pendingKeys = "";
setText("<>");
d->showPopup();
}
void ElementSelector::focusOutEvent(QFocusEvent *)
{
Q_D(ElementSelector);
setText(d->selectedElement);
d->popupWidget->hide();
}
void ElementSelector::keyPressEvent(QKeyEvent *e)
{
Q_D(ElementSelector);
if (e->matches(QKeySequence::Cancel))
{
clearFocus();
return;
}
else if (e->key() == Qt::Key_Backspace)
{
if (d->pendingKeys.length() > 0)
d->pendingKeys.resize(d->pendingKeys.length() - 1);
if (d->pendingKeys.length() == 0)
setText("<>");
else
setText(d->pendingKeys + "...");
}
else if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter)
{
if (d->pendingKeys.length() == 1)
{
auto element = Element::fromAbbr(d->pendingKeys);
if (element.isValid())
d->selectedElement = element.abbr;
emit elementChanged(d->selectedElement);
}
d->pendingKeys = QString();
clearFocus();
return;
}
else
{
//TODO: Ensure there aren't any other e->text() edge cases
if (!e->text().isEmpty() && e->text()[0].isLetter())
d->pendingKeys += e->text()[0];
if (d->pendingKeys.length() >= 2)
{
QString abbr = QString(d->pendingKeys[0].toUpper()) + QString(d->pendingKeys[1]);
QString newElement;
if (Element::fromAbbr(abbr).isValid())
{
newElement = abbr;
}
else if (Element::fromAbbr(abbr.left(1)).isValid())
{
newElement = abbr.left(1);
}
if (!newElement.isEmpty())
{
d->selectedElement = newElement;
emit elementChanged(d->selectedElement);
}
d->pendingKeys = QString();
clearFocus();
return;
}
else if (d->pendingKeys.length() == 1)
{
setText(d->pendingKeys + "...");
}
return;
}
}