More compact search widget

* Move the search icon (with popup menu) and clear icon inside the
line edit
* Move the search widget to the right-side of toolbar
This commit is contained in:
Francois Ferrand 2017-04-05 10:42:15 +02:00 committed by Jonathan White
parent b7546b45b3
commit 86e88c18b0
3 changed files with 38 additions and 38 deletions

View File

@ -21,6 +21,7 @@
#include <QKeyEvent> #include <QKeyEvent>
#include <QMenu> #include <QMenu>
#include <QShortcut> #include <QShortcut>
#include <QToolButton>
#include "core/FilePath.h" #include "core/FilePath.h"
@ -34,9 +35,7 @@ SearchWidget::SearchWidget(QWidget *parent)
m_searchTimer->setSingleShot(true); m_searchTimer->setSingleShot(true);
connect(m_ui->searchEdit, SIGNAL(textChanged(QString)), SLOT(startSearchTimer())); connect(m_ui->searchEdit, SIGNAL(textChanged(QString)), SLOT(startSearchTimer()));
connect(m_ui->searchIcon, SIGNAL(pressed()), m_ui->searchEdit, SLOT(setFocus())); connect(m_ui->clearIcon, SIGNAL(triggered(bool)), m_ui->searchEdit, SLOT(clear()));
connect(m_ui->clearIcon, SIGNAL(pressed()), m_ui->searchEdit, SLOT(clear()));
connect(m_ui->clearIcon, SIGNAL(pressed()), m_ui->searchEdit, SLOT(setFocus()));
connect(m_searchTimer, SIGNAL(timeout()), this, SLOT(startSearch())); connect(m_searchTimer, SIGNAL(timeout()), this, SLOT(startSearch()));
connect(this, SIGNAL(escapePressed()), m_ui->searchEdit, SLOT(clear())); connect(this, SIGNAL(escapePressed()), m_ui->searchEdit, SLOT(clear()));
@ -52,10 +51,16 @@ SearchWidget::SearchWidget(QWidget *parent)
m_ui->searchIcon->setIcon(filePath()->icon("actions", "system-search")); m_ui->searchIcon->setIcon(filePath()->icon("actions", "system-search"));
m_ui->searchIcon->setMenu(searchMenu); m_ui->searchIcon->setMenu(searchMenu);
m_ui->searchIcon->setPopupMode(QToolButton::MenuButtonPopup); m_ui->searchEdit->addAction(m_ui->searchIcon, QLineEdit::LeadingPosition);
m_ui->clearIcon->setIcon(filePath()->icon("actions", "edit-clear-locationbar-rtl")); m_ui->clearIcon->setIcon(filePath()->icon("actions", "edit-clear-locationbar-rtl"));
m_ui->clearIcon->setEnabled(false); m_ui->clearIcon->setVisible(false);
m_ui->searchEdit->addAction(m_ui->clearIcon, QLineEdit::TrailingPosition);
// Fix initial visibility of actions (bug in Qt)
for (QToolButton * toolButton: m_ui->searchEdit->findChildren<QToolButton *>()) {
toolButton->setVisible(toolButton->defaultAction()->isVisible());
}
} }
SearchWidget::~SearchWidget() SearchWidget::~SearchWidget()
@ -133,7 +138,7 @@ void SearchWidget::startSearch()
} }
bool hasText = m_ui->searchEdit->text().length() > 0; bool hasText = m_ui->searchEdit->text().length() > 0;
m_ui->clearIcon->setEnabled(hasText); m_ui->clearIcon->setVisible(hasText);
search(m_ui->searchEdit->text()); search(m_ui->searchEdit->text());
} }

View File

