123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- #include "MultiSelectComboBox.h"
- #include <QStandardItem>
- #include <QHBoxLayout>
- #include <QEvent>
- #include <QMouseEvent>
- MultiSelectComboBox::MultiSelectComboBox(QWidget *parent) : QComboBox(parent), isPermitHidePopup(false)
- {
-
- setStyleSheet("QComboBox::drop-down{border-style:none;}");
-
- setEditable(true);
-
- selectItemView = new QListView(this);
- selectItemView->setMinimumHeight(30);
-
- selectItemView->setStyleSheet("QListView{outline: none;} QListView::item {background:#BDD7FD;} "
- "QListView::item:hover {background: #BDD7FD;}");
- selectItemView->setIconSize(QSize(12, 12));
-
- selectItemView->setSpacing(3);
-
- selectItemView->setEditTriggers(QAbstractItemView::NoEditTriggers);
-
- selectItemView->setResizeMode(QListView::Adjust);
-
- selectItemView->setWrapping(true);
-
- selectItemView->setFlow(QListView::LeftToRight);
-
- selectItemView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
-
- selectItemView->setSelectionMode(QAbstractItemView::NoSelection);
- selectModel = new QStandardItemModel();
- selectItemView->setModel(selectModel);
- QHBoxLayout *lineLayout = new QHBoxLayout(this);
- lineLayout->setMargin(0);
- lineLayout->setSpacing(0);
- lineLayout->addWidget(selectItemView);
-
- selectItemView->viewport()->installEventFilter(this);
- popupView = new QListView();
- popupModel = new QStandardItemModel();
- popupView->setModel(popupModel);
- setView(popupView);
- setModel(popupModel);
- installEventFilter(this);
-
- connect(this, QOverload<int>::of(&QComboBox::activated), [=](int index) {
- bool hasExist = false;
- QStandardItem *clickItem = popupModel->item(index);
- QString text = clickItem->text();
-
- for (int row = 0; row < selectModel->rowCount(); row++) {
- QStandardItem *item = selectModel->item(row);
- if (item->text() == text) {
- selectModel->removeRow(row);
- hasExist = true;
- break;
- }
- }
- if (!hasExist) {
-
- clickItem->setCheckState(Qt::Checked);
- QStandardItem *item = new QStandardItem(QIcon("://del.png"), text);
- selectModel->appendRow(item);
- } else {
-
- clickItem->setCheckState(Qt::Unchecked);
- }
- });
-
- QFrame *frame = this->findChild<QFrame *>();
- if (frame) {
- frame->installEventFilter(this);
- }
- installEventFilter(this);
- }
- MultiSelectComboBox::~MultiSelectComboBox() { }
- void MultiSelectComboBox::addItem(const QString &text, const QVariant &userData)
- {
- QComboBox::addItem(text, userData);
- popupModel->item(findText(text))->setCheckable(true);
- }
- void MultiSelectComboBox::addItem(const QIcon &icon, const QString &text, const QVariant &userData)
- {
- QComboBox::addItem(icon, text, userData);
- popupModel->item(findText(text))->setCheckable(true);
- }
- void MultiSelectComboBox::addItems(const QStringList &texts)
- {
- QComboBox::addItems(texts);
- for (int row = 0; row < popupModel->rowCount(); row++) {
- popupModel->item(row)->setCheckable(true);
- }
- }
- QStringList MultiSelectComboBox::currentText()
- {
- QStringList items;
- for (int row = 0; row < popupModel->rowCount(); row++) {
- QStandardItem *item = popupModel->item(row);
- if (item->checkState() == Qt::Checked) {
- items << item->text();
- }
- }
- return items;
- }
- QList<int> MultiSelectComboBox::currentIndex()
- {
- QList<int> indexs;
- for (int row = 0; row < popupModel->rowCount(); row++) {
- QStandardItem *item = popupModel->item(row);
- if (item->checkState() == Qt::Checked) {
- indexs << row;
- }
- }
- return indexs;
- }
- bool MultiSelectComboBox::eventFilter(QObject *watched, QEvent *event)
- {
- QFrame *frame = this->findChild<QFrame *>();
- if (frame && frame == watched) {
- if (event->type() == QEvent::MouseButtonPress) {
- QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
- QPoint globalPos = mouseEvent->globalPos();
- QRect rect(frame->mapToGlobal(QPoint(0, 0)), QSize(frame->width(), frame->height()));
- QRect rect1(selectItemView->viewport()->mapToGlobal(QPoint(0, 0)),
- QSize(selectItemView->viewport()->width(), selectItemView->viewport()->height()));
-
- if (!rect.contains(globalPos) && !rect1.contains(globalPos)) {
- isPermitHidePopup = true;
- } else {
- selectItemViewPress(selectItemView->viewport()->mapFromGlobal(globalPos));
- }
- }
- }
- if (watched == selectItemView->viewport()) {
-
- if (event->type() == QEvent::MouseButtonPress) {
- QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
- selectItemViewPress(mouseEvent->pos());
- }
- }
-
- if (watched == this) {
- if (event->type() == QEvent::Wheel) {
- return true;
- }
- }
- return QComboBox::eventFilter(watched, event);
- }
- void MultiSelectComboBox::showPopup()
- {
- QComboBox::showPopup();
- isPermitHidePopup = false;
- }
- void MultiSelectComboBox::hidePopup()
- {
- if (isPermitHidePopup) {
- QComboBox::hidePopup();
- }
- }
- void MultiSelectComboBox::selectItemViewPress(QPoint pos)
- {
- QModelIndex index = selectItemView->indexAt(pos);
-
- if (index.isValid()) {
- QRect rect = selectItemView->visualRect(index);
- QSize iconSize = selectItemView->iconSize();
-
- QRect iconRect = QRect(rect.left(), rect.top(), iconSize.width(), iconSize.height());
-
- if (iconRect.contains(pos)) {
- QStandardItem *item = selectModel->itemFromIndex(index);
-
- popupModel->item(findText(item->text()))->setCheckState(Qt::Unchecked);
-
- selectModel->removeRow(index.row());
- }
- }
- showPopup();
- }
|