1 /***************************************************************************
2 * Copyright (C) 2007 by Pino Toscano <pino@kde.org> *
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"
15 #include <qstyleoption.h>
16 #include <qstylepainter.h>
17 #include <qtoolbutton.h>
21 #include <kpushbutton.h>
23 #include "searchlineedit.h"
31 HandleDrag( QWidget
*parent
= 0 )
34 setCursor( Qt::SizeAllCursor
);
35 setFixedWidth( style()->pixelMetric( QStyle::PM_ToolBarHandleExtent
) );
36 installEventFilter( parent
);
39 void paintEvent( QPaintEvent
* )
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 );
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() );
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
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
)
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
);
136 if ( obj
== m_anchor
&& e
->type() == QEvent::Resize
)
138 m_point
= QPoint( m_anchor
->width() / 2, m_anchor
->height() );
141 move( m_point
.x() - width() / 2, m_point
.y() - height() );
147 #include "presentationsearchbar.moc"