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"
24 #include <mlt++/Mlt.h>
26 #include <KUrlRequester>
28 #include <KStandardDirs>
30 #include <KFileDialog>
32 #include <QHBoxLayout>
33 #include <QDomDocument>
34 #include <QTreeWidgetItem>
35 #include <QHeaderView>
37 DvdWizardVob::DvdWizardVob(const QString &profile, QWidget *parent) :
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()));
54 m_view.vobs_list->setIconSize(QSize(60, 45));
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);
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);
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);
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);
78 m_view.vobs_list->setItemDelegate(new DvdViewDelegate(m_view.vobs_list));
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();
92 DvdWizardVob::~DvdWizardVob()
97 void DvdWizardVob::slotCheckProfiles()
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) {
111 m_warnMessage->animatedShow();
113 else m_warnMessage->animatedHide();
117 void DvdWizardVob::slotAddVobFile(KUrl url, const QString &chapters)
119 if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl("kfiledialog:///projectfolder"), "video/mpeg", this, i18n("Add new video file"));
120 if (url.isEmpty()) return;
122 qint64 fileSize = f.size();
124 Mlt::Profile profile;
125 profile.set_explicit(false);
126 QTreeWidgetItem *item = new QTreeWidgetItem(m_view.vobs_list, QStringList() << url.path() << QString() << KIO::convertSize(fileSize));
127 item->setData(0, Qt::UserRole, fileSize);
128 item->setData(0, Qt::DecorationRole, KIcon("video-x-generic").pixmap(60, 45));
129 item->setToolTip(0, url.path());
131 Mlt::Producer *producer = new Mlt::Producer(profile, url.path().toUtf8().data());
132 if (producer && producer->is_valid() && !producer->is_blank()) {
133 //Mlt::Frame *frame = producer->get_frame();
135 profile.from_producer(*producer);
136 int width = 45.0 * profile.dar();
137 int swidth = 45.0 * profile.width() / profile.height();
138 if (width % 2 == 1) width++;
139 item->setData(0, Qt::DecorationRole, QPixmap::fromImage(KThumb::getFrame(producer, 0, swidth, width, 45)));
140 int playTime = producer->get_playtime();
141 item->setText(1, Timecode::getStringTimecode(playTime, profile.fps()));
142 item->setData(1, Qt::UserRole, playTime);
144 int aspect = profile.dar() * 100;
145 if (profile.height() == 576) {
146 if (aspect > 150) standard = 1;
149 else if (profile.height() == 480) {
150 if (aspect > 150) standard = 3;
153 QString standardName;
156 standardName = i18n("NTSC 16:9");
159 standardName = i18n("NTSC 4:3");
162 standardName = i18n("PAL 16:9");
165 standardName = i18n("PAL 4:3");
168 standardName = i18n("Unknown");
170 item->setData(0, Qt::UserRole, standardName);
171 item->setData(0, Qt::UserRole + 1, standard);
172 if (m_view.vobs_list->topLevelItemCount() == 1) {
173 // This is the first added movie, auto select DVD format
175 m_view.dvd_profile->blockSignals(true);
176 m_view.dvd_profile->setCurrentIndex(standard);
177 m_view.dvd_profile->blockSignals(false);
182 if (producer) delete producer;
184 if (chapters.isEmpty() == false) {
185 item->setData(1, Qt::UserRole + 1, chapters);
187 else if (QFile::exists(url.path() + ".dvdchapter")) {
188 // insert chapters as children
189 QFile file(url.path() + ".dvdchapter");
190 if (file.open(QIODevice::ReadOnly)) {
192 if (doc.setContent(&file) == false) {
197 QDomNodeList chapters = doc.elementsByTagName("chapter");
198 QStringList chaptersList;
199 for (int j = 0; j < chapters.count(); j++) {
200 chaptersList.append(QString::number(chapters.at(j).toElement().attribute("time").toInt()));
202 item->setData(1, Qt::UserRole + 1, chaptersList.join(";"));
204 } else // Explicitly add a chapter at 00:00:00:00
205 item->setData(1, Qt::UserRole + 1, "0");
211 void DvdWizardVob::changeFormat()
213 int max = m_view.vobs_list->topLevelItemCount();
215 switch (m_view.dvd_profile->currentIndex()) {
217 profilename = "dv_pal_wide";
220 profilename = "dv_ntsc";
223 profilename = "dv_ntsc_wide";
226 profilename = "dv_pal";
230 Mlt::Profile profile(profilename.toUtf8().constData());
231 QPixmap pix(180, 135);
233 for (int i = 0; i < max; i++) {
234 QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
235 Mlt::Producer *producer = new Mlt::Producer(profile, item->text(0).toUtf8().data());
237 if (producer->is_blank() == false) {
238 //pix = KThumb::getFrame(producer, 0, 135 * profile.dar(), 135);
239 //item->setIcon(0, pix);
240 item->setText(1, Timecode::getStringTimecode(producer->get_playtime(), profile.fps()));
243 int submax = item->childCount();
244 for (int j = 0; j < submax; j++) {
245 QTreeWidgetItem *subitem = item->child(j);
246 subitem->setText(1, Timecode::getStringTimecode(subitem->data(1, Qt::UserRole).toInt(), profile.fps()));
252 void DvdWizardVob::slotDeleteVobFile()
254 QTreeWidgetItem *item = m_view.vobs_list->currentItem();
255 if (item == NULL) return;
263 bool DvdWizardVob::isComplete() const
265 if (!m_view.error_message->text().isEmpty()) return false;
266 if (m_view.vobs_list->topLevelItemCount() == 0) return false;
270 void DvdWizardVob::setUrl(const QString &url)
272 slotAddVobFile(KUrl(url));
275 QStringList DvdWizardVob::selectedUrls() const
279 int max = m_view.vobs_list->topLevelItemCount();
280 for (int i = 0; i < max; i++) {
281 QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
282 if (item) result.append(item->text(0));
288 QStringList DvdWizardVob::durations() const
292 int max = m_view.vobs_list->topLevelItemCount();
293 for (int i = 0; i < max; i++) {
294 QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
295 if (item) result.append(QString::number(item->data(1, Qt::UserRole).toInt()));
300 QStringList DvdWizardVob::chapters() const
304 int max = m_view.vobs_list->topLevelItemCount();
305 for (int i = 0; i < max; i++) {
306 QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
308 result.append(item->data(1, Qt::UserRole + 1).toString());
314 void DvdWizardVob::updateChapters(QMap <QString, QString> chaptersdata)
316 int max = m_view.vobs_list->topLevelItemCount();
317 for (int i = 0; i < max; i++) {
318 QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
319 if (chaptersdata.contains(item->text(0))) item->setData(1, Qt::UserRole + 1, chaptersdata.value(item->text(0)));
323 int DvdWizardVob::duration(int ix) const
326 QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(ix);
328 result = item->data(1, Qt::UserRole).toInt();
334 QString DvdWizardVob::introMovie() const
336 if (!m_view.use_intro->isChecked()) return QString();
337 return m_view.intro_vob->url().path();
340 void DvdWizardVob::setIntroMovie(const QString& path)
342 m_view.intro_vob->setUrl(KUrl(path));
343 m_view.use_intro->setChecked(path.isEmpty() == false);
347 void DvdWizardVob::slotCheckVobList()
349 emit completeChanged();
350 int max = m_view.vobs_list->topLevelItemCount();
351 QTreeWidgetItem *item = m_view.vobs_list->currentItem();
353 if (item == NULL) hasItem = false;
354 m_view.button_delete->setEnabled(hasItem);
355 if (hasItem && m_view.vobs_list->indexOfTopLevelItem(item) == 0) m_view.button_up->setEnabled(false);
356 else m_view.button_up->setEnabled(hasItem);
357 if (hasItem && m_view.vobs_list->indexOfTopLevelItem(item) == max - 1) m_view.button_down->setEnabled(false);
358 else m_view.button_down->setEnabled(hasItem);
360 qint64 totalSize = 0;
361 for (int i = 0; i < max; i++) {
362 item = m_view.vobs_list->topLevelItem(i);
363 if (item) totalSize += (qint64) item->data(0, Qt::UserRole).toInt();
366 qint64 maxSize = (qint64) 47000 * 100000;
367 m_capacityBar->setValue(100 * totalSize / maxSize);
368 m_capacityBar->setText(KIO::convertSize(totalSize));
371 void DvdWizardVob::slotItemUp()
373 QTreeWidgetItem *item = m_view.vobs_list->currentItem();
374 if (item == NULL) return;
375 int index = m_view.vobs_list->indexOfTopLevelItem(item);
376 if (index == 0) return;
377 m_view.vobs_list->insertTopLevelItem(index - 1, m_view.vobs_list->takeTopLevelItem(index));
380 void DvdWizardVob::slotItemDown()
382 int max = m_view.vobs_list->topLevelItemCount();
383 QTreeWidgetItem *item = m_view.vobs_list->currentItem();
384 if (item == NULL) return;
385 int index = m_view.vobs_list->indexOfTopLevelItem(item);
386 if (index == max - 1) return;
387 m_view.vobs_list->insertTopLevelItem(index + 1, m_view.vobs_list->takeTopLevelItem(index));
390 bool DvdWizardVob::isPal() const
392 return m_view.dvd_profile->currentIndex() < 2;
395 bool DvdWizardVob::isWide() const
397 return (m_view.dvd_profile->currentIndex() == 1 || m_view.dvd_profile->currentIndex() == 3);
400 void DvdWizardVob::setProfile(const QString& profile)
402 if (profile == "dv_pal") m_view.dvd_profile->setCurrentIndex(0);
403 else if (profile == "dv_pal_wide") m_view.dvd_profile->setCurrentIndex(1);
404 else if (profile == "dv_ntsc") m_view.dvd_profile->setCurrentIndex(2);
405 else if (profile == "dv_ntsc_wide") m_view.dvd_profile->setCurrentIndex(3);
408 void DvdWizardVob::clear()
410 m_view.vobs_list->clear();