Skip to content

Commit

Permalink
Search: save queries across restarts and when switching skins
Browse files Browse the repository at this point in the history
  • Loading branch information
ronso0 committed Oct 19, 2021
1 parent 42f6cbf commit 966c0c0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/skin/legacy/legacyskinparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,7 @@ QWidget* LegacySkinParser::parseSearchBox(const QDomElement& node) {
WSearchLineEdit::kDefaultDebouncingTimeoutMillis);
WSearchLineEdit::setDebouncingTimeoutMillis(searchDebouncingTimeoutMillis);

WSearchLineEdit* pLineEditSearch = new WSearchLineEdit(m_pParent);
WSearchLineEdit* pLineEditSearch = new WSearchLineEdit(m_pParent, m_pConfig);
commonWidgetSetup(node, pLineEditSearch, false);
pLineEditSearch->setup(node, *m_pContext);

Expand Down
34 changes: 33 additions & 1 deletion src/widget/wsearchlineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const QColor kDefaultBackgroundColor = QColor(0, 0, 0);

const QString kDisabledText = QStringLiteral("- - -");

const QString kSavedQueriesConfigGroup = QStringLiteral("[SearchQueries]");

constexpr int kClearButtonClearence = 1;

inline QString clearButtonStyleSheet(int pxPadding, Qt::LayoutDirection direction) {
Expand Down Expand Up @@ -74,9 +76,10 @@ void WSearchLineEdit::setDebouncingTimeoutMillis(int debouncingTimeoutMillis) {
s_debouncingTimeoutMillis = verifyDebouncingTimeoutMillis(debouncingTimeoutMillis);
}

WSearchLineEdit::WSearchLineEdit(QWidget* pParent)
WSearchLineEdit::WSearchLineEdit(QWidget* pParent, UserSettingsPointer pConfig)
: QComboBox(pParent),
WBaseWidget(this),
m_pConfig(pConfig),
m_clearButton(make_parented<QToolButton>(this)) {
setAcceptDrops(false);
setEditable(true);
Expand Down Expand Up @@ -146,9 +149,15 @@ WSearchLineEdit::WSearchLineEdit(QWidget* pParent)
clearButtonSize.width() + m_frameWidth + kClearButtonClearence,
layoutDirection()));

loadQueriesFromConfig();

refreshState();
}

WSearchLineEdit::~WSearchLineEdit() {
saveQueriesInConfig();
}

void WSearchLineEdit::setup(const QDomNode& node, const SkinContext& context) {
auto backgroundColor = kDefaultBackgroundColor;
QString bgColorName;
Expand Down Expand Up @@ -231,6 +240,29 @@ void WSearchLineEdit::setup(const QDomNode& node, const SkinContext& context) {
tr("Esc") + " " + tr("Exit search", "Exit search bar and leave focus"));
}

void WSearchLineEdit::loadQueriesFromConfig() {
if (m_pConfig) {
const QList<ConfigKey> queries =
m_pConfig->getKeysWithGroup(kSavedQueriesConfigGroup);
for (const auto& query : queries) {
const QString queryString = m_pConfig->getValueString(query);
if (!queryString.isEmpty()) {
addItem(queryString);
}
}
}
}

void WSearchLineEdit::saveQueriesInConfig() {
if (m_pConfig) {
for (int index = 0; index < count(); index++) {
m_pConfig->setValue(
ConfigKey(kSavedQueriesConfigGroup, QString::number(index)),
this->itemText(index));
}
}
}

void WSearchLineEdit::updateStyleMetrics() {
QStyleOptionComboBox styleArrow;
styleArrow.initFrom(this);
Expand Down
9 changes: 7 additions & 2 deletions src/widget/wsearchlineedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <QTimer>
#include <QToolButton>

#include "preferences/usersettings.h"
#include "util/parented_ptr.h"
#include "widget/wbasewidget.h"

Expand All @@ -25,8 +26,8 @@ class WSearchLineEdit : public QComboBox, public WBaseWidget {
static void setDebouncingTimeoutMillis(int debouncingTimeoutMillis);
virtual void showPopup() override;

explicit WSearchLineEdit(QWidget* pParent);
~WSearchLineEdit() override = default;
explicit WSearchLineEdit(QWidget* pParent, UserSettingsPointer pConfig = nullptr);
~WSearchLineEdit();

void setup(const QDomNode& node, const SkinContext& context);

Expand Down Expand Up @@ -89,6 +90,10 @@ class WSearchLineEdit : public QComboBox, public WBaseWidget {
// Update the displayed text without (re-)starting the timer
void setTextBlockSignals(const QString& text);

UserSettingsPointer m_pConfig;
void loadQueriesFromConfig();
void saveQueriesInConfig();

parented_ptr<QToolButton> const m_clearButton;

int m_frameWidth;
Expand Down

0 comments on commit 966c0c0

Please sign in to comment.