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