compile
[kdegraphics.git] / okular / ui / presentationsearchbar.cpp
blob5e7180038b7c38e3591c1b09c4f6bc1b9ac0fd7a
1 /***************************************************************************
2 * Copyright (C) 2007 by Pino Toscano <pino@kde.org> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 ***************************************************************************/
10 #include "presentationsearchbar.h"
12 #include <qevent.h>
13 #include <qlayout.h>
14 #include <qstyle.h>
15 #include <qstyleoption.h>
16 #include <qstylepainter.h>
17 #include <qtoolbutton.h>
19 #include <kicon.h>
20 #include <klocale.h>
21 #include <kpushbutton.h>
23 #include "searchlineedit.h"
25 #define SNAP_DELTA 15
27 class HandleDrag
28 : public QWidget
30 public:
31 HandleDrag( QWidget *parent = 0 )
32 : QWidget( parent )
34 setCursor( Qt::SizeAllCursor );
35 setFixedWidth( style()->pixelMetric( QStyle::PM_ToolBarHandleExtent ) );
36 installEventFilter( parent );
39 void paintEvent( QPaintEvent * )
41 QStyleOption opt;
42 opt.initFrom( this );
43 opt.state |= QStyle::State_Horizontal;
44 QStylePainter p( this );
45 p.drawPrimitive( QStyle::PE_IndicatorToolBarHandle, opt );
50 PresentationSearchBar::PresentationSearchBar( Okular::Document *document, QWidget *anchor, QWidget *parent )
51 : QWidget( parent ), m_anchor( anchor ), m_snapped( true )
53 setAutoFillBackground( true );
55 QHBoxLayout * lay = new QHBoxLayout( this );
56 lay->setMargin( 0 );
58 m_handle = new HandleDrag( this );
59 lay->addWidget( m_handle );
61 QToolButton * closeBtn = new QToolButton( this );
62 closeBtn->setIcon( KIcon( "dialog-close" ) );
63 closeBtn->setIconSize( QSize( 24, 24 ) );
64 closeBtn->setToolTip( i18n( "Close" ) );
65 closeBtn->setAutoRaise( true );
66 lay->addWidget( closeBtn );
68 m_search = new SearchLineEdit( this, document );
69 m_search->setClearButtonShown( true );
70 m_search->setSearchCaseSensitivity( Qt::CaseInsensitive );
71 m_search->setSearchMinimumLength( 0 );
72 m_search->setSearchType( Okular::Document::NextMatch );
73 m_search->setSearchId( PRESENTATION_SEARCH_ID );
74 m_search->setSearchColor( qRgb( 255, 255, 64 ) );
75 m_search->setSearchMoveViewport( true );
76 lay->addWidget( m_search );
78 KPushButton * findNextBtn = new KPushButton( KIcon( "go-down-search" ), i18n( "Find Next" ), this );
79 lay->addWidget( findNextBtn );
81 m_anchor->installEventFilter( this );
83 connect( closeBtn, SIGNAL( clicked() ), this, SLOT( close() ) );
84 connect( findNextBtn, SIGNAL( clicked() ), m_search, SLOT( findNext() ) );
87 PresentationSearchBar::~PresentationSearchBar()
91 void PresentationSearchBar::forceSnap()
93 m_point = QPoint( m_anchor->width() / 2, m_anchor->height() );
94 m_snapped = true;
95 move( m_point.x() - width() / 2, m_point.y() - height() );
98 void PresentationSearchBar::focusOnSearchEdit()
100 m_search->setFocus();
103 void PresentationSearchBar::resizeEvent( QResizeEvent * )
105 // if in snap mode, then force the snap and place ourselves correctly again
106 if ( m_snapped )
107 forceSnap();
110 bool PresentationSearchBar::eventFilter( QObject *obj, QEvent *e )
112 if ( obj == m_handle &&
113 ( e->type() == QEvent::MouseButtonPress || e->type() == QEvent::MouseButtonRelease || e->type() == QEvent::MouseMove ) )
115 QMouseEvent *me = (QMouseEvent*)e;
116 if ( e->type() == QEvent::MouseButtonPress )
118 m_drag = m_handle->mapTo( this, me->pos() );
120 else if ( e->type() == QEvent::MouseButtonRelease )
122 m_drag = QPoint();
124 else if ( e->type() == QEvent::MouseMove )
126 QPoint snapdelta( width() / 2, height() );
127 QPoint delta = m_handle->mapTo( this, me->pos() ) - m_drag;
128 QPoint newpostemp = pos() + delta;
129 QPoint tmp = newpostemp + snapdelta - m_point;
130 QPoint newpos = abs( tmp.x() ) < SNAP_DELTA && abs( tmp.y() ) < SNAP_DELTA ? m_point - snapdelta : newpostemp;
131 m_snapped = newpos == ( m_point - snapdelta );
132 move( newpos );
134 return true;
136 if ( obj == m_anchor && e->type() == QEvent::Resize )
138 m_point = QPoint( m_anchor->width() / 2, m_anchor->height() );
140 if ( m_snapped )
141 move( m_point.x() - width() / 2, m_point.y() - height() );
144 return false;
147 #include "presentationsearchbar.moc"