[64efc22] | 1 | /* |
---|
| 2 | Copyright 2013 Christian Surlykke |
---|
| 3 | |
---|
| 4 | This program is free software; you can redistribute it and/or modify |
---|
| 5 | it under the terms of the GNU General Public License as published by |
---|
| 6 | the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | (at your option) any later version. |
---|
| 8 | |
---|
| 9 | This program is distributed in the hope that it will be useful, |
---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | GNU General Public License for more details. |
---|
| 13 | |
---|
| 14 | You should have received a copy of the GNU General Public License |
---|
| 15 | along with this program; if not, write to the Free Software |
---|
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
---|
| 17 | 02110-1301 USA. |
---|
| 18 | */ |
---|
| 19 | #include <QMenu> |
---|
| 20 | #include <QAction> |
---|
[fedf2a2] | 21 | #if QT_VERSION > 0x060000 |
---|
| 22 | #include <QRegularExpression> |
---|
| 23 | #else |
---|
[64efc22] | 24 | #include <QRegExp> |
---|
[fedf2a2] | 25 | #endif |
---|
[64efc22] | 26 | #include <QDebug> |
---|
| 27 | |
---|
| 28 | #include "SearchBar.h" |
---|
| 29 | |
---|
| 30 | SearchBar::SearchBar(QWidget *parent) : QWidget(parent) |
---|
| 31 | { |
---|
| 32 | widget.setupUi(this); |
---|
| 33 | setAutoFillBackground(true); // make it always opaque, especially inside translucent windows |
---|
| 34 | connect(widget.closeButton, &QAbstractButton::clicked, this, &SearchBar::hide); |
---|
| 35 | connect(widget.searchTextEdit, SIGNAL(textChanged(QString)), this, SIGNAL(searchCriteriaChanged())); |
---|
| 36 | connect(widget.findPreviousButton, SIGNAL(clicked()), this, SIGNAL(findPrevious())); |
---|
| 37 | connect(widget.findNextButton, SIGNAL(clicked()), this, SIGNAL(findNext())); |
---|
| 38 | |
---|
| 39 | connect(this, SIGNAL(searchCriteriaChanged()), this, SLOT(clearBackgroundColor())); |
---|
| 40 | |
---|
| 41 | QMenu *optionsMenu = new QMenu(widget.optionsButton); |
---|
| 42 | widget.optionsButton->setMenu(optionsMenu); |
---|
| 43 | |
---|
| 44 | m_matchCaseMenuEntry = optionsMenu->addAction(tr("Match case")); |
---|
| 45 | m_matchCaseMenuEntry->setCheckable(true); |
---|
| 46 | m_matchCaseMenuEntry->setChecked(true); |
---|
| 47 | connect(m_matchCaseMenuEntry, SIGNAL(toggled(bool)), this, SIGNAL(searchCriteriaChanged())); |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | m_useRegularExpressionMenuEntry = optionsMenu->addAction(tr("Regular expression")); |
---|
| 51 | m_useRegularExpressionMenuEntry->setCheckable(true); |
---|
| 52 | connect(m_useRegularExpressionMenuEntry, SIGNAL(toggled(bool)), this, SIGNAL(searchCriteriaChanged())); |
---|
| 53 | |
---|
| 54 | m_highlightMatchesMenuEntry = optionsMenu->addAction(tr("Highlight all matches")); |
---|
| 55 | m_highlightMatchesMenuEntry->setCheckable(true); |
---|
| 56 | m_highlightMatchesMenuEntry->setChecked(true); |
---|
| 57 | connect(m_highlightMatchesMenuEntry, SIGNAL(toggled(bool)), this, SIGNAL(highlightMatchesChanged(bool))); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | SearchBar::~SearchBar() { |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | QString SearchBar::searchText() |
---|
| 64 | { |
---|
| 65 | return widget.searchTextEdit->text(); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | bool SearchBar::useRegularExpression() |
---|
| 70 | { |
---|
| 71 | return m_useRegularExpressionMenuEntry->isChecked(); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | bool SearchBar::matchCase() |
---|
| 75 | { |
---|
| 76 | return m_matchCaseMenuEntry->isChecked(); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | bool SearchBar::highlightAllMatches() |
---|
| 80 | { |
---|
| 81 | return m_highlightMatchesMenuEntry->isChecked(); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | void SearchBar::show() |
---|
| 85 | { |
---|
| 86 | QWidget::show(); |
---|
| 87 | widget.searchTextEdit->setFocus(); |
---|
| 88 | widget.searchTextEdit->selectAll(); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | void SearchBar::hide() |
---|
| 92 | { |
---|
| 93 | QWidget::hide(); |
---|
| 94 | if (QWidget *p = parentWidget()) |
---|
| 95 | { |
---|
| 96 | p->setFocus(Qt::OtherFocusReason); // give the focus to the parent widget on hiding |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | void SearchBar::noMatchFound() |
---|
| 101 | { |
---|
| 102 | QPalette palette; |
---|
| 103 | palette.setColor(widget.searchTextEdit->backgroundRole(), QColor(255, 128, 128)); |
---|
| 104 | widget.searchTextEdit->setPalette(palette); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | void SearchBar::keyReleaseEvent(QKeyEvent* keyEvent) |
---|
| 109 | { |
---|
| 110 | if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) |
---|
| 111 | { |
---|
| 112 | if (keyEvent->modifiers() == Qt::ShiftModifier) |
---|
| 113 | { |
---|
| 114 | Q_EMIT findPrevious(); |
---|
| 115 | } |
---|
| 116 | else |
---|
| 117 | { |
---|
| 118 | Q_EMIT findNext(); |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | else if (keyEvent->key() == Qt::Key_Escape) |
---|
| 122 | { |
---|
| 123 | hide(); |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | void SearchBar::clearBackgroundColor() |
---|
| 128 | { |
---|
| 129 | widget.searchTextEdit->setPalette(QWidget::window()->palette()); |
---|
| 130 | |
---|
| 131 | } |
---|