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