| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/reusable/searchtextwidget.h" |
| 4 | |
| 5 | #include "miscellaneous/application.h" |
| 6 | #include "miscellaneous/iconfactory.h" |
| 7 | |
| 8 | #include <QKeyEvent> |
| 9 | |
| 10 | SearchTextWidget::SearchTextWidget(QWidget* parent) : QWidget(parent) { |
| 11 | m_ui.setupUi(this); |
| 12 | setFocusProxy(m_ui.m_txtSearch); |
| 13 | setFixedHeight(28); |
| 14 | |
| 15 | m_ui.m_btnSearchBackward->setIcon(qApp->icons()->fromTheme(QSL("go-previous"))); |
| 16 | m_ui.m_btnSearchForward->setIcon(qApp->icons()->fromTheme(QSL("go-next"))); |
| 17 | |
| 18 | connect(m_ui.m_txtSearch, &BaseLineEdit::textChanged, this, &SearchTextWidget::onTextChanged); |
| 19 | connect(m_ui.m_txtSearch, &BaseLineEdit::submitted, this, [this]() { |
| 20 | emit searchForText(m_ui.m_txtSearch->text(), false); |
| 21 | }); |
| 22 | connect(m_ui.m_btnSearchForward, &QToolButton::clicked, this, [this]() { |
| 23 | emit searchForText(m_ui.m_txtSearch->text(), false); |
| 24 | }); |
| 25 | connect(m_ui.m_btnSearchBackward, &QToolButton::clicked, this, [this]() { |
| 26 | emit searchForText(m_ui.m_txtSearch->text(), true); |
| 27 | }); |
| 28 | } |
| 29 | |
| 30 | void SearchTextWidget::clear() { |
| 31 | m_ui.m_txtSearch->clear(); |
| 32 | } |
| 33 | |
| 34 | void SearchTextWidget::cancelSearch() { |
| 35 | emit searchCancelled(); |
| 36 | |
| 37 | hide(); |
| 38 | } |
| 39 | |
| 40 | void SearchTextWidget::onTextChanged(const QString& text) { |
| 41 | m_ui.m_btnSearchBackward->setDisabled(text.isEmpty()); |
| 42 | m_ui.m_btnSearchForward->setDisabled(text.isEmpty()); |
| 43 | |
| 44 | if (!text.isEmpty()) { |
| 45 | emit searchForText(text, false); |
| 46 | } |
| 47 | else { |
| 48 | emit searchCancelled(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void SearchTextWidget::keyPressEvent(QKeyEvent* event) { |
| 53 | if (event->key() == Qt::Key::Key_Escape) { |
| 54 | cancelSearch(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void SearchTextWidget::focusInEvent(QFocusEvent* event) { |
| 59 | QWidget::focusInEvent(event); |
| 60 | } |