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()));
53 m_view.vobs_list->setIconSize(QSize(60, 45));
55 if (KStandardDirs::findExe("dvdauthor").isEmpty()) m_errorMessage.append(i18n("<strong>Program %1 is required for the DVD wizard.</strong>", i18n("dvdauthor")));
56 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")));
57 if (m_errorMessage.isEmpty()) m_view.error_message->setVisible(false);
58 else m_view.error_message->setText(m_errorMessage);
60 m_view.dvd_profile->addItems(QStringList() << i18n("PAL 4:3") << i18n("PAL 16:9") << i18n("NTSC 4:3") << i18n("NTSC 16:9"));
61 if (profile == "dv_pal_wide") m_view.dvd_profile->setCurrentIndex(1);
62 else if (profile == "dv_ntsc") m_view.dvd_profile->setCurrentIndex(2);
63 else if (profile == "dv_ntsc_wide") m_view.dvd_profile->setCurrentIndex(3);
65 connect(m_view.dvd_profile, SIGNAL(activated(int)), this, SLOT(changeFormat()));
66 m_view.vobs_list->header()->setStretchLastSection(false);
67 m_view.vobs_list->header()->setResizeMode(0, QHeaderView::Stretch);
68 m_view.vobs_list->header()->setResizeMode(1, QHeaderView::Custom);
69 m_view.vobs_list->header()->setResizeMode(2, QHeaderView::Custom);
71 m_capacityBar = new KCapacityBar(KCapacityBar::DrawTextInline, this);
72 QHBoxLayout *layout = new QHBoxLayout;
73 layout->addWidget(m_capacityBar);
74 m_view.size_box->setLayout(layout);
78 DvdWizardVob::~DvdWizardVob()
84 void DvdWizardVob::slotAddVobFile(KUrl url, const QString &chapters)
86 if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl("kfiledialog:///projectfolder"), "video/mpeg", this, i18n("Add new video file"));
87 if (url.isEmpty()) return;
89 qint64 fileSize = f.size();
91 switch (m_view.dvd_profile->currentIndex()) {
93 profilename = "dv_pal_wide";
96 profilename = "dv_ntsc";
99 profilename = "dv_ntsc_wide";
102 profilename = "dv_pal";
106 Mlt::Profile profile(profilename.toUtf8().data());
107 QTreeWidgetItem *item = new QTreeWidgetItem(m_view.vobs_list, QStringList() << url.path() << QString() << KIO::convertSize(fileSize));
108 item->setData(0, Qt::UserRole, fileSize);
109 item->setIcon(0, KIcon("video-x-generic"));
113 Mlt::Producer *producer = new Mlt::Producer(profile, url.path().toUtf8().data());
115 if (producer->is_blank() == false) {
116 int width = 45.0 * profile.dar();
117 int swidth = 45.0 * profile.width() / profile.height();
118 if (width % 2 == 1) width++;
119 item->setIcon(0, QPixmap::fromImage(KThumb::getFrame(producer, 0, swidth, width, 45)));
120 int playTime = producer->get_playtime();
121 item->setText(1, Timecode::getStringTimecode(playTime, profile.fps()));
122 item->setData(1, Qt::UserRole, playTime);
126 if (chapters.isEmpty() == false) {
127 item->setData(1, Qt::UserRole + 1, chapters);
129 else if (QFile::exists(url.path() + ".dvdchapter")) {
130 // insert chapters as children
131 QFile file(url.path() + ".dvdchapter");
132 if (file.open(QIODevice::ReadOnly)) {
134 doc.setContent(&file);
136 QDomNodeList chapters = doc.elementsByTagName("chapter");
137 QStringList chaptersList;
138 for (int j = 0; j < chapters.count(); j++) {
139 chaptersList.append(QString::number(chapters.at(j).toElement().attribute("time").toInt()));
141 item->setData(1, Qt::UserRole + 1, chaptersList.join(";"));
143 } else // Explicitly add a chapter at 00:00:00:00
144 item->setData(1, Qt::UserRole + 1, "0");
149 void DvdWizardVob::changeFormat()
151 int max = m_view.vobs_list->topLevelItemCount();
153 switch (m_view.dvd_profile->currentIndex()) {
155 profilename = "dv_pal_wide";
158 profilename = "dv_ntsc";
161 profilename = "dv_ntsc_wide";
164 profilename = "dv_pal";
168 Mlt::Profile profile(profilename.toUtf8().data());
169 QPixmap pix(180, 135);
171 for (int i = 0; i < max; i++) {
172 QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
173 Mlt::Producer *producer = new Mlt::Producer(profile, item->text(0).toUtf8().data());
175 if (producer->is_blank() == false) {
176 //pix = KThumb::getFrame(producer, 0, 135 * profile.dar(), 135);
177 //item->setIcon(0, pix);
178 item->setText(1, Timecode::getStringTimecode(producer->get_playtime(), profile.fps()));
181 int submax = item->childCount();
182 for (int j = 0; j < submax; j++) {
183 QTreeWidgetItem *subitem = item->child(j);
184 subitem->setText(1, Timecode::getStringTimecode(subitem->data(1, Qt::UserRole).toInt(), profile.fps()));
190 void DvdWizardVob::slotDeleteVobFile()
192 QTreeWidgetItem *item = m_view.vobs_list->currentItem();
193 if (item == NULL) return;
200 bool DvdWizardVob::isComplete() const
202 if (!m_view.error_message->text().isEmpty()) return false;
203 if (m_view.vobs_list->topLevelItemCount() == 0) return false;
207 void DvdWizardVob::setUrl(const QString &url)
209 slotAddVobFile(KUrl(url));
212 QStringList DvdWizardVob::selectedUrls() const
216 int max = m_view.vobs_list->topLevelItemCount();
217 for (int i = 0; i < max; i++) {
218 QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
219 if (item) result.append(item->text(0));
225 QStringList DvdWizardVob::durations() const
229 int max = m_view.vobs_list->topLevelItemCount();
230 for (int i = 0; i < max; i++) {
231 QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
232 if (item) result.append(QString::number(item->data(1, Qt::UserRole).toInt()));
237 QStringList DvdWizardVob::chapters() const
241 int max = m_view.vobs_list->topLevelItemCount();
242 for (int i = 0; i < max; i++) {
243 QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
245 result.append(item->data(1, Qt::UserRole + 1).toString());
251 void DvdWizardVob::updateChapters(QMap <QString, QString> chaptersdata)
253 int max = m_view.vobs_list->topLevelItemCount();
254 for (int i = 0; i < max; i++) {
255 QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
256 if (chaptersdata.contains(item->text(0))) item->setData(1, Qt::UserRole + 1, chaptersdata.value(item->text(0)));
260 int DvdWizardVob::duration(int ix) const
263 QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(ix);
265 result = item->data(1, Qt::UserRole).toInt();
271 QString DvdWizardVob::introMovie() const
273 if (!m_view.use_intro->isChecked()) return QString();
274 return m_view.intro_vob->url().path();
277 void DvdWizardVob::setIntroMovie(const QString& path)
279 m_view.intro_vob->setUrl(KUrl(path));
280 m_view.use_intro->setChecked(path.isEmpty() == false);
284 void DvdWizardVob::slotCheckVobList()
286 emit completeChanged();
287 int max = m_view.vobs_list->topLevelItemCount();
288 QTreeWidgetItem *item = m_view.vobs_list->currentItem();
290 if (item == NULL) hasItem = false;
291 m_view.button_delete->setEnabled(hasItem);
292 if (hasItem && m_view.vobs_list->indexOfTopLevelItem(item) == 0) m_view.button_up->setEnabled(false);
293 else m_view.button_up->setEnabled(hasItem);
294 if (hasItem && m_view.vobs_list->indexOfTopLevelItem(item) == max - 1) m_view.button_down->setEnabled(false);
295 else m_view.button_down->setEnabled(hasItem);
297 qint64 totalSize = 0;
298 for (int i = 0; i < max; i++) {
299 item = m_view.vobs_list->topLevelItem(i);
300 if (item) totalSize += (qint64) item->data(0, Qt::UserRole).toInt();
303 qint64 maxSize = (qint64) 47000 * 100000;
304 m_capacityBar->setValue(100 * totalSize / maxSize);
305 m_capacityBar->setText(KIO::convertSize(totalSize));
308 void DvdWizardVob::slotItemUp()
310 QTreeWidgetItem *item = m_view.vobs_list->currentItem();
311 if (item == NULL) return;
312 int index = m_view.vobs_list->indexOfTopLevelItem(item);
313 if (index == 0) return;
314 m_view.vobs_list->insertTopLevelItem(index - 1, m_view.vobs_list->takeTopLevelItem(index));
317 void DvdWizardVob::slotItemDown()
319 int max = m_view.vobs_list->topLevelItemCount();
320 QTreeWidgetItem *item = m_view.vobs_list->currentItem();
321 if (item == NULL) return;
322 int index = m_view.vobs_list->indexOfTopLevelItem(item);
323 if (index == max - 1) return;
324 m_view.vobs_list->insertTopLevelItem(index + 1, m_view.vobs_list->takeTopLevelItem(index));
327 bool DvdWizardVob::isPal() const
329 return m_view.dvd_profile->currentIndex() < 2;
332 bool DvdWizardVob::isWide() const
334 return (m_view.dvd_profile->currentIndex() == 1 || m_view.dvd_profile->currentIndex() == 3);
337 void DvdWizardVob::setProfile(const QString& profile)
339 if (profile == "dv_pal") m_view.dvd_profile->setCurrentIndex(0);
340 else if (profile == "dv_pal_wide") m_view.dvd_profile->setCurrentIndex(1);
341 else if (profile == "dv_ntsc") m_view.dvd_profile->setCurrentIndex(2);
342 else if (profile == "dv_ntsc_wide") m_view.dvd_profile->setCurrentIndex(3);
345 void DvdWizardVob::clear()
347 m_view.vobs_list->clear();