]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/customwidgets.cpp
KEY_SPACE = 32, simplify several outputs and interfaces
[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$
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 "customwidgets.hpp"
32 #include "qt4.hpp" /*needed for qtr and CONNECT, but not necessary */
33
34 #include <QPainter>
35 #include <QLineEdit>
36 #include <QColorGroup>
37 #include <QRect>
38 #include <QKeyEvent>
39 #include <QWheelEvent>
40 #include <QToolButton>
41 #include <QHBoxLayout>
42 #include <vlc_intf_strings.h>
43
44
45 #include <vlc_keys.h>
46
47 ClickLineEdit::ClickLineEdit( const QString &msg, QWidget *parent) : QLineEdit( parent )
48 {
49     mDrawClickMsg = true;
50     setClickMessage( msg );
51 }
52
53 void ClickLineEdit::setClickMessage( const QString &msg )
54 {
55     mClickMessage = msg;
56     repaint();
57 }
58
59
60 void ClickLineEdit::setText( const QString &txt )
61 {
62     mDrawClickMsg = txt.isEmpty();
63     repaint();
64     QLineEdit::setText( txt );
65 }
66
67 void ClickLineEdit::paintEvent( QPaintEvent *pe )
68 {
69     QLineEdit::paintEvent( pe );
70     if ( mDrawClickMsg == true && !hasFocus() ) {
71         QPainter p( this );
72         QPen tmp = p.pen();
73         p.setPen( palette().color( QPalette::Disabled, QPalette::Text ) );
74         QRect cr = contentsRect();
75         // Add two pixel margin on the left side
76         cr.setLeft( cr.left() + 3 );
77         p.drawText( cr, Qt::AlignLeft | Qt::AlignVCenter, mClickMessage );
78         p.setPen( tmp );
79         p.end();
80     }
81 }
82
83 void ClickLineEdit::dropEvent( QDropEvent *ev )
84 {
85     mDrawClickMsg = false;
86     QLineEdit::dropEvent( ev );
87 }
88
89 void ClickLineEdit::focusInEvent( QFocusEvent *ev )
90 {
91     if ( mDrawClickMsg == true ) {
92         mDrawClickMsg = false;
93         repaint();
94     }
95     QLineEdit::focusInEvent( ev );
96 }
97
98 void ClickLineEdit::focusOutEvent( QFocusEvent *ev )
99 {
100     if ( text().isEmpty() ) {
101         mDrawClickMsg = true;
102         repaint();
103     }
104     QLineEdit::focusOutEvent( ev );
105 }
106
107 SearchLineEdit::SearchLineEdit( QWidget *parent ) : QFrame( parent )
108 {
109     setFrameStyle( QFrame::WinPanel | QFrame::Sunken );
110     setLineWidth( 0 );
111
112     QHBoxLayout *frameLayout = new QHBoxLayout( this );
113     frameLayout->setMargin( 0 );
114     frameLayout->setSpacing( 0 );
115
116     QPalette palette;
117     QBrush brush( QColor(255, 255, 255, 255) );
118     brush.setStyle(Qt::SolidPattern);
119     palette.setBrush(QPalette::Active, QPalette::Window, brush); //Qt::white
120
121     setPalette(palette);
122     setAutoFillBackground(true);
123
124     searchLine = new  ClickLineEdit( qtr(I_PL_FILTER), 0 );
125     searchLine->setFrame( false );
126     searchLine->setMinimumWidth( 80 );
127
128     CONNECT( searchLine, textChanged( const QString& ),
129              this, updateText( const QString& ) );
130     frameLayout->addWidget( searchLine );
131
132     clearButton = new QToolButton;
133     clearButton->setAutoRaise( true );
134     clearButton->setMaximumWidth( 30 );
135     clearButton->setIcon( QIcon( ":/toolbar/clear" ) );
136     clearButton->setToolTip( qfu(vlc_pgettext("Tooltip|Clear", "Clear")) );
137     clearButton->hide();
138
139     CONNECT( clearButton, clicked(), searchLine, clear() );
140     frameLayout->addWidget( clearButton );
141 }
142
143 void SearchLineEdit::updateText( const QString& text )
144 {
145     clearButton->setVisible( !text.isEmpty() );
146     emit textChanged( text );
147 }
148
149 /***************************************************************************
150  * Hotkeys converters
151  ***************************************************************************/
152 int qtKeyModifiersToVLC( QInputEvent* e )
153 {
154     int i_keyModifiers = 0;
155     if( e->modifiers() & Qt::ShiftModifier ) i_keyModifiers |= KEY_MODIFIER_SHIFT;
156     if( e->modifiers() & Qt::AltModifier ) i_keyModifiers |= KEY_MODIFIER_ALT;
157     if( e->modifiers() & Qt::ControlModifier ) i_keyModifiers |= KEY_MODIFIER_CTRL;
158     if( e->modifiers() & Qt::MetaModifier ) i_keyModifiers |= KEY_MODIFIER_META;
159     return i_keyModifiers;
160 }
161
162 int qtEventToVLCKey( QKeyEvent *e )
163 {
164     int i_vlck = 0;
165     /* Handle modifiers */
166     i_vlck |= qtKeyModifiersToVLC( e );
167
168     bool found = false;
169     /* Look for some special keys */
170 #define HANDLE( qt, vk ) case Qt::qt : i_vlck |= vk; found = true;break
171     switch( e->key() )
172     {
173         HANDLE( Key_Left, KEY_LEFT );
174         HANDLE( Key_Right, KEY_RIGHT );
175         HANDLE( Key_Up, KEY_UP );
176         HANDLE( Key_Down, KEY_DOWN );
177         HANDLE( Key_Space, ' ' );
178         HANDLE( Key_Escape, KEY_ESC );
179         HANDLE( Key_Return, KEY_ENTER );
180         HANDLE( Key_Enter, KEY_ENTER );
181         HANDLE( Key_F1, KEY_F1 );
182         HANDLE( Key_F2, KEY_F2 );
183         HANDLE( Key_F3, KEY_F3 );
184         HANDLE( Key_F4, KEY_F4 );
185         HANDLE( Key_F5, KEY_F5 );
186         HANDLE( Key_F6, KEY_F6 );
187         HANDLE( Key_F7, KEY_F7 );
188         HANDLE( Key_F8, KEY_F8 );
189         HANDLE( Key_F9, KEY_F9 );
190         HANDLE( Key_F10, KEY_F10 );
191         HANDLE( Key_F11, KEY_F11 );
192         HANDLE( Key_F12, KEY_F12 );
193         HANDLE( Key_PageUp, KEY_PAGEUP );
194         HANDLE( Key_PageDown, KEY_PAGEDOWN );
195         HANDLE( Key_Home, KEY_HOME );
196         HANDLE( Key_End, KEY_END );
197         HANDLE( Key_Insert, KEY_INSERT );
198         HANDLE( Key_Delete, KEY_DELETE );
199         HANDLE( Key_VolumeDown, KEY_VOLUME_DOWN);
200         HANDLE( Key_VolumeUp, KEY_VOLUME_UP );
201         HANDLE( Key_VolumeMute, KEY_VOLUME_MUTE );
202         HANDLE( Key_MediaPlay, KEY_MEDIA_PLAY_PAUSE );
203         HANDLE( Key_MediaStop, KEY_MEDIA_STOP );
204         HANDLE( Key_MediaPrevious, KEY_MEDIA_PREV_TRACK );
205         HANDLE( Key_MediaNext, KEY_MEDIA_NEXT_TRACK );
206
207     }
208     if( !found )
209     {
210         /* Force lowercase */
211         if( e->key() >= Qt::Key_A && e->key() <= Qt::Key_Z )
212             i_vlck += e->key() + 32;
213         /* Rest of the ascii range */
214         else if( e->key() >= Qt::Key_Space && e->key() <= Qt::Key_AsciiTilde )
215             i_vlck += e->key();
216     }
217     return i_vlck;
218 }
219
220 int qtWheelEventToVLCKey( QWheelEvent *e )
221 {
222     int i_vlck = 0;
223     /* Handle modifiers */
224     i_vlck |= qtKeyModifiersToVLC( e );
225     if ( e->delta() > 0 )
226         i_vlck |= KEY_MOUSEWHEELUP;
227     else
228         i_vlck |= KEY_MOUSEWHEELDOWN;
229     return i_vlck;
230 }
231
232 QString VLCKeyToString( int val )
233 {
234     const char *base = KeyToString (val & ~KEY_MODIFIER);
235
236     QString r = "";
237     if( val & KEY_MODIFIER_CTRL )
238         r+= "Ctrl+";
239     if( val & KEY_MODIFIER_ALT )
240         r+= "Alt+";
241     if( val & KEY_MODIFIER_SHIFT )
242         r+= "Shift+";
243
244     return r + (base ? base : qtr( "Unset" ) );
245 }
246