]> git.sesse.net Git - vlc/blobdiff - modules/gui/qt4/dialogs/preferences.cpp
Qt: warn when we cannot save the configuration
[vlc] / modules / gui / qt4 / dialogs / preferences.cpp
index 7910fa8167f5482435c60e237cd268251565043e..2bfa9db793a3488580724f2b5ef0fa7256551e67 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include "dialogs/preferences.hpp"
-#include "dialogs_provider.hpp"
 #include "util/qvlcframe.hpp"
+#include "dialogs/errors.hpp"
 
 #include "components/complete_preferences.hpp"
 #include "components/simple_preferences.hpp"
 #include <QHBoxLayout>
 #include <QGroupBox>
 #include <QRadioButton>
-#include <QVBoxLayout>
 #include <QPushButton>
-#include <QCheckBox>
-#include <QScrollArea>
 #include <QMessageBox>
 #include <QDialogButtonBox>
 
-PrefsDialog *PrefsDialog::instance = NULL;
-
-PrefsDialog::PrefsDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
+PrefsDialog::PrefsDialog( QWidget *parent, intf_thread_t *_p_intf )
+            : QVLCDialog( parent, _p_intf )
 {
     QGridLayout *main_layout = new QGridLayout( this );
     setWindowTitle( qtr( "Preferences" ) );
-    resize( 750, 550 );
+    setWindowRole( "vlc-preferences" );
+    setWindowModality( Qt::WindowModal );
+
+    /* Whether we want it or not, we need to destroy on close to get
+       consistency when reset */
+    setAttribute( Qt::WA_DeleteOnClose );
 
     /* Create Panels */
-    tree_panel = new QWidget( 0 );
+    tree_panel = new QWidget;
     tree_panel_l = new QHBoxLayout;
     tree_panel->setLayout( tree_panel_l );
-    main_panel = new QWidget( 0 );
+    main_panel = new QWidget;
     main_panel_l = new QHBoxLayout;
     main_panel->setLayout( main_panel_l );
 
     /* Choice for types */
-    types = new QGroupBox( "Show settings" );
+    types = new QGroupBox( qtr("Show settings") );
     types->setAlignment( Qt::AlignHCenter );
-    QHBoxLayout *types_l = new QHBoxLayout(0);
+    QHBoxLayout *types_l = new QHBoxLayout;
     types_l->setSpacing( 3 ); types_l->setMargin( 3 );
-    small = new QRadioButton( qtr("Basic"), types );
+    small = new QRadioButton( qtr( "Simple" ), types );
+    small->setToolTip( qtr( "Switch to simple preferences view" ) );
     types_l->addWidget( small );
     all = new QRadioButton( qtr("All"), types ); types_l->addWidget( all );
+    all->setToolTip( qtr( "Switch to full preferences view" ) );
     types->setLayout( types_l );
     small->setChecked( true );
 
@@ -75,58 +82,61 @@ PrefsDialog::PrefsDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
     /* Buttons */
     QDialogButtonBox *buttonsBox = new QDialogButtonBox();
     QPushButton *save = new QPushButton( qtr( "&Save" ) );
+    save->setToolTip( qtr( "Save and close the dialog" ) );
     QPushButton *cancel = new QPushButton( qtr( "&Cancel" ) );
     QPushButton *reset = new QPushButton( qtr( "&Reset Preferences" ) );
 
     buttonsBox->addButton( save, QDialogButtonBox::AcceptRole );
     buttonsBox->addButton( cancel, QDialogButtonBox::RejectRole );
-    buttonsBox->addButton( reset, QDialogButtonBox::ActionRole );
+    buttonsBox->addButton( reset, QDialogButtonBox::ResetRole );
 
     /* Layout  */
     main_layout->addWidget( tree_panel, 0, 0, 3, 1 );
     main_layout->addWidget( types, 3, 0, 2, 1 );
-    main_layout->addWidget( main_panel, 0, 1, 4, 1 );
-    main_layout->addWidget( buttonsBox, 4, 1, 1 ,2 );
+    main_layout->addWidget( main_panel, 0, 1, 4, 2 );
+    main_layout->addWidget( buttonsBox, 4, 2, 1 ,1 );
 
     main_layout->setColumnMinimumWidth( 0, 150 );
+    main_layout->setColumnMinimumWidth( 1, 10 );
     main_layout->setColumnStretch( 0, 1 );
-    main_layout->setColumnStretch( 1, 3 );
+    main_layout->setColumnStretch( 1, 0 );
+    main_layout->setColumnStretch( 2, 10 );
 
-    main_layout->setRowStretch( 2, 4);
+    main_layout->setRowStretch( 2, 4 );
 
+    main_layout->setMargin( 9 );
     setLayout( main_layout );
 
     /* Margins */
     tree_panel_l->setMargin( 1 );
-    main_panel_l->setMargin( 3 );
+    main_panel_l->setContentsMargins( 6, 0, 0, 3 );
 
+    b_small = (p_intf->p_sys->i_screenHeight < 750);
+    if( b_small ) msg_Dbg( p_intf, "Small");
+    setMaximumHeight( p_intf->p_sys->i_screenHeight );
     for( int i = 0; i < SPrefsMax ; i++ ) simple_panels[i] = NULL;
 
-    if( config_GetInt( p_intf, "qt-advanced-pref") == 1 )
-    {
-        setAll();
-    }
+    if( var_InheritBool( p_intf, "qt-advanced-pref" )
+     || var_InheritBool( p_intf, "advanced" ) )
+        setAdvanced();
     else
-    {
         setSmall();
-    }
 
     BUTTONACT( save, save() );
     BUTTONACT( cancel, cancel() );
     BUTTONACT( reset, reset() );
+
     BUTTONACT( small, setSmall() );
-    BUTTONACT( all, setAll() );
+    BUTTONACT( all, setAdvanced() );
 
+    resize( 780, sizeHint().height() );
 }
 
