From b1c38149729d69a0c2abf916c43bc30db6b89493 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Tue, 18 Nov 2014 16:26:04 +0900 Subject: [PATCH] Make Ctrl+F not toggle the search mode but always enable it. Switching back from other applications, the previous behavior of Ctrl+F would often bother you in that it would dismiss the search widget if it was already enabled when you meant by the key you wanted to perform a search. Making Ctrl+F always set you in search mode should save user from having to care about the mode which is persistent across application switching and database locking. --- src/gui/DatabaseWidget.cpp | 16 ++++++++++++++++ src/gui/DatabaseWidget.h | 1 + src/gui/MainWindow.cpp | 15 +++++++++------ src/gui/MainWindow.ui | 7 ++++++- tests/gui/TestGui.cpp | 33 ++++++++++++++++++++++++--------- 5 files changed, 56 insertions(+), 16 deletions(-) diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index cc9c5fd1..df321424 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -702,6 +702,22 @@ void DatabaseWidget::switchToImportKeepass1(const QString& fileName) setCurrentWidget(m_keepass1OpenWidget); } +void DatabaseWidget::openSearch() +{ + if (isInSearchMode()) { + m_searchUi->searchEdit->selectAll(); + + if (!m_searchUi->searchEdit->hasFocus()) { + m_searchUi->searchEdit->setFocus(); + // make sure the search action is checked again + emitCurrentModeChanged(); + } + } + else { + showSearch(); + } +} + void DatabaseWidget::toggleSearch() { if (isInSearchMode()) { diff --git a/src/gui/DatabaseWidget.h b/src/gui/DatabaseWidget.h index cbab175e..821a21d5 100644 --- a/src/gui/DatabaseWidget.h +++ b/src/gui/DatabaseWidget.h @@ -117,6 +117,7 @@ public Q_SLOTS: void switchToOpenDatabase(const QString& fileName); void switchToOpenDatabase(const QString& fileName, const QString& password, const QString& keyFile); void switchToImportKeepass1(const QString& fileName); + void openSearch(); void toggleSearch(); private Q_SLOTS: diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index dd77989c..1933ef4d 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -119,7 +119,7 @@ MainWindow::MainWindow() m_ui->actionAbout->setIcon(filePath()->icon("actions", "help-about")); - m_ui->actionSearch->setIcon(filePath()->icon("actions", "system-search")); + m_ui->actionToggleSearch->setIcon(filePath()->icon("actions", "system-search")); m_actionMultiplexer.connect(SIGNAL(currentModeChanged(DatabaseWidget::Mode)), this, SLOT(setMenuActionState(DatabaseWidget::Mode))); @@ -200,8 +200,10 @@ MainWindow::MainWindow() connect(m_ui->actionAbout, SIGNAL(triggered()), SLOT(showAboutDialog())); - m_actionMultiplexer.connect(m_ui->actionSearch, SIGNAL(triggered()), + m_actionMultiplexer.connect(m_ui->actionToggleSearch, SIGNAL(triggered()), SLOT(toggleSearch())); + m_actionMultiplexer.connect(m_ui->actionSearch, SIGNAL(triggered()), + SLOT(openSearch())); updateTrayIcon(); } @@ -295,9 +297,10 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode) m_ui->actionGroupNew->setEnabled(groupSelected); m_ui->actionGroupEdit->setEnabled(groupSelected); m_ui->actionGroupDelete->setEnabled(groupSelected && dbWidget->canDeleteCurrentGroup()); - m_ui->actionSearch->setEnabled(true); // TODO: get checked state from db widget - m_ui->actionSearch->setChecked(inSearch); + m_ui->actionSearch->setEnabled(true); + m_ui->actionToggleSearch->setEnabled(true); + m_ui->actionToggleSearch->setChecked(inSearch); m_ui->actionChangeMasterKey->setEnabled(true); m_ui->actionChangeDatabaseSettings->setEnabled(true); m_ui->actionDatabaseSave->setEnabled(true); @@ -321,7 +324,7 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode) m_ui->menuEntryCopyAttribute->setEnabled(false); m_ui->actionSearch->setEnabled(false); - m_ui->actionSearch->setChecked(false); + m_ui->actionToggleSearch->setEnabled(false); m_ui->actionChangeMasterKey->setEnabled(false); m_ui->actionChangeDatabaseSettings->setEnabled(false); m_ui->actionDatabaseSave->setEnabled(false); @@ -348,7 +351,7 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode) m_ui->menuEntryCopyAttribute->setEnabled(false); m_ui->actionSearch->setEnabled(false); - m_ui->actionSearch->setChecked(false); + m_ui->actionToggleSearch->setEnabled(false); m_ui->actionChangeMasterKey->setEnabled(false); m_ui->actionChangeDatabaseSettings->setEnabled(false); m_ui->actionDatabaseSave->setEnabled(false); diff --git a/src/gui/MainWindow.ui b/src/gui/MainWindow.ui index 13c5d679..838bb5f3 100644 --- a/src/gui/MainWindow.ui +++ b/src/gui/MainWindow.ui @@ -177,7 +177,7 @@ - + @@ -304,6 +304,11 @@ + + Find + + + true diff --git a/tests/gui/TestGui.cpp b/tests/gui/TestGui.cpp index 326c3497..592f57ec 100644 --- a/tests/gui/TestGui.cpp +++ b/tests/gui/TestGui.cpp @@ -166,24 +166,39 @@ void TestGui::testAddEntry() void TestGui::testSearch() { - QAction* searchAction = m_mainWindow->findChild("actionSearch"); - QVERIFY(searchAction->isEnabled()); + QAction* toggleSearchAction = m_mainWindow->findChild("actionToggleSearch"); + QVERIFY(toggleSearchAction->isEnabled()); QToolBar* toolBar = m_mainWindow->findChild("toolBar"); - QWidget* searchActionWidget = toolBar->widgetForAction(searchAction); - QVERIFY(searchActionWidget->isEnabled()); - QTest::mouseClick(searchActionWidget, Qt::LeftButton); - + QWidget* toggleSearchActionWidget = toolBar->widgetForAction(toggleSearchAction); EntryView* entryView = m_dbWidget->findChild("entryView"); QLineEdit* searchEdit = m_dbWidget->findChild("searchEdit"); QToolButton* clearSearch = m_dbWidget->findChild("clearButton"); + QVERIFY(!searchEdit->hasFocus()); + + // Toggle + QTest::mouseClick(toggleSearchActionWidget, Qt::LeftButton); + QTRY_VERIFY(searchEdit->hasFocus()); + // Search for "ZZZ" QTest::keyClicks(searchEdit, "ZZZ"); - QTRY_COMPARE(entryView->model()->rowCount(), 0); - + // Escape + QTest::keyClick(m_mainWindow, Qt::Key_Escape); + QTRY_VERIFY(!searchEdit->hasFocus()); + // Toggle again + QTest::mouseClick(toggleSearchActionWidget, Qt::LeftButton); + QTRY_VERIFY(searchEdit->hasFocus()); + // Input and clear + QTest::keyClicks(searchEdit, "ZZZ"); + QTRY_COMPARE(searchEdit->text(), QString("ZZZ")); QTest::mouseClick(clearSearch, Qt::LeftButton); + QTRY_COMPARE(searchEdit->text(), QString("")); + // Ctrl+F should select the current text + QTest::keyClicks(searchEdit, "ZZZ"); + QTest::keyClick(m_mainWindow, Qt::Key_F, Qt::ControlModifier); + QTRY_VERIFY(searchEdit->hasFocus()); + // Search for "some" QTest::keyClicks(searchEdit, "some"); - QTRY_COMPARE(entryView->model()->rowCount(), 4); clickIndex(entryView->model()->index(0, 1), entryView, Qt::LeftButton);