]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/customwidgets.hpp
Qt: add new widgets: AnimatedIcon & SpinningIcon
[vlc] / modules / gui / qt4 / util / customwidgets.hpp
1 /*****************************************************************************
2  * customwidgets.h: 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 #ifndef _CUSTOMWIDGETS_H_
28 #define _CUSTOMWIDGETS_H_
29
30 #include <QLineEdit>
31 #include <QPushButton>
32 #include <QLabel>
33 #include <QStackedWidget>
34 #include <QSpinBox>
35 #include <QList>
36 #include <QTimer>
37 class QPixmap;
38
39 class QVLCFramelessButton : public QPushButton
40 {
41     Q_OBJECT
42 public:
43     QVLCFramelessButton( QWidget *parent = NULL );
44     virtual QSize sizeHint() const { return iconSize(); }
45 protected:
46     virtual void paintEvent( QPaintEvent * event );
47 };
48
49
50 class QVLCElidingLabel : public QLabel
51 {
52 public:
53     QVLCElidingLabel( const QString &s = QString(),
54                       Qt::TextElideMode mode = Qt::ElideRight,
55                       QWidget * parent = NULL );
56     void setElideMode( Qt::TextElideMode );
57 protected:
58     virtual void paintEvent( QPaintEvent * event );
59 private:
60     Qt::TextElideMode elideMode;
61 };
62
63
64 class QVLCStackedWidget : public QStackedWidget
65 {
66 public:
67     QVLCStackedWidget( QWidget *parent ) : QStackedWidget( parent ) { }
68     QSize minimumSizeHint () const
69     {
70         return currentWidget() ? currentWidget()->minimumSizeHint() : QSize();
71     }
72 };
73
74 class DebugLevelSpinBox : public QSpinBox
75 {
76     Q_OBJECT
77 public:
78     DebugLevelSpinBox( QWidget *parent ) : QSpinBox( parent ) { };
79 protected:
80     virtual QString textFromValue( int ) const;
81     /* DebugLevelSpinBox is read-only */
82     virtual int valueFromText( const QString& ) const { return -1; }
83 };
84
85 class AnimatedIcon : public QLabel
86 {
87     /** An animated pixmap
88      * Use this widget to display an animated icon based on a series of
89      * pixmaps. The pixmaps will be stored in memory and should be kept small.
90      * First, create the widget, add frames and then start playing. Looping
91      * is supported.
92      * Frames #1 to #n are displayed at regular intervals when playing.
93      * Frame #0 is the idle frame, displayed when the icon is not animated.
94      * If not #0 frame has been specified, the last frame will be shown when
95      * idle.
96      **/
97
98     Q_OBJECT
99
100 public:
101     /** Create an empty AnimatedIcon */
102     AnimatedIcon( QWidget *parent );
103     virtual ~AnimatedIcon();
104
105     /** Adds a frame to play in the loop.
106      * @param pixmap The QPixmap to display. Data will be copied internally.
107      * @param index If -1, append the frame. If 0, replace the idle frame.
108      *              Otherwise, insert the frame at the given position.
109      **/
110     void addFrame( const QPixmap &pixmap, int index = -1 );
111
112     /** Play the animation (or restart it)
113      * @param loops Number of times to play the loop. 0 means stop, while -1
114      *              means play forever. When stopped, the frame #0 will be
115      *              displayed until play() is called again.
116      * @param interval Delay between frames, in milliseconds (minimum 20ms)
117      * @note If isPlaying() is true, then restart the animation from frame #1
118      **/
119     void play( int loops = 1, int interval = 200 );
120
121     /** Stop playback. Same as play(0). */
122     inline void stop()
123     {
124         play( 0 );
125     }
126
127     /** Is the animation currently running? */
128     inline bool isPlaying()
129     {
130         return mTimer.isActive();
131     }
132
133 private:
134     QTimer mTimer;
135     QPixmap *mIdleFrame;
136     QList<QPixmap*> mFrames; // Keeps deep copies of all the frames
137     int mCurrentFrame, mRemainingLoops;
138
139 private slots:
140     /** Slot connected to the timeout() signal of our internal timer */
141     void onTimerTick();
142 };
143
144 class SpinningIcon : public AnimatedIcon
145 {
146     /** This spinning icon, to the colors of the VLC cone, will show
147      * that there is some background activity running
148      **/
149
150     Q_OBJECT
151
152 public:
153     SpinningIcon( QWidget *parent, bool noIdleFrame = false );
154     virtual ~SpinningIcon();
155 };
156
157 /* VLC Key/Wheel hotkeys interactions */
158
159 class QKeyEvent;
160 class QWheelEvent;
161 class QInputEvent;
162
163 int qtKeyModifiersToVLC( QInputEvent* e );
164 int qtEventToVLCKey( QKeyEvent *e );
165 int qtWheelEventToVLCKey( QWheelEvent *e );
166 QString VLCKeyToString( int val );
167
168 #endif
169