]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/searchlineedit.cpp
Search child of playlist rather than anywhere
[vlc] / modules / gui / qt4 / util / searchlineedit.cpp
1 /*****************************************************************************
2  * customwidgets.cpp: Custom widgets
3  ****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * Copyright (C) 2004 Daniel Molkentin <molkentin@kde.org>
6  * $Id$
7  *
8  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
9  * The "ClickLineEdit" control is based on code by  Daniel Molkentin
10  * <molkentin@kde.org> for libkdepim
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include "searchlineedit.hpp"
32 #include "customwidgets.hpp"
33
34 #include "qt4.hpp" /*needed for qtr and CONNECT, but not necessary */
35
36 #include <QPainter>
37 #include <QRect>
38 #include <QStyle>
39 #include <QStyleOption>
40
41 #include <vlc_intf_strings.h>
42
43 #if !HAS_QT47
44
45 ClickLineEdit::ClickLineEdit( const QString &msg, QWidget *parent) : QLineEdit( parent )
46 {
47     mDrawClickMsg = true;
48     setPlaceholderText( msg );
49 }
50
51 void ClickLineEdit::setPlaceholderText( const QString &msg )
52 {
53     mClickMessage = msg;
54     repaint();
55 }
56
57
58 void ClickLineEdit::setText( const QString &txt )
59 {
60     mDrawClickMsg = txt.isEmpty();
61     repaint();
62     QLineEdit::setText( txt );
63 }
64
65 void ClickLineEdit::paintEvent( QPaintEvent *pe )
66 {
67     QLineEdit::paintEvent( pe );
68     if ( mDrawClickMsg && !hasFocus() ) {
69         QPainter p( this );
70         QPen tmp = p.pen();
71         p.setPen( palette().color( QPalette::Disabled, QPalette::Text ) );
72         QRect cr = contentsRect();
73         // Add two pixel margin on the left side
74         cr.setLeft( cr.left() + 3 );
75         p.drawText( cr, Qt::AlignLeft | Qt::AlignVCenter, mClickMessage );
76         p.setPen( tmp );
77         p.end();
78     }
79 }
80
81 void ClickLineEdit::dropEvent( QDropEvent *ev )
82 {
83     mDrawClickMsg = false;
84     QLineEdit::dropEvent( ev );
85 }
86
87 void ClickLineEdit::focusInEvent( QFocusEvent *ev )
88 {
89     if ( mDrawClickMsg ) {
90         mDrawClickMsg = false;
91         repaint();
92     }
93     QLineEdit::focusInEvent( ev );
94 }
95
96 void ClickLineEdit::focusOutEvent( QFocusEvent *ev )
97 {
98     if ( text().isEmpty() ) {
99         mDrawClickMsg = true;
100         repaint();
101     }
102     QLineEdit::focusOutEvent( ev );
103 }
104 #endif
105
106 #ifndef Q_WS_MAC
107 SearchLineEdit::SearchLineEdit( QWidget *parent ) : QLineEdit( parent )
108 {
109     clearButton = new QFramelessButton( this );
110     clearButton->setIcon( QIcon( ":/toolbar/clear" ) );
111     clearButton->setIconSize( QSize( 16, 16 ) );
112     clearButton->setCursor( Qt::ArrowCursor );
113     clearButton->setToolTip( qfu(vlc_pgettext("Tooltip|Clear", "Clear")) );
114     clearButton->hide();
115
116     CONNECT( clearButton, clicked(), this, clear() );
117
118     int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth, 0, this );
119
120     QFontMetrics metrics( font() );
121     QString styleSheet = QString( "min-height: %1px; "
122                                   "padding-top: 1px; "
123                                   "padding-bottom: 1px; "
124                                   "padding-right: %2px;" )
125                                   .arg( metrics.height() + ( 2 * frameWidth ) )
126                                   .arg( clearButton->sizeHint().width() + 1 );
127     setStyleSheet( styleSheet );
128
129     setMessageVisible( true );
130
131     CONNECT( this, textEdited( const QString& ),
132              this, updateText( const QString& ) );
133
134     CONNECT( this, editingFinished(),
135              this, searchEditingFinished() );
136
137 }
138
139 void SearchLineEdit::clear()
140 {
141     setText( QString() );
142     clearButton->hide();
143     setMessageVisible( true );
144 }
145
146 void SearchLineEdit::setMessageVisible( bool on )
147 {
148     message = on;
149     repaint();
150     return;
151 }
152
153 void SearchLineEdit::updateText( const QString& text )
154 {
155     clearButton->setVisible( !text.isEmpty() );
156 }
157
158 void SearchLineEdit::resizeEvent ( QResizeEvent * event )
159 {
160     QLineEdit::resizeEvent( event );
161     int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this);
162     clearButton->resize( clearButton->sizeHint().width(), height() );
163     clearButton->move( width() - clearButton->width() - frameWidth, 0 );
164 }
165
166 void SearchLineEdit::focusInEvent( QFocusEvent *event )
167 {
168     if( message )
169     {
170         setMessageVisible( false );
171     }
172     QLineEdit::focusInEvent( event );
173 }
174
175 void SearchLineEdit::focusOutEvent( QFocusEvent *event )
176 {
177     if( text().isEmpty() )
178     {
179         setMessageVisible( true );
180     }
181     QLineEdit::focusOutEvent( event );
182 }
183
184 void SearchLineEdit::paintEvent( QPaintEvent *event )
185 {
186     QLineEdit::paintEvent( event );
187     if( !message ) return;
188     QStyleOption option;
189     option.initFrom( this );
190     QRect rect = style()->subElementRect( QStyle::SE_LineEditContents, &option, this )
191         .adjusted( 3, 0, clearButton->width() + 1, 0 );
192     QPainter painter( this );
193     painter.setPen( palette().color( QPalette::Disabled, QPalette::Text ) );
194     painter.drawText( rect, Qt::AlignLeft | Qt::AlignVCenter, qtr( I_PL_FILTER ) );
195 }
196
197 void SearchLineEdit::searchEditingFinished()
198 {
199     emit searchDelayedChanged( text() );
200 }
201
202 #endif