1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

feat: add hot keys for search

This commit is contained in:
TriKriSta 2018-02-12 19:24:22 +02:00
parent 18fa8a745b
commit ffb51e8a0e
4 changed files with 77 additions and 24 deletions

View File

@ -227,7 +227,7 @@ GenericChatForm::GenericChatForm(QWidget* parent)
connect(searchForm, &SearchForm::searchInBegin, this, &GenericChatForm::searchInBegin); connect(searchForm, &SearchForm::searchInBegin, this, &GenericChatForm::searchInBegin);
connect(searchForm, &SearchForm::searchUp, this, &GenericChatForm::onSearchUp); connect(searchForm, &SearchForm::searchUp, this, &GenericChatForm::onSearchUp);
connect(searchForm, &SearchForm::searchDown, this, &GenericChatForm::onSearchDown); 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); connect(chatWidget, &ChatLog::workerTimeoutFinished, this, &GenericChatForm::onContinueSearch);
@ -311,14 +311,18 @@ void GenericChatForm::showEvent(QShowEvent*)
bool GenericChatForm::event(QEvent* e) bool GenericChatForm::event(QEvent* e)
{ {
// If the user accidentally starts typing outside of the msgEdit, focus it automatically // If the user accidentally starts typing outside of the msgEdit, focus it automatically
if (searchForm->isHidden()) {
if (e->type() == QEvent::KeyRelease && !msgEdit->hasFocus()) { if (e->type() == QEvent::KeyRelease && !msgEdit->hasFocus()) {
QKeyEvent* ke = static_cast<QKeyEvent*>(e); QKeyEvent* ke = static_cast<QKeyEvent*>(e);
if ((ke->modifiers() == Qt::NoModifier || ke->modifiers() == Qt::ShiftModifier) if ((ke->modifiers() == Qt::NoModifier || ke->modifiers() == Qt::ShiftModifier)
&& !ke->text().isEmpty()) && !ke->text().isEmpty()) {
msgEdit->setFocus(); if (searchForm->isHidden()) {
msgEdit->setFocus();
} else {
searchForm->setFocusEditor();
}
}
} }
}
return QWidget::event(e); return QWidget::event(e);
} }
@ -578,9 +582,7 @@ bool GenericChatForm::searchInText(const QString& phrase, bool searchUp)
for (int i = startLine; searchUp ? i >= 0 : i < numLines; searchUp ? --i : ++i) { for (int i = startLine; searchUp ? i >= 0 : i < numLines; searchUp ? --i : ++i) {
ChatLine::Ptr l = lines[i]; ChatLine::Ptr l = lines[i];
if (l->getColumnCount() < 2) { if (l->getColumnCount() < 2) { continue; }
continue;
}
ChatLineContent* content = l->getContent(1); ChatLineContent* content = l->getContent(1);
Text* text = static_cast<Text*>(content); Text* text = static_cast<Text*>(content);
@ -784,10 +786,11 @@ void GenericChatForm::searchFormShow()
{ {
if (searchForm->isHidden()) { if (searchForm->isHidden()) {
searchForm->show(); searchForm->show();
searchForm->setFocusEditor();
} }
} }
void GenericChatForm::onSearchTrigered() void GenericChatForm::onSearchTriggered()
{ {
if (searchForm->isHidden()) { if (searchForm->isHidden()) {
searchForm->removeSearchPhrase(); searchForm->removeSearchPhrase();

View File

@ -104,7 +104,7 @@ protected slots:
void quoteSelectedText(); void quoteSelectedText();
void copyLink(); void copyLink();
void searchFormShow(); void searchFormShow();
void onSearchTrigered(); void onSearchTriggered();
void searchInBegin(const QString& phrase); void searchInBegin(const QString& phrase);
virtual void onSearchUp(const QString& phrase) = 0; virtual void onSearchUp(const QString& phrase) = 0;

View File

@ -20,13 +20,13 @@
#include "searchform.h" #include "searchform.h"
#include "src/widget/style.h" #include "src/widget/style.h"
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QLineEdit>
#include <QPushButton> #include <QPushButton>
#include <QKeyEvent>
SearchForm::SearchForm(QWidget* parent) : QWidget(parent) SearchForm::SearchForm(QWidget* parent) : QWidget(parent)
{ {
QHBoxLayout *layout = new QHBoxLayout(); QHBoxLayout *layout = new QHBoxLayout();
searchLine = new QLineEdit(); searchLine = new LineEdit();
upButton = createButton("searchUpButton", "green"); upButton = createButton("searchUpButton", "green");
downButton = createButton("searchDownButton", "green"); downButton = createButton("searchDownButton", "green");
@ -40,7 +40,11 @@ SearchForm::SearchForm(QWidget* parent) : QWidget(parent)
setLayout(layout); 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(upButton, &QPushButton::clicked, this, &SearchForm::clickedUp);
connect(downButton, &QPushButton::clicked, this, &SearchForm::clickedDown); connect(downButton, &QPushButton::clicked, this, &SearchForm::clickedDown);
connect(hideButton, &QPushButton::clicked, this, &SearchForm::clickedHide); connect(hideButton, &QPushButton::clicked, this, &SearchForm::clickedHide);
@ -56,6 +60,17 @@ QString SearchForm::getSearchPhrase() const
return searchPhrase; 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 *SearchForm::createButton(const QString& name, const QString& state)
{ {
QPushButton* btn = new QPushButton(); QPushButton* btn = new QPushButton();
@ -67,12 +82,6 @@ QPushButton *SearchForm::createButton(const QString& name, const QString& state)
return btn; return btn;
} }
void SearchForm::showEvent(QShowEvent* event)
{
QWidget::showEvent(event);
emit visibleChanged();
}
void SearchForm::changedSearchPhrase(const QString& text) void SearchForm::changedSearchPhrase(const QString& text)
{ {
searchPhrase = text; searchPhrase = text;
@ -94,3 +103,26 @@ void SearchForm::clickedHide()
hide(); hide();
emit visibleChanged(); 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);
}

View File

@ -21,9 +21,10 @@
#define SEARCHFORM_H #define SEARCHFORM_H
#include <QWidget> #include <QWidget>
#include <QLineEdit>
class QPushButton; class QPushButton;
class QLineEdit; class LineEdit;
class SearchForm final : public QWidget class SearchForm final : public QWidget
{ {
@ -32,6 +33,10 @@ public:
explicit SearchForm(QWidget* parent = nullptr); explicit SearchForm(QWidget* parent = nullptr);
void removeSearchPhrase(); void removeSearchPhrase();
QString getSearchPhrase() const; QString getSearchPhrase() const;
void setFocusEditor();
protected:
virtual void showEvent(QShowEvent* event) final override;
private: private:
QPushButton* createButton(const QString& name, const QString& state); QPushButton* createButton(const QString& name, const QString& state);
@ -39,13 +44,10 @@ private:
QPushButton* upButton; QPushButton* upButton;
QPushButton* downButton; QPushButton* downButton;
QPushButton* hideButton; QPushButton* hideButton;
QLineEdit* searchLine; LineEdit* searchLine;
QString searchPhrase; QString searchPhrase;
protected:
virtual void showEvent(QShowEvent* event);
private slots: private slots:
void changedSearchPhrase(const QString& text); void changedSearchPhrase(const QString& text);
void clickedUp(); void clickedUp();
@ -59,4 +61,20 @@ signals:
void visibleChanged(); 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 #endif // SEARCHFORM_H