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 "animatedwidget.h"
15 #include "core/document.h"
18 #include <qapplication.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()
47 void SearchLineEdit::setSearchCaseSensitivity( Qt::CaseSensitivity cs
)
49 m_caseSensitivity
= cs
;
53 void SearchLineEdit::setSearchMinimumLength( int length
)
59 void SearchLineEdit::setSearchType( Okular::Document::SearchType type
)
61 if ( type
== m_searchType
)
65 m_changed
= ( m_searchType
!= Okular::Document::NextMatch
&& m_searchType
!= Okular::Document::PreviousMatch
);
68 void SearchLineEdit::setSearchId( int id
)
74 void SearchLineEdit::setSearchColor( const QColor
&color
)
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 );
97 void SearchLineEdit::findNext()
99 if ( m_id
== -1 || m_searchType
!= Okular::Document::NextMatch
)
104 emit
searchStarted();
105 m_document
->continueSearch( m_id
, m_searchType
);
111 void SearchLineEdit::findPrev()
113 if ( m_id
== -1 || m_searchType
!= Okular::Document::PreviousMatch
)
118 emit
searchStarted();
119 m_document
->continueSearch( m_id
, m_searchType
);
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
) );
137 const QPalette qAppPalette
= QApplication::palette();
138 pal
.setColor( QPalette::Base
, qAppPalette
.color( QPalette::Base
) );
139 pal
.setColor( QPalette::Text
, qAppPalette
.color( QPalette::Text
) );
145 void SearchLineEdit::startSearch()
147 if ( m_id
== -1 || !m_color
.isValid() )
150 if ( m_changed
&& ( m_searchType
== Okular::Document::NextMatch
|| m_searchType
== Okular::Document::PreviousMatch
) )
152 m_document
->resetSearch( m_id
);
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
);
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
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
) );
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
) );
191 emit
searchStopped();
195 SearchLineWidget::SearchLineWidget( QWidget
* parent
, Okular::Document
* document
)
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
221 void SearchLineWidget::slotSearchStarted()
223 m_timer
->start( 100 );
226 void SearchLineWidget::slotSearchStopped()
232 #include "searchlineedit.moc"