]> git.sesse.net Git - kdenlive/blob - src/wizard.cpp
New wizard at first Kdenlive startup: allows user to select video standard (pal,...
[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 <QVBoxLayout>
22 #include <QFile>
23 #include <QXmlStreamWriter>
24
25 #include <KStandardDirs>
26 #include <KLocale>
27 #include <KProcess>
28 #include <kmimetype.h>
29
30 #include "kdenlivesettings.h"
31 #include "profilesdialog.h"
32 #include "wizard.h"
33
34 Wizard::Wizard(QWidget *parent): QWizard(parent) {
35     setPixmap(QWizard::WatermarkPixmap, QPixmap(KStandardDirs::locate("appdata", "banner.png")));
36
37     QWizardPage *page1 = new QWizardPage;
38     page1->setTitle("Welcome");
39     QLabel *label = new QLabel("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...");
40     label->setWordWrap(true);
41     QVBoxLayout *layout = new QVBoxLayout;
42     layout->addWidget(label);
43     page1->setLayout(layout);
44     addPage(page1);
45
46     QWizardPage *page2 = new QWizardPage;
47     page2->setTitle("Video Standard");
48     m_standard.setupUi(page2);
49     m_standard.profiles_list->setMaximumHeight(100);
50     connect(m_standard.button_pal, SIGNAL(toggled(bool)), this, SLOT(slotCheckStandard()));
51     connect(m_standard.button_dv, SIGNAL(toggled(bool)), this, SLOT(slotCheckStandard()));
52     slotCheckStandard();
53     connect(m_standard.profiles_list, SIGNAL(itemSelectionChanged()), this, SLOT(slotCheckSelectedItem()));
54     addPage(page2);
55
56     QWizardPage *page3 = new QWizardPage;
57     page3->setTitle("Additional Settings");
58     m_extra.setupUi(page3);
59     m_extra.videothumbs->setChecked(KdenliveSettings::videothumbnails());
60     m_extra.audiothumbs->setChecked(KdenliveSettings::audiothumbnails());
61     m_extra.autosave->setChecked(KdenliveSettings::crashrecovery());
62     connect(m_extra.videothumbs, SIGNAL(stateChanged(int)), this, SLOT(slotCheckThumbs()));
63     connect(m_extra.audiothumbs, SIGNAL(stateChanged(int)), this, SLOT(slotCheckThumbs()));
64     slotCheckThumbs();
65     addPage(page3);
66 }
67
68 void Wizard::installExtraMimes(QString baseName, QStringList globs) {
69     QString mimefile = baseName;
70     mimefile.replace('/', '-');
71     KMimeType::Ptr mime = KMimeType::mimeType(baseName);
72     if (!mime) {
73         kDebug() << "KMimeTypeTrader: mimeType " << baseName << " not found";
74     } else {
75         QStringList extensions = mime->patterns();
76         QString comment = mime->comment();
77         foreach(const QString &glob, globs) {
78             if (!extensions.contains(glob)) extensions << glob;
79         }
80         kDebug() << "EXTS: " << extensions;
81         QString packageFileName = KStandardDirs::locateLocal("xdgdata-mime", "packages/" + mimefile + ".xml");
82         kDebug() << "INSTALLING NEW MIME TO: " << packageFileName;
83         QFile packageFile(packageFileName);
84         if (!packageFile.open(QIODevice::WriteOnly)) {
85             kError() << "Couldn't open" << packageFileName << "for writing";
86             return;
87         }
88         QXmlStreamWriter writer(&packageFile);
89         writer.setAutoFormatting(true);
90         writer.writeStartDocument();
91
92         const QString nsUri = "http://www.freedesktop.org/standards/shared-mime-info";
93         writer.writeDefaultNamespace(nsUri);
94         writer.writeStartElement("mime-info");
95         writer.writeStartElement(nsUri, "mime-type");
96         writer.writeAttribute("type", baseName);
97
98         if (!comment.isEmpty()) {
99             writer.writeStartElement(nsUri, "comment");
100             writer.writeCharacters(comment);
101             writer.writeEndElement(); // comment
102         }
103
104         foreach(const QString& pattern, extensions) {
105             writer.writeStartElement(nsUri, "glob");
106             writer.writeAttribute("pattern", pattern);
107             writer.writeEndElement(); // glob
108         }
109
110         writer.writeEndElement(); // mime-info
111         writer.writeEndElement(); // mime-type
112         writer.writeEndDocument();
113     }
114 }
115
116 void Wizard::runUpdateMimeDatabase() {
117     const QString localPackageDir = KStandardDirs::locateLocal("xdgdata-mime", QString());
118     Q_ASSERT(!localPackageDir.isEmpty());
119     KProcess proc;
120     proc << "update-mime-database";
121     proc << localPackageDir;
122     const int exitCode = proc.execute();
123     if (exitCode) {
124         kWarning() << proc.program() << "exited with error code" << exitCode;
125     }
126 }
127
128 void Wizard::slotCheckThumbs() {
129     QString pixname = "timeline_vthumbs.png";
130     if (!m_extra.audiothumbs->isChecked() && !m_extra.videothumbs->isChecked()) {
131         pixname = "timeline_nothumbs.png";
132     } else if (m_extra.audiothumbs->isChecked()) {
133         if (m_extra.videothumbs->isChecked())
134             pixname = "timeline_avthumbs.png";
135         else pixname = "timeline_athumbs.png";
136     }
137
138     m_extra.timeline_preview->setPixmap(QPixmap(KStandardDirs::locate("appdata", pixname)));
139 }
140
141 void Wizard::slotCheckStandard() {
142     QStringList profiles;
143     if (m_standard.button_pal->isChecked()) {
144         // PAL standard
145         if (m_standard.button_dv->isChecked()) {
146             profiles << "dv_pal" << "dv_pal_wide";
147         } else {
148             profiles << "hdv_720_25p" << "hdv_1080_50i";
149         }
150     } else {
151         // NTSC standard
152         if (m_standard.button_dv->isChecked()) {
153             profiles << "dv_ntsc" << "dv_ntsc_wide";
154         } else {
155             profiles << "hdv_720_30p" << "hdv_1080_60i" << "atsc_720p_30" << "atsc_1080i_60";
156         }
157     }
158     m_standard.profiles_list->clear();
159     QStringList profilesDescription;
160     foreach(const QString &prof, profiles) {
161         QListWidgetItem *item = new QListWidgetItem(ProfilesDialog::getProfileDescription(prof), m_standard.profiles_list);
162         item->setData(Qt::UserRole, prof);
163     }
164     m_standard.profiles_list->setCurrentRow(0);
165 }
166
167 void Wizard::slotCheckSelectedItem() {
168     // Make sure we always have an item highlighted
169     m_standard.profiles_list->setCurrentRow(m_standard.profiles_list->currentRow());
170 }
171
172
173 void Wizard::adjustSettings() {
174     if (m_extra.installmimes->isChecked()) {
175         QStringList globs;
176         globs << "*.mts" << "*.m2t";
177         installExtraMimes("video/mpeg", globs);
178         globs.clear();
179         globs << "*.dv";
180         installExtraMimes("video/dv", globs);
181         runUpdateMimeDatabase();
182     }
183     KdenliveSettings::setAudiothumbnails(m_extra.audiothumbs->isChecked());
184     KdenliveSettings::setVideothumbnails(m_extra.videothumbs->isChecked());
185     KdenliveSettings::setCrashrecovery(m_extra.autosave->isChecked());
186     if (m_standard.profiles_list->currentItem()) {
187         KdenliveSettings::setDefault_profile(m_standard.profiles_list->currentItem()->data(Qt::UserRole).toString());
188     }
189 }
190
191 #include "wizard.moc"