1 /*****************************************************************************
2 * customwidgets.cpp: Custom widgets
3 ****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
5 * Copyright (C) 2004 Daniel Molkentin <molkentin@kde.org>
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
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.
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.
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 *****************************************************************************/
31 #include "searchlineedit.hpp"
32 #include "customwidgets.hpp"
34 #include "qt4.hpp" /*needed for qtr and CONNECT, but not necessary */
39 #include <QStyleOption>
41 #include <vlc_intf_strings.h>
45 ClickLineEdit::ClickLineEdit( const QString &msg, QWidget *parent) : QLineEdit( parent )
48 setPlaceholderText( msg );
51 void ClickLineEdit::setPlaceholderText( const QString &msg )
58 void ClickLineEdit::setText( const QString &txt )
60 mDrawClickMsg = txt.isEmpty();
62 QLineEdit::setText( txt );
65 void ClickLineEdit::paintEvent( QPaintEvent *pe )
67 QLineEdit::paintEvent( pe );
68 if ( mDrawClickMsg && !hasFocus() ) {
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 );
81 void ClickLineEdit::dropEvent( QDropEvent *ev )
83 mDrawClickMsg = false;
84 QLineEdit::dropEvent( ev );
87 void ClickLineEdit::focusInEvent( QFocusEvent *ev )
89 if ( mDrawClickMsg ) {
90 mDrawClickMsg = false;
93 QLineEdit::focusInEvent( ev );
96 void ClickLineEdit::focusOutEvent( QFocusEvent *ev )
98 if ( text().isEmpty() ) {
102 QLineEdit::focusOutEvent( ev );
107 SearchLineEdit::SearchLineEdit( QWidget *parent ) : QLineEdit( parent )
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")) );
116 CONNECT( clearButton, clicked(), this, clear() );
118 int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth, 0, this );
120 QFontMetrics metrics( font() );
121 QString styleSheet = QString( "min-height: %1px; "
123 "padding-bottom: 1px; "
124 "padding-right: %2px;" )
125 .arg( metrics.height() + ( 2 * frameWidth ) )
126 .arg( clearButton->sizeHint().width() + 1 );
127 setStyleSheet( styleSheet );
129 setMessageVisible( true );
131 CONNECT( this, textEdited( const QString& ),
132 this, updateText( const QString& ) );
134 CONNECT( this, editingFinished(),
135 this, searchEditingFinished() );
139 void SearchLineEdit::clear()
141 setText( QString() );
143 setMessageVisible( true );
146 void SearchLineEdit::setMessageVisible( bool on )
153 void SearchLineEdit::updateText( const QString& text )
155 clearButton->setVisible( !text.isEmpty() );
158 void SearchLineEdit::resizeEvent ( QResizeEvent * event )
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 );
166 void SearchLineEdit::focusInEvent( QFocusEvent *event )
170 setMessageVisible( false );
172 QLineEdit::focusInEvent( event );
175 void SearchLineEdit::focusOutEvent( QFocusEvent *event )
177 if( text().isEmpty() )
179 setMessageVisible( true );
181 QLineEdit::focusOutEvent( event );
184 void SearchLineEdit::paintEvent( QPaintEvent *event )
186 QLineEdit::paintEvent( event );
187 if( !message ) return;
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 ) );
197 void SearchLineEdit::searchEditingFinished()
199 emit searchDelayedChanged( text() );