Zack Rusin | f04cf8a | 2011-04-12 23:21:52 -0400 | [diff] [blame] | 1 | #include "searchwidget.h" |
| 2 | |
| 3 | #include <QDebug> |
| 4 | #include <QKeyEvent> |
Zack Rusin | 2f15f4f | 2011-04-13 17:26:59 -0400 | [diff] [blame] | 5 | #include <QKeySequence> |
Zack Rusin | f04cf8a | 2011-04-12 23:21:52 -0400 | [diff] [blame] | 6 | |
| 7 | SearchWidget::SearchWidget(QWidget *parent) |
| 8 | : QWidget(parent) |
| 9 | { |
| 10 | m_ui.setupUi(this); |
| 11 | |
| 12 | m_ui.notFoundLabel->hide(); |
| 13 | m_origPalette = m_ui.lineEdit->palette(); |
| 14 | |
| 15 | connect(m_ui.nextButton, SIGNAL(clicked()), |
| 16 | SLOT(slotSearchNext())); |
| 17 | connect(m_ui.prevButton, SIGNAL(clicked()), |
| 18 | SLOT(slotSearchPrev())); |
| 19 | connect(m_ui.closeButton, SIGNAL(clicked()), |
| 20 | SLOT(slotCancel())); |
| 21 | connect(m_ui.lineEdit, SIGNAL(returnPressed()), |
| 22 | SLOT(slotSearchNext())); |
| 23 | |
Zack Rusin | 2f15f4f | 2011-04-13 17:26:59 -0400 | [diff] [blame] | 24 | m_ui.nextButton->setShortcut( |
| 25 | QKeySequence::FindNext); |
| 26 | m_ui.prevButton->setShortcut( |
| 27 | QKeySequence::FindPrevious); |
| 28 | |
Zack Rusin | f04cf8a | 2011-04-12 23:21:52 -0400 | [diff] [blame] | 29 | installEventFilter(this); |
| 30 | } |
| 31 | |
| 32 | void SearchWidget::slotSearchNext() |
| 33 | { |
| 34 | QString txt = m_ui.lineEdit->text(); |
| 35 | if (!txt.isEmpty()) |
Ruslan Kabatsayev | 75d6905 | 2019-02-20 13:41:18 +0300 | [diff] [blame] | 36 | emit searchNext(txt, caseSensitivity(), regexEnabled()); |
Zack Rusin | f04cf8a | 2011-04-12 23:21:52 -0400 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void SearchWidget::slotSearchPrev() |
| 40 | { |
| 41 | QString txt = m_ui.lineEdit->text(); |
| 42 | if (!txt.isEmpty()) |
Ruslan Kabatsayev | 75d6905 | 2019-02-20 13:41:18 +0300 | [diff] [blame] | 43 | emit searchPrev(txt, caseSensitivity(), regexEnabled()); |
Zack Rusin | f04cf8a | 2011-04-12 23:21:52 -0400 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void SearchWidget::slotCancel() |
| 47 | { |
| 48 | hide(); |
| 49 | } |
| 50 | |
| 51 | void SearchWidget::showEvent(QShowEvent *event) |
| 52 | { |
Zack Rusin | f04cf8a | 2011-04-12 23:21:52 -0400 | [diff] [blame] | 53 | return QWidget::showEvent(event); |
| 54 | } |
| 55 | |
| 56 | Qt::CaseSensitivity SearchWidget::caseSensitivity() const |
| 57 | { |
| 58 | if (m_ui.caseSensitiveBox->isChecked()) |
| 59 | return Qt::CaseSensitive; |
| 60 | else |
| 61 | return Qt::CaseInsensitive; |
| 62 | } |
| 63 | |
Ruslan Kabatsayev | 75d6905 | 2019-02-20 13:41:18 +0300 | [diff] [blame] | 64 | bool SearchWidget::regexEnabled() const |
| 65 | { |
| 66 | return m_ui.regexBox->isChecked(); |
| 67 | } |
| 68 | |
Zack Rusin | f04cf8a | 2011-04-12 23:21:52 -0400 | [diff] [blame] | 69 | bool SearchWidget::eventFilter(QObject *object, QEvent* event) |
| 70 | { |
| 71 | if (event->type() == QEvent::KeyPress) { |
| 72 | if ((static_cast<QKeyEvent*>(event))->key() == Qt::Key_Escape) { |
| 73 | hide(); |
| 74 | } |
| 75 | } |
| 76 | return QWidget::eventFilter(object, event); |
| 77 | } |
| 78 | |
| 79 | void SearchWidget::setFound(bool found) |
| 80 | { |
| 81 | QPalette pal = m_origPalette; |
| 82 | pal.setColor(QPalette::Active, QPalette::Base, |
| 83 | found ? Qt::white : QColor(255, 102, 102)); |
| 84 | m_ui.lineEdit->setPalette(pal); |
| 85 | m_ui.notFoundLabel->setVisible(!found); |
| 86 | } |
| 87 | |
Zack Rusin | f54c4fc | 2011-04-12 23:39:13 -0400 | [diff] [blame] | 88 | void SearchWidget::show() |
| 89 | { |
| 90 | QWidget::show(); |
| 91 | m_ui.lineEdit->selectAll(); |
| 92 | m_ui.lineEdit->setFocus(Qt::ShortcutFocusReason); |
| 93 | m_ui.lineEdit->setPalette(m_origPalette); |
| 94 | } |
| 95 | |
Zack Rusin | f04cf8a | 2011-04-12 23:21:52 -0400 | [diff] [blame] | 96 | #include "searchwidget.moc" |