]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/qvlcframe.hpp
ddc271da3420946560fd732317eaac30fad29562
[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 <QMainWindow>
33 #include <QPushButton>
34 #include <QKeyEvent>
35 #include <QDesktopWidget>
36 #include <QSettings>
37
38 #include "qt4.hpp"
39
40 class QVLCTools
41 {
42    public:
43        /*
44         use this function to save a widgets screen position
45         only for windows / dialogs which are floating, if a
46         window is docked into an other - don't all this function
47         or it may write garbage to position info!
48        */
49        static void saveWidgetPosition( QSettings *settings, QWidget *widget)
50        {
51          settings->setValue("geometry", widget->saveGeometry());
52        }
53        static void saveWidgetPosition( intf_thread_t *p_intf,
54                                        QString configName,
55                                        QWidget *widget)
56        {
57          getSettings()->beginGroup( configName );
58          QVLCTools::saveWidgetPosition(getSettings(), widget);
59          getSettings()->endGroup();
60        }
61
62
63        /*
64          use this method only for restoring window state of non docked
65          windows!
66        */
67        static bool restoreWidgetPosition(QSettings *settings,
68                                            QWidget *widget,
69                                            QSize defSize = QSize( 0, 0 ),
70                                            QPoint defPos = QPoint( 0, 0 ))
71        {
72           if(!widget->restoreGeometry(settings->value("geometry")
73                                       .toByteArray()))
74           {
75             widget->move(defPos);
76             widget->resize(defSize);
77
78             if(defPos.x() == 0 && defPos.y()==0)
79                centerWidgetOnScreen(widget);
80             return true;
81           }
82           return false;
83        }
84
85        static bool restoreWidgetPosition( intf_thread_t *p_intf,
86                                            QString configName,
87                                            QWidget *widget,
88                                            QSize defSize = QSize( 0, 0 ),
89                                            QPoint defPos = QPoint( 0, 0 ) )
90        {
91          getSettings()->beginGroup( configName );
92          bool defaultUsed = QVLCTools::restoreWidgetPosition( getSettings(),
93                                                                    widget,
94                                                                    defSize,
95                                                                    defPos);
96          getSettings()->endGroup();
97
98          return defaultUsed;
99        }
100
101       /*
102         call this method for a window or dialog to show it centred on
103         current screen
104       */
105       static void centerWidgetOnScreen(QWidget *widget)
106       {
107          QDesktopWidget * const desktop = QApplication::desktop();
108          QRect screenRect = desktop->availableGeometry(widget);
109          QPoint p1 = widget->frameGeometry().center();
110
111          widget->move ( screenRect.center() - p1 );
112       }
113 };
114
115 class QVLCFrame : public QWidget
116 {
117 public:
118 #ifdef __APPLE__
119     QVLCFrame( intf_thread_t *_p_intf ) : QWidget( NULL, Qt::Window ), p_intf( _p_intf )
120 #else
121     QVLCFrame( intf_thread_t *_p_intf ) : QWidget( NULL ), p_intf( _p_intf )
122 #endif
123     {};
124     virtual ~QVLCFrame()   {};
125
126     void toggleVisible()
127     {
128         if( isVisible() ) hide();
129         else show();
130     }
131 protected:
132     intf_thread_t *p_intf;
133
134     void readSettings( QString name,
135                        QSize defSize = QSize( 1, 1 ),
136                        QPoint defPos = QPoint( 0, 0 ) )
137     {
138         QVLCTools::restoreWidgetPosition(p_intf, name, this, defSize, defPos);
139     }
140
141     void writeSettings( QString name )
142     {
143         QVLCTools::saveWidgetPosition( p_intf, name, this);
144     }
145
146     virtual void cancel()
147     {
148         hide();
149     }
150     virtual void close()
151     {
152         hide();
153     }
154     virtual void keyPressEvent( QKeyEvent *keyEvent )
155     {
156         if( keyEvent->key() == Qt::Key_Escape )
157         {
158             msg_Dbg( p_intf, "Escp Key pressed" );
159             cancel();
160         }
161         else if( keyEvent->key() == Qt::Key_Return )
162         {
163              msg_Dbg( p_intf, "Enter Key pressed" );
164              close();
165          }
166     }
167 };
168
169 class QVLCDialog : public QDialog
170 {
171 public:
172     QVLCDialog( QWidget* parent, intf_thread_t *_p_intf ) :
173                                     QDialog( parent ), p_intf( _p_intf )
174     {}
175     virtual ~QVLCDialog() {};
176     void toggleVisible()
177     {
178         if( isVisible() ) hide();
179         else show();
180     }
181
182 protected:
183     intf_thread_t *p_intf;
184
185     virtual void cancel()
186     {
187         hide();
188     }
189     virtual void close()
190     {
191         hide();
192     }
193     virtual void keyPressEvent( QKeyEvent *keyEvent )
194     {
195         if( keyEvent->key() == Qt::Key_Escape )
196         {
197             msg_Dbg( p_intf, "Escp Key pressed" );
198             cancel();
199         }
200         else if( keyEvent->key() == Qt::Key_Return )
201         {
202              msg_Dbg( p_intf, "Enter Key pressed" );
203              close();
204         }
205     }
206 };
207
208 class QVLCMW : public QMainWindow
209 {
210 public:
211     QVLCMW( intf_thread_t *_p_intf ) : QMainWindow( NULL ), p_intf( _p_intf )
212     {    }
213     virtual ~QVLCMW() {};
214     void toggleVisible()
215     {
216         if( isVisible() ) hide();
217         else show();
218     }
219 protected:
220     intf_thread_t *p_intf;
221     QSize mainSize;
222
223     void readSettings( QString name, QSize defSize )
224     {
225         QVLCTools::restoreWidgetPosition( p_intf, name, this, defSize);
226     }
227
228     void readSettings( QString name )
229     {
230         QVLCTools::restoreWidgetPosition( p_intf, name, this);
231     }
232     void readSettings( QSettings *settings )
233     {
234         QVLCTools::restoreWidgetPosition(settings, this);
235     }
236
237     void readSettings( QSettings *settings, QSize defSize)
238     {
239         QVLCTools::restoreWidgetPosition(settings, this, defSize);
240     }
241
242     void writeSettings(QString name )
243     {
244         QVLCTools::saveWidgetPosition( p_intf, name, this);
245     }
246     void writeSettings(QSettings *settings )
247     {
248         QVLCTools::saveWidgetPosition(settings, this);
249     }
250 };
251
252 #endif