]> git.sesse.net Git - vlc/blobdiff - modules/gui/qt4/components/preferences_widgets.cpp
Qt4- SImple Preferences. Add file and directory configuration.
[vlc] / modules / gui / qt4 / components / preferences_widgets.cpp
index 7167c853dc4db77a63fd4a5e26b17169a76432bd..15a092ebcd7f30c9c45c24ed0928313bd42975d6 100644 (file)
@@ -1,11 +1,12 @@
 /*****************************************************************************
  * preferences_widgets.cpp : Widgets for preferences displays
  ****************************************************************************
- * Copyright (C) 2006 the VideoLAN team
+ * Copyright (C) 2006-2007 the VideoLAN team
  * $Id$
  *
  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
  *          Antoine Cellerier <dionoea@videolan.org>
+ *          Jean-Baptiste Kempf <jb@videolan.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -32,7 +33,9 @@
  */
 
 #include "components/preferences_widgets.hpp"
+#include "util/customwidgets.hpp"
 #include "qt4.hpp"
+
 #include <QLineEdit>
 #include <QString>
 #include <QSpinBox>
 #include <QVariant>
 #include <QComboBox>
 #include <QGridLayout>
+#include <QPushButton>
+#include <QSlider>
+#include <QFileDialog>
+
+#include <vlc_keys.h>
 
 ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
                                              module_config_t *p_item,
@@ -87,7 +95,7 @@ ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
         if( p_item->i_list )
             p_control = new IntegerListConfigControl( p_this, p_item,
                                             parent, false, l, line );
-        else if( p_item->i_min || p_item->i_max )
+        else if( p_item->min.i || p_item->max.i )
             p_control = new IntegerRangeConfigControl( p_this, p_item, parent,
                                                        l, line );
         else
@@ -95,16 +103,21 @@ ConfigControl *ConfigControl::createControl( vlc_object_t *p_this,
                                                   l, line );
         break;
     case CONFIG_ITEM_FILE:
-        fprintf( stderr, "Todo (CONFIG_ITEM_FILE)\n" );
+        p_control = new FileConfigControl( p_this, p_item, parent, l,
+                                                line, false );
         break;
     case CONFIG_ITEM_DIRECTORY:
-        fprintf( stderr, "Todo (CONFIG_ITEM_DIRECTORY)\n" );
+        p_control = new DirectoryConfigControl( p_this, p_item, parent, l,
+                                                line, false );
+        break;
+    case CONFIG_ITEM_KEY:
+        p_control = new KeySelectorControl( p_this, p_item, parent, l, line );
         break;
     case CONFIG_ITEM_BOOL:
         p_control = new BoolConfigControl( p_this, p_item, parent, l, line );
         break;
     case CONFIG_ITEM_FLOAT:
-        if( p_item->f_min || p_item->f_max )
+        if( p_item->min.f || p_item->max.f )
             p_control = new FloatRangeConfigControl( p_this, p_item, parent,
                                                      l, line );
         else
@@ -124,13 +137,15 @@ void ConfigControl::doApply( intf_thread_t *p_intf )
         case 1:
         {
             VIntConfigControl *vicc = qobject_cast<VIntConfigControl *>(this);
+            assert( vicc );
             config_PutInt( p_intf, vicc->getName(), vicc->getValue() );
             break;
         }
         case 2:
         {
-            VFloatConfigControl *vfcc = 
+            VFloatConfigControl *vfcc =
                                     qobject_cast<VFloatConfigControl *>(this);
+            assert( vfcc );
             config_PutFloat( p_intf, vfcc->getName(), vfcc->getValue() );
             break;
         }
@@ -138,7 +153,15 @@ void ConfigControl::doApply( intf_thread_t *p_intf )
         {
             VStringConfigControl *vscc =
                             qobject_cast<VStringConfigControl *>(this);
+            assert( vscc );
             config_PutPsz( p_intf, vscc->getName(), qta( vscc->getValue() ) );
+            break;
+        }
+        case 4:
+        {
+            KeySelectorControl *ksc = qobject_cast<KeySelectorControl *>(this);
+            assert( ksc );
+            ksc->doApply();
         }
     }
 }
