]> git.sesse.net Git - kdenlive/blob - src/kdenlivesettingsdialog.cpp
krazy code fixes
[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 <QDir>
21 #include <QTimer>
22
23 #include <KStandardDirs>
24 #include <KDebug>
25 #include <kopenwithdialog.h>
26 #include <KConfigDialogManager>
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #ifndef NO_JOGSHUTTLE
33 #include <linux/input.h>
34 #endif /* NO_JOGSHUTTLE */
35
36 #include "profilesdialog.h"
37 #include "kdenlivesettings.h"
38 #include "kdenlivesettingsdialog.h"
39
40 KdenliveSettingsDialog::KdenliveSettingsDialog(QWidget * parent): KConfigDialog(parent, "settings", KdenliveSettings::self()) {
41
42     QWidget *p1 = new QWidget;
43     m_configMisc.setupUi(p1);
44     page1 = addPage(p1, i18n("Misc"), "configure");
45
46     // Hide multi tab option until Kdenlive really supports it
47     m_configMisc.kcfg_activatetabs->setVisible(false);
48
49     QWidget *p3 = new QWidget;
50     m_configDisplay.setupUi(p3);
51     page3 = addPage(p3, i18n("Display"), "display");
52
53     QWidget *p2 = new QWidget;
54     m_configEnv.setupUi(p2);
55     m_configEnv.mltpathurl->setMode(KFile::Directory);
56     m_configEnv.mltpathurl->lineEdit()->setObjectName("kcfg_mltpath");
57     m_configEnv.rendererpathurl->lineEdit()->setObjectName("kcfg_rendererpath");
58     m_configEnv.tmppathurl->setMode(KFile::Directory);
59     m_configEnv.tmppathurl->lineEdit()->setObjectName("kcfg_currenttmpfolder");
60     m_configEnv.projecturl->setMode(KFile::Directory);
61     m_configEnv.projecturl->lineEdit()->setObjectName("kcfg_defaultprojectfolder");
62     m_configEnv.capturefolderurl->setMode(KFile::Directory);
63     m_configEnv.capturefolderurl->lineEdit()->setObjectName("kcfg_capturefolder");
64     page2 = addPage(p2, i18n("Environment"), "terminal");
65
66     QWidget *p4 = new QWidget;
67     m_configCapture.setupUi(p4);
68     page4 = addPage(p4, i18n("Capture"), "audio-card");
69     m_configCapture.tabWidget->setCurrentIndex(KdenliveSettings::defaultcapture());
70
71     QWidget *p5 = new QWidget;
72     m_configShuttle.setupUi(p5);
73 #ifndef NO_JOGSHUTTLE
74     connect(m_configShuttle.kcfg_enableshuttle, SIGNAL(stateChanged(int)), this, SLOT(slotCheckShuttle(int)));
75     connect(m_configShuttle.shuttledevicelist, SIGNAL(activated(int)), this, SLOT(slotUpdateShuttleDevice(int)));
76     slotCheckShuttle(KdenliveSettings::enableshuttle());
77     m_configShuttle.shuttledisabled->hide();
78 #else
79     m_configShuttle.kcfg_enableshuttle->hide();
80     m_configShuttle.kcfg_enableshuttle->setDisabled(true);
81 #endif /* NO_JOGSHUTTLE */
82     page5 = addPage(p5, i18n("JogShuttle"), "input-mouse");
83
84     QWidget *p6 = new QWidget;
85     m_configSdl.setupUi(p6);
86     page6 = addPage(p6, i18n("Playback"), "audio-card");
87
88     QStringList actions;
89     actions << i18n("Do nothing");
90     actions << i18n("Play / Pause");
91     actions << i18n("Cut");
92     m_configShuttle.kcfg_shuttle1->addItems(actions);
93     m_configShuttle.kcfg_shuttle2->addItems(actions);
94     m_configShuttle.kcfg_shuttle3->addItems(actions);
95     m_configShuttle.kcfg_shuttle4->addItems(actions);
96     m_configShuttle.kcfg_shuttle5->addItems(actions);
97
98     connect(m_configCapture.kcfg_video4vdevice, SIGNAL(editingFinished()), this, SLOT(rebuildVideo4Commands()));
99     connect(m_configCapture.kcfg_video4adevice, SIGNAL(editingFinished()), this, SLOT(rebuildVideo4Commands()));
100     connect(m_configCapture.kcfg_video4vformat, SIGNAL(editingFinished()), this, SLOT(rebuildVideo4Commands()));
101     connect(m_configCapture.kcfg_video4aformat, SIGNAL(editingFinished()), this, SLOT(rebuildVideo4Commands()));
102     connect(m_configCapture.kcfg_video4size, SIGNAL(editingFinished()), this, SLOT(rebuildVideo4Commands()));
103     connect(m_configCapture.kcfg_video4rate, SIGNAL(editingFinished()), this, SLOT(rebuildVideo4Commands()));
104
105     connect(m_configCapture.kcfg_rmd_capture_audio, SIGNAL(clicked(bool)), m_configCapture.audio_group, SLOT(setVisible(bool)));
106
107     m_configCapture.audio_group->setVisible(KdenliveSettings::rmd_capture_audio());
108
109     connect(m_configEnv.kp_image, SIGNAL(clicked()), this, SLOT(slotEditImageApplication()));
110     connect(m_configEnv.kp_audio, SIGNAL(clicked()), this, SLOT(slotEditAudioApplication()));
111     connect(m_configEnv.kp_player, SIGNAL(clicked()), this, SLOT(slotEditVideoApplication()));
112
113     checkProfile();
114
115     slotUpdateDisplay();
116
117     initDevices();
118     connect(m_configMisc.kcfg_profiles_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateDisplay()));
119     connect(m_configCapture.kcfg_rmd_capture_type, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateRmdRegionStatus()));
120
121     slotUpdateRmdRegionStatus();
122
123
124     //HACK: check dvgrab version, because only dvgrab >= 3.3 supports
125     //   --timestamp option without bug
126
127     if (KdenliveSettings::dvgrab_path().isEmpty() || !QFile::exists(KdenliveSettings::dvgrab_path())) {
128         QString dvgrabpath = KStandardDirs::findExe("dvgrab");
129         KdenliveSettings::setDvgrab_path(dvgrabpath);
130     }
131
132
133     double dvgrabVersion = 0;
134     if (!KdenliveSettings::dvgrab_path().isEmpty()) {
135         QProcess *versionCheck = new QProcess;
136         versionCheck->setProcessChannelMode(QProcess::MergedChannels);
137         versionCheck->start("dvgrab", QStringList() << "--version");
138         if (versionCheck->waitForFinished()) {
139             QString version = QString(versionCheck->readAll()).simplified();
140             if (version.contains(' ')) version = version.section(' ', -1);
141             dvgrabVersion = version.toDouble();
142
143             kDebug() << "// FOUND DVGRAB VERSION: " << dvgrabVersion;
144         }
145         if (versionCheck) delete versionCheck;
146         if (dvgrabVersion < 3.3) {
147             KdenliveSettings::setFirewiretimestamp(false);
148             m_configCapture.kcfg_firewiretimestamp->setEnabled(false);
149         }
150         m_configCapture.dvgrab_info->setText(i18n("dvgrab version %1 at %2", dvgrabVersion, KdenliveSettings::dvgrab_path()));
151     } else m_configCapture.dvgrab_info->setText(i18n("<strong><em>dvgrab</em> utility not found, please install it for firewire capture</strong>"));
152
153     if (KdenliveSettings::rmd_path().isEmpty() || !QFile::exists(KdenliveSettings::rmd_path())) {
154         QString rmdpath = KStandardDirs::findExe("recordmydesktop");
155         KdenliveSettings::setRmd_path(rmdpath);
156     }
157     if (KdenliveSettings::rmd_path().isEmpty()) m_configCapture.rmd_info->setText(i18n("<strong><em>Recordmydesktop</em> utility not found, please install it for screen grabs</strong>"));
158     else m_configCapture.rmd_info->setText(i18n("Recordmydesktop found at: %1", KdenliveSettings::rmd_path()));
159 }
160
161 KdenliveSettingsDialog::~KdenliveSettingsDialog() {}
162
163 void KdenliveSettingsDialog::slotUpdateRmdRegionStatus() {
164     m_configCapture.region_group->setHidden(m_configCapture.kcfg_rmd_capture_type->currentIndex() != 1);
165 }
166
167 void KdenliveSettingsDialog::checkProfile() {
168     m_configMisc.kcfg_profiles_list->clear();
169     QMap <QString, QString> profilesInfo = ProfilesDialog::getProfilesInfo();
170     QMapIterator<QString, QString> i(profilesInfo);
171     while (i.hasNext()) {
172         i.next();
173         m_configMisc.kcfg_profiles_list->addItem(i.key(), i.value());
174     }
175
176     if (!KdenliveSettings::default_profile().isEmpty()) {
177         for (int i = 0; i < m_configMisc.kcfg_profiles_list->count(); i++) {
178             if (m_configMisc.kcfg_profiles_list->itemData(i).toString() == KdenliveSettings::default_profile()) {
179                 m_configMisc.kcfg_profiles_list->setCurrentIndex(i);
180                 KdenliveSettings::setProfiles_list(i);
181                 break;
182             }
183         }
184     }
185 }
186
187 void KdenliveSettingsDialog::initDevices() {
188     // Fill audio drivers
189     m_configSdl.kcfg_audio_driver->addItem(i18n("Automatic"), QString());
190     m_configSdl.kcfg_audio_driver->addItem(i18n("OSS"), "dsp");
191     m_configSdl.kcfg_audio_driver->addItem(i18n("ALSA"), "alsa");
192     m_configSdl.kcfg_audio_driver->addItem(i18n("PulseAudio"), "pulse");
193     m_configSdl.kcfg_audio_driver->addItem(i18n("OSS with DMA access"), "dma");
194     m_configSdl.kcfg_audio_driver->addItem(i18n("Esound daemon"), "esd");
195     m_configSdl.kcfg_audio_driver->addItem(i18n("ARTS daemon"), "artsc");
196
197     if (!KdenliveSettings::audiodrivername().isEmpty())
198         for (int i = 1;i < m_configSdl.kcfg_audio_driver->count(); i++) {
199             if (m_configSdl.kcfg_audio_driver->itemData(i).toString() == KdenliveSettings::audiodrivername()) {
200                 m_configSdl.kcfg_audio_driver->setCurrentIndex(i);
201                 KdenliveSettings::setAudio_driver((uint) i);
202             }
203         }
204
205     // Fill video drivers
206     m_configSdl.kcfg_video_driver->addItem(i18n("Automatic"), QString());
207     m_configSdl.kcfg_video_driver->addItem(i18n("XVideo"), "x11");
208     m_configSdl.kcfg_video_driver->addItem(i18n("X11"), "x11_noaccel");
209     m_configSdl.kcfg_video_driver->addItem(i18n("XFree86 DGA 2.0"), "dga");
210     m_configSdl.kcfg_video_driver->addItem(i18n("Nano X"), "nanox");
211     m_configSdl.kcfg_video_driver->addItem(i18n("Framebuffer console"), "fbcon");
212     m_configSdl.kcfg_video_driver->addItem(i18n("Direct FB"), "directfb");
213     m_configSdl.kcfg_video_driver->addItem(i18n("SVGAlib"), "svgalib");
214     m_configSdl.kcfg_video_driver->addItem(i18n("General graphics interface"), "ggi");
215     m_configSdl.kcfg_video_driver->addItem(i18n("Ascii art library"), "aalib");
216
217     // Fill the list of audio playback devices
218     m_configSdl.kcfg_audio_device->addItem(i18n("Default"), QString());
219     m_configCapture.kcfg_rmd_alsa_device->addItem(i18n("Default"), QString());
220     if (KStandardDirs::findExe("aplay") != QString::null) {
221         m_readProcess.setOutputChannelMode(KProcess::OnlyStdoutChannel);
222         m_readProcess.setProgram("aplay", QStringList() << "-l");
223         connect(&m_readProcess, SIGNAL(readyReadStandardOutput()) , this, SLOT(slotReadAudioDevices()));
224         m_readProcess.execute(5000);
225     } else {
226         // If aplay is not installed on the system, parse the /proc/asound/pcm file
227         QFile file("/proc/asound/pcm");
228         if (file.open(QIODevice::ReadOnly)) {
229             QTextStream stream(&file);
230             QString line;
231             QString deviceId;
232             while (!stream.atEnd()) {
233                 line = stream.readLine();
234                 if (line.contains("playback")) {
235                     deviceId = line.section(':', 0, 0);
236                     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()));
237                 }
238                 if (line.contains("capture")) {
239                     deviceId = line.section(':', 0, 0);
240                     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()));
241                 }
242             }
243             file.close();
244         }
245     }
246     if (!KdenliveSettings::audiodevicename().isEmpty()) {
247         // Select correct alsa device
248         int ix = m_configSdl.kcfg_audio_device->findData(KdenliveSettings::audiodevicename());
249         m_configSdl.kcfg_audio_device->setCurrentIndex(ix);
250         KdenliveSettings::setAudio_device(ix);
251     }
252     if (!KdenliveSettings::rmd_alsadevicename().isEmpty()) {
253         // Select correct alsa device
254         int ix = m_configCapture.kcfg_rmd_alsa_device->findData(KdenliveSettings::rmd_alsadevicename());
255         m_configCapture.kcfg_rmd_alsa_device->setCurrentIndex(ix);
256         KdenliveSettings::setRmd_alsa_device(ix);
257     }
258 }
259
260
261 void KdenliveSettingsDialog::slotReadAudioDevices() {
262     QString result = QString(m_readProcess.readAllStandardOutput());
263     kDebug() << "// / / / / / READING APLAY: ";
264     kDebug() << result;
265     QStringList lines = result.split('\n');
266     foreach(const QString &data, lines) {
267         kDebug() << "// READING LINE: " << data;
268         if (data.simplified().startsWith("card")) {
269             QString card = data.section(':', 0, 0).section(' ', -1);
270             QString device = data.section(':', 1, 1).section(' ', -1);
271             m_configSdl.kcfg_audio_device->addItem(data.section(':', -1), "plughw:" + card + ',' + device);
272             m_configCapture.kcfg_rmd_alsa_device->addItem(data.section(':', -1), "plughw:" + card + ',' + device);
273         }
274     }
275 }
276
277 void KdenliveSettingsDialog::showPage(int page, int option) {
278     switch (page) {
279     case 1:
280         setCurrentPage(page1);
281         break;
282     case 2:
283         setCurrentPage(page2);
284         break;
285     case 3:
286         setCurrentPage(page3);
287         break;
288     case 4:
289         setCurrentPage(page4);
290         m_configCapture.tabWidget->setCurrentIndex(option);
291         break;
292     case 5:
293         setCurrentPage(page5);
294         break;
295
296     }
297 }
298
299 void KdenliveSettingsDialog::slotEditVideoApplication() {
300     KService::Ptr service;
301     KOpenWithDialog dlg(KUrl::List(), i18n("Select default video player"), m_configEnv.kcfg_defaultplayerapp->text(), this);
302     if (dlg.exec() != QDialog::Accepted)
303         return;
304
305     service = dlg.service();
306     m_configEnv.kcfg_defaultplayerapp->setText(service->exec());
307 }
308
309 void KdenliveSettingsDialog::slotEditAudioApplication() {
310     KService::Ptr service;
311     KOpenWithDialog dlg(KUrl::List(), i18n("Select default audio editor"), m_configEnv.kcfg_defaultaudioapp->text(), this);
312     if (dlg.exec() != QDialog::Accepted)
313         return;
314
315     service = dlg.service();
316     m_configEnv.kcfg_defaultaudioapp->setText(service->exec());
317 }
318
319 void KdenliveSettingsDialog::slotEditImageApplication() {
320     KService::Ptr service;
321     KOpenWithDialog dlg(KUrl::List(), i18n("Select default image editor"), m_configEnv.kcfg_defaultimageapp->text(), this);
322     if (dlg.exec() != QDialog::Accepted)
323         return;
324
325     service = dlg.service();
326     m_configEnv.kcfg_defaultimageapp->setText(service->exec());
327 }
328
329 #ifndef NO_JOGSHUTTLE
330 void KdenliveSettingsDialog::slotCheckShuttle(int state) {
331     m_configShuttle.config_group->setEnabled(state);
332     if (m_configShuttle.shuttledevicelist->count() == 0) {
333         // parse devices
334         QString baseName = "/dev/input/event";
335         int fd;
336         for (int i = 0; i < 30; i++) {
337             QString filename = baseName + QString::number(i);
338             kDebug() << "/// CHECKING OFR: " << filename;
339
340             char name[256] = "unknown";
341             fd = ::open((char *) filename.toUtf8().data(), O_RDONLY);
342             if (fd >= 0 && ioctl(fd, EVIOCGNAME(sizeof(name)), name) >= 0) {
343                 m_configShuttle.shuttledevicelist->addItem(name, filename);
344             }
345             ::close(fd);
346         }
347         if (KdenliveSettings::shuttledevice().isEmpty()) QTimer::singleShot(1500, this, SLOT(slotUpdateShuttleDevice()));
348     }
349 }
350
351 void KdenliveSettingsDialog::slotUpdateShuttleDevice(int ix) {
352     QString device = m_configShuttle.shuttledevicelist->itemData(ix).toString();
353     //KdenliveSettings::setShuttledevice(device);
354     m_configShuttle.kcfg_shuttledevice->setText(device);
355 }
356 #endif /* NO_JOGSHUTTLE */
357
358 void KdenliveSettingsDialog::rebuildVideo4Commands() {
359     QString captureCommand;
360     if (!m_configCapture.kcfg_video4adevice->text().isEmpty()) captureCommand = "-f " + m_configCapture.kcfg_video4aformat->text() + " -i " + m_configCapture.kcfg_video4adevice->text();
361
362     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();
363     m_configCapture.kcfg_video4capture->setText(captureCommand);
364 }
365
366
367 void KdenliveSettingsDialog::updateSettings() {
368     kDebug() << "// // // KCONFIG UPDATE called";
369     m_defaultProfile = m_configMisc.kcfg_profiles_list->currentText();
370     KdenliveSettings::setDefault_profile(m_defaultPath);
371
372     bool resetProfile = false;
373     QString value = m_configSdl.kcfg_audio_device->itemData(m_configSdl.kcfg_audio_device->currentIndex()).toString();
374     if (value != KdenliveSettings::audiodevicename()) {
375         KdenliveSettings::setAudiodevicename(value);
376         resetProfile = true;
377     }
378
379     value = m_configCapture.kcfg_rmd_alsa_device->itemData(m_configCapture.kcfg_rmd_alsa_device->currentIndex()).toString();
380     if (value != KdenliveSettings::rmd_alsadevicename()) {
381         KdenliveSettings::setRmd_alsadevicename(value);
382     }
383
384     value = m_configCapture.kcfg_rmd_audio_freq->itemText(m_configCapture.kcfg_rmd_audio_freq->currentIndex());
385     kDebug() << "// AUDIO FREQ VALUE: " << value << ", CURRENT: " << KdenliveSettings::rmd_freq() << ", IX: " << m_configCapture.kcfg_rmd_audio_freq->currentIndex();
386     if (value != KdenliveSettings::rmd_freq()) {
387         kDebug() << "// SETTING AUDIO FREQ TO: " << value;
388         KdenliveSettings::setRmd_freq(value);
389     }
390
391     value = m_configSdl.kcfg_audio_driver->itemData(m_configSdl.kcfg_audio_driver->currentIndex()).toString();
392     if (value != KdenliveSettings::audiodrivername()) {
393         KdenliveSettings::setAudiodrivername(value);
394         resetProfile = true;
395     }
396
397     value = m_configSdl.kcfg_video_driver->itemData(m_configSdl.kcfg_video_driver->currentIndex()).toString();
398     if (value != KdenliveSettings::videodrivername()) {
399         KdenliveSettings::setVideodrivername(value);
400         resetProfile = true;
401     }
402
403     bool updatePreview = false;
404     if (m_configSdl.kcfg_dropbframes->isChecked() != KdenliveSettings::dropbframes()) {
405         KdenliveSettings::setDropbframes(m_configSdl.kcfg_dropbframes->isChecked());
406         updatePreview = true;
407     }
408
409     KConfigDialog::updateSettings();
410     if (resetProfile) emit doResetProfile();
411     if (updatePreview) emit updatePreviewSettings();
412 }
413
414 void KdenliveSettingsDialog::slotUpdateDisplay() {
415     QString currentProfile = m_configMisc.kcfg_profiles_list->itemData(m_configMisc.kcfg_profiles_list->currentIndex()).toString();
416     QMap< QString, QString > values = ProfilesDialog::getSettingsFromFile(currentProfile);
417     m_configMisc.p_size->setText(values.value("width") + 'x' + values.value("height"));
418     m_configMisc.p_fps->setText(values.value("frame_rate_num") + '/' + values.value("frame_rate_den"));
419     m_configMisc.p_aspect->setText(values.value("sample_aspect_num") + '/' + values.value("sample_aspect_den"));
420     m_configMisc.p_display->setText(values.value("display_aspect_num") + '/' + values.value("display_aspect_den"));
421     if (values.value("progressive").toInt() == 0) m_configMisc.p_progressive->setText(i18n("Interlaced"));
422     else m_configMisc.p_progressive->setText(i18n("Progressive"));
423     m_defaultProfile = m_configMisc.kcfg_profiles_list->itemText(m_configMisc.kcfg_profiles_list->currentIndex());
424     m_defaultPath = currentProfile;
425 }
426
427
428 #include "kdenlivesettingsdialog.moc"
429
430