Merge pull request #654 from weslly/feature/search-scope-option

Add option to limit search to current group
This commit is contained in:
Weslly 2017-06-21 16:35:53 -03:00 committed by GitHub
commit 3c2b4b1560
6 changed files with 43 additions and 1 deletions

View File

@ -114,6 +114,7 @@ void Config::init(const QString& fileName)
m_defaults.insert("AutoReloadOnChange", true); m_defaults.insert("AutoReloadOnChange", true);
m_defaults.insert("AutoSaveOnExit", false); m_defaults.insert("AutoSaveOnExit", false);
m_defaults.insert("ShowToolbar", true); m_defaults.insert("ShowToolbar", true);
m_defaults.insert("SearchLimitGroup", false);
m_defaults.insert("MinimizeOnCopy", false); m_defaults.insert("MinimizeOnCopy", false);
m_defaults.insert("UseGroupIconOnEntryCreation", false); m_defaults.insert("UseGroupIconOnEntryCreation", false);
m_defaults.insert("AutoTypeEntryTitleMatch", true); m_defaults.insert("AutoTypeEntryTitleMatch", true);

View File

@ -186,6 +186,7 @@ DatabaseWidget::DatabaseWidget(Database* db, QWidget* parent)
m_ignoreAutoReload = false; m_ignoreAutoReload = false;
m_searchCaseSensitive = false; m_searchCaseSensitive = false;
m_searchLimitGroup = config()->get("SearchLimitGroup", false).toBool();
setCurrentWidget(m_mainWidget); setCurrentWidget(m_mainWidget);
} }
@ -963,7 +964,9 @@ void DatabaseWidget::search(const QString& searchtext)
Qt::CaseSensitivity caseSensitive = m_searchCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive; Qt::CaseSensitivity caseSensitive = m_searchCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive;
QList<Entry*> searchResult = EntrySearcher().search(searchtext, currentGroup(), caseSensitive); Group* searchGroup = m_searchLimitGroup ? currentGroup() : m_db->rootGroup();
QList<Entry*> searchResult = EntrySearcher().search(searchtext, searchGroup, caseSensitive);
m_entryView->setEntryList(searchResult); m_entryView->setEntryList(searchResult);
m_lastSearchText = searchtext; m_lastSearchText = searchtext;
@ -987,6 +990,12 @@ void DatabaseWidget::setSearchCaseSensitive(bool state)
refreshSearch(); refreshSearch();
} }
void DatabaseWidget::setSearchLimitGroup(bool state)
{
m_searchLimitGroup = state;
refreshSearch();
}
void DatabaseWidget::onGroupChanged(Group* group) void DatabaseWidget::onGroupChanged(Group* group)
{ {
// Intercept group changes if in search mode // Intercept group changes if in search mode

View File

@ -163,6 +163,7 @@ public slots:
// Search related slots // Search related slots
void search(const QString& searchtext); void search(const QString& searchtext);
void setSearchCaseSensitive(bool state); void setSearchCaseSensitive(bool state);
void setSearchLimitGroup(bool state);
void endSearch(); void endSearch();
void showMessage(const QString& text, MessageWidget::MessageType type); void showMessage(const QString& text, MessageWidget::MessageType type);
@ -221,6 +222,7 @@ private:
// Search state // Search state
QString m_lastSearchText; QString m_lastSearchText;
bool m_searchCaseSensitive; bool m_searchCaseSensitive;
bool m_searchLimitGroup;
// Autoreload // Autoreload
QFileSystemWatcher m_fileWatcher; QFileSystemWatcher m_fileWatcher;

View File

@ -24,6 +24,7 @@
#include <QShortcut> #include <QShortcut>
#include <QToolButton> #include <QToolButton>
#include "core/Config.h"
#include "core/FilePath.h" #include "core/FilePath.h"
SearchWidget::SearchWidget(QWidget* parent) SearchWidget::SearchWidget(QWidget* parent)
@ -50,6 +51,11 @@ SearchWidget::SearchWidget(QWidget* parent)
m_actionCaseSensitive->setObjectName("actionSearchCaseSensitive"); m_actionCaseSensitive->setObjectName("actionSearchCaseSensitive");
m_actionCaseSensitive->setCheckable(true); m_actionCaseSensitive->setCheckable(true);
m_actionLimitGroup = searchMenu->addAction(tr("Limit search to selected group"), this, SLOT(updateLimitGroup()));
m_actionLimitGroup->setObjectName("actionSearchLimitGroup");
m_actionLimitGroup->setCheckable(true);
m_actionLimitGroup->setChecked(config()->get("SearchLimitGroup", false).toBool());
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->searchEdit->addAction(m_ui->searchIcon, QLineEdit::LeadingPosition); m_ui->searchEdit->addAction(m_ui->searchIcon, QLineEdit::LeadingPosition);
@ -102,6 +108,7 @@ void SearchWidget::connectSignals(SignalMultiplexer& mx)
{ {
mx.connect(this, SIGNAL(search(QString)), SLOT(search(QString))); mx.connect(this, SIGNAL(search(QString)), SLOT(search(QString)));
mx.connect(this, SIGNAL(caseSensitiveChanged(bool)), SLOT(setSearchCaseSensitive(bool))); mx.connect(this, SIGNAL(caseSensitiveChanged(bool)), SLOT(setSearchCaseSensitive(bool)));
mx.connect(this, SIGNAL(limitGroupChanged(bool)), SLOT(setSearchLimitGroup(bool)));
mx.connect(this, SIGNAL(copyPressed()), SLOT(copyPassword())); mx.connect(this, SIGNAL(copyPressed()), SLOT(copyPassword()));
mx.connect(this, SIGNAL(downPressed()), SLOT(setFocus())); mx.connect(this, SIGNAL(downPressed()), SLOT(setFocus()));
mx.connect(m_ui->searchEdit, SIGNAL(returnPressed()), SLOT(switchToEntryEdit())); mx.connect(m_ui->searchEdit, SIGNAL(returnPressed()), SLOT(switchToEntryEdit()));
@ -115,6 +122,7 @@ void SearchWidget::databaseChanged(DatabaseWidget* dbWidget)
// Enforce search policy // Enforce search policy
emit caseSensitiveChanged(m_actionCaseSensitive->isChecked()); emit caseSensitiveChanged(m_actionCaseSensitive->isChecked());
emit limitGroupChanged(m_actionLimitGroup->isChecked());
} else { } else {
m_ui->searchEdit->clear(); m_ui->searchEdit->clear();
} }
@ -151,6 +159,19 @@ void SearchWidget::setCaseSensitive(bool state)
updateCaseSensitive(); updateCaseSensitive();
} }
void SearchWidget::updateLimitGroup()
{
config()->set("SearchLimitGroup", m_actionLimitGroup->isChecked());
emit limitGroupChanged(m_actionLimitGroup->isChecked());
}
void SearchWidget::setLimitGroup(bool state)
{
m_actionLimitGroup->setChecked(state);
updateLimitGroup();
}
void SearchWidget::searchFocus() void SearchWidget::searchFocus()
{ {
m_ui->searchEdit->setFocus(); m_ui->searchEdit->setFocus();

View File

@ -39,6 +39,7 @@ public:
void connectSignals(SignalMultiplexer& mx); void connectSignals(SignalMultiplexer& mx);
void setCaseSensitive(bool state); void setCaseSensitive(bool state);
void setLimitGroup(bool state);
protected: protected:
bool eventFilter(QObject* obj, QEvent* event); bool eventFilter(QObject* obj, QEvent* event);
@ -46,6 +47,7 @@ protected:
signals: signals:
void search(const QString& text); void search(const QString& text);
void caseSensitiveChanged(bool state); void caseSensitiveChanged(bool state);
void limitGroupChanged(bool state);
void escapePressed(); void escapePressed();
void copyPressed(); void copyPressed();
void downPressed(); void downPressed();
@ -58,12 +60,14 @@ private slots:
void startSearchTimer(); void startSearchTimer();
void startSearch(); void startSearch();
void updateCaseSensitive(); void updateCaseSensitive();
void updateLimitGroup();
void searchFocus(); void searchFocus();
private: private:
const QScopedPointer<Ui::SearchWidget> m_ui; const QScopedPointer<Ui::SearchWidget> m_ui;
QTimer* m_searchTimer; QTimer* m_searchTimer;
QAction* m_actionCaseSensitive; QAction* m_actionCaseSensitive;
QAction* m_actionLimitGroup;
Q_DISABLE_COPY(SearchWidget) Q_DISABLE_COPY(SearchWidget)
}; };

View File

@ -577,7 +577,12 @@ void TestGui::testSearch()
QModelIndex rootGroupIndex = groupView->model()->index(0, 0); QModelIndex rootGroupIndex = groupView->model()->index(0, 0);
clickIndex(groupView->model()->index(0, 0, rootGroupIndex), groupView, Qt::LeftButton); clickIndex(groupView->model()->index(0, 0, rootGroupIndex), groupView, Qt::LeftButton);
QCOMPARE(groupView->currentGroup()->name(), QString("General")); QCOMPARE(groupView->currentGroup()->name(), QString("General"));
searchWidget->setLimitGroup(false);
QTRY_COMPARE(entryView->model()->rowCount(), 2);
searchWidget->setLimitGroup(true);
QTRY_COMPARE(entryView->model()->rowCount(), 0); QTRY_COMPARE(entryView->model()->rowCount(), 0);
// reset // reset
clickIndex(rootGroupIndex, groupView, Qt::LeftButton); clickIndex(rootGroupIndex, groupView, Qt::LeftButton);
QCOMPARE(groupView->currentGroup(), m_db->rootGroup()); QCOMPARE(groupView->currentGroup(), m_db->rootGroup());