qvlcframe.hpp

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * qvlcframe.hpp : A few helpers
00003  *****************************************************************************
00004  * Copyright (C) 2006-2007 the VideoLAN team
00005  * $Id$
00006  *
00007  * Authors: Clément Stenac <zorglub@videolan.org>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
00022  *****************************************************************************/
00023 
00024 #ifndef _QVLCFRAME_H_
00025 #define _QVLCFRAME_H_
00026 
00027 #include <QWidget>
00028 #include <QDialog>
00029 #include <QSpacerItem>
00030 #include <QHBoxLayout>
00031 #include <QApplication>
00032 #include <QMainWindow>
00033 #include <QPushButton>
00034 #include <QKeyEvent>
00035 #include <QDesktopWidget>
00036 #include <QSettings>
00037 
00038 #include "qt4.hpp"
00039 #include <vlc_common.h>
00040 #include <vlc_charset.h>
00041 
00042 class QVLCTools
00043 {
00044    public:
00045        /*
00046         use this function to save a widgets screen position
00047         only for windows / dialogs which are floating, if a
00048         window is docked into an other - don't all this function
00049         or it may write garbage to position info!
00050        */
00051        static void saveWidgetPosition( QSettings *settings, QWidget *widget)
00052        {
00053          settings->setValue("geometry", widget->saveGeometry());
00054        }
00055        static void saveWidgetPosition( intf_thread_t *p_intf,
00056                                        QString configName,
00057                                        QWidget *widget)
00058        {
00059          getSettings()->beginGroup( configName );
00060          QVLCTools::saveWidgetPosition(getSettings(), widget);
00061          getSettings()->endGroup();
00062        }
00063 
00064 
00065        /*
00066          use this method only for restoring window state of non docked
00067          windows!
00068        */
00069        static bool restoreWidgetPosition(QSettings *settings,
00070                                            QWidget *widget,
00071                                            QSize defSize = QSize( 0, 0 ),
00072                                            QPoint defPos = QPoint( 0, 0 ))
00073        {
00074           if(!widget->restoreGeometry(settings->value("geometry")
00075                                       .toByteArray()))
00076           {
00077             widget->move(defPos);
00078             widget->resize(defSize);
00079 
00080             if(defPos.x() == 0 && defPos.y()==0)
00081                centerWidgetOnScreen(widget);
00082             return true;
00083           }
00084           return false;
00085        }
00086 
00087        static bool restoreWidgetPosition( intf_thread_t *p_intf,
00088                                            QString configName,
00089                                            QWidget *widget,
00090                                            QSize defSize = QSize( 0, 0 ),
00091                                            QPoint defPos = QPoint( 0, 0 ) )
00092        {
00093          getSettings()->beginGroup( configName );
00094          bool defaultUsed = QVLCTools::restoreWidgetPosition( getSettings(),
00095                                                                    widget,
00096                                                                    defSize,
00097                                                                    defPos);
00098          getSettings()->endGroup();
00099 
00100          return defaultUsed;
00101        }
00102 
00103       /*
00104         call this method for a window or dialog to show it centred on
00105         current screen
00106       */
00107       static void centerWidgetOnScreen(QWidget *widget)
00108       {
00109          QDesktopWidget * const desktop = QApplication::desktop();
00110          QRect screenRect = desktop->availableGeometry(widget);
00111          QPoint p1 = widget->frameGeometry().center();
00112 
00113          widget->move ( screenRect.center() - p1 );
00114       }
00115 };
00116 
00117 class QVLCFrame : public QWidget
00118 {
00119 public:
00120 #ifdef __APPLE__
00121     QVLCFrame( intf_thread_t *_p_intf ) : QWidget( NULL, Qt::Window ), p_intf( _p_intf )
00122 #else
00123     QVLCFrame( intf_thread_t *_p_intf ) : QWidget( NULL ), p_intf( _p_intf )
00124 #endif
00125     {};
00126     virtual ~QVLCFrame()   {};
00127 
00128     void toggleVisible()
00129     {
00130         if( isVisible() ) hide();
00131         else show();
00132     }
00133 protected:
00134     intf_thread_t *p_intf;
00135 
00136     void readSettings( QString name,
00137                        QSize defSize = QSize( 0, 0 ),
00138                        QPoint defPos = QPoint( 0, 0 ) )
00139     {
00140         QVLCTools::restoreWidgetPosition(p_intf, name, this, defSize, defPos);
00141     }
00142 
00143     void writeSettings( QString name )
00144     {
00145         QVLCTools::saveWidgetPosition( p_intf, name, this);
00146     }
00147 
00148     virtual void cancel()
00149     {
00150         hide();
00151     }
00152     virtual void close()
00153     {
00154         hide();
00155     }
00156     virtual void keyPressEvent( QKeyEvent *keyEvent )
00157     {
00158         if( keyEvent->key() == Qt::Key_Escape )
00159         {
00160             msg_Dbg( p_intf, "Escp Key pressed" );
00161             cancel();
00162         }
00163         else if( keyEvent->key() == Qt::Key_Return )
00164         {
00165              msg_Dbg( p_intf, "Enter Key pressed" );
00166              close();
00167          }
00168     }
00169 };
00170 
00171 class QVLCDialog : public QDialog
00172 {
00173 public:
00174     QVLCDialog( QWidget* parent, intf_thread_t *_p_intf ) :
00175                                     QDialog( parent ), p_intf( _p_intf )
00176     {}
00177     virtual ~QVLCDialog() {};
00178     void toggleVisible()
00179     {
00180         if( isVisible() ) hide();
00181         else show();
00182     }
00183 
00184 protected:
00185     intf_thread_t *p_intf;
00186 
00187     virtual void cancel()
00188     {
00189         hide();
00190     }
00191     virtual void close()
00192     {
00193         hide();
00194     }
00195     virtual void keyPressEvent( QKeyEvent *keyEvent )
00196     {
00197         if( keyEvent->key() == Qt::Key_Escape )
00198         {
00199             msg_Dbg( p_intf, "Escp Key pressed" );
00200             cancel();
00201         }
00202         else if( keyEvent->key() == Qt::Key_Return )
00203         {
00204              msg_Dbg( p_intf, "Enter Key pressed" );
00205              close();
00206         }
00207     }
00208 };
00209 
00210 class QVLCMW : public QMainWindow
00211 {
00212 public:
00213     QVLCMW( intf_thread_t *_p_intf ) : QMainWindow( NULL ), p_intf( _p_intf )
00214     {    }
00215     virtual ~QVLCMW() {};
00216     void toggleVisible()
00217     {
00218         if( isVisible() ) hide();
00219         else show();
00220     }
00221 protected:
00222     intf_thread_t *p_intf;
00223     QSize mainSize;
00224 
00225     void readSettings( QString name, QSize defSize )
00226     {
00227         QVLCTools::restoreWidgetPosition( p_intf, name, this, defSize);
00228     }
00229 
00230     void readSettings( QString name )
00231     {
00232         QVLCTools::restoreWidgetPosition( p_intf, name, this);
00233     }
00234     void readSettings( QSettings *settings )
00235     {
00236         QVLCTools::restoreWidgetPosition(settings, this);
00237     }
00238 
00239     void readSettings( QSettings *settings, QSize defSize)
00240     {
00241         QVLCTools::restoreWidgetPosition(settings, this, defSize);
00242     }
00243 
00244     void writeSettings(QString name )
00245     {
00246         QVLCTools::saveWidgetPosition( p_intf, name, this);
00247     }
00248     void writeSettings(QSettings *settings )
00249     {
00250         QVLCTools::saveWidgetPosition(settings, this);
00251     }
00252 };
00253 
00254 #endif

Generated on Wed Aug 13 08:02:38 2008 for VLC by  doxygen 1.5.1