searchwidget.cpp 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/***************************************************************************
 *   Copyright (C) 2004 by Enrico Ros <eros.kde@email.it>                  *
 *                                                                         *
 *   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.                                   *
 ***************************************************************************/

// qt/kde includes
#include <qtooltip.h>
Laurent Montel's avatar
Laurent Montel committed
12
#include <qapplication.h>
13 14 15 16 17 18 19 20 21 22 23
#include <kaction.h>
#include <kactioncollection.h>
#include <klocale.h>
#include <kiconloader.h>
#include <klineedit.h>
#include <kpopupmenu.h>
#include <ktoolbarbutton.h>

// local includes
#include "searchwidget.h"
#include "document.h"
24
#include "settings.h"
25 26 27 28 29 30 31 32

//#include <qiconset.h>
SearchWidget::SearchWidget( QWidget * parent, KPDFDocument * document )
    : QHBox( parent ), m_document( document ), m_caseSensitive( false )
{
    setMargin( 4 );

    // clear button
Laurent Montel's avatar
Laurent Montel committed
33
    KToolBarButton * m_clearButton = new KToolBarButton( SmallIcon(QApplication::reverseLayout() ? "clear_left" : "locationbar_erase"), 1, this );
34
    QToolTip::add( m_clearButton, i18n( "Clear filter" ) );
35 36 37 38 39

    // line edit
    m_lineEdit = new KLineEdit( this );
    m_lineEdit->setFrame( QFrame::Sunken );
    connect( m_lineEdit, SIGNAL(textChanged(const QString &)), SLOT(slotTextChanged(const QString &)) );
40
    connect( m_clearButton, SIGNAL(clicked()), m_lineEdit, SLOT(clear()) );
41 42 43
    QToolTip::add( m_lineEdit, i18n( "Enter at least 3 letters to filter pages" ) );

    // change case button and menu
44
/*    KToolBarButton * search = new KToolBarButton( SmallIcon("find"), 2, this );
45 46 47 48 49 50
    m_caseMenu = new KPopupMenu( search );
    m_caseMenu->insertItem( i18n("Case Insensitive"), 1 );
    m_caseMenu->insertItem( i18n("Case Sensitive"), 2 );
    m_caseMenu->setItemChecked( 1, true );
    connect( m_caseMenu, SIGNAL( activated(int) ), SLOT( slotChangeCase(int) ) );
    search->setPopup( m_caseMenu );
51 52
*/
    int sideLength = m_lineEdit->sizeHint().height();
53
    m_clearButton->setMinimumSize( QSize( sideLength, sideLength ) );
54
 //   search->setMinimumSize( QSize( sideLength, sideLength ) );
55 56
}

57
void SearchWidget::setupActions( KActionCollection * ac )
58 59 60 61 62
{
    KToggleAction * ss = new KToggleAction( i18n( "Show Search Bar" ), 0, ac, "show_searchbar" );
    ss->setCheckedState(i18n("Hide Search Bar"));
    connect( ss, SIGNAL( toggled( bool ) ), SLOT( slotToggleSearchBar( bool ) ) );

63
    ss->setChecked( Settings::showSearchBar() );
64 65 66
    slotToggleSearchBar( ss->isChecked() );
}

67
void SearchWidget::saveSettings()
68
{
69
    Settings::setShowSearchBar( isShown() );
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
}

void SearchWidget::slotTextChanged( const QString & text )
{
    if ( text.length() > 2 || text.isEmpty() )
    {
        m_lineEdit->setPaletteForegroundColor( palette().active().text() );
        m_document->slotSetFilter( text, m_caseSensitive );
    }
    else
    {
        m_lineEdit->setPaletteForegroundColor( Qt::red );
        m_document->slotSetFilter( QString::null, m_caseSensitive );
    }
}

void SearchWidget::slotChangeCase( int index )
{
    bool newState = (index == 2);
    if ( newState != m_caseSensitive )
    {
        m_caseSensitive = newState;
        m_caseMenu->setItemChecked( 1, !m_caseSensitive );
        m_caseMenu->setItemChecked( 2, m_caseSensitive );
        slotTextChanged( m_lineEdit->text() );
    }
}

void SearchWidget::slotToggleSearchBar( bool visible )
{
    setShown( visible );
    if ( !visible )
        m_document->slotSetFilter( QString::null, m_caseSensitive );
}

#include "searchwidget.moc"