]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/customwidgets.cpp
Qt4 - New kind of volume slider.
[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
27 #include "customwidgets.hpp"
28 #include <QPainter>
29 #include <QLineEdit>
30 #include <QPainter>
31 #include <QColorGroup>
32 #include <QRect>
33 #include <QKeyEvent>
34 #include <QWheelEvent>
35
36 #include <vlc_keys.h>
37
38 ClickLineEdit::ClickLineEdit( const QString &msg, QWidget *parent) : QLineEdit( parent )
39 {
40     mDrawClickMsg = true;
41     setClickMessage( msg );
42 }
43
44 void ClickLineEdit::setClickMessage( const QString &msg )
45 {
46     mClickMessage = msg;
47     repaint();
48 }
49
50
51 void ClickLineEdit::setText( const QString &txt )
52 {
53     mDrawClickMsg = txt.isEmpty();
54     repaint();
55     QLineEdit::setText( txt );
56 }
57
58 void ClickLineEdit::paintEvent( QPaintEvent *pe )
59 {
60     QPainter p( this );
61     QLineEdit::paintEvent( pe );
62     if ( mDrawClickMsg == true && !hasFocus() ) {
63         QPen tmp = p.pen();
64         p.setPen( palette().color( QPalette::Disabled, QPalette::Text ) );
65         QRect cr = contentsRect();
66         // Add two pixel margin on the left side
67         cr.setLeft( cr.left() + 3 );
68         p.drawText( cr, Qt::AlignLeft | Qt::AlignVCenter, mClickMessage );
69         p.setPen( tmp );
70     }
71 }
72
73 void ClickLineEdit::dropEvent( QDropEvent *ev )
74 {
75     mDrawClickMsg = false;
76     QLineEdit::dropEvent( ev );
77 }
78
79 void ClickLineEdit::focusInEvent( QFocusEvent *ev )
80 {
81     if ( mDrawClickMsg == true ) {
82         mDrawClickMsg = false;
83         repaint();
84     }
85     QLineEdit::focusInEvent( ev );
86 }
87
88 void ClickLineEdit::focusOutEvent( QFocusEvent *ev )
89 {
90     if ( text().isEmpty() ) {
91         mDrawClickMsg = true;
92         repaint();
93     }
94     QLineEdit::focusOutEvent( ev );
95 }
96
97 /***************************************************************************
98  * Hotkeys converters
99  ***************************************************************************/
100 int qtKeyModifiersToVLC( QInputEvent* e )
101 {
102     int i_keyModifiers = 0;
103     if( e->modifiers() & Qt::ShiftModifier ) i_keyModifiers |= KEY_MODIFIER_SHIFT;
104     if( e->modifiers() & Qt::AltModifier ) i_keyModifiers |= KEY_MODIFIER_ALT;
105     if( e->modifiers() & Qt::ControlModifier ) i_keyModifiers |= KEY_MODIFIER_CTRL;
106     if( e->modifiers() & Qt::MetaModifier ) i_keyModifiers |= KEY_MODIFIER_META;
107     return i_keyModifiers;
108 }
109
110 int qtEventToVLCKey( QKeyEvent *e )
111 {
112     int i_vlck = 0;
113     /* Handle modifiers */
114     i_vlck |= qtKeyModifiersToVLC( e );
115
116     bool found = false;
117     /* Look for some special keys */
118 #define HANDLE( qt, vk ) case Qt::qt : i_vlck |= vk; found = true;break
119     switch( e->key() )
120     {
121         HANDLE( Key_Left, KEY_LEFT );
122         HANDLE( Key_Right, KEY_RIGHT );
123         HANDLE( Key_Up, KEY_UP );
124         HANDLE( Key_Down, KEY_DOWN );
125         HANDLE( Key_Space, KEY_SPACE );
126         HANDLE( Key_Escape, KEY_ESC );
127         HANDLE( Key_Enter, KEY_ENTER );
128         HANDLE( Key_F1, KEY_F1 );
129         HANDLE( Key_F2, KEY_F2 );
130         HANDLE( Key_F3, KEY_F3 );
131         HANDLE( Key_F4, KEY_F4 );
132         HANDLE( Key_F5, KEY_F5 );
133         HANDLE( Key_F6, KEY_F6 );
134         HANDLE( Key_F7, KEY_F7 );
135         HANDLE( Key_F8, KEY_F8 );
136         HANDLE( Key_F9, KEY_F9 );
137         HANDLE( Key_F10, KEY_F10 );
138         HANDLE( Key_F11, KEY_F11 );
139         HANDLE( Key_F12, KEY_F12 );
140         HANDLE( Key_PageUp, KEY_PAGEUP );
141         HANDLE( Key_PageDown, KEY_PAGEDOWN );
142         HANDLE( Key_Home, KEY_HOME );
143         HANDLE( Key_End, KEY_END );
144         HANDLE( Key_Insert, KEY_INSERT );
145         HANDLE( Key_Delete, KEY_DELETE );
146         HANDLE( Key_VolumeDown, KEY_VOLUME_DOWN);
147         HANDLE( Key_VolumeUp, KEY_VOLUME_UP );
148         HANDLE( Key_MediaPlay, KEY_MEDIA_PLAY_PAUSE );
149         HANDLE( Key_MediaStop, KEY_MEDIA_STOP );
150         HANDLE( Key_MediaPrevious, KEY_MEDIA_PREV_TRACK );
151         HANDLE( Key_MediaNext, KEY_MEDIA_NEXT_TRACK );
152
153     }
154     if( !found )
155     {
156         /* Force lowercase */
157         if( e->key() >= Qt::Key_A && e->key() <= Qt::Key_Z )
158             i_vlck += e->key() + 32;
159         /* Rest of the ascii range */
160         else if( e->key() >= Qt::Key_Space && e->key() <= Qt::Key_AsciiTilde )
161             i_vlck += e->key();
162     }
163     return i_vlck;
164 }
165
166 int qtWheelEventToVLCKey( QWheelEvent *e )
167 {
168     int i_vlck = 0;
169     /* Handle modifiers */
170     i_vlck |= qtKeyModifiersToVLC( e );
171     if ( e->delta() > 0 )
172         i_vlck |= KEY_MOUSEWHEELUP;
173     else
174         i_vlck |= KEY_MOUSEWHEELDOWN;
175     return i_vlck;
176 }
177
178 QString VLCKeyToString( int val )
179 {
180     QString r = "";
181     if( val & KEY_MODIFIER_CTRL )
182         r+= "Ctrl+";
183     if( val & KEY_MODIFIER_ALT )
184         r+= "Alt+";
185     if( val & KEY_MODIFIER_SHIFT )
186         r+= "Shift+";
187
188     unsigned int i_keys = sizeof(vlc_keys)/sizeof(key_descriptor_t);
189     for( unsigned int i = 0; i< i_keys; i++ )
190     {
191         if( vlc_keys[i].i_key_code == (val& ~KEY_MODIFIER) )
192         {
193             r+= vlc_keys[i].psz_key_string;
194         }
195     }
196     return r;
197 }
198