Skip to content

Commit

Permalink
Fix resize problem, update history behaviour
Browse files Browse the repository at this point in the history
Update internal metrics to calculate button positions on font changes.

Move the new entry to the top of the list when an old entry was found.
This way the search behaves more like expected.
Avoid duplicates when Enter is pressed.
Save new query term if keydown is pressed or popup opened.
  • Loading branch information
poelzi committed Oct 13, 2020
1 parent d1a0c3b commit b992c17
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 20 deletions.
80 changes: 60 additions & 20 deletions src/widget/wsearchlineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,12 @@ WSearchLineEdit::WSearchLineEdit(QWidget* pParent)

//: Shown in the library search bar when it is empty.
lineEdit()->setPlaceholderText(tr("Search..."));
installEventFilter(this);

m_clearButton->setCursor(Qt::ArrowCursor);
m_clearButton->setObjectName(QStringLiteral("SearchClearButton"));
// Query style for arrow width and frame border
QStyleOptionComboBox styleArrow;
styleArrow.initFrom(this);
QRect rectArrow(style()->subControlRect(
QStyle::CC_ComboBox, &styleArrow, QStyle::SC_ComboBoxArrow, this));

m_dropButtonWidth = rectArrow.width() + 1;
m_frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, nullptr, this);
updateStyleMetrics();

m_clearButton->hide();
connect(m_clearButton,
Expand Down Expand Up @@ -221,8 +216,19 @@ void WSearchLineEdit::setup(const QDomNode& node, const SkinContext& context) {
tr("Esc") + " " + tr("Exit search", "Exit search bar and leave focus"));
}

void WSearchLineEdit::updateStyleMetrics() {
QStyleOptionComboBox styleArrow;
styleArrow.initFrom(this);
QRect rectArrow(style()->subControlRect(
QStyle::CC_ComboBox, &styleArrow, QStyle::SC_ComboBoxArrow, this));

m_dropButtonWidth = rectArrow.width() + 1;
m_frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, nullptr, this);
}

void WSearchLineEdit::resizeEvent(QResizeEvent* e) {
QComboBox::resizeEvent(e);
updateStyleMetrics();
m_innerHeight = this->height() - 2 * m_frameWidth;
// Test if this is a vertical resize due to changed library font.
// Assuming current button height is innerHeight from last resize,
Expand All @@ -239,7 +245,7 @@ void WSearchLineEdit::resizeEvent(QResizeEvent* e) {
if (layoutDirection() == Qt::LeftToRight) {
m_clearButton->move(rect().right() - m_innerHeight - m_frameWidth - m_dropButtonWidth, top);
} else {
m_clearButton->move(m_frameWidth, top);
m_clearButton->move(m_frameWidth + m_dropButtonWidth, top);
}
}

Expand All @@ -252,6 +258,25 @@ QString WSearchLineEdit::getSearchText() const {
}
}

bool WSearchLineEdit::eventFilter(QObject* obj, QEvent* event) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Down) {
// in case the user entered a new search query
// und presses the down key, save the query for later recall
if (findData(currentText(), Qt::DisplayRole) == -1) {
slotSaveSearch();
}
} else if (keyEvent->key() == Qt::Key_Enter) {
if (findData(currentText(), Qt::DisplayRole) == -1) {
slotSaveSearch();
}
return true;
}
}
return QComboBox::eventFilter(obj, event);
}

void WSearchLineEdit::focusInEvent(QFocusEvent* event) {
#if ENABLE_TRACE_LOG
kLogger.trace()
Expand Down Expand Up @@ -336,19 +361,22 @@ void WSearchLineEdit::slotTriggerSearch() {

/// saves the current query as selection
void WSearchLineEdit::slotSaveSearch() {
qDebug() << "save search" << findData(currentText(), Qt::DisplayRole);
DEBUG_ASSERT(isEnabled());
int cIndex = findData(currentText(), Qt::DisplayRole);
qDebug() << "save search. Index: " << cIndex;
m_saveTimer.stop();
if (currentText().length()) {
int cIndex = findData(currentText(), Qt::DisplayRole);
if (cIndex == -1) {
insertItem(0, currentText());
setCurrentIndex(0);
while (count() > kMaxSearchEntries) {
removeItem(kMaxSearchEntries);
}
} else {
setCurrentIndex(cIndex);
// entry already exists and is on top
if (cIndex == 0) {
return;
}
if (currentText().length() && isEnabled()) {
// we remove the existing item and add a new one at the top
if (cIndex != -1) {
removeItem(cIndex);
}
insertItem(0, currentText());
setCurrentIndex(0);
while (count() > kMaxSearchEntries) {
removeItem(kMaxSearchEntries);
}
}
}
Expand All @@ -365,6 +393,17 @@ void WSearchLineEdit::refreshState() {
}
}

void WSearchLineEdit::showPopup() {
int cIndex = findData(currentText(), Qt::DisplayRole);
if (cIndex == -1) {
slotSaveSearch();
} else {
m_saveTimer.stop();
setCurrentIndex(cIndex);
}
QComboBox::showPopup();
}

void WSearchLineEdit::updateEditBox(const QString& text) {
#if ENABLE_TRACE_LOG
kLogger.trace()
Expand Down Expand Up @@ -466,5 +505,6 @@ void WSearchLineEdit::slotSetShortcutFocus() {

// Use the same font as the library table and the sidebar
void WSearchLineEdit::slotSetFont(const QFont& font) {
updateStyleMetrics();
setFont(font);
}
3 changes: 3 additions & 0 deletions src/widget/wsearchlineedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class WSearchLineEdit : public QComboBox, public WBaseWidget {

// TODO(XXX): Replace with a public slot
static void setDebouncingTimeoutMillis(int debouncingTimeoutMillis);
virtual void showPopup();

explicit WSearchLineEdit(QWidget* pParent);
~WSearchLineEdit() override = default;
Expand All @@ -34,6 +35,7 @@ class WSearchLineEdit : public QComboBox, public WBaseWidget {
void focusInEvent(QFocusEvent*) override;
void focusOutEvent(QFocusEvent*) override;
bool event(QEvent*) override;
bool eventFilter(QObject* obj, QEvent* event) override;

signals:
void search(const QString& text);
Expand Down Expand Up @@ -67,6 +69,7 @@ class WSearchLineEdit : public QComboBox, public WBaseWidget {
void enableSearch(const QString& text);
void updateEditBox(const QString& text);
void updateClearButton(const QString& text);
void updateStyleMetrics();

QString getSearchText() const;

Expand Down

0 comments on commit b992c17

Please sign in to comment.