KHTML
khtmlfindbar.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
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
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 ;
00099 }
00100
00101 QString KHTMLFindBar::pattern() const
00102 {
00103 return m_find->currentText();
00104 }
00105
00106 void KHTMLFindBar::slotSearchChanged()
00107 {
00108
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
00135
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
00160
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
00172 m_fromCursor->setEnabled( !selec && (m_enabled & KFind::FromCursor) );
00173 if ( selec )
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
00239
00240
00241
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 }