]> git.sesse.net Git - vlc/blobdiff - modules/gui/qt4/util/qvlcframe.hpp
Qt: add a close to systray menu option.
[vlc] / modules / gui / qt4 / util / qvlcframe.hpp
index d1f2a615574f66052557ac1b8ce688862ef1d484..edaa08be96d10a89257974ef4b2a36fdc4b568e7 100644 (file)
 
 #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 <QStyle>
 
 #include "qt4.hpp"
-#include <vlc/vlc.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 another - 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,
+                                       const 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)
+               widget->setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, widget->size(), qApp->desktop()->availableGeometry()));
+            return true;
+          }
+          return false;
+       }
+
+       static bool restoreWidgetPosition( intf_thread_t *p_intf,
+                                           const 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;
+       }
+};
 
 class QVLCFrame : public QWidget
 {
 public:
     QVLCFrame( intf_thread_t *_p_intf ) : QWidget( NULL ), p_intf( _p_intf )
-    {    };
+    {};
     virtual ~QVLCFrame()   {};
 
     void toggleVisible()
@@ -54,22 +113,18 @@ public:
 protected:
     intf_thread_t *p_intf;
 
-    void readSettings( QString name, QSize defSize, QPoint defPos )
+    void readSettings( const QString& name,
+                       QSize defSize = QSize( 1, 1 ),
+                       QPoint defPos = QPoint( 0, 0 ) )
     {
-        QSettings settings( "vlc", "vlc-qt-interface" );
-        settings.beginGroup( name );
-        resize( settings.value( "size", defSize ).toSize() );
-        move( settings.value( "pos", defPos ).toPoint() );
-        settings.endGroup();
+        QVLCTools::restoreWidgetPosition(p_intf, name, this, defSize, defPos);
     }
-    void writeSettings( QString name )
+
+    void writeSettings( const QString& name )
     {
-        QSettings settings( "vlc", "vlc-qt-interface" );
-        settings.beginGroup( name );
-        settings.setValue ("size", size() );
-        settings.setValue( "pos", pos() );
-        settings.endGroup();
+        QVLCTools::saveWidgetPosition( p_intf, name, this);
     }
+
     virtual void cancel()
     {
         hide();
@@ -82,14 +137,13 @@ protected:
     {
         if( keyEvent->key() == Qt::Key_Escape )
         {
-            msg_Dbg( p_intf, "Escp Key pressed" );
-            cancel();
+            this->cancel();
         }
-        else if( keyEvent->key() == Qt::Key_Return )
+        else if( keyEvent->key() == Qt::Key_Return
+              || keyEvent->key() == Qt::Key_Enter )
         {
-             msg_Dbg( p_intf, "Enter Key pressed" );
-             close();
-         }
+             this->close();
+        }
     }
 };
 
@@ -121,13 +175,12 @@ protected:
     {
         if( keyEvent->key() == Qt::Key_Escape )
         {
-            msg_Dbg( p_intf, "Escp Key pressed" );
-            cancel();
+            this->cancel();
         }
-        else if( keyEvent->key() == Qt::Key_Return )
+        else if( keyEvent->key() == Qt::Key_Return
+              || keyEvent->key() == Qt::Key_Enter )
         {
-             msg_Dbg( p_intf, "Enter Key pressed" );
-             close();
+            this->close();
         }
     }
 };
@@ -147,29 +200,32 @@ protected:
     intf_thread_t *p_intf;
     QSize mainSize;
 
-    void readSettings( QString name, QSize defSize )
+    void readSettings( const QString& name, QSize defSize )
+    {
+        QVLCTools::restoreWidgetPosition( p_intf, name, this, defSize);
+    }
+
+    void readSettings( const QString& name )
     {
-        QSettings settings( "vlc", "vlc-qt-interface" );
-        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);
     }
-    void readSettings( QString name )
+    void readSettings( QSettings *settings )
+    {
+        QVLCTools::restoreWidgetPosition(settings, this);
+    }
+
+    void readSettings( QSettings *settings, QSize defSize)
+    {
+        QVLCTools::restoreWidgetPosition(settings, this, defSize);
+    }
+
+    void writeSettings( const QString& name )
     {
-        QSettings settings( "vlc", "vlc-qt-interface" );
-        settings.beginGroup( name );
-        mainSize = settings.value( "size", QSize( 0,0 ) ).toSize();
-        settings.endGroup();
+        QVLCTools::saveWidgetPosition( p_intf, name, this);
     }
-    void writeSettings( QString name )
+    void writeSettings(QSettings *settings )
     {
-        QSettings settings( "vlc", "vlc-qt-interface" );
-        settings.beginGroup( name );
-        settings.setValue ("size", size() );
-        settings.setValue( "pos", pos() );
-        settings.endGroup();
+        QVLCTools::saveWidgetPosition(settings, this);
     }
 };