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.
  • Loading branch information
poelzi committed Oct 13, 2020
1 parent d1a0c3b commit 63f8825
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
36 changes: 21 additions & 15 deletions src/widget/wsearchlineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,7 @@ WSearchLineEdit::WSearchLineEdit(QWidget* pParent)
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 +215,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 Down Expand Up @@ -341,14 +346,14 @@ void WSearchLineEdit::slotSaveSearch() {
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);
// 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 Down Expand Up @@ -466,5 +471,6 @@ void WSearchLineEdit::slotSetShortcutFocus() {

// Use the same font as the library table and the sidebar
void WSearchLineEdit::slotSetFont(const QFont& font) {
updateStyleMetrics();
setFont(font);
}
1 change: 1 addition & 0 deletions src/widget/wsearchlineedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,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 63f8825

Please sign in to comment.