preferences.cpp
上传用户:riyaled888
上传日期:2009-03-27
资源大小:7338k
文件大小:15k
源码类别:

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * preferences.cpp: preferences window for the kde gui
  3.  *****************************************************************************
  4.  * Copyright (C) 2001 VideoLAN
  5.  * $Id: preferences.cpp 6961 2004-03-05 17:34:23Z sam $
  6.  *
  7.  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no> Mon Aug 12 2002
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  22.  *****************************************************************************/
  23. #include <kdialogbase.h>
  24. #include <qmap.h>
  25. #include <qcheckbox.h>
  26. #include <qframe.h>
  27. #include <qgroupbox.h>
  28. #include <qlayout.h>
  29. #include <qlabel.h>
  30. #include <qlistview.h>
  31. #include <qnamespace.h>
  32. #include <qobjectlist.h>
  33. #include <qslider.h>
  34. #include <qspinbox.h>
  35. #include <qtooltip.h>
  36. #include <qvbox.h>
  37. #include <kbuttonbox.h>
  38. #include <klineedit.h>
  39. #include <klocale.h>
  40. #include <knuminput.h>
  41. #include <kurlrequester.h>
  42. #include <kfiledialog.h>
  43. #include <kcombobox.h>
  44. #include "QConfigItem.h"
  45. #include "pluginsbox.h"
  46. #include "preferences.h"
  47. /*
  48.   construct a new configuration window for the given module
  49. */
  50. KPreferences::KPreferences(intf_thread_t *p_intf, const char *psz_module_name,
  51.                            QWidget *parent, const QString &caption) :
  52.     KDialogBase ( TreeList, caption, Ok| Apply|Cancel|User1, Ok, parent,
  53.                   _("vlc preferences"), true, false, i18n(_("&Save")) )
  54. {
  55.     module_t *p_parser = NULL;
  56.     vlc_list_t *p_list;
  57.     module_config_t *p_item;
  58.     int i_index;
  59.     QVBox *category_table = NULL;
  60.     QString *category_label;
  61.     this->p_intf = p_intf;
  62.     /* List all modules */
  63.     p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
  64.     for( i_index = 0; i_index < p_list->i_count; i_index++ )
  65.     {
  66.         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
  67.         p_item = p_parser->p_config;
  68.         while( p_item && p_item->i_type != CONFIG_HINT_END )
  69.         {
  70.             switch( p_item->i_type )
  71.             {
  72.             case CONFIG_HINT_CATEGORY:
  73.                 /* force the content to the top of the page */
  74.                 if ( category_table )
  75.                 {
  76.                     QWidget *space = new QWidget( category_table );
  77.                     category_table->setStretchFactor( space, 10 );
  78.                     category_table = NULL;
  79.                 }
  80.                 
  81.                 /*
  82.                  * Now we can start taking care of the new category
  83.                  */
  84.                 if( p_item->i_type == CONFIG_HINT_CATEGORY )
  85.                 {
  86.                     category_label = new QString( p_item->psz_text );
  87.                     QStringList path;
  88.                     if ( strcmp( p_parser->psz_object_name, "main" ) )
  89.                     {
  90.                         path += _( "Plugins" );
  91.                         path += p_parser->psz_capability;
  92.                         path += p_parser->psz_object_name;
  93.                     }
  94.                     path += *category_label;
  95.                     QFrame *page = addPage( path );
  96.                     QVBoxLayout *toplayout = new QVBoxLayout( page);
  97.                     QScrollView *sv = new QScrollView(page);
  98.                     sv->setResizePolicy(QScrollView::AutoOneFit);
  99.                     sv->setFrameStyle(QScrollView::NoFrame);
  100.                     toplayout->addWidget(sv);
  101.                     category_table = new QVBox(sv->viewport());
  102.                     sv->addChild(category_table);
  103.                     category_table->setSpacing(spacingHint());
  104.                 }
  105.                 break;
  106.             case CONFIG_ITEM_MODULE:
  107.             {
  108.                 vlc_mutex_lock( p_item->p_lock );
  109.                 KPluginsBox *item_frame =
  110.                     new KPluginsBox( p_intf, p_item->psz_text,
  111.                                      p_item->psz_value ? p_item->psz_value :"",
  112.                                      category_table,
  113.                                      spacingHint(),
  114.                                      this );
  115.                 QConfigItem *ci = new QConfigItem(this,
  116.                                                   p_item->psz_name,
  117.                                                   p_item->i_type,
  118.                                                   p_item->psz_value);
  119.                 connect(item_frame, SIGNAL(selectionChanged(const QString &)),
  120.                         ci, SLOT(setValue(const QString &)));
  121.                 /* build a list of available plugins */
  122.                 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
  123.                 {
  124.                     module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object ;
  125.                     if( !strcmp( p_parser->psz_capability,
  126.                                  p_item->psz_type ) )
  127.                     {
  128.                         new QListViewItem(item_frame->getListView(),
  129.                                           p_parser->psz_object_name,
  130.                                           p_parser->psz_longname);
  131.                     }
  132.                 }
  133.                 vlc_mutex_unlock( p_item->p_lock );
  134.             }
  135.             break;
  136.             case CONFIG_ITEM_STRING:
  137.             {
  138.                 QHBox *hb = new QHBox(category_table);
  139.                 hb->setSpacing(spacingHint());
  140.                 new QLabel(p_item->psz_text, hb);
  141.                 /* add input box with default value */
  142.                 vlc_mutex_lock( p_item->p_lock );
  143.                 QConfigItem *ci = new QConfigItem(this, p_item->psz_name,
  144.                                                   p_item->i_type,
  145.                                                   p_item->psz_value ?
  146.                                                   p_item->psz_value : "");
  147.                 if ( p_item->ppsz_list )
  148.                 {
  149.                     char **ppsz_list = p_item->ppsz_list;
  150.                     KComboBox *p_combobox = new KComboBox( true, hb );
  151.                     QToolTip::add(p_combobox, p_item->psz_longtext);
  152.                     connect(p_combobox, SIGNAL(activated ( const QString & )),
  153.                             ci, SLOT(setValue( const QString &)));
  154.                     while ( *ppsz_list )
  155.                     {
  156.                         p_combobox->insertItem( *ppsz_list );
  157.                         if ( !strcmp( *ppsz_list, p_item->psz_value ?
  158.                                                   p_item->psz_value : "" ) )
  159.                         {
  160. #if KDE_VERSION_MAJOR >= 3
  161.                             p_combobox->setCurrentText( *ppsz_list );
  162. #else
  163.                             p_combobox->setCurrentItem( p_combobox->count() );
  164. #endif
  165.                         }
  166.                         ppsz_list++;
  167.                     }
  168.                 }
  169.                 else
  170.                 {
  171.                     KLineEdit *kl = new KLineEdit( p_item->psz_value ?
  172.                                                    p_item->psz_value : "", hb);
  173.                     connect(kl, SIGNAL(textChanged ( const QString & )),
  174.                             ci, SLOT(setValue( const QString &)));
  175.                     QToolTip::add(kl, p_item->psz_longtext);
  176.                     kl->setMaxLength(40);
  177.                 }
  178.                 vlc_mutex_unlock( p_item->p_lock );
  179.             }
  180.             break;
  181.             case CONFIG_ITEM_FILE:
  182.             case CONFIG_ITEM_DIRECTORY:
  183.             {
  184.                 QHBox *hb = new QHBox(category_table);
  185.                 hb->setSpacing(spacingHint());
  186.                 new QLabel(p_item->psz_text, hb);
  187.                 /* add input box with default value */
  188.                 vlc_mutex_lock( p_item->p_lock );
  189. //            KLineEdit *kl = new KLineEdit( p_item->psz_value ?
  190. //                                           p_item->psz_value : "", hb);
  191.                 QConfigItem *ci = new QConfigItem(this, p_item->psz_name,
  192.                                                   p_item->i_type,
  193.                                                   p_item->psz_value ?
  194.                                                   p_item->psz_value : "");
  195. //            QPushButton *bbrowse = new QPushButton( _("Browse"), hb );
  196.                 KURLRequester *kfile = new KURLRequester( p_item->psz_value ?
  197.                                                           p_item->psz_value : "",
  198.                                                           hb );
  199.                 if ( p_item->i_type == CONFIG_ITEM_DIRECTORY )
  200.                 {
  201.                     kfile->fileDialog()->setMode(KFile::Directory|KFile::ExistingOnly|KFile::LocalOnly);
  202.                 }
  203.                 connect(kfile, SIGNAL(textChanged ( const QString & )),
  204.                         ci, SLOT(setValue( const QString &)));
  205.                 QToolTip::add(kfile, p_item->psz_longtext);
  206.                 vlc_mutex_unlock( p_item->p_lock );
  207.             }
  208.             break;
  209.             case CONFIG_ITEM_INTEGER:
  210.                 /* add input box with default value */
  211.             {
  212.                 QHBox *hb = new QHBox(category_table);
  213.                 hb->setSpacing(spacingHint());
  214.                 new QLabel(p_item->psz_text, hb);
  215.                 QConfigItem *ci = new QConfigItem(this, p_item->psz_name,
  216.                                                   p_item->i_type,
  217.                                                   p_item->i_value);
  218.                 if ( p_item->i_min == 0 && p_item->i_max == 0 )
  219.                 {
  220.                     QSpinBox *item_adj = new QSpinBox(-1, 99999, 1, hb);
  221.                     item_adj->setValue( p_item->i_value );
  222.                     connect(item_adj, SIGNAL(valueChanged( int)),
  223.                             ci, SLOT(setValue(int)));
  224.                     QToolTip::add(item_adj, p_item->psz_longtext);
  225.                 }
  226.                 else
  227.                 {
  228.                     KIntNumInput *p_ii = new KIntNumInput( p_item->i_value, hb );
  229.                     p_ii->setRange( p_item->i_min, p_item->i_max, 1, true ); 
  230.                     connect( p_ii, SIGNAL( valueChanged( int ) ),
  231.                              ci, SLOT( setValue( int ) ) );
  232.                     QToolTip::add( p_ii, p_item->psz_longtext );
  233.                 }
  234.             }
  235.             break;
  236.             case CONFIG_ITEM_FLOAT:
  237.             {
  238.                 QHBox *hb = new QHBox(category_table);
  239.                 hb->setSpacing(spacingHint());
  240.                 new QLabel(p_item->psz_text, hb);
  241.                 KDoubleNumInput *kdi= new KDoubleNumInput(p_item->f_value, hb);
  242.                 if ( p_item->f_min == 0 && p_item->f_max == 0 )
  243.                 {
  244.                     kdi->setRange(-1, 99999, 0.01, false);
  245.                 }
  246.                 else
  247.                 {
  248.                     kdi->setRange( p_item->f_min, p_item->f_max, 0.01, true );
  249.                 }
  250.                 QConfigItem *ci = new QConfigItem(this, p_item->psz_name,
  251.                                                   p_item->i_type,
  252.                                                   p_item->f_value);
  253.                 connect(kdi, SIGNAL(valueChanged(double)),
  254.                         ci, SLOT(setValue(double)));
  255.                 QToolTip::add(kdi, p_item->psz_longtext);
  256.             }
  257.             break;
  258.             case CONFIG_ITEM_BOOL:
  259.                 /* add check button */
  260.             {
  261.                 QCheckBox *bool_checkbutton =
  262.                     new QCheckBox(QString(p_item->psz_text), category_table);
  263.                 QConfigItem *ci = new QConfigItem(this, p_item->psz_name,
  264.                                                   p_item->i_type,
  265.                                                   p_item->i_value);
  266.                 bool_checkbutton->setChecked(p_item->i_value);
  267.                 connect(bool_checkbutton, SIGNAL(stateChanged( int)),
  268.                         ci, SLOT(setValue(int)));
  269.                 QToolTip::add(bool_checkbutton, p_item->psz_longtext);
  270.             }
  271.             break;
  272.             }
  273.             
  274.             p_item++;
  275.         }
  276.     }
  277.     /* force the content to the top of the page, even on the last page */
  278.     if ( category_table )
  279.     {
  280.         QWidget *space = new QWidget( category_table );
  281.         category_table->setStretchFactor( space, 10 );
  282.         category_table = NULL;
  283.     }
  284.                 
  285.     vlc_list_release( p_list );
  286.     exec();
  287. }
  288. /*
  289.   empty destructor, qt takes care of this (I think)
  290. */
  291. KPreferences::~KPreferences()
  292. {
  293. }
  294. /*
  295.   return true if the give module is configureable
  296. */
  297. bool KPreferences::isConfigureable(QString module)
  298. {
  299.     module_t *p_parser;
  300.     vlc_list_t *p_list;
  301.     int i_index;
  302.     p_list = vlc_list_find( this->p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
  303.     for( i_index = 0; i_index < p_list->i_count; i_index++ )
  304.     {
  305.         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
  306.         if( !module.compare( p_parser->psz_object_name ) )
  307.         {
  308.             bool ret = p_parser->i_config_items != 0;
  309.             vlc_list_release( p_list );
  310.             return ret;
  311.         }
  312.     }
  313.     vlc_list_release( p_list );
  314.     return false;
  315. }
  316. /*
  317.   run when the Apply button is pressed, and by the methods for the ok
  318.   and save buttons
  319. */
  320. void KPreferences::slotApply()
  321. {
  322.     QObjectList * l = queryList( "QConfigItem" );
  323.     QObjectListIt it( *l );             // iterate over the config items
  324.     QObject * obj;
  325.     while ( (obj=it.current()) != 0 ) {
  326.         ++it;
  327.         QConfigItem *p_config = (QConfigItem *)obj;
  328.         if ( p_config->changed() )
  329.         {
  330.             msg_Dbg( p_intf, const_cast<char *>(p_config->name()));
  331.             msg_Dbg( p_intf, "%d", p_config->getType());
  332.             switch( p_config->getType() ) {
  333.             case CONFIG_ITEM_DIRECTORY:
  334.             case CONFIG_ITEM_STRING:
  335.             case CONFIG_ITEM_FILE:
  336.             case CONFIG_ITEM_MODULE:
  337.                 if (p_config->sValue()) {
  338.                     config_PutPsz( p_intf, p_config->name(),
  339.                                    strdup(p_config->sValue().latin1()));
  340.                 }
  341.                 else {
  342.                     config_PutPsz( p_intf, p_config->name(), NULL );
  343.                 }
  344.                 break;
  345.             case CONFIG_ITEM_INTEGER:
  346.             case CONFIG_ITEM_BOOL:
  347.                 config_PutInt( p_intf, p_config->name(), p_config->iValue() );
  348.                 break;
  349.             case CONFIG_ITEM_FLOAT:
  350.                 config_PutFloat( p_intf, p_config->name(), p_config->fValue() );
  351.                 break;
  352.             }
  353.             p_config->resetChanged();
  354.         }
  355.     }
  356.     delete l;
  357. }
  358. /*
  359.   run when the Ok button is pressed
  360. */
  361. void KPreferences::slotOk()
  362. {
  363.     slotApply();
  364.     accept();
  365. }
  366. /*
  367.   run when the save button is pressed
  368. */
  369. void KPreferences::slotUser1()
  370. {
  371.     slotApply();
  372.     config_SaveConfigFile( p_intf, NULL );
  373. }