1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
/***************************************************************************
* Copyright (C) 2007 by Pino Toscano <pino@kde.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "findbar.h"
// qt/kde includes
#include <qlabel.h>
#include <qlayout.h>
#include <qmenu.h>
#include <qtoolbutton.h>
#include <qevent.h>
#include <kicon.h>
#include <klocale.h>
#include <kpushbutton.h>
// local includes
#include "searchlineedit.h"
#include "core/document.h"
#include "settings.h"
FindBar::FindBar( Okular::Document * document, QWidget * parent )
: QWidget( parent )
, m_active( false )
{
QHBoxLayout * lay = new QHBoxLayout( this );
lay->setMargin( 2 );
QToolButton * closeBtn = new QToolButton( this );
closeBtn->setIcon( KIcon( "dialog-close" ) );
closeBtn->setToolTip( i18n( "Close" ) );
closeBtn->setAutoRaise( true );
lay->addWidget( closeBtn );
QLabel * label = new QLabel( i18nc( "Find text", "F&ind:" ), this );
lay->addWidget( label );
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 );
m_search->lineEdit()->setToolTip( i18n( "Text to search for" ) );
m_search->installEventFilter( this );
label->setBuddy( m_search->lineEdit() );
lay->addWidget( m_search );
QPushButton * findNextBtn = new QPushButton( KIcon( "go-down-search" ), i18nc( "Find and go to the next search match", "Next" ), this );
findNextBtn->setToolTip( i18n( "Jump to next match" ) );
lay->addWidget( findNextBtn );
QPushButton * findPrevBtn = new QPushButton( KIcon( "go-up-search" ), i18nc( "Find and go to the previous search match", "Previous" ), this );
findPrevBtn->setToolTip( i18n( "Jump to previous match" ) );
lay->addWidget( findPrevBtn );
QPushButton * optionsBtn = new QPushButton( this );
optionsBtn->setText( i18n( "Options" ) );
optionsBtn->setToolTip( i18n( "Modify search behavior" ) );
QMenu * optionsMenu = new QMenu( optionsBtn );
m_caseSensitiveAct = optionsMenu->addAction( i18n( "Case sensitive" ) );
m_caseSensitiveAct->setCheckable( true );
m_fromCurrentPageAct = optionsMenu->addAction( i18n( "From current page" ) );
m_fromCurrentPageAct->setCheckable( true );
optionsBtn->setMenu( optionsMenu );
lay->addWidget( optionsBtn );
connect( closeBtn, SIGNAL(clicked()), this, SLOT(closeAndStopSearch()) );
connect( findNextBtn, SIGNAL(clicked()), this, SLOT(findNext()) );
connect( findPrevBtn, SIGNAL(clicked()), this, SLOT(findPrev()) );
connect( m_caseSensitiveAct, SIGNAL(toggled(bool)), this, SLOT(caseSensitivityChanged()) );
connect( m_fromCurrentPageAct, SIGNAL(toggled(bool)), this, SLOT(fromCurrentPageChanged()) );
m_caseSensitiveAct->setChecked( Okular::Settings::searchCaseSensitive() );
m_fromCurrentPageAct->setChecked( Okular::Settings::searchFromCurrentPage() );
hide();
// "activate" it only at th very end
m_active = true;
}
FindBar::~FindBar()
{
}
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 )
{
emit forwardKeyPressEvent( keyEvent );
return true;
}
}
}
return false;
}
QString FindBar::text() const
{
return m_search->lineEdit()->text();
}
Qt::CaseSensitivity FindBar::caseSensitivity() const
{
return m_caseSensitiveAct->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive;
}
void FindBar::focusAndSetCursor()
{
setFocus();
m_search->lineEdit()->selectAll();
m_search->lineEdit()->setFocus();
}
bool FindBar::maybeHide()
{
if ( m_search->lineEdit()->isSearchRunning() )
{
m_search->lineEdit()->stopSearch();
return false;
}
else
{
hide();
return true;
}
}
void FindBar::findNext()
{
m_search->lineEdit()->setSearchType( Okular::Document::NextMatch );
m_search->lineEdit()->findNext();
}
void FindBar::findPrev()
{
m_search->lineEdit()->setSearchType( Okular::Document::PreviousMatch );
m_search->lineEdit()->findPrev();
}
void FindBar::resetSearch()
{
m_search->lineEdit()->resetSearch();
}
void FindBar::caseSensitivityChanged()
{
m_search->lineEdit()->setSearchCaseSensitivity( m_caseSensitiveAct->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive );
if ( !m_active )
return;
Okular::Settings::setSearchCaseSensitive( m_caseSensitiveAct->isChecked() );
Okular::Settings::self()->writeConfig();
m_search->lineEdit()->restartSearch();
}
void FindBar::fromCurrentPageChanged()
{
m_search->lineEdit()->setSearchFromStart( !m_fromCurrentPageAct->isChecked() );
if ( !m_active )
return;
Okular::Settings::setSearchFromCurrentPage( m_fromCurrentPageAct->isChecked() );
Okular::Settings::self()->writeConfig();
}
void FindBar::closeAndStopSearch()
{
if ( m_search->lineEdit()->isSearchRunning() )
{
m_search->lineEdit()->stopSearch();
}
emit onCloseButtonPressed();
close();
}
#include "findbar.moc"
|