]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/qvlcframe.hpp
Fix annoying bug that prevented Bookmark dialog to be shown at first time.
[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 #include <vlc_common.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( intf_thread_t *p_intf,
56                                        QString configName,
57                                        QWidget *widget)
58        {
59          getSettings()->beginGroup( configName );
60          QVLCTools::saveWidgetPosition(getSettings(), widget);
61          getSettings()->endGroup();
62        }
63
64
65        /*
66          use this method only for restoring window state of non docked
67          windows!
68        */
69        static bool 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 true;
83           }
84           return false;
85        }
86
87        static bool restoreWidgetPosition( intf_thread_t *p_intf,
88                                            QString configName,
89                                            QWidget *widget,
90                                            QSize defSize = QSize( 0, 0 ),
91                                            QPoint defPos = QPoint( 0, 0 ) )
92        {
93          getSettings()->beginGroup( configName );
94          bool defaultUsed = QVLCTools::restoreWidgetPosition( getSettings(),
95                                                                    widget,
96                                                                    defSize,
97                                                                    defPos);
98          getSettings()->endGroup();
99
100          return defaultUsed;
101        }
102
103       /*
104         call this method for a window or dialog to show it centred on
105         current screen
106       */
107       static void centerWidgetOnScreen(QWidget *widget)
108       {
109          QDesktopWidget * const desktop = QApplication::desktop();
110          QRect screenRect = desktop->availableGeometry(widget);
111          QPoint p1 = widget->frameGeometry().center();
112
113          widget->move ( screenRect.center() - p1 );
114       }
115 };
116
117 class QVLCFrame : public QWidget
118 {
119 public:
120 #ifdef __APPLE__
121     QVLCFrame( intf_thread_t *_p_intf ) : QWidget( NULL, Qt::Window ), p_intf( _p_intf )
122 #else
123     QVLCFrame( intf_thread_t *_p_intf ) : QWidget( NULL ), p_intf( _p_intf )
124 #endif
125     {};
126     virtual ~QVLCFrame()   {};
127
128     void toggleVisible()
129     {
130         if( isVisible() ) hide();
131         else show();
132     }
133 protected:
134     intf_thread_t *p_intf;
135
136     void readSettings( QString name,
137                        QSize defSize = QSize( 1, 1 ),
138                        QPoint defPos = QPoint( 0, 0 ) )
139     {
140         QVLCTools::restoreWidgetPosition(p_intf, name, this, defSize, defPos);
141     }
142
143     void writeSettings( QString name )
144     {
145         QVLCTools::saveWidgetPosition( p_intf, name, this);
146     }
147
148     virtual void cancel()
149     {
150         hide();
151     }
152     virtual void close()
153     {
154         hide();
155     }
156     virtual void keyPressEvent( QKeyEvent *keyEvent )
157     {
158         if( keyEvent->key() == Qt::Key_Escape )
159         {
160             msg_Dbg( p_intf, "Escp Key pressed" );
161             cancel();
162         }
163         else if( keyEvent->key() == Qt::Key_Return )
164         {
165              msg_Dbg( p_intf, "Enter Key pressed" );
166              close();
167          }
168     }
169 };
170
171 class QVLCDialog : public QDialog
172 {
173 public:
174     QVLCDialog( QWidget* parent, intf_thread_t *_p_intf ) :
175                                     QDialog( parent ), p_intf( _p_intf )
176     {}
177     virtual ~QVLCDialog() {};
178     void toggleVisible()
179     {
180         if( isVisible() ) hide();
181         else show();
182     }
183
184 protected:
185     intf_thread_t *p_intf;
186
187     virtual void cancel()
188     {
189         hide();
190     }
191     virtual void close()
192     {
193         hide();
194     }
195     virtual void keyPressEvent( QKeyEvent *keyEvent )
196     {
197         if( keyEvent->key() == Qt::Key_Escape )
198         {
199             msg_Dbg( p_intf, "Escp Key pressed" );
200             cancel();
201         }
202         else if( keyEvent->key() == Qt::Key_Return )
203         {
204              msg_Dbg( p_intf, "Enter Key pressed" );
205              close();
206         }
207     }
208 };
209
210 class QVLCMW : public QMainWindow
211 {
212 public:
213     QVLCMW( intf_thread_t *_p_intf ) : QMainWindow( NULL ), p_intf( _p_intf )
214     {    }
215     virtual ~QVLCMW() {};
216     void toggleVisible()
217     {
218         if( isVisible() ) hide();
219         else show();
220     }
221 protected:
222     intf_thread_t *p_intf;
223     QSize mainSize;
224
225     void readSettings( QString name, QSize defSize )
226     {
227         QVLCTools::restoreWidgetPosition( p_intf, name, this, defSize);
228     }
229
230     void readSettings( QString name )
231     {
232         QVLCTools::restoreWidgetPosition( p_intf, name, this);
233     }
234     void readSettings( QSettings *settings )
235     {
236         QVLCTools::restoreWidgetPosition(settings, this);
237     }
238
239     void readSettings( QSettings *settings, QSize defSize)
240     {
241         QVLCTools::restoreWidgetPosition(settings, this, defSize);
242     }
243
244     void writeSettings(QString name )
245     {
246         QVLCTools::saveWidgetPosition( p_intf, name, this);
247     }
248     void writeSettings(QSettings *settings )
249     {
250         QVLCTools::saveWidgetPosition(settings, this);
251     }
252 };
253
254 #endif