1 /***************************************************************************
2 * Copyright (C) 2004 by Enrico Ros <eros.kde@email.it> *
3 * Copyright (C) 2007 by Pino Toscano <pino@kde.org> *
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"
14 #include "core/document.h"
17 #include <qapplication.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()
45 void SearchLineEdit::setSearchCaseSensitivity( Qt::CaseSensitivity cs
)
47 m_caseSensitivity
= cs
;
51 void SearchLineEdit::setSearchMinimumLength( int length
)
57 void SearchLineEdit::setSearchType( Okular::Document::SearchType type
)
59 if ( type
== m_searchType
)
63 m_changed
= ( m_searchType
!= Okular::Document::NextMatch
&& m_searchType
!= Okular::Document::PreviousMatch
);
66 void SearchLineEdit::setSearchId( int id
)
72 void SearchLineEdit::setSearchColor( const QColor
&color
)
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 );
95 void SearchLineEdit::findNext()
97 if ( m_id
== -1 || m_searchType
!= Okular::Document::NextMatch
)
101 m_document
->continueSearch( m_id
, m_searchType
);
106 void SearchLineEdit::findPrev()
108 if ( m_id
== -1 || m_searchType
!= Okular::Document::PreviousMatch
)
112 m_document
->continueSearch( m_id
, m_searchType
);
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
) );
129 const QPalette qAppPalette
= QApplication::palette();
130 pal
.setColor( QPalette::Base
, qAppPalette
.color( QPalette::Base
) );
131 pal
.setColor( QPalette::Text
, qAppPalette
.color( QPalette::Text
) );
137 void SearchLineEdit::startSearch()
139 if ( m_id
== -1 || !m_color
.isValid() )
142 if ( m_changed
&& ( m_searchType
== Okular::Document::NextMatch
|| m_searchType
== Okular::Document::PreviousMatch
) )
144 m_document
->resetSearch( m_id
);
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
);
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
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
) );
175 #include "searchlineedit.moc"