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

KHTML

khtmlfindbar.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002  *
00003  * Copyright (C) 2008 Bernhard Beschow <bbeschow cs tu berlin de>
00004  *           (C) 2008 Germain Garand <germain@ebooksfrance.org>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public License
00017  * along with this library; see the file COPYING.LIB.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  */
00021 
00022 #include "khtmlfindbar.h"
00023 
00024 #include "khtml_part.h"
00025 
00026 #include <kfind.h>
00027 #include <kcolorscheme.h>
00028 
00029 #include <QtGui/QMenu>
00030 #include <QtGui/QLineEdit>
00031 #include <QtGui/QShortcut>
00032 
00033 #define d this
00034 
00035 KHTMLFindBar::KHTMLFindBar( QWidget *parent ) :
00036     KHTMLViewBarWidget( true, parent ),
00037     m_enabled( KFind::WholeWordsOnly | KFind::FromCursor | KFind::SelectedText | KFind::CaseSensitive | KFind::FindBackwards | KFind::RegularExpression )
00038 {
00039     setupUi( centralWidget() );
00040 
00041     m_next->setIcon( KIcon( "go-down-search" ) );
00042     m_previous->setIcon( KIcon( "go-up-search" ) );
00043     m_next->setDisabled( true );
00044     m_previous->setDisabled( true );
00045 
00046     // Fill options menu
00047     m_incMenu = new QMenu();
00048     m_options->setMenu(m_incMenu);
00049     m_caseSensitive = m_incMenu->addAction(i18n("C&ase sensitive"));
00050     m_caseSensitive->setCheckable(true);
00051     m_wholeWordsOnly = m_incMenu->addAction(i18n("&Whole words only"));
00052     m_wholeWordsOnly->setCheckable(true);
00053     m_fromCursor = m_incMenu->addAction(i18n("From c&ursor"));
00054     m_fromCursor->setCheckable(true);
00055     m_selectedText = m_incMenu->addAction(i18n("&Selected text"));
00056     m_selectedText->setCheckable(true);
00057     m_regExp = m_incMenu->addAction(i18n("Regular e&xpression"));
00058     m_regExp->setCheckable(true);
00059 
00060     m_atEnd = false;
00061 
00062     m_find->setDuplicatesEnabled( false );
00063     centralWidget()->setFocusProxy( m_find );
00064 
00065     connect( m_selectedText, SIGNAL(toggled(bool)), this, SLOT(slotSelectedTextToggled(bool)) );
00066     connect( m_find, SIGNAL(editTextChanged(const QString &)), this, SIGNAL(searchChanged()) );
00067     connect( m_find->lineEdit(), SIGNAL(clearButtonClicked()), this, SLOT(slotAddPatternToHistory()) );
00068     connect( this, SIGNAL(hideMe()), this, SLOT(slotAddPatternToHistory()) );
00069     connect( this, SIGNAL(searchChanged()), this, SLOT(slotSearchChanged()) );
00070     connect( m_next, SIGNAL(clicked()), this, SIGNAL(findNextClicked()) );
00071     connect( m_previous, SIGNAL(clicked()), this, SIGNAL(findPreviousClicked()) );
00072     new QShortcut(QKeySequence(Qt::Key_Escape), this, SIGNAL(hideMe()));
00073     connect( m_caseSensitive, SIGNAL(changed()), this, SIGNAL(searchChanged()) );
00074     connect( m_wholeWordsOnly, SIGNAL(changed()), this, SIGNAL(searchChanged()) );
00075     connect( m_fromCursor, SIGNAL(changed()), this, SIGNAL(searchChanged()) );
00076     connect( m_regExp, SIGNAL(changed()), this, SIGNAL(searchChanged()) );
00077 
00078     m_find->setFocus();
00079 }
00080 
00081 QStringList KHTMLFindBar::findHistory() const
00082 {
00083     return d->m_find->historyItems();
00084 }
00085 
00086 long KHTMLFindBar::options() const
00087 {
00088     long options = 0;
00089 
00090     if (d->m_caseSensitive->isChecked())
00091         options |= KFind::CaseSensitive;
00092     if (d->m_wholeWordsOnly->isChecked())
00093         options |= KFind::WholeWordsOnly;
00094     if (d->m_fromCursor->isChecked())
00095         options |= KFind::FromCursor;
00096     if (d->m_selectedText->isChecked())
00097         options |= KFind::SelectedText;
00098     if (d->m_regExp->isChecked())
00099         options |= KFind::RegularExpression;
00100     return options | KHTMLPart::FindNoPopups /* | KFind::FindIncremental */;
00101 }
00102 
00103 QString KHTMLFindBar::pattern() const
00104 {
00105     return d->m_find->currentText();
00106 }
00107 
00108 void KHTMLFindBar::slotSearchChanged()
00109 {
00110    // reset background color of the combo box
00111    if (pattern().isEmpty()) {
00112        d->m_find->setPalette(QPalette());
00113        m_next->setDisabled( true );
00114        m_previous->setDisabled( true );
00115        m_statusLabel->clear();       
00116    } else {
00117        m_prevPattern = pattern();
00118        m_next->setDisabled( false );
00119        m_previous->setDisabled( false );
00120    }
00121 }
00122 
00123 bool KHTMLFindBar::restoreLastPatternFromHistory()
00124 {
00125     if (d->m_find->historyItems().isEmpty())
00126         return false;
00127     d->m_find->lineEdit()->setText( d->m_find->historyItems().first() );
00128     return true;
00129 }
00130 
00131 void KHTMLFindBar::setFindHistory(const QStringList &strings)
00132 {
00133     if (strings.count() > 0)
00134     {
00135         d->m_find->setHistoryItems(strings, true);
00136         //d->m_find->lineEdit()->setText( strings.first() );
00137         //d->m_find->lineEdit()->selectAll();
00138     }
00139     else
00140         d->m_find->clearHistory();
00141 }
00142 
00143 void KHTMLFindBar::setHasSelection(bool hasSelection)
00144 {
00145     if (hasSelection) d->m_enabled |= KFind::SelectedText;
00146     else d->m_enabled &= ~KFind::SelectedText;
00147     d->m_selectedText->setEnabled( hasSelection );
00148     if ( !hasSelection )
00149     {
00150         d->m_selectedText->setChecked( false );
00151         slotSelectedTextToggled( hasSelection );
00152     }
00153 }
00154 
00155 void KHTMLFindBar::slotAddPatternToHistory()
00156 {
00157     bool patternIsEmpty = pattern().isEmpty();
00158     if (!patternIsEmpty || !m_prevPattern.isEmpty()) {
00159         d->m_find->addToHistory(pattern().isEmpty() ? m_prevPattern : pattern());
00160         if (patternIsEmpty && !pattern().isEmpty()) {
00161             // ### Hack - addToHistory sometimes undo the clearing of the lineEdit
00162             // with clear button. Clear it again.
00163             bool sb = d->m_find->blockSignals(true);
00164             d->m_find->lineEdit()->setText(QString());
00165             d->m_find->blockSignals(sb);
00166         }
00167         m_prevPattern = QString();
00168     }
00169 }
00170 
00171 void KHTMLFindBar::slotSelectedTextToggled(bool selec)
00172 {
00173     // From cursor doesn't make sense if we have a selection
00174     m_fromCursor->setEnabled( !selec && (m_enabled & KFind::FromCursor) );
00175     if ( selec ) // uncheck if disabled
00176         m_fromCursor->setChecked( false );
00177 }
00178 
00179 void KHTMLFindBar::setHasCursor(bool hasCursor)
00180 {
00181     if (hasCursor) d->m_enabled |= KFind::FromCursor;
00182     else d->m_enabled &= ~KFind::FromCursor;
00183     d->m_fromCursor->setEnabled( hasCursor );
00184     d->m_fromCursor->setChecked( hasCursor && (options() & KFind::FromCursor) );
00185 }
00186 
00187 void KHTMLFindBar::setOptions(long options)
00188 {
00189     d->m_caseSensitive->setChecked((d->m_enabled & KFind::CaseSensitive) && (options & KFind::CaseSensitive));
00190     d->m_wholeWordsOnly->setChecked((d->m_enabled & KFind::WholeWordsOnly) && (options & KFind::WholeWordsOnly));
00191     d->m_fromCursor->setChecked((d->m_enabled & KFind::FromCursor) && (options & KFind::FromCursor));
00192     d->m_selectedText->setChecked((d->m_enabled & KFind::SelectedText) && (options & KFind::SelectedText));
00193     d->m_regExp->setChecked((d->m_enabled & KFind::RegularExpression) && (options & KFind::RegularExpression));
00194 }
00195 
00196 void KHTMLFindBar::setFoundMatch( bool match )
00197 {
00198     if ( pattern().isEmpty() ) {
00199         m_find->setPalette(QPalette());
00200         m_next->setDisabled( true );
00201         m_previous->setDisabled( true );
00202         m_statusLabel->clear();
00203     } else if ( !match ) {
00204         QPalette newPal( m_find->palette() );
00205         KColorScheme::adjustBackground(newPal, KColorScheme::NegativeBackground);
00206         m_find->setPalette(newPal);
00207         m_statusLabel->setText(i18n("Not found"));
00208     } else {
00209         QPalette newPal( m_find->palette() );
00210         KColorScheme::adjustBackground(newPal, KColorScheme::PositiveBackground);
00211         m_find->setPalette(newPal);
00212         m_statusLabel->clear();
00213     }
00214 }
00215 
00216 void KHTMLFindBar::setAtEnd( bool atEnd )
00217 {
00218     if (atEnd == m_atEnd)
00219         return;
00220     if ( atEnd ) {
00221         m_statusLabel->setText( i18n( "No more matches for this search direction." ) );
00222     } else {
00223         m_statusLabel->clear();
00224     }
00225     m_atEnd = atEnd;
00226 }
00227 
00228 void KHTMLFindBar::setVisible( bool visible )
00229 {
00230     KHTMLViewBarWidget::setVisible( visible );
00231 
00232     if ( visible ) {
00233         m_find->setFocus( Qt::ActiveWindowFocusReason );
00234         m_find->lineEdit()->selectAll();
00235     }
00236 }

KHTML

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
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