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(textChanged(const QString&)) , _searchTimer , SLOT(start()));
00078
00079 QToolButton* findNext = new QToolButton(this);
00080 findNext->setObjectName("find-next-button");
00081 findNext->setText(i18n("Next"));
00082 findNext->setAutoRaise(true);
00083 findNext->setIcon( KIcon("go-down-search") );
00084 findNext->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
00085 findNext->setToolTip( i18n("Find the next match for the current search phrase") );
00086 connect( findNext , SIGNAL(clicked()) , this , SIGNAL(findNextClicked()) );
00087
00088 QToolButton* findPrev = new QToolButton(this);
00089 findPrev->setObjectName("find-previous-button");
00090 findPrev->setText(i18n("Previous"));
00091 findPrev->setAutoRaise(true);
00092 findPrev->setIcon( KIcon("go-up-search") );
00093 findPrev->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
00094 findPrev->setToolTip( i18n("Find the previous match for the current search phrase") );
00095 connect( findPrev , SIGNAL(clicked()) , this , SIGNAL(findPreviousClicked()) );
00096
00097 if ( features & HighlightMatches )
00098 {
00099 _highlightBox = new QCheckBox( i18n("Highlight all") , this );
00100 _highlightBox->setObjectName("highlight-matches-box");
00101 _highlightBox->setToolTip( i18n("Sets whether matching text should be highlighted") );
00102 _highlightBox->setChecked(true);
00103 connect( _highlightBox , SIGNAL(toggled(bool)) , this ,
00104 SIGNAL(highlightMatchesToggled(bool)) );
00105 }
00106
00107 if ( features & MatchCase )
00108 {
00109 _matchCaseBox = new QCheckBox( i18n("Match case") , this );
00110 _matchCaseBox->setObjectName("match-case-box");
00111 _matchCaseBox->setToolTip( i18n("Sets whether the search is case sensitive") );
00112 connect( _matchCaseBox , SIGNAL(toggled(bool)) , this , SIGNAL(matchCaseToggled(bool)) );
00113 }
00114
00115 if ( features & RegExp )
00116 {
00117 _matchRegExpBox = new QCheckBox( i18n("Match regular expression") , this );
00118 _matchRegExpBox->setObjectName("match-regexp-box");
00119 _matchRegExpBox->setToolTip( i18n("Sets whether the search phrase is interpreted as normal text or"
00120 " as a regular expression") );
00121 connect( _matchRegExpBox , SIGNAL(toggled(bool)) , this , SIGNAL(matchRegExpToggled(bool)) );
00122 }
00123
00124 QProgressBar* _progress = new QProgressBar(this);
00125 _progress->setMinimum(0);
00126 _progress->setMaximum(0);
00127 _progress->setVisible(false);
00128
00129 QLabel* _continueLabel = new QLabel(this);
00130 _continueLabel->setVisible(false);
00131
00132 layout->addWidget(close);
00133 layout->addWidget(findLabel);
00134 layout->addWidget(_searchEdit);
00135 layout->addWidget(findNext);
00136 layout->addWidget(findPrev);
00137
00138
00139 if ( features & HighlightMatches ) layout->addWidget(_highlightBox);
00140 if ( features & MatchCase ) layout->addWidget(_matchCaseBox);
00141 if ( features & RegExp ) layout->addWidget(_matchRegExpBox);
00142
00143 layout->addWidget(_progress);
00144 layout->addWidget(_continueLabel);
00145 layout->addStretch();
00146
00147 layout->setMargin(4);
00148
00149 setLayout(layout);
00150 }
00151 void IncrementalSearchBar::notifySearchChanged()
00152 {
00153 emit searchChanged( searchText() );
00154 }
00155 QString IncrementalSearchBar::searchText()
00156 {
00157 return _searchEdit->text();
00158 }
00159 bool IncrementalSearchBar::highlightMatches()
00160 {
00161 if ( !_highlightBox )
00162 {
00163 return true;
00164 }
00165 else
00166 {
00167 return _highlightBox->isChecked();
00168 }
00169 }
00170 bool IncrementalSearchBar::matchCase()
00171 {
00172 if ( !_matchCaseBox )
00173 {
00174 return false;
00175 }
00176 else
00177 {
00178 return _matchCaseBox->isChecked();
00179 }
00180 }
00181 bool IncrementalSearchBar::matchRegExp()
00182 {
00183 if ( !_matchRegExpBox )
00184 {
00185 return false;
00186 }
00187 else
00188 {
00189 return _matchRegExpBox->isChecked();
00190 }
00191 }
00192
00193 bool IncrementalSearchBar::eventFilter(QObject* watched , QEvent* event)
00194 {
00195 if ( watched == _searchEdit )
00196 {
00197 if ( event->type() == QEvent::KeyPress )
00198 {
00199 QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
00200
00201 if ( keyEvent->key() == Qt::Key_Escape )
00202 {
00203 emit closeClicked();
00204 return true;
00205 }
00206 }
00207 }
00208
00209 return QWidget::eventFilter(watched,event);
00210 }
00211
00212 void IncrementalSearchBar::setVisible(bool visible)
00213 {
00214 QWidget::setVisible(visible);
00215
00216 if ( visible )
00217 {
00218
00219 _searchEdit->setFocus( Qt::ActiveWindowFocusReason );
00220 _searchEdit->selectAll();
00221 }
00222 }
00223
00224 void IncrementalSearchBar::setFoundMatch( bool match )
00225 {
00226 if ( !match && !_searchEdit->text().isEmpty() )
00227 {
00228 KStatefulBrush backgroundBrush(KColorScheme::View,KColorScheme::NegativeBackground);
00229
00230 QString styleSheet = QString("QLineEdit{ background-color:%1 }")
00231 .arg(backgroundBrush.brush(_searchEdit).color().name());
00232
00233 _searchEdit->setStyleSheet( styleSheet );
00234 }
00235 else
00236 {
00237 _searchEdit->setStyleSheet( QString() );
00238 }
00239 }
00240
00241 void IncrementalSearchBar::setContinueFlag( Continue flag )
00242 {
00243 if ( flag == ContinueFromTop )
00244 {
00245 _continueLabel->setText( i18n("Search reached bottom, continued from top.") );
00246 _continueLabel->show();
00247 }
00248 else if ( flag == ContinueFromBottom )
00249 {
00250 _continueLabel->setText( i18n("Search reached top, continued from bottom.") );
00251 _continueLabel->show();
00252 }
00253 else if ( flag == ClearContinue )
00254 {
00255 _continueLabel->hide();
00256 }
00257 }
00258
00259 #include "IncrementalSearchBar.moc"