From 09419c544ae0ac03836c42253655e48281b4430b Mon Sep 17 00:00:00 2001 From: Anton Filimonov Date: Sun, 25 Jul 2021 20:44:31 +0300 Subject: [PATCH] feat: allow to configure search history (#364) --- src/ui/include/savedsearches.h | 10 +++++++++- src/ui/src/optionsdialog.cpp | 8 ++++++++ src/ui/src/savedsearches.cpp | 26 ++++++++++++++++++++------ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/ui/include/savedsearches.h b/src/ui/include/savedsearches.h index 7617b1ce9..9c4367bad 100644 --- a/src/ui/include/savedsearches.h +++ b/src/ui/include/savedsearches.h @@ -41,16 +41,24 @@ class SavedSearches final : public Persistable // Returns a list of recent searches (newer first) QStringList recentSearches() const; + int historySize() const; + void setHistorySize(int historySize); + void clear(); // Reads/writes the current config in the QSettings object passed void saveToStorage( QSettings& settings ) const; void retrieveFromStorage( QSettings& settings ); + private: + void trim(); + private: static constexpr int SAVEDSEARCHES_VERSION = 1; - static constexpr int maxNumberOfRecentSearches = 50; + static constexpr int MaxNumberOfRecentSearches = 50; + + int historySize_ = MaxNumberOfRecentSearches; QStringList savedSearches_; }; diff --git a/src/ui/src/optionsdialog.cpp b/src/ui/src/optionsdialog.cpp index bf07ae6c8..2a1efaa67 100644 --- a/src/ui/src/optionsdialog.cpp +++ b/src/ui/src/optionsdialog.cpp @@ -46,6 +46,7 @@ #include "log.h" #include "shortcuts.h" #include "styles.h" +#include "savedsearches.h" #include "optionsdialog.h" @@ -300,6 +301,9 @@ void OptionsDialog::updateDialogFromConfig() verifySslCheckBox->setChecked( config.verifySslPeers() ); buildShortcutsTable(); + + const auto& savedSearches = SavedSearches::get(); + searchHistorySpinBox->setValue( savedSearches.historySize() ); } // @@ -442,6 +446,10 @@ void OptionsDialog::updateConfigFromDialog() config.save(); + auto& savedSearches = SavedSearches::get(); + savedSearches.setHistorySize( searchHistorySpinBox->value() ); + savedSearches.save(); + emit optionsChanged(); } diff --git a/src/ui/src/savedsearches.cpp b/src/ui/src/savedsearches.cpp index 24e7021b3..86ce2e4a3 100644 --- a/src/ui/src/savedsearches.cpp +++ b/src/ui/src/savedsearches.cpp @@ -31,15 +31,10 @@ void SavedSearches::addRecent( const QString& text ) if ( text.isEmpty() ) return; - // Remove any copy of the about to be added text savedSearches_.removeAll( text ); - - // Add at the front savedSearches_.push_front( text ); - // Trim the list if it's too long - while ( savedSearches_.size() > maxNumberOfRecentSearches ) - savedSearches_.pop_back(); + trim(); } QStringList SavedSearches::recentSearches() const @@ -47,11 +42,28 @@ QStringList SavedSearches::recentSearches() const return savedSearches_; } +int SavedSearches::historySize() const +{ + return historySize_; +} + +void SavedSearches::setHistorySize( int historySize ) +{ + historySize_ = historySize; + trim(); +} + void SavedSearches::clear() { savedSearches_.clear(); } +void SavedSearches::trim() +{ + while ( savedSearches_.size() > historySize_ ) + savedSearches_.pop_back(); +} + // // Persistable virtual functions implementation // @@ -69,6 +81,7 @@ void SavedSearches::saveToStorage( QSettings& settings ) const settings.setValue( "string", savedSearches_.at( i ) ); } settings.endArray(); + settings.setValue( "historySize", historySize_ ); settings.endGroup(); } @@ -88,6 +101,7 @@ void SavedSearches::retrieveFromStorage( QSettings& settings ) savedSearches_.append( search ); } settings.endArray(); + historySize_ = settings.value( "historySize", MaxNumberOfRecentSearches ).toInt(); } else { LOG_ERROR << "Unknown version of saved searches, ignoring it...";