]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/customwidgets.cpp
Qt4 - MouseWheel support - patch by Sergey Volk.
[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 #include <QKeyEvent>
33
34 #include <vlc_keys.h>
35
36 ClickLineEdit::ClickLineEdit( const QString &msg, QWidget *parent) : QLineEdit( parent )
37 {
38     mDrawClickMsg = true;
39     setClickMessage( msg );
40 }
41
42 void ClickLineEdit::setClickMessage( const QString &msg )
43 {
44     mClickMessage = msg;
45     repaint();
46 }
47
48
49 void ClickLineEdit::setText( const QString &txt )
50 {
51     mDrawClickMsg = txt.isEmpty();
52     repaint();
53     QLineEdit::setText( txt );
54 }
55
56 void ClickLineEdit::paintEvent( QPaintEvent *pe )
57 {
58     QPainter p( this );
59     QLineEdit::paintEvent( pe );
60     if ( mDrawClickMsg == true && !hasFocus() ) {
61         QPen tmp = p.pen();
62         p.setPen( palette().color( QPalette::Disabled, QPalette::Text ) );
63         QRect cr = contentsRect();
64         // Add two pixel margin on the left side
65         cr.setLeft( cr.left() + 3 );
66         p.drawText( cr, Qt::AlignLeft | Qt::AlignVCenter, mClickMessage );
67         p.setPen( tmp );
68     }
69 }
70
71 void ClickLineEdit::dropEvent( QDropEvent *ev )
72 {
73     mDrawClickMsg = false;
74     QLineEdit::dropEvent( ev );
75 }
76
77
78 void ClickLineEdit::focusInEvent( QFocusEvent *ev )
79 {
80     if ( mDrawClickMsg == true ) {
81         mDrawClickMsg = false;
82         repaint();
83     }
84     QLineEdit::focusInEvent( ev );
85 }
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
147     }
148     if( !found )
149     {
150         /* Force lowercase */
151         if( e->key() >= Qt::Key_A && e->key() <= Qt::Key_Z )
152             i_vlck += e->key() + 32;
153         /* Rest of the ascii range */
154         else if( e->key() >= Qt::Key_Space && e->key() <= Qt::Key_AsciiTilde )
155             i_vlck += e->key();
156     }
157     return i_vlck;
158 }
159
160 QString VLCKeyToString( int val )
161 {
162     QString r = "";
163     if( val & KEY_MODIFIER_CTRL )
164         r+= "Ctrl+";
165     if( val & KEY_MODIFIER_ALT )
166         r+= "Alt+";
167     if( val & KEY_MODIFIER_SHIFT )
168         r+= "Shift+";
169
170     unsigned int i_keys = sizeof(vlc_keys)/sizeof(key_descriptor_t);
171     for( unsigned int i = 0; i< i_keys; i++ )
172     {
173         if( vlc_keys[i].i_key_code == (val& ~KEY_MODIFIER) )
174         {
175             r+= vlc_keys[i].psz_key_string;
176         }
177     }
178     return r;
179 }