]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/qvlcframe.hpp
update module LIST file.
[vlc] / modules / gui / qt4 / util / qvlcframe.hpp
1 /*****************************************************************************
2  * qvlcframe.hpp : A few helpers
3  *****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifndef _QVLCFRAME_H_
25 #define _QVLCFRAME_H_
26
27 #include <QWidget>
28 #include <QDialog>
29 #include <QSpacerItem>
30 #include <QHBoxLayout>
31 #include <QApplication>
32 #include <QSettings>
33 #include <QMainWindow>
34 #include <QPushButton>
35 #include <QKeyEvent>
36 #include <QDesktopWidget>
37
38 #include "qt4.hpp"
39 #include <vlc/vlc.h>
40 #include <vlc_charset.h>
41
42 class QVLCTools
43 {
44    public:
45        /*
46         use this function to save a widgets screen position
47         only for windows / dialogs which are floating, if a
48         window is docked into an other - don't all this function
49         or it may write garbage to position info!
50        */
51        static void saveWidgetPosition(QSettings *settings, QWidget *widget)
52        {
53          settings->setValue("geometry", widget->saveGeometry());
54        }
55        static void saveWidgetPosition(QString configName, QWidget *widget)
56        {
57          QSettings *settings = new QSettings("vlc", "vlc-qt-interface");
58          settings->beginGroup( configName );
59          QVLCTools::saveWidgetPosition(settings, widget);
60          settings->endGroup();
61          delete settings;
62        }
63
64
65        /*
66          use this method only for restoring window state of non docked
67          windows!
68        */
69        static vlc_bool_t restoreWidgetPosition(QSettings *settings,
70                                            QWidget *widget,
71                                            QSize defSize = QSize( 0, 0 ),
72                                            QPoint defPos = QPoint( 0, 0 ))
73        {
74           if(!widget->restoreGeometry(settings->value("geometry")
75                                       .toByteArray()))
76           {
77             widget->move(defPos);
78             widget->resize(defSize);
79
80             if(defPos.x() == 0 && defPos.y()==0)
81                centerWidgetOnScreen(widget);
82             return VLC_TRUE;
83           }
84           return VLC_FALSE;
85        }
86
87        static vlc_bool_t restoreWidgetPosition(QString configName,
88                                            QWidget *widget,
89                                            QSize defSize = QSize( 0, 0 ),
90                                            QPoint defPos = QPoint( 0, 0 ) )
91        {
92          QSettings *settings = new QSettings( "vlc", "vlc-qt-interface" );
93          settings->beginGroup( configName );
94          vlc_bool_t defaultUsed = QVLCTools::restoreWidgetPosition(settings,
95                                                                    widget,
96                                                                    defSize,
97                                                                    defPos);
98          settings->endGroup();
99          delete settings;
100
101          return defaultUsed;
102        }
103
104       /*
105         call this method for a window or dialog to show it centred on
106         current screen
107       */
108       static void centerWidgetOnScreen(QWidget *widget)
109       {
110          QDesktopWidget * const desktop = QApplication::desktop();
111          QRect screenRect = desktop->screenGeometry(widget);
112          QPoint p1 = widget->frameGeometry().center();
113
114          widget->move ( screenRect.center() - p1 );
115       }
116 };
117
118 class QVLCFrame : public QWidget
119 {
120 public:
121 #ifdef __APPLE__
122     QVLCFrame( intf_thread_t *_p_intf ) : QWidget( NULL, Qt::Window ), p_intf( _p_intf )
123 #else
124     QVLCFrame( intf_thread_t *_p_intf ) : QWidget( NULL ), p_intf( _p_intf )
125 #endif
126     {};
127     virtual ~QVLCFrame()   {};
128
129     void toggleVisible()
130     {
131         if( isVisible() ) hide();
132         else show();
133     }
134 protected:
135     intf_thread_t *p_intf;
136
137     void readSettings( QString name,
138                        QSize defSize = QSize( 0, 0 ),
139                        QPoint defPos = QPoint( 0, 0 ) )
140     {
141         QVLCTools::restoreWidgetPosition(name, this, defSize, defPos);
142     }
143
144     void writeSettings( QString name )
145     {
146         QVLCTools::saveWidgetPosition(name, this);
147     }
148
149     virtual void cancel()
150     {
151         hide();
152     }
153     virtual void close()
154     {
155         hide();
156     }
157     virtual void keyPressEvent( QKeyEvent *keyEvent )
158     {
159         if( keyEvent->key() == Qt::Key_Escape )
160         {
161             msg_Dbg( p_intf, "Escp Key pressed" );
162             cancel();
163         }
164         else if( keyEvent->key() == Qt::Key_Return )
165         {
166              msg_Dbg( p_intf, "Enter Key pressed" );
167              close();
168          }
169     }
170 };
171
172 class QVLCDialog : public QDialog
173 {
174 public:
175     QVLCDialog( QWidget* parent, intf_thread_t *_p_intf ) :
176                                     QDialog( parent ), p_intf( _p_intf )
177     {}
178     virtual ~QVLCDialog() {};
179     void toggleVisible()
180     {
181         if( isVisible() ) hide();
182         else show();
183     }
184
185 protected:
186     intf_thread_t *p_intf;
187
188     virtual void cancel()
189     {
190         hide();
191     }
192     virtual void close()
193     {
194         hide();
195     }
196     virtual void keyPressEvent( QKeyEvent *keyEvent )
197     {
198         if( keyEvent->key() == Qt::Key_Escape )
199         {
200             msg_Dbg( p_intf, "Escp Key pressed" );
201             cancel();
202         }
203         else if( keyEvent->key() == Qt::Key_Return )
204         {
205              msg_Dbg( p_intf, "Enter Key pressed" );
206              close();
207         }
208     }
209 };
210
211 class QVLCMW : public QMainWindow
212 {
213 public:
214     QVLCMW( intf_thread_t *_p_intf ) : QMainWindow( NULL ), p_intf( _p_intf )
215     {    }
216     virtual ~QVLCMW() {};
217     void toggleVisible()
218     {
219         if( isVisible() ) hide();
220         else show();
221     }
222 protected:
223     intf_thread_t *p_intf;
224     QSize mainSize;
225
226     void readSettings( QString name, QSize defSize )
227     {
228         QVLCTools::restoreWidgetPosition(name, this, defSize);
229     }
230
231     void readSettings( QString name )
232     {
233         QVLCTools::restoreWidgetPosition(name, this);
234     }
235
236     void readSettings( QSettings *settings )
237     {
238         QVLCTools::restoreWidgetPosition(settings, this);
239     }
240
241     void readSettings( QSettings *settings, QSize defSize)
242     {
243         QVLCTools::restoreWidgetPosition(settings, this, defSize);
244     }
245
246     void writeSettings(QString name )
247     {
248         QVLCTools::saveWidgetPosition(name, this);
249     }
250
251     void writeSettings(QSettings *settings )
252     {
253         QVLCTools::saveWidgetPosition(settings, this);
254     }
255
256 };
257
258 #endif