Skip to content

Commit

Permalink
WSearchLineEdit: Model search history options as statics
Browse files Browse the repository at this point in the history
As per the PR suggestions:

- #10942 (comment)
- #10942 (comment)
  • Loading branch information
fwcd committed Nov 24, 2022
1 parent a2c892f commit 0db64de
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 33 deletions.
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
35 changes: 20 additions & 15 deletions src/preferences/dialog/dlgpreflibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@

using namespace mixxx::library::prefs;

namespace {

const QString kEnableSearchCompletionsKey = QStringLiteral("EnableSearchCompletions");
const QString kEnableSearchHistoryShortcutsKey = QStringLiteral("EnableSearchHistoryShortcuts");

} // namespace

DlgPrefLibrary::DlgPrefLibrary(
QWidget* pParent,
UserSettingsPointer pConfig,
Expand Down Expand Up @@ -97,12 +90,7 @@ DlgPrefLibrary::DlgPrefLibrary(
this,
&DlgPrefLibrary::slotSearchDebouncingTimeoutMillisChanged);

checkBoxEnableSearchCompletions->setChecked(m_pConfig->getValue(
ConfigKey(kConfigGroup, kEnableSearchCompletionsKey),
WSearchLineEdit::kEnableSearchCompletionsByDefault));
checkBoxEnableSearchHistoryShortcuts->setChecked(m_pConfig->getValue(
ConfigKey(kConfigGroup, kEnableSearchHistoryShortcutsKey),
WSearchLineEdit::kEnableSearchHistoryShortcutsByDefault));
searchHistoryOptionsChanged();

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

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

checkBoxEnableSearchCompletions->setChecked(m_pConfig->getValue(
kEnableSearchCompletionsConfigKey,
WSearchLineEdit::kEnableSearchCompletionsByDefault));
checkBoxEnableSearchHistoryShortcuts->setChecked(m_pConfig->getValue(
kEnableSearchHistoryShortcutsConfigKey,
WSearchLineEdit::kEnableSearchHistoryShortcutsByDefault));

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

m_pConfig->set(ConfigKey(kConfigGroup, kEnableSearchCompletionsKey),
m_pConfig->set(kEnableSearchCompletionsConfigKey,
ConfigValue(checkBoxEnableSearchCompletions->isChecked()));
m_pConfig->set(ConfigKey(kConfigGroup, kEnableSearchHistoryShortcutsKey),
m_pConfig->set(kEnableSearchHistoryShortcutsConfigKey,
ConfigValue(checkBoxEnableSearchHistoryShortcuts->isChecked()));
searchHistoryOptionsChanged();

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

void DlgPrefLibrary::searchHistoryOptionsChanged() {
WSearchLineEdit::setSearchCompletionsEnabled(m_pConfig->getValue<bool>(
kEnableSearchCompletionsConfigKey,
WSearchLineEdit::kEnableSearchCompletionsByDefault));
WSearchLineEdit::setSearchHistoryShortcutsEnabled(m_pConfig->getValue<bool>(
kEnableSearchHistoryShortcutsConfigKey,
WSearchLineEdit::kEnableSearchHistoryShortcutsByDefault));
}

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 searchHistoryOptionsChanged();

QStandardItemModel m_dirListModel;
UserSettingsPointer m_pConfig;
Expand Down
46 changes: 28 additions & 18 deletions src/widget/wsearchlineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ const QString kDisabledText = QStringLiteral("- - -");
const QString kLibraryConfigGroup = QStringLiteral("[Library]");
const QString kSavedQueriesConfigGroup = QStringLiteral("[SearchQueries]");

const QString kEnableSearchCompletionsKey = QStringLiteral("EnableSearchCompletions");
const QString kEnableSearchHistoryShortcutsKey = QStringLiteral("EnableSearchHistoryShortcuts");

// Border width, max. 2 px when focused (in official skins)
constexpr int kBorderWidth = 2;

Expand Down Expand Up @@ -67,12 +64,24 @@ constexpr int WSearchLineEdit::kMaxSearchEntries;

//static
int WSearchLineEdit::s_debouncingTimeoutMillis = kDefaultDebouncingTimeoutMillis;
bool WSearchLineEdit::s_searchCompletionsEnabled = kEnableSearchCompletionsByDefault;
bool WSearchLineEdit::s_searchHistoryShortcutsEnabled = kEnableSearchHistoryShortcutsByDefault;

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

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

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

WSearchLineEdit::WSearchLineEdit(QWidget* pParent, UserSettingsPointer pConfig)
: QComboBox(pParent),
WBaseWidget(this),
Expand Down Expand Up @@ -321,8 +330,6 @@ bool WSearchLineEdit::eventFilter(QObject* obj, QEvent* event) {

void WSearchLineEdit::keyPressEvent(QKeyEvent* keyEvent) {
int currentTextIndex = 0;
bool isHistoryEnabled = m_pConfig->getValue<bool>(
ConfigKey(kLibraryConfigGroup, kEnableSearchHistoryShortcutsKey));
switch (keyEvent->key()) {
// Ctrl + F is handled in slotSetShortcutFocus()
case Qt::Key_Backspace:
Expand All @@ -337,7 +344,7 @@ 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 (!isHistoryEnabled) {
if (!s_searchHistoryShortcutsEnabled) {
return;
}
currentTextIndex = findCurrentTextIndex();
Expand All @@ -350,7 +357,7 @@ 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 (!isHistoryEnabled) {
if (!s_searchHistoryShortcutsEnabled) {
return;
}
if (currentText().isEmpty()) {
Expand Down Expand Up @@ -380,7 +387,7 @@ void WSearchLineEdit::keyPressEvent(QKeyEvent* keyEvent) {
return;
case Qt::Key_Space:
// Open/close popup with Ctrl + space
if (isHistoryEnabled && keyEvent->modifiers() == Qt::ControlModifier) {
if (s_searchHistoryShortcutsEnabled && keyEvent->modifiers() == Qt::ControlModifier) {
if (view()->isVisible()) {
hidePopup();
} else {
Expand Down Expand Up @@ -416,6 +423,7 @@ void WSearchLineEdit::focusInEvent(QFocusEvent* event) {
<< "focusInEvent";
#endif // ENABLE_TRACE_LOG
QComboBox::focusInEvent(event);
updateCompleter();
updateClearAndDropdownButton(currentText());
}

Expand Down Expand Up @@ -590,10 +598,6 @@ void WSearchLineEdit::refreshState() {
} else {
slotDisableSearch();
}

bool isCompletionEnabled = m_pConfig->getValue<bool>(
ConfigKey(kLibraryConfigGroup, kEnableSearchCompletionsKey));
lineEdit()->setCompleter(isCompletionEnabled ? completer() : nullptr);
}

void WSearchLineEdit::showPopup() {
Expand Down Expand Up @@ -659,12 +663,8 @@ void WSearchLineEdit::updateClearAndDropdownButton(const QString& text) {
const int paddingPx = text.isEmpty() ? 0 : m_innerHeight;
const QString clearPos(layoutDirection() == Qt::RightToLeft ? "left" : "right");

// Hide the nonfunctional drop-down button (set width to 0) if the search or history is disabled.
const bool isHistoryEnabled = m_pConfig->getValue<bool>(
ConfigKey(kLibraryConfigGroup, kEnableSearchHistoryShortcutsKey));
const int dropDownWidth = isEnabled() && isHistoryEnabled
? static_cast<int>(m_innerHeight * 0.7)
: 0;
// Hide the nonfunctional drop-down button (set width to 0) if the search is disabled.
const int dropDownWidth = isEnabled() ? static_cast<int>(m_innerHeight * 0.7) : 0;

const QString styleSheet = QStringLiteral(
"WSearchLineEdit { padding-%1: %2px; }"
Expand All @@ -682,6 +682,16 @@ void WSearchLineEdit::updateClearAndDropdownButton(const QString& text) {
setStyleSheet(styleSheet);
}

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

lineEdit()->setCompleter(s_searchCompletionsEnabled ? completer() : nullptr);
}

bool WSearchLineEdit::event(QEvent* pEvent) {
if (pEvent->type() == QEvent::ToolTip) {
updateTooltip();
Expand Down
5 changes: 5 additions & 0 deletions src/widget/wsearchlineedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class WSearchLineEdit : public QComboBox, public WBaseWidget {

// 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 @@ -76,12 +78,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_searchCompletionsEnabled;
static bool s_searchHistoryShortcutsEnabled;

void refreshState();

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

Expand Down

0 comments on commit 0db64de

Please sign in to comment.