• Skip to content
  • Skip to link menu
KDE 4.3 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 
00032 #define d this
00033 
00034 KHTMLFindBar::KHTMLFindBar( QWidget *parent ) :
00035     KHTMLViewBarWidget( true, parent ),
00036     m_enabled( KFind::WholeWordsOnly | KFind::FromCursor | KFind::SelectedText | KFind::CaseSensitive | KFind::FindBackwards | KFind::RegularExpression )
00037 {
00038     setupUi( centralWidget() );
00039 
00040     m_next->setIcon( KIcon( "go-down-search" ) );
00041     m_previous->setIcon( KIcon( "go-up-search" ) );
00042     m_next->setDisabled( true );
00043     m_previous->setDisabled( true );
00044 
00045     // Fill options menu
00046     m_incMenu = new QMenu();
00047     m_options->setMenu(m_incMenu);
00048     m_caseSensitive = m_incMenu->addAction(i18n("C&ase sensitive"));
00049     m_caseSensitive->setCheckable(true);
00050     m_wholeWordsOnly = m_incMenu->addAction(i18n("&Whole words only"));
00051     m_wholeWordsOnly->setCheckable(true);
00052     m_fromCursor = m_incMenu->addAction(i18n("From c&ursor"));
00053     m_fromCursor->setCheckable(true);
00054     m_selectedText = m_incMenu->addAction(i18n("&Selected text"));
00055     m_selectedText->setCheckable(true);
00056     m_regExp = m_incMenu->addAction(i18n("Regular e&xpression"));
00057     m_regExp->setCheckable(true);
00058 
00059     m_atEnd = false;
00060 
00061     m_find->setDuplicatesEnabled( false );
00062     centralWidget()->setFocusProxy( m_find );
00063 
00064     connect( m_selectedText, SIGNAL(toggled(bool)), this, SLOT(slotSelectedTextToggled(bool)) );
00065     connect( m_find, SIGNAL(editTextChanged(const QString &)), this, SIGNAL(searchChanged()) );
00066     connect( m_find->lineEdit(), SIGNAL(clearButtonClicked()), this, SLOT(slotAddPatternToHistory()) );
00067     connect( this, SIGNAL(hideMe()), this, SLOT(slotAddPatternToHistory()) );
00068     connect( this, SIGNAL(searchChanged()), this, SLOT(slotSearchChanged()) );
00069     connect( m_next, SIGNAL(clicked()), this, SIGNAL(findNextClicked()) );
00070     connect( m_previous, SIGNAL(clicked()), this, SIGNAL(findPreviousClicked()) );
00071     connect( m_caseSensitive, SIGNAL(changed()), this, SIGNAL(searchChanged()) );
00072     connect( m_wholeWordsOnly, SIGNAL(changed()), this, SIGNAL(searchChanged()) );
00073     connect( m_fromCursor, SIGNAL(changed()), this, SIGNAL(searchChanged()) );
00074     connect( m_regExp, SIGNAL(changed()), this, SIGNAL(searchChanged()) );
00075 
00076     m_find->setFocus();
00077 }
00078 
00079 QStringList KHTMLFindBar::findHistory() const
00080 {
00081     return d->m_find->historyItems();
00082 }
00083 
00084 long KHTMLFindBar::options() const
00085 {
00086     long options = 0;
00087 
00088     if (d->m_caseSensitive->isChecked())
00089         options |= KFind::CaseSensitive;
00090     if (d->m_wholeWordsOnly->isChecked())
00091         options |= KFind::WholeWordsOnly;
00092     if (d->m_fromCursor->isChecked())
00093         options |= KFind::FromCursor;
00094     if (d->m_selectedText->isChecked())
00095         options |= KFind::SelectedText;
00096     if (d->m_regExp->isChecked())
00097         options |= KFind::RegularExpression;
00098     return options | KHTMLPart::FindNoPopups /* | KFind::FindIncremental */;
00099 }
00100 
00101 QString KHTMLFindBar::pattern() const
00102 {
00103     return m_find->currentText();
00104 }
00105 
00106 void KHTMLFindBar::slotSearchChanged()
00107 {
00108    // reset background color of the combo box
00109    if (pattern().isEmpty()) {
00110        d->m_find->setPalette(QPalette());
00111        m_next->setDisabled( true );
00112        m_previous->setDisabled( true );
00113        m_statusLabel->clear();
00114    } else {
00115        m_prevPattern = pattern();
00116        m_next->setDisabled( false );
00117        m_previous->setDisabled( false );
00118    }
00119 }
00120 
00121 bool KHTMLFindBar::restoreLastPatternFromHistory()
00122 {
00123     if (d->m_find->historyItems().isEmpty())
00124         return false;
00125     d->m_find->lineEdit()->setText( d->m_find->historyItems().first() );
00126     return true;
00127 }
00128 
00129 void KHTMLFindBar::setFindHistory(const QStringList &strings)
00130 {
00131     if (strings.count() > 0)
00132     {
00133         d->m_find->setHistoryItems(strings, true);
00134         //d->m_find->lineEdit()->setText( strings.first() );
00135         //d->m_find->lineEdit()->selectAll();
00136     }
00137     else
00138         d->m_find->clearHistory();
00139 }
00140 
00141 void KHTMLFindBar::setHasSelection(bool hasSelection)
00142 {
00143     if (hasSelection) d->m_enabled |= KFind::SelectedText;
00144     else d->m_enabled &= ~KFind::SelectedText;
00145     d->m_selectedText->setEnabled( hasSelection );
00146     if ( !hasSelection )
00147     {
00148         d->m_selectedText->setChecked( false );
00149         slotSelectedTextToggled( hasSelection );
00150     }
00151 }
00152 
00153 void KHTMLFindBar::slotAddPatternToHistory()
00154 {
00155     bool patternIsEmpty = pattern().isEmpty();
00156     if (!patternIsEmpty || !m_prevPattern.isEmpty()) {
00157         d->m_find->addToHistory(pattern().isEmpty() ? m_prevPattern : pattern());
00158         if (patternIsEmpty && !pattern().isEmpty()) {
00159             // ### Hack - addToHistory sometimes undo the clearing of the lineEdit
00160             // with clear button. Clear it again.
00161             bool sb = d->m_find->blockSignals(true);
00162             d->m_find->lineEdit()->setText(QString());
00163             d->m_find->blockSignals(sb);
00164         }
00165         m_prevPattern = QString();
00166     }
00167 }
00168 
00169 void KHTMLFindBar::slotSelectedTextToggled(bool selec)
00170 {
00171     // From cursor doesn't make sense if we have a selection
00172     m_fromCursor->setEnabled( !selec && (m_enabled & KFind::FromCursor) );
00173     if ( selec ) // uncheck if disabled
00174         m_fromCursor->setChecked( false );
00175 }
00176 
00177 void KHTMLFindBar::setHasCursor(bool hasCursor)
00178 {
00179     if (hasCursor) d->m_enabled |= KFind::FromCursor;
00180     else d->m_enabled &= ~KFind::FromCursor;
00181     d->m_fromCursor->setEnabled( hasCursor );
00182     d->m_fromCursor->setChecked( hasCursor && (options() & KFind::FromCursor) );
00183 }
00184 
00185 void KHTMLFindBar::setOptions(long options)
00186 {
00187     d->m_caseSensitive->setChecked((d->m_enabled & KFind::CaseSensitive) && (options & KFind::CaseSensitive));
00188     d->m_wholeWordsOnly->setChecked((d->m_enabled & KFind::WholeWordsOnly) && (options & KFind::WholeWordsOnly));
00189     d->m_fromCursor->setChecked((d->m_enabled & KFind::FromCursor) && (options & KFind::FromCursor));
00190     d->m_selectedText->setChecked((d->m_enabled & KFind::SelectedText) && (options & KFind::SelectedText));
00191     d->m_regExp->setChecked((d->m_enabled & KFind::RegularExpression) && (options & KFind::RegularExpression));
00192 }
00193 
00194 void KHTMLFindBar::setFoundMatch( bool match )
00195 {
00196     if ( pattern().isEmpty() ) {
00197         m_find->setPalette(QPalette());
00198         m_next->setDisabled( true );
00199         m_previous->setDisabled( true );
00200         m_statusLabel->clear();
00201     } else if ( !match ) {
00202         QPalette newPal( m_find->palette() );
00203         KColorScheme::adjustBackground(newPal, KColorScheme::NegativeBackground);
00204         m_find->setPalette(newPal);
00205         m_statusLabel->setText(i18n("Not found"));
00206     } else {
00207         QPalette newPal( m_find->palette() );
00208         KColorScheme::adjustBackground(newPal, KColorScheme::PositiveBackground);
00209         m_find->setPalette(newPal);
00210         m_statusLabel->clear();
00211     }
00212 }
00213 
00214 void KHTMLFindBar::setAtEnd( bool atEnd )
00215 {
00216     if (atEnd == m_atEnd)
00217         return;
00218     if ( atEnd ) {
00219         m_statusLabel->setText( i18n( "No more matches for this search direction." ) );
00220     } else {
00221         m_statusLabel->clear();
00222     }
00223     m_atEnd = atEnd;
00224 }
00225 
00226 void KHTMLFindBar::setVisible( bool visible )
00227 {
00228     KHTMLViewBarWidget::setVisible( visible );
00229 
00230     if ( visible ) {
00231         m_find->setFocus( Qt::ActiveWindowFocusReason );
00232         m_find->lineEdit()->selectAll();
00233     }
00234 }
00235 
00236 bool KHTMLFindBar::event(QEvent* e)
00237 {
00238     // Close the bar when pressing Escape.
00239     // Not using a QShortcut for this because it could conflict with
00240     // window-global actions (e.g. Emil Sedgh binds Esc to "close tab").
00241     // With a shortcut override we can catch this before it gets to kactions.
00242     if (e->type() == QEvent::ShortcutOverride) {
00243         QKeyEvent* kev = static_cast<QKeyEvent* >(e);
00244         if (kev->key() == Qt::Key_Escape) {
00245             e->accept();
00246             emit hideMe();
00247             return true;
00248         }
00249     }
00250     return KHTMLViewBarWidget::event(e);
00251 }

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
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
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