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