]> git.sesse.net Git - vlc/blobdiff - modules/gui/qt4/util/qvlcframe.hpp
Stop allocating QSettings all the time everywhere.
[vlc] / modules / gui / qt4 / util / qvlcframe.hpp
index 3daff2d75a7771ec6602998381f5a6adee9a122d..d8baf9ad596b44bc8f937b2e77064ed540559ca1 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * qvlcframe.hpp : A few helpers
- ****************************************************************************
- * Copyright (C) 2006 the VideoLAN team
+ *****************************************************************************
+ * Copyright (C) 2006-2007 the VideoLAN team
  * $Id$
  *
  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
 
 #ifndef _QVLCFRAME_H_
 #define _QVLCFRAME_H_
 
 #include <QWidget>
+#include <QDialog>
 #include <QSpacerItem>
 #include <QHBoxLayout>
 #include <QApplication>
-#include <QSettings>
 #include <QMainWindow>
-#include <QPlastiqueStyle>
 #include <QPushButton>
+#include <QKeyEvent>
+#include <QDesktopWidget>
+#include <QSettings>
+
 #include "qt4.hpp"
-#include <vlc/vlc.h>
+#include <vlc_common.h>
+#include <vlc_charset.h>
+
+class QVLCTools
+{
+   public:
+       /*
+        use this function to save a widgets screen position
+        only for windows / dialogs which are floating, if a
+        window is docked into an other - don't all this function
+        or it may write garbage to position info!
+       */
+       static void saveWidgetPosition( QSettings *settings, QWidget *widget)
+       {
+         settings->setValue("geometry", widget->saveGeometry());
+       }
+       static void saveWidgetPosition( intf_thread_t *p_intf,
+                                       QString configName,
+                                       QWidget *widget)
+       {
+         getSettings()->beginGroup( configName );
+         QVLCTools::saveWidgetPosition(getSettings(), widget);
+         getSettings()->endGroup();
+       }
+
+
+       /*
+         use this method only for restoring window state of non docked
+         windows!
+       */
+       static bool restoreWidgetPosition(QSettings *settings,
+                                           QWidget *widget,
+                                           QSize defSize = QSize( 0, 0 ),
+                                           QPoint defPos = QPoint( 0, 0 ))
+       {
+          if(!widget->restoreGeometry(settings->value("geometry")
+                                      .toByteArray()))
+          {
+            widget->move(defPos);
+            widget->resize(defSize);
+
+            if(defPos.x() == 0 && defPos.y()==0)
+               centerWidgetOnScreen(widget);
+            return true;
+          }
+          return false;
+       }
+
+       static bool restoreWidgetPosition( intf_thread_t *p_intf,
+                                           QString configName,
+                                           QWidget *widget,
+                                           QSize defSize = QSize( 0, 0 ),
+                                           QPoint defPos = QPoint( 0, 0 ) )
+       {
+         getSettings()->beginGroup( configName );
+         bool defaultUsed = QVLCTools::restoreWidgetPosition( getSettings(),
+                                                                   widget,
+                                                                   defSize,
+                                                                   defPos);
+         getSettings()->endGroup();
+
+         return defaultUsed;
+       }
+
+      /*
+        call this method for a window or dialog to show it centred on
+        current screen
+      */
+      static void centerWidgetOnScreen(QWidget *widget)
+      {
+         QDesktopWidget * const desktop = QApplication::desktop();
+         QRect screenRect = desktop->screenGeometry(widget);
+         QPoint p1 = widget->frameGeometry().center();
+
+         widget->move ( screenRect.center() - p1 );
+      }
+};
 
 class QVLCFrame : public QWidget
 {
 public:
-    static void fixStyle( QWidget *w)
-    {
-         QStyle *style = qApp->style();
-#if 0
-        // Plastique is too dark.
-        /// theming ? getting KDE data ? ?
-        if( qobject_cast<QPlastiqueStyle *>(style) )
-        {
-            QPalette plt( w->palette() );
-            plt.setColor( QPalette::Active, QPalette::Highlight, Qt::gray );
-            QColor vlg = (Qt::lightGray);
-            vlg = vlg.toHsv();
-            vlg.setHsv( vlg.hue(), vlg.saturation(), 235  );
-            plt.setColor( QPalette::Active, QPalette::Window, vlg );
-            plt.setColor( QPalette::Inactive, QPalette::Window, vlg );
-            w->setPalette( plt );
-        }
+#ifdef __APPLE__
+    QVLCFrame( intf_thread_t *_p_intf ) : QWidget( NULL, Qt::Window ), p_intf( _p_intf )
+#else
+    QVLCFrame( intf_thread_t *_p_intf ) : QWidget( NULL ), p_intf( _p_intf )
 #endif
+    {};
+    virtual ~QVLCFrame()   {};
+
+    void toggleVisible()
+    {
+        if( isVisible() ) hide();
+        else show();
     }
-    static void doButtons( QWidget *w, QBoxLayout *l,
-                           QPushButton *defaul, char *psz_default,
-                           QPushButton *alt, char *psz_alt,
-                           QPushButton *other, char *psz_other )
+protected:
+    intf_thread_t *p_intf;
+
+    void readSettings( QString name,
+                       QSize defSize = QSize( 0, 0 ),
+                       QPoint defPos = QPoint( 0, 0 ) )
     {
-#ifdef QT42
-#else
-        QHBoxLayout *buttons_layout = new QHBoxLayout;
-        QSpacerItem *spacerItem = new QSpacerItem( 40, 20,
-                               QSizePolicy::Expanding, QSizePolicy::Minimum);
-        buttons_layout->addItem( spacerItem );
+        QVLCTools::restoreWidgetPosition(p_intf, name, this, defSize, defPos);
+    }
 
-        if( psz_default )
-        {
-            defaul = new QPushButton;
-            buttons_layout->addWidget( defaul );
-            defaul->setText( qfu( psz_default ) );
-        }
-        if( psz_alt )
+    void writeSettings( QString name )
+    {
+        QVLCTools::saveWidgetPosition( p_intf, name, this);
+    }
+
+    virtual void cancel()
+    {
+        hide();
+    }
+    virtual void close()
+    {
+        hide();
+    }
+    virtual void keyPressEvent( QKeyEvent *keyEvent )
+    {
+        if( keyEvent->key() == Qt::Key_Escape )
         {
-            alt = new QPushButton;
-            buttons_layout->addWidget( alt );
-            alt->setText( qfu( psz_alt ) );
+            msg_Dbg( p_intf, "Escp Key pressed" );
+            cancel();
         }
-        if( psz_other )
+        else if( keyEvent->key() == Qt::Key_Return )
         {
-            other = new QPushButton;
-            buttons_layout->addWidget( other );
-            other->setText( qfu( psz_other ) );
-        }
-        l->addLayout( buttons_layout );
-#endif
-    };
-
-    QVLCFrame( intf_thread_t *_p_intf ) : QWidget( NULL ), p_intf( _p_intf )
-    {
-        fixStyle( this );
-    };
-    virtual ~QVLCFrame()   {};
+             msg_Dbg( p_intf, "Enter Key pressed" );
+             close();
+         }
+    }
+};
 
