compile
[kdegraphics.git] / okular / ui / searchlineedit.cpp
blob52ac8c62068302e872e24215d44b1d35ba1c7bcc
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 "core/document.h"
16 // qt/kde includes
17 #include <qapplication.h>
18 #include <qtimer.h>
19 #include <kcolorscheme.h>
21 SearchLineEdit::SearchLineEdit( QWidget * parent, Okular::Document * document )
22 : KLineEdit( parent ), m_document( document ), m_minLength( 0 ),
23 m_caseSensitivity( Qt::CaseInsensitive ),
24 m_searchType( Okular::Document::AllDocument ), m_id( -1 ),
25 m_moveViewport( false ), m_changed( false ), m_fromStart( true )
27 setObjectName( "SearchLineEdit" );
28 setClearButtonShown( true );
30 // a timer to ensure that we don't flood the document with requests to search
31 m_inputDelayTimer = new QTimer(this);
32 m_inputDelayTimer->setSingleShot(true);
33 connect( m_inputDelayTimer, SIGNAL( timeout() ),
34 this, SLOT( startSearch() ) );
36 connect(this, SIGNAL( textChanged(const QString &) ), this, SLOT( slotTextChanged(const QString &) ));
37 connect(document, SIGNAL( searchFinished(int, Okular::Document::SearchStatus) ), this, SLOT( searchFinished(int, Okular::Document::SearchStatus) ));
40 void SearchLineEdit::clearText()
42 clear();
45 void SearchLineEdit::setSearchCaseSensitivity( Qt::CaseSensitivity cs )
47 m_caseSensitivity = cs;
48 m_changed = true;
51 void SearchLineEdit::setSearchMinimumLength( int length )
53 m_minLength = length;
54 m_changed = true;
57 void SearchLineEdit::setSearchType( Okular::Document::SearchType type )
59 if ( type == m_searchType )
60 return;
62 m_searchType = type;
63 m_changed = ( m_searchType != Okular::Document::NextMatch && m_searchType != Okular::Document::PreviousMatch );
66 void SearchLineEdit::setSearchId( int id )
68 m_id = id;
69 m_changed = true;
72 void SearchLineEdit::setSearchColor( const QColor &color )
74 m_color = color;
75 m_changed = true;
78 void SearchLineEdit::setSearchMoveViewport( bool move )
80 m_moveViewport = move;
83 void SearchLineEdit::setSearchFromStart( bool fromStart )
85 m_fromStart = fromStart;
88 void SearchLineEdit::restartSearch()
90 m_inputDelayTimer->stop();
91 m_inputDelayTimer->start( 500 );
92 m_changed = true;
95 void SearchLineEdit::findNext()
97 if ( m_id == -1 || m_searchType != Okular::Document::NextMatch )
98 return;
100 if ( !m_changed )
101 m_document->continueSearch( m_id, m_searchType );
102 else
103 startSearch();
106 void SearchLineEdit::findPrev()
108 if ( m_id == -1 || m_searchType != Okular::Document::PreviousMatch )
109 return;
111 if ( !m_changed )
112 m_document->continueSearch( m_id, m_searchType );
113 else
114 startSearch();
117 void SearchLineEdit::slotTextChanged( const QString & text )
119 QPalette pal = palette();
120 const int textLength = text.length();
121 if ( textLength > 0 && textLength < m_minLength )
123 const KColorScheme scheme( QPalette::Active, KColorScheme::View );
124 pal.setBrush( QPalette::Base, scheme.background( KColorScheme::NegativeBackground ) );
125 pal.setBrush( QPalette::Text, scheme.foreground( KColorScheme::NegativeText ) );
127 else
129 const QPalette qAppPalette = QApplication::palette();
130 pal.setColor( QPalette::Base, qAppPalette.color( QPalette::Base ) );
131 pal.setColor( QPalette::Text, qAppPalette.color( QPalette::Text ) );
133 setPalette( pal );
134 restartSearch();
137 void SearchLineEdit::startSearch()
139 if ( m_id == -1 || !m_color.isValid() )
140 return;
142 if ( m_changed && ( m_searchType == Okular::Document::NextMatch || m_searchType == Okular::Document::PreviousMatch ) )
144 m_document->resetSearch( m_id );
146 m_changed = false;
147 // search text if have more than 3 chars or else clear search
148 QString thistext = text();
149 if ( thistext.length() >= qMax( m_minLength, 1 ) )
151 m_document->searchText( m_id, thistext, m_fromStart, m_caseSensitivity,
152 m_searchType, m_moveViewport, m_color );
154 else
155 m_document->resetSearch( m_id );
158 void SearchLineEdit::searchFinished( int id, Okular::Document::SearchStatus endStatus )
160 // ignore the searches not started by this search edit
161 if ( id != m_id )
162 return;
164 // if not found, use warning colors
165 if ( endStatus == Okular::Document::NoMatchFound )
167 QPalette pal = palette();
168 const KColorScheme scheme( QPalette::Active, KColorScheme::View );
169 pal.setBrush( QPalette::Base, scheme.background( KColorScheme::NegativeBackground ) );
170 pal.setBrush( QPalette::Text, scheme.foreground( KColorScheme::NegativeText ) );
171 setPalette( pal );
175 #include "searchlineedit.moc"