compile
[kdegraphics.git] / okular / ui / searchlineedit.cpp
blobe058b487afee78703ebaa1caa5af0e94a72743e2
1 /***************************************************************************
2 * Copyright (C) 2004 by Enrico Ros <eros.kde@email.it> *
3 * Copyright (C) 2007 by Pino Toscano <pino@kde.org> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 ***************************************************************************/
11 #include "searchlineedit.h"
13 // local includes
14 #include "animatedwidget.h"
15 #include "core/document.h"
17 // qt/kde includes
18 #include <qapplication.h>
19 #include <qlayout.h>
20 #include <qtimer.h>
21 #include <kcolorscheme.h>
23 SearchLineEdit::SearchLineEdit( QWidget * parent, Okular::Document * document )
24 : KLineEdit( parent ), m_document( document ), m_minLength( 0 ),
25 m_caseSensitivity( Qt::CaseInsensitive ),
26 m_searchType( Okular::Document::AllDocument ), m_id( -1 ),
27 m_moveViewport( false ), m_changed( false ), m_fromStart( true )
29 setObjectName( "SearchLineEdit" );
30 setClearButtonShown( true );
32 // a timer to ensure that we don't flood the document with requests to search
33 m_inputDelayTimer = new QTimer(this);
34 m_inputDelayTimer->setSingleShot(true);
35 connect( m_inputDelayTimer, SIGNAL( timeout() ),
36 this, SLOT( startSearch() ) );
38 connect(this, SIGNAL( textChanged(const QString &) ), this, SLOT( slotTextChanged(const QString &) ));
39 connect(document, SIGNAL( searchFinished(int, Okular::Document::SearchStatus) ), this, SLOT( searchFinished(int, Okular::Document::SearchStatus) ));
42 void SearchLineEdit::clearText()
44 clear();
47 void SearchLineEdit::setSearchCaseSensitivity( Qt::CaseSensitivity cs )
49 m_caseSensitivity = cs;
50 m_changed = true;
53 void SearchLineEdit::setSearchMinimumLength( int length )
55 m_minLength = length;
56 m_changed = true;
59 void SearchLineEdit::setSearchType( Okular::Document::SearchType type )
61 if ( type == m_searchType )
62 return;
64 m_searchType = type;
65 m_changed = ( m_searchType != Okular::Document::NextMatch && m_searchType != Okular::Document::PreviousMatch );
68 void SearchLineEdit::setSearchId( int id )
70 m_id = id;
71 m_changed = true;
74 void SearchLineEdit::setSearchColor( const QColor &color )
76 m_color = color;
77 m_changed = true;
80 void SearchLineEdit::setSearchMoveViewport( bool move )
82 m_moveViewport = move;
85 void SearchLineEdit::setSearchFromStart( bool fromStart )
87 m_fromStart = fromStart;
90 void SearchLineEdit::restartSearch()
92 m_inputDelayTimer->stop();
93 m_inputDelayTimer->start( 500 );
94 m_changed = true;
97 void SearchLineEdit::findNext()
99 if ( m_id == -1 || m_searchType != Okular::Document::NextMatch )
100 return;
102 if ( !m_changed )
104 emit searchStarted();
105 m_document->continueSearch( m_id, m_searchType );
107 else
108 startSearch();
111 void SearchLineEdit::findPrev()
113 if ( m_id == -1 || m_searchType != Okular::Document::PreviousMatch )
114 return;
116 if ( !m_changed )
118 emit searchStarted();
119 m_document->continueSearch( m_id, m_searchType );
121 else
122 startSearch();
125 void SearchLineEdit::slotTextChanged( const QString & text )
127 QPalette pal = palette();
128 const int textLength = text.length();
129 if ( textLength > 0 && textLength < m_minLength )
131 const KColorScheme scheme( QPalette::Active, KColorScheme::View );
132 pal.setBrush( QPalette::Base, scheme.background( KColorScheme::NegativeBackground ) );
133 pal.setBrush( QPalette::Text, scheme.foreground( KColorScheme::NegativeText ) );
135 else
137 const QPalette qAppPalette = QApplication::palette();
138 pal.setColor( QPalette::Base, qAppPalette.color( QPalette::Base ) );
139 pal.setColor( QPalette::Text, qAppPalette.color( QPalette::Text ) );
141 setPalette( pal );
142 restartSearch();
145 void SearchLineEdit::startSearch()
147 if ( m_id == -1 || !m_color.isValid() )
148 return;
150 if ( m_changed && ( m_searchType == Okular::Document::NextMatch || m_searchType == Okular::Document::PreviousMatch ) )
152 m_document->resetSearch( m_id );
154 m_changed = false;
155 // search text if have more than 3 chars or else clear search
156 QString thistext = text();
157 if ( thistext.length() >= qMax( m_minLength, 1 ) )
159 emit searchStarted();
160 m_document->searchText( m_id, thistext, m_fromStart, m_caseSensitivity,
161 m_searchType, m_moveViewport, m_color );
163 else
164 m_document->resetSearch( m_id );
167 void SearchLineEdit::searchFinished( int id, Okular::Document::SearchStatus endStatus )
169 // ignore the searches not started by this search edit
170 if ( id != m_id )
171 return;
173 // if not found, use warning colors
174 if ( endStatus == Okular::Document::NoMatchFound )
176 QPalette pal = palette();
177 const KColorScheme scheme( QPalette::Active, KColorScheme::View );
178 pal.setBrush( QPalette::Base, scheme.background( KColorScheme::NegativeBackground ) );
179 pal.setBrush( QPalette::Text, scheme.foreground( KColorScheme::NegativeText ) );
180 setPalette( pal );
182 else
184 QPalette pal = palette();
185 const QPalette qAppPalette = QApplication::palette();
186 pal.setColor( QPalette::Base, qAppPalette.color( QPalette::Base ) );
187 pal.setColor( QPalette::Text, qAppPalette.color( QPalette::Text ) );
188 setPalette( pal );
191 emit searchStopped();
195 SearchLineWidget::SearchLineWidget( QWidget * parent, Okular::Document * document )
196 : QWidget( parent )
198 QHBoxLayout *layout = new QHBoxLayout( this );
199 layout->setMargin( 0 );
201 m_edit = new SearchLineEdit( this, document );
202 layout->addWidget( m_edit );
204 m_anim = new AnimatedWidget( "process-working", this );
205 m_anim->setFixedSize( 22, 22 );
206 layout->addWidget( m_anim );
208 m_timer = new QTimer( this );
209 m_timer->setSingleShot( true );
210 connect( m_timer, SIGNAL( timeout() ), m_anim, SLOT( start() ) );
212 connect( m_edit, SIGNAL( searchStarted() ), this, SLOT( slotSearchStarted() ) );
213 connect( m_edit, SIGNAL( searchStopped() ), this, SLOT( slotSearchStopped() ) );
216 SearchLineEdit* SearchLineWidget::lineEdit() const
218 return m_edit;
221 void SearchLineWidget::slotSearchStarted()
223 m_timer->start( 100 );
226 void SearchLineWidget::slotSearchStopped()
228 m_timer->stop();
229 m_anim->stop();
232 #include "searchlineedit.moc"