]> git.sesse.net Git - kdenlive/blob - src/wizard.cpp
Improve startup Wizard:
[kdenlive] / src / wizard.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 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 <QLabel>
21 #include <QFile>
22 #include <QXmlStreamWriter>
23 #include <QApplication>
24 #include <QTimer>
25
26 #include <KStandardDirs>
27 #include <KLocale>
28 #include <KProcess>
29 #include <kmimetype.h>
30
31 #include "kdenlivesettings.h"
32 #include "profilesdialog.h"
33 #include "wizard.h"
34
35 Wizard::Wizard(QWidget *parent): QWizard(parent) {
36     setPixmap(QWizard::WatermarkPixmap, QPixmap(KStandardDirs::locate("appdata", "banner.png")));
37
38     QWizardPage *page1 = new QWizardPage;
39     page1->setTitle(i18n("Welcome"));
40     QLabel *label = new QLabel(i18n("This is the first time you run Kdenlive. This wizard will let you adjust some basic settings, you will be ready to edit your first movie in a few seconds..."));
41     label->setWordWrap(true);
42     m_startLayout = new QVBoxLayout;
43     m_startLayout->addWidget(label);
44     page1->setLayout(m_startLayout);
45     addPage(page1);
46
47     QWizardPage *page4 = new QWizardPage;
48     page4->setTitle(i18n("Checking MLT engine"));
49     m_mltCheck.setupUi(page4);
50     addPage(page4);
51
52     WizardDelegate *listViewDelegate = new WizardDelegate(m_mltCheck.programList);
53     m_mltCheck.programList->setItemDelegate(listViewDelegate);
54
55     QWizardPage *page2 = new QWizardPage;
56     page2->setTitle(i18n("Video Standard"));
57     m_standard.setupUi(page2);
58
59     // build profiles lists
60     m_profilesInfo = ProfilesDialog::getProfilesInfo();
61     QMap<QString, QString>::const_iterator i = m_profilesInfo.constBegin();
62     while (i != m_profilesInfo.constEnd()) {
63         QMap< QString, QString > profileData = ProfilesDialog::getSettingsFromFile(i.value());
64         if (profileData.value("width") == "720") m_dvProfiles.append(i.key());
65         else if (profileData.value("width").toInt() >= 1080) m_hdvProfiles.append(i.key());
66         else m_otherProfiles.append(i.key());
67         ++i;
68     }
69
70     connect(m_standard.button_all, SIGNAL(toggled(bool)), this, SLOT(slotCheckStandard()));
71     connect(m_standard.button_hdv, SIGNAL(toggled(bool)), this, SLOT(slotCheckStandard()));
72     connect(m_standard.button_dv, SIGNAL(toggled(bool)), this, SLOT(slotCheckStandard()));
73     m_standard.button_all->setChecked(true);
74     connect(m_standard.profiles_list, SIGNAL(itemSelectionChanged()), this, SLOT(slotCheckSelectedItem()));
75
76     // select default profile
77     QList<QListWidgetItem *> profiles = m_standard.profiles_list->findItems(ProfilesDialog::getProfileDescription(KdenliveSettings::default_profile()), Qt::MatchExactly);
78     if (profiles.count() > 0) m_standard.profiles_list->setCurrentItem(profiles.at(0));
79     addPage(page2);
80
81     QWizardPage *page3 = new QWizardPage;
82     page3->setTitle(i18n("Additional Settings"));
83     m_extra.setupUi(page3);
84     m_extra.projectfolder->setMode(KFile::Directory);
85     m_extra.projectfolder->setPath(QDir::homePath() + "/kdenlive");
86     m_extra.videothumbs->setChecked(KdenliveSettings::videothumbnails());
87     m_extra.audiothumbs->setChecked(KdenliveSettings::audiothumbnails());
88     m_extra.autosave->setChecked(KdenliveSettings::crashrecovery());
89     connect(m_extra.videothumbs, SIGNAL(stateChanged(int)), this, SLOT(slotCheckThumbs()));
90     connect(m_extra.audiothumbs, SIGNAL(stateChanged(int)), this, SLOT(slotCheckThumbs()));
91     slotCheckThumbs();
92     addPage(page3);
93
94
95     QWizardPage *page5 = new QWizardPage;
96     page5->setTitle(i18n("Checking system"));
97     m_check.setupUi(page5);
98     slotCheckPrograms();
99     addPage(page5);
100
101     listViewDelegate = new WizardDelegate(m_check.programList);
102     m_check.programList->setItemDelegate(listViewDelegate);
103
104     QTimer::singleShot(500, this, SLOT(slotCheckMlt()));
105 }
106
107
108 void Wizard::checkMltComponents() {
109     m_mltCheck.programList->setColumnCount(2);
110     m_mltCheck.programList->setRootIsDecorated(false);
111     m_mltCheck.programList->setHeaderHidden(true);
112     QSize itemSize(20, this->fontMetrics().height() * 2.5);
113     KIcon okIcon("dialog-ok");
114     KIcon missingIcon("dialog-close");
115     m_mltCheck.programList->setColumnWidth(0, 30);
116     m_mltCheck.programList->setIconSize(QSize(24, 24));
117
118
119     QTreeWidgetItem *item = new QTreeWidgetItem(m_mltCheck.programList, QStringList() << QString() << i18n("Inigo") + " (" + KdenliveSettings::rendererpath() + ')');
120     item->setData(1, Qt::UserRole, i18n("Required for rendering (part of MLT package)"));
121     item->setSizeHint(0, itemSize);
122     item->setIcon(0, okIcon);
123
124     // Check MLT's installed producers
125     QProcess checkProcess;
126     checkProcess.start(KdenliveSettings::rendererpath(), QStringList() << "-query" << "producer");
127     if (!checkProcess.waitForStarted()) {
128         item->setIcon(0, missingIcon);
129         item->setData(1, Qt::UserRole, i18n("Error starting MLT's command line player (inigo)"));
130         button(QWizard::NextButton)->setEnabled(false);
131     } else {
132         checkProcess.waitForFinished();
133         QByteArray result = checkProcess.readAllStandardError();
134
135         // Check MLT avformat module
136         QTreeWidgetItem *avformatItem = new QTreeWidgetItem(m_mltCheck.programList, QStringList() << QString() << i18n("Avformat module (FFmpeg)"));
137         avformatItem->setData(1, Qt::UserRole, i18n("Required to work with various video formats (hdv, mpeg, flash, ...)"));
138         avformatItem->setSizeHint(0, itemSize);
139         if (!result.contains("- avformat")) {
140             avformatItem->setIcon(0, missingIcon);
141             m_mltCheck.tabWidget->setTabEnabled(1, false);
142         } else {
143             avformatItem->setIcon(0, okIcon);
144             // Check installed codecs
145             QProcess checkProcess2;
146             checkProcess2.start(KdenliveSettings::rendererpath(), QStringList() << "-producer" << "avformat:acodec-list");
147             if (!checkProcess2.waitForStarted()) {
148                 m_mltCheck.tabWidget->setTabEnabled(1, false);
149                 kDebug() << "// Error parsing MLT's avformat codecs";
150             } else {
151                 checkProcess2.waitForFinished();
152                 QByteArray codecList = checkProcess2.readAllStandardError();
153                 QString acodecList(codecList);
154                 acodecList = acodecList.section("...", 0, 0);
155                 QStringList alist = acodecList.split("\n", QString::SkipEmptyParts);
156                 for (int i = 0; i < alist.count(); i++) {
157                     if (alist.at(i).contains("- ")) m_audioCodecs.append(alist.at(i).section("- ", 1).simplified());
158                 }
159                 m_mltCheck.acodecs_list->addItems(m_audioCodecs);
160                 //kDebug()<<"// FOUND LIST:\n\n"<<m_audioCodecs<<"\n\n++++++++++++++++++++";
161             }
162             checkProcess2.start(KdenliveSettings::rendererpath(), QStringList() << "-producer" << "avformat:vcodec-list");
163             if (!checkProcess2.waitForStarted()) {
164                 kDebug() << "// Error parsing MLT's avformat codecs";
165             } else {
166                 checkProcess2.waitForFinished();
167                 QByteArray codecList = checkProcess2.readAllStandardError();
168                 QString vcodecList(codecList);
169                 vcodecList = vcodecList.section("...", 0, 0);
170                 QStringList vlist = vcodecList.split("\n", QString::SkipEmptyParts);
171                 for (int i = 0; i < vlist.count(); i++) {
172                     if (vlist.at(i).contains("- ")) m_videoCodecs.append(vlist.at(i).section("- ", 1).simplified());
173                 }
174                 m_mltCheck.vcodecs_list->addItems(m_videoCodecs);
175                 //kDebug()<<"// FOUND LIST:\n\n"<<m_videoCodecs<<"\n\n++++++++++++++++++++";
176             }
177
178
179         }
180
181         // Check MLT dv module
182         QTreeWidgetItem *dvItem = new QTreeWidgetItem(m_mltCheck.programList, QStringList() << QString() << i18n("DV module (libdv)"));
183         dvItem->setData(1, Qt::UserRole, i18n("Required to work with dv files if avformat module is not installed"));
184         dvItem->setSizeHint(0, itemSize);
185         if (!result.contains("- libdv")) {
186             dvItem->setIcon(0, missingIcon);
187         } else {
188             dvItem->setIcon(0, okIcon);
189         }
190
191         // Check MLT image format module
192         QTreeWidgetItem *imageItem = new QTreeWidgetItem(m_mltCheck.programList, QStringList() << QString() << i18n("QImage module"));
193         imageItem->setData(1, Qt::UserRole, i18n("Required to work with images"));
194         imageItem->setSizeHint(0, itemSize);
195         if (!result.contains("- qimage")) {
196             imageItem->setIcon(0, missingIcon);
197             imageItem = new QTreeWidgetItem(m_mltCheck.programList, QStringList() << QString() << i18n("Pixbuf module"));
198             imageItem->setData(1, Qt::UserRole, i18n("Required to work with images"));
199             imageItem->setSizeHint(0, itemSize);
200             if (!result.contains("- pixbuf")) imageItem->setIcon(0, missingIcon);
201             else imageItem->setIcon(0, okIcon);
202         } else {
203             imageItem->setIcon(0, okIcon);
204         }
205     }
206 }
207
208 void Wizard::slotCheckPrograms() {
209     m_check.programList->setColumnCount(2);
210     m_check.programList->setRootIsDecorated(false);
211     m_check.programList->setHeaderHidden(true);
212     QSize itemSize(20, this->fontMetrics().height() * 2.5);
213     KIcon okIcon("dialog-ok");
214     KIcon missingIcon("dialog-close");
215     m_check.programList->setColumnWidth(0, 30);
216     m_check.programList->setIconSize(QSize(24, 24));
217
218     QTreeWidgetItem *item = new QTreeWidgetItem(m_check.programList, QStringList() << QString() << i18n("FFmpeg & ffplay"));
219     item->setData(1, Qt::UserRole, i18n("Required for webcam capture"));
220     item->setSizeHint(0, itemSize);
221     QString exepath = KStandardDirs::findExe("ffmpeg");
222     if (exepath.isEmpty()) item->setIcon(0, missingIcon);
223     else if (KStandardDirs::findExe("ffplay").isEmpty()) item->setIcon(0, missingIcon);
224     else item->setIcon(0, okIcon);
225
226     item = new QTreeWidgetItem(m_check.programList, QStringList() << QString() << i18n("Recordmydesktop"));
227     item->setData(1, Qt::UserRole, i18n("Required for screen capture"));
228     item->setSizeHint(0, itemSize);
229     if (KStandardDirs::findExe("recordmydesktop").isEmpty()) item->setIcon(0, missingIcon);
230     else item->setIcon(0, okIcon);
231
232     item = new QTreeWidgetItem(m_check.programList, QStringList() << QString() << i18n("Dvgrab"));
233     item->setData(1, Qt::UserRole, i18n("Required for firewire capture"));
234     item->setSizeHint(0, itemSize);
235     if (KStandardDirs::findExe("dvgrab").isEmpty()) item->setIcon(0, missingIcon);
236     else item->setIcon(0, okIcon);
237
238 }
239
240 void Wizard::installExtraMimes(QString baseName, QStringList globs) {
241     QString mimefile = baseName;
242     mimefile.replace('/', '-');
243     KMimeType::Ptr mime = KMimeType::mimeType(baseName);
244     if (!mime) {
245         kDebug() << "KMimeTypeTrader: mimeType " << baseName << " not found";
246     } else {
247         QStringList extensions = mime->patterns();
248         QString comment = mime->comment();
249         foreach(const QString &glob, globs) {
250             if (!extensions.contains(glob)) extensions << glob;
251         }
252         kDebug() << "EXTS: " << extensions;
253         QString packageFileName = KStandardDirs::locateLocal("xdgdata-mime", "packages/" + mimefile + ".xml");
254         kDebug() << "INSTALLING NEW MIME TO: " << packageFileName;
255         QFile packageFile(packageFileName);
256         if (!packageFile.open(QIODevice::WriteOnly)) {
257             kError() << "Couldn't open" << packageFileName << "for writing";
258             return;
259         }
260         QXmlStreamWriter writer(&packageFile);
261         writer.setAutoFormatting(true);
262         writer.writeStartDocument();
263
264         const QString nsUri = "http://www.freedesktop.org/standards/shared-mime-info";
265         writer.writeDefaultNamespace(nsUri);
266         writer.writeStartElement("mime-info");
267         writer.writeStartElement(nsUri, "mime-type");
268         writer.writeAttribute("type", baseName);
269
270         if (!comment.isEmpty()) {
271             writer.writeStartElement(nsUri, "comment");
272             writer.writeCharacters(comment);
273             writer.writeEndElement(); // comment
274         }
275
276         foreach(const QString& pattern, extensions) {
277             writer.writeStartElement(nsUri, "glob");
278             writer.writeAttribute("pattern", pattern);
279             writer.writeEndElement(); // glob
280         }
281
282         writer.writeEndElement(); // mime-info
283         writer.writeEndElement(); // mime-type
284         writer.writeEndDocument();
285     }
286 }
287
288 void Wizard::runUpdateMimeDatabase() {
289     const QString localPackageDir = KStandardDirs::locateLocal("xdgdata-mime", QString());
290     //Q_ASSERT(!localPackageDir.isEmpty());
291     KProcess proc;
292     proc << "update-mime-database";
293     proc << localPackageDir;
294     const int exitCode = proc.execute();
295     if (exitCode) {
296         kWarning() << proc.program() << "exited with error code" << exitCode;
297     }
298 }
299
300 void Wizard::slotCheckThumbs() {
301     QString pixname = "timeline_vthumbs.png";
302     if (!m_extra.audiothumbs->isChecked() && !m_extra.videothumbs->isChecked()) {
303         pixname = "timeline_nothumbs.png";
304     } else if (m_extra.audiothumbs->isChecked()) {
305         if (m_extra.videothumbs->isChecked())
306             pixname = "timeline_avthumbs.png";
307         else pixname = "timeline_athumbs.png";
308     }
309
310     m_extra.timeline_preview->setPixmap(QPixmap(KStandardDirs::locate("appdata", pixname)));
311 }
312
313 void Wizard::slotCheckStandard() {
314     m_standard.profiles_list->clear();
315     QStringList profiles;
316     if (m_standard.button_dv->isChecked()) {
317         // DV standard
318         m_standard.profiles_list->addItems(m_dvProfiles);
319     } else if (m_standard.button_hdv->isChecked()) {
320         // HDV standard
321         m_standard.profiles_list->addItems(m_hdvProfiles);
322     } else {
323         m_standard.profiles_list->addItems(m_dvProfiles);
324         m_standard.profiles_list->addItems(m_hdvProfiles);
325         m_standard.profiles_list->addItems(m_otherProfiles);
326         //m_standard.profiles_list->sortItems();
327     }
328
329     for (int i = 0; i < m_standard.profiles_list->count(); i++) {
330         QListWidgetItem *item = m_standard.profiles_list->item(i);
331         MltVideoProfile prof = ProfilesDialog::getVideoProfile(m_profilesInfo.value(item->text()));
332         const QString infoString = i18n("<b>Frame size: </b>%1x%2<br><b>Frame rate: </b>%3/%4<br><b>Pixel aspect ratio: </b>%5/%6<br><b>Display aspect ratio: </b>%7/%8").arg(QString::number(prof.width), QString::number(prof.height), QString::number(prof.frame_rate_num), QString::number(prof.frame_rate_den), QString::number(prof.sample_aspect_num), QString::number(prof.sample_aspect_den), QString::number(prof.display_aspect_num), QString::number(prof.display_aspect_den));
333         item->setToolTip(infoString);
334     }
335
336     m_standard.profiles_list->setSortingEnabled(true);
337     m_standard.profiles_list->setCurrentRow(0);
338 }
339
340 void Wizard::slotCheckSelectedItem() {
341     // Make sure we always have an item highlighted
342     m_standard.profiles_list->setCurrentRow(m_standard.profiles_list->currentRow());
343 }
344
345
346 void Wizard::adjustSettings() {
347     if (m_extra.installmimes->isChecked()) {
348         QStringList globs;
349         globs << "*.mts" << "*.m2t" << "*.mod" << "*.ts";
350         installExtraMimes("video/mpeg", globs);
351         globs.clear();
352         globs << "*.dv";
353         installExtraMimes("video/dv", globs);
354         runUpdateMimeDatabase();
355     }
356     KdenliveSettings::setAudiothumbnails(m_extra.audiothumbs->isChecked());
357     KdenliveSettings::setVideothumbnails(m_extra.videothumbs->isChecked());
358     KdenliveSettings::setCrashrecovery(m_extra.autosave->isChecked());
359     if (m_standard.profiles_list->currentItem()) {
360         QString selectedProfile = m_profilesInfo.value(m_standard.profiles_list->currentItem()->text());
361         if (selectedProfile.isEmpty()) selectedProfile = "dv_pal";
362         KdenliveSettings::setDefault_profile(selectedProfile);
363     }
364     QString path = m_extra.projectfolder->url().path();
365     if (KStandardDirs::makeDir(path) == false) kDebug() << "/// ERROR CREATING PROJECT FOLDER: " << path;
366     KdenliveSettings::setDefaultprojectfolder(path);
367
368 }
369
370 void Wizard::slotCheckMlt() {
371     QString errorMessage;
372     if (KdenliveSettings::rendererpath().isEmpty()) {
373         errorMessage.append(i18n("your MLT installation cannot be found. Install MLT and restart Kdenlive.\n"));
374     }
375     /*QProcess checkProcess;
376     checkProcess.start(KdenliveSettings::rendererpath(), QStringList() << "-query" << "producer");
377     if (!checkProcess.waitForStarted())
378         errorMessage.append(i18n("Error starting MLT's command line player (inigo)") + ".\n");
379
380     checkProcess.waitForFinished();
381
382     QByteArray result = checkProcess.readAllStandardError();
383     if (!result.contains("- avformat")) errorMessage.append(i18n("MLT's avformat (FFMPEG) module not found. Please check your FFMPEG and MLT install. Kdenlive will not work until this issue is fixed.") + "\n");*/
384
385     QProcess checkProcess2;
386     checkProcess2.start(KdenliveSettings::rendererpath(), QStringList() << "-query" << "consumer");
387     if (!checkProcess2.waitForStarted())
388         errorMessage.append(i18n("Error starting MLT's command line player (inigo).") + "\n");
389
390     checkProcess2.waitForFinished();
391
392     QByteArray result = checkProcess2.readAllStandardError();
393     if (!result.contains("sdl") || !result.contains("sdl_preview")) errorMessage.append(i18n("MLT's SDL module not found. Please check your MLT install. Kdenlive will not work until this issue is fixed.") + "\n");
394
395     if (!errorMessage.isEmpty()) {
396         errorMessage.prepend(QString("<b>%1</b><br>").arg(i18n("Fatal Error")));
397         QLabel *pix = new QLabel();
398         pix->setPixmap(KIcon("dialog-error").pixmap(30));
399         QLabel *label = new QLabel(errorMessage);
400         label->setWordWrap(true);
401         m_startLayout->addSpacing(40);
402         m_startLayout->addWidget(pix);
403         m_startLayout->addWidget(label);
404         m_systemCheckIsOk = false;
405         button(QWizard::NextButton)->setEnabled(false);
406     } else m_systemCheckIsOk = true;
407
408     if (m_systemCheckIsOk) checkMltComponents();
409 }
410
411 bool Wizard::isOk() const {
412     return m_systemCheckIsOk;
413 }
414
415 #include "wizard.moc"