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 switch (m_view.dvd_profile->currentIndex()) {
126 profilename = "dv_pal_wide";
129 profilename = "dv_ntsc";
132 profilename = "dv_ntsc_wide";
135 profilename = "dv_pal";
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());
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();
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);
159 int aspect = profile.dar() * 100;
160 if (profile.height() == 576) {
161 if (aspect > 150) standard = 1;
164 else if (profile.height() == 480) {
165 if (aspect > 150) standard = 3;
168 QString standardName;
171 standardName = i18n("NTSC 16:9");
174 standardName = i18n("NTSC 4:3");
177 standardName = i18n("PAL 16:9");
180 standardName = i18n("PAL 4:3");
183 standardName = i18n("Unknown");
185 item->setData(0, Qt::UserRole, standardName);
186 item->setData(0, Qt::UserRole + 1, standard);
189 if (producer) delete producer;
191 if (chapters.isEmpty() == false) {
192 item->setData(1, Qt::UserRole + 1, chapters);
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)) {
199 if (doc.setContent(&file) == false) {
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()));
209 item->setData(1, Qt::UserRole + 1, chaptersList.join(";"));
211 } else // Explicitly add a chapter at 00:00:00:00
212 item->setData(1, Qt::UserRole + 1, "0");
218 void DvdWizardVob::changeFormat()
220 int max = m_view.vobs_list->topLevelItemCount();
222 switch (m_view.dvd_profile->currentIndex()) {
224 profilename = "dv_pal_wide";
227 profilename = "dv_ntsc";
230 profilename = "dv_ntsc_wide";
233 profilename = "dv_pal";
237 Mlt::Profile profile(profilename.toUtf8().constData());
238 QPixmap pix(180, 135);
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());
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()));
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()));
259 void DvdWizardVob::slotDeleteVobFile()
261 QTreeWidgetItem *item = m_view.vobs_list->currentItem();
262 if (item == NULL) return;
270 bool DvdWizardVob::isComplete() const
272 if (!m_view.error_message->text().isEmpty()) return false;
273 if (m_view.vobs_list->topLevelItemCount() == 0) return false;
277 void DvdWizardVob::setUrl(const QString &url)
279 slotAddVobFile(KUrl(url));
282 QStringList DvdWizardVob::selectedUrls() const
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));
295 QStringList DvdWizardVob::durations() const
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()));
307 QStringList DvdWizardVob::chapters() const
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);
315 result.append(item->data(1, Qt::UserRole + 1).toString());
321 void DvdWizardVob::updateChapters(QMap <QString, QString> chaptersdata)
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)));
330 int DvdWizardVob::duration(int ix) const
333 QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(ix);
335 result = item->data(1, Qt::UserRole).toInt();
341 QString DvdWizardVob::introMovie() const
343 if (!m_view.use_intro->isChecked()) return QString();
344 return m_view.intro_vob->url().path();
347 void DvdWizardVob::setIntroMovie(const QString& path)
349 m_view.intro_vob->setUrl(KUrl(path));
350 m_view.use_intro->setChecked(path.isEmpty() == false);
354 void DvdWizardVob::slotCheckVobList()
356 emit completeChanged();
357 int max = m_view.vobs_list->topLevelItemCount();
358 QTreeWidgetItem *item = m_view.vobs_list->currentItem();
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);
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();
373 qint64 maxSize = (qint64) 47000 * 100000;
374 m_capacityBar->setValue(100 * totalSize / maxSize);
375 m_capacityBar->setText(KIO::convertSize(totalSize));
378 void DvdWizardVob::slotItemUp()
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));
387 void DvdWizardVob::slotItemDown()
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));
397 bool DvdWizardVob::isPal() const
399 return m_view.dvd_profile->currentIndex() < 2;
402 bool DvdWizardVob::isWide() const
404 return (m_view.dvd_profile->currentIndex() == 1 || m_view.dvd_profile->currentIndex() == 3);
407 void DvdWizardVob::setProfile(const QString& profile)
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);
415 void DvdWizardVob::clear()
417 m_view.vobs_list->clear();