2007-03-17 22:58:41 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* 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. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
2007-04-19 18:30:20 +00:00
|
|
|
#include "presentationsearchbar.h"
|
|
|
|
|
2007-03-17 22:58:41 +00:00
|
|
|
#include <qevent.h>
|
|
|
|
#include <qlayout.h>
|
|
|
|
#include <qstyle.h>
|
|
|
|
#include <qstyleoption.h>
|
|
|
|
#include <qstylepainter.h>
|
|
|
|
#include <qtoolbutton.h>
|
|
|
|
|
|
|
|
#include <kicon.h>
|
|
|
|
#include <klocale.h>
|
2007-03-23 23:28:39 +00:00
|
|
|
#include <kpushbutton.h>
|
2007-03-17 22:58:41 +00:00
|
|
|
|
|
|
|
#include "searchlineedit.h"
|
|
|
|
|
|
|
|
#define SNAP_DELTA 15
|
|
|
|
|
|
|
|
class HandleDrag
|
|
|
|
: public QWidget
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
HandleDrag( QWidget *parent = 0 )
|
|
|
|
: QWidget( parent )
|
|
|
|
{
|
|
|
|
setCursor( Qt::SizeAllCursor );
|
|
|
|
setFixedWidth( style()->pixelMetric( QStyle::PM_ToolBarHandleExtent ) );
|
|
|
|
installEventFilter( parent );
|
|
|
|
}
|
|
|
|
|
|
|
|
void paintEvent( QPaintEvent * )
|
|
|
|
{
|
|
|
|
QStyleOption opt;
|
|
|
|
opt.initFrom( this );
|
|
|
|
opt.state |= QStyle::State_Horizontal;
|
|
|
|
QStylePainter p( this );
|
|
|
|
p.drawPrimitive( QStyle::PE_IndicatorToolBarHandle, opt );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
PresentationSearchBar::PresentationSearchBar( Okular::Document *document, QWidget *anchor, QWidget *parent )
|
|
|
|
: QWidget( parent ), m_anchor( anchor ), m_snapped( true )
|
|
|
|
{
|
|
|
|
setAutoFillBackground( true );
|
|
|
|
|
|
|
|
QHBoxLayout * lay = new QHBoxLayout( this );
|
|
|
|
lay->setMargin( 0 );
|
|
|
|
|
|
|
|
m_handle = new HandleDrag( this );
|
|
|
|
lay->addWidget( m_handle );
|
|
|
|
|
|
|
|
QToolButton * closeBtn = new QToolButton( this );
|
|
|
|
closeBtn->setIcon( KIcon( "fileclose" ) );
|
|
|
|
closeBtn->setIconSize( QSize( 24, 24 ) );
|
|
|
|
closeBtn->setToolTip( i18n( "Close" ) );
|
|
|
|
closeBtn->setAutoRaise( true );
|
|
|
|
lay->addWidget( closeBtn );
|
|
|
|
|
|
|
|
m_search = new SearchLineEdit( this, document );
|
|
|
|
m_search->setClearButtonShown( true );
|
|
|
|
m_search->setSearchCaseSensitivity( Qt::CaseInsensitive );
|
|
|
|
m_search->setSearchMinimumLength( 0 );
|
|
|
|
m_search->setSearchType( Okular::Document::NextMatch );
|
|
|
|
m_search->setSearchId( PRESENTATION_SEARCH_ID );
|
|
|
|
m_search->setSearchColor( qRgb( 255, 255, 64 ) );
|
|
|
|
m_search->setSearchMoveViewport( true );
|
|
|
|
lay->addWidget( m_search );
|
|
|
|
|
2007-10-21 21:40:12 +00:00
|
|
|
KPushButton * findNextBtn = new KPushButton( KIcon( "edit-find-next" ), i18n( "Find Next" ), this );
|
2007-03-23 23:28:39 +00:00
|
|
|
lay->addWidget( findNextBtn );
|
|
|
|
|
2007-03-17 22:58:41 +00:00
|
|
|
m_anchor->installEventFilter( this );
|
|
|
|
|
|
|
|
connect( closeBtn, SIGNAL( clicked() ), this, SLOT( close() ) );
|
2007-03-23 23:28:39 +00:00
|
|
|
connect( findNextBtn, SIGNAL( clicked() ), m_search, SLOT( findNext() ) );
|
2007-03-17 22:58:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PresentationSearchBar::~PresentationSearchBar()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void PresentationSearchBar::forceSnap()
|
|
|
|
{
|
|
|
|
m_point = QPoint( m_anchor->width() / 2, m_anchor->height() );
|
|
|
|
m_snapped = true;
|
|
|
|
move( m_point.x() - width() / 2, m_point.y() - height() );
|
|
|
|
}
|
|
|
|
|
2007-04-20 19:45:33 +00:00
|
|
|
void PresentationSearchBar::focusOnSearchEdit()
|
|
|
|
{
|
|
|
|
m_search->setFocus();
|
|
|
|
}
|
|
|
|
|
2007-03-23 15:02:22 +00:00
|
|
|
void PresentationSearchBar::resizeEvent( QResizeEvent * )
|
|
|
|
{
|
|
|
|
// if in snap mode, then force the snap and place ourselves correctly again
|
|
|
|
if ( m_snapped )
|
|
|
|
forceSnap();
|
|
|
|
}
|
|
|
|
|
2007-03-17 22:58:41 +00:00
|
|
|
bool PresentationSearchBar::eventFilter( QObject *obj, QEvent *e )
|
|
|
|
{
|
|
|
|
if ( obj == m_handle &&
|
|
|
|
( e->type() == QEvent::MouseButtonPress || e->type() == QEvent::MouseButtonRelease || e->type() == QEvent::MouseMove ) )
|
|
|
|
{
|
|
|
|
QMouseEvent *me = (QMouseEvent*)e;
|
|
|
|
if ( e->type() == QEvent::MouseButtonPress )
|
|
|
|
{
|
|
|
|
m_drag = m_handle->mapTo( this, me->pos() );
|
|
|
|
}
|
|
|
|
else if ( e->type() == QEvent::MouseButtonRelease )
|
|
|
|
{
|
|
|
|
m_drag = QPoint();
|
|
|
|
}
|
|
|
|
else if ( e->type() == QEvent::MouseMove )
|
|
|
|
{
|
|
|
|
QPoint snapdelta( width() / 2, height() );
|
|
|
|
QPoint delta = m_handle->mapTo( this, me->pos() ) - m_drag;
|
|
|
|
QPoint newpostemp = pos() + delta;
|
|
|
|
QPoint tmp = newpostemp + snapdelta - m_point;
|
|
|
|
QPoint newpos = abs( tmp.x() ) < SNAP_DELTA && abs( tmp.y() ) < SNAP_DELTA ? m_point - snapdelta : newpostemp;
|
|
|
|
m_snapped = newpos == ( m_point - snapdelta );
|
|
|
|
move( newpos );
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ( obj == m_anchor && e->type() == QEvent::Resize )
|
|
|
|
{
|
|
|
|
m_point = QPoint( m_anchor->width() / 2, m_anchor->height() );
|
|
|
|
|
|
|
|
if ( m_snapped )
|
|
|
|
move( m_point.x() - width() / 2, m_point.y() - height() );
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "presentationsearchbar.moc"
|