]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/customwidgets.cpp
Merge branch 'master' into lpcm_encoder
[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 <QColorGroup>
36 #include <QRect>
37 #include <QKeyEvent>
38 #include <QWheelEvent>
39 #include <QHBoxLayout>
40 #include <QStyle>
41 #include <QStyleOption>
42 #include <vlc_intf_strings.h>
43 #include <vlc_keys.h>
44 #include <wctype.h> /* twolower() */
45
46 ClickLineEdit::ClickLineEdit( const QString &msg, QWidget *parent) : QLineEdit( parent )
47 {
48     mDrawClickMsg = true;
49     setClickMessage( msg );
50 }
51
52 void ClickLineEdit::setClickMessage( const QString &msg )
53 {
54     mClickMessage = msg;
55     repaint();
56 }
57
58
59 void ClickLineEdit::setText( const QString &txt )
60 {
61     mDrawClickMsg = txt.isEmpty();
62     repaint();
63     QLineEdit::setText( txt );
64 }
65
66 void ClickLineEdit::paintEvent( QPaintEvent *pe )
67 {
68     QLineEdit::paintEvent( pe );
69     if ( mDrawClickMsg == true && !hasFocus() ) {
70         QPainter p( this );
71         QPen tmp = p.pen();
72         p.setPen( palette().color( QPalette::Disabled, QPalette::Text ) );
73         QRect cr = contentsRect();
74         // Add two pixel margin on the left side
75         cr.setLeft( cr.left() + 3 );
76         p.drawText( cr, Qt::AlignLeft | Qt::AlignVCenter, mClickMessage );
77         p.setPen( tmp );
78         p.end();
79     }
80 }
81
82 void ClickLineEdit::dropEvent( QDropEvent *ev )
83 {
84     mDrawClickMsg = false;
85     QLineEdit::dropEvent( ev );
86 }
87
88 void ClickLineEdit::focusInEvent( QFocusEvent *ev )
89 {
90     if ( mDrawClickMsg == true ) {
91         mDrawClickMsg = false;
92         repaint();
93     }
94     QLineEdit::focusInEvent( ev );
95 }
96
97 void ClickLineEdit::focusOutEvent( QFocusEvent *ev )
98 {
99     if ( text().isEmpty() ) {
100         mDrawClickMsg = true;
101         repaint();
102     }
103     QLineEdit::focusOutEvent( ev );
104 }
105
106 QVLCFramelessButton::QVLCFramelessButton( QWidget *parent )
107   : QPushButton( parent )
108 {
109     setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
110 }
111
112 void QVLCFramelessButton::paintEvent( QPaintEvent * )
113 {
114     QPainter painter( this );
115     QPixmap pix = icon().pixmap( size() );
116     QPoint pos( (width() - pix.width()) / 2, (height() - pix.height()) / 2 );
117     painter.drawPixmap( QRect( pos.x(), pos.y(), pix.width(), pix.height() ), pix );
118 }
119
120 QSize QVLCFramelessButton::sizeHint() const
121 {
122     return iconSize();
123 }
124
125 SearchLineEdit::SearchLineEdit( QWidget *parent ) : QLineEdit( parent )
126 {
127     clearButton = new QVLCFramelessButton( this );
128     clearButton->setIcon( QIcon( ":/toolbar/clear" ) );
129     clearButton->setIconSize( QSize( 16, 16 ) );
130     clearButton->setCursor( Qt::ArrowCursor );
131     clearButton->setToolTip( qfu(vlc_pgettext("Tooltip|Clear", "Clear")) );
132     clearButton->hide();
133
134     CONNECT( clearButton, clicked(), this, clear() );
135
136     int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth, 0, this );
137
138     QFontMetrics metrics( font() );
139     QString styleSheet = QString( "min-height: %1px; "
140                                   "padding-top: 1px; "
141                                   "padding-bottom: 1px; "
142                                   "padding-right: %2px;" )
143                                   .arg( metrics.height() + ( 2 * frameWidth ) )
144                                   .arg( clearButton->sizeHint().width() + 1 );
145     setStyleSheet( styleSheet );
146
147     setMessageVisible( true );
148
149     CONNECT( this, textEdited( const QString& ),
150              this, updateText( const QString& ) );
151 }
152
153 void SearchLineEdit::clear()
154 {
155     setText( QString() );
156     clearButton->hide();
157     setMessageVisible( true );
158 }
159
160 void SearchLineEdit::setMessageVisible( bool on )
161 {
162     message = on;
163     repaint();
164     return;
165 }
166
167 void SearchLineEdit::updateText( const QString& text )
168 {
169     clearButton->setVisible( !text.isEmpty() );
170 }
171
172 void SearchLineEdit::resizeEvent ( QResizeEvent * event )
173 {
174   QLineEdit::resizeEvent( event );
175   int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this);
176   clearButton->resize( clearButton->sizeHint().width(), height() );
177   clearButton->move( width() - clearButton->width() - frameWidth, 0 );
178 }
179
180 void SearchLineEdit::focusInEvent( QFocusEvent *event )
181 {
182   if( message )
183   {
184       setMessageVisible( false );
185   }
186   QLineEdit::focusInEvent( event );
187 }
188
189 void SearchLineEdit::focusOutEvent( QFocusEvent *event )
190 {
191   if( text().isEmpty() )
192   {
193       setMessageVisible( true );
194   }
195   QLineEdit::focusOutEvent( event );
196 }
197
198 void SearchLineEdit::paintEvent( QPaintEvent *event )
199 {
200   QLineEdit::paintEvent( event );
201   if( !message ) return;
202   QStyleOption option;
203   option.initFrom( this );
204   QRect rect = style()->subElementRect( QStyle::SE_LineEditContents, &option, this )
205                   .adjusted( 3, 0, clearButton->width() + 1, 0 );
206   QPainter painter( this );
207   painter.setPen( palette().color( QPalette::Disabled, QPalette::Text ) );
208   painter.drawText( rect, Qt::AlignLeft | Qt::AlignVCenter, qtr( I_PL_FILTER ) );
209 }
210
211
212 QVLCElidingLabel::QVLCElidingLabel( const QString &s, Qt::TextElideMode mode, QWidget * parent )
213   : elideMode( mode ), QLabel( s, parent )
214 { }
215
216 void QVLCElidingLabel::setElideMode( Qt::TextElideMode mode )
217 {
218     elideMode = mode;
219     repaint();
220 }
221
222 void QVLCElidingLabel::paintEvent( QPaintEvent * event )
223 {
224     QPainter p( this );
225     int space = frameWidth() + margin();
226     QRect r = rect().adjusted( space, space, -space, -space );
227     p.drawText( r, fontMetrics().elidedText( text(), elideMode, r.width() ), alignment() );
228 }
229
230 QString DebugLevelSpinBox::textFromValue( int v ) const
231 {
232     QString const texts[] = {
233     /* Note that min level 0 is 'errors' in Qt Ui
234        FIXME: fix debug levels accordingly to documentation */
235     /*  qtr("infos"),*/
236         qtr("errors"),
237         qtr("warnings"),
238         qtr("debug")
239     };
240     if ( v < 0 ) v = 0;
241     if ( v >= 2 ) v = 2;
242
243     return QString( "%1 (%2)" ).arg( v ).arg( texts[v] );
244 }
245
246 int DebugLevelSpinBox::mapTextToValue ( bool *ok )
247 {
248     int parsedvalue = cleanText().toInt();
249     /* fix range */
250     *ok = ( parsedvalue < 0 || parsedvalue > 2 )? FALSE : TRUE;
251     return parsedvalue;
252 }
253
254 /***************************************************************************
255  * Hotkeys converters
256  ***************************************************************************/
257 int qtKeyModifiersToVLC( QInputEvent* e )
258 {
259     int i_keyModifiers = 0;
260     if( e->modifiers() & Qt::ShiftModifier ) i_keyModifiers |= KEY_MODIFIER_SHIFT;
261     if( e->modifiers() & Qt::AltModifier ) i_keyModifiers |= KEY_MODIFIER_ALT;
262     if( e->modifiers() & Qt::ControlModifier ) i_keyModifiers |= KEY_MODIFIER_CTRL;
263     if( e->modifiers() & Qt::MetaModifier ) i_keyModifiers |= KEY_MODIFIER_META;
264     return i_keyModifiers;
265 }
266
267 typedef struct
268 {
269     int      qt;
270     uint32_t vlc;
271 } vlc_qt_key_t;
272
273 static const vlc_qt_key_t keys[] =
274 {
275     { Qt::Key_Escape,                KEY_ESC },
276     { Qt::Key_Tab,                   '\t', },
277     // Qt::Key_Backtab
278     { Qt::Key_Backspace,             '\b' },
279     { Qt::Key_Return,                '\r' },
280     { Qt::Key_Enter,                 '\r' }, // numeric pad
281     { Qt::Key_Insert,                KEY_INSERT },
282     { Qt::Key_Delete,                KEY_DELETE },
283     // Qt::Key_Pause
284     // Qt::Key_Print
285     // Qt::Key_SysReq
286     // Qt::Key_Clear
287     { Qt::Key_Home,                  KEY_HOME },
288     { Qt::Key_End,                   KEY_END },
289     { Qt::Key_Left,                  KEY_LEFT },
290     { Qt::Key_Up,                    KEY_UP },
291     { Qt::Key_Right,                 KEY_RIGHT },
292     { Qt::Key_Down,                  KEY_DOWN },
293     { Qt::Key_PageUp,                KEY_PAGEUP },
294     { Qt::Key_PageDown,              KEY_PAGEDOWN },
295     // Qt::Key_Shift
296     // Qt::Key_Control
297     // Qt::Key_Meta
298     // Qt::Key_Alt
299     // Qt::Key_CapsLock
300     // Qt::Key_NumLock
301     // Qt::Key_ScrollLock
302     /* F1 - F35 - Qt goes to F35, VLC stops at F12 */
303     { Qt::Key_F1,                    KEY_F1 },
304     { Qt::Key_F2,                    KEY_F2 },
305     { Qt::Key_F3,                    KEY_F3 },
306     { Qt::Key_F4,                    KEY_F4 },
307     { Qt::Key_F5,                    KEY_F5 },
308     { Qt::Key_F6,                    KEY_F6 },
309     { Qt::Key_F7,                    KEY_F7 },
310     { Qt::Key_F8,                    KEY_F8 },
311     { Qt::Key_F9,                    KEY_F9 },
312     { Qt::Key_F10,                   KEY_F10 },
313     { Qt::Key_F11,                   KEY_F11 },
314     { Qt::Key_F12,                   KEY_F12 },
315     // Qt::Key_Super_L
316     // Qt::Key_Super_R
317     { Qt::Key_Menu,                  KEY_MENU },
318     // Qt::Key_Hyper_L
319     // Qt::Key_Hyper_R
320     // Qt::Key_Help
321     // Qt::Key_Direction_L
322     // Qt::Key_Direction_R
323
324     // Qt::Key_Multi_key
325     // Qt::Key_Codeinput
326     // Qt::Key_SingleCandidate
327     // Qt::Key_MultipleCandidate
328     // Qt::Key_PreviousCandidate
329     // Qt::Key_Mode_switch
330     // Qt::Key_Kanji
331     // Qt::Key_Muhenkan
332     // Qt::Key_Henkan
333     // Qt::Key_Romaji
334     // Qt::Key_Hiragana
335     // Qt::Key_Katakana
336     // Qt::Key_Hiragana_Katakana
337     // Qt::Key_Zenkaku
338     // Qt::Key_Hankaku
339     // Qt::Key_Zenkaku_Hankaku
340     // Qt::Key_Touroku
341     // Qt::Key_Massyo
342     // Qt::Key_Kana_Lock
343     // Qt::Key_Kana_Shift
344     // Qt::Key_Eisu_Shift
345     // Qt::Key_Eisu_toggle
346     // Qt::Key_Hangul
347     // Qt::Key_Hangul_Start
348     // Qt::Key_Hangul_End
349     // Qt::Key_Hangul_Hanja
350     // Qt::Key_Hangul_Jamo
351     // Qt::Key_Hangul_Romaja
352     // Qt::Key_Hangul_Jeonja
353     // Qt::Key_Hangul_Banja
354     // Qt::Key_Hangul_PreHanja
355     // Qt::Key_Hangul_PostHanja
356     // Qt::Key_Hangul_Special
357     // Qt::Key_Dead_Grave
358     // Qt::Key_Dead_Acute
359     // Qt::Key_Dead_Circumflex
360     // Qt::Key_Dead_Tilde
361     // Qt::Key_Dead_Macron
362     // Qt::Key_Dead_Breve
363     // Qt::Key_Dead_Abovedot
364     // Qt::Key_Dead_Diaeresis
365     // Qt::Key_Dead_Abovering
366     // Qt::Key_Dead_Doubleacute
367     // Qt::Key_Dead_Caron
368     // Qt::Key_Dead_Cedilla
369     // Qt::Key_Dead_Ogonek
370     // Qt::Key_Dead_Iota
371     // Qt::Key_Dead_Voiced_Sound
372     // Qt::Key_Dead_Semivoiced_Sound
373     // Qt::Key_Dead_Belowdot
374     // Qt::Key_Dead_Hook
375     // Qt::Key_Dead_Horn
376     { Qt::Key_Back,                  KEY_BROWSER_BACK },
377     { Qt::Key_Forward,               KEY_BROWSER_FORWARD },
378     { Qt::Key_Stop,                  KEY_BROWSER_STOP },
379     { Qt::Key_Refresh,               KEY_BROWSER_REFRESH },
380     { Qt::Key_VolumeDown,            KEY_VOLUME_DOWN },
381     { Qt::Key_VolumeMute,            KEY_VOLUME_MUTE },
382     { Qt::Key_VolumeUp,              KEY_VOLUME_UP },
383     // Qt::Key_BassBoost
384     // Qt::Key_BassUp
385     // Qt::Key_BassDown
386     // Qt::Key_TrebleUp
387     // Qt::Key_TrebleDown
388     { Qt::Key_MediaPlay,             KEY_MEDIA_PLAY_PAUSE },
389     { Qt::Key_MediaStop,             KEY_MEDIA_STOP },
390     { Qt::Key_MediaPrevious,         KEY_MEDIA_PREV_TRACK },
391     { Qt::Key_MediaNext,             KEY_MEDIA_NEXT_TRACK },
392     // Qt::Key_MediaRecord
393     { Qt::Key_HomePage,              KEY_BROWSER_HOME },
394     { Qt::Key_Favorites,             KEY_BROWSER_FAVORITES },
395     { Qt::Key_Search,                KEY_BROWSER_SEARCH },
396     // Qt::Key_Standby
397     // Qt::Key_OpenUrl
398     // Qt::Key_LaunchMail
399     // Qt::Key_LaunchMedia
400     /* Qt::Key_Launch0 through Qt::Key_LaunchF */
401     // Qt::Key_MediaLast
402 };
403
404 static int keycmp( const void *a, const void *b )
405 {
406     const int *q = (const int *)a;
407     const vlc_qt_key_t *m = (const vlc_qt_key_t *)b;
408
409     return *q - m->qt;
410 }
411
412 int qtEventToVLCKey( QKeyEvent *e )
413 {
414     int qtk = e->key();
415     uint32_t i_vlck = 0;
416
417     if( qtk <= 0xff )
418         /* VLC and X11 use lowercase whereas Qt uses uppercase */
419 #if defined( __STDC_ISO_10646__ ) || defined( _WIN32 ) || defined( __APPLE__ )
420         i_vlck = towlower( qtk );
421 #else
422 # error FIXME
423 #endif
424     else
425     {
426         const vlc_qt_key_t *map;
427
428         map = (const vlc_qt_key_t *)
429               bsearch( &qtk, (const void *)keys, sizeof(keys)/sizeof(keys[0]),
430                        sizeof(*keys), keycmp );
431         if( map != NULL )
432             i_vlck = map->vlc;
433     }
434
435     /* Handle modifiers */
436     i_vlck |= qtKeyModifiersToVLC( e );
437     return i_vlck;
438 }
439
440 int qtWheelEventToVLCKey( QWheelEvent *e )
441 {
442     int i_vlck = 0;
443     /* Handle modifiers */
444     i_vlck |= qtKeyModifiersToVLC( e );
445     if ( e->delta() > 0 )
446         i_vlck |= KEY_MOUSEWHEELUP;
447     else
448         i_vlck |= KEY_MOUSEWHEELDOWN;
449     return i_vlck;
450 }
451
452 QString VLCKeyToString( int val )
453 {
454     char *base = KeyToString (val & ~KEY_MODIFIER);
455
456     QString r = "";
457     if( val & KEY_MODIFIER_CTRL )
458         r+= qfu( "Ctrl+" );
459     if( val & KEY_MODIFIER_ALT )
460         r+= qfu( "Alt+" );
461     if( val & KEY_MODIFIER_SHIFT )
462         r+= qfu( "Shift+" );
463     if( val & KEY_MODIFIER_META )
464         r+= qfu( "Meta+" );
465
466     if (base)
467     {
468         r += qfu( base );
469         free( base );
470     }
471     else
472         r += qtr( "Unset" );
473     return r;
474 }
475