]> git.sesse.net Git - kdenlive/blob - src/wizard.cpp
Improve profile selection in First run 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 *page2 = new QWizardPage;
48     page2->setTitle(i18n("Video Standard"));
49     m_standard.setupUi(page2);
50     //m_standard.profiles_list->setMaximumHeight(100);
51     // build profiles lists
52     m_profilesInfo = ProfilesDialog::getProfilesInfo();
53     QMap<QString, QString>::const_iterator i = m_profilesInfo.constBegin();
54     while (i != m_profilesInfo.constEnd()) {
55         QMap< QString, QString > profileData = ProfilesDialog::getSettingsFromFile(i.value());
56         if (profileData.value("width") == "720") m_dvProfiles.append(i.key());
57         else if (profileData.value("width").toInt() >= 1080) m_hdvProfiles.append(i.key());
58         else m_otherProfiles.append(i.key());
59         ++i;
60     }
61
62     connect(m_standard.button_all, SIGNAL(toggled(bool)), this, SLOT(slotCheckStandard()));
63     connect(m_standard.button_hdv, SIGNAL(toggled(bool)), this, SLOT(slotCheckStandard()));
64     connect(m_standard.button_dv, SIGNAL(toggled(bool)), this, SLOT(slotCheckStandard()));
65     slotCheckStandard();
66     connect(m_standard.profiles_list, SIGNAL(itemSelectionChanged()), this, SLOT(slotCheckSelectedItem()));
67     addPage(page2);
68
69     QWizardPage *page3 = new QWizardPage;
70     page3->setTitle(i18n("Additional Settings"));
71     m_extra.setupUi(page3);
72     m_extra.projectfolder->setMode(KFile::Directory);
73     m_extra.projectfolder->setPath(QDir::homePath() + "/kdenlive");
74     m_extra.videothumbs->setChecked(KdenliveSettings::videothumbnails());
75     m_extra.audiothumbs->setChecked(KdenliveSettings::audiothumbnails());
76     m_extra.autosave->setChecked(KdenliveSettings::crashrecovery());
77     connect(m_extra.videothumbs, SIGNAL(stateChanged(int)), this, SLOT(slotCheckThumbs()));
78     connect(m_extra.audiothumbs, SIGNAL(stateChanged(int)), this, SLOT(slotCheckThumbs()));
79     slotCheckThumbs();
80     addPage(page3);
81     QTimer::singleShot(500, this, SLOT(slotCheckMlt()));
82 }
83
84 void Wizard::installExtraMimes(QString baseName, QStringList globs) {
85     QString mimefile = baseName;
86     mimefile.replace('/', '-');
87     KMimeType::Ptr mime = KMimeType::mimeType(baseName);
88     if (!mime) {
89         kDebug() << "KMimeTypeTrader: mimeType " << baseName << " not found";
90     } else {
91         QStringList extensions = mime->patterns();
92         QString comment = mime->comment();
93         foreach(const QString &glob, globs) {
94             if (!extensions.contains(glob)) extensions << glob;
95         }
96         kDebug() << "EXTS: " << extensions;
97         QString packageFileName = KStandardDirs::locateLocal("xdgdata-mime", "packages/" + mimefile + ".xml");
98         kDebug() << "INSTALLING NEW MIME TO: " << packageFileName;
99         QFile packageFile(packageFileName);
100         if (!packageFile.open(QIODevice::WriteOnly)) {
101             kError() << "Couldn't open" << packageFileName << "for writing";
102             return;
103         }
104         QXmlStreamWriter writer(&packageFile);
105         writer.setAutoFormatting(true);
106         writer.writeStartDocument();
107
108         const QString nsUri = "http://www.freedesktop.org/standards/shared-mime-info";
109         writer.writeDefaultNamespace(nsUri);
110         writer.writeStartElement("mime-info");
111         writer.writeStartElement(nsUri, "mime-type");
112         writer.writeAttribute("type", baseName);
113
114         if (!comment.isEmpty()) {
115             writer.writeStartElement(nsUri, "comment");
116             writer.writeCharacters(comment);
117             writer.writeEndElement(); // comment
118         }
119
120         foreach(const QString& pattern, extensions) {
121             writer.writeStartElement(nsUri, "glob");
122             writer.writeAttribute("pattern", pattern);
123             writer.writeEndElement(); // glob
124         }
125
126         writer.writeEndElement(); // mime-info
127         writer.writeEndElement(); // mime-type
128         writer.writeEndDocument();
129     }
130 }
131
132 void Wizard::runUpdateMimeDatabase() {
133     const QString localPackageDir = KStandardDirs::locateLocal("xdgdata-mime", QString());
134     //Q_ASSERT(!localPackageDir.isEmpty());
135     KProcess proc;
136     proc << "update-mime-database";
137     proc << localPackageDir;
138     const int exitCode = proc.execute();
139     if (exitCode) {
140         kWarning() << proc.program() << "exited with error code" << exitCode;
141     }
142 }
143
144 void Wizard::slotCheckThumbs() {
145     QString pixname = "timeline_vthumbs.png";
146     if (!m_extra.audiothumbs->isChecked() && !m_extra.videothumbs->isChecked()) {
147         pixname = "timeline_nothumbs.png";
148     } else if (m_extra.audiothumbs->isChecked()) {
149         if (m_extra.videothumbs->isChecked())
150             pixname = "timeline_avthumbs.png";
151         else pixname = "timeline_athumbs.png";
152     }
153
154     m_extra.timeline_preview->setPixmap(QPixmap(KStandardDirs::locate("appdata", pixname)));
155 }
156
157 void Wizard::slotCheckStandard() {
158     m_standard.profiles_list->clear();
159     QStringList profiles;
160     if (m_standard.button_dv->isChecked()) {
161         // DV standard
162         m_standard.profiles_list->addItems(m_dvProfiles);
163     } else if (m_standard.button_hdv->isChecked()) {
164         // HDV standard
165         m_standard.profiles_list->addItems(m_hdvProfiles);
166     } else {
167         m_standard.profiles_list->addItems(m_dvProfiles);
168         m_standard.profiles_list->addItems(m_hdvProfiles);
169         m_standard.profiles_list->addItems(m_otherProfiles);
170         //m_standard.profiles_list->sortItems();
171     }
172
173     for (int i = 0; i < m_standard.profiles_list->count(); i++) {
174         QListWidgetItem *item = m_standard.profiles_list->item(i);
175         MltVideoProfile prof = ProfilesDialog::getVideoProfile(m_profilesInfo.value(item->text()));
176         const QString infoString = i18n("<b>Frame size:</b>%1x%2<br><b>Frame rate:</b>%3/%4<br><b>Aspect ratio:</b>%5/%6<br><b>Display 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));
177         item->setToolTip(infoString);
178     }
179
180     m_standard.profiles_list->setSortingEnabled(true);
181     m_standard.profiles_list->setCurrentRow(0);
182 }
183
184 void Wizard::slotCheckSelectedItem() {
185     // Make sure we always have an item highlighted
186     m_standard.profiles_list->setCurrentRow(m_standard.profiles_list->currentRow());
187 }
188
189
190 void Wizard::adjustSettings() {
191     if (m_extra.installmimes->isChecked()) {
192         QStringList globs;
193         globs << "*.mts" << "*.m2t" << "*.mod" << "*.ts";
194         installExtraMimes("video/mpeg", globs);
195         globs.clear();
196         globs << "*.dv";
197         installExtraMimes("video/dv", globs);
198         runUpdateMimeDatabase();
199     }
200     KdenliveSettings::setAudiothumbnails(m_extra.audiothumbs->isChecked());
201     KdenliveSettings::setVideothumbnails(m_extra.videothumbs->isChecked());
202     KdenliveSettings::setCrashrecovery(m_extra.autosave->isChecked());
203     if (m_standard.profiles_list->currentItem()) {
204         QString selectedProfile = m_profilesInfo.value(m_standard.profiles_list->currentItem()->text());
205         if (selectedProfile.isEmpty()) selectedProfile = "dv_pal";
206         KdenliveSettings::setDefault_profile(selectedProfile);
207     }
208     QString path = m_extra.projectfolder->url().path();
209     if (KStandardDirs::makeDir(path) == false) kDebug() << "/// ERROR CREATING PROJECT FOLDER: " << path;
210     KdenliveSettings::setDefaultprojectfolder(path);
211
212 }
213
214 void Wizard::slotCheckMlt() {
215     QString errorMessage;
216     if (KdenliveSettings::rendererpath().isEmpty()) {
217         errorMessage.append(i18n("your MLT installation cannot be found. Install MLT and restart Kdenlive.\n"));
218     }
219     QProcess checkProcess;
220     checkProcess.start(KdenliveSettings::rendererpath(), QStringList() << "-query" << "producer");
221     if (!checkProcess.waitForStarted())
222         errorMessage.append(i18n("Error starting MLT's command line player (inigo)") + ".\n");
223
224     checkProcess.waitForFinished();
225
226     QByteArray result = checkProcess.readAllStandardError();
227     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");
228
229     QProcess checkProcess2;
230     checkProcess2.start(KdenliveSettings::rendererpath(), QStringList() << "-query" << "consumer");
231     if (!checkProcess2.waitForStarted())
232         errorMessage.append(i18n("Error starting MLT's command line player (inigo).") + "\n");
233
234     checkProcess2.waitForFinished();
235
236     result = checkProcess2.readAllStandardError();
237     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");
238
239     if (!errorMessage.isEmpty()) {
240         errorMessage.prepend(QString("<b>%1</b><br>").arg(i18n("Fatal Error")));
241         QLabel *pix = new QLabel();
242         pix->setPixmap(KIcon("dialog-error").pixmap(30));
243         QLabel *label = new QLabel(errorMessage);
244         label->setWordWrap(true);
245         m_startLayout->addSpacing(40);
246         m_startLayout->addWidget(pix);
247         m_startLayout->addWidget(label);
248         m_systemCheckIsOk = false;
249         button(QWizard::NextButton)->setEnabled(false);
250     } else m_systemCheckIsOk = true;
251 }
252
253 bool Wizard::isOk() const {
254     return m_systemCheckIsOk;
255 }
256
257 #include "wizard.moc"