2
Copyright 2006-2008 by Robert Knight <robertknight@gmail.com>
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.
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.
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
21
#include "IncrementalSearchBar.h"
24
#include <QtGui/QCheckBox>
25
#include <QtGui/QBoxLayout>
26
#include <QtGui/QLabel>
27
#include <QtGui/QKeyEvent>
28
#include <QtCore/QTimer>
29
#include <QtGui/QToolButton>
30
#include <QtGui/QMenu>
33
#include <KColorScheme>
38
using namespace Konsole;
40
IncrementalSearchBar::IncrementalSearchBar(QWidget* parent)
48
QHBoxLayout* layout = new QHBoxLayout(this);
50
QToolButton* close = new QToolButton(this);
51
close->setObjectName( QLatin1String("close-button" ));
52
close->setToolTip( i18n("Close the search bar") );
53
close->setAutoRaise(true);
54
close->setIcon(KIcon("dialog-close"));
55
connect( close , SIGNAL(clicked()) , this , SIGNAL(closeClicked()) );
57
QLabel* findLabel = new QLabel(i18n("Find:"),this);
58
_searchEdit = new KLineEdit(this);
59
_searchEdit->setClearButtonShown(true);
60
_searchEdit->installEventFilter(this);
61
_searchEdit->setObjectName( QLatin1String("search-edit" ));
62
_searchEdit->setToolTip( i18n("Enter the text to search for here") );
64
// text box may be a minimum of 6 characters wide and a maximum of 10 characters wide
65
// (since the maxWidth metric is used here, more characters probably will fit in than 6
67
QFontMetrics metrics(_searchEdit->font());
68
int maxWidth = metrics.maxWidth();
69
_searchEdit->setMinimumWidth(maxWidth*6);
70
_searchEdit->setMaximumWidth(maxWidth*10);
72
_searchTimer = new QTimer(this);
73
_searchTimer->setInterval(250);
74
_searchTimer->setSingleShot(true);
75
connect( _searchTimer , SIGNAL(timeout()) , this , SLOT(notifySearchChanged()) );
76
connect( _searchEdit , SIGNAL(clearButtonClicked()) , this , SLOT(clearLineEdit()) );
77
connect( _searchEdit , SIGNAL(textChanged(const QString&)) , _searchTimer , SLOT(start()));
79
QToolButton* findNext = new QToolButton(this);
80
findNext->setObjectName( QLatin1String("find-next-button" ));
81
findNext->setText(i18nc("@action:button Go to the next phrase", "Next"));
82
findNext->setIcon( KIcon("go-down-search") );
83
findNext->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
84
findNext->setToolTip( i18n("Find the next match for the current search phrase") );
85
connect( findNext , SIGNAL(clicked()) , this , SIGNAL(findNextClicked()) );
87
QToolButton* findPrev = new QToolButton(this);
88
findPrev->setObjectName( QLatin1String("find-previous-button" ));
89
findPrev->setText(i18nc("@action:button Go to the previous phrase", "Previous"));
90
findPrev->setIcon( KIcon("go-up-search") );
91
findPrev->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
92
findPrev->setToolTip( i18n("Find the previous match for the current search phrase") );
93
connect( findPrev , SIGNAL(clicked()) , this , SIGNAL(findPreviousClicked()) );
95
QToolButton* optionsButton = new QToolButton(this);
96
optionsButton->setObjectName( QLatin1String("find-options-button" ));
97
optionsButton->setText(i18nc("@action:button Display options menu", "Options"));
98
optionsButton->setCheckable(false);
99
optionsButton->setPopupMode(QToolButton::InstantPopup);
100
optionsButton->setArrowType(Qt::DownArrow);
101
optionsButton->setToolButtonStyle(Qt::ToolButtonTextOnly);
102
optionsButton->setToolTip( i18n("Display the options menu") );
104
layout->addWidget(close);
105
layout->addWidget(findLabel);
106
layout->addWidget(_searchEdit);
107
layout->addWidget(findNext);
108
layout->addWidget(findPrev);
109
layout->addWidget(optionsButton);
111
// Fill the options menu
112
QMenu* optionsMenu = new QMenu(this);
113
optionsButton->setMenu(optionsMenu);
115
_caseSensitive = optionsMenu->addAction(i18n("Case sensitive"));
116
_caseSensitive->setCheckable(true);
117
_caseSensitive->setToolTip(i18n("Sets whether the search is case sensitive"));
118
connect(_caseSensitive, SIGNAL(toggled(bool)),
119
this, SIGNAL(matchCaseToggled(bool)) );
121
_regExpression = optionsMenu->addAction(i18n("Match regular expression"));
122
_regExpression->setCheckable(true);
123
connect(_regExpression, SIGNAL(toggled(bool)),
124
this, SIGNAL(matchRegExpToggled(bool)));
126
_highlightMatches = optionsMenu->addAction(i18n("Highlight all matches"));
127
_highlightMatches->setCheckable(true);
128
_highlightMatches->setToolTip(i18n("Sets whether matching text should be highlighted"));
129
_highlightMatches->setChecked(true);
130
connect(_highlightMatches, SIGNAL(toggled(bool)),
131
this, SIGNAL(highlightMatchesToggled(bool)) );
133
layout->addStretch();
135
layout->setContentsMargins(4, 4, 4, 4);
139
void IncrementalSearchBar::notifySearchChanged()
141
emit searchChanged( searchText() );
143
QString IncrementalSearchBar::searchText()
145
return _searchEdit->text();
148
bool IncrementalSearchBar::eventFilter(QObject* watched , QEvent* event)
150
if ( watched == _searchEdit )
152
if ( event->type() == QEvent::KeyPress )
154
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
156
if ( keyEvent->key() == Qt::Key_Escape )
164
return QWidget::eventFilter(watched,event);
167
void IncrementalSearchBar::setVisible(bool visible)
169
QWidget::setVisible(visible);
173
//TODO - Check if this is the correct reason value to use here
174
_searchEdit->setFocus( Qt::ActiveWindowFocusReason );
175
_searchEdit->selectAll();
179
void IncrementalSearchBar::setFoundMatch( bool match )
181
if ( !match && !_searchEdit->text().isEmpty() )
183
KStatefulBrush backgroundBrush(KColorScheme::View,KColorScheme::NegativeBackground);
185
QString styleSheet = QString("QLineEdit{ background-color:%1 }")
186
.arg(backgroundBrush.brush(_searchEdit).color().name());
188
_searchEdit->setStyleSheet( styleSheet );
192
_searchEdit->setStyleSheet( QString() );
196
void IncrementalSearchBar::clearLineEdit()
198
_searchEdit->setStyleSheet( QString() );
201
const QBitArray IncrementalSearchBar::optionsChecked()
203
QBitArray options(3, 0);
205
if (_caseSensitive->isChecked()) options.setBit(MatchCase);
206
if (_regExpression->isChecked()) options.setBit(RegExp);
207
if (_highlightMatches->isChecked()) options.setBit(HighlightMatches);
213
#include "IncrementalSearchBar.moc"