1 /***************************************************************************
2 * Copyright (C) 2009 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
20 #include "dvdwizardvob.h"
23 #include "cliptranscode.h"
25 #include <mlt++/Mlt.h>
27 #include <KUrlRequester>
29 #include <KStandardDirs>
31 #include <KFileDialog>
33 #include <QHBoxLayout>
34 #include <QDomDocument>
35 #include <QTreeWidgetItem>
36 #include <QHeaderView>
38 DvdTreeWidget::DvdTreeWidget(QWidget *parent) :
44 void DvdTreeWidget::dragEnterEvent(QDragEnterEvent * event ) {
45 if (event->mimeData()->hasUrls()) {
46 event->setDropAction(Qt::CopyAction);
47 event->setAccepted(true);
49 else QTreeWidget::dragEnterEvent(event);
52 void DvdTreeWidget::dragMoveEvent(QDragMoveEvent * event) {
53 event->acceptProposedAction();
56 void DvdTreeWidget::mouseDoubleClickEvent( QMouseEvent * )
61 void DvdTreeWidget::dropEvent(QDropEvent * event ) {
62 QList<QUrl> clips = event->mimeData()->urls();
67 DvdWizardVob::DvdWizardVob(QWidget *parent) :
72 m_view.button_add->setIcon(KIcon("list-add"));
73 m_view.button_delete->setIcon(KIcon("list-remove"));
74 m_view.button_up->setIcon(KIcon("go-up"));
75 m_view.button_down->setIcon(KIcon("go-down"));
76 m_vobList = new DvdTreeWidget(this);
77 QVBoxLayout *lay1 = new QVBoxLayout;
78 lay1->addWidget(m_vobList);
79 m_view.list_frame->setLayout(lay1);
80 m_vobList->setColumnCount(3);
81 m_vobList->setHeaderHidden(true);
83 connect(m_vobList, SIGNAL(addClips(QList<QUrl>)), this, SLOT(slotAddVobList(QList<QUrl>)));
84 connect(m_vobList, SIGNAL(addNewClip()), this, SLOT(slotAddVobFile()));
85 connect(m_view.button_add, SIGNAL(clicked()), this, SLOT(slotAddVobFile()));
86 connect(m_view.button_delete, SIGNAL(clicked()), this, SLOT(slotDeleteVobFile()));
87 connect(m_view.button_up, SIGNAL(clicked()), this, SLOT(slotItemUp()));
88 connect(m_view.button_down, SIGNAL(clicked()), this, SLOT(slotItemDown()));
89 connect(m_vobList, SIGNAL(itemSelectionChanged()), this, SLOT(slotCheckVobList()));
91 m_vobList->setIconSize(QSize(60, 45));
93 if (KStandardDirs::findExe("dvdauthor").isEmpty()) m_errorMessage.append(i18n("<strong>Program %1 is required for the DVD wizard.</strong>", i18n("dvdauthor")));
94 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")));
95 if (m_errorMessage.isEmpty()) m_view.error_message->setVisible(false);
97 m_view.error_message->setText(m_errorMessage);
98 m_installCheck = false;
101 m_view.dvd_profile->addItems(QStringList() << i18n("PAL 4:3") << i18n("PAL 16:9") << i18n("NTSC 4:3") << i18n("NTSC 16:9"));
103 connect(m_view.dvd_profile, SIGNAL(activated(int)), this, SLOT(slotCheckProfiles()));
104 m_vobList->header()->setStretchLastSection(false);
105 m_vobList->header()->setResizeMode(0, QHeaderView::Stretch);
106 m_vobList->header()->setResizeMode(1, QHeaderView::Custom);
107 m_vobList->header()->setResizeMode(2, QHeaderView::Custom);
109 m_capacityBar = new KCapacityBar(KCapacityBar::DrawTextInline, this);
110 QHBoxLayout *lay = new QHBoxLayout;
111 lay->addWidget(m_capacityBar);
112 m_view.size_box->setLayout(lay);
114 m_vobList->setItemDelegate(new DvdViewDelegate(m_vobList));
115 m_transcodeAction = new QAction(i18n("Transcode"), this);
116 connect(m_transcodeAction, SIGNAL(triggered()), this, SLOT(slotTranscodeFiles()));
118 #if KDE_IS_VERSION(4,7,0)
119 m_warnMessage = new KMessageWidget;
120 m_warnMessage->setMessageType(KMessageWidget::Warning);
121 m_warnMessage->setText(i18n("Your clips do not match selected DVD format, transcoding required."));
122 m_warnMessage->setCloseButtonVisible(false);
123 m_warnMessage->addAction(m_transcodeAction);
124 QGridLayout *s = static_cast <QGridLayout*> (layout());
125 s->addWidget(m_warnMessage, 3, 0, 1, -1);
126 m_warnMessage->hide();
127 m_view.button_transcode->setHidden(true);
129 m_view.button_transcode->setDefaultAction(m_transcodeAction);
130 m_view.button_transcode->setEnabled(false);
136 DvdWizardVob::~DvdWizardVob()
138 delete m_capacityBar;
141 void DvdWizardVob::slotCheckProfiles()
143 bool conflict = false;
144 int comboProfile = m_view.dvd_profile->currentIndex();
145 for (int i = 0; i < m_vobList->topLevelItemCount(); i++) {
146 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
147 if (item->data(0, Qt::UserRole + 1).toInt() != comboProfile) {
152 m_transcodeAction->setEnabled(conflict);
157 #if KDE_IS_VERSION(4,7,0)
158 m_warnMessage->animatedHide();
160 if (m_installCheck) m_view.error_message->setVisible(false);
165 void DvdWizardVob::slotAddVobList(QList <QUrl>list)
167 foreach (const QUrl url, list) {
168 slotAddVobFile(KUrl(url), QString(), false);
174 void DvdWizardVob::slotAddVobFile(KUrl url, const QString &chapters, bool checkFormats)
176 if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl("kfiledialog:///projectfolder"), "video/mpeg", this, i18n("Add new video file"));
177 if (url.isEmpty()) return;
179 qint64 fileSize = f.size();
181 Mlt::Profile profile;
182 profile.set_explicit(false);
183 QTreeWidgetItem *item = new QTreeWidgetItem(m_vobList, QStringList() << url.path() << QString() << KIO::convertSize(fileSize));
184 item->setData(2, Qt::UserRole, fileSize);
185 item->setData(0, Qt::DecorationRole, KIcon("video-x-generic").pixmap(60, 45));
186 item->setToolTip(0, url.path());
188 QString resource = url.path();
189 resource.prepend("avformat:");
190 Mlt::Producer *producer = new Mlt::Producer(profile, resource.toUtf8().data());
191 if (producer && producer->is_valid() && !producer->is_blank()) {
192 //Mlt::Frame *frame = producer->get_frame();
194 profile.from_producer(*producer);
195 int width = 45.0 * profile.dar();
196 int swidth = 45.0 * profile.width() / profile.height();
197 if (width % 2 == 1) width++;
198 item->setData(0, Qt::DecorationRole, QPixmap::fromImage(KThumb::getFrame(producer, 0, swidth, width, 45)));
199 int playTime = producer->get_playtime();
200 item->setText(1, Timecode::getStringTimecode(playTime, profile.fps()));
201 item->setData(1, Qt::UserRole, playTime);
203 int aspect = profile.dar() * 100;
204 if (profile.height() == 576) {
205 if (aspect > 150) standard = 1;
208 else if (profile.height() == 480) {
209 if (aspect > 150) standard = 3;
212 QString standardName;
215 standardName = i18n("NTSC 16:9");
218 standardName = i18n("NTSC 4:3");
221 standardName = i18n("PAL 16:9");
224 standardName = i18n("PAL 4:3");
227 standardName = i18n("Unknown");
229 item->setData(0, Qt::UserRole, standardName);
230 item->setData(0, Qt::UserRole + 1, standard);
231 item->setData(0, Qt::UserRole + 2, QSize(profile.dar() * profile.height(), profile.height()));
232 if (m_vobList->topLevelItemCount() == 1) {
233 // This is the first added movie, auto select DVD format
235 m_view.dvd_profile->blockSignals(true);
236 m_view.dvd_profile->setCurrentIndex(standard);
237 m_view.dvd_profile->blockSignals(false);
243 // Cannot load movie, reject
244 showError(i18n("The clip %1 is invalid.", url.fileName()));
246 if (producer) delete producer;
248 if (chapters.isEmpty() == false) {
249 item->setData(1, Qt::UserRole + 1, chapters);
251 else if (QFile::exists(url.path() + ".dvdchapter")) {
252 // insert chapters as children
253 QFile file(url.path() + ".dvdchapter");
254 if (file.open(QIODevice::ReadOnly)) {
256 if (doc.setContent(&file) == false) {
261 QDomNodeList chapters = doc.elementsByTagName("chapter");
262 QStringList chaptersList;
263 for (int j = 0; j < chapters.count(); j++) {
264 chaptersList.append(QString::number(chapters.at(j).toElement().attribute("time").toInt()));
266 item->setData(1, Qt::UserRole + 1, chaptersList.join(";"));
268 } else // Explicitly add a chapter at 00:00:00:00
269 item->setData(1, Qt::UserRole + 1, "0");
277 void DvdWizardVob::slotDeleteVobFile()
279 QTreeWidgetItem *item = m_vobList->currentItem();
280 if (item == NULL) return;
288 bool DvdWizardVob::isComplete() const
290 if (!m_installCheck) return false;
291 if (m_vobList->topLevelItemCount() == 0) return false;
295 void DvdWizardVob::setUrl(const QString &url)
297 slotAddVobFile(KUrl(url));
300 QStringList DvdWizardVob::selectedUrls() const
304 int max = m_vobList->topLevelItemCount();
306 if (m_view.use_intro->isChecked()) {
307 // First movie is only for intro
310 for (; i < max; i++) {
311 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
312 if (item) result.append(item->text(0));
318 QStringList DvdWizardVob::durations() const
322 int max = m_vobList->topLevelItemCount();
323 for (int i = 0; i < max; i++) {
324 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
325 if (item) result.append(QString::number(item->data(1, Qt::UserRole).toInt()));
330 QStringList DvdWizardVob::chapters() const
334 int max = m_vobList->topLevelItemCount();
335 for (int i = 0; i < max; i++) {
336 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
338 result.append(item->data(1, Qt::UserRole + 1).toString());
344 void DvdWizardVob::updateChapters(QMap <QString, QString> chaptersdata)
346 int max = m_vobList->topLevelItemCount();
347 for (int i = 0; i < max; i++) {
348 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
349 if (chaptersdata.contains(item->text(0))) item->setData(1, Qt::UserRole + 1, chaptersdata.value(item->text(0)));
353 int DvdWizardVob::duration(int ix) const
356 QTreeWidgetItem *item = m_vobList->topLevelItem(ix);
358 result = item->data(1, Qt::UserRole).toInt();
363 const QString DvdWizardVob::introMovie() const
366 if (m_view.use_intro->isChecked() && m_vobList->topLevelItemCount() > 0) url = m_vobList->topLevelItem(0)->text(0);
370 void DvdWizardVob::setUseIntroMovie(bool use)
372 m_view.use_intro->setChecked(use);
375 void DvdWizardVob::slotCheckVobList()
377 emit completeChanged();
378 int max = m_vobList->topLevelItemCount();
379 QTreeWidgetItem *item = m_vobList->currentItem();
381 if (item == NULL) hasItem = false;
382 m_view.button_delete->setEnabled(hasItem);
383 if (hasItem && m_vobList->indexOfTopLevelItem(item) == 0) m_view.button_up->setEnabled(false);
384 else m_view.button_up->setEnabled(hasItem);
385 if (hasItem && m_vobList->indexOfTopLevelItem(item) == max - 1) m_view.button_down->setEnabled(false);
386 else m_view.button_down->setEnabled(hasItem);
388 qint64 totalSize = 0;
389 for (int i = 0; i < max; i++) {
390 item = m_vobList->topLevelItem(i);
391 if (item) totalSize += (qint64) item->data(2, Qt::UserRole).toInt();
394 qint64 maxSize = (qint64) 47000 * 100000;
395 m_capacityBar->setValue(100 * totalSize / maxSize);
396 m_capacityBar->setText(KIO::convertSize(totalSize));
399 void DvdWizardVob::slotItemUp()
401 QTreeWidgetItem *item = m_vobList->currentItem();
402 if (item == NULL) return;
403 int index = m_vobList->indexOfTopLevelItem(item);
404 if (index == 0) return;
405 m_vobList->insertTopLevelItem(index - 1, m_vobList->takeTopLevelItem(index));
408 void DvdWizardVob::slotItemDown()
410 int max = m_vobList->topLevelItemCount();
411 QTreeWidgetItem *item = m_vobList->currentItem();
412 if (item == NULL) return;
413 int index = m_vobList->indexOfTopLevelItem(item);
414 if (index == max - 1) return;
415 m_vobList->insertTopLevelItem(index + 1, m_vobList->takeTopLevelItem(index));
418 DVDFORMAT DvdWizardVob::dvdFormat() const
420 return (DVDFORMAT) m_view.dvd_profile->currentIndex();
423 const QString DvdWizardVob::dvdProfile() const
426 switch (m_view.dvd_profile->currentIndex()) {
428 profile = "dv_pal_wide";
434 profile = "dv_ntsc_wide";
443 QString DvdWizardVob::getDvdProfile(DVDFORMAT format)
448 profile = "dv_pal_wide";
454 profile = "dv_ntsc_wide";
462 void DvdWizardVob::setProfile(const QString& profile)
464 if (profile == "dv_pal_wide") m_view.dvd_profile->setCurrentIndex(PAL_WIDE);
465 else if (profile == "dv_ntsc") m_view.dvd_profile->setCurrentIndex(NTSC);
466 else if (profile == "dv_ntsc_wide") m_view.dvd_profile->setCurrentIndex(NTSC_WIDE);
467 else m_view.dvd_profile->setCurrentIndex(PAL);
470 void DvdWizardVob::clear()
475 void DvdWizardVob::slotTranscodeFiles()
477 // Find transcoding infos related to selected DVD profile
478 KSharedConfigPtr config = KSharedConfig::openConfig("kdenlivetranscodingrc", KConfig::CascadeConfig);
479 KConfigGroup transConfig(config, "Transcoding");
481 QString profileEasyName;
484 switch (m_view.dvd_profile->currentIndex()) {
486 profileEasyName = "DVD PAL 16:9";
487 destSize = QSize(1024, 576);
488 finalSize = QSize(720, 576);
491 profileEasyName = "DVD NTSC 4:3";
492 destSize = QSize(640, 480);
493 finalSize = QSize(720, 480);
496 profileEasyName = "DVD NTSC 16:9";
497 destSize = QSize(853, 480);
498 finalSize = QSize(720, 480);
501 profileEasyName = "DVD PAL 4:3";
502 destSize = QSize(768, 576);
503 finalSize = QSize(720, 576);
505 QString params = transConfig.readEntry(profileEasyName);
507 // Transcode files that do not match selected profile
508 int max = m_vobList->topLevelItemCount();
509 int format = m_view.dvd_profile->currentIndex();
510 for (int i = 0; i < max; i++) {
511 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
512 if (item->data(0, Qt::UserRole + 1).toInt() != format) {
513 // File needs to be transcoded
514 m_transcodeAction->setEnabled(false);
515 QSize original = item->data(0, Qt::UserRole + 2).toSize();
516 double input_aspect= (double) original.width() / original.height();
517 QStringList postParams;
518 if (input_aspect > (double) destSize.width() / destSize.height()) {
520 int conv_height = (int) (destSize.width() / input_aspect);
521 int conv_pad = (int) (((double) (destSize.height() - conv_height)) / 2.0);
522 if (conv_pad %2 == 1) conv_pad --;
523 postParams << "-vf" << QString("scale=%1:%2,pad=%3:%4:0:%5,setdar=%6").arg(finalSize.width()).arg(destSize.height() - 2 * conv_pad).arg(finalSize.width()).arg(finalSize.height()).arg(conv_pad).arg(input_aspect);
526 int conv_width = (int) (destSize.height() * input_aspect);
527 int conv_pad = (int) (((double) (destSize.width() - conv_width)) / destSize.width() * finalSize.width() / 2.0);
528 if (conv_pad %2 == 1) conv_pad --;
529 postParams << "-vf" << QString("scale=%1:%2,pad=%3:%4:%5:0,setdar=%6").arg(finalSize.width() - 2 * conv_pad).arg(destSize.height()).arg(finalSize.width()).arg(finalSize.height()).arg(conv_pad).arg(input_aspect);
531 ClipTranscode *d = new ClipTranscode(KUrl::List () << KUrl(item->text(0)), params.section(';', 0, 0), postParams, i18n("Transcoding to DVD format"), true, this);
532 connect(d, SIGNAL(transcodedClip(KUrl,KUrl)), this, SLOT(slotTranscodedClip(KUrl, KUrl)));
538 void DvdWizardVob::slotTranscodedClip(KUrl src, KUrl transcoded)
540 int max = m_vobList->topLevelItemCount();
541 for (int i = 0; i < max; i++) {
542 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
543 if (KUrl(item->text(0)).path() == src.path()) {
544 // Replace movie with transcoded version
545 item->setText(0, transcoded.path());
547 QFile f(transcoded.path());
548 qint64 fileSize = f.size();
550 Mlt::Profile profile;
551 profile.set_explicit(false);
552 item->setText(2, KIO::convertSize(fileSize));
553 item->setData(2, Qt::UserRole, fileSize);
554 item->setData(0, Qt::DecorationRole, KIcon("video-x-generic").pixmap(60, 45));
555 item->setToolTip(0, transcoded.path());
557 QString resource = transcoded.path();
558 resource.prepend("avformat:");
559 Mlt::Producer *producer = new Mlt::Producer(profile, resource.toUtf8().data());
560 if (producer && producer->is_valid() && !producer->is_blank()) {
561 profile.from_producer(*producer);
562 int width = 45.0 * profile.dar();
563 int swidth = 45.0 * profile.width() / profile.height();
564 if (width % 2 == 1) width++;
565 item->setData(0, Qt::DecorationRole, QPixmap::fromImage(KThumb::getFrame(producer, 0, swidth, width, 45)));
566 int playTime = producer->get_playtime();
567 item->setText(1, Timecode::getStringTimecode(playTime, profile.fps()));
568 item->setData(1, Qt::UserRole, playTime);
570 int aspect = profile.dar() * 100;
571 if (profile.height() == 576) {
572 if (aspect > 150) standard = 1;
575 else if (profile.height() == 480) {
576 if (aspect > 150) standard = 3;
579 QString standardName;
582 standardName = i18n("NTSC 16:9");
585 standardName = i18n("NTSC 4:3");
588 standardName = i18n("PAL 16:9");
591 standardName = i18n("PAL 4:3");
594 standardName = i18n("Unknown");
596 item->setData(0, Qt::UserRole, standardName);
597 item->setData(0, Qt::UserRole + 1, standard);
598 item->setData(0, Qt::UserRole + 2, QSize(profile.dar() * profile.height(), profile.height()));
601 // Cannot load movie, reject
602 showError(i18n("The clip %1 is invalid.", transcoded.fileName()));
604 if (producer) delete producer;
612 void DvdWizardVob::showProfileError()
614 #if KDE_IS_VERSION(4,7,0)
615 m_warnMessage->setText(i18n("Your clips do not match selected DVD format, transcoding required."));
616 m_warnMessage->setCloseButtonVisible(false);
617 m_warnMessage->addAction(m_transcodeAction);
618 m_warnMessage->animatedShow();
620 m_view.error_message->setText(i18n("Your clips do not match selected DVD format, transcoding required."));
621 m_view.error_message->setVisible(true);
625 void DvdWizardVob::showError(const QString error)
627 #if KDE_IS_VERSION(4,7,0)
628 m_warnMessage->setText(error);
629 m_warnMessage->setCloseButtonVisible(true);
630 m_warnMessage->removeAction(m_transcodeAction);
631 m_warnMessage->animatedShow();
633 m_view.error_message->setText(error);
634 m_view.error_message->setVisible(true);