]> git.sesse.net Git - kdenlive/blob - src/kdenlivesettingsdialog.cpp
Small GUI fix
[kdenlive] / src / kdenlivesettingsdialog.cpp
1 /***************************************************************************
2  *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20 #include "kdenlivesettingsdialog.h"
21 #include "profilesdialog.h"
22 #include "v4l/v4lcapture.h"
23 #include "blackmagic/devices.h"
24 #include "kdenlivesettings.h"
25
26 #include <KStandardDirs>
27 #include <KDebug>
28 #include <kopenwithdialog.h>
29 #include <KConfigDialogManager>
30 #include <kde_file.h>
31 #include <KIO/NetAccess>
32 #include <kdeversion.h>
33
34 #include <QDir>
35 #include <QTimer>
36 #include <QTreeWidgetItem>
37 #include <QThread>
38
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <fcntl.h>
43 #ifndef NO_JOGSHUTTLE
44 #include "jogaction.h"
45 #include "jogshuttleconfig.h"
46 #include <linux/input.h>
47 #endif /* NO_JOGSHUTTLE */
48
49
50 KdenliveSettingsDialog::KdenliveSettingsDialog(const QMap<QString, QString>& mappable_actions, QWidget * parent) :
51     KConfigDialog(parent, "settings", KdenliveSettings::self()),
52     m_modified(false),
53     m_shuttleModified(false),
54     m_mappable_actions(mappable_actions)
55 {
56
57     QWidget *p1 = new QWidget;
58     m_configMisc.setupUi(p1);
59     m_page1 = addPage(p1, i18n("Misc"), "configure");
60
61     // Hide multi tab option until Kdenlive really supports it
62     m_configMisc.kcfg_activatetabs->setVisible(false);
63
64     QWidget *p8 = new QWidget;
65     m_configProject.setupUi(p8);
66     m_page8 = addPage(p8, i18n("Project Defaults"), "document-new");
67     connect(m_configProject.kcfg_generateproxy, SIGNAL(toggled(bool)), m_configProject.kcfg_proxyminsize, SLOT(setEnabled(bool)));
68     m_configProject.kcfg_proxyminsize->setEnabled(KdenliveSettings::generateproxy());
69     connect(m_configProject.kcfg_generateimageproxy, SIGNAL(toggled(bool)), m_configProject.kcfg_proxyimageminsize, SLOT(setEnabled(bool)));
70     m_configProject.kcfg_proxyimageminsize->setEnabled(KdenliveSettings::generateimageproxy());
71     m_configProject.kcfg_proxyparams->setMaximumHeight(QFontMetrics(font()).lineSpacing() * 5);
72
73     QWidget *p3 = new QWidget;
74     m_configTimeline.setupUi(p3);
75     m_page3 = addPage(p3, i18n("Timeline"), "video-display");
76
77     QWidget *p2 = new QWidget;
78     m_configEnv.setupUi(p2);
79     m_configEnv.mltpathurl->setMode(KFile::Directory);
80     m_configEnv.mltpathurl->lineEdit()->setObjectName("kcfg_mltpath");
81     m_configEnv.rendererpathurl->lineEdit()->setObjectName("kcfg_rendererpath");
82     m_configEnv.kcfg_mltthreads->setMaximum(QThread::idealThreadCount());
83     m_configEnv.tmppathurl->setMode(KFile::Directory);
84     m_configEnv.tmppathurl->lineEdit()->setObjectName("kcfg_currenttmpfolder");
85     m_configEnv.projecturl->setMode(KFile::Directory);
86     m_configEnv.projecturl->lineEdit()->setObjectName("kcfg_defaultprojectfolder");
87     m_configEnv.capturefolderurl->setMode(KFile::Directory);
88     m_configEnv.capturefolderurl->lineEdit()->setObjectName("kcfg_capturefolder");
89     m_configEnv.capturefolderurl->setEnabled(!KdenliveSettings::capturetoprojectfolder());
90     connect(m_configEnv.kcfg_capturetoprojectfolder, SIGNAL(clicked()), this, SLOT(slotEnableCaptureFolder()));
91     m_page2 = addPage(p2, i18n("Environment"), "application-x-executable-script");
92
93     QWidget *p4 = new QWidget;
94     m_configCapture.setupUi(p4);
95
96 #if !defined(Q_WS_MAC) && !defined(Q_OS_FREEBSD)
97     V4lCaptureHandler v4l(NULL);
98     // Video 4 Linux device detection
99     for (int i = 0; i < 10; i++) {
100         QString path = "/dev/video" + QString::number(i);
101         if (QFile::exists(path)) {
102             QStringList deviceInfo = v4l.getDeviceName(path);
103             if (!deviceInfo.isEmpty()) {
104                 m_configCapture.kcfg_detectedv4ldevices->addItem(deviceInfo.at(0), path);
105                 m_configCapture.kcfg_detectedv4ldevices->setItemData(m_configCapture.kcfg_detectedv4ldevices->count() - 1, deviceInfo.at(1), Qt::UserRole + 1);
106             }
107         }
108     }
109     connect(m_configCapture.kcfg_detectedv4ldevices, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdatev4lDevice()));
110 #endif
111
112     m_page4 = addPage(p4, i18n("Capture"), "media-record");
113     m_configCapture.tabWidget->setCurrentIndex(KdenliveSettings::defaultcapture());
114 #ifdef Q_WS_MAC
115     m_configCapture.tabWidget->setEnabled(false);
116     m_configCapture.kcfg_defaultcapture->setEnabled(false);
117     m_configCapture.label->setText(i18n("Capture is not yet available on OS X."));
118 #endif
119
120     QWidget *p5 = new QWidget;
121     m_configShuttle.setupUi(p5);
122 #ifdef NO_JOGSHUTTLE
123     m_configShuttle.kcfg_enableshuttle->hide();
124     m_configShuttle.kcfg_enableshuttle->setDisabled(true);
125 #else
126     connect(m_configShuttle.kcfg_enableshuttle, SIGNAL(stateChanged(int)), this, SLOT(slotCheckShuttle(int)));
127     connect(m_configShuttle.shuttledevicelist, SIGNAL(activated(int)), this, SLOT(slotUpdateShuttleDevice(int)));
128     slotCheckShuttle(KdenliveSettings::enableshuttle());
129     m_configShuttle.shuttledisabled->hide();
130
131     // Store the button pointers into an array for easier handling them in the other functions.
132     m_shuttle_buttons.push_back(m_configShuttle.shuttle1);
133     m_shuttle_buttons.push_back(m_configShuttle.shuttle2);
134     m_shuttle_buttons.push_back(m_configShuttle.shuttle3);
135     m_shuttle_buttons.push_back(m_configShuttle.shuttle4);
136     m_shuttle_buttons.push_back(m_configShuttle.shuttle5);
137
138     // populate the buttons with the current configuration. The items are sorted
139     // according to the user-selected language, so they do not appear in random order.
140     QList<QString> action_names = mappable_actions.keys();
141     qSort(action_names);
142
143     // Here we need to compute the action_id -> index-in-action_names. We iterate over the
144     // action_names, as the sorting may depend on the user-language.
145     QStringList actions_map = JogShuttleConfig::actionMap(KdenliveSettings::shuttlebuttons());
146     QMap<QString, int> action_pos;
147     foreach (const QString& action_id, actions_map) {
148       // This loop find out at what index is the string that would map to the action_id.
149       for (int i = 0; i < action_names.size(); i++) {
150           if (mappable_actions[action_names[i]] == action_id) {
151               action_pos[action_id] = i;
152               break;
153           }
154       }
155     }
156
157     int i = 0;
158     foreach (KComboBox* button, m_shuttle_buttons) {
159       button->addItems(action_names);
160       connect(button, SIGNAL(activated(int)), this, SLOT(slotShuttleModified()));
161       ++i;
162       if (i < actions_map.size())
163         button->setCurrentIndex(action_pos[actions_map[i]]);
164     }
165 #endif /* NO_JOGSHUTTLE */
166     m_page5 = addPage(p5, i18n("JogShuttle"), "input-mouse");
167
168     QWidget *p6 = new QWidget;
169     m_configSdl.setupUi(p6);
170
171 #if not defined(Q_WS_MAC) && not defined(USE_OPEN_GL)
172     m_configSdl.kcfg_openglmonitors->setHidden(true);
173 #endif
174
175     m_page6 = addPage(p6, i18n("Playback"), "media-playback-start");
176
177     QWidget *p7 = new QWidget;
178     m_configTranscode.setupUi(p7);
179     m_page7 = addPage(p7, i18n("Transcode"), "edit-copy");
180     connect(m_configTranscode.button_add, SIGNAL(clicked()), this, SLOT(slotAddTranscode()));
181     connect(m_configTranscode.button_delete, SIGNAL(clicked()), this, SLOT(slotDeleteTranscode()));
182     connect(m_configTranscode.profiles_list, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotDialogModified()));
183     
184     connect(m_configCapture.kcfg_video4vdevice, SIGNAL(editingFinished()), this, SLOT(rebuildVideo4Commands()));
185     connect(m_configCapture.kcfg_video4adevice, SIGNAL(editingFinished()), this, SLOT(rebuildVideo4Commands()));
186     connect(m_configCapture.kcfg_video4vcodec, SIGNAL(editingFinished()), this, SLOT(rebuildVideo4Commands()));
187     connect(m_configCapture.kcfg_video4acodec, SIGNAL(editingFinished()), this, SLOT(rebuildVideo4Commands()));
188     connect(m_configCapture.kcfg_video4vformat, SIGNAL(editingFinished()), this, SLOT(rebuildVideo4Commands()));
189     connect(m_configCapture.kcfg_video4aformat, SIGNAL(editingFinished()), this, SLOT(rebuildVideo4Commands()));
190     connect(m_configCapture.kcfg_video4size, SIGNAL(editingFinished()), this, SLOT(rebuildVideo4Commands()));
191     connect(m_configCapture.kcfg_video4rate, SIGNAL(editingFinished()), this, SLOT(rebuildVideo4Commands()));
192
193     connect(m_configCapture.kcfg_rmd_capture_audio, SIGNAL(clicked(bool)), m_configCapture.audio_group, SLOT(setVisible(bool)));
194
195     m_configCapture.audio_group->setVisible(KdenliveSettings::rmd_capture_audio());
196
197     connect(m_configEnv.kp_image, SIGNAL(clicked()), this, SLOT(slotEditImageApplication()));
198     connect(m_configEnv.kp_audio, SIGNAL(clicked()), this, SLOT(slotEditAudioApplication()));
199     connect(m_configEnv.kp_player, SIGNAL(clicked()), this, SLOT(slotEditVideoApplication()));
200
201     checkProfile();
202
203     slotUpdateDisplay();
204
205     connect(m_configSdl.kcfg_audio_driver, SIGNAL(currentIndexChanged(int)), this, SLOT(slotCheckAlsaDriver()));
206     initDevices();
207     connect(m_configProject.kcfg_profiles_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateDisplay()));
208     connect(m_configCapture.kcfg_rmd_capture_type, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateRmdRegionStatus()));
209
210     slotUpdateRmdRegionStatus();
211     loadTranscodeProfiles();
212
213
214     //HACK: check dvgrab version, because only dvgrab >= 3.3 supports
215     //   --timestamp option without bug
216
217     if (KdenliveSettings::dvgrab_path().isEmpty() || !QFile::exists(KdenliveSettings::dvgrab_path())) {
218         QString dvgrabpath = KStandardDirs::findExe("dvgrab");
219         KdenliveSettings::setDvgrab_path(dvgrabpath);
220     }
221
222
223     BMInterface::getBlackMagicDeviceList(m_configCapture.kcfg_hdmi_capturedevice, m_configCapture.kcfg_hdmi_capturemode);
224     connect(m_configCapture.kcfg_hdmi_capturedevice, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateHDMIModes()));
225
226     if (BMInterface::getBlackMagicOutputDeviceList(m_configSdl.kcfg_blackmagic_output_device)) {
227         // Found blackmagic card
228     } else m_configSdl.kcfg_external_display->setEnabled(false);
229
230     double dvgrabVersion = 0;
231     if (!KdenliveSettings::dvgrab_path().isEmpty()) {
232         QProcess *versionCheck = new QProcess;
233         versionCheck->setProcessChannelMode(QProcess::MergedChannels);
234         versionCheck->start("dvgrab", QStringList() << "--version");
235         if (versionCheck->waitForFinished()) {
236             QString version = QString(versionCheck->readAll()).simplified();
237             if (version.contains(' ')) version = version.section(' ', -1);
238             dvgrabVersion = version.toDouble();
239
240             kDebug() << "// FOUND DVGRAB VERSION: " << dvgrabVersion;
241         }
242         delete versionCheck;
243         if (dvgrabVersion < 3.3) {
244             KdenliveSettings::setFirewiretimestamp(false);
245             m_configCapture.kcfg_firewiretimestamp->setEnabled(false);
246         }
247         m_configCapture.dvgrab_info->setText(i18n("dvgrab version %1 at %2", dvgrabVersion, KdenliveSettings::dvgrab_path()));
248     } else m_configCapture.dvgrab_info->setText(i18n("<strong><em>dvgrab</em> utility not found, please install it for firewire capture</strong>"));
249
250     if (KdenliveSettings::rmd_path().isEmpty() || !QFile::exists(KdenliveSettings::rmd_path())) {
251         QString rmdpath = KStandardDirs::findExe("recordmydesktop");
252         KdenliveSettings::setRmd_path(rmdpath);
253     }
254     if (KdenliveSettings::rmd_path().isEmpty())
255         m_configCapture.rmd_info->setText(i18n("<strong><em>Recordmydesktop</em> utility not found, please install it for screen grabs</strong>"));
256     else
257         m_configCapture.rmd_info->setText(i18n("Recordmydesktop found at: %1", KdenliveSettings::rmd_path()));
258 }
259
260 KdenliveSettingsDialog::~KdenliveSettingsDialog() {}
261
262 void KdenliveSettingsDialog::slotUpdateHDMIModes()
263 {
264     QStringList modes = m_configCapture.kcfg_hdmi_capturedevice->itemData(m_configCapture.kcfg_hdmi_capturedevice->currentIndex()).toStringList();
265     m_configCapture.kcfg_hdmi_capturemode->clear();
266     m_configCapture.kcfg_hdmi_capturemode->insertItems(0, modes);
267 }
268
269 void KdenliveSettingsDialog::slotUpdateRmdRegionStatus()
270 {
271     m_configCapture.region_group->setHidden(m_configCapture.kcfg_rmd_capture_type->currentIndex() != 1);
272 }
273
274 void KdenliveSettingsDialog::slotEnableCaptureFolder()
275 {
276     m_configEnv.capturefolderurl->setEnabled(!m_configEnv.kcfg_capturetoprojectfolder->isChecked());
277 }
278
279 void KdenliveSettingsDialog::checkProfile()
280 {
281     m_configProject.kcfg_profiles_list->clear();
282     QMap <QString, QString> profilesInfo = ProfilesDialog::getProfilesInfo();
283     QMapIterator<QString, QString> i(profilesInfo);
284     while (i.hasNext()) {
285         i.next();
286         m_configProject.kcfg_profiles_list->addItem(i.key(), i.value());
287     }
288
289     if (!KdenliveSettings::default_profile().isEmpty()) {
290         for (int i = 0; i < m_configProject.kcfg_profiles_list->count(); i++) {
291             if (m_configProject.kcfg_profiles_list->itemData(i).toString() == KdenliveSettings::default_profile()) {
292                 m_configProject.kcfg_profiles_list->setCurrentIndex(i);
293                 KdenliveSettings::setProfiles_list(i);
294                 break;
295             }
296         }
297     }
298 }
299
300 void KdenliveSettingsDialog::initDevices()
301 {
302     // Fill audio drivers
303     m_configSdl.kcfg_audio_driver->addItem(i18n("Automatic"), QString());
304 #ifndef Q_WS_MAC
305     m_configSdl.kcfg_audio_driver->addItem(i18n("OSS"), "dsp");
306     m_configSdl.kcfg_audio_driver->addItem(i18n("ALSA"), "alsa");
307     m_configSdl.kcfg_audio_driver->addItem(i18n("PulseAudio"), "pulse");
308     m_configSdl.kcfg_audio_driver->addItem(i18n("OSS with DMA access"), "dma");
309     m_configSdl.kcfg_audio_driver->addItem(i18n("Esound daemon"), "esd");
310     m_configSdl.kcfg_audio_driver->addItem(i18n("ARTS daemon"), "artsc");
311 #endif
312
313     if (!KdenliveSettings::audiodrivername().isEmpty())
314         for (int i = 1; i < m_configSdl.kcfg_audio_driver->count(); i++) {
315             if (m_configSdl.kcfg_audio_driver->itemData(i).toString() == KdenliveSettings::audiodrivername()) {
316                 m_configSdl.kcfg_audio_driver->setCurrentIndex(i);
317                 KdenliveSettings::setAudio_driver((uint) i);
318             }
319         }
320
321     // Fill video drivers
322     m_configSdl.kcfg_video_driver->addItem(i18n("Automatic"), QString());
323 #ifndef Q_WS_MAC
324     m_configSdl.kcfg_video_driver->addItem(i18n("XVideo"), "x11");
325     m_configSdl.kcfg_video_driver->addItem(i18n("X11"), "x11_noaccel");
326     m_configSdl.kcfg_video_driver->addItem(i18n("XFree86 DGA 2.0"), "dga");
327     m_configSdl.kcfg_video_driver->addItem(i18n("Nano X"), "nanox");
328     m_configSdl.kcfg_video_driver->addItem(i18n("Framebuffer console"), "fbcon");
329     m_configSdl.kcfg_video_driver->addItem(i18n("Direct FB"), "directfb");
330     m_configSdl.kcfg_video_driver->addItem(i18n("SVGAlib"), "svgalib");
331     m_configSdl.kcfg_video_driver->addItem(i18n("General graphics interface"), "ggi");
332     m_configSdl.kcfg_video_driver->addItem(i18n("Ascii art library"), "aalib");
333 #endif
334
335     // Fill the list of audio playback devices
336     m_configSdl.kcfg_audio_device->addItem(i18n("Default"), QString());
337     m_configCapture.kcfg_rmd_alsa_device->addItem(i18n("Default"), QString());
338     if (!KStandardDirs::findExe("aplay").isEmpty()) {
339         m_readProcess.setOutputChannelMode(KProcess::OnlyStdoutChannel);
340         m_readProcess.setProgram("aplay", QStringList() << "-l");
341         connect(&m_readProcess, SIGNAL(readyReadStandardOutput()) , this, SLOT(slotReadAudioDevices()));
342         m_readProcess.execute(5000);
343     } else {
344         // If aplay is not installed on the system, parse the /proc/asound/pcm file
345         QFile file("/proc/asound/pcm");
346         if (file.open(QIODevice::ReadOnly)) {
347             QTextStream stream(&file);
348             QString line;
349             QString deviceId;
350             while (!stream.atEnd()) {
351                 line = stream.readLine();
352                 if (line.contains("playback")) {
353                     deviceId = line.section(':', 0, 0);
354                     m_configSdl.kcfg_audio_device->addItem(line.section(':', 1, 1), "plughw:" + QString::number(deviceId.section('-', 0, 0).toInt()) + ',' + QString::number(deviceId.section('-', 1, 1).toInt()));
355                 }
356                 if (line.contains("capture")) {
357                     deviceId = line.section(':', 0, 0);
358                     m_configCapture.kcfg_rmd_alsa_device->addItem(line.section(':', 1, 1), "plughw:" + QString::number(deviceId.section('-', 0, 0).toInt()) + ',' + QString::number(deviceId.section('-', 1, 1).toInt()));
359                 }
360             }
361             file.close();
362         }
363     }
364     if (!KdenliveSettings::audiodevicename().isEmpty()) {
365         // Select correct alsa device
366         int ix = m_configSdl.kcfg_audio_device->findData(KdenliveSettings::audiodevicename());
367         m_configSdl.kcfg_audio_device->setCurrentIndex(ix);
368         KdenliveSettings::setAudio_device(ix);
369     }
370     if (!KdenliveSettings::rmd_alsadevicename().isEmpty()) {
371         // Select correct alsa device
372         int ix = m_configCapture.kcfg_rmd_alsa_device->findData(KdenliveSettings::rmd_alsadevicename());
373         m_configCapture.kcfg_rmd_alsa_device->setCurrentIndex(ix);
374         KdenliveSettings::setRmd_alsa_device(ix);
375     }
376 }
377
378
379 void KdenliveSettingsDialog::slotReadAudioDevices()
380 {
381     QString result = QString(m_readProcess.readAllStandardOutput());
382     kDebug() << "// / / / / / READING APLAY: ";
383     kDebug() << result;
384     QStringList lines = result.split('\n');
385     foreach(const QString & data, lines) {
386         kDebug() << "// READING LINE: " << data;
387         if (data.simplified().startsWith("card")) {
388             QString card = data.section(':', 0, 0).section(' ', -1);
389             QString device = data.section(':', 1, 1).section(' ', -1);
390             m_configSdl.kcfg_audio_device->addItem(data.section(':', -1), "plughw:" + card + ',' + device);
391             m_configCapture.kcfg_rmd_alsa_device->addItem(data.section(':', -1), "plughw:" + card + ',' + device);
392         }
393     }
394 }
395
396 void KdenliveSettingsDialog::showPage(int page, int option)
397 {
398     switch (page) {
399     case 1:
400         setCurrentPage(m_page1);
401         break;
402     case 2:
403         setCurrentPage(m_page2);
404         break;
405     case 3:
406         setCurrentPage(m_page3);
407         break;
408     case 4:
409         setCurrentPage(m_page4);
410         m_configCapture.tabWidget->setCurrentIndex(option);
411         break;
412     case 5:
413         setCurrentPage(m_page5);
414         break;
415     case 6:
416         setCurrentPage(m_page6);
417         break;
418     case 7:
419         setCurrentPage(m_page7);
420         break;
421     default:
422         setCurrentPage(m_page1);
423     }
424 }
425
426 void KdenliveSettingsDialog::slotEditVideoApplication()
427 {
428     KService::Ptr service;
429     KOpenWithDialog dlg(KUrl::List(), i18n("Select default video player"), m_configEnv.kcfg_defaultplayerapp->text(), this);
430     if (dlg.exec() != QDialog::Accepted)
431         return;
432
433     service = dlg.service();
434     m_configEnv.kcfg_defaultplayerapp->setText(service->exec());
435 }
436
437 void KdenliveSettingsDialog::slotEditAudioApplication()
438 {
439     KService::Ptr service;
440     KOpenWithDialog dlg(KUrl::List(), i18n("Select default audio editor"), m_configEnv.kcfg_defaultaudioapp->text(), this);
441     if (dlg.exec() != QDialog::Accepted)
442         return;
443
444     service = dlg.service();
445     m_configEnv.kcfg_defaultaudioapp->setText(service->exec());
446 }
447
448 void KdenliveSettingsDialog::slotEditImageApplication()
449 {
450     KService::Ptr service;
451     KOpenWithDialog dlg(KUrl::List(), i18n("Select default image editor"), m_configEnv.kcfg_defaultimageapp->text(), this);
452     if (dlg.exec() != QDialog::Accepted)
453         return;
454
455     service = dlg.service();
456     m_configEnv.kcfg_defaultimageapp->setText(service->exec());
457 }
458
459 #ifndef NO_JOGSHUTTLE
460 void KdenliveSettingsDialog::slotCheckShuttle(int state)
461 {
462     m_configShuttle.config_group->setEnabled(state);
463     if (m_configShuttle.shuttledevicelist->count() == 0) {
464         // parse devices
465         QString baseName = "/dev/input/event";
466         int fd;
467         for (int i = 0; i < 30; i++) {
468             QString filename = baseName + QString::number(i);
469             kDebug() << "/// CHECKING OFR: " << filename;
470
471             char name[256] = "unknown";
472             fd = KDE_open((char *) filename.toUtf8().data(), O_RDONLY);
473             if (fd >= 0 && ioctl(fd, EVIOCGNAME(sizeof(name)), name) >= 0) {
474                 m_configShuttle.shuttledevicelist->addItem(name, filename);
475             }
476             ::close(fd);
477         }
478         if (KdenliveSettings::shuttledevice().isEmpty()) QTimer::singleShot(1500, this, SLOT(slotUpdateShuttleDevice()));
479     }
480 }
481
482 void KdenliveSettingsDialog::slotUpdateShuttleDevice(int ix)
483 {
484     QString device = m_configShuttle.shuttledevicelist->itemData(ix).toString();
485     //KdenliveSettings::setShuttledevice(device);
486     m_configShuttle.kcfg_shuttledevice->setText(device);
487 }
488
489 #endif /* NO_JOGSHUTTLE */
490
491 void KdenliveSettingsDialog::rebuildVideo4Commands()
492 {
493     QString captureCommand;
494     if (!m_configCapture.kcfg_video4adevice->text().isEmpty()) captureCommand = "-f " + m_configCapture.kcfg_video4aformat->text() + " -i " + m_configCapture.kcfg_video4adevice->text() + " -acodec " + m_configCapture.kcfg_video4acodec->text();
495
496     captureCommand +=  " -f " + m_configCapture.kcfg_video4vformat->text() + " -s " + m_configCapture.kcfg_video4size->text() + " -r " + QString::number(m_configCapture.kcfg_video4rate->value()) + " -i " + m_configCapture.kcfg_video4vdevice->text() + " -vcodec " + m_configCapture.kcfg_video4vcodec->text();
497     m_configCapture.kcfg_video4capture->setText(captureCommand);
498 }
499 void KdenliveSettingsDialog::updateWidgets()
500 {
501     // Revert widgets to last saved state (for example when user pressed "Cancel")
502     // kDebug() << "// // // KCONFIG Revert called";
503 #ifndef NO_JOGSHUTTLE
504     // revert jog shuttle device
505     if (m_configShuttle.shuttledevicelist->count() > 0) {
506         for (int i = 0; i < m_configShuttle.shuttledevicelist->count(); i++) {
507             if (m_configShuttle.shuttledevicelist->itemData(i) == KdenliveSettings::shuttledevice()) {
508                 m_configShuttle.shuttledevicelist->setCurrentIndex(i);
509                 break;
510             }
511         }
512     }
513
514     // Revert jog shuttle buttons
515     QList<QString> action_names = m_mappable_actions.keys();
516     qSort(action_names);
517     QStringList actions_map = JogShuttleConfig::actionMap(KdenliveSettings::shuttlebuttons());
518     QMap<QString, int> action_pos;
519     foreach (const QString& action_id, actions_map) {
520       // This loop find out at what index is the string that would map to the action_id.
521       for (int i = 0; i < action_names.size(); i++) {
522           if (m_mappable_actions[action_names[i]] == action_id) {
523               action_pos[action_id] = i;
524               break;
525           }
526       }
527     }
528     int i = 0;
529     foreach (KComboBox* button, m_shuttle_buttons) {
530       ++i;
531       if (i < actions_map.size())
532         button->setCurrentIndex(action_pos[actions_map[i]]);
533     }
534 #endif
535 }
536
537 void KdenliveSettingsDialog::updateSettings()
538 {
539     // Save changes to settings (for example when user pressed "Apply" or "Ok")
540     // kDebug() << "// // // KCONFIG UPDATE called";
541     m_defaultProfile = m_configProject.kcfg_profiles_list->currentText();
542     KdenliveSettings::setDefault_profile(m_defaultPath);
543
544     bool resetProfile = false;
545     bool updateCapturePath = false;
546     
547     /*if (m_configShuttle.shuttledevicelist->count() > 0) {
548         QString device = m_configShuttle.shuttledevicelist->itemData(m_configShuttle.shuttledevicelist->currentIndex()).toString();
549         if (device != KdenliveSettings::shuttledevice()) KdenliveSettings::setShuttledevice(device);
550     }*/
551
552     if (m_configEnv.kcfg_capturetoprojectfolder->isChecked() != KdenliveSettings::capturetoprojectfolder()) {
553         KdenliveSettings::setCapturetoprojectfolder(m_configEnv.kcfg_capturetoprojectfolder->isChecked());
554         updateCapturePath = true;
555     }
556
557     if (m_configEnv.capturefolderurl->url().path() != KdenliveSettings::capturefolder()) {
558         KdenliveSettings::setCapturefolder(m_configEnv.capturefolderurl->url().path());
559         updateCapturePath = true;
560     }
561
562     if (m_configCapture.kcfg_dvgrabfilename->text() != KdenliveSettings::dvgrabfilename()) {
563         KdenliveSettings::setDvgrabfilename(m_configCapture.kcfg_dvgrabfilename->text());
564         updateCapturePath = true;
565     }
566
567     if ((uint) m_configCapture.kcfg_firewireformat->currentIndex() != KdenliveSettings::firewireformat()) {
568         KdenliveSettings::setFirewireformat(m_configCapture.kcfg_firewireformat->currentIndex());
569         updateCapturePath = true;
570     }
571
572     if (updateCapturePath) emit updateCaptureFolder();
573
574     QString value = m_configCapture.kcfg_rmd_alsa_device->itemData(m_configCapture.kcfg_rmd_alsa_device->currentIndex()).toString();
575     if (value != KdenliveSettings::rmd_alsadevicename()) {
576         KdenliveSettings::setRmd_alsadevicename(value);
577     }
578
579     value = m_configCapture.kcfg_rmd_audio_freq->itemText(m_configCapture.kcfg_rmd_audio_freq->currentIndex());
580     kDebug() << "// AUDIO FREQ VALUE: " << value << ", CURRENT: " << KdenliveSettings::rmd_freq() << ", IX: " << m_configCapture.kcfg_rmd_audio_freq->currentIndex();
581     if (value != KdenliveSettings::rmd_freq()) {
582         kDebug() << "// SETTING AUDIO FREQ TO: " << value;
583         KdenliveSettings::setRmd_freq(value);
584     }
585
586     if (m_configSdl.kcfg_external_display->isChecked() != KdenliveSettings::external_display()) {
587         KdenliveSettings::setExternal_display(m_configSdl.kcfg_external_display->isChecked());
588         resetProfile = true;
589     }
590
591     value = m_configSdl.kcfg_audio_driver->itemData(m_configSdl.kcfg_audio_driver->currentIndex()).toString();
592     if (value != KdenliveSettings::audiodrivername()) {
593         KdenliveSettings::setAudiodrivername(value);
594         resetProfile = true;
595     }
596
597     if (value == "alsa") {
598         // Audio device setting is only valid for alsa driver
599         value = m_configSdl.kcfg_audio_device->itemData(m_configSdl.kcfg_audio_device->currentIndex()).toString();
600         if (value != KdenliveSettings::audiodevicename()) {
601             KdenliveSettings::setAudiodevicename(value);
602             resetProfile = true;
603         }
604     } else if (KdenliveSettings::audiodevicename().isEmpty() == false) {
605         KdenliveSettings::setAudiodevicename(QString::null);
606         resetProfile = true;
607     }
608
609     value = m_configSdl.kcfg_video_driver->itemData(m_configSdl.kcfg_video_driver->currentIndex()).toString();
610     if (value != KdenliveSettings::videodrivername()) {
611         KdenliveSettings::setVideodrivername(value);
612         resetProfile = true;
613     }
614
615     if (m_configSdl.kcfg_window_background->color() != KdenliveSettings::window_background()) {
616         KdenliveSettings::setWindow_background(m_configSdl.kcfg_window_background->color());
617         resetProfile = true;
618     }
619
620     if (m_configSdl.kcfg_volume->value() != KdenliveSettings::volume()) {
621         KdenliveSettings::setVolume(m_configSdl.kcfg_volume->value());
622         resetProfile = true;
623     }
624     
625     if (m_configProject.kcfg_enableproxy->isChecked() != KdenliveSettings::enableproxy()) {
626         KdenliveSettings::setEnableproxy(m_configProject.kcfg_enableproxy->isChecked());
627     }
628     
629     if (m_configProject.kcfg_generateproxy->isChecked() != KdenliveSettings::generateproxy()) {
630         KdenliveSettings::setGenerateproxy(m_configProject.kcfg_generateproxy->isChecked());
631     }
632     
633     if (m_configProject.kcfg_proxyminsize->value() != KdenliveSettings::proxyminsize()) {
634         KdenliveSettings::setProxyminsize(m_configProject.kcfg_proxyminsize->value());
635     }
636
637     if (m_modified) {
638         // The transcoding profiles were modified, save.
639         m_modified = false;
640         saveTranscodeProfiles();
641     }
642
643 #ifndef NO_JOGSHUTTLE
644     m_shuttleModified = false;
645
646     QStringList actions;
647     actions << "monitor_pause";  // the Job rest position action.
648     foreach (KComboBox* button, m_shuttle_buttons) {
649         actions << m_mappable_actions[button->currentText()];
650     }
651     QString maps = JogShuttleConfig::actionMap(actions);
652     //fprintf(stderr, "Shuttle config: %s\n", JogShuttleConfig::actionMap(actions).toAscii().constData());
653     if (KdenliveSettings::shuttlebuttons() != maps)
654         KdenliveSettings::setShuttlebuttons(maps);
655 #endif
656
657 #if KDE_IS_VERSION(4,3,0)
658     KConfigDialog::settingsChangedSlot();
659 #endif
660
661     //KConfigDialog::updateSettings();
662     if (resetProfile) emit doResetProfile();
663 }
664
665 void KdenliveSettingsDialog::slotUpdateDisplay()
666 {
667     QString currentProfile = m_configProject.kcfg_profiles_list->itemData(m_configProject.kcfg_profiles_list->currentIndex()).toString();
668     QMap< QString, QString > values = ProfilesDialog::getSettingsFromFile(currentProfile);
669     m_configProject.p_size->setText(values.value("width") + 'x' + values.value("height"));
670     m_configProject.p_fps->setText(values.value("frame_rate_num") + '/' + values.value("frame_rate_den"));
671     m_configProject.p_aspect->setText(values.value("sample_aspect_num") + '/' + values.value("sample_aspect_den"));
672     m_configProject.p_display->setText(values.value("display_aspect_num") + '/' + values.value("display_aspect_den"));
673     if (values.value("progressive").toInt() == 0)
674         m_configProject.p_progressive->setText(i18n("Interlaced"));
675     else
676         m_configProject.p_progressive->setText(i18n("Progressive"));
677     m_defaultProfile = m_configProject.kcfg_profiles_list->itemText(m_configProject.kcfg_profiles_list->currentIndex());
678     m_defaultPath = currentProfile;
679 }
680
681 void KdenliveSettingsDialog::slotCheckAlsaDriver()
682 {
683     QString value = m_configSdl.kcfg_audio_driver->itemData(m_configSdl.kcfg_audio_driver->currentIndex()).toString();
684     m_configSdl.kcfg_audio_device->setEnabled(value == "alsa");
685 }
686
687 void KdenliveSettingsDialog::loadTranscodeProfiles()
688 {
689     KSharedConfigPtr config = KSharedConfig::openConfig("kdenlivetranscodingrc");
690     KConfigGroup transConfig(config, "Transcoding");
691     // read the entries
692     m_configTranscode.profiles_list->blockSignals(true);
693     QMap< QString, QString > profiles = transConfig.entryMap();
694     QMapIterator<QString, QString> i(profiles);
695     while (i.hasNext()) {
696         i.next();
697         QTreeWidgetItem *item = new QTreeWidgetItem(m_configTranscode.profiles_list, QStringList() << i.key() << i.value());
698         item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
699     }
700     m_configTranscode.profiles_list->blockSignals(false);
701 }
702
703 void KdenliveSettingsDialog::saveTranscodeProfiles()
704 {
705     KSharedConfigPtr config = KSharedConfig::openConfig("kdenlivetranscodingrc");
706     //KSharedConfigPtr config = KGlobal::config();
707     KConfigGroup transConfig(config, "Transcoding");
708     // read the entries
709     transConfig.deleteGroup();
710     int max = m_configTranscode.profiles_list->topLevelItemCount();
711     for (int i = 0; i < max; i++) {
712         QTreeWidgetItem *item = m_configTranscode.profiles_list->topLevelItem(i);
713         transConfig.writeEntry(item->text(0), item->text(1));
714     }
715     config->sync();
716 }
717
718 void KdenliveSettingsDialog::slotAddTranscode()
719 {
720     QTreeWidgetItem *item = new QTreeWidgetItem(m_configTranscode.profiles_list, QStringList() << i18n("Name") << i18n("Parameters"));
721     item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
722     m_configTranscode.profiles_list->setCurrentItem(item);
723     m_configTranscode.profiles_list->editItem(item);
724     slotDialogModified();
725 }
726
727 void KdenliveSettingsDialog::slotDeleteTranscode()
728 {
729     QTreeWidgetItem *item = m_configTranscode.profiles_list->currentItem();
730     if (item == NULL) return;
731     delete item;
732     slotDialogModified();
733 }
734
735 void KdenliveSettingsDialog::slotShuttleModified()
736 {
737 #ifndef NO_JOGSHUTTLE
738     QStringList actions;
739     actions << "monitor_pause";  // the Job rest position action.
740     foreach (KComboBox* button, m_shuttle_buttons) {
741         actions << m_mappable_actions[button->currentText()];
742     }
743     QString maps = JogShuttleConfig::actionMap(actions);
744     m_shuttleModified = KdenliveSettings::shuttlebuttons() != maps;
745 #endif
746 #if KDE_IS_VERSION(4,3,0)
747     KConfigDialog::updateButtons();
748 #endif
749 }
750
751 void KdenliveSettingsDialog::slotDialogModified()
752 {
753     m_modified = true;
754 #if KDE_IS_VERSION(4,3,0)
755     KConfigDialog::updateButtons();
756 #endif
757 }
758
759 //virtual
760 bool KdenliveSettingsDialog::hasChanged()
761 {
762     if (m_modified || m_shuttleModified) return true;
763     return KConfigDialog::hasChanged();
764 }
765
766 void KdenliveSettingsDialog::slotUpdatev4lDevice()
767 {
768     QString device = m_configCapture.kcfg_detectedv4ldevices->itemData(m_configCapture.kcfg_detectedv4ldevices->currentIndex()).toString();
769     if (!device.isEmpty()) m_configCapture.kcfg_video4vdevice->setText(device);
770     QString size = m_configCapture.kcfg_detectedv4ldevices->itemData(m_configCapture.kcfg_detectedv4ldevices->currentIndex(), Qt::UserRole + 1).toString();
771     if (!size.isEmpty()) m_configCapture.kcfg_video4size->setText(size);
772     rebuildVideo4Commands();
773 }
774
775
776 #include "kdenlivesettingsdialog.moc"
777
778