56ef09881d6f1c89d636f070b2a80dd56a255490
[vlc.git] / modules / gui / kde / preferences.cpp
1 #include <kdialogbase.h>
2 #include <qmap.h>
3 #include <qcheckbox.h>
4 #include <qframe.h>
5 #include <qgroupbox.h>
6 #include <qlayout.h>
7 #include <qlabel.h>
8 #include <qlistview.h>
9 #include <qnamespace.h>
10 #include <qobjectlist.h>
11 #include <qspinbox.h>
12 #include <qtooltip.h>
13 #include <qvbox.h>
14
15 #include <kbuttonbox.h>
16 #include <klineedit.h>
17 #include <klocale.h>
18 #include <knuminput.h>
19
20 #include "QConfigItem.h"
21 #include "pluginsbox.h"
22 #include "preferences.h"
23
24 /*
25  construkt a new configuration window for the given module
26 */
27 KPreferences::KPreferences(intf_thread_t *p_intf, const char *psz_module_name,
28                            QWidget *parent, const QString &caption) :
29     KDialogBase ( Tabbed, caption, Ok| Apply|Cancel|User1, Ok, parent,
30                   "vlc preferences", true, false, "Save")
31 {
32     module_t *p_module, *p_module_bis;
33     module_config_t *p_item;
34     QVBox *category_table = NULL;
35     QString *category_label;
36
37     this->p_intf = p_intf;
38
39     /* Look for the selected module */
40     for( p_module = p_intf->p_vlc->p_module_bank->first ; p_module != NULL ;
41          p_module = p_module->next )
42     {
43
44         if( psz_module_name && !strcmp( psz_module_name, p_module->psz_object_name ) )
45             break;
46     }
47     if( !p_module ) return;
48     p_item = p_module->p_config;
49     do
50     {
51         switch( p_item->i_type )
52         {
53
54         case CONFIG_HINT_CATEGORY:
55         case CONFIG_HINT_END:
56
57             /*
58              * Now we can start taking care of the new category
59              */
60             if( p_item->i_type == CONFIG_HINT_CATEGORY )
61             {
62                 category_label = new QString( p_item->psz_text );
63                 QFrame *page = addPage( *category_label );
64                 QVBoxLayout *toplayout = new QVBoxLayout( page);
65                 QScrollView *sv = new QScrollView(page);
66                 sv->setResizePolicy(QScrollView::AutoOneFit);
67                 sv->setFrameStyle(QScrollView::NoFrame);
68                 toplayout->addWidget(sv);
69                 category_table = new QVBox(sv->viewport());
70                 sv->addChild(category_table);
71                 toplayout->addStretch(10);
72                 category_table->setSpacing(spacingHint());
73             }
74
75             break;
76
77         case CONFIG_ITEM_MODULE:
78
79             {
80                 
81                 vlc_mutex_lock( p_item->p_lock );
82                 KPluginsBox *item_frame =
83                     new KPluginsBox( p_intf, p_item->psz_text,
84                                      p_item->psz_value ? p_item->psz_value :"",
85                                      category_table,
86                                      spacingHint(),
87                                      this );
88                 QConfigItem *ci = new QConfigItem(this,
89                                                   p_item->psz_name,
90                                                   p_item->i_type,
91                                                   p_item->psz_value);
92                 connect(item_frame, SIGNAL(selectionChanged(const QString &)),
93                         ci, SLOT(setValue(const QString &)));
94
95                 
96                 /* build a list of available plugins */
97                 
98 #if 0 /* FIXME */
99                 for( p_module_bis = p_intf->p_vlc->p_module_bank->first ;
100                      p_module_bis != NULL ;
101                      p_module_bis = p_module_bis->next ) {
102                     if( p_module_bis->i_capabilities & (1 << p_item->i_value)){
103                         new QListViewItem(item_frame->getListView(),
104                                           p_module_bis->psz_object_name,
105                                           p_module_bis->psz_longname);
106                     }
107                 }
108 #endif
109                 vlc_mutex_unlock( p_item->p_lock );
110             }
111             break;
112
113         case CONFIG_ITEM_STRING:
114         case CONFIG_ITEM_FILE:
115
116             {
117                 QHBox *hb = new QHBox(category_table);
118                 hb->setSpacing(spacingHint());
119                 new QLabel(p_item->psz_text, hb);
120                 /* add input box with default value */
121                 vlc_mutex_lock( p_item->p_lock );
122                 
123                 KLineEdit *kl = new KLineEdit( p_item->psz_value ?
124                                                p_item->psz_value : "", hb);
125                 QConfigItem *ci = new QConfigItem(this, p_item->psz_name,
126                                                   p_item->i_type,
127                                                   p_item->psz_value ?
128                                                   p_item->psz_value : "");
129                 connect(kl, SIGNAL(textChanged ( const QString & )),
130                         ci, SLOT(setValue( const QString &)));
131                 QToolTip::add(kl, p_item->psz_longtext);
132                 kl->setMaxLength(10);
133                 
134                 vlc_mutex_unlock( p_item->p_lock );
135                 
136             }
137             break;
138
139         case CONFIG_ITEM_INTEGER:
140             /* add input box with default value */
141             {
142                 QHBox *hb = new QHBox(category_table);
143                 hb->setSpacing(spacingHint());
144                 new QLabel(p_item->psz_text, hb);                
145                 QSpinBox *item_adj = new QSpinBox(-1, 99999, 1, hb);
146                 QConfigItem *ci = new QConfigItem(this, p_item->psz_name,
147                                                   p_item->i_type,
148                                                   p_item->i_value);
149                 connect(item_adj, SIGNAL(valueChanged( int)),
150                         ci, SLOT(setValue(int)));
151                 QToolTip::add(item_adj, p_item->psz_longtext);
152                 item_adj->setValue( p_item->i_value );
153             }
154             break;
155
156         case CONFIG_ITEM_FLOAT:
157             {
158                 QHBox *hb = new QHBox(category_table);
159                 hb->setSpacing(spacingHint());
160                 new QLabel(p_item->psz_text, hb);                
161                 KDoubleNumInput *kdi= new KDoubleNumInput(p_item->f_value, hb);
162                 kdi->setRange(-1, 99999, 0.01, false);
163                 QConfigItem *ci = new QConfigItem(this, p_item->psz_name,
164                                                   p_item->i_type,
165                                                   p_item->f_value);
166                 connect(kdi, SIGNAL(valueChanged(double)),
167                         ci, SLOT(setValue(double)));
168                 QToolTip::add(kdi, p_item->psz_longtext);
169                 
170             }
171             break;
172                                                   
173                 
174         case CONFIG_ITEM_BOOL:
175
176             /* add check button */
177             {
178                 QCheckBox *bool_checkbutton =
179                     new QCheckBox(QString(p_item->psz_text), category_table);
180                 QConfigItem *ci = new QConfigItem(this, p_item->psz_name,
181                                                   p_item->i_type,
182                                                   p_item->i_value);
183                 bool_checkbutton->setChecked(p_item->i_value);
184                 connect(bool_checkbutton, SIGNAL(stateChanged( int)),
185                         ci, SLOT(setValue(int)));
186                 QToolTip::add(bool_checkbutton, p_item->psz_longtext);
187
188             }
189             break;
190
191         }
192
193         p_item++;
194     }
195     while( p_item->i_type != CONFIG_HINT_END );
196     exec();
197     
198 }
199
200 /*
201   empty destructor, qt takes care of this (I think)
202 */
203 KPreferences::~KPreferences()
204 {
205 }
206
207 /*
208   return true if the give module is configureable
209 */
210 bool KPreferences::isConfigureable(QString module)
211 {
212     module_t *p_module;
213     for( p_module = p_intf->p_vlc->p_module_bank->first ;
214          p_module != NULL ;
215          p_module = p_module->next ) {
216         if( !module.compare( p_module->psz_object_name ) ) {
217             return p_module->i_config_items != 0;
218         }
219     }
220     return false;
221
222 }
223
224 /*
225   run when the Apply button is pressed, and by the methods for the ok
226   and save buttons
227 */
228 void KPreferences::slotApply()
229 {
230     QObjectList * l = queryList( "QConfigItem" );
231     QObjectListIt it( *l );             // iterate over the config items
232     QObject * obj;
233     while ( (obj=it.current()) != 0 ) {
234         ++it;
235         QConfigItem *p_config = (QConfigItem *)obj;
236         msg_Dbg( p_intf, const_cast<char *>(p_config->name()));
237         msg_Dbg( p_intf, "%d", p_config->getType());
238
239         switch( p_config->getType() ) {
240
241         case CONFIG_ITEM_STRING:
242         case CONFIG_ITEM_FILE:
243         case CONFIG_ITEM_MODULE:
244             if (p_config->sValue()) {
245                 config_PutPsz( p_intf, p_config->name(),
246                                strdup(p_config->sValue().latin1()));
247             }
248             else {
249                 config_PutPsz( p_intf, p_config->name(), NULL );
250             }
251             break;
252         case CONFIG_ITEM_INTEGER:
253         case CONFIG_ITEM_BOOL:
254             config_PutInt( p_intf, p_config->name(), p_config->iValue() );
255             break;
256         case CONFIG_ITEM_FLOAT:
257             config_PutFloat( p_intf, p_config->name(), p_config->fValue() );
258             break;
259         }
260     }
261     delete l;
262 }
263
264 /*
265  run when the Ok button is pressed
266 */
267 void KPreferences::slotOk()
268 {
269     slotApply();
270     accept();
271 }
272
273 /*
274   run when the save button is pressed
275 */
276 void KPreferences::slotUser1()
277 {
278     slotApply();
279     config_SaveConfigFile( p_intf, NULL );
280 }