From 59161e633f57c61e7925e30c202449d8e3297e64 Mon Sep 17 00:00:00 2001 From: optizone <42874998+optizone@users.noreply.github.com> Date: Tue, 1 Oct 2019 10:39:59 +0300 Subject: [PATCH] Add search color option feature + sort color options by name (#1803) --- .../preferences/ColorThemeEditDialog.cpp | 15 ++++--- .../preferences/ColorThemeEditDialog.ui | 39 ++++++++++++------- src/widgets/ColorThemeListView.cpp | 37 +++++++++++++++--- src/widgets/ColorThemeListView.h | 4 ++ 4 files changed, 71 insertions(+), 24 deletions(-) diff --git a/src/dialogs/preferences/ColorThemeEditDialog.cpp b/src/dialogs/preferences/ColorThemeEditDialog.cpp index 245412d5..396bef31 100644 --- a/src/dialogs/preferences/ColorThemeEditDialog.cpp +++ b/src/dialogs/preferences/ColorThemeEditDialog.cpp @@ -9,6 +9,7 @@ #include #include +#include ColorThemeEditDialog::ColorThemeEditDialog(QWidget *parent) : QDialog(parent), @@ -50,6 +51,11 @@ ColorThemeEditDialog::ColorThemeEditDialog(QWidget *parent) : ui->colorPicker->setAlphaEnabled(showAlphaOptions.contains(optionName)); }); + connect(ui->filterLineEdit, &QLineEdit::textChanged, this, + [this](const QString& s) { + static_cast(ui->colorThemeListView->model())->setFilterFixedString(s); + }); + ui->colorThemeListView->setCurrentIndex(ui->colorThemeListView->model()->index(0, 0)); connect(ui->colorPicker, &ColorPicker::colorChanged, this, &ColorThemeEditDialog::colorOptionChanged); @@ -67,7 +73,7 @@ ColorThemeEditDialog::~ColorThemeEditDialog() void ColorThemeEditDialog::accept() { colorTheme = Config()->getColorTheme(); - QJsonDocument sch = qobject_cast(ui->colorThemeListView->model())->getTheme(); + QJsonDocument sch = ui->colorThemeListView->colorSettingsModel()->getTheme(); if (ThemeWorker().isCustomTheme(colorTheme)) { QString err = ThemeWorker().save(sch, colorTheme); if (!err.isEmpty()) { @@ -115,7 +121,6 @@ void ColorThemeEditDialog::keyPressEvent(QKeyEvent *event) void ColorThemeEditDialog::colorOptionChanged(const QColor& newColor) { - auto model = qobject_cast(ui->colorThemeListView->model()); QModelIndex currIndex = ui->colorThemeListView->currentIndex(); if (!currIndex.isValid()) { @@ -125,7 +130,7 @@ void ColorThemeEditDialog::colorOptionChanged(const QColor& newColor) ColorOption currOption = currIndex.data(Qt::UserRole).value(); currOption.color = newColor; currOption.changed = true; - model->setData(currIndex, QVariant::fromValue(currOption)); + ui->colorThemeListView->model()->setData(currIndex, QVariant::fromValue(currOption)); Config()->setColor(currOption.optionName, currOption.color); if (!ColorThemeWorker::cutterSpecificOptions.contains(currOption.optionName)) { @@ -151,13 +156,13 @@ void ColorThemeEditDialog::editThemeChanged(const QString& newTheme) } } colorTheme = newTheme; - qobject_cast(ui->colorThemeListView->model())->updateTheme(); + ui->colorThemeListView->colorSettingsModel()->updateTheme(); previewDisasmWidget->colorsUpdatedSlot(); setWindowTitle(tr("Theme Editor - <%1>").arg(colorTheme)); } bool ColorThemeEditDialog::themeWasEdited(const QString& theme) const { - auto model = qobject_cast(ui->colorThemeListView->model()); + auto model = ui->colorThemeListView->colorSettingsModel(); return ThemeWorker().getTheme(theme) != model->getTheme(); } diff --git a/src/dialogs/preferences/ColorThemeEditDialog.ui b/src/dialogs/preferences/ColorThemeEditDialog.ui index daea4cc7..2e873d57 100644 --- a/src/dialogs/preferences/ColorThemeEditDialog.ui +++ b/src/dialogs/preferences/ColorThemeEditDialog.ui @@ -48,20 +48,31 @@ - - - - 0 - 0 - - - - - 400 - 400 - - - + + + + + Search + + + + + + + + 0 + 0 + + + + + 400 + 400 + + + + + diff --git a/src/widgets/ColorThemeListView.cpp b/src/widgets/ColorThemeListView.cpp index 165442f8..45bd3f4d 100644 --- a/src/widgets/ColorThemeListView.cpp +++ b/src/widgets/ColorThemeListView.cpp @@ -10,12 +10,14 @@ #include #include #include +#include #include "common/Configuration.h" #include "common/ColorThemeWorker.h" #include "widgets/ColorThemeListView.h" +constexpr int allFieldsRole = Qt::UserRole + 2; struct OptionInfo { QString info; @@ -204,12 +206,19 @@ QPixmap ColorOptionDelegate::getPixmapFromSvg(const QString& fileName, const QCo ColorThemeListView::ColorThemeListView(QWidget *parent) : QListView (parent) { - setModel(new ColorSettingsModel(static_cast(this))); - static_cast(this->model())->updateTheme(); + QSortFilterProxyModel* proxy = new QSortFilterProxyModel(this); + ColorSettingsModel* model = new ColorSettingsModel(this); + proxy->setSourceModel(model); + model->updateTheme(); + setModel(proxy); + proxy->setFilterRole(allFieldsRole); + proxy->setFilterCaseSensitivity(Qt::CaseSensitivity::CaseInsensitive); + proxy->setSortRole(Qt::DisplayRole); + proxy->setSortCaseSensitivity(Qt::CaseSensitivity::CaseInsensitive); setItemDelegate(new ColorOptionDelegate(this)); setResizeMode(ResizeMode::Adjust); - QJsonArray rgb = qobject_cast(model())->getTheme() + QJsonArray rgb = colorSettingsModel()->getTheme() .object().find("gui.background").value().toArray(); if (rgb.size() == 3) { backgroundColor = QColor(rgb[0].toInt(), rgb[1].toInt(), rgb[2].toInt()); @@ -253,13 +262,12 @@ void ColorThemeListView::dataChanged(const QModelIndex& topLeft, const QModelInd void ColorThemeListView::mouseReleaseEvent(QMouseEvent* e) { if (qobject_cast(itemDelegate())->getResetButtonRect().contains(e->pos())) { - auto model = qobject_cast(this->model()); ColorOption co = currentIndex().data(Qt::UserRole).value(); co.changed = false; QJsonArray rgb = ThemeWorker().getTheme( Config()->getColorTheme()).object()[co.optionName].toArray(); co.color = QColor(rgb[0].toInt(), rgb[1].toInt(), rgb[2].toInt()); - model->setData(currentIndex(), QVariant::fromValue(co)); + model()->setData(currentIndex(), QVariant::fromValue(co)); QCursor c; c.setShape(Qt::CursorShape::ArrowCursor); setCursor(c); @@ -279,6 +287,11 @@ void ColorThemeListView::mouseMoveEvent(QMouseEvent* e) } } +ColorSettingsModel* ColorThemeListView::colorSettingsModel() const +{ + return static_cast(static_cast(model())->sourceModel()); +} + void ColorThemeListView::blinkTimeout() { static enum { Normal, Invisible } state = Normal; @@ -328,6 +341,14 @@ QVariant ColorSettingsModel::data(const QModelIndex &index, int role) const return QVariant::fromValue(optionInfoMap__[theme.at(index.row()).optionName].info); } + if (role == allFieldsRole) { + const QString name = theme.at(index.row()).optionName; + return QVariant::fromValue(optionInfoMap__[name].displayingtext + " " + + optionInfoMap__[theme.at(index.row()).optionName].info + " " + + name); + } + + return QVariant(); } @@ -356,6 +377,12 @@ void ColorSettingsModel::updateTheme() theme.push_back({it.key(), QColor(rgb[0].toInt(), rgb[1].toInt(), rgb[2].toInt(), rgb[3].toInt()), false}); } + std::sort(theme.begin(), theme.end(), [](const ColorOption& f, const ColorOption& s) { + QString s1 = optionInfoMap__[f.optionName].displayingtext; + QString s2 = optionInfoMap__[s.optionName].displayingtext; + int r = s1.compare(s2, Qt::CaseSensitivity::CaseInsensitive); + return r < 0; + }); if (!theme.isEmpty()) { dataChanged(index(0), index(theme.size() - 1)); } diff --git a/src/widgets/ColorThemeListView.h b/src/widgets/ColorThemeListView.h index b259ee7c..4feb004d 100644 --- a/src/widgets/ColorThemeListView.h +++ b/src/widgets/ColorThemeListView.h @@ -14,6 +14,7 @@ struct ColorOption { }; Q_DECLARE_METATYPE(ColorOption); +class ColorSettingsModel; class ColorThemeListView : public QListView { @@ -22,6 +23,8 @@ public: ColorThemeListView(QWidget *parent = nullptr); virtual ~ColorThemeListView() override {} + ColorSettingsModel* colorSettingsModel() const; + protected slots: void currentChanged(const QModelIndex ¤t, const QModelIndex &previous) override; @@ -33,6 +36,7 @@ protected slots: void mouseMoveEvent(QMouseEvent *e) override; + private slots: void blinkTimeout();