Updated CheckComboBox to look more like QComboBox

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2024-04-26 00:16:48 +03:00
parent 1961e2081f
commit e1ed317c13
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
4 changed files with 34 additions and 37 deletions

View File

@ -26,6 +26,7 @@
#include <QLineEdit> #include <QLineEdit>
#include <QListView> #include <QListView>
#include <QStringList> #include <QStringList>
#include <QStylePainter>
class CheckComboModel : public QIdentityProxyModel { class CheckComboModel : public QIdentityProxyModel {
Q_OBJECT Q_OBJECT
@ -70,12 +71,6 @@ class CheckComboModel : public QIdentityProxyModel {
CheckComboBox::CheckComboBox(QWidget* parent) : QComboBox(parent), m_separator(", ") CheckComboBox::CheckComboBox(QWidget* parent) : QComboBox(parent), m_separator(", ")
{ {
QLineEdit* lineEdit = new QLineEdit(this);
lineEdit->setReadOnly(true);
setLineEdit(lineEdit);
lineEdit->disconnect(this);
setInsertPolicy(QComboBox::NoInsert);
view()->installEventFilter(this); view()->installEventFilter(this);
view()->window()->installEventFilter(this); view()->window()->installEventFilter(this);
view()->viewport()->installEventFilter(this); view()->viewport()->installEventFilter(this);
@ -89,9 +84,9 @@ void CheckComboBox::setSourceModel(QAbstractItemModel* new_model)
model()->disconnect(this); model()->disconnect(this);
QComboBox::setModel(proxy); QComboBox::setModel(proxy);
connect(this, QOverload<int>::of(&QComboBox::activated), this, &CheckComboBox::toggleCheckState); connect(this, QOverload<int>::of(&QComboBox::activated), this, &CheckComboBox::toggleCheckState);
connect(proxy, &CheckComboModel::checkStateChanged, this, &CheckComboBox::updateCheckedItems); connect(proxy, &CheckComboModel::checkStateChanged, this, &CheckComboBox::emitCheckedItemsChanged);
connect(model(), &CheckComboModel::rowsInserted, this, &CheckComboBox::updateCheckedItems); connect(model(), &CheckComboModel::rowsInserted, this, &CheckComboBox::emitCheckedItemsChanged);
connect(model(), &CheckComboModel::rowsRemoved, this, &CheckComboBox::updateCheckedItems); connect(model(), &CheckComboModel::rowsRemoved, this, &CheckComboBox::emitCheckedItemsChanged);
} }
void CheckComboBox::hidePopup() void CheckComboBox::hidePopup()
@ -100,15 +95,9 @@ void CheckComboBox::hidePopup()
QComboBox::hidePopup(); QComboBox::hidePopup();
} }
void CheckComboBox::updateCheckedItems() void CheckComboBox::emitCheckedItemsChanged()
{ {
QStringList items = checkedItems(); emit checkedItemsChanged(checkedItems());
if (items.isEmpty())
setEditText(defaultText());
else
setEditText(items.join(separator()));
emit checkedItemsChanged(items);
} }
QString CheckComboBox::defaultText() const QString CheckComboBox::defaultText() const
@ -118,10 +107,7 @@ QString CheckComboBox::defaultText() const
void CheckComboBox::setDefaultText(const QString& text) void CheckComboBox::setDefaultText(const QString& text)
{ {
if (m_default_text != text) { m_default_text = text;
m_default_text = text;
updateCheckedItems();
}
} }
QString CheckComboBox::separator() const QString CheckComboBox::separator() const
@ -131,10 +117,7 @@ QString CheckComboBox::separator() const
void CheckComboBox::setSeparator(const QString& separator) void CheckComboBox::setSeparator(const QString& separator)
{ {
if (m_separator != separator) { m_separator = separator;
m_separator = separator;
updateCheckedItems();
}
} }
bool CheckComboBox::eventFilter(QObject* receiver, QEvent* event) bool CheckComboBox::eventFilter(QObject* receiver, QEvent* event)
@ -157,6 +140,8 @@ bool CheckComboBox::eventFilter(QObject* receiver, QEvent* event)
case QEvent::MouseButtonRelease: case QEvent::MouseButtonRelease:
containerMousePress = false; containerMousePress = false;
break; break;
case QEvent::Wheel:
return receiver == this;
default: default:
break; break;
} }
@ -170,7 +155,7 @@ void CheckComboBox::toggleCheckState(int index)
Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt()); Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt());
setItemData(index, (state == Qt::Unchecked ? Qt::Checked : Qt::Unchecked), Qt::CheckStateRole); setItemData(index, (state == Qt::Unchecked ? Qt::Checked : Qt::Unchecked), Qt::CheckStateRole);
} }
updateCheckedItems(); emitCheckedItemsChanged();
} }
Qt::CheckState CheckComboBox::itemCheckState(int index) const Qt::CheckState CheckComboBox::itemCheckState(int index) const
@ -198,4 +183,23 @@ void CheckComboBox::setCheckedItems(const QStringList& items)
} }
} }
void CheckComboBox::paintEvent(QPaintEvent*)
{
QStylePainter painter(this);
painter.setPen(palette().color(QPalette::Text));
// draw the combobox frame, focusrect and selected etc.
QStyleOptionComboBox opt;
initStyleOption(&opt);
QStringList items = checkedItems();
if (items.isEmpty())
opt.currentText = defaultText();
else
opt.currentText = items.join(separator());
painter.drawComplexControl(QStyle::CC_ComboBox, opt);
// draw the icon and text
painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
}
#include "CheckComboBox.moc" #include "CheckComboBox.moc"

