Cleaned up SearchWidget code

This commit is contained in:
Jonathan White 2016-12-23 23:14:50 -05:00
parent 298665e32a
commit 9660510cc7
2 changed files with 37 additions and 58 deletions

View File

@ -24,39 +24,6 @@
#include "core/FilePath.h"
bool SearchWidget::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Escape) {
emit escapePressed();
return true;
}
else if (keyEvent->matches(QKeySequence::Copy)) {
// If Control+C is pressed in the search edit when no
// text is selected, copy the password of the current
// entry.
if (!m_ui->searchEdit->hasSelectedText()) {
emit copyPressed();
return true;
}
}
else if (keyEvent->matches(QKeySequence::MoveToNextLine)) {
// If Down is pressed at EOL in the search edit, move
// the focus to the entry view.
QLineEdit* searchEdit = m_ui->searchEdit;
if (!searchEdit->hasSelectedText() &&
searchEdit->cursorPosition() == searchEdit->text().length()) {
emit downPressed();
return true;
}
}
}
return QObject::eventFilter(obj, event);
}
SearchWidget::SearchWidget(QWidget *parent)
: QWidget(parent)
, m_ui(new Ui::SearchWidget())
@ -71,8 +38,6 @@ SearchWidget::SearchWidget(QWidget *parent)
connect(m_ui->searchIcon, SIGNAL(triggered(QAction*)), m_ui->searchEdit, SLOT(setFocus()));
connect(m_searchTimer, SIGNAL(timeout()), this, SLOT(startSearch()));
connect(this, SIGNAL(escapePressed()), m_ui->searchEdit, SLOT(clear()));
connect(this, SIGNAL(copyPressed()), SLOT(copyPassword()));
connect(this, SIGNAL(downPressed()), SLOT(setFocusToEntry()));
new QShortcut(Qt::CTRL + Qt::Key_F, m_ui->searchEdit, SLOT(setFocus()), nullptr, Qt::ApplicationShortcut);
@ -93,16 +58,48 @@ SearchWidget::~SearchWidget()
}
bool SearchWidget::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Escape) {
emit escapePressed();
return true;
}
else if (keyEvent->matches(QKeySequence::Copy)) {
// If Control+C is pressed in the search edit when no
// text is selected, copy the password of the current
// entry.
if (!m_ui->searchEdit->hasSelectedText()) {
emit copyPressed();
return true;
}
}
else if (keyEvent->matches(QKeySequence::MoveToNextLine)) {
// If Down is pressed at EOL in the search edit, move
// the focus to the entry view.
QLineEdit* searchEdit = m_ui->searchEdit;
if (!searchEdit->hasSelectedText() &&
searchEdit->cursorPosition() == searchEdit->text().length()) {
emit downPressed();
return true;
}
}
}
return QObject::eventFilter(obj, event);
}
void SearchWidget::connectSignals(SignalMultiplexer& mx)
{
mx.connect(this, SIGNAL(search(QString)), SLOT(search(QString)));
mx.connect(this, SIGNAL(caseSensitiveChanged(bool)), SLOT(setSearchCaseSensitive(bool)));
mx.connect(this, SIGNAL(copyPressed()), SLOT(copyPassword()));
mx.connect(this, SIGNAL(downPressed()), SLOT(setFocus()));
}
void SearchWidget::databaseChanged(DatabaseWidget *dbWidget)
{
m_dbWidget = dbWidget;
if (dbWidget != nullptr) {
// Set current search text from this database
m_ui->searchEdit->setText(dbWidget->getCurrentSearch());
@ -141,15 +138,3 @@ void SearchWidget::setCaseSensitive(bool state)
m_actionCaseSensitive->setChecked(state);
updateCaseSensitive();
}
void SearchWidget::copyPassword()
{
if (m_dbWidget)
m_dbWidget->copyPassword();
}
void SearchWidget::setFocusToEntry()
{
if (m_dbWidget)
m_dbWidget->setFocus();
}

View File

@ -28,7 +28,6 @@ namespace Ui {
class SearchWidget;
}
class SearchWidget : public QWidget
{
Q_OBJECT
@ -40,6 +39,9 @@ public:
void connectSignals(SignalMultiplexer& mx);
void setCaseSensitive(bool state);
protected:
bool eventFilter(QObject *obj, QEvent *event);
signals:
void search(const QString &text);
void caseSensitiveChanged(bool state);
@ -54,21 +56,13 @@ private slots:
void startSearchTimer();
void startSearch();
void updateCaseSensitive();
void copyPassword();
void setFocusToEntry();
private:
const QScopedPointer<Ui::SearchWidget> m_ui;
QTimer* m_searchTimer;
DatabaseWidget *m_dbWidget;
QAction *m_actionCaseSensitive;
Q_DISABLE_COPY(SearchWidget)
protected:
bool eventFilter(QObject *obj, QEvent *event);
};
#endif // SEARCHWIDGET_H