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