]> 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 623d6523619708068021f263f1f6c6da49d90593..d8baf9ad596b44bc8f937b2e77064ed540559ca1 100644 (file)
 #include <QSpacerItem>
 #include <QHBoxLayout>
 #include <QApplication>
-#include <QSettings>
 #include <QMainWindow>
 #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:
+#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()
@@ -57,26 +137,14 @@ protected:
                        QSize defSize = QSize( 0, 0 ),
                        QPoint defPos = QPoint( 0, 0 ) )
     {
-        QSettings settings( "vlc", "vlc-qt-interface" );
-        settings.beginGroup( name );
-        /* never trust any saved size ;-) */
-        QSize newSize = settings.value( "size", defSize ).toSize();
-        if( newSize.isValid() )
-           resize( newSize );
-        move( settings.value( "pos", defPos ).toPoint() );
-        settings.endGroup();
+        QVLCTools::restoreWidgetPosition(p_intf, name, this, defSize, defPos);
     }
+
     void writeSettings( QString name )
     {
-        QSettings settings( "vlc", "vlc-qt-interface" );
-        settings.beginGroup( name );
-        /* only save valid sizes ... */
-        QSize currentsize = size();
-        if( currentsize.isValid() )
-           settings.setValue ("size", currentsize );
-        settings.setValue( "pos", pos() );
-        settings.endGroup();
+        QVLCTools::saveWidgetPosition( p_intf, name, this);
     }
+
     virtual void cancel()
     {
         hide();
@@ -156,35 +224,30 @@ protected:
 
     void readSettings( QString name, QSize defSize )
     {
-        QSettings settings( "vlc", "vlc-qt-interface" );
-        settings.beginGroup( name );
-        QSize s =  settings.value( "size", defSize ).toSize() ;
-        move( settings.value( "pos", QPoint( 0,0 ) ).toPoint() );
-        settings.endGroup();
+        QVLCTools::restoreWidgetPosition( p_intf, name, this, defSize);
     }
 
     void readSettings( QString name )
     {
-        QSettings settings( "vlc", "vlc-qt-interface" );
-        settings.beginGroup( name );
-        mainSize = settings.value( "size", QSize( 0,0 ) ).toSize();
-        if( !mainSize.isValid() )
-        {
-           mainSize = QSize(0,0);
-        }
-        settings.endGroup();
+        QVLCTools::restoreWidgetPosition( p_intf, name, this);
+    }
+    void readSettings( QSettings *settings )
+    {
+        QVLCTools::restoreWidgetPosition(settings, this);
     }
 
-    void writeSettings( QString name )
+    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( "vlc", "vlc-qt-interface" );
-        settings.beginGroup( name );
-        /* only save valid sizes ... */
-        QSize currentsize = size();
-        if( currentsize.isValid() )
-            settings.setValue ("size", currentsize );
-        settings.setValue( "pos", pos() );
-        settings.endGroup();
+        QVLCTools::saveWidgetPosition(settings, this);
     }
 };