]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/customwidgets.cpp
Qt4: render artists/title on pictureflow
[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     CONNECT( this, editingFinished(),
153              this, searchEditingFinished() );
154
155 }
156
157 void SearchLineEdit::clear()
158 {
159     setText( QString() );
160     clearButton->hide();
161     setMessageVisible( true );
162 }
163
164 void SearchLineEdit::setMessageVisible( bool on )
165 {
166     message = on;
167     repaint();
168     return;
169 }
170
171 void SearchLineEdit::updateText( const QString& text )
172 {
173     clearButton->setVisible( !text.isEmpty() );
174 }
175
176 void SearchLineEdit::resizeEvent ( QResizeEvent * event )
177 {
178   QLineEdit::resizeEvent( event );
179   int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this);
180   clearButton->resize( clearButton->sizeHint().width(), height() );
181   clearButton->move( width() - clearButton->width() - frameWidth, 0 );
182 }
183
184 void SearchLineEdit::focusInEvent( QFocusEvent *event )
185 {
186   if( message )
187   {
188       setMessageVisible( false );
189   }
190   QLineEdit::focusInEvent( event );
191 }
192
193 void SearchLineEdit::focusOutEvent( QFocusEvent *event )
194 {
195   if( text().isEmpty() )
196   {
197       setMessageVisible( true );
198   }
199   QLineEdit::focusOutEvent( event );
200 }
201
202 void SearchLineEdit::paintEvent( QPaintEvent *event )
203 {
204   QLineEdit::paintEvent( event );
205   if( !message ) return;
206   QStyleOption option;
207   option.initFrom( this );
208   QRect rect = style()->subElementRect( QStyle::SE_LineEditContents, &option, this )
209                   .adjusted( 3, 0, clearButton->width() + 1, 0 );
210   QPainter painter( this );
211   painter.setPen( palette().color( QPalette::Disabled, QPalette::Text ) );
212   painter.drawText( rect, Qt::AlignLeft | Qt::AlignVCenter, qtr( I_PL_FILTER ) );
213 }
214
215 void SearchLineEdit::searchEditingFinished()
216 {
217     emit searchDelayedChanged( text() );
218 }
219
220
221 QVLCElidingLabel::QVLCElidingLabel( const QString &s, Qt::TextElideMode mode, QWidget * parent )
222   : elideMode( mode ), QLabel( s, parent )
223 { }
224
225 void QVLCElidingLabel::setElideMode( Qt::TextElideMode mode )
226 {
227     elideMode = mode;
228     repaint();
229 }
230
231 void QVLCElidingLabel::paintEvent( QPaintEvent * event )
232 {
233     QPainter p( this );
234     int space = frameWidth() + margin();
235     QRect r = rect().adjusted( space, space, -space, -space );
236     p.drawText( r, fontMetrics().elidedText( text(), elideMode, r.width() ), alignment() );
237 }
238
239 QString DebugLevelSpinBox::textFromValue( int v ) const
240 {
241     QString const texts[] = {
242     /* Note that min level 0 is 'errors' in Qt Ui
243        FIXME: fix debug levels accordingly to documentation */
244     /*  qtr("infos"),*/
245         qtr("errors"),
246         qtr("warnings"),
247         qtr("debug")
248     };
249     if ( v < 0 ) v = 0;
250     if ( v >= 2 ) v = 2;
251
252     return QString( "%1 (%2)" ).arg( v ).arg( texts[v] );
253 }
254
255 int DebugLevelSpinBox::mapTextToValue ( bool *ok )
256 {
257     int parsedvalue = cleanText().toInt();
258     /* fix range */
259     *ok = ( parsedvalue < 0 || parsedvalue > 2 )? FALSE : TRUE;
260     return parsedvalue;
261 }
262
263 /***************************************************************************
264  * Hotkeys converters
265  ***************************************************************************/
266 int qtKeyModifiersToVLC( QInputEvent* e )
267 {
268     int i_keyModifiers = 0;
269     if( e->modifiers() & Qt::ShiftModifier ) i_keyModifiers |= KEY_MODIFIER_SHIFT;
270     if( e->modifiers() & Qt::AltModifier ) i_keyModifiers |= KEY_MODIFIER_ALT;
271     if( e->modifiers() & Qt::ControlModifier ) i_keyModifiers |= KEY_MODIFIER_CTRL;
272     if( e->modifiers() & Qt::MetaModifier ) i_keyModifiers |= KEY_MODIFIER_META;
273     return i_keyModifiers;
274 }
275
276 typedef struct
277 {
278     int      qt;
279     uint32_t vlc;
280 } vlc_qt_key_t;
281
282 static const vlc_qt_key_t keys[] =
283 {
284     { Qt::Key_Escape,                KEY_ESC },
285     { Qt::Key_Tab,                   '\t', },
286     // Qt::Key_Backtab
287     { Qt::Key_Backspace,             '\b' },
288     { Qt::Key_Return,                '\r' },
289     { Qt::Key_Enter,                 '\r' }, // numeric pad
290     { Qt::Key_Insert,                KEY_INSERT },
291     { Qt::Key_Delete,                KEY_DELETE },
292     // Qt::Key_Pause
293     // Qt::Key_Print
294     // Qt::Key_SysReq
295     // Qt::Key_Clear
296     { Qt::Key_Home,                  KEY_HOME },
297     { Qt::Key_End,                   KEY_END },
298     { Qt::Key_Left,                  KEY_LEFT },
299     { Qt::Key_Up,                    KEY_UP },
300     { Qt::Key_Right,                 KEY_RIGHT },
301     { Qt::Key_Down,                  KEY_DOWN },
302     { Qt::Key_PageUp,                KEY_PAGEUP },
303     { Qt::Key_PageDown,              KEY_PAGEDOWN },
304     // Qt::Key_Shift
305     // Qt::Key_Control
306     // Qt::Key_Meta
307     // Qt::Key_Alt
308     // Qt::Key_CapsLock
309     // Qt::Key_NumLock
310     // Qt::Key_ScrollLock
311     /* F1 - F35 - Qt goes to F35, VLC stops at F12 */
312     { Qt::Key_F1,                    KEY_F1 },
313     { Qt::Key_F2,                    KEY_F2 },
314     { Qt::Key_F3,                    KEY_F3 },
315     { Qt::Key_F4,                    KEY_F4 },
316     { Qt::Key_F5,                    KEY_F5 },
317     { Qt::Key_F6,                    KEY_F6 },
318     { Qt::Key_F7,                    KEY_F7 },
319     { Qt::Key_F8,                    KEY_F8 },
320     { Qt::Key_F9,                    KEY_F9 },
321     { Qt::Key_F10,                   KEY_F10 },
322     { Qt::Key_F11,                   KEY_F11 },
323     { Qt::Key_F12,                   KEY_F12 },
324     // Qt::Key_Super_L
325     // Qt::Key_Super_R
326     { Qt::Key_Menu,                  KEY_MENU },
327     // Qt::Key_Hyper_L
328     // Qt::Key_Hyper_R
329     // Qt::Key_Help
330     // Qt::Key_Direction_L
331     // Qt::Key_Direction_R
332
333     // Qt::Key_Multi_key
334     // Qt::Key_Codeinput
335     // Qt::Key_SingleCandidate
336     // Qt::Key_MultipleCandidate
337     // Qt::Key_PreviousCandidate
338     // Qt::Key_Mode_switch
339     // Qt::Key_Kanji
340     // Qt::Key_Muhenkan
341     // Qt::Key_Henkan
342     // Qt::Key_Romaji
343     // Qt::Key_Hiragana
344     // Qt::Key_Katakana
345     // Qt::Key_Hiragana_Katakana
346     // Qt::Key_Zenkaku
347     // Qt::Key_Hankaku
348     // Qt::Key_Zenkaku_Hankaku
349     // Qt::Key_Touroku
350     // Qt::Key_Massyo
351     // Qt::Key_Kana_Lock
352     // Qt::Key_Kana_Shift
353     // Qt::Key_Eisu_Shift
354     // Qt::Key_Eisu_toggle
355     // Qt::Key_Hangul
356     // Qt::Key_Hangul_Start
357     // Qt::Key_Hangul_End
358     // Qt::Key_Hangul_Hanja
359     // Qt::Key_Hangul_Jamo
360     // Qt::Key_Hangul_Romaja
361     // Qt::Key_Hangul_Jeonja
362     // Qt::Key_Hangul_Banja
363     // Qt::Key_Hangul_PreHanja
364     // Qt::Key_Hangul_PostHanja
365     // Qt::Key_Hangul_Special
366     // Qt::Key_Dead_Grave
367     // Qt::Key_Dead_Acute
368     // Qt::Key_Dead_Circumflex
369     // Qt::Key_Dead_Tilde
370     // Qt::Key_Dead_Macron
371     // Qt::Key_Dead_Breve
372     // Qt::Key_Dead_Abovedot
373     // Qt::Key_Dead_Diaeresis
374     // Qt::Key_Dead_Abovering
375     // Qt::Key_Dead_Doubleacute
376     // Qt::Key_Dead_Caron
377     // Qt::Key_Dead_Cedilla
378     // Qt::Key_Dead_Ogonek
379     // Qt::Key_Dead_Iota
380     // Qt::Key_Dead_Voiced_Sound
381     // Qt::Key_Dead_Semivoiced_Sound
382     // Qt::Key_Dead_Belowdot
383     // Qt::Key_Dead_Hook
384     // Qt::Key_Dead_Horn
385     { Qt::Key_Back,                  KEY_BROWSER_BACK },
386     { Qt::Key_Forward,               KEY_BROWSER_FORWARD },
387     { Qt::Key_Stop,                  KEY_BROWSER_STOP },
388     { Qt::Key_Refresh,               KEY_BROWSER_REFRESH },
389     { Qt::Key_VolumeDown,            KEY_VOLUME_DOWN },
390     { Qt::Key_VolumeMute,            KEY_VOLUME_MUTE },
391     { Qt::Key_VolumeUp,              KEY_VOLUME_UP },
392     // Qt::Key_BassBoost
393     // Qt::Key_BassUp
394     // Qt::Key_BassDown
395     // Qt::Key_TrebleUp
396     // Qt::Key_TrebleDown
397     { Qt::Key_MediaPlay,             KEY_MEDIA_PLAY_PAUSE },
398     { Qt::Key_MediaStop,             KEY_MEDIA_STOP },
399     { Qt::Key_MediaPrevious,         KEY_MEDIA_PREV_TRACK },
400     { Qt::Key_MediaNext,             KEY_MEDIA_NEXT_TRACK },
401     // Qt::Key_MediaRecord
402     { Qt::Key_HomePage,              KEY_BROWSER_HOME },
403     { Qt::Key_Favorites,             KEY_BROWSER_FAVORITES },
404     { Qt::Key_Search,                KEY_BROWSER_SEARCH },
405     // Qt::Key_Standby
406     // Qt::Key_OpenUrl
407     // Qt::Key_LaunchMail
408     // Qt::Key_LaunchMedia
409     /* Qt::Key_Launch0 through Qt::Key_LaunchF */
410     // Qt::Key_MediaLast
411 };
412
413 static int keycmp( const void *a, const void *b )
414 {
415     const int *q = (const int *)a;
416     const vlc_qt_key_t *m = (const vlc_qt_key_t *)b;
417
418     return *q - m->qt;
419 }
420
421 int qtEventToVLCKey( QKeyEvent *e )
422 {
423     int qtk = e->key();
424     uint32_t i_vlck = 0;
425
426     if( qtk <= 0xff )
427         /* VLC and X11 use lowercase whereas Qt uses uppercase */
428 #if defined( __STDC_ISO_10646__ ) || defined( _WIN32 ) || defined( __APPLE__ )
429         i_vlck = towlower( qtk );
430 #else
431 # error FIXME
432 #endif
433     else
434     {
435         const vlc_qt_key_t *map;
436
437         map = (const vlc_qt_key_t *)
438               bsearch( &qtk, (const void *)keys, sizeof(keys)/sizeof(keys[0]),
439                        sizeof(*keys), keycmp );
440         if( map != NULL )
441             i_vlck = map->vlc;
442     }
443
444     /* Handle modifiers */
445     i_vlck |= qtKeyModifiersToVLC( e );
446     return i_vlck;
447 }
448
449 int qtWheelEventToVLCKey( QWheelEvent *e )
450 {
451     int i_vlck = 0;
452     /* Handle modifiers */
453     i_vlck |= qtKeyModifiersToVLC( e );
454     if ( e->delta() > 0 )
455         i_vlck |= KEY_MOUSEWHEELUP;
456     else
457         i_vlck |= KEY_MOUSEWHEELDOWN;
458     return i_vlck;
459 }
460
461 QString VLCKeyToString( int val )
462 {
463     char *base = KeyToString (val & ~KEY_MODIFIER);
464
465     QString r = "";
466     if( val & KEY_MODIFIER_CTRL )
467         r+= qfu( "Ctrl+" );
468     if( val & KEY_MODIFIER_ALT )
469         r+= qfu( "Alt+" );
470     if( val & KEY_MODIFIER_SHIFT )
471         r+= qfu( "Shift+" );
472     if( val & KEY_MODIFIER_META )
473         r+= qfu( "Meta+" );
474
475     if (base)
476     {
477         r += qfu( base );
478         free( base );
479     }
480     else
481         r += qtr( "Unset" );
482     return r;
483 }
484