Konsole
IncrementalSearchBar.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "IncrementalSearchBar.h"
00022
00023
00024 #include <QtGui/QCheckBox>
00025 #include <QtGui/QBoxLayout>
00026 #include <QtGui/QLabel>
00027 #include <QtGui/QProgressBar>
00028 #include <QtGui/QKeyEvent>
00029 #include <QtCore/QTimer>
00030 #include <QtGui/QToolButton>
00031
00032
00033 #include <KColorScheme>
00034 #include <KLineEdit>
00035 #include <KLocale>
00036 #include <KIcon>
00037
00038 using namespace Konsole;
00039
00040 IncrementalSearchBar::IncrementalSearchBar(Features features , QWidget* parent)
00041 : QWidget(parent)
00042 , _foundMatch(false)
00043 , _matchCaseBox(0)
00044 , _matchRegExpBox(0)
00045 , _highlightBox(0)
00046 , _searchEdit(0)
00047 , _continueLabel(0)
00048 {
00049 QHBoxLayout* layout = new QHBoxLayout(this);
00050
00051 QToolButton* close = new QToolButton(this);
00052 close->setObjectName("close-button");
00053 close->setToolTip( i18n("Close the search bar") );
00054 close->setAutoRaise(true);
00055 close->setIcon(KIcon("dialog-close"));
00056 connect( close , SIGNAL(clicked()) , this , SIGNAL(closeClicked()) );
00057
00058 QLabel* findLabel = new QLabel(i18n("Find:"),this);
00059 _searchEdit = new KLineEdit(this);
00060 _searchEdit->setClearButtonShown(true);
00061 _searchEdit->installEventFilter(this);
00062 _searchEdit->setObjectName("search-edit");
00063 _searchEdit->setToolTip( i18n("Enter the text to search for here") );
00064
00065
00066
00067
00068 QFontMetrics metrics(_searchEdit->font());
00069 int maxWidth = metrics.maxWidth();
00070 _searchEdit->setMinimumWidth(maxWidth*6);
00071 _searchEdit->setMaximumWidth(maxWidth*10);
00072
00073 _searchTimer = new QTimer(this);
00074 _searchTimer->setInterval(250);
00075 _searchTimer->setSingleShot(true);
00076 connect( _searchTimer , SIGNAL(timeout()) , this , SLOT(notifySearchChanged()) );
00077 connect( _searchEdit , SIGNAL(clearButtonClicked()) , this , SLOT(clearLineEdit()) );
00078 connect( _searchEdit , SIGNAL(textChanged(const QString&)) , _searchTimer , SLOT(start()));
00079
00080 QToolButton* findNext = new QToolButton(this);
00081 findNext->setObjectName("find-next-button");
00082 findNext->setText(i18n("Next"));
00083 findNext->setAutoRaise(true);
00084 findNext->setIcon( KIcon("go-down-search") );
00085 findNext->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
00086 findNext->setToolTip( i18n("Find the next match for the current search phrase") );
00087 connect( findNext , SIGNAL(clicked()) , this , SIGNAL(findNextClicked()) );
00088
00089 QToolButton* findPrev = new QToolButton(this);
00090 findPrev->setObjectName("find-previous-button");
00091 findPrev->setText(i18n("Previous"));
00092 findPrev->setAutoRaise(true);
00093 findPrev->setIcon( KIcon("go-up-search") );
00094 findPrev->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
00095 findPrev->setToolTip( i18n("Find the previous match for the current search phrase") );
00096 connect( findPrev , SIGNAL(clicked()) , this , SIGNAL(findPreviousClicked()) );
00097
00098 if ( features & HighlightMatches )
00099 {
00100 _highlightBox = new QCheckBox( i18n("Highlight all") , this );
00101 _highlightBox->setObjectName("highlight-matches-box");
00102 _highlightBox->setToolTip( i18n("Sets whether matching text should be highlighted") );
00103 _highlightBox->setChecked(true);
00104 connect( _highlightBox , SIGNAL(toggled(bool)) , this ,
00105 SIGNAL(highlightMatchesToggled(bool)) );
00106 }
00107
00108 if ( features & MatchCase )
00109 {
00110 _matchCaseBox = new QCheckBox( i18n("Match case") , this );
00111 _matchCaseBox->setObjectName("match-case-box");
00112 _matchCaseBox->setToolTip( i18n("Sets whether the search is case sensitive") );
00113 connect( _matchCaseBox , SIGNAL(toggled(bool)) , this , SIGNAL(matchCaseToggled(bool)) );
00114 }
00115
00116 if ( features & RegExp )
00117 {
00118 _matchRegExpBox = new QCheckBox( i18n("Match regular expression") , this );
00119 _matchRegExpBox->setObjectName("match-regexp-box");
00120 _matchRegExpBox->setToolTip( i18n("Sets whether the search phrase is interpreted as normal text or"
00121 " as a regular expression") );
00122 connect( _matchRegExpBox , SIGNAL(toggled(bool)) , this , SIGNAL(matchRegExpToggled(bool)) );
00123 }
00124
00125 QProgressBar* _progress = new QProgressBar(this);
00126 _progress->setMinimum(0);
00127 _progress->setMaximum(0);
00128 _progress->setVisible(false);
00129
00130 QLabel* _continueLabel = new QLabel(this);
00131 _continueLabel->setVisible(false);
00132
00133 layout->addWidget(close);
00134 layout->addWidget(findLabel);
00135 layout->addWidget(_searchEdit);
00136 layout->addWidget(findNext);
00137 layout->addWidget(findPrev);
00138
00139
00140 if ( features & HighlightMatches ) layout->addWidget(_highlightBox);
00141 if ( features & MatchCase ) layout->addWidget(_matchCaseBox);
00142 if ( features & RegExp ) layout->addWidget(_matchRegExpBox);
00143
00144 layout->addWidget(_progress);
00145 layout->addWidget(_continueLabel);
00146 layout->addStretch();
00147
00148 layout->setMargin(4);
00149
00150 setLayout(layout);
00151 }
00152 void IncrementalSearchBar::notifySearchChanged()
00153 {
00154 emit searchChanged( searchText() );
00155 }
00156 QString IncrementalSearchBar::searchText()
00157 {
00158 return _searchEdit->text();
00159 }
00160 bool IncrementalSearchBar::highlightMatches()
00161 {
00162 if ( !_highlightBox )
00163 {
00164 return true;
00165 }
00166 else
00167 {
00168 return _highlightBox->isChecked();
00169 }
00170 }
00171 bool IncrementalSearchBar::matchCase()
00172 {
00173 if ( !_matchCaseBox )
00174 {
00175 return false;
00176 }
00177 else
00178 {
00179 return _matchCaseBox->isChecked();
00180 }
00181 }
00182 bool IncrementalSearchBar::matchRegExp()
00183 {
00184 if ( !_matchRegExpBox )
00185 {
00186 return false;
00187 }
00188 else
00189 {
00190 return _matchRegExpBox->isChecked();
00191 }
00192 }
00193
00194 bool IncrementalSearchBar::eventFilter(QObject* watched , QEvent* event)
00195 {
00196 if ( watched == _searchEdit )
00197 {
00198 if ( event->type() == QEvent::KeyPress )
00199 {
00200 QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
00201
00202 if ( keyEvent->key() == Qt::Key_Escape )
00203 {
00204 emit closeClicked();
00205 return true;
00206 }
00207 }
00208 }
00209
00210 return QWidget::eventFilter(watched,event);
00211 }
00212
00213 void IncrementalSearchBar::setVisible(bool visible)
00214 {
00215 QWidget::setVisible(visible);
00216
00217 if ( visible )
00218 {
00219
00220 _searchEdit->setFocus( Qt::ActiveWindowFocusReason );
00221 _searchEdit->selectAll();
00222 }
00223 }
00224
00225 void IncrementalSearchBar::setFoundMatch( bool match )
00226 {
00227 if ( !match && !_searchEdit->text().isEmpty() )
00228 {
00229 KStatefulBrush backgroundBrush(KColorScheme::View,KColorScheme::NegativeBackground);
00230
00231 QString styleSheet = QString("QLineEdit{ background-color:%1 }")
00232 .arg(backgroundBrush.brush(_searchEdit).color().name());
00233
00234 _searchEdit->setStyleSheet( styleSheet );
00235 }
00236 else
00237 {
00238 _searchEdit->setStyleSheet( QString() );
00239 }
00240 }
00241
00242 void IncrementalSearchBar::setContinueFlag( Continue flag )
00243 {
00244 if ( flag == ContinueFromTop )
00245 {
00246 _continueLabel->setText( i18n("Search reached bottom, continued from top.") );
00247 _continueLabel->show();
00248 }
00249 else if ( flag == ContinueFromBottom )
00250 {
00251 _continueLabel->setText( i18n("Search reached top, continued from bottom.") );
00252 _continueLabel->show();
00253 }
00254 else if ( flag == ClearContinue )
00255 {
00256 _continueLabel->hide();
00257 }
00258 }
00259
00260 void IncrementalSearchBar::clearLineEdit()
00261 {
00262 _searchEdit->setStyleSheet( QString() );
00263 }
00264
00265 #include "IncrementalSearchBar.moc"