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()) m_errorMessage.append(i18n("<strong>Program %1 is required for the DVD wizard.</strong>", i18n("mkisofs")));
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 #if KDE_IS_VERSION(4,2,0)
72 m_capacityBar = new KCapacityBar(KCapacityBar::DrawTextInline, this);
73 QHBoxLayout *layout = new QHBoxLayout;
74 layout->addWidget(m_capacityBar);
75 m_view.size_box->setLayout(layout);
77 m_view.size_box->setHidden(true);
82 DvdWizardVob::~DvdWizardVob()
84 #if KDE_IS_VERSION(4,2,0)
90 void DvdWizardVob::slotAddVobFile(KUrl url, const QString &chapters)
92 if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl("kfiledialog:///projectfolder"), "video/mpeg", this, i18n("Add new video file"));
93 if (url.isEmpty()) return;
95 qint64 fileSize = f.size();
97 switch (m_view.dvd_profile->currentIndex()) {
99 profilename = "dv_pal_wide";
102 profilename = "dv_ntsc";
105 profilename = "dv_ntsc_wide";
108 profilename = "dv_pal";
112 char *tmp = (char*) qstrdup(profilename.toUtf8().data());
113 Mlt::Profile profile(tmp);
115 QTreeWidgetItem *item = new QTreeWidgetItem(m_view.vobs_list, QStringList() << url.path() << QString() << KIO::convertSize(fileSize));
116 item->setData(0, Qt::UserRole, fileSize);
117 item->setIcon(0, KIcon("video-x-generic"));
121 tmp = (char *) qstrdup(url.path().toUtf8().data());
122 Mlt::Producer *producer = new Mlt::Producer(profile, tmp);
125 if (producer->is_blank() == false) {
126 int width = 45.0 * profile.dar();
127 if (width % 2 == 1) width++;
128 item->setIcon(0, QPixmap::fromImage(KThumb::getFrame(producer, 0, width, 45)));
129 int playTime = producer->get_playtime();
130 item->setText(1, Timecode::getStringTimecode(playTime, profile.fps()));
131 item->setData(1, Qt::UserRole, playTime);
135 if (chapters.isEmpty() == false)
136 item->setData(1, Qt::UserRole + 1, chapters);
137 else if (QFile::exists(url.path() + ".dvdchapter")) {
138 // insert chapters as children
139 QFile file(url.path() + ".dvdchapter");
140 if (file.open(QIODevice::ReadOnly)) {
142 doc.setContent(&file);
144 QDomNodeList chapters = doc.elementsByTagName("chapter");
145 QStringList chaptersList;
146 for (int j = 0; j < chapters.count(); j++) {
147 chaptersList.append(QString::number(chapters.at(j).toElement().attribute("time").toInt()));
149 item->setData(1, Qt::UserRole + 1, chaptersList.join(";"));
151 } else // Explicitly add a chapter at 00:00:00:00
152 item->setData(1, Qt::UserRole + 1, "0");
157 void DvdWizardVob::changeFormat()
159 int max = m_view.vobs_list->topLevelItemCount();
161 switch (m_view.dvd_profile->currentIndex()) {
163 profilename = "dv_pal_wide";
166 profilename = "dv_ntsc";
169 profilename = "dv_ntsc_wide";
172 profilename = "dv_pal";
176 char *tmp = (char*) qstrdup(profilename.toUtf8().data());
177 Mlt::Profile profile(tmp);
179 QPixmap pix(180, 135);
181 for (int i = 0; i < max; i++) {
182 QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
183 tmp = (char *) qstrdup(item->text(0).toUtf8().data());
184 Mlt::Producer *producer = new Mlt::Producer(profile, tmp);
187 if (producer->is_blank() == false) {
188 //pix = KThumb::getFrame(producer, 0, 135 * profile.dar(), 135);
189 //item->setIcon(0, pix);
190 item->setText(1, Timecode::getStringTimecode(producer->get_playtime(), profile.fps()));
193 int submax = item->childCount();
194 for (int j = 0; j < submax; j++) {
195 QTreeWidgetItem *subitem = item->child(j);
196 subitem->setText(1, Timecode::getStringTimecode(subitem->data(1, Qt::UserRole).toInt(), profile.fps()));
202 void DvdWizardVob::slotDeleteVobFile()
204 QTreeWidgetItem *item = m_view.vobs_list->currentItem();
205 if (item == NULL) return;
212 bool DvdWizardVob::isComplete() const
214 if (!m_view.error_message->text().isEmpty()) return false;
215 if (m_view.vobs_list->topLevelItemCount() == 0) return false;
219 void DvdWizardVob::setUrl(const QString &url)
221 slotAddVobFile(KUrl(url));
224 QStringList DvdWizardVob::selectedUrls() const
228 int max = m_view.vobs_list->topLevelItemCount();
229 for (int i = 0; i < max; i++) {
230 QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
231 if (item) result.append(item->text(0));
237 QStringList DvdWizardVob::durations() 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);
244 if (item) result.append(QString::number(item->data(1, Qt::UserRole).toInt()));
249 QStringList DvdWizardVob::chapters() const
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 (item) result.append(item->data(1, Qt::UserRole + 1).toString());
261 void DvdWizardVob::updateChapters(QMap <QString, QString> chaptersdata)
263 int max = m_view.vobs_list->topLevelItemCount();
264 for (int i = 0; i < max; i++) {
265 QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
266 item->setData(1, Qt::UserRole + 1, chaptersdata.value(item->text(0)));
270 int DvdWizardVob::duration(int ix) const
273 QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(ix);
275 result = item->data(1, Qt::UserRole).toInt();
281 QString DvdWizardVob::introMovie() const
283 if (!m_view.use_intro->isChecked()) return QString();
284 return m_view.intro_vob->url().path();
287 void DvdWizardVob::setIntroMovie(const QString path)
289 m_view.intro_vob->setUrl(KUrl(path));
290 m_view.use_intro->setChecked(path.isEmpty() == false);
294 void DvdWizardVob::slotCheckVobList()
296 emit completeChanged();
297 int max = m_view.vobs_list->topLevelItemCount();
298 QTreeWidgetItem *item = m_view.vobs_list->currentItem();
300 if (item == NULL) hasItem = false;
301 m_view.button_delete->setEnabled(hasItem);
302 if (hasItem && m_view.vobs_list->indexOfTopLevelItem(item) == 0) m_view.button_up->setEnabled(false);
303 else m_view.button_up->setEnabled(hasItem);
304 if (hasItem && m_view.vobs_list->indexOfTopLevelItem(item) == max - 1) m_view.button_down->setEnabled(false);
305 else m_view.button_down->setEnabled(hasItem);
307 #if KDE_IS_VERSION(4,2,0)
308 qint64 totalSize = 0;
309 for (int i = 0; i < max; i++) {
310 item = m_view.vobs_list->topLevelItem(i);
311 if (item) totalSize += (qint64) item->data(0, Qt::UserRole).toInt();
314 qint64 maxSize = (qint64) 47000 * 100000;
315 m_capacityBar->setValue(100 * totalSize / maxSize);
316 m_capacityBar->setText(KIO::convertSize(totalSize));
320 void DvdWizardVob::slotItemUp()
322 QTreeWidgetItem *item = m_view.vobs_list->currentItem();
323 if (item == NULL) return;
324 int index = m_view.vobs_list->indexOfTopLevelItem(item);
325 if (index == 0) return;
326 m_view.vobs_list->insertTopLevelItem(index - 1, m_view.vobs_list->takeTopLevelItem(index));
329 void DvdWizardVob::slotItemDown()
331 int max = m_view.vobs_list->topLevelItemCount();
332 QTreeWidgetItem *item = m_view.vobs_list->currentItem();
333 if (item == NULL) return;
334 int index = m_view.vobs_list->indexOfTopLevelItem(item);
335 if (index == max - 1) return;
336 m_view.vobs_list->insertTopLevelItem(index + 1, m_view.vobs_list->takeTopLevelItem(index));
339 bool DvdWizardVob::isPal() const
341 return m_view.dvd_profile->currentIndex() < 2;
344 bool DvdWizardVob::isWide() const
346 return (m_view.dvd_profile->currentIndex() == 1 || m_view.dvd_profile->currentIndex() == 3);
349 void DvdWizardVob::setProfile(const QString profile)
351 if (profile == "dv_pal") m_view.dvd_profile->setCurrentIndex(0);
352 else if (profile == "dv_pal_wide") m_view.dvd_profile->setCurrentIndex(1);
353 else if (profile == "dv_ntsc") m_view.dvd_profile->setCurrentIndex(2);
354 else if (profile == "dv_ntsc_wide") m_view.dvd_profile->setCurrentIndex(3);
357 void DvdWizardVob::clear()
359 m_view.vobs_list->clear();