-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathComboBox.cpp
More file actions
294 lines (232 loc) · 8.51 KB
/
Copy pathComboBox.cpp
File metadata and controls
294 lines (232 loc) · 8.51 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include "ComboBox.h"
ComboBox::ComboBox(QWidget *parent) : TextField(parent) {
setFixedSize(QSize(250, 36));
setDropDownButton();
setMaxVisibleItems(8);
init();
}
void ComboBox::init() {
// Item Delegate
delegate = new Delegate(this->size());
// Popup
popup = new Popup(this);
popup->setModel(&model);
popup->setItemDelegate(delegate);
popup->setSelectionMode(QAbstractItemView::SingleSelection);
popup->setPopupWidth(this->width());
// Auto Completer
completer = new QCompleter(&model, this);
completer->setCaseSensitivity(Qt::CaseInsensitive);
completer->setCompletionMode(QCompleter::InlineCompletion);
completer->setFilterMode(Qt::MatchContains);
setCompleter(completer);
// Install Event Filter
qApp->installEventFilter(this);
// Dropdown Button Signal Slot
if (dropdown) connect(dropdown, &Button::clicked, this, &ComboBox::onDropDownButtonClicked);
// List item Signal Slot
connect(popup->list(), &QListView::clicked, this, &ComboBox::onComboItemClicked);
}
bool ComboBox::eventFilter(QObject *obj, QEvent *event) {
if (!popup || !isVisible())
return false;
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
QPoint globalPos = mouseEvent->globalPosition().toPoint();
QWidget *clickedWidget = QApplication::widgetAt(globalPos);
bool clickedInsidePopup = popup->isAncestorOf(clickedWidget);
bool clickedOnTextField = this->isAncestorOf(clickedWidget);
if (popup->isVisible() && !clickedInsidePopup && !clickedOnTextField)
popup->fadeOut();
}
if (event->type() == QEvent::ApplicationDeactivate)
if (popup->isVisible()) popup->fadeOut();
return QObject::eventFilter(obj, event);
}
void ComboBox::setFieldSize(const QSize &fieldSize) {
TextField::setFixedSize(fieldSize);
popup->setPopupWidth(this->width());
update();
}
void ComboBox::setPopupWidth(int width) { popup->setPopupWidth(width); }
void ComboBox::setIconic(bool value) {
isIconic = value;
delegate->setIconic(isIconic);
TextField::setIconic(isIconic);
}
void ComboBox::setEditable(bool value) {
isEditable = value;
setReadOnly(!isEditable);
setCursor(isEditable ? Qt::IBeamCursor : Qt::ArrowCursor);
}
void ComboBox::setDarkMode(bool value) {
isDarkMode = value;
TextField::setDarkMode(value);
dropdown->setDarkMode(value);
popup->setDarkMode(value);
delegate->setDarkMode(value);
updateItemIcons();
}
void ComboBox::setDropDownButton() {
dropdown = new Button(this);
dropdown->setDisplayMode(Button::IconOnly);
dropdown->setNormalBackgroundTransparent(true);
dropdown->setBorderTransparent(true);
dropdown->setCursor(Qt::PointingHandCursor);
dropdown->setIconSize(QSize(20, 20));
dropdown->setFixedSize(QSize(28, 28));
dropdown->setIconPaths(ArrowDown, ArrowDown);
dropdown->raise();
positionDropDownButton();
}
void ComboBox::positionDropDownButton() {
if (dropdown) {
int x = width() - (12 + dropdown->width()) + 3;
int y = (height() - dropdown->height()) / 2;
dropdown->move(x, y);
}
}
void ComboBox::addItem(const QString &text, const QString &lightIcon, const QString &darkIcon) {
items.append({text, lightIcon, darkIcon});
QStandardItem *item;
if (!lightIcon.isEmpty() && !darkIcon.isEmpty())
item = new QStandardItem(QIcon(isDarkMode ? darkIcon : lightIcon), text);
else
item = new QStandardItem(text);
model.appendRow(item);
popup->updatePopup();
}
void ComboBox::addItems(const QVector<ComboItem> comboItems) {
for (const ComboItem &ci : comboItems)
addItem(ci.text, ci.lightIcon, ci.darkIcon);
}
void ComboBox::onComboItemClicked(const QModelIndex &index) {
setText(currentText());
if (isIconic && index.row() < items.size()) {
const ComboItem &ci = items[index.row()];
setIconPaths(ci.lightIcon, ci.darkIcon);
}
repaint();
popup->fadeOut();
}
void ComboBox::onDropDownButtonClicked() {
popup->isVisible() ? popup->fadeOut() : positionPopup();
}
void ComboBox::positionPopup() {
QPoint globalPos = this->mapToGlobal(QPoint(0, 0));
QScreen *screenAtCursor = QApplication::screenAt(globalPos);
if (!screenAtCursor)
screenAtCursor = QApplication::primaryScreen();
QRect screenGeometry = screenAtCursor->availableGeometry();
QRect cbRect = QRect(globalPos, this->size());
QSize popupSize = popup->size();
QPoint abovePos(cbRect.left(), cbRect.top() - popupSize.height() - 4);
QPoint belowPos(cbRect.left(), cbRect.bottom() + 4);
QPoint centerPos(cbRect.left(), cbRect.center().y() - popupSize.height() / 2);
QPoint currPosition;
if (screenGeometry.contains(QRect(centerPos, popupSize)))
currPosition = centerPos;
else if (screenGeometry.contains(QRect(belowPos, popupSize)))
currPosition = belowPos;
else if (screenGeometry.contains(QRect(abovePos, popupSize)))
currPosition = abovePos;
else
currPosition = belowPos;
int x = std::clamp(currPosition.x(), screenGeometry.left(), screenGeometry.right() - popupSize.width());
int y = std::clamp(currPosition.y(), screenGeometry.top(), screenGeometry.bottom() - popupSize.height());
popup->move(QPoint(x, y));
popup->raise();
popup->fadeIn();
}
void ComboBox::setMaxVisibleItems(int items) { _maxVisibleItems = items; }
int ComboBox::maxVisibleItems() const { return _maxVisibleItems; }
void ComboBox::updateItemIcons() {
for (int i = 0; i < items.size(); ++i) {
const ComboItem &ci = items[i];
if (ci.lightIcon.isEmpty() || ci.darkIcon.isEmpty())
continue;
QStandardItem *item = model.item(i);
if (!item)
continue;
item->setIcon(QIcon(isDarkMode ? ci.darkIcon : ci.lightIcon));
}
popup->list()->viewport()->update();
}
void ComboBox::deleteItem(int index) {
if (index < 0 || index >= items.size())
return;
items.removeAt(index);
model.removeRow(index);
popup->updatePopup();
}
QString ComboBox::currentText() const {
QModelIndex index = popup->list()->currentIndex();
if (index.isValid() && index.row() < items.size())
return index.data(Qt::DisplayRole).toString();
return QString();
}
int ComboBox::currentIndex() const { return popup->list()->currentIndex().row(); }
void ComboBox::setCurrentItem(int index) {
if (index < 0 || index >= items.size()) return;
QModelIndex idx = popup->list()->model()->index(index, 0);
popup->list()->setCurrentIndex(idx);
setText(items[index].text);
if (isIconic)
setIconPaths(items[index].lightIcon, items[index].darkIcon);
update();
}
void ComboBox::clearAll() {
items.clear();
model.clear();
if (popup->list())
popup->list()->reset();
}
void ComboBox::mousePressEvent(QMouseEvent *event) {
if (!isEditable)
popup->isVisible() ? popup->fadeOut() : (QTimer::singleShot(300, this, [this]() { positionPopup(); }));
else
QLineEdit::mousePressEvent(event);
}
void ComboBox::mouseMoveEvent(QMouseEvent *event) {
!isEditable ? event->accept() : QLineEdit::mouseMoveEvent(event);
}
void ComboBox::keyPressEvent(QKeyEvent *event) {
if (!isEditable) {
event->accept();
} else {
switch (event->key()) {
case Qt::Key_Up:
case Qt::Key_Down: {
QApplication::sendEvent(popup->list(), event);
break;
}
case Qt::Key_Return:
case Qt::Key_Enter: {
int index = currentIndex();
if (index != -1) {
setCurrentItem(currentIndex());
popup->fadeOut();
}
event->accept();
break;
}
case Qt::Key_Escape: {
if (popup->isVisible())
popup->fadeOut();
event->accept();
break;
}
default: break;
}
}
}
void ComboBox::contextMenuEvent(QContextMenuEvent *event) {
!isEditable ? event->accept() : TextField::contextMenuEvent(event);
}
void ComboBox::mouseDoubleClickEvent(QMouseEvent *event) {
!isEditable ? event->accept() : QLineEdit::mouseDoubleClickEvent(event);
}
void ComboBox::resizeEvent(QResizeEvent *event) {
TextField::resizeEvent(event);
positionDropDownButton();
}