]> 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 82f9134688ea80e11b543f02055737267c33441b..d8baf9ad596b44bc8f937b2e77064ed540559ca1 100644 (file)
 #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 QHBoxLayout* doButtons( QWidget *w, QBoxLayout *l,
-                               QPushButton **defaul, char *psz_default,
-                               QPushButton **alt, char *psz_alt,
-                               QPushButton **other, char *psz_other )
-    {
-#ifdef QT42
+#ifdef __APPLE__
+    QVLCFrame( intf_thread_t *_p_intf ) : QWidget( NULL, Qt::Window ), p_intf( _p_intf )
 #else
-        QHBoxLayout *buttons_layout = new QHBoxLayout;
-        QSpacerItem *spacerItem = new QSpacerItem( 40, 20,
-                               QSizePolicy::Expanding, QSizePolicy::Minimum);
-        buttons_layout->addItem( spacerItem );
-
-        if( psz_default )
-        {
-            *defaul = new QPushButton(0);
-            (*defaul)->setFocus();
-            buttons_layout->addWidget( *defaul );
-            (*defaul)->setText( qfu( psz_default ) );
-        }
-        if( psz_alt )
-        {
-            *alt = new QPushButton(0);
-            buttons_layout->addWidget( *alt );
-            (*alt)->setText( qfu( psz_alt ) );
-        }
-        if( psz_other )
-        {
-            *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 )
-    {    };
+#endif
+    {};
     virtual ~QVLCFrame()   {};
 
     void toggleVisible()
@@ -90,32 +133,38 @@ public:
 protected:
     intf_thread_t *p_intf;
 
-    void readSettings( QString name, QSize defSize )
+    void readSettings( QString name,
+                       QSize defSize = QSize( 0, 0 ),
+                       QPoint defPos = QPoint( 0, 0 ) )
     {
-        QSettings settings( "VideoLAN", "VLC" );
-        settings.beginGroup( name );
-        resize( settings.value( "size", defSize ).toSize() );
-        move( settings.value( "pos", QPoint( 0,0 ) ).toPoint() );
-        settings.endGroup();
+        QVLCTools::restoreWidgetPosition(p_intf, name, this, defSize, defPos);
     }
+
     void writeSettings( QString name )
     {
-        QSettings settings( "VideoLAN", "VLC" );
-        settings.beginGroup( name );
-        settings.setValue ("size", size() );
-        settings.setValue( "pos", pos() );
-        settings.endGroup();
+        QVLCTools::saveWidgetPosition( p_intf, name, this);
+    }
+
+    virtual void cancel()
+    {
+        hide();
     }
-    void cancel()
+    virtual void close()
     {
         hide();
     }
-    void keyPressEvent( QKeyEvent *keyEvent )
+    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();
+         }
     }
 };
 
@@ -135,16 +184,26 @@ public:
 protected:
     intf_thread_t *p_intf;
 
-    void cancel()
+    virtual void cancel()
+    {
+        hide();
+    }
+    virtual void close()
     {
         hide();
     }
-    void keyPressEvent( QKeyEvent *keyEvent )
+    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();
+        }
     }
 };
 
@@ -165,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);
     }
 };