+class QVLCDialog : public QDialog
+{
+public:
+    QVLCDialog( QWidget* parent, intf_thread_t *_p_intf ) :
+                                    QDialog( parent ), p_intf( _p_intf )
+    {}
+    virtual ~QVLCDialog() {};
     void toggleVisible()
     {
         if( isVisible() ) hide();
         else show();
     }
+
 protected:
     intf_thread_t *p_intf;
 
-    void readSettings( QString name, QSize defSize )
+    virtual void cancel()
     {
-        QSettings settings( "VideoLAN", "VLC" );
-        settings.beginGroup( name );
-        resize( settings.value( "size", defSize ).toSize() );
-        move( settings.value( "pos", QPoint( 0,0 ) ).toPoint() );
-        settings.endGroup();
+        hide();
     }
-    void writeSettings( QString name )
+    virtual void close()
     {
-        QSettings settings( "VideoLAN", "VLC" );
-        settings.beginGroup( name );
-        settings.setValue ("size", size() );
-        settings.setValue( "pos", pos() );
-        settings.endGroup();
+        hide();
+    }
+    virtual void keyPressEvent( QKeyEvent *keyEvent )
+    {
+        if( keyEvent->key() == Qt::Key_Escape )
+        {
+            msg_Dbg( p_intf, "Escp Key pressed" );
+            cancel();
+        }
+        else if( keyEvent->key() == Qt::Key_Return )
+        {
+             msg_Dbg( p_intf, "Enter Key pressed" );
+             close();
+        }
     }
 };
 
@@ -126,38 +211,43 @@ class QVLCMW : public QMainWindow
 {
 public:
     QVLCMW( intf_thread_t *_p_intf ) : QMainWindow( NULL ), p_intf( _p_intf )
+    {    }
+    virtual ~QVLCMW() {};
+    void toggleVisible()
     {
-        QVLCFrame::fixStyle( this );
+        if( isVisible() ) hide();
+        else show();
     }
-    virtual ~QVLCMW() {};
 protected:
     intf_thread_t *p_intf;
     QSize mainSize;
 
     void readSettings( QString name, QSize defSize )
     {
-        QSettings settings( "VideoLAN", "VLC" );
-        settings.beginGroup( name );
-        mainSize = settings.value( "size", defSize ).toSize();
-        QPoint npos = settings.value( "pos", QPoint( 0,0 ) ).toPoint();
-        if( npos.x() > 0 )
-            move( npos );
-        settings.endGroup();
+        QVLCTools::restoreWidgetPosition( p_intf, name, this, defSize);
     }
+
     void readSettings( QString name )
     {
-        QSettings settings( "VideoLAN", "VLC" );
-        settings.beginGroup( name );
-        mainSize = settings.value( "size", QSize( 0,0 ) ).toSize();
-        settings.endGroup();
+        QVLCTools::restoreWidgetPosition( p_intf, name, this);
     }
-    void writeSettings( QString name )
+    void readSettings( QSettings *settings )
+    {
+        QVLCTools::restoreWidgetPosition(settings, this);
+    }
+
+    void readSettings( QSettings *settings, QSize defSize)
+    {
+        QVLCTools::restoreWidgetPosition(settings, this, defSize);
+    }
+
+    void writeSettings(QString name )
+    {
+        QVLCTools::saveWidgetPosition( p_intf, name, this);
+    }
+    void writeSettings(QSettings *settings )
     {
-        QSettings settings( "VideoLAN", "VLC" );
-        settings.beginGroup( name );
-        settings.setValue ("size", size() );
-        settings.setValue( "pos", pos() );
-        settings.endGroup();
+        QVLCTools::saveWidgetPosition(settings, this);
     }
 };