]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/qvlcframe.hpp
Fix some typos.
[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 another - 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     QVLCFrame( intf_thread_t *_p_intf ) : QWidget( NULL ), p_intf( _p_intf )
105     {};
106     virtual ~QVLCFrame()   {};
107
108     void toggleVisible()
109     {
110         if( isVisible() ) hide();
111         else show();
112     }
113 protected:
114     intf_thread_t *p_intf;
115
116     void readSettings( const QString& name,
117                        QSize defSize = QSize( 1, 1 ),
118                        QPoint defPos = QPoint( 0, 0 ) )
119     {
120         QVLCTools::restoreWidgetPosition(p_intf, name, this, defSize, defPos);
121     }
122
123     void writeSettings( const QString& name )
124     {
125         QVLCTools::saveWidgetPosition( p_intf, name, this);
126     }
127
128     virtual void cancel()
129     {
130         hide();
131     }
132     virtual void close()
133     {
134         hide();
135     }
136     virtual void keyPressEvent( QKeyEvent *keyEvent )
137     {
138         if( keyEvent->key() == Qt::Key_Escape )
139         {
140             this->cancel();
141         }
142         else if( keyEvent->key() == Qt::Key_Return
143               || keyEvent->key() == Qt::Key_Enter )
144         {
145              this->close();
146         }
147     }
148 };
149
150 class QVLCDialog : public QDialog
151 {
152 public:
153     QVLCDialog( QWidget* parent, intf_thread_t *_p_intf ) :
154                                     QDialog( parent ), p_intf( _p_intf )
155     {}
156     virtual ~QVLCDialog() {};
157     void toggleVisible()
158     {
159         if( isVisible() ) hide();
160         else show();
161     }
162
163 protected:
164     intf_thread_t *p_intf;
165
166     virtual void cancel()
167     {
168         hide();
169     }
170     virtual void close()
171     {
172         hide();
173     }
174     virtual void keyPressEvent( QKeyEvent *keyEvent )
175     {
176         if( keyEvent->key() == Qt::Key_Escape )
177         {
178             this->cancel();
179         }
180         else if( keyEvent->key() == Qt::Key_Return
181               || keyEvent->key() == Qt::Key_Enter )
182         {
183             this->close();
184         }
185     }
186 };
187
188 class QVLCMW : public QMainWindow
189 {
190 public:
191     QVLCMW( intf_thread_t *_p_intf ) : QMainWindow( NULL ), p_intf( _p_intf )
192     {    }
193     virtual ~QVLCMW() {};
194     void toggleVisible()
195     {
196         if( isVisible() ) hide();
197         else show();
198     }
199 protected:
200     intf_thread_t *p_intf;
201     QSize mainSize;
202
203     void readSettings( const QString& name, QSize defSize )
204     {
205         QVLCTools::restoreWidgetPosition( p_intf, name, this, defSize);
206     }
207
208     void readSettings( const QString& name )
209     {
210         QVLCTools::restoreWidgetPosition( p_intf, name, this);
211     }
212     void readSettings( QSettings *settings )
213     {
214         QVLCTools::restoreWidgetPosition(settings, this);
215     }
216
217     void readSettings( QSettings *settings, QSize defSize)
218     {
219         QVLCTools::restoreWidgetPosition(settings, this, defSize);
220     }
221
222     void writeSettings( const QString& name )
223     {
224         QVLCTools::saveWidgetPosition( p_intf, name, this);
225     }
226     void writeSettings(QSettings *settings )
227     {
228         QVLCTools::saveWidgetPosition(settings, this);
229     }
230 };
231
232 #endif