Skip to content

Commit

Permalink
WSearchLineEdit: Use custom QCompleter
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Nov 24, 2022
1 parent 0db64de commit d1a78df
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/widget/wsearchlineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#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 Down Expand Up @@ -86,6 +87,7 @@ 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 @@ -100,6 +102,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 @@ -689,7 +695,7 @@ void WSearchLineEdit::updateCompleter() {
<< text;
#endif // ENABLE_TRACE_LOG

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

bool WSearchLineEdit::event(QEvent* pEvent) {
Expand Down
3 changes: 3 additions & 0 deletions src/widget/wsearchlineedit.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <qcompleter.h>

#include <QComboBox>
#include <QDomNode>
#include <QEvent>
Expand Down Expand Up @@ -103,6 +105,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 d1a78df

Please sign in to comment.