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> |
21 | #include <QRegExp> |
22 | #include <QDebug> |
23 | |
24 | #include "SearchBar.h" |
25 | |
26 | SearchBar::SearchBar(QWidget *parent) : QWidget(parent) |
27 | { |
28 | widget.setupUi(this); |
29 | setAutoFillBackground(true); // make it always opaque, especially inside translucent windows |
30 | connect(widget.closeButton, SIGNAL(clicked()), this, SLOT(hide())); |
31 | connect(widget.searchTextEdit, SIGNAL(textChanged(QString)), this, SIGNAL(searchCriteriaChanged())); |
32 | connect(widget.findPreviousButton, SIGNAL(clicked()), this, SIGNAL(findPrevious())); |
33 | connect(widget.findNextButton, SIGNAL(clicked()), this, SIGNAL(findNext())); |
34 | |
35 | connect(this, SIGNAL(searchCriteriaChanged()), this, SLOT(clearBackgroundColor())); |
36 | |
37 | QMenu * = new QMenu(widget.optionsButton); |
38 | widget.optionsButton->setMenu(optionsMenu); |
39 | |
40 | m_matchCaseMenuEntry = optionsMenu->addAction(tr("Match case" )); |
41 | m_matchCaseMenuEntry->setCheckable(true); |
42 | m_matchCaseMenuEntry->setChecked(true); |
43 | connect(m_matchCaseMenuEntry, SIGNAL(toggled(bool)), this, SIGNAL(searchCriteriaChanged())); |
44 | |
45 | |
46 | m_useRegularExpressionMenuEntry = optionsMenu->addAction(tr("Regular expression" )); |
47 | m_useRegularExpressionMenuEntry->setCheckable(true); |
48 | connect(m_useRegularExpressionMenuEntry, SIGNAL(toggled(bool)), this, SIGNAL(searchCriteriaChanged())); |
49 | |
50 | m_highlightMatchesMenuEntry = optionsMenu->addAction(tr("Highlight all matches" )); |
51 | m_highlightMatchesMenuEntry->setCheckable(true); |
52 | m_highlightMatchesMenuEntry->setChecked(true); |
53 | connect(m_highlightMatchesMenuEntry, SIGNAL(toggled(bool)), this, SIGNAL(highlightMatchesChanged(bool))); |
54 | } |
55 | |
56 | SearchBar::~SearchBar() { |
57 | } |
58 | |
59 | QString SearchBar::searchText() |
60 | { |
61 | return widget.searchTextEdit->text(); |
62 | } |
63 | |
64 | |
65 | bool SearchBar::useRegularExpression() |
66 | { |
67 | return m_useRegularExpressionMenuEntry->isChecked(); |
68 | } |
69 | |
70 | bool SearchBar::matchCase() |
71 | { |
72 | return m_matchCaseMenuEntry->isChecked(); |
73 | } |
74 | |
75 | bool SearchBar::highlightAllMatches() |
76 | { |
77 | return m_highlightMatchesMenuEntry->isChecked(); |
78 | } |
79 | |
80 | void SearchBar::show() |
81 | { |
82 | QWidget::show(); |
83 | widget.searchTextEdit->setFocus(); |
84 | } |
85 | |
86 | void SearchBar::noMatchFound() |
87 | { |
88 | QPalette palette; |
89 | palette.setColor(widget.searchTextEdit->backgroundRole(), QColor(255, 128, 128)); |
90 | widget.searchTextEdit->setPalette(palette); |
91 | } |
92 | |
93 | |
94 | void SearchBar::keyReleaseEvent(QKeyEvent* keyEvent) |
95 | { |
96 | if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) |
97 | { |
98 | if (keyEvent->modifiers() == Qt::ShiftModifier) |
99 | { |
100 | findPrevious(); |
101 | } |
102 | else |
103 | { |
104 | findNext(); |
105 | } |
106 | } |
107 | else if (keyEvent->key() == Qt::Key_Escape) |
108 | { |
109 | hide(); |
110 | } |
111 | } |
112 | |
113 | void SearchBar::clearBackgroundColor() |
114 | { |
115 | QPalette p; |
116 | p.setColor(QPalette::Base, Qt::white); |
117 | widget.searchTextEdit->setPalette(p); |
118 | |
119 | } |
120 | |