@@ -155,7 +178,7 @@ StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
                            VStringConfigControl( _p_this, _p_item, _parent )
 {
     label = new QLabel( qfu(p_item->psz_text) );
-    text = new QLineEdit( qfu(p_item->psz_value) );
+    text = new QLineEdit( qfu(p_item->value.psz) );
     finish();
 
     if( !l )
@@ -182,12 +205,93 @@ StringConfigControl::StringConfigControl( vlc_object_t *_p_this,
 
 void StringConfigControl::finish()
 {
-    text->setText( qfu(p_item->psz_value) );
+    text->setText( qfu(p_item->value.psz) );
     text->setToolTip( qfu(p_item->psz_longtext) );
     if( label )
         label->setToolTip( qfu(p_item->psz_longtext) );
 }
 
+/*********** File **************/
+FileConfigControl::FileConfigControl( vlc_object_t *_p_this,
+                                          module_config_t *_p_item,
+                                          QWidget *_parent, QGridLayout *l,
+                                          int &line, bool pwd ) :
+                           VStringConfigControl( _p_this, _p_item, _parent )
+{
+    label = new QLabel( qfu(p_item->psz_text) );
+    text = new QLineEdit( qfu(p_item->value.psz) );
+    browse = new QPushButton( qtr( "Browse" ) );
+
+    BUTTONACT( browse, updateField() );
+
+    finish();
+
+    if( !l )
+    {
+        QHBoxLayout *layout = new QHBoxLayout();
+        layout->addWidget( label, 0 ); layout->addWidget( text, 1 );
+        layout->addWidget( browse, 2 );
+        widget->setLayout( layout );
+    }
+    else
+    {
+        l->addWidget( label, line, 0 ); l->addWidget( text, line, 1 );
+        l->addWidget( browse, line, 2 );
+    }
+}
+
+
+FileConfigControl::FileConfigControl( vlc_object_t *_p_this,
+                                   module_config_t *_p_item,
+                                   QLabel *_label, QLineEdit *_text, 
+                                   QPushButton *_button, bool pwd ):
+                           VStringConfigControl( _p_this, _p_item )
+{
+    browse = _button;
+    text = _text;
+    label = _label;
+
+    BUTTONACT( browse, updateField() );
+
+    finish( );
+}
+
+void FileConfigControl::updateField()
+{
+        text->setText( QFileDialog::getOpenFileName( NULL, 
+                qtr( "Select File" ), qfu( p_this->p_libvlc->psz_homedir ) ) );
+}
+
+void FileConfigControl::finish()
+{
+    text->setText( qfu(p_item->value.psz) );
+    text->setToolTip( qfu(p_item->psz_longtext) );
+    if( label )
+        label->setToolTip( qfu(p_item->psz_longtext) );
+}
+
+/********* String / Directory **********/
+
+DirectoryConfigControl::DirectoryConfigControl( vlc_object_t *_p_this,
+                        module_config_t *_p_item, QWidget *_p_widget,
+                        QGridLayout *_p_layout, int& _int, bool _pwd ) :
+     FileConfigControl( _p_this, _p_item, _p_widget, _p_layout, _int, _pwd)
+{}
+
+DirectoryConfigControl::DirectoryConfigControl( vlc_object_t *_p_this,
+                        module_config_t *_p_item, QLabel *_p_label,
+                        QLineEdit *_p_line, QPushButton *_p_button, bool _pwd ):
+     FileConfigControl( _p_this, _p_item, _p_label, _p_line, _p_button, _pwd)
+{}
+
+
+void DirectoryConfigControl::updateField()
+{
+    text->setText( QFileDialog::getOpenFileName( NULL,
+                qtr( "Select File" ), qfu( p_this->p_libvlc->psz_homedir ),
+                NULL, 0,  QFileDialog::ShowDirsOnly ) );
+}
+
 /********* String / choice list **********/
 StringListConfigControl::StringListConfigControl( vlc_object_t *_p_this,
                module_config_t *_p_item, QWidget *_parent, bool bycat,
@@ -228,7 +332,7 @@ void StringListConfigControl::finish( bool bycat )
                             p_item->ppsz_list_text[i_index] :
                             p_item->ppsz_list[i_index] ),
                         QVariant( p_item->ppsz_list[i_index] ) );
-        if( p_item->psz_value && !strcmp( p_item->psz_value,
+        if( p_item->value.psz && !strcmp( p_item->value.psz,
                                           p_item->ppsz_list[i_index] ) )
             combo->setCurrentIndex( combo->count() - 1 );
     }
@@ -290,24 +394,24 @@ void ModuleConfigControl::finish( bool bycat )
         {
             if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
 
-            module_config_t *p_config = p_parser->p_config;
-            if( p_config ) do
+            for (size_t i = 0; i < p_parser->confsize; i++)
             {
+                module_config_t *p_config = p_parser->p_config + i;
                 /* Hack: required subcategory is stored in i_min */
                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
-                    p_config->i_value == p_item->i_min )
+                    p_config->value.i == p_item->min.i )
                     combo->addItem( qfu(p_parser->psz_longname),
                                     QVariant( p_parser->psz_object_name ) );
-                if( p_item->psz_value && !strcmp( p_item->psz_value,
+                if( p_item->value.psz && !strcmp( p_item->value.psz,
                                                   p_parser->psz_object_name) )
                     combo->setCurrentIndex( combo->count() - 1 );
-            } while( p_config->i_type != CONFIG_HINT_END && p_config++ );
+            }
         }
         else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
         {
             combo->addItem( qfu(p_parser->psz_longname),
                             QVariant( p_parser->psz_object_name ) );
-            if( p_item->psz_value && !strcmp( p_item->psz_value,
+            if( p_item->value.psz && !strcmp( p_item->value.psz,
                                               p_parser->psz_object_name) )
                 combo->setCurrentIndex( combo->count() - 1 );
         }
@@ -387,19 +491,19 @@ void ModuleListConfigControl::finish( bool bycat )
         {
             if( !strcmp( p_parser->psz_object_name, "main" ) ) continue;
 
-            module_config_t *p_config = p_parser->p_config;
-            if( p_config ) do
+            for (size_t i = 0; i < p_parser->confsize; i++)
             {
+                module_config_t *p_config = p_parser->p_config + i;
                 /* Hack: required subcategory is stored in i_min */
                 if( p_config->i_type == CONFIG_SUBCATEGORY &&
-                    p_config->i_value == p_item->i_min )
+                    p_config->value.i == p_item->min.i )
                 {
                     QCheckBox *cb =
                         new QCheckBox( qfu( p_parser->psz_object_name ) );
                     cb->setToolTip( qfu(p_parser->psz_longname) );
                     modules.push_back( cb );
                 }
-            } while( p_config->i_type != CONFIG_HINT_END && p_config++ );
+            }
         }
         else if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
         {
@@ -494,7 +598,7 @@ void IntegerConfigControl::finish()
 {
     spin->setMaximum( 2000000000 );
     spin->setMinimum( -2000000000 );
-    spin->setValue( p_item->i_value );
+    spin->setValue( p_item->value.i );
     spin->setToolTip( qfu(p_item->psz_longtext) );
     if( label )
         label->setToolTip( qfu(p_item->psz_longtext) );
@@ -525,10 +629,32 @@ IntegerRangeConfigControl::IntegerRangeConfigControl( vlc_object_t *_p_this,
 
 void IntegerRangeConfigControl::finish()
 {
-    spin->setMaximum( p_item->i_max );
-    spin->setMinimum( p_item->i_min );
+    spin->setMaximum( p_item->max.i );
+    spin->setMinimum( p_item->min.i );
+}
+
+IntegerRangeSliderConfigControl::IntegerRangeSliderConfigControl(
+                                            vlc_object_t *_p_this,
+                                            module_config_t *_p_item,
+                                            QLabel *_label, QSlider *_slider ):
+                    VIntConfigControl( _p_this, _p_item )
+{
+    slider = _slider;
+    label = _label;
+    slider->setMaximum( p_item->max.i );
+    slider->setMinimum( p_item->min.i );
+    slider->setValue( p_item->value.i );
+    slider->setToolTip( qfu(p_item->psz_longtext) );
+    if( label )
+        label->setToolTip( qfu(p_item->psz_longtext) );
+}
+
+int IntegerRangeSliderConfigControl::getValue()
+{
+        return slider->value();
 }
 
+
 /********* Integer / choice list **********/
 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *_p_this,
                module_config_t *_p_item, QWidget *_parent, bool bycat,
@@ -567,7 +693,7 @@ void IntegerListConfigControl::finish( bool bycat )
     {
         combo->addItem( qfu(p_item->ppsz_list_text[i_index] ),
                         QVariant( p_item->pi_list[i_index] ) );
-        if( p_item->i_value == p_item->pi_list[i_index] )
+        if( p_item->value.i == p_item->pi_list[i_index] )
             combo->setCurrentIndex( combo->count() - 1 );
     }
     combo->setToolTip( qfu(p_item->psz_longtext) );
@@ -614,7 +740,7 @@ BoolConfigControl::BoolConfigControl( vlc_object_t *_p_this,
 
 void BoolConfigControl::finish()
 {
-    checkbox->setCheckState( p_item->i_value == VLC_TRUE ? Qt::Checked
+    checkbox->setCheckState( p_item->value.i == VLC_TRUE ? Qt::Checked
                                                         : Qt::Unchecked );
     checkbox->setToolTip( qfu(p_item->psz_longtext) );
 }
@@ -669,7 +795,7 @@ void FloatConfigControl::finish()
     spin->setMaximum( 2000000000. );
     spin->setMinimum( -2000000000. );
     spin->setSingleStep( 0.1 );
-    spin->setValue( (double)p_item->f_value );
+    spin->setValue( (double)p_item->value.f );
     spin->setToolTip( qfu(p_item->psz_longtext) );
     if( label )
         label->setToolTip( qfu(p_item->psz_longtext) );
@@ -701,6 +827,157 @@ FloatRangeConfigControl::FloatRangeConfigControl( vlc_object_t *_p_this,
 
 void FloatRangeConfigControl::finish()
 {
-    spin->setMaximum( (double)p_item->f_max );
-    spin->setMinimum( (double)p_item->f_min );
+    spin->setMaximum( (double)p_item->max.f );
+    spin->setMinimum( (double)p_item->min.f );
+}
+
+
+/**********************************************************************
+ * Key selector widget
+ **********************************************************************/
+KeySelectorControl::KeySelectorControl( vlc_object_t *_p_this,
+                                      module_config_t *_p_item,
+                                      QWidget *_parent, QGridLayout *l,
+                                      int &line ) :
+                                ConfigControl( _p_this, _p_item, _parent )
+
+{
+    label = new QLabel( qtr("Select an action to change the associated hotkey") );
+    table = new QTreeWidget( 0 );
+    finish();
+
+    if( !l )
+    {
+        QVBoxLayout *layout = new QVBoxLayout();
+        layout->addWidget( label, 0 ); layout->addWidget( table, 1 );
+        widget->setLayout( layout );
+    }
+    else
+    {
+        l->addWidget( label, line, 0, 1, 2 );
+        l->addWidget( table, line+1, 0, 1,2 );
+    }
+}
+
+void KeySelectorControl::finish()
+{
+    if( label )
+        label->setToolTip( qfu(p_item->psz_longtext) );
+
+    /* Fill the table */
+    table->setColumnCount( 2 );
+    table->setAlternatingRowColors( true );
+
+    module_t *p_main = config_FindModule( p_this, "main" );
+    assert( p_main );
+
+    for (size_t i = 0; i < p_main->confsize; i++)
+    {
+        module_config_t *p_item = p_main->p_config + i;
+
+        if( p_item->i_type & CONFIG_ITEM && p_item->psz_name &&
+            strstr( p_item->psz_name , "key-" ) )
+        {
+            QTreeWidgetItem *treeItem = new QTreeWidgetItem();
+            treeItem->setText( 0, qfu( p_item->psz_text ) );
+            treeItem->setText( 1, VLCKeyToString( p_item->value.i ) );
+            treeItem->setData( 0, Qt::UserRole,
+                                  QVariant::fromValue( (void*)p_item ) );
+            values += p_item;
+            table->addTopLevelItem( treeItem );
+        }
+    }
+    table->resizeColumnToContents( 0 );
+
+    CONNECT( table, itemDoubleClicked( QTreeWidgetItem *, int ),
+             this, selectKey( QTreeWidgetItem * ) );
+}
+
+void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem )
+{
+   module_config_t *p_keyItem = static_cast<module_config_t*>
+                          (keyItem->data( 0, Qt::UserRole ).value<void*>());
+
+    KeyInputDialog *d = new KeyInputDialog( values, p_keyItem->psz_text );
+    d->exec();
+    if( d->result() == QDialog::Accepted )
+    {
+        p_keyItem->value.i = d->keyValue;
+        if( d->conflicts )
+        {
+            for( int i = 0; i < table->topLevelItemCount() ; i++ )
+            {
+                QTreeWidgetItem *it = table->topLevelItem(i);
+                module_config_t *p_item = static_cast<module_config_t*>
+                              (it->data( 0, Qt::UserRole ).value<void*>());
+                it->setText( 1, VLCKeyToString( p_item->value.i ) );
+            }
+        }
+        else
+            keyItem->setText( 1, VLCKeyToString( p_keyItem->value.i ) );
+    }
+    delete d;
+}
+
+void KeySelectorControl::doApply()
+{
+    foreach( module_config_t *p_current, values )
+    {
+        config_PutInt( p_this, p_current->psz_name, p_current->value.i );
+    }
+}
+
+KeyInputDialog::KeyInputDialog( QList<module_config_t*>& _values,
+                                const char * _keyToChange ) :
+                                                QDialog(0), keyValue(0)
+{
+    setModal( true );
+    values = _values;
+    conflicts = false;
+    keyToChange = _keyToChange;
+    setWindowTitle( qtr( "Hotkey for " ) + qfu( keyToChange)  );
+
+    QVBoxLayout *l = new QVBoxLayout( this );
+    selected = new QLabel( qtr("Press the new keys for ")  + qfu(keyToChange) );
+    warning = new QLabel();
+    l->addWidget( selected , Qt::AlignCenter );
+    l->addWidget( warning, Qt::AlignCenter );
+
+    QHBoxLayout *l2 = new QHBoxLayout();
+    QPushButton *ok = new QPushButton( qtr("OK") );
+    l2->addWidget( ok );
+    QPushButton *cancel = new QPushButton( qtr("Cancel") );
+    l2->addWidget( cancel );
+
+    BUTTONACT( ok, accept() );
+    BUTTONACT( cancel, reject() );
+
+    l->addLayout( l2 );
+}
+
+void KeyInputDialog::keyPressEvent( QKeyEvent *e )
+{
+    if( e->key() == Qt::Key_Tab ) return;
+    int i_vlck = qtEventToVLCKey( e );
+    selected->setText( VLCKeyToString( i_vlck ) );
+    conflicts = false;
+    module_config_t *p_current = NULL;
+    foreach( p_current, values )
+    {
+        if( p_current->value.i == i_vlck && strcmp( p_current->psz_text,
+                                                    keyToChange ) )
+        {
+            p_current->value.i = 0;
+            conflicts = true;
+            break;
+        }
+    }
+    if( conflicts )
+    {
+        warning->setText(
+          qtr("Warning: the  key is already assigned to \"") +
+          QString( p_current->psz_text ) + "\"" );
+    }
+    else warning->setText( "" );
+    keyValue = i_vlck;
 }