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(QWidget *parent) :
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()));
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"));
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);
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);
75 m_view.vobs_list->setItemDelegate(new DvdViewDelegate(m_view.vobs_list));
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();
89 DvdWizardVob::~DvdWizardVob()
94 void DvdWizardVob::slotCheckProfiles()
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) {
108 m_warnMessage->animatedShow();
110 else m_warnMessage->animatedHide();
114 void DvdWizardVob::slotAddVobFile(KUrl url, const QString &chapters)
116 if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl("kfiledialog:///projectfolder"), "video/mpeg", this, i18n("Add new video file"));
117 if (url.isEmpty()) return;
119 qint64 fileSize = f.size();
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());
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();
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);
141 int aspect = profile.dar() * 100;
142 if (profile.height() == 576) {
143 if (aspect > 150) standard = 1;
146 else if (profile.height() == 480) {
147 if (aspect > 150) standard = 3;
150 QString standardName;
153 standardName = i18n("NTSC 16:9");
156 standardName = i18n("NTSC 4:3");
159 standardName = i18n("PAL 16:9");
162 standardName = i18n("PAL 4:3");
165 standardName = i18n("Unknown");
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
172 m_view.dvd_profile->blockSignals(true);
173 m_view.dvd_profile->setCurrentIndex(standard);
174 m_view.dvd_profile->blockSignals(false);
179 if (producer) delete producer;
181 if (chapters.isEmpty() == false) {
182 item->setData(1, Qt::UserRole + 1, chapters);
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)) {
189 if (doc.setContent(&file) == false) {
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()));
199 item->setData(1, Qt::UserRole + 1, chaptersList.join(";"));
201 } else // Explicitly add a chapter at 00:00:00:00
202 item->setData(1, Qt::UserRole + 1, "0");
208 void DvdWizardVob::changeFormat()
210 int max = m_view.vobs_list->topLevelItemCount();
212 switch (m_view.dvd_profile->currentIndex()) {
214 profilename = "dv_pal_wide";
217 profilename = "dv_ntsc";
220 profilename = "dv_ntsc_wide";
223 profilename = "dv_pal";
227 Mlt::Profile profile(profilename.toUtf8().constData());
228 QPixmap pix(180, 135);
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());
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()));
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()));
249 void DvdWizardVob::slotDeleteVobFile()
251 QTreeWidgetItem *item = m_view.vobs_list->currentItem();
252 if (item == NULL) return;
260 bool DvdWizardVob::isComplete() const
262 if (!m_view.error_message->text().isEmpty()) return false;
263 if (m_view.vobs_list->topLevelItemCount() == 0) return false;
267 void DvdWizardVob::setUrl(const QString &url)
269 slotAddVobFile(KUrl(url));
272 QStringList DvdWizardVob::selectedUrls() const
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));
285 QStringList DvdWizardVob::durations() const
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()));
297 QStringList DvdWizardVob::chapters() const
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);
305 result.append(item->data(1, Qt::UserRole + 1).toString());
311 void DvdWizardVob::updateChapters(QMap <QString, QString> chaptersdata)
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)));
320 int DvdWizardVob::duration(int ix) const
323 QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(ix);
325 result = item->data(1, Qt::UserRole).toInt();
331 QString DvdWizardVob::introMovie() const
333 if (!m_view.use_intro->isChecked()) return QString();
334 return m_view.intro_vob->url().path();
337 void DvdWizardVob::setIntroMovie(const QString& path)
339 m_view.intro_vob->setUrl(KUrl(path));
340 m_view.use_intro->setChecked(path.isEmpty() == false);
344 void DvdWizardVob::slotCheckVobList()
346 emit completeChanged();
347 int max = m_view.vobs_list->topLevelItemCount();
348 QTreeWidgetItem *item = m_view.vobs_list->currentItem();
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);
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();
363 qint64 maxSize = (qint64) 47000 * 100000;
364 m_capacityBar->setValue(100 * totalSize / maxSize);
365 m_capacityBar->setText(KIO::convertSize(totalSize));
368 void DvdWizardVob::slotItemUp()
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));
377 void DvdWizardVob::slotItemDown()
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));
387 DVDFORMAT DvdWizardVob::dvdFormat() const
389 return (DVDFORMAT) m_view.dvd_profile->currentIndex();
392 const QString DvdWizardVob::dvdProfile() const
395 switch (m_view.dvd_profile->currentIndex()) {
397 profile = "dv_pal_wide";
403 profile = "dv_ntsc_wide";
412 QString DvdWizardVob::getDvdProfile(DVDFORMAT format)
417 profile = "dv_pal_wide";
423 profile = "dv_ntsc_wide";
431 void DvdWizardVob::setProfile(const QString& profile)
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);
439 void DvdWizardVob::clear()
441 m_view.vobs_list->clear();