]> git.sesse.net Git - kdenlive/blob - src/dvdwizardvob.cpp
Merge branch 'master' into feature/pkey
[kdenlive] / src / dvdwizardvob.cpp
1 /***************************************************************************
2  *   Copyright (C) 2009 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 "dvdwizardvob.h"
21 #include "kthumb.h"
22 #include "timecode.h"
23
24 #include <mlt++/Mlt.h>
25
26 #include <KUrlRequester>
27 #include <KDebug>
28 #include <KStandardDirs>
29 #include <KFileItem>
30 #include <KFileDialog>
31
32 #include <QHBoxLayout>
33 #include <QDomDocument>
34 #include <QTreeWidgetItem>
35 #include <QHeaderView>
36
37 DvdWizardVob::DvdWizardVob(const QString &profile, QWidget *parent) :
38         QWizardPage(parent)
39 {
40     m_view.setupUi(this);
41     m_view.intro_vob->setEnabled(false);
42     m_view.intro_vob->setFilter("video/mpeg");
43     m_view.button_add->setIcon(KIcon("document-new"));
44     m_view.button_delete->setIcon(KIcon("edit-delete"));
45     m_view.button_up->setIcon(KIcon("go-up"));
46     m_view.button_down->setIcon(KIcon("go-down"));
47     connect(m_view.use_intro, SIGNAL(toggled(bool)), m_view.intro_vob, SLOT(setEnabled(bool)));
48     connect(m_view.button_add, SIGNAL(clicked()), this, SLOT(slotAddVobFile()));
49     connect(m_view.button_delete, SIGNAL(clicked()), this, SLOT(slotDeleteVobFile()));
50     connect(m_view.button_up, SIGNAL(clicked()), this, SLOT(slotItemUp()));
51     connect(m_view.button_down, SIGNAL(clicked()), this, SLOT(slotItemDown()));
52     connect(m_view.vobs_list, SIGNAL(itemSelectionChanged()), this, SLOT(slotCheckVobList()));
53     
54     m_view.vobs_list->setIconSize(QSize(60, 45));
55
56     if (KStandardDirs::findExe("dvdauthor").isEmpty()) m_errorMessage.append(i18n("<strong>Program %1 is required for the DVD wizard.</strong>", i18n("dvdauthor")));
57     if (KStandardDirs::findExe("mkisofs").isEmpty() && KStandardDirs::findExe("genisoimage").isEmpty()) m_errorMessage.append(i18n("<strong>Program %1 or %2 is required for the DVD wizard.</strong>", i18n("mkisofs"), i18n("genisoimage")));
58     if (m_errorMessage.isEmpty()) m_view.error_message->setVisible(false);
59     else m_view.error_message->setText(m_errorMessage);
60
61     m_view.dvd_profile->addItems(QStringList() << i18n("PAL 4:3") << i18n("PAL 16:9") << i18n("NTSC 4:3") << i18n("NTSC 16:9"));
62     if (profile == "dv_pal_wide") m_view.dvd_profile->setCurrentIndex(1);
63     else if (profile == "dv_ntsc") m_view.dvd_profile->setCurrentIndex(2);
64     else if (profile == "dv_ntsc_wide") m_view.dvd_profile->setCurrentIndex(3);
65
66     connect(m_view.dvd_profile, SIGNAL(activated(int)), this, SLOT(changeFormat()));
67     connect(m_view.dvd_profile, SIGNAL(activated(int)), this, SLOT(slotCheckProfiles()));
68     m_view.vobs_list->header()->setStretchLastSection(false);
69     m_view.vobs_list->header()->setResizeMode(0, QHeaderView::Stretch);
70     m_view.vobs_list->header()->setResizeMode(1, QHeaderView::Custom);
71     m_view.vobs_list->header()->setResizeMode(2, QHeaderView::Custom);
72
73     m_capacityBar = new KCapacityBar(KCapacityBar::DrawTextInline, this);
74     QHBoxLayout *lay = new QHBoxLayout;
75     lay->addWidget(m_capacityBar);
76     m_view.size_box->setLayout(lay);
77
78     m_view.vobs_list->setItemDelegate(new DvdViewDelegate(m_view.vobs_list));
79
80 #if KDE_IS_VERSION(4,7,0)
81     m_warnMessage = new KMessageWidget;
82     m_warnMessage->setText(i18n("Conflicting video standards, check DVD profile and clips"));
83     m_warnMessage->setMessageType(KMessageWidget::Warning);
84     QGridLayout *s =  static_cast <QGridLayout*> (layout());
85     s->addWidget(m_warnMessage, 3, 0, 1, -1);
86     m_warnMessage->hide();
87 #endif
88     
89     slotCheckVobList();
90 }
91
92 DvdWizardVob::~DvdWizardVob()
93 {
94     delete m_capacityBar;
95 }
96
97 void DvdWizardVob::slotCheckProfiles()
98 {
99 #if KDE_IS_VERSION(4,7,0)
100     bool conflict = false;
101     int comboProfile = m_view.dvd_profile->currentIndex();
102     for (int i = 0; i < m_view.vobs_list->topLevelItemCount(); i++) {
103         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
104         if (item->data(0, Qt::UserRole + 1).toInt() != comboProfile) {
105             conflict = true;
106             break;
107         }
108     }
109
110     if (conflict) {
111         m_warnMessage->animatedShow();
112     }
113     else m_warnMessage->animatedHide();
114 #endif
115 }
116
117 void DvdWizardVob::slotAddVobFile(KUrl url, const QString &chapters)
118 {
119     if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl("kfiledialog:///projectfolder"), "video/mpeg", this, i18n("Add new video file"));
120     if (url.isEmpty()) return;
121     QFile f(url.path());
122     qint64 fileSize = f.size();
123     QString profilename;
124     switch (m_view.dvd_profile->currentIndex()) {
125     case 1:
126         profilename = "dv_pal_wide";
127         break;
128     case 2:
129         profilename = "dv_ntsc";
130         break;
131     case 3:
132         profilename = "dv_ntsc_wide";
133         break;
134     default:
135         profilename = "dv_pal";
136         break;
137     }
138
139     Mlt::Profile profile(profilename.toUtf8().constData());
140     profile.set_explicit(false);
141     QTreeWidgetItem *item = new QTreeWidgetItem(m_view.vobs_list, QStringList() << url.path() << QString() << KIO::convertSize(fileSize));
142     item->setData(0, Qt::UserRole, fileSize);
143     item->setData(0, Qt::DecorationRole, KIcon("video-x-generic").pixmap(60, 45));
144     item->setToolTip(0, url.path());
145     
146     Mlt::Producer *producer = new Mlt::Producer(profile, url.path().toUtf8().data());
147     if (producer && producer->is_valid() && !producer->is_blank()) {
148         //Mlt::Frame *frame = producer->get_frame();
149         //delete frame;
150         profile.from_producer(*producer);
151         int width = 45.0 * profile.dar();
152         int swidth = 45.0 * profile.width() / profile.height();
153         if (width % 2 == 1) width++;
154         item->setData(0, Qt::DecorationRole, QPixmap::fromImage(KThumb::getFrame(producer, 0, swidth, width, 45)));
155         int playTime = producer->get_playtime();
156         item->setText(1, Timecode::getStringTimecode(playTime, profile.fps()));
157         item->setData(1, Qt::UserRole, playTime);
158         int standard = -1;
159         int aspect = profile.dar() * 100;
160         if (profile.height() == 576) {
161             if (aspect > 150) standard = 1;
162             else standard = 0;
163         }
164         else if (profile.height() == 480) {
165             if (aspect > 150) standard = 3;
166             else standard = 2;
167         }
168         QString standardName;
169         switch (standard) {
170           case 3:
171               standardName = i18n("NTSC 16:9");
172               break;
173           case 2:
174               standardName = i18n("NTSC 4:3");
175               break;
176           case 1:
177               standardName = i18n("PAL 16:9");
178               break;
179           case 0:
180               standardName = i18n("PAL 4:3");
181               break;
182           default:
183               standardName = i18n("Unknown");
184         }
185         item->setData(0, Qt::UserRole, standardName);
186         item->setData(0, Qt::UserRole + 1, standard);
187         
188     }
189     if (producer) delete producer;
190
191     if (chapters.isEmpty() == false) {
192         item->setData(1, Qt::UserRole + 1, chapters);
193     }
194     else if (QFile::exists(url.path() + ".dvdchapter")) {
195         // insert chapters as children
196         QFile file(url.path() + ".dvdchapter");
197         if (file.open(QIODevice::ReadOnly)) {
198             QDomDocument doc;
199             if (doc.setContent(&file) == false) {
200                 file.close();
201                 return;
202             }
203             file.close();
204             QDomNodeList chapters = doc.elementsByTagName("chapter");
205             QStringList chaptersList;
206             for (int j = 0; j < chapters.count(); j++) {
207                 chaptersList.append(QString::number(chapters.at(j).toElement().attribute("time").toInt()));
208             }
209             item->setData(1, Qt::UserRole + 1, chaptersList.join(";"));
210         }
211     } else // Explicitly add a chapter at 00:00:00:00
212         item->setData(1, Qt::UserRole + 1, "0");
213
214     slotCheckVobList();
215     slotCheckProfiles();
216 }
217
218 void DvdWizardVob::changeFormat()
219 {
220     int max = m_view.vobs_list->topLevelItemCount();
221     QString profilename;
222     switch (m_view.dvd_profile->currentIndex()) {
223     case 1:
224         profilename = "dv_pal_wide";
225         break;
226     case 2:
227         profilename = "dv_ntsc";
228         break;
229     case 3:
230         profilename = "dv_ntsc_wide";
231         break;
232     default:
233         profilename = "dv_pal";
234         break;
235     }
236
237     Mlt::Profile profile(profilename.toUtf8().constData());
238     QPixmap pix(180, 135);
239
240     for (int i = 0; i < max; i++) {
241         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
242         Mlt::Producer *producer = new Mlt::Producer(profile, item->text(0).toUtf8().data());
243
244         if (producer->is_blank() == false) {
245             //pix = KThumb::getFrame(producer, 0, 135 * profile.dar(), 135);
246             //item->setIcon(0, pix);
247             item->setText(1, Timecode::getStringTimecode(producer->get_playtime(), profile.fps()));
248         }
249         delete producer;
250         int submax = item->childCount();
251         for (int j = 0; j < submax; j++) {
252             QTreeWidgetItem *subitem = item->child(j);
253             subitem->setText(1, Timecode::getStringTimecode(subitem->data(1, Qt::UserRole).toInt(), profile.fps()));
254         }
255     }
256     slotCheckVobList();
257 }
258
259 void DvdWizardVob::slotDeleteVobFile()
260 {
261     QTreeWidgetItem *item = m_view.vobs_list->currentItem();
262     if (item == NULL) return;
263     delete item;
264     slotCheckVobList();
265     slotCheckProfiles();
266 }
267
268
269 // virtual
270 bool DvdWizardVob::isComplete() const
271 {
272     if (!m_view.error_message->text().isEmpty()) return false;
273     if (m_view.vobs_list->topLevelItemCount() == 0) return false;
274     return true;
275 }
276
277 void DvdWizardVob::setUrl(const QString &url)
278 {
279     slotAddVobFile(KUrl(url));
280 }
281
282 QStringList DvdWizardVob::selectedUrls() const
283 {
284     QStringList result;
285     QString path;
286     int max = m_view.vobs_list->topLevelItemCount();
287     for (int i = 0; i < max; i++) {
288         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
289         if (item) result.append(item->text(0));
290     }
291     return result;
292 }
293
294
295 QStringList DvdWizardVob::durations() const
296 {
297     QStringList result;
298     QString path;
299     int max = m_view.vobs_list->topLevelItemCount();
300     for (int i = 0; i < max; i++) {
301         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
302         if (item) result.append(QString::number(item->data(1, Qt::UserRole).toInt()));
303     }
304     return result;
305 }
306
307 QStringList DvdWizardVob::chapters() const
308 {
309     QStringList result;
310     QString path;
311     int max = m_view.vobs_list->topLevelItemCount();
312     for (int i = 0; i < max; i++) {
313         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
314         if (item) {
315             result.append(item->data(1, Qt::UserRole + 1).toString());
316         }
317     }
318     return result;
319 }
320
321 void DvdWizardVob::updateChapters(QMap <QString, QString> chaptersdata)
322 {
323     int max = m_view.vobs_list->topLevelItemCount();
324     for (int i = 0; i < max; i++) {
325         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
326         if (chaptersdata.contains(item->text(0))) item->setData(1, Qt::UserRole + 1, chaptersdata.value(item->text(0)));
327     }
328 }
329
330 int DvdWizardVob::duration(int ix) const
331 {
332     int result = -1;
333     QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(ix);
334     if (item) {
335         result = item->data(1, Qt::UserRole).toInt();
336     }
337     return result;
338 }
339
340
341 QString DvdWizardVob::introMovie() const
342 {
343     if (!m_view.use_intro->isChecked()) return QString();
344     return m_view.intro_vob->url().path();
345 }
346
347 void DvdWizardVob::setIntroMovie(const QString& path)
348 {
349     m_view.intro_vob->setUrl(KUrl(path));
350     m_view.use_intro->setChecked(path.isEmpty() == false);
351 }
352
353
354 void DvdWizardVob::slotCheckVobList()
355 {
356     emit completeChanged();
357     int max = m_view.vobs_list->topLevelItemCount();
358     QTreeWidgetItem *item = m_view.vobs_list->currentItem();
359     bool hasItem = true;
360     if (item == NULL) hasItem = false;
361     m_view.button_delete->setEnabled(hasItem);
362     if (hasItem && m_view.vobs_list->indexOfTopLevelItem(item) == 0) m_view.button_up->setEnabled(false);
363     else m_view.button_up->setEnabled(hasItem);
364     if (hasItem && m_view.vobs_list->indexOfTopLevelItem(item) == max - 1) m_view.button_down->setEnabled(false);
365     else m_view.button_down->setEnabled(hasItem);
366
367     qint64 totalSize = 0;
368     for (int i = 0; i < max; i++) {
369         item = m_view.vobs_list->topLevelItem(i);
370         if (item) totalSize += (qint64) item->data(0, Qt::UserRole).toInt();
371     }
372
373     qint64 maxSize = (qint64) 47000 * 100000;
374     m_capacityBar->setValue(100 * totalSize / maxSize);
375     m_capacityBar->setText(KIO::convertSize(totalSize));
376 }
377
378 void DvdWizardVob::slotItemUp()
379 {
380     QTreeWidgetItem *item = m_view.vobs_list->currentItem();
381     if (item == NULL) return;
382     int index = m_view.vobs_list->indexOfTopLevelItem(item);
383     if (index == 0) return;
384     m_view.vobs_list->insertTopLevelItem(index - 1, m_view.vobs_list->takeTopLevelItem(index));
385 }
386
387 void DvdWizardVob::slotItemDown()
388 {
389     int max = m_view.vobs_list->topLevelItemCount();
390     QTreeWidgetItem *item = m_view.vobs_list->currentItem();
391     if (item == NULL) return;
392     int index = m_view.vobs_list->indexOfTopLevelItem(item);
393     if (index == max - 1) return;
394     m_view.vobs_list->insertTopLevelItem(index + 1, m_view.vobs_list->takeTopLevelItem(index));
395 }
396
397 bool DvdWizardVob::isPal() const
398 {
399     return m_view.dvd_profile->currentIndex() < 2;
400 }
401
402 bool DvdWizardVob::isWide() const
403 {
404     return (m_view.dvd_profile->currentIndex() == 1 || m_view.dvd_profile->currentIndex() == 3);
405 }
406
407 void DvdWizardVob::setProfile(const QString& profile)
408 {
409     if (profile == "dv_pal") m_view.dvd_profile->setCurrentIndex(0);
410     else if (profile == "dv_pal_wide") m_view.dvd_profile->setCurrentIndex(1);
411     else if (profile == "dv_ntsc") m_view.dvd_profile->setCurrentIndex(2);
412     else if (profile == "dv_ntsc_wide") m_view.dvd_profile->setCurrentIndex(3);
413 }
414
415 void DvdWizardVob::clear()
416 {
417     m_view.vobs_list->clear();
418 }