From f0387db46876c22db6ab3fa1e20624755c9e8f3b Mon Sep 17 00:00:00 2001 From: Albert S Date: Tue, 16 Mar 2021 22:29:26 +0100 Subject: [PATCH] Add a basic ranking of system entries and show most relevant first --- window.cpp | 60 +++++++++++++++++++++++++++++++++++++++++------------- window.h | 9 +++++++- 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/window.cpp b/window.cpp index cd7d0ac..72eb457 100644 --- a/window.cpp +++ b/window.cpp @@ -403,6 +403,27 @@ void Window::keyPressEvent(QKeyEvent *event) QWidget::keyPressEvent(event); } +int Window::rankConfig(const EntryConfig &config, QString filter) const +{ + if(config.command.startsWith(filter, Qt::CaseInsensitive)) + { + return 0; + } + else if(config.command.contains(filter, Qt::CaseInsensitive)) + { + return 1; + } + else if(config.name.startsWith(filter, Qt::CaseInsensitive)) + { + return 2; + } + else if(config.name.contains(filter, Qt::CaseInsensitive)) + { + return 3; + } + return -1; +} + void Window::filterGridFor(QString filter) { if(filter.length() > 0) @@ -422,27 +443,38 @@ void Window::filterGridFor(QString filter) } if(!userEntryMatch) { + QVector rankedEntries; int currow = 0; int curcol = 0; int i = 1; const int MAX_COLS = this->settingsProvider->getMaxCols(); for(EntryPushButton *button : this->systemEntryButtons) { - if(button->getName().contains(filter, Qt::CaseInsensitive) || - button->getCommand().contains(filter, Qt::CaseInsensitive)) + int ranking = rankConfig(button->getEntryConfig(), filter); + if(ranking > -1) { - button->setVisible(true); - if(i < 10) - { - button->setShortcutKey(QString::number(i++)); - } - grid->addWidget(button, currow, curcol++); - this->buttonsInGrid.append(button); - if(curcol == MAX_COLS) - { - curcol = 0; - ++currow; - } + RankedButton rb; + rb.button = button; + rb.ranking = ranking; + rankedEntries.append(rb); + } + } + std::sort(rankedEntries.begin(), rankedEntries.end(), + [](const RankedButton &a, const RankedButton &b) -> bool { return a.ranking < b.ranking; }); + for(RankedButton &rankedButton : rankedEntries) + { + EntryPushButton *button = rankedButton.button; + button->setVisible(true); + if(i < 10) + { + button->setShortcutKey(QString::number(i++)); + } + grid->addWidget(button, currow, curcol++); + this->buttonsInGrid.append(button); + if(curcol == MAX_COLS) + { + curcol = 0; + ++currow; } } } diff --git a/window.h b/window.h index 496dbf0..17c0fe0 100644 --- a/window.h +++ b/window.h @@ -37,6 +37,13 @@ #include "calculationengine.h" #include "settingsprovider.h" +class RankedButton +{ + public: + EntryPushButton *button = nullptr; + int ranking; +}; + class Window : public QWidget { Q_OBJECT @@ -77,7 +84,7 @@ class Window : public QWidget QStringList generatePATHSuggestions(const QString &text); void closeWindow(); std::pair getNextFreeCell(); - + int rankConfig(const EntryConfig &config, QString filter) const; private slots: void lineEditReturnPressed(); void showCalculationResultContextMenu(const QPoint &point);