]> 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 702595a35d1ee97e0b0c566f7d8b64cc7b7ec0d0..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 <charset.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 );
-            plt.setColor( QPalette::Inactive, QPalette::Button, vlg );
-            plt.setColor( QPalette::Active, QPalette::Button, vlg );
-            plt.setColor( QPalette::Active, QPalette::Text, Qt::yellow );
-            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();
+    }
+protected:
+    intf_thread_t *p_intf;
+
+    void readSettings( QString name,
+                       QSize defSize = QSize( 0, 0 ),
+                       QPoint defPos = QPoint( 0, 0 ) )
+    {
+        QVLCTools::restoreWidgetPosition(p_intf, name, this, defSize, defPos);
     }
-    static QHBoxLayout* doButtons( QWidget *w, QBoxLayout *l,
-                               QPushButton **defaul, char *psz_default,
-                               QPushButton **alt, char *psz_alt,
-                               QPushButton **other, char *psz_other )
+
+    void writeSettings( QString name )
     {
-#ifdef QT42
-#else
-        QHBoxLayout *buttons_layout = new QHBoxLayout;
-        QSpacerItem *spacerItem = new QSpacerItem( 40, 20,
-                               QSizePolicy::Expanding, QSizePolicy::Minimum);
-        buttons_layout->addItem( spacerItem );
+        QVLCTools::saveWidgetPosition( p_intf, name, this);
+    }
 
-        if( psz_default )
-        {
-            *defaul = new QPushButton(0);
-            (*defaul)->setFocus();
-            buttons_layout->addWidget( *defaul );
-            (*defaul)->setText( qfu( psz_default ) );
-        }
-        if( psz_alt )
+    virtual void cancel()
+    {
+        hide();
+    }
+    virtual void close()
+    {
+        hide();
+    }
+    virtual void keyPressEvent( QKeyEvent *keyEvent )
+    {
+        if( keyEvent->key() == Qt::Key_Escape )
         {
-            *alt = new QPushButton(0);
-            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( 0 );
-            buttons_layout->addWidget( *other );
-            (*other)->setText( qfu( psz_other ) );
-        }
-        if( l )
-            l->addLayout( buttons_layout );
-#endif
-        return buttons_layout;
-    };
-
-    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();
+        }
     }
 };
 
@@ -133,9 +211,7 @@ class QVLCMW : public QMainWindow
 {
 public:
     QVLCMW( intf_thread_t *_p_intf ) : QMainWindow( NULL ), p_intf( _p_intf )
-    {
-        QVLCFrame::fixStyle( this );
-    }
+    {    }
     virtual ~QVLCMW() {};
     void toggleVisible()
     {
@@ -148,27 +224,30 @@ protected:
 
     void readSettings( QString name, QSize defSize )
     {
-        QSettings settings( "VideoLAN", "VLC" );
-        settings.beginGroup( name );
-      QSize s =  settings.value( "size", defSize ).toSize() ;
-      fprintf( stderr, "%i %i ", s.width(), s.height() );
-        move( settings.value( "pos", QPoint( 0,0 ) ).toPoint() );
-        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);
     }
 };