Skip to content

Commit

Permalink
Merge pull request #10942 from fwcd/configurable-search-bar
Browse files Browse the repository at this point in the history
WSearchLineEdit: Add options to disable auto-completion and history
  • Loading branch information
daschuer committed Jan 18, 2023
2 parents f0fa380 + 65375b2 commit 89f5860
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/library/library_prefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ const ConfigKey mixxx::library::prefs::kSearchDebouncingTimeoutMillisConfigKey =
mixxx::library::prefs::kConfigGroup,
QStringLiteral("SearchDebouncingTimeoutMillis")};

const ConfigKey mixxx::library::prefs::kEnableSearchCompletionsConfigKey =
ConfigKey{
mixxx::library::prefs::kConfigGroup,
QStringLiteral("EnableSearchCompletions")};

const ConfigKey mixxx::library::prefs::kEnableSearchHistoryShortcutsConfigKey =
ConfigKey{
mixxx::library::prefs::kConfigGroup,
QStringLiteral("EnableSearchHistoryShortcuts")};

// The "Export" suffix in the key is kept for backward compatibility
const ConfigKey mixxx::library::prefs::kSyncTrackMetadataConfigKey =
ConfigKey{
Expand Down
4 changes: 4 additions & 0 deletions src/library/library_prefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ extern const ConfigKey kTrackDoubleClickActionConfigKey;

extern const ConfigKey kSearchDebouncingTimeoutMillisConfigKey;

extern const ConfigKey kEnableSearchCompletionsConfigKey;

extern const ConfigKey kEnableSearchHistoryShortcutsConfigKey;

extern const ConfigKey kEditMetadataSelectedClickConfigKey;

extern const ConfigKey kHistoryMinTracksToKeepConfigKey;
Expand Down
27 changes: 27 additions & 0 deletions src/preferences/dialog/dlgpreflibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ DlgPrefLibrary::DlgPrefLibrary(
this,
&DlgPrefLibrary::slotSearchDebouncingTimeoutMillisChanged);

updateSearchLineEditHistoryOptions();

connect(libraryFontButton, &QAbstractButton::clicked, this, &DlgPrefLibrary::slotSelectFont);

// TODO(XXX) this string should be extracted from the soundsources
Expand Down Expand Up @@ -210,6 +212,9 @@ void DlgPrefLibrary::slotResetToDefaults() {
setLibraryFont(QApplication::font());
searchDebouncingTimeoutSpinBox->setValue(
WSearchLineEdit::kDefaultDebouncingTimeoutMillis);
checkBoxEnableSearchCompletions->setChecked(WSearchLineEdit::kCompletionsEnabledDefault);
checkBoxEnableSearchHistoryShortcuts->setChecked(
WSearchLineEdit::kHistoryShortcutsEnabledDefault);
}

void DlgPrefLibrary::slotUpdate() {
Expand Down Expand Up @@ -267,6 +272,13 @@ void DlgPrefLibrary::slotUpdate() {
checkBoxEditMetadataSelectedClicked->setChecked(editMetadataSelectedClick);
m_pLibrary->setEditMedatataSelectedClick(editMetadataSelectedClick);

checkBoxEnableSearchCompletions->setChecked(m_pConfig->getValue(
kEnableSearchCompletionsConfigKey,
WSearchLineEdit::kCompletionsEnabledDefault));
checkBoxEnableSearchHistoryShortcuts->setChecked(m_pConfig->getValue(
kEnableSearchHistoryShortcutsConfigKey,
WSearchLineEdit::kHistoryShortcutsEnabledDefault));

m_originalTrackTableFont = m_pLibrary->getTrackTableFont();
m_iOriginalTrackTableRowHeight = m_pLibrary->getTrackTableRowHeight();
spinBoxRowHeight->setValue(m_iOriginalTrackTableRowHeight);
Expand Down Expand Up @@ -413,6 +425,12 @@ void DlgPrefLibrary::slotApply() {
m_pConfig->set(kUseRelativePathOnExportConfigKey,
ConfigValue((int)checkBox_use_relative_path->isChecked()));

m_pConfig->set(kEnableSearchCompletionsConfigKey,
ConfigValue(checkBoxEnableSearchCompletions->isChecked()));
m_pConfig->set(kEnableSearchHistoryShortcutsConfigKey,
ConfigValue(checkBoxEnableSearchHistoryShortcuts->isChecked()));
updateSearchLineEditHistoryOptions();

m_pConfig->set(ConfigKey("[Library]","ShowRhythmboxLibrary"),
ConfigValue((int)checkBox_show_rhythmbox->isChecked()));
m_pConfig->set(ConfigKey("[Library]","ShowBansheeLibrary"),
Expand Down Expand Up @@ -495,6 +513,15 @@ void DlgPrefLibrary::slotSearchDebouncingTimeoutMillisChanged(int searchDebounci
WSearchLineEdit::setDebouncingTimeoutMillis(searchDebouncingTimeoutMillis);
}

void DlgPrefLibrary::updateSearchLineEditHistoryOptions() {
WSearchLineEdit::setSearchCompletionsEnabled(m_pConfig->getValue<bool>(
kEnableSearchCompletionsConfigKey,
WSearchLineEdit::kCompletionsEnabledDefault));
WSearchLineEdit::setSearchHistoryShortcutsEnabled(m_pConfig->getValue<bool>(
kEnableSearchHistoryShortcutsConfigKey,
WSearchLineEdit::kHistoryShortcutsEnabledDefault));
}

void DlgPrefLibrary::slotSyncTrackMetadataToggled() {
if (isVisible() && checkBox_SyncTrackMetadata->isChecked()) {
mixxx::DlgTrackMetadataExport::showMessageBoxOncePerSession();
Expand Down
1 change: 1 addition & 0 deletions src/preferences/dialog/dlgpreflibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class DlgPrefLibrary : public DlgPreferencePage, public Ui::DlgPrefLibraryDlg {
private:
void initializeDirList();
void setLibraryFont(const QFont& font);
void updateSearchLineEditHistoryOptions();

QStandardItemModel m_dirListModel;
UserSettingsPointer m_pConfig;
Expand Down
17 changes: 17 additions & 0 deletions src/preferences/dialog/dlgpreflibrarydlg.ui
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,21 @@
</widget>
</item>

<item row="4" column="0" colspan="3">
<widget class="QCheckBox" name="checkBoxEnableSearchCompletions">
<property name="text">
<string>Enable search completions</string>
</property>
</widget>
</item>
<item row="5" column="0" colspan="3">
<widget class="QCheckBox" name="checkBoxEnableSearchHistoryShortcuts">
<property name="text">
<string>Enable search history keyboard shortcuts</string>
</property>
</widget>
</item>

</layout>
</widget>
</item>
Expand Down Expand Up @@ -540,6 +555,8 @@
<tabstop>libraryFont</tabstop>
<tabstop>libraryFontButton</tabstop>
<tabstop>searchDebouncingTimeoutSpinBox</tabstop>
<tabstop>checkBoxEnableSearchCompletions</tabstop>
<tabstop>checkBoxEnableSearchHistoryShortcuts</tabstop>
<tabstop>checkBox_show_rhythmbox</tabstop>
<tabstop>checkBox_show_banshee</tabstop>
<tabstop>checkBox_show_itunes</tabstop>
Expand Down
39 changes: 38 additions & 1 deletion src/widget/wsearchlineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
#include <QLineEdit>
#include <QShortcut>
#include <QSizePolicy>
#include <QStringLiteral>
#include <QStyle>

#include "moc_wsearchlineedit.cpp"
#include "preferences/configobject.h"
#include "skin/legacy/skincontext.h"
#include "util/assert.h"
#include "util/logger.h"
#include "util/parented_ptr.h"
#include "wskincolor.h"
#include "wwidget.h"

Expand All @@ -25,6 +28,7 @@ const QColor kDefaultBackgroundColor = QColor(0, 0, 0);

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

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

// Border width, max. 2 px when focused (in official skins)
Expand Down Expand Up @@ -59,16 +63,29 @@ constexpr int WSearchLineEdit::kMaxSearchEntries;

//static
int WSearchLineEdit::s_debouncingTimeoutMillis = kDefaultDebouncingTimeoutMillis;
bool WSearchLineEdit::s_completionsEnabled = kCompletionsEnabledDefault;
bool WSearchLineEdit::s_historyShortcutsEnabled = kHistoryShortcutsEnabledDefault;

//static
void WSearchLineEdit::setDebouncingTimeoutMillis(int debouncingTimeoutMillis) {
s_debouncingTimeoutMillis = verifyDebouncingTimeoutMillis(debouncingTimeoutMillis);
}

// static
void WSearchLineEdit::setSearchCompletionsEnabled(bool searchCompletionsEnabled) {
s_completionsEnabled = searchCompletionsEnabled;
}

// static
void WSearchLineEdit::setSearchHistoryShortcutsEnabled(bool searchHistoryShortcutsEnabled) {
s_historyShortcutsEnabled = searchHistoryShortcutsEnabled;
}

WSearchLineEdit::WSearchLineEdit(QWidget* pParent, UserSettingsPointer pConfig)
: QComboBox(pParent),
WBaseWidget(this),
m_pConfig(pConfig),
m_completer(make_parented<QCompleter>(this)),
m_clearButton(make_parented<QToolButton>(this)),
m_queryEmitted(false) {
qRegisterMetaType<FocusWidget>("FocusWidget");
Expand All @@ -83,6 +100,10 @@ WSearchLineEdit::WSearchLineEdit(QWidget* pParent, UserSettingsPointer pConfig)
//: Shown in the library search bar when it is empty.
lineEdit()->setPlaceholderText(tr("Search..."));

m_completer->setModel(model());
m_completer->setCompletionMode(QCompleter::CompletionMode::InlineCompletion);
updateCompleter();

// The goal is to make Esc natively close the popup, while in the line edit it
// should move the keyboard focus to the tracks table. Unfortunately, eventFilter()
// can't catch Esc before the popup is closed, and keyPressEvent() can't catch
Expand Down Expand Up @@ -327,6 +348,9 @@ void WSearchLineEdit::keyPressEvent(QKeyEvent* keyEvent) {
// If we're at the top of the list the Up key clears the search bar,
// no matter if it's a saved or unsaved query.
// Otherwise Up is handled by the combobox itself.
if (!s_historyShortcutsEnabled) {
return;
}
currentTextIndex = findCurrentTextIndex();
if (currentTextIndex == 0 ||
(currentTextIndex == -1 && !currentText().isEmpty())) {
Expand All @@ -337,6 +361,9 @@ void WSearchLineEdit::keyPressEvent(QKeyEvent* keyEvent) {
case Qt::Key_Down:
// After clearing the text field the Down key
// is expected to show the latest query
if (!s_historyShortcutsEnabled) {
return;
}
if (currentText().isEmpty()) {
setCurrentIndex(0);
return;
Expand Down Expand Up @@ -364,7 +391,7 @@ void WSearchLineEdit::keyPressEvent(QKeyEvent* keyEvent) {
return;
case Qt::Key_Space:
// Open/close popup with Ctrl + space
if (keyEvent->modifiers() == Qt::ControlModifier) {
if (s_historyShortcutsEnabled && keyEvent->modifiers() == Qt::ControlModifier) {
if (view()->isVisible()) {
hidePopup();
} else {
Expand Down Expand Up @@ -400,6 +427,7 @@ void WSearchLineEdit::focusInEvent(QFocusEvent* event) {
<< "focusInEvent";
#endif // ENABLE_TRACE_LOG
QComboBox::focusInEvent(event);
updateCompleter();
updateClearAndDropdownButton(currentText());
}

Expand Down Expand Up @@ -654,6 +682,15 @@ void WSearchLineEdit::updateClearAndDropdownButton(const QString& text) {
setStyleSheet(styleSheet);
}

void WSearchLineEdit::updateCompleter() {
#if ENABLE_TRACE_LOG
kLogger.trace()
<< "updateCompleter";
#endif // ENABLE_TRACE_LOG

lineEdit()->setCompleter(s_completionsEnabled ? m_completer.toWeakRef() : nullptr);
}

bool WSearchLineEdit::event(QEvent* pEvent) {
if (pEvent->type() == QEvent::ToolTip) {
updateTooltip();
Expand Down
9 changes: 9 additions & 0 deletions src/widget/wsearchlineedit.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <QComboBox>
#include <QCompleter>
#include <QDomNode>
#include <QEvent>
#include <QTimer>
Expand All @@ -22,9 +23,13 @@ class WSearchLineEdit : public QComboBox, public WBaseWidget {
static constexpr int kMaxDebouncingTimeoutMillis = 9999;
static constexpr int kSaveTimeoutMillis = 5000;
static constexpr int kMaxSearchEntries = 50;
static constexpr bool kCompletionsEnabledDefault = true;
static constexpr bool kHistoryShortcutsEnabledDefault = true;

// TODO(XXX): Replace with a public slot
static void setDebouncingTimeoutMillis(int debouncingTimeoutMillis);
static void setSearchCompletionsEnabled(bool searchCompletionsEnabled);
static void setSearchHistoryShortcutsEnabled(bool searchHistoryShortcutsEnabled);
virtual void showPopup() override;

explicit WSearchLineEdit(QWidget* pParent, UserSettingsPointer pConfig = nullptr);
Expand Down Expand Up @@ -74,12 +79,15 @@ class WSearchLineEdit : public QComboBox, public WBaseWidget {
// value provider that sends signals whenever the corresponding
// configuration value changes.
static int s_debouncingTimeoutMillis;
static bool s_completionsEnabled;
static bool s_historyShortcutsEnabled;

void refreshState();

void enableSearch(const QString& text);
void updateEditBox(const QString& text);
void updateClearAndDropdownButton(const QString& text);
void updateCompleter();
void deleteSelectedComboboxItem();
void deleteSelectedListItem();

Expand All @@ -96,6 +104,7 @@ class WSearchLineEdit : public QComboBox, public WBaseWidget {
void loadQueriesFromConfig();
void saveQueriesInConfig();

parented_ptr<QCompleter> m_completer;
parented_ptr<QToolButton> const m_clearButton;

int m_innerHeight;
Expand Down

0 comments on commit 89f5860

Please sign in to comment.