-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathMultiSelectComboBox.cpp
224 lines (194 loc) · 5.9 KB
/
MultiSelectComboBox.cpp
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
#include "MultiSelectComboBox.h"
#include <QLineEdit>
#include <QCheckBox>
#include <QEvent>
namespace {
const int scSearchBarIndex = 0;
}
MultiSelectComboBox::MultiSelectComboBox(QWidget* aParent) :
QComboBox(aParent),
mListWidget(new QListWidget(this)),
mLineEdit(new QLineEdit(this)),
mSearchBar(new QLineEdit(this))
{
QListWidgetItem* curItem = new QListWidgetItem(mListWidget);
mSearchBar->setPlaceholderText("Search..");
mSearchBar->setClearButtonEnabled(true);
mListWidget->addItem(curItem);
mListWidget->setItemWidget(curItem, mSearchBar);
mLineEdit->setReadOnly(true);
mLineEdit->installEventFilter(this);
setModel(mListWidget->model());
setView(mListWidget);
setLineEdit(mLineEdit);
connect(mSearchBar, &QLineEdit::textChanged, this, &MultiSelectComboBox::onSearch);
connect(this, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this, &MultiSelectComboBox::itemClicked);
}
void MultiSelectComboBox::hidePopup()
{
int width = this->width();
int height = mListWidget->height();
int x = QCursor::pos().x() - mapToGlobal(geometry().topLeft()).x() + geometry().x();
int y = QCursor::pos().y() - mapToGlobal(geometry().topLeft()).y() + geometry().y();
if (x >= 0 && x <= width && y >= this->height() && y <= height + this->height())
{
// Item was clicked, do not hide popup
}
else
{
QComboBox::hidePopup();
}
}
void MultiSelectComboBox::stateChanged(int aState)
{
Q_UNUSED(aState);
QString selectedData("");
int count = mListWidget->count();
for (int i = 1; i < count; ++i)
{
QWidget *widget = mListWidget->itemWidget(mListWidget->item(i));
QCheckBox *checkBox = static_cast<QCheckBox *>(widget);
if (checkBox->isChecked())
{
selectedData.append(checkBox->text()).append(";");
}
}
if (selectedData.endsWith(";"))
{
selectedData.remove(selectedData.count() - 1, 1);
}
if (!selectedData.isEmpty())
{
mLineEdit->setText(selectedData);
}
else
{
mLineEdit->clear();
}
mLineEdit->setToolTip(selectedData);
emit selectionChanged();
}
void MultiSelectComboBox::addItem(const QString& aText, const QVariant& aUserData)
{
Q_UNUSED(aUserData);
QListWidgetItem* listWidgetItem = new QListWidgetItem(mListWidget);
QCheckBox* checkBox = new QCheckBox(this);
checkBox->setText(aText);
mListWidget->addItem(listWidgetItem);
mListWidget->setItemWidget(listWidgetItem, checkBox);
connect(checkBox, &QCheckBox::stateChanged, this, &MultiSelectComboBox::stateChanged);
}
QStringList MultiSelectComboBox::currentText()
{
QStringList emptyStringList;
if(!mLineEdit->text().isEmpty())
{
emptyStringList = mLineEdit->text().split(';');
}
return emptyStringList;
}
void MultiSelectComboBox::addItems(const QStringList& aTexts)
{
for(const auto& string : aTexts)
{
addItem(string);
}
}
int MultiSelectComboBox::count() const
{
int count = mListWidget->count() - 1;// Do not count the search bar
if(count < 0)
{
count = 0;
}
return count;
}
void MultiSelectComboBox::onSearch(const QString& aSearchString)
{
for(int i = 1; i < mListWidget->count(); i++)
{
QCheckBox* checkBox = static_cast<QCheckBox*>(mListWidget->itemWidget(mListWidget->item(i)));
if(checkBox->text().contains(aSearchString, Qt::CaseInsensitive))
{
mListWidget->item(i)->setHidden(false);
}
else
{
mListWidget->item(i)->setHidden(true);
}
}
}
void MultiSelectComboBox::itemClicked(int aIndex)
{
if(aIndex != scSearchBarIndex)// 0 means the search bar
{
QWidget* widget = mListWidget->itemWidget(mListWidget->item(aIndex));
QCheckBox *checkBox = static_cast<QCheckBox *>(widget);
checkBox->setChecked(!checkBox->isChecked());
}
}
void MultiSelectComboBox::SetSearchBarPlaceHolderText(const QString& aPlaceHolderText)
{
mSearchBar->setPlaceholderText(aPlaceHolderText);
}
void MultiSelectComboBox::SetPlaceHolderText(const QString& aPlaceHolderText)
{
mLineEdit->setPlaceholderText(aPlaceHolderText);
}
void MultiSelectComboBox::clear()
{
mListWidget->clear();
QListWidgetItem* curItem = new QListWidgetItem(mListWidget);
mSearchBar = new QLineEdit(this);
mSearchBar->setPlaceholderText("Search..");
mSearchBar->setClearButtonEnabled(true);
mListWidget->addItem(curItem);
mListWidget->setItemWidget(curItem, mSearchBar);
connect(mSearchBar, &QLineEdit::textChanged, this, &MultiSelectComboBox::onSearch);
}
void MultiSelectComboBox::wheelEvent(QWheelEvent* aWheelEvent)
{
// Do not handle the wheel event
Q_UNUSED(aWheelEvent);
}
bool MultiSelectComboBox::eventFilter(QObject* aObject, QEvent* aEvent)
{
if(aObject == mLineEdit && aEvent->type() == QEvent::MouseButtonRelease) {
showPopup();
return false;
}
return false;
}
void MultiSelectComboBox::keyPressEvent(QKeyEvent* aEvent)
{
// Do not handle key event
Q_UNUSED(aEvent);
}
void MultiSelectComboBox::setCurrentText(const QString& aText)
{
Q_UNUSED(aText);
}
void MultiSelectComboBox::setCurrentText(const QStringList& aText)
{
int count = mListWidget->count();
for (int i = 1; i < count; ++i)
{
QWidget* widget = mListWidget->itemWidget(mListWidget->item(i));
QCheckBox* checkBox = static_cast<QCheckBox*>(widget);
QString checkBoxString = checkBox->text();
if(aText.contains(checkBoxString))
{
checkBox->setChecked(true);
}
}
}
void MultiSelectComboBox::ResetSelection()
{
int count = mListWidget->count();
for (int i = 1; i < count; ++i)
{
QWidget *widget = mListWidget->itemWidget(mListWidget->item(i));
QCheckBox *checkBox = static_cast<QCheckBox *>(widget);
checkBox->setChecked(false);
}
}