]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/qvlcframe.hpp
Qt: Return == Enter
[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               || keyEvent->key() == Qt::Key_Enter )
163         {
164              msg_Dbg( p_intf, "Enter Key pressed" );
165              close();
166          }
167     }
168 };
169
170 class QVLCDialog : public QDialog
171 {
172 public:
173     QVLCDialog( QWidget* parent, intf_thread_t *_p_intf ) :
174                                     QDialog( parent ), p_intf( _p_intf )
175     {}
176     virtual ~QVLCDialog() {};
177     void toggleVisible()
178     {
179         if( isVisible() ) hide();
180         else show();
181     }
182
183 protected:
184     intf_thread_t *p_intf;
185
186     virtual void cancel()
187     {
188         hide();
189     }
190     virtual void close()
191     {
192         hide();
193     }
194     virtual void keyPressEvent( QKeyEvent *keyEvent )
195     {
196         if( keyEvent->key() == Qt::Key_Escape )
197         {
198             msg_Dbg( p_intf, "Escp Key pressed" );
199             cancel();
200         }
201         else if( keyEvent->key() == Qt::Key_Return
202               || keyEvent->key() == Qt::Key_Enter )
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