• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Konsole

  • sources
  • kde-4.12
  • applications
  • konsole
  • src
IncrementalSearchBar.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2008 by Robert Knight <robertknight@gmail.com>
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 
20 // Own
21 #include "IncrementalSearchBar.h"
22 
23 // Qt
24 #include <QHBoxLayout>
25 #include <QLabel>
26 #include <QtGui/QKeyEvent>
27 #include <QtCore/QTimer>
28 #include <QToolButton>
29 #include <QMenu>
30 
31 // KDE
32 #include <KColorScheme>
33 #include <KLineEdit>
34 #include <KLocalizedString>
35 #include <KIcon>
36 
37 using namespace Konsole;
38 
39 IncrementalSearchBar::IncrementalSearchBar(QWidget* aParent)
40  : QWidget(aParent)
41  , _searchEdit(0)
42  , _caseSensitive(0)
43  , _regExpression(0)
44  , _highlightMatches(0)
45  , _reverseSearch(0)
46  , _findNextButton(0)
47  , _findPreviousButton(0)
48  , _searchFromButton(0)
49 {
50  QHBoxLayout* barLayout = new QHBoxLayout(this);
51 
52  QToolButton* closeButton = new QToolButton(this);
53  closeButton->setObjectName(QLatin1String("close-button"));
54  closeButton->setToolTip(i18nc("@info:tooltip", "Close the search bar"));
55  closeButton->setAutoRaise(true);
56  closeButton->setIcon(KIcon("dialog-close"));
57  connect(closeButton , SIGNAL(clicked()) , this , SIGNAL(closeClicked()));
58 
59  QLabel* findLabel = new QLabel(i18nc("@label:textbox", "Find:"), this);
60  _searchEdit = new KLineEdit(this);
61  _searchEdit->setClearButtonShown(true);
62  _searchEdit->installEventFilter(this);
63  _searchEdit->setObjectName(QLatin1String("search-edit"));
64  _searchEdit->setToolTip(i18nc("@info:tooltip", "Enter the text to search for here"));
65 
66  // text box may be a minimum of 6 characters wide and a maximum of 10 characters wide
67  // (since the maxWidth metric is used here, more characters probably will fit in than 6
68  // and 10)
69  QFontMetrics metrics(_searchEdit->font());
70  int maxWidth = metrics.maxWidth();
71  _searchEdit->setMinimumWidth(maxWidth * 6);
72  _searchEdit->setMaximumWidth(maxWidth * 10);
73 
74  _searchTimer = new QTimer(this);
75  _searchTimer->setInterval(250);
76  _searchTimer->setSingleShot(true);
77  connect(_searchTimer , SIGNAL(timeout()) , this , SLOT(notifySearchChanged()));
78  connect(_searchEdit , SIGNAL(clearButtonClicked()) , this , SLOT(clearLineEdit()));
79  connect(_searchEdit , SIGNAL(textChanged(QString)) , _searchTimer , SLOT(start()));
80 
81  _findNextButton = new QToolButton(this);
82  _findNextButton->setObjectName(QLatin1String("find-next-button"));
83  _findNextButton->setText(i18nc("@action:button Go to the next phrase", "Next"));
84  _findNextButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
85  _findNextButton->setToolTip(i18nc("@info:tooltip", "Find the next match for the current search phrase"));
86  connect(_findNextButton , SIGNAL(clicked()) , this , SIGNAL(findNextClicked()));
87 
88  _findPreviousButton = new QToolButton(this);
89  _findPreviousButton->setObjectName(QLatin1String("find-previous-button"));
90  _findPreviousButton->setText(i18nc("@action:button Go to the previous phrase", "Previous"));
91  _findPreviousButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
92  _findPreviousButton->setToolTip(i18nc("@info:tooltip", "Find the previous match for the current search phrase"));
93  connect(_findPreviousButton , SIGNAL(clicked()) , this , SIGNAL(findPreviousClicked()));
94 
95 
96  _searchFromButton = new QToolButton(this);
97  _searchFromButton->setObjectName(QLatin1String("search-from-button"));
98  connect(_searchFromButton , SIGNAL(clicked()) , this , SIGNAL(searchFromClicked()));
99 
100  QToolButton* optionsButton = new QToolButton(this);
101  optionsButton->setObjectName(QLatin1String("find-options-button"));
102  optionsButton->setText(i18nc("@action:button Display options menu", "Options"));
103  optionsButton->setCheckable(false);
104  optionsButton->setPopupMode(QToolButton::InstantPopup);
105  optionsButton->setArrowType(Qt::DownArrow);
106  optionsButton->setToolButtonStyle(Qt::ToolButtonTextOnly);
107  optionsButton->setToolTip(i18nc("@info:tooltip", "Display the options menu"));
108 
109  barLayout->addWidget(closeButton);
110  barLayout->addWidget(findLabel);
111  barLayout->addWidget(_searchEdit);
112  barLayout->addWidget(_findNextButton);
113  barLayout->addWidget(_findPreviousButton);
114  barLayout->addWidget(_searchFromButton);
115  barLayout->addWidget(optionsButton);
116 
117  // Fill the options menu
118  QMenu* optionsMenu = new QMenu(this);
119  optionsButton->setMenu(optionsMenu);
120 
121  _caseSensitive = optionsMenu->addAction(i18nc("@item:inmenu", "Case sensitive"));
122  _caseSensitive->setCheckable(true);
123  _caseSensitive->setToolTip(i18nc("@info:tooltip", "Sets whether the search is case sensitive"));
124  connect(_caseSensitive, SIGNAL(toggled(bool)),
125  this, SIGNAL(matchCaseToggled(bool)));
126 
127  _regExpression = optionsMenu->addAction(i18nc("@item:inmenu", "Match regular expression"));
128  _regExpression->setCheckable(true);
129  connect(_regExpression, SIGNAL(toggled(bool)),
130  this, SIGNAL(matchRegExpToggled(bool)));
131 
132  _highlightMatches = optionsMenu->addAction(i18nc("@item:inmenu", "Highlight all matches"));
133  _highlightMatches->setCheckable(true);
134  _highlightMatches->setToolTip(i18nc("@info:tooltip", "Sets whether matching text should be highlighted"));
135  _highlightMatches->setChecked(true);
136  connect(_highlightMatches, SIGNAL(toggled(bool)),
137  this, SIGNAL(highlightMatchesToggled(bool)));
138 
139  _reverseSearch = optionsMenu->addAction(i18n("Search backwards"));
140  _reverseSearch->setCheckable(true);
141  _reverseSearch->setToolTip(i18n("Sets whether search should start from the bottom"));
142  _reverseSearch->setChecked(true);
143  connect(_reverseSearch, SIGNAL(toggled(bool)),
144  this, SLOT(updateButtonsAccordingToReverseSearchSetting()));
145  updateButtonsAccordingToReverseSearchSetting();
146 
147  barLayout->addStretch();
148 
149  barLayout->setContentsMargins(4, 4, 4, 4);
150 
151  setLayout(barLayout);
152 }
153 
154 void IncrementalSearchBar::notifySearchChanged()
155 {
156  emit searchChanged(searchText());
157 }
158 
159 void IncrementalSearchBar::updateButtonsAccordingToReverseSearchSetting()
160 {
161  Q_ASSERT(_reverseSearch);
162  if (_reverseSearch->isChecked()) {
163  _searchFromButton->setText(i18nc("@action:button Search from bottom", "From bottom"));
164  _searchFromButton->setToolTip(i18n("Search for the current search phrase from the bottom"));
165  _findNextButton->setIcon(KIcon("go-up-search"));
166  _findPreviousButton->setIcon(KIcon("go-down-search"));
167  } else {
168  _searchFromButton->setText(i18nc("@action:button Search from top", "From top"));
169  _searchFromButton->setToolTip(i18n("Search for the current search phrase from the top"));
170  _findNextButton->setIcon(KIcon("go-down-search"));
171  _findPreviousButton->setIcon(KIcon("go-up-search"));
172  }
173 }
174 
175 QString IncrementalSearchBar::searchText()
176 {
177  return _searchEdit->text();
178 }
179 
180 void IncrementalSearchBar::setSearchText(const QString& text)
181 {
182  if (text != searchText())
183  _searchEdit->setText(text);
184 }
185 
186 bool IncrementalSearchBar::eventFilter(QObject* watched , QEvent* aEvent)
187 {
188  if (watched == _searchEdit) {
189  if (aEvent->type() == QEvent::KeyPress) {
190  QKeyEvent* keyEvent = static_cast<QKeyEvent*>(aEvent);
191 
192  if (keyEvent->key() == Qt::Key_Escape) {
193  emit closeClicked();
194  return true;
195  }
196 
197  if (keyEvent->key() == Qt::Key_Return && !keyEvent->modifiers()) {
198  _findNextButton->click();
199  return true;
200  }
201 
202  if ((keyEvent->key() == Qt::Key_Return) && (keyEvent->modifiers() == Qt::ShiftModifier)) {
203  _findPreviousButton->click();
204  return true;
205  }
206 
207  if ((keyEvent->key() == Qt::Key_Return) && (keyEvent->modifiers() == Qt::ControlModifier)) {
208  _searchFromButton->click();
209  return true;
210  }
211  }
212  }
213 
214  return QWidget::eventFilter(watched, aEvent);
215 }
216 
217 void IncrementalSearchBar::keyPressEvent(QKeyEvent* event)
218 {
219  static QSet<int> movementKeysToPassAlong = QSet<int>()
220  << Qt::Key_PageUp
221  << Qt::Key_PageDown
222  << Qt::Key_Up
223  << Qt::Key_Down;
224 
225  if (movementKeysToPassAlong.contains(event->key()) &&
226  (event->modifiers() == Qt::ShiftModifier)) {
227  emit unhandledMovementKeyPressed(event);
228  } else {
229  QWidget::keyPressEvent(event);
230  }
231 }
232 
233 void IncrementalSearchBar::setVisible(bool visible)
234 {
235  QWidget::setVisible(visible);
236 
237  if (visible) {
238  focusLineEdit();
239  }
240 }
241 
242 void IncrementalSearchBar::setFoundMatch(bool match)
243 {
244  if (!match && !_searchEdit->text().isEmpty()) {
245  KStatefulBrush backgroundBrush(KColorScheme::View, KColorScheme::NegativeBackground);
246 
247  QString matchStyleSheet = QString("QLineEdit{ background-color:%1 }")
248  .arg(backgroundBrush.brush(_searchEdit).color().name());
249 
250  _searchEdit->setStyleSheet(matchStyleSheet);
251  } else if (_searchEdit->text().isEmpty()) {
252  clearLineEdit();
253  } else {
254  KStatefulBrush backgroundBrush(KColorScheme::View, KColorScheme::PositiveBackground);
255 
256  QString matchStyleSheet = QString("QLineEdit{ background-color:%1 }")
257  .arg(backgroundBrush.brush(_searchEdit).color().name());
258 
259  _searchEdit->setStyleSheet(matchStyleSheet);
260  }
261 }
262 
263 void IncrementalSearchBar::clearLineEdit()
264 {
265  _searchEdit->setStyleSheet(QString());
266 }
267 
268 void IncrementalSearchBar::focusLineEdit()
269 {
270  _searchEdit->setFocus(Qt::ActiveWindowFocusReason);
271  _searchEdit->selectAll();
272 }
273 
274 const QBitArray IncrementalSearchBar::optionsChecked()
275 {
276  QBitArray options(4, 0);
277 
278  if (_caseSensitive->isChecked()) options.setBit(MatchCase);
279  if (_regExpression->isChecked()) options.setBit(RegExp);
280  if (_highlightMatches->isChecked()) options.setBit(HighlightMatches);
281  if (_reverseSearch->isChecked()) options.setBit(ReverseSearch);
282 
283  return options;
284 }
285 
286 #include "IncrementalSearchBar.moc"
Konsole::IncrementalSearchBar::findPreviousClicked
void findPreviousClicked()
Emitted when the user clicks the button to find the previous match.
Konsole::IncrementalSearchBar::eventFilter
virtual bool eventFilter(QObject *watched, QEvent *event)
Definition: IncrementalSearchBar.cpp:186
Konsole::IncrementalSearchBar::searchChanged
void searchChanged(const QString &text)
Emitted when the text entered in the search box is altered.
Konsole::IncrementalSearchBar::searchText
QString searchText()
Returns the current search text.
Definition: IncrementalSearchBar.cpp:175
Konsole::IncrementalSearchBar::MatchCase
Searches are case-sensitive or not.
Definition: IncrementalSearchBar.h:68
Konsole::IncrementalSearchBar::RegExp
Searches use regular expressions.
Definition: IncrementalSearchBar.h:70
Konsole::IncrementalSearchBar::searchFromClicked
void searchFromClicked()
The search from beginning/end button.
Konsole::IncrementalSearchBar::closeClicked
void closeClicked()
Emitted when the close button is clicked.
QWidget
Konsole::IncrementalSearchBar::focusLineEdit
void focusLineEdit()
Definition: IncrementalSearchBar.cpp:268
Konsole::IncrementalSearchBar::matchRegExpToggled
void matchRegExpToggled(bool)
Emitted when the user toggles the checkbox to indicate whether the search text should be treated as a...
Konsole::IncrementalSearchBar::setVisible
virtual void setVisible(bool visible)
Definition: IncrementalSearchBar.cpp:233
QObject
IncrementalSearchBar.h
Konsole::IncrementalSearchBar::unhandledMovementKeyPressed
void unhandledMovementKeyPressed(QKeyEvent *event)
A movement key not handled is forwarded to the terminal display.
Konsole::IncrementalSearchBar::optionsChecked
const QBitArray optionsChecked()
Definition: IncrementalSearchBar.cpp:274
Konsole::IncrementalSearchBar::setSearchText
void setSearchText(const QString &text)
Definition: IncrementalSearchBar.cpp:180
Konsole::IncrementalSearchBar::matchCaseToggled
void matchCaseToggled(bool)
Emitted when the user toggles the checkbox to indicate whether matching for the search text should be...
Konsole::IncrementalSearchBar::HighlightMatches
Highlight all matches.
Definition: IncrementalSearchBar.h:66
Konsole::IncrementalSearchBar::findNextClicked
void findNextClicked()
Emitted when the user clicks the button to find the next match.
Konsole::IncrementalSearchBar::clearLineEdit
void clearLineEdit()
Definition: IncrementalSearchBar.cpp:263
Konsole::IncrementalSearchBar::ReverseSearch
Search from the bottom and up.
Definition: IncrementalSearchBar.h:72
Konsole::IncrementalSearchBar::keyPressEvent
virtual void keyPressEvent(QKeyEvent *event)
Definition: IncrementalSearchBar.cpp:217
Konsole::IncrementalSearchBar::setFoundMatch
void setFoundMatch(bool match)
Sets an indicator for the user as to whether or not a match for the current search text was found in ...
Definition: IncrementalSearchBar.cpp:242
Konsole::IncrementalSearchBar::IncrementalSearchBar
IncrementalSearchBar(QWidget *parent=0)
Constructs a new incremental search bar with the given parent widget.
Definition: IncrementalSearchBar.cpp:39
Konsole::IncrementalSearchBar::highlightMatchesToggled
void highlightMatchesToggled(bool)
Emitted when the user toggles the checkbox to indicate whether matches for the search text should be ...
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:31:24 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Konsole

Skip menu "Konsole"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Applications
  •   Libraries
  •     libkonq
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal