Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
feat: add hot keys for search
Browse files Browse the repository at this point in the history
  • Loading branch information
TriKriSta committed Feb 12, 2018
1 parent 18fa8a7 commit ffb51e8
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 24 deletions.
21 changes: 12 additions & 9 deletions src/widget/form/genericchatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ GenericChatForm::GenericChatForm(QWidget* parent)
connect(searchForm, &SearchForm::searchInBegin, this, &GenericChatForm::searchInBegin);
connect(searchForm, &SearchForm::searchUp, this, &GenericChatForm::onSearchUp);
connect(searchForm, &SearchForm::searchDown, this, &GenericChatForm::onSearchDown);
connect(searchForm, &SearchForm::visibleChanged, this, &GenericChatForm::onSearchTrigered);
connect(searchForm, &SearchForm::visibleChanged, this, &GenericChatForm::onSearchTriggered);

connect(chatWidget, &ChatLog::workerTimeoutFinished, this, &GenericChatForm::onContinueSearch);

Expand Down Expand Up @@ -311,14 +311,18 @@ void GenericChatForm::showEvent(QShowEvent*)
bool GenericChatForm::event(QEvent* e)
{
// If the user accidentally starts typing outside of the msgEdit, focus it automatically
if (searchForm->isHidden()) {

if (e->type() == QEvent::KeyRelease && !msgEdit->hasFocus()) {
QKeyEvent* ke = static_cast<QKeyEvent*>(e);
if ((ke->modifiers() == Qt::NoModifier || ke->modifiers() == Qt::ShiftModifier)
&& !ke->text().isEmpty())
msgEdit->setFocus();
&& !ke->text().isEmpty()) {
if (searchForm->isHidden()) {
msgEdit->setFocus();
} else {
searchForm->setFocusEditor();
}
}
}
}
return QWidget::event(e);
}

Expand Down Expand Up @@ -578,9 +582,7 @@ bool GenericChatForm::searchInText(const QString& phrase, bool searchUp)
for (int i = startLine; searchUp ? i >= 0 : i < numLines; searchUp ? --i : ++i) {
ChatLine::Ptr l = lines[i];

if (l->getColumnCount() < 2) {
continue;
}
if (l->getColumnCount() < 2) { continue; }

ChatLineContent* content = l->getContent(1);
Text* text = static_cast<Text*>(content);
Expand Down Expand Up @@ -784,10 +786,11 @@ void GenericChatForm::searchFormShow()
{
if (searchForm->isHidden()) {
searchForm->show();
searchForm->setFocusEditor();
}
}

void GenericChatForm::onSearchTrigered()
void GenericChatForm::onSearchTriggered()
{
if (searchForm->isHidden()) {
searchForm->removeSearchPhrase();
Expand Down
2 changes: 1 addition & 1 deletion src/widget/form/genericchatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected slots:
void quoteSelectedText();
void copyLink();
void searchFormShow();
void onSearchTrigered();
void onSearchTriggered();

void searchInBegin(const QString& phrase);
virtual void onSearchUp(const QString& phrase) = 0;
Expand Down
50 changes: 41 additions & 9 deletions src/widget/searchform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
#include "searchform.h"
#include "src/widget/style.h"
#include <QHBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QKeyEvent>

SearchForm::SearchForm(QWidget* parent) : QWidget(parent)
{
QHBoxLayout *layout = new QHBoxLayout();
searchLine = new QLineEdit();
searchLine = new LineEdit();

upButton = createButton("searchUpButton", "green");
downButton = createButton("searchDownButton", "green");
Expand All @@ -40,7 +40,11 @@ SearchForm::SearchForm(QWidget* parent) : QWidget(parent)

setLayout(layout);

connect(searchLine, &QLineEdit::textChanged, this, &SearchForm::changedSearchPhrase);
connect(searchLine, &LineEdit::textChanged, this, &SearchForm::changedSearchPhrase);
connect(searchLine, &LineEdit::clickEnter, this, &SearchForm::clickedUp);
connect(searchLine, &LineEdit::clickShiftEnter, this, &SearchForm::clickedDown);
connect(searchLine, &LineEdit::clickEsc, this, &SearchForm::clickedHide);

connect(upButton, &QPushButton::clicked, this, &SearchForm::clickedUp);
connect(downButton, &QPushButton::clicked, this, &SearchForm::clickedDown);
connect(hideButton, &QPushButton::clicked, this, &SearchForm::clickedHide);
Expand All @@ -56,6 +60,17 @@ QString SearchForm::getSearchPhrase() const
return searchPhrase;
}

void SearchForm::setFocusEditor()
{
searchLine->setFocus();
}

void SearchForm::showEvent(QShowEvent* event)
{
QWidget::showEvent(event);
emit visibleChanged();
}

QPushButton *SearchForm::createButton(const QString& name, const QString& state)
{
QPushButton* btn = new QPushButton();
Expand All @@ -67,12 +82,6 @@ QPushButton *SearchForm::createButton(const QString& name, const QString& state)
return btn;
}

void SearchForm::showEvent(QShowEvent* event)
{
QWidget::showEvent(event);
emit visibleChanged();
}

void SearchForm::changedSearchPhrase(const QString& text)
{
searchPhrase = text;
Expand All @@ -94,3 +103,26 @@ void SearchForm::clickedHide()
hide();
emit visibleChanged();
}

LineEdit::LineEdit(QWidget* parent) : QLineEdit(parent)
{
}

void LineEdit::keyPressEvent(QKeyEvent* event)
{
int key = event->key();

if ((key == Qt::Key_Enter || key == Qt::Key_Return)) {
if ((event->modifiers() & Qt::ShiftModifier)) {
emit clickShiftEnter();
} else {
emit clickEnter();
}
} else if (key == Qt::Key_Escape) {
emit clickEsc();
}

QLineEdit::keyPressEvent(event);
}


28 changes: 23 additions & 5 deletions src/widget/searchform.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
#define SEARCHFORM_H

#include <QWidget>
#include <QLineEdit>

class QPushButton;
class QLineEdit;
class LineEdit;

class SearchForm final : public QWidget
{
Expand All @@ -32,20 +33,21 @@ class SearchForm final : public QWidget
explicit SearchForm(QWidget* parent = nullptr);
void removeSearchPhrase();
QString getSearchPhrase() const;
void setFocusEditor();

protected:
virtual void showEvent(QShowEvent* event) final override;

private:
QPushButton* createButton(const QString& name, const QString& state);

QPushButton* upButton;
QPushButton* downButton;
QPushButton* hideButton;
QLineEdit* searchLine;
LineEdit* searchLine;

QString searchPhrase;

protected:
virtual void showEvent(QShowEvent* event);

private slots:
void changedSearchPhrase(const QString& text);
void clickedUp();
Expand All @@ -59,4 +61,20 @@ private slots:
void visibleChanged();
};

class LineEdit : public QLineEdit
{
Q_OBJECT

public:
LineEdit(QWidget* parent = nullptr);

protected:
virtual void keyPressEvent(QKeyEvent* event) final override;

signals:
void clickEnter();
void clickShiftEnter();
void clickEsc();
};

#endif // SEARCHFORM_H

0 comments on commit ffb51e8

Please sign in to comment.