]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/customwidgets.cpp
Find as you type
[vlc] / modules / gui / qt4 / util / customwidgets.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: qvlcframe.hpp 16283 2006-08-17 18:16:09Z zorglub $
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 #include "customwidgets.hpp"
27 #include <QPainter>
28 #include <QLineEdit>
29 #include <QPainter>
30 #include <QColorGroup>
31 #include <QRect>
32
33 ClickLineEdit::ClickLineEdit( const QString &msg, QWidget *parent) : QLineEdit( parent )
34 {
35     mDrawClickMsg = true;
36     setClickMessage( msg );
37 }
38
39 void ClickLineEdit::setClickMessage( const QString &msg )
40 {
41     mClickMessage = msg;
42     repaint();
43 }
44
45
46 void ClickLineEdit::setText( const QString &txt )
47 {
48     mDrawClickMsg = txt.isEmpty();
49     repaint();
50     QLineEdit::setText( txt );
51 }
52
53 void ClickLineEdit::paintEvent( QPaintEvent *pe )
54 {
55     QPainter p( this );
56     QLineEdit::paintEvent( pe );
57     if ( mDrawClickMsg == true && !hasFocus() ) {
58         QPen tmp = p.pen();
59         p.setPen( palette().color( QPalette::Disabled, QPalette::Text ) );
60         QRect cr = contentsRect();
61         // Add two pixel margin on the left side
62         cr.setLeft( cr.left() + 3 );
63         p.drawText( cr, Qt::AlignLeft | Qt::AlignVCenter, mClickMessage );
64         p.setPen( tmp );
65     }
66 }
67
68 void ClickLineEdit::dropEvent( QDropEvent *ev )
69 {
70     mDrawClickMsg = false;
71     QLineEdit::dropEvent( ev );
72 }
73
74
75 void ClickLineEdit::focusInEvent( QFocusEvent *ev )
76 {
77     if ( mDrawClickMsg == true ) {
78         mDrawClickMsg = false;
79         repaint();
80     }
81     QLineEdit::focusInEvent( ev );
82 }
83
84
85 void ClickLineEdit::focusOutEvent( QFocusEvent *ev )
86 {
87     if ( text().isEmpty() ) {
88         mDrawClickMsg = true;
89         repaint();
90     }
91     QLineEdit::focusOutEvent( ev );
92 }