diff --git a/CMakeLists.txt b/CMakeLists.txt index 4c687d5bd..2f2af6648 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -112,6 +112,7 @@ set(okularpart_SRCS ui/pageviewutils.cpp ui/presentationwidget.cpp ui/propertiesdialog.cpp + ui/searchlineedit.cpp ui/searchwidget.cpp ui/side_reviews.cpp ui/thumbnaillist.cpp diff --git a/ui/searchlineedit.cpp b/ui/searchlineedit.cpp new file mode 100644 index 000000000..f750a83d4 --- /dev/null +++ b/ui/searchlineedit.cpp @@ -0,0 +1,107 @@ +/*************************************************************************** + * Copyright (C) 2004 by Enrico Ros * + * Copyright (C) 2007 by Pino Toscano * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + ***************************************************************************/ + +// qt/kde includes +#include +#include + +// local includes +#include "searchlineedit.h" + +SearchLineEdit::SearchLineEdit( QWidget * parent, Okular::Document * document ) + : KLineEdit( parent ), m_document( document ), m_minLength( 0 ), + m_caseSensitivity( Qt::CaseInsensitive ), + m_searchType( Okular::Document::AllDocument ), m_id( -1 ) +{ + setObjectName( "SearchLineEdit" ); + + // a timer to ensure that we don't flood the document with requests to search + m_inputDelayTimer = new QTimer(this); + m_inputDelayTimer->setSingleShot(true); + connect( m_inputDelayTimer, SIGNAL( timeout() ), + this, SLOT( startSearch() ) ); + + connect(this, SIGNAL( textChanged(const QString &) ), this, SLOT( slotTextChanged(const QString &) )); +} + +void SearchLineEdit::clearText() +{ + clear(); +} + +void SearchLineEdit::setSearchCaseSensitivity( Qt::CaseSensitivity cs ) +{ + m_caseSensitivity = cs; +} + +void SearchLineEdit::setSearchMinimumLength( int length ) +{ + m_minLength = length; +} + +void SearchLineEdit::setSearchType( Okular::Document::SearchType type ) +{ + m_searchType = type; +} + +void SearchLineEdit::setSearchId( int id ) +{ + m_id = id; +} + +void SearchLineEdit::setSearchColor( const QColor &color ) +{ + m_color = color; +} + +void SearchLineEdit::restartSearch() +{ + m_inputDelayTimer->stop(); + m_inputDelayTimer->start( 500 ); +} + +void SearchLineEdit::slotTextChanged( const QString & text ) +{ + QPalette qAppPalette = QApplication::palette(); + // if 0 0 ? Qt::darkRed : qAppPalette.color( QPalette::Text ); + QPalette pal = palette(); + pal.setColor( QPalette::Base, qAppPalette.color( QPalette::Base ) ); + pal.setColor( QPalette::Text, color ); + setPalette( pal ); + restartSearch(); +} + +void SearchLineEdit::startSearch() +{ + if ( m_id == -1 || !m_color.isValid() ) + return; + + // search text if have more than 3 chars or else clear search + QString thistext = text(); + bool ok = true; + if ( thistext.length() >= m_minLength ) + { + ok = m_document->searchText( m_id, thistext, true, m_caseSensitivity, + m_searchType, false, m_color ); + } + else + m_document->resetSearch( m_id ); + // if not found, use warning colors + if ( !ok ) + { + QPalette pal = palette(); + pal.setColor( QPalette::Base, Qt::red ); + pal.setColor( QPalette::Text, Qt::white ); + setPalette( pal ); + } +} + +#include "searchlineedit.moc" diff --git a/ui/searchlineedit.h b/ui/searchlineedit.h new file mode 100644 index 000000000..310d6b485 --- /dev/null +++ b/ui/searchlineedit.h @@ -0,0 +1,53 @@ +/*************************************************************************** + * Copyright (C) 2004 by Enrico Ros * + * Copyright (C) 2007 by Pino Toscano * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + ***************************************************************************/ + +#ifndef _OKULAR_SEARCHLINEEDIT_H_ +#define _OKULAR_SEARCHLINEEDIT_H_ + +#include "core/document.h" + +#include + +class QTimer; + +/** + * @short A line edit for find-as-you-type search. Outputs to the Document. + */ +class SearchLineEdit : public KLineEdit +{ + Q_OBJECT + public: + SearchLineEdit( QWidget *parent, Okular::Document *document ); + + void clearText(); + + void setSearchCaseSensitivity( Qt::CaseSensitivity cs ); + void setSearchMinimumLength( int length ); + void setSearchType( Okular::Document::SearchType type ); + void setSearchId( int id ); + void setSearchColor( const QColor &color ); + + void restartSearch(); + + private: + Okular::Document * m_document; + QTimer * m_inputDelayTimer; + int m_minLength; + Qt::CaseSensitivity m_caseSensitivity; + Okular::Document::SearchType m_searchType; + int m_id; + QColor m_color; + + private slots: + void slotTextChanged( const QString & text ); + void startSearch(); +}; + +#endif diff --git a/ui/searchwidget.cpp b/ui/searchwidget.cpp index b162e8ce2..ec78c782e 100644 --- a/ui/searchwidget.cpp +++ b/ui/searchwidget.cpp @@ -10,23 +10,17 @@ // qt/kde includes #include #include -#include #include -#include #include #include #include -#include -#include // local includes #include "searchwidget.h" -#include "core/document.h" -#include "settings.h" +#include "searchlineedit.h" SearchWidget::SearchWidget( QWidget * parent, Okular::Document * document ) - : QToolBar( parent ), m_document( document ), - m_searchType( 0 ) + : QToolBar( parent ) { setObjectName( "iSearchBar" ); // change toolbar appearance @@ -36,17 +30,15 @@ SearchWidget::SearchWidget( QWidget * parent, Okular::Document * document ) sp.setVerticalPolicy( QSizePolicy::Minimum ); setSizePolicy( sp ); - // a timer to ensure that we don't flood the document with requests to search - m_inputDelayTimer = new QTimer(this); - m_inputDelayTimer->setSingleShot(true); - connect( m_inputDelayTimer, SIGNAL( timeout() ), - this, SLOT( startSearch() ) ); - // 2. text line - m_lineEdit = new KLineEdit(this); + m_lineEdit = new SearchLineEdit( this, document ); m_lineEdit->setClearButtonShown( true ); m_lineEdit->setToolTip(i18n( "Enter at least 3 letters to filter pages" )); - connect(m_lineEdit, SIGNAL( textChanged(const QString &) ), this, SLOT( slotTextChanged(const QString &) )); + m_lineEdit->setSearchCaseSensitivity( Qt::CaseInsensitive ); + m_lineEdit->setSearchMinimumLength( 3 ); + m_lineEdit->setSearchType( Okular::Document::GoogleAll ); + m_lineEdit->setSearchId( SW_SEARCH_ID ); + m_lineEdit->setSearchColor( qRgb( 0, 183, 255 ) ); addWidget(m_lineEdit); // 3.1. create the popup menu for changing filtering features @@ -83,71 +75,30 @@ void SearchWidget::clearText() m_lineEdit->clear(); } -void SearchWidget::slotTextChanged( const QString & text ) -{ - QPalette qAppPalette = QApplication::palette(); - // if 0 0 ? Qt::darkRed : qAppPalette.color( QPalette::Text ); - QPalette pal = m_lineEdit->palette(); - pal.setColor( QPalette::Base, qAppPalette.color( QPalette::Base ) ); - pal.setColor( QPalette::Text, color ); - m_lineEdit->setPalette( pal ); - m_inputDelayTimer->stop(); - m_inputDelayTimer->start(333); -} - void SearchWidget::slotMenuChaged( QAction * act ) { // update internal variables and checked state if ( act == m_caseSensitiveAction ) { - // do nothing, just update the search + m_lineEdit->setSearchCaseSensitivity( m_caseSensitiveAction->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive ); } else if ( act == m_matchPhraseAction ) { - m_searchType = 0; + m_lineEdit->setSearchType( Okular::Document::AllDocument ); } else if ( act == m_marchAllWordsAction ) { - m_searchType = 1; + m_lineEdit->setSearchType( Okular::Document::GoogleAll ); } else if ( act == m_marchAnyWordsAction ) { - m_searchType = 2; + m_lineEdit->setSearchType( Okular::Document::GoogleAny ); } else return; // update search - slotTextChanged( m_lineEdit->text() ); -} - -void SearchWidget::startSearch() -{ - // search text if have more than 3 chars or else clear search - QString text = m_lineEdit->text(); - bool ok = true; - if ( text.length() >= 3 ) - { - Qt::CaseSensitivity caseSensitivity = m_caseSensitiveAction->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive; - - Okular::Document::SearchType type = !m_searchType ? Okular::Document::AllDocument : - ( (m_searchType > 1) ? Okular::Document::GoogleAny : - Okular::Document::GoogleAll ); - - ok = m_document->searchText( SW_SEARCH_ID, text, true, caseSensitivity, - type, false, qRgb( 0, 183, 255 ) ); - } - else - m_document->resetSearch( SW_SEARCH_ID ); - // if not found, use warning colors - if ( !ok ) - { - QPalette pal = m_lineEdit->palette(); - pal.setColor( QPalette::Base, Qt::red ); - pal.setColor( QPalette::Text, Qt::white ); - m_lineEdit->setPalette( pal ); - } + m_lineEdit->restartSearch(); } #include "searchwidget.moc" diff --git a/ui/searchwidget.h b/ui/searchwidget.h index afd7f166b..2b7f463e7 100644 --- a/ui/searchwidget.h +++ b/ui/searchwidget.h @@ -16,9 +16,8 @@ namespace Okular { class Document; } -class KLineEdit; class QAction; -class QTimer; +class SearchLineEdit; /** * @short A widget for find-as-you-type search. Outputs to the Document. @@ -36,17 +35,12 @@ class SearchWidget : public QToolBar void clearText(); private: - Okular::Document * m_document; QMenu * m_menu; - QTimer * m_inputDelayTimer; - int m_searchType; QAction *m_matchPhraseAction, *m_caseSensitiveAction, * m_marchAllWordsAction, *m_marchAnyWordsAction; - KLineEdit *m_lineEdit; + SearchLineEdit *m_lineEdit; private slots: - void slotTextChanged( const QString & text ); void slotMenuChaged( QAction * ); - void startSearch(); }; #endif