• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Konsole

IncrementalSearchBar.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2006-2007 by Robert Knight <robertknight@gmail.com>
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or
00007     (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program; if not, write to the Free Software
00016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017     02110-1301  USA.
00018 */
00019 
00020 // Own
00021 #include "IncrementalSearchBar.h"
00022 
00023 // Qt
00024 #include <QtGui/QCheckBox>
00025 #include <QtGui/QBoxLayout>
00026 #include <QtGui/QLabel>
00027 #include <QtGui/QLineEdit>
00028 #include <QtGui/QProgressBar>
00029 #include <QtGui/QKeyEvent>
00030 #include <QtCore/QTimer>
00031 #include <QtGui/QToolButton>
00032 
00033 // KDE
00034 #include <KColorScheme>
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 QLineEdit(this);
00060     _searchEdit->installEventFilter(this);
00061     _searchEdit->setObjectName("search-edit");
00062     _searchEdit->setToolTip( i18n("Enter the text to search for here") );
00063 
00064     // text box may be a minimum of 6 characters wide and a maximum of 10 characters wide
00065     // (since the maxWidth metric is used here, more characters probably will fit in than 6
00066     //  and 10)
00067     QFontMetrics metrics(_searchEdit->font());
00068     int maxWidth = metrics.maxWidth();
00069     _searchEdit->setMinimumWidth(maxWidth*6);
00070     _searchEdit->setMaximumWidth(maxWidth*10);
00071 
00072     _searchTimer = new QTimer(this);
00073     _searchTimer->setInterval(250);
00074     _searchTimer->setSingleShot(true);
00075     connect( _searchTimer , SIGNAL(timeout()) , this , SLOT(notifySearchChanged()) );
00076     connect( _searchEdit , SIGNAL(textChanged(const QString&)) , _searchTimer , SLOT(start()));
00077 
00078     QToolButton* findNext = new QToolButton(this);
00079     findNext->setObjectName("find-next-button");
00080     findNext->setText(i18n("Next"));
00081     findNext->setAutoRaise(true);
00082     findNext->setIcon( KIcon("go-down-search") );
00083     findNext->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
00084     findNext->setToolTip( i18n("Find the next match for the current search phrase") );
00085     connect( findNext , SIGNAL(clicked()) , this , SIGNAL(findNextClicked()) );
00086 
00087     QToolButton* findPrev = new QToolButton(this);
00088     findPrev->setObjectName("find-previous-button");
00089     findPrev->setText(i18n("Previous"));
00090     findPrev->setAutoRaise(true);
00091     findPrev->setIcon( KIcon("go-up-search") );
00092     findPrev->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
00093     findPrev->setToolTip( i18n("Find the previous match for the current search phrase") );
00094     connect( findPrev , SIGNAL(clicked()) , this , SIGNAL(findPreviousClicked()) );
00095 
00096     if ( features & HighlightMatches )
00097     {
00098         _highlightBox = new QCheckBox( i18n("Highlight all") , this );
00099         _highlightBox->setObjectName("highlight-matches-box");
00100         _highlightBox->setToolTip( i18n("Sets whether matching text should be highlighted") );
00101         _highlightBox->setChecked(true);
00102         connect( _highlightBox , SIGNAL(toggled(bool)) , this , 
00103                  SIGNAL(highlightMatchesToggled(bool)) );
00104     }
00105 
00106     if ( features & MatchCase )
00107     {
00108         _matchCaseBox = new QCheckBox( i18n("Match case") , this );
00109         _matchCaseBox->setObjectName("match-case-box");
00110         _matchCaseBox->setToolTip( i18n("Sets whether the searching is case sensitive") );
00111         connect( _matchCaseBox , SIGNAL(toggled(bool)) , this , SIGNAL(matchCaseToggled(bool)) );
00112     }
00113 
00114     if ( features & RegExp )
00115     {
00116         _matchRegExpBox = new QCheckBox( i18n("Match regular expression") , this );
00117         _matchRegExpBox->setObjectName("match-regexp-box");
00118         _matchRegExpBox->setToolTip( i18n("Sets whether the search phrase is interpreted as normal text or"
00119                       " as a regular expression") );
00120         connect( _matchRegExpBox , SIGNAL(toggled(bool)) , this , SIGNAL(matchRegExpToggled(bool)) );
00121     }
00122 
00123     QProgressBar* _progress = new QProgressBar(this);
00124     _progress->setMinimum(0);
00125     _progress->setMaximum(0);
00126     _progress->setVisible(false);
00127 
00128     QLabel* _continueLabel = new QLabel(this);
00129     _continueLabel->setVisible(false);
00130 
00131     layout->addWidget(close);
00132     layout->addWidget(findLabel);
00133     layout->addWidget(_searchEdit);
00134     layout->addWidget(findNext);
00135     layout->addWidget(findPrev);
00136 
00137     // optional features
00138     if ( features & HighlightMatches ) layout->addWidget(_highlightBox);
00139     if ( features & MatchCase        ) layout->addWidget(_matchCaseBox);
00140     if ( features & RegExp           ) layout->addWidget(_matchRegExpBox);
00141     
00142     layout->addWidget(_progress);
00143     layout->addWidget(_continueLabel);
00144     layout->addStretch();
00145 
00146     layout->setMargin(4);
00147 
00148     setLayout(layout);
00149 }
00150 void IncrementalSearchBar::notifySearchChanged()
00151 {
00152     emit searchChanged( searchText() );
00153 }
00154 QString IncrementalSearchBar::searchText()
00155 {
00156     return _searchEdit->text();
00157 }
00158 bool IncrementalSearchBar::highlightMatches()
00159 {
00160     if ( !_highlightBox )
00161     {
00162         return true;
00163     }
00164     else
00165     {
00166         return _highlightBox->isChecked();
00167     }
00168 }
00169 bool IncrementalSearchBar::matchCase()
00170 {
00171     if ( !_matchCaseBox )
00172     {
00173         return false;
00174     }
00175     else
00176     {
00177         return _matchCaseBox->isChecked();
00178     }
00179 }
00180 bool IncrementalSearchBar::matchRegExp()
00181 {
00182     if ( !_matchRegExpBox )
00183     {
00184         return false;
00185     }
00186     else
00187     {
00188         return _matchRegExpBox->isChecked();
00189     }
00190 }
00191 
00192 bool IncrementalSearchBar::eventFilter(QObject* watched , QEvent* event)
00193 {
00194     if ( watched == _searchEdit )
00195     {
00196         if ( event->type() == QEvent::KeyPress )
00197         {
00198             QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
00199 
00200             if ( keyEvent->key() == Qt::Key_Escape )
00201             {
00202                 emit closeClicked();
00203                 return true;
00204             }
00205         }        
00206     }
00207         
00208     return QWidget::eventFilter(watched,event);
00209 }
00210 
00211 void IncrementalSearchBar::setVisible(bool visible)
00212 {
00213     QWidget::setVisible(visible);
00214 
00215     if ( visible )
00216     {
00217         //TODO - Check if this is the correct reason value to use here
00218         _searchEdit->setFocus( Qt::ActiveWindowFocusReason );
00219         _searchEdit->selectAll();
00220     }
00221 }
00222 
00223 void IncrementalSearchBar::setFoundMatch( bool match )
00224 {
00225     if ( !match && !_searchEdit->text().isEmpty() )
00226     {
00227         KStatefulBrush foregroundBrush(KColorScheme::View,KColorScheme::NegativeText);
00228         KStatefulBrush backgroundBrush(KColorScheme::View,KColorScheme::NegativeBackground);
00229 
00230         QString styleSheet = QString("QLineEdit{ color:%1 ; background-color:%2 }")
00231                              .arg(foregroundBrush.brush(_searchEdit).color().name())
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 #include "IncrementalSearchBar.moc"

Konsole

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

API Reference

Skip menu "API Reference"
  • Konsole
  • Libraries
  •   libkonq
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal