2021-05-24 07:25:56 +00:00
/*
SPDX - FileCopyrightText : 2007 Pino Toscano < pino @ kde . org >
SPDX - License - Identifier : GPL - 2.0 - or - later
*/
2007-03-17 22:58:41 +00:00
2007-04-19 18:30:20 +00:00
# include "findbar.h"
2007-03-17 22:58:41 +00:00
// qt/kde includes
2014-10-19 13:20:30 +00:00
# include <KLocalizedString>
2020-07-08 11:54:37 +00:00
# include <QEvent>
2014-10-06 06:31:17 +00:00
# include <QIcon>
2020-07-08 11:54:37 +00:00
# include <QKeyEvent>
# include <QLabel>
# include <QLayout>
# include <QMenu>
# include <QPushButton>
# include <QToolButton>
2007-03-17 22:58:41 +00:00
// local includes
# include "core/document.h"
# include "searchlineedit.h"
2009-05-08 16:26:02 +00:00
# include "settings.h"
2007-03-17 22:58:41 +00:00
FindBar : : FindBar ( Okular : : Document * document , QWidget * parent )
: QWidget ( parent )
2009-05-08 16:26:02 +00:00
, m_active ( false )
2007-03-17 22:58:41 +00:00
{
QHBoxLayout * lay = new QHBoxLayout ( this ) ;
2019-09-18 11:35:04 +00:00
lay - > setContentsMargins ( 2 , 2 , 2 , 2 ) ;
2020-07-10 22:15:05 +00:00
2007-03-17 22:58:41 +00:00
QToolButton * closeBtn = new QToolButton ( this ) ;
2015-10-29 12:37:11 +00:00
closeBtn - > setIcon ( QIcon : : fromTheme ( QStringLiteral ( " dialog-close " ) ) ) ;
2007-03-17 22:58:41 +00:00
closeBtn - > setToolTip ( i18n ( " Close " ) ) ;
closeBtn - > setAutoRaise ( true ) ;
lay - > addWidget ( closeBtn ) ;
2020-07-10 22:15:05 +00:00
2007-09-15 15:10:59 +00:00
QLabel * label = new QLabel ( i18nc ( " Find text " , " F&ind: " ) , this ) ;
2007-08-23 09:58:43 +00:00
lay - > addWidget ( label ) ;
2020-07-10 22:15:05 +00:00
2009-02-15 00:36:35 +00:00
m_search = new SearchLineWidget ( this , document ) ;
m_search - > lineEdit ( ) - > setSearchCaseSensitivity ( Qt : : CaseInsensitive ) ;
m_search - > lineEdit ( ) - > setSearchMinimumLength ( 0 ) ;
m_search - > lineEdit ( ) - > setSearchType ( Okular : : Document : : NextMatch ) ;
m_search - > lineEdit ( ) - > setSearchId ( PART_SEARCH_ID ) ;
m_search - > lineEdit ( ) - > setSearchColor ( qRgb ( 255 , 255 , 64 ) ) ;
m_search - > lineEdit ( ) - > setSearchMoveViewport ( true ) ;
2017-03-13 23:31:42 +00:00
m_search - > lineEdit ( ) - > setFindAsYouType ( false ) ;
2009-02-15 00:36:35 +00:00
m_search - > lineEdit ( ) - > setToolTip ( i18n ( " Text to search for " ) ) ;
2011-06-08 05:47:57 +00:00
m_search - > installEventFilter ( this ) ;
2012-01-22 18:01:56 +00:00
label - > setBuddy ( m_search - > lineEdit ( ) ) ;
2009-02-15 00:36:35 +00:00
lay - > addWidget ( m_search ) ;
2020-07-10 22:15:05 +00:00
2015-10-29 12:37:11 +00:00
QPushButton * findNextBtn = new QPushButton ( QIcon : : fromTheme ( QStringLiteral ( " go-down-search " ) ) , i18nc ( " Find and go to the next search match " , " Next " ) , this ) ;
2007-09-15 15:10:59 +00:00
findNextBtn - > setToolTip ( i18n ( " Jump to next match " ) ) ;
2007-03-17 22:58:41 +00:00
lay - > addWidget ( findNextBtn ) ;
2020-07-10 22:15:05 +00:00
2015-10-29 12:37:11 +00:00
QPushButton * findPrevBtn = new QPushButton ( QIcon : : fromTheme ( QStringLiteral ( " go-up-search " ) ) , i18nc ( " Find and go to the previous search match " , " Previous " ) , this ) ;
2008-03-14 14:19:25 +00:00
findPrevBtn - > setToolTip ( i18n ( " Jump to previous match " ) ) ;
lay - > addWidget ( findPrevBtn ) ;
2020-07-10 22:15:05 +00:00
2007-03-17 22:58:41 +00:00
QPushButton * optionsBtn = new QPushButton ( this ) ;
optionsBtn - > setText ( i18n ( " Options " ) ) ;
2008-04-11 21:16:43 +00:00
optionsBtn - > setToolTip ( i18n ( " Modify search behavior " ) ) ;
2007-03-24 01:13:47 +00:00
QMenu * optionsMenu = new QMenu ( optionsBtn ) ;
2007-03-17 22:58:41 +00:00
m_caseSensitiveAct = optionsMenu - > addAction ( i18n ( " Case sensitive " ) ) ;
m_caseSensitiveAct - > setCheckable ( true ) ;
2008-08-12 14:10:49 +00:00
m_fromCurrentPageAct = optionsMenu - > addAction ( i18n ( " From current page " ) ) ;
m_fromCurrentPageAct - > setCheckable ( true ) ;
2017-03-13 23:31:42 +00:00
m_findAsYouTypeAct = optionsMenu - > addAction ( i18n ( " Find as you type " ) ) ;
m_findAsYouTypeAct - > setCheckable ( true ) ;
2007-03-17 22:58:41 +00:00
optionsBtn - > setMenu ( optionsMenu ) ;
lay - > addWidget ( optionsBtn ) ;
2020-07-10 22:15:05 +00:00
2015-10-29 12:37:11 +00:00
connect ( closeBtn , & QAbstractButton : : clicked , this , & FindBar : : closeAndStopSearch ) ;
connect ( findNextBtn , & QAbstractButton : : clicked , this , & FindBar : : findNext ) ;
connect ( findPrevBtn , & QAbstractButton : : clicked , this , & FindBar : : findPrev ) ;
connect ( m_caseSensitiveAct , & QAction : : toggled , this , & FindBar : : caseSensitivityChanged ) ;
connect ( m_fromCurrentPageAct , & QAction : : toggled , this , & FindBar : : fromCurrentPageChanged ) ;
2017-03-13 23:31:42 +00:00
connect ( m_findAsYouTypeAct , & QAction : : toggled , this , & FindBar : : findAsYouTypeChanged ) ;
2020-07-10 22:15:05 +00:00
2009-05-08 16:26:02 +00:00
m_caseSensitiveAct - > setChecked ( Okular : : Settings : : searchCaseSensitive ( ) ) ;
m_fromCurrentPageAct - > setChecked ( Okular : : Settings : : searchFromCurrentPage ( ) ) ;
2017-03-13 23:31:42 +00:00
m_findAsYouTypeAct - > setChecked ( Okular : : Settings : : findAsYouType ( ) ) ;
2009-05-08 16:26:02 +00:00
2007-03-17 22:58:41 +00:00
hide ( ) ;
2009-05-08 16:26:02 +00:00
// "activate" it only at th very end
m_active = true ;
2007-03-17 22:58:41 +00:00
}
FindBar : : ~ FindBar ( )
{
}
2011-06-08 05:47:57 +00:00
bool FindBar : : eventFilter ( QObject * target , QEvent * event )
{
if ( target = = m_search ) {
if ( event - > type ( ) = = QEvent : : KeyPress ) {
QKeyEvent * keyEvent = static_cast < QKeyEvent * > ( event ) ;
if ( keyEvent - > key ( ) = = Qt : : Key_PageUp | | keyEvent - > key ( ) = = Qt : : Key_PageDown ) {
2022-03-18 21:35:45 +00:00
Q_EMIT forwardKeyPressEvent ( keyEvent ) ;
2011-06-08 05:47:57 +00:00
return true ;
}
}
}
return false ;
}
2007-03-17 22:58:41 +00:00
QString FindBar : : text ( ) const
{
2009-02-15 00:36:35 +00:00
return m_search - > lineEdit ( ) - > text ( ) ;
2007-03-17 22:58:41 +00:00
}
Qt : : CaseSensitivity FindBar : : caseSensitivity ( ) const
{
return m_caseSensitiveAct - > isChecked ( ) ? Qt : : CaseSensitive : Qt : : CaseInsensitive ;
}
void FindBar : : focusAndSetCursor ( )
{
setFocus ( ) ;
2009-02-15 00:36:35 +00:00
m_search - > lineEdit ( ) - > selectAll ( ) ;
m_search - > lineEdit ( ) - > setFocus ( ) ;
2007-03-17 22:58:41 +00:00
}
2009-05-08 23:19:37 +00:00
bool FindBar : : maybeHide ( )
{
if ( m_search - > lineEdit ( ) - > isSearchRunning ( ) ) {
m_search - > lineEdit ( ) - > stopSearch ( ) ;
return false ;
} else {
hide ( ) ;
return true ;
}
}
2007-03-17 22:58:41 +00:00
void FindBar : : findNext ( )
{
2009-02-15 00:36:35 +00:00
m_search - > lineEdit ( ) - > setSearchType ( Okular : : Document : : NextMatch ) ;
m_search - > lineEdit ( ) - > findNext ( ) ;
2007-03-17 22:58:41 +00:00
}
2008-03-14 14:19:25 +00:00
void FindBar : : findPrev ( )
{
2009-02-15 00:36:35 +00:00
m_search - > lineEdit ( ) - > setSearchType ( Okular : : Document : : PreviousMatch ) ;
m_search - > lineEdit ( ) - > findPrev ( ) ;
2008-03-14 14:19:25 +00:00
}
2012-07-12 18:46:24 +00:00
void FindBar : : resetSearch ( )
{
m_search - > lineEdit ( ) - > resetSearch ( ) ;
}
2007-03-17 22:58:41 +00:00
void FindBar : : caseSensitivityChanged ( )
{
2009-02-15 00:36:35 +00:00
m_search - > lineEdit ( ) - > setSearchCaseSensitivity ( m_caseSensitiveAct - > isChecked ( ) ? Qt : : CaseSensitive : Qt : : CaseInsensitive ) ;
2009-05-08 16:26:02 +00:00
if ( ! m_active ) {
return ;
2022-03-08 10:10:43 +00:00
}
2009-05-08 16:26:02 +00:00
Okular : : Settings : : setSearchCaseSensitive ( m_caseSensitiveAct - > isChecked ( ) ) ;
2014-10-01 05:27:09 +00:00
Okular : : Settings : : self ( ) - > save ( ) ;
2009-02-15 00:36:35 +00:00
m_search - > lineEdit ( ) - > restartSearch ( ) ;
2007-03-17 22:58:41 +00:00
}
2008-08-12 14:10:49 +00:00
void FindBar : : fromCurrentPageChanged ( )
{
2009-02-15 00:36:35 +00:00
m_search - > lineEdit ( ) - > setSearchFromStart ( ! m_fromCurrentPageAct - > isChecked ( ) ) ;
2009-05-08 16:26:02 +00:00
if ( ! m_active ) {
return ;
2022-03-08 10:10:43 +00:00
}
2009-05-08 16:26:02 +00:00
Okular : : Settings : : setSearchFromCurrentPage ( m_fromCurrentPageAct - > isChecked ( ) ) ;
2014-10-01 05:27:09 +00:00
Okular : : Settings : : self ( ) - > save ( ) ;
2008-08-12 14:10:49 +00:00
}
2017-03-13 23:31:42 +00:00
void FindBar : : findAsYouTypeChanged ( )
{
m_search - > lineEdit ( ) - > setFindAsYouType ( m_findAsYouTypeAct - > isChecked ( ) ) ;
if ( ! m_active ) {
return ;
2022-03-08 10:10:43 +00:00
}
2017-03-13 23:31:42 +00:00
Okular : : Settings : : setFindAsYouType ( m_findAsYouTypeAct - > isChecked ( ) ) ;
Okular : : Settings : : self ( ) - > save ( ) ;
}
2009-11-05 23:21:56 +00:00
void FindBar : : closeAndStopSearch ( )
{
if ( m_search - > lineEdit ( ) - > isSearchRunning ( ) ) {
m_search - > lineEdit ( ) - > stopSearch ( ) ;
}
2022-03-18 21:35:45 +00:00
Q_EMIT onCloseButtonPressed ( ) ;
2009-11-05 23:21:56 +00:00
close ( ) ;
}
2019-02-13 21:55:02 +00:00
void FindBar : : startSearch ( const QString & findText )
{
m_search - > lineEdit ( ) - > setText ( findText ) ;
show ( ) ;
}
2014-08-08 22:00:07 +00:00
# include "moc_findbar.cpp"