add more spacing
[personal-kdebase.git] / apps / konsole / src / IncrementalSearchBar.cpp
blob50262ca0e289c347d988301576503a462ed6cba8
1 /*
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
17 02110-1301 USA.
20 // Own
21 #include "IncrementalSearchBar.h"
23 // Qt
24 #include <QtGui/QCheckBox>
25 #include <QtGui/QBoxLayout>
26 #include <QtGui/QLabel>
27 #include <QtGui/QProgressBar>
28 #include <QtGui/QKeyEvent>
29 #include <QtCore/QTimer>
30 #include <QtGui/QToolButton>
32 // KDE
33 #include <KColorScheme>
34 #include <KLineEdit>
35 #include <KLocale>
36 #include <KIcon>
38 using namespace Konsole;
40 IncrementalSearchBar::IncrementalSearchBar(Features features , QWidget* parent)
41 : QWidget(parent)
42 , _foundMatch(false)
43 , _matchCaseBox(0)
44 , _matchRegExpBox(0)
45 , _highlightBox(0)
46 , _searchEdit(0)
47 , _continueLabel(0)
49 QHBoxLayout* layout = new QHBoxLayout(this);
51 QToolButton* close = new QToolButton(this);
52 close->setObjectName("close-button");
53 close->setToolTip( i18n("Close the search bar") );
54 close->setAutoRaise(true);
55 close->setIcon(KIcon("dialog-close"));
56 connect( close , SIGNAL(clicked()) , this , SIGNAL(closeClicked()) );
58 QLabel* findLabel = new QLabel(i18n("Find:"),this);
59 _searchEdit = new KLineEdit(this);
60 _searchEdit->setClearButtonShown(true);
61 _searchEdit->installEventFilter(this);
62 _searchEdit->setObjectName("search-edit");
63 _searchEdit->setToolTip( i18n("Enter the text to search for here") );
65 // text box may be a minimum of 6 characters wide and a maximum of 10 characters wide
66 // (since the maxWidth metric is used here, more characters probably will fit in than 6
67 // and 10)
68 QFontMetrics metrics(_searchEdit->font());
69 int maxWidth = metrics.maxWidth();
70 _searchEdit->setMinimumWidth(maxWidth*6);
71 _searchEdit->setMaximumWidth(maxWidth*10);
73 _searchTimer = new QTimer(this);
74 _searchTimer->setInterval(250);
75 _searchTimer->setSingleShot(true);
76 connect( _searchTimer , SIGNAL(timeout()) , this , SLOT(notifySearchChanged()) );
77 connect( _searchEdit , SIGNAL(clearButtonClicked()) , this , SLOT(clearLineEdit()) );
78 connect( _searchEdit , SIGNAL(textChanged(const QString&)) , _searchTimer , SLOT(start()));
80 QToolButton* findNext = new QToolButton(this);
81 findNext->setObjectName("find-next-button");
82 findNext->setText(i18n("Next"));
83 findNext->setAutoRaise(true);
84 findNext->setIcon( KIcon("go-down-search") );
85 findNext->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
86 findNext->setToolTip( i18n("Find the next match for the current search phrase") );
87 connect( findNext , SIGNAL(clicked()) , this , SIGNAL(findNextClicked()) );
89 QToolButton* findPrev = new QToolButton(this);
90 findPrev->setObjectName("find-previous-button");
91 findPrev->setText(i18n("Previous"));
92 findPrev->setAutoRaise(true);
93 findPrev->setIcon( KIcon("go-up-search") );
94 findPrev->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
95 findPrev->setToolTip( i18n("Find the previous match for the current search phrase") );
96 connect( findPrev , SIGNAL(clicked()) , this , SIGNAL(findPreviousClicked()) );
98 if ( features & HighlightMatches )
100 _highlightBox = new QCheckBox( i18n("Highlight all") , this );
101 _highlightBox->setObjectName("highlight-matches-box");
102 _highlightBox->setToolTip( i18n("Sets whether matching text should be highlighted") );
103 _highlightBox->setChecked(true);
104 connect( _highlightBox , SIGNAL(toggled(bool)) , this ,
105 SIGNAL(highlightMatchesToggled(bool)) );
108 if ( features & MatchCase )
110 _matchCaseBox = new QCheckBox( i18n("Match case") , this );
111 _matchCaseBox->setObjectName("match-case-box");
112 _matchCaseBox->setToolTip( i18n("Sets whether the search is case sensitive") );
113 connect( _matchCaseBox , SIGNAL(toggled(bool)) , this , SIGNAL(matchCaseToggled(bool)) );
116 if ( features & RegExp )
118 _matchRegExpBox = new QCheckBox( i18n("Match regular expression") , this );
119 _matchRegExpBox->setObjectName("match-regexp-box");
120 _matchRegExpBox->setToolTip( i18n("Sets whether the search phrase is interpreted as normal text or"
121 " as a regular expression") );
122 connect( _matchRegExpBox , SIGNAL(toggled(bool)) , this , SIGNAL(matchRegExpToggled(bool)) );
125 QProgressBar* _progress = new QProgressBar(this);
126 _progress->setMinimum(0);
127 _progress->setMaximum(0);
128 _progress->setVisible(false);
130 QLabel* _continueLabel = new QLabel(this);
131 _continueLabel->setVisible(false);
133 layout->addWidget(close);
134 layout->addWidget(findLabel);
135 layout->addWidget(_searchEdit);
136 layout->addWidget(findNext);
137 layout->addWidget(findPrev);
139 // optional features
140 if ( features & HighlightMatches ) layout->addWidget(_highlightBox);
141 if ( features & MatchCase ) layout->addWidget(_matchCaseBox);
142 if ( features & RegExp ) layout->addWidget(_matchRegExpBox);
144 layout->addWidget(_progress);
145 layout->addWidget(_continueLabel);
146 layout->addStretch();
148 layout->setMargin(4);
150 setLayout(layout);
152 void IncrementalSearchBar::notifySearchChanged()
154 emit searchChanged( searchText() );
156 QString IncrementalSearchBar::searchText()
158 return _searchEdit->text();
160 bool IncrementalSearchBar::highlightMatches()
162 if ( !_highlightBox )
164 return true;
166 else
168 return _highlightBox->isChecked();
171 bool IncrementalSearchBar::matchCase()
173 if ( !_matchCaseBox )
175 return false;
177 else
179 return _matchCaseBox->isChecked();
182 bool IncrementalSearchBar::matchRegExp()
184 if ( !_matchRegExpBox )
186 return false;
188 else
190 return _matchRegExpBox->isChecked();
194 bool IncrementalSearchBar::eventFilter(QObject* watched , QEvent* event)
196 if ( watched == _searchEdit )
198 if ( event->type() == QEvent::KeyPress )
200 QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
202 if ( keyEvent->key() == Qt::Key_Escape )
204 emit closeClicked();
205 return true;
210 return QWidget::eventFilter(watched,event);
213 void IncrementalSearchBar::setVisible(bool visible)
215 QWidget::setVisible(visible);
217 if ( visible )
219 //TODO - Check if this is the correct reason value to use here
220 _searchEdit->setFocus( Qt::ActiveWindowFocusReason );
221 _searchEdit->selectAll();
225 void IncrementalSearchBar::setFoundMatch( bool match )
227 if ( !match && !_searchEdit->text().isEmpty() )
229 KStatefulBrush backgroundBrush(KColorScheme::View,KColorScheme::NegativeBackground);
231 QString styleSheet = QString("QLineEdit{ background-color:%1 }")
232 .arg(backgroundBrush.brush(_searchEdit).color().name());
234 _searchEdit->setStyleSheet( styleSheet );
236 else
238 _searchEdit->setStyleSheet( QString() );
242 void IncrementalSearchBar::setContinueFlag( Continue flag )
244 if ( flag == ContinueFromTop )
246 _continueLabel->setText( i18n("Search reached bottom, continued from top.") );
247 _continueLabel->show();
249 else if ( flag == ContinueFromBottom )
251 _continueLabel->setText( i18n("Search reached top, continued from bottom.") );
252 _continueLabel->show();
254 else if ( flag == ClearContinue )
256 _continueLabel->hide();
260 void IncrementalSearchBar::clearLineEdit()
262 _searchEdit->setStyleSheet( QString() );
265 #include "IncrementalSearchBar.moc"