-void PrefsDialog::setAll()
+void PrefsDialog::setAdvanced()
 {
     /* We already have a simple TREE, and we just want to hide it */
     if( simple_tree )
-    {
-        tree_panel_l->removeWidget( simple_tree );
-        simple_tree->hide();
-    }
+        if( simple_tree->isVisible() ) simple_tree->hide();
 
     /* If don't have already and advanced TREE, then create it */
     if( !advanced_tree )
@@ -135,101 +145,110 @@ void PrefsDialog::setAll()
          advanced_tree = new PrefsTree( p_intf, tree_panel );
         /* and connections */
          CONNECT( advanced_tree,
-                  currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem *),
-                  this, changePanel( QTreeWidgetItem * ) );
+                  currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem * ),
+                  this, changeAdvPanel( QTreeWidgetItem * ) );
+        tree_panel_l->addWidget( advanced_tree );
     }
-    /* Add the Advanced tree to the tree_panel, even if it is already inside,
-       since it can't hurt. And show it. */
-    tree_panel_l->addWidget( advanced_tree );
+
+    /* Show it */
     advanced_tree->show();
 
     /* Remove the simple current panel from the main panels*/
-    if( current_simple_panel  )
-    {
-        main_panel_l->removeWidget( current_simple_panel );
-        current_simple_panel->hide();
-    }
+    if( current_simple_panel )
+        if( current_simple_panel->isVisible() ) current_simple_panel->hide();
+
     /* If no advanced Panel exist, create one, attach it and show it*/
     if( !advanced_panel )
-         advanced_panel = new PrefsPanel( main_panel );
-    main_panel_l->addWidget( advanced_panel );
-    all->setChecked( true );
+    {
+        advanced_panel = new AdvPrefsPanel( main_panel );
+        main_panel_l->addWidget( advanced_panel );
+    }
     advanced_panel->show();
+
+    /* Select the first Item of the preferences. Maybe you want to select a specified
+       category... */
+    advanced_tree->setCurrentIndex(
+            advanced_tree->model()->index( 0, 0, QModelIndex() ) );
+
+    all->setChecked( true );
 }
 
 void PrefsDialog::setSmall()
 {
     /* If an advanced TREE exists, remove and hide it */
     if( advanced_tree )
-    {
-        tree_panel_l->removeWidget( advanced_tree );
-        advanced_tree->hide();
-    }
+        if( advanced_tree->isVisible() ) advanced_tree->hide();
+
     /* If no simple_tree, create one, connect it */
     if( !simple_tree )
     {
-         simple_tree = new SPrefsCatList( p_intf, tree_panel );
+         simple_tree = new SPrefsCatList( p_intf, tree_panel, b_small );
          CONNECT( simple_tree,
                   currentItemChanged( int ),
                   this,  changeSimplePanel( int ) );
+        tree_panel_l->addWidget( simple_tree );
     }
-    /* Attach anyway and show it */
-    tree_panel_l->addWidget( simple_tree );
+
+    /*show it */
     simple_tree->show();
 
     /* If an Advanced PANEL exists, remove it */
     if( advanced_panel )
+        if( advanced_panel->isVisible() ) advanced_panel->hide();
+
+    if( !current_simple_panel )
     {
-        main_panel_l->removeWidget( advanced_panel );
-        advanced_panel->hide();
-    }
-    if( !current_simple_panel  )
-    {
-        current_simple_panel  = new SPrefsPanel( p_intf, main_panel, SPrefsDefaultCat );
+        current_simple_panel =
+            new SPrefsPanel( p_intf, main_panel, SPrefsDefaultCat, b_small );
         simple_panels[SPrefsDefaultCat] =  current_simple_panel;
+        main_panel_l->addWidget( current_simple_panel );
     }
-    main_panel_l->addWidget( current_simple_panel  );
-    small->setChecked( true );
+
     current_simple_panel->show();
+    small->setChecked( true );
 }
 
