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