@ -30,20 +30,17 @@
<number>0</number> <number>0</number>
</property> </property>
<item> <item>
<widget class="QToolButton" name="searchIcon"> <spacer name="horizontalSpacer">
<property name="focusPolicy"> <property name="orientation">
<enum>Qt::ClickFocus</enum> <enum>Qt::Horizontal</enum>
</property> </property>
<property name="text"> <property name="sizeHint" stdset="0">
<string>Search</string> <size>
<width>40</width>
<height>20</height>
</size>
</property> </property>
<property name="toolButtonStyle"> </spacer>
<enum>Qt::ToolButtonIconOnly</enum>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item> </item>
<item> <item>
<widget class="QLineEdit" name="searchEdit"> <widget class="QLineEdit" name="searchEdit">
@ -51,33 +48,26 @@
<string notr="true">padding:3px</string> <string notr="true">padding:3px</string>
</property> </property>
<property name="placeholderText"> <property name="placeholderText">
<string>Find</string> <string>Search...</string>
</property> </property>
<property name="clearButtonEnabled"> <property name="clearButtonEnabled">
<bool>false</bool> <bool>false</bool>
</property> </property>
</widget> </widget>
</item> </item>
<item> </layout>
<widget class="QToolButton" name="clearIcon"> <action name="searchIcon">
<property name="focusPolicy"> <property name="text">
<enum>Qt::ClickFocus</enum> <string>Search</string>
</property> </property>
</action>
<action name="clearIcon">
<property name="text"> <property name="text">
<string>Clear</string> <string>Clear</string>
</property> </property>
<property name="toolButtonStyle"> </action>
<enum>Qt::ToolButtonIconOnly</enum>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget> </widget>
<tabstops> <tabstops>
<tabstop>searchIcon</tabstop>
<tabstop>searchEdit</tabstop> <tabstop>searchEdit</tabstop>
</tabstops> </tabstops>
<resources/> <resources/>

View File

@ -410,19 +410,24 @@ void TestGui::testSearch()
EntryView* entryView = m_dbWidget->findChild<EntryView*>("entryView"); EntryView* entryView = m_dbWidget->findChild<EntryView*>("entryView");
QVERIFY(entryView->isVisible()); QVERIFY(entryView->isVisible());
QAction* clearButton = searchWidget->findChild<QAction*>("clearIcon");
QVERIFY(!clearButton->isVisible());
// Enter search // Enter search
QTest::mouseClick(searchTextEdit, Qt::LeftButton); QTest::mouseClick(searchTextEdit, Qt::LeftButton);
QTRY_VERIFY(searchTextEdit->hasFocus()); QTRY_VERIFY(searchTextEdit->hasFocus());
QTRY_VERIFY(!clearButton->isVisible());
// Search for "ZZZ" // Search for "ZZZ"
QTest::keyClicks(searchTextEdit, "ZZZ"); QTest::keyClicks(searchTextEdit, "ZZZ");
QTRY_COMPARE(searchTextEdit->text(), QString("ZZZ")); QTRY_COMPARE(searchTextEdit->text(), QString("ZZZ"));
QTRY_VERIFY(clearButton->isVisible());
QTRY_VERIFY(m_dbWidget->isInSearchMode()); QTRY_VERIFY(m_dbWidget->isInSearchMode());
QTRY_COMPARE(entryView->model()->rowCount(), 0); QTRY_COMPARE(entryView->model()->rowCount(), 0);
// Press the search clear button // Press the search clear button
QToolButton* clearButton = searchWidget->findChild<QToolButton*>("clearIcon"); clearButton->trigger();
QTest::mouseClick(clearButton, Qt::LeftButton);
QTRY_VERIFY(searchTextEdit->text().isEmpty()); QTRY_VERIFY(searchTextEdit->text().isEmpty());
QTRY_VERIFY(searchTextEdit->hasFocus()); QTRY_VERIFY(searchTextEdit->hasFocus());
QTRY_VERIFY(!clearButton->isVisible());
// Escape clears searchedit and retains focus // Escape clears searchedit and retains focus
QTest::keyClicks(searchTextEdit, "ZZZ"); QTest::keyClicks(searchTextEdit, "ZZZ");
QTest::keyClick(searchTextEdit, Qt::Key_Escape); QTest::keyClick(searchTextEdit, Qt::Key_Escape);