24 #include <QHBoxLayout>
26 #include <QtGui/QKeyEvent>
27 #include <QtCore/QTimer>
28 #include <QToolButton>
32 #include <KColorScheme>
34 #include <KLocalizedString>
37 using namespace Konsole;
44 , _highlightMatches(0)
47 , _findPreviousButton(0)
48 , _searchFromButton(0)
50 QHBoxLayout* barLayout =
new QHBoxLayout(
this);
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()));
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"));
69 QFontMetrics metrics(_searchEdit->font());
70 int maxWidth = metrics.maxWidth();
71 _searchEdit->setMinimumWidth(maxWidth * 6);
72 _searchEdit->setMaximumWidth(maxWidth * 10);
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()));
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()));
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"));
96 _searchFromButton =
new QToolButton(
this);
97 _searchFromButton->setObjectName(QLatin1String(
"search-from-button"));
98 connect(_searchFromButton , SIGNAL(clicked()) ,
this , SIGNAL(
searchFromClicked()));
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"));
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);
118 QMenu* optionsMenu =
new QMenu(
this);
119 optionsButton->setMenu(optionsMenu);
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)),
127 _regExpression = optionsMenu->addAction(i18nc(
"@item:inmenu",
"Match regular expression"));
128 _regExpression->setCheckable(
true);
129 connect(_regExpression, SIGNAL(toggled(
bool)),
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)),
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();
147 barLayout->addStretch();
149 barLayout->setContentsMargins(4, 4, 4, 4);
151 setLayout(barLayout);
154 void IncrementalSearchBar::notifySearchChanged()
159 void IncrementalSearchBar::updateButtonsAccordingToReverseSearchSetting()
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"));
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"));
177 return _searchEdit->text();
183 _searchEdit->setText(text);
188 if (watched == _searchEdit) {
189 if (aEvent->type() == QEvent::KeyPress) {
190 QKeyEvent* keyEvent =
static_cast<QKeyEvent*
>(aEvent);
192 if (keyEvent->key() == Qt::Key_Escape) {
197 if (keyEvent->key() == Qt::Key_Return && !keyEvent->modifiers()) {
198 _findNextButton->click();
202 if ((keyEvent->key() == Qt::Key_Return) && (keyEvent->modifiers() == Qt::ShiftModifier)) {
203 _findPreviousButton->click();
207 if ((keyEvent->key() == Qt::Key_Return) && (keyEvent->modifiers() == Qt::ControlModifier)) {
208 _searchFromButton->click();
214 return QWidget::eventFilter(watched, aEvent);
219 static QSet<int> movementKeysToPassAlong = QSet<int>()
225 if (movementKeysToPassAlong.contains(event->key()) &&
226 (event->modifiers() == Qt::ShiftModifier)) {
229 QWidget::keyPressEvent(event);
235 QWidget::setVisible(visible);
244 if (!match && !_searchEdit->text().isEmpty()) {
245 KStatefulBrush backgroundBrush(KColorScheme::View, KColorScheme::NegativeBackground);
247 QString matchStyleSheet = QString(
"QLineEdit{ background-color:%1 }")
248 .arg(backgroundBrush.brush(_searchEdit).color().name());
250 _searchEdit->setStyleSheet(matchStyleSheet);
251 }
else if (_searchEdit->text().isEmpty()) {
254 KStatefulBrush backgroundBrush(KColorScheme::View, KColorScheme::PositiveBackground);
256 QString matchStyleSheet = QString(
"QLineEdit{ background-color:%1 }")
257 .arg(backgroundBrush.brush(_searchEdit).color().name());
259 _searchEdit->setStyleSheet(matchStyleSheet);
265 _searchEdit->setStyleSheet(QString());
270 _searchEdit->setFocus(Qt::ActiveWindowFocusReason);
271 _searchEdit->selectAll();
276 QBitArray options(4, 0);
278 if (_caseSensitive->isChecked()) options.setBit(
MatchCase);
279 if (_regExpression->isChecked()) options.setBit(
RegExp);
281 if (_reverseSearch->isChecked()) options.setBit(
ReverseSearch);
286 #include "IncrementalSearchBar.moc"
void findPreviousClicked()
Emitted when the user clicks the button to find the previous match.
virtual bool eventFilter(QObject *watched, QEvent *event)
void searchChanged(const QString &text)
Emitted when the text entered in the search box is altered.
QString searchText()
Returns the current search text.
Searches are case-sensitive or not.
Searches use regular expressions.
void searchFromClicked()
The search from beginning/end button.
void closeClicked()
Emitted when the close button is clicked.
void matchRegExpToggled(bool)
Emitted when the user toggles the checkbox to indicate whether the search text should be treated as a...
virtual void setVisible(bool visible)
void unhandledMovementKeyPressed(QKeyEvent *event)
A movement key not handled is forwarded to the terminal display.
const QBitArray optionsChecked()
void setSearchText(const QString &text)
void matchCaseToggled(bool)
Emitted when the user toggles the checkbox to indicate whether matching for the search text should be...
void findNextClicked()
Emitted when the user clicks the button to find the next match.
Search from the bottom and up.
virtual void keyPressEvent(QKeyEvent *event)
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 ...
IncrementalSearchBar(QWidget *parent=0)
Constructs a new incremental search bar with the given parent widget.
void highlightMatchesToggled(bool)
Emitted when the user toggles the checkbox to indicate whether matches for the search text should be ...