isolate the search search bar in an own class (child of KLineEdit), so it can be used with no copy-paste job

svn path=/trunk/playground/graphics/okular/; revision=643508
This commit is contained in:
Pino Toscano 2007-03-17 16:58:01 +00:00
parent c363cad735
commit 0ac881f1f3
5 changed files with 176 additions and 70 deletions

View file

@ -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

107
ui/searchlineedit.cpp Normal file
View file

@ -0,0 +1,107 @@
/***************************************************************************
* Copyright (C) 2004 by Enrico Ros <eros.kde@email.it> *
* Copyright (C) 2007 by Pino Toscano <pino@kde.org> *
* *
* 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 <qapplication.h>
#include <qtimer.h>
// 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<length<minLength set 'red' text and send a blank string to document
QColor color = text.length() < m_minLength && text.length() > 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"

53
ui/searchlineedit.h Normal file
View file

@ -0,0 +1,53 @@
/***************************************************************************
* Copyright (C) 2004 by Enrico Ros <eros.kde@email.it> *
* Copyright (C) 2007 by Pino Toscano <pino@kde.org> *
* *
* 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 <klineedit.h>
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

View file

@ -10,23 +10,17 @@
// qt/kde includes
#include <qmenu.h>
#include <qaction.h>
#include <qapplication.h>
#include <qsizepolicy.h>
#include <qtimer.h>
#include <qtoolbutton.h>
#include <kicon.h>
#include <klocale.h>
#include <kiconloader.h>
#include <klineedit.h>
// 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<length<3 set 'red' text and send a blank string to document
QColor color = text.length() < 3 && text.length() > 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"

View file

@ -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