+/* Switching from on simple panel to another */
 void PrefsDialog::changeSimplePanel( int number )
 {
-    if( current_simple_panel  )
-    {
-        main_panel_l->removeWidget( current_simple_panel  );
-        current_simple_panel->hide();
-    }
+    if( current_simple_panel )
+        if( current_simple_panel->isVisible() ) current_simple_panel->hide();
+
     current_simple_panel = simple_panels[number];
-    if( !current_simple_panel  )
+    if( !current_simple_panel )
     {
-        current_simple_panel  = new SPrefsPanel( p_intf, main_panel, number );
+        current_simple_panel  = new SPrefsPanel( p_intf, main_panel, number, b_small );
         simple_panels[number] = current_simple_panel;
+        main_panel_l->addWidget( current_simple_panel );
     }
-    main_panel_l->addWidget( current_simple_panel  );
+
     current_simple_panel->show();
 }
 
-void PrefsDialog::changePanel( QTreeWidgetItem *item )
+/* Changing from one Advanced Panel to another */
+void PrefsDialog::changeAdvPanel( QTreeWidgetItem *item )
 {
+    if( item == NULL ) return;
     PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData*>();
 
     if( advanced_panel )
+        if( advanced_panel->isVisible() ) advanced_panel->hide();
+
+    if( !data->panel )
     {
-        main_panel_l->removeWidget( advanced_panel );
-        advanced_panel->hide();
+        data->panel = new AdvPrefsPanel( p_intf, main_panel , data );
+        main_panel_l->addWidget( data->panel );
     }
-    if( !data->panel )
-        data->panel = new PrefsPanel( p_intf, main_panel , data );
 
     advanced_panel = data->panel;
-    main_panel_l->addWidget( advanced_panel );
     advanced_panel->show();
 }
 
+#if 0
+/*Called from extended settings, is not used anymore, but could be useful one day*/
 void PrefsDialog::showModulePrefs( char *psz_module )
 {
-    setAll();
+    setAdvanced();
     all->setChecked( true );
     for( int i_cat_index = 0 ; i_cat_index < advanced_tree->topLevelItemCount();
          i_cat_index++ )
@@ -257,65 +276,55 @@ void PrefsDialog::showModulePrefs( char *psz_module )
     }
     show();
 }
+#endif
 
+/* Actual apply and save for the preferences */
 void PrefsDialog::save()
 {
-    apply();
-    hide();
-}
-
-void PrefsDialog::apply()
-{
-    if( small->isChecked() && simple_tree )
+    if( small->isChecked() && simple_tree->isVisible() )
     {
+        msg_Dbg( p_intf, "Saving the simple preferences" );
         for( int i = 0 ; i< SPrefsMax; i++ ){
             if( simple_panels[i] )simple_panels[i]->apply();
         }
     }
-    else if( all->isChecked() && advanced_tree )
-        advanced_tree->applyAll();
-
-    config_SaveConfigFile( p_intf, NULL );
-
-    /* Delete the other panel in order to force its reload after clicking
-       on apply - UGLY but will work for now. */
-    if( current_simple_panel  && current_simple_panel->isVisible() && advanced_panel )
+    else if( all->isChecked() && advanced_tree->isVisible() )
     {
-        delete advanced_panel;
-        advanced_panel = NULL;
+        msg_Dbg( p_intf, "Saving the advanced preferences" );
+        advanced_tree->applyAll();
     }
-    if( advanced_panel && advanced_panel->isVisible() && current_simple_panel  )
+
+    /* Save to file */
+    if( config_SaveConfigFile( p_intf, NULL ) != 0 )
     {
-        delete current_simple_panel;
-        current_simple_panel  = NULL;
+        ErrorsDialog::getInstance (p_intf)->addError( qtr( "Cannot save Configuration" ),
+            qtr("Preferences file could not be saved") );
     }
+    accept();
 }
 
+/* Clean the preferences, dunno if it does something really */
 void PrefsDialog::cancel()
 {
-    if( small->isChecked() && simple_tree )
-    {
-        for( int i = 0 ; i< SPrefsMax; i++ )
-            if( simple_panels[i] ) simple_panels[i]->clean();
-    }
-    else if( all->isChecked() && advanced_tree )
-    {
-        advanced_tree->cleanAll();
-        advanced_panel = NULL;
-    }
-    hide();
+    reject();
 }
 
+/* Reset all the preferences, when you click the button */
 void PrefsDialog::reset()
 {
-    int ret = QMessageBox::question(this, qtr("Reset Preferences"),
-                 qtr("This will reset your VLC media player preferences.\n"
-                         "Are you sure you want to continue?"),
-                  QMessageBox::Ok | QMessageBox::Cancel,
-                                                         QMessageBox::Ok);
-    if ( ret == QMessageBox::Ok )
+    int ret = QMessageBox::question(
+                 this,
+                 qtr( "Reset Preferences" ),
+                 qtr( "Are you sure you want to reset your VLC media player preferences?" ),
+                 QMessageBox::Ok | QMessageBox::Cancel,
+                 QMessageBox::Ok);
+
+    if( ret == QMessageBox::Ok )
     {
         config_ResetAll( p_intf );
         config_SaveConfigFile( p_intf, NULL );
+        getSettings()->clear();
+
+        accept();
     }
 }