View File

@ -49,8 +49,11 @@ class CheckComboBox : public QComboBox {
signals: signals:
void checkedItemsChanged(const QStringList& items); void checkedItemsChanged(const QStringList& items);
protected:
void paintEvent(QPaintEvent*) override;
private: private:
void updateCheckedItems(); void emitCheckedItemsChanged();
bool eventFilter(QObject* receiver, QEvent* event) override; bool eventFilter(QObject* receiver, QEvent* event) override;
void toggleCheckState(int index); void toggleCheckState(int index);

View File

@ -89,8 +89,6 @@ ModFilterWidget::ModFilterWidget(MinecraftInstance* instance, bool extended, QWi
ui->versions->setStyleSheet("combobox-popup: 0;"); ui->versions->setStyleSheet("combobox-popup: 0;");
ui->version->setStyleSheet("combobox-popup: 0;"); ui->version->setStyleSheet("combobox-popup: 0;");
ui->versions->installEventFilter(this);
ui->version->installEventFilter(this);
connect(ui->showAllVersions, &QCheckBox::stateChanged, this, &ModFilterWidget::onShowAllVersionsChanged); connect(ui->showAllVersions, &QCheckBox::stateChanged, this, &ModFilterWidget::onShowAllVersionsChanged);
connect(ui->versions, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ModFilterWidget::onVersionFilterChanged); connect(ui->versions, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ModFilterWidget::onVersionFilterChanged);
connect(ui->version, &QComboBox::currentTextChanged, this, &ModFilterWidget::onVersionFilterTextChanged); connect(ui->version, &QComboBox::currentTextChanged, this, &ModFilterWidget::onVersionFilterTextChanged);
@ -117,13 +115,6 @@ ModFilterWidget::ModFilterWidget(MinecraftInstance* instance, bool extended, QWi
prepareBasicFilter(); prepareBasicFilter();
} }
bool ModFilterWidget::eventFilter(QObject* obj, QEvent* evt)
{
if ((obj != ui->versions && obj != ui->version) || evt->type() != QEvent::Wheel)
return QTabWidget::eventFilter(obj, evt);
return true;
}
auto ModFilterWidget::getFilter() -> std::shared_ptr<Filter> auto ModFilterWidget::getFilter() -> std::shared_ptr<Filter>
{ {
m_filter_changed = false; m_filter_changed = false;

View File

@ -85,7 +85,6 @@ class ModFilterWidget : public QTabWidget {
public slots: public slots:
void setCategories(const QList<ModPlatform::Category>&); void setCategories(const QList<ModPlatform::Category>&);
bool eventFilter(QObject* obj, QEvent* evt);
private: private:
ModFilterWidget(MinecraftInstance* instance, bool extendedSupport, QWidget* parent = nullptr); ModFilterWidget(MinecraftInstance* instance, bool extendedSupport, QWidget* parent = nullptr);