]> git.sesse.net Git - kdenlive/blob - src/dvdwizardvob.cpp
* Fix aspect ratio of thumbnails to correctly use the project's arpect ratio
[kdenlive] / src / dvdwizardvob.cpp
1 /***************************************************************************
2  *   Copyright (C) 2009 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20 #include "dvdwizardvob.h"
21 #include "kthumb.h"
22 #include "timecode.h"
23
24 #include <mlt++/Mlt.h>
25
26 #include <KUrlRequester>
27 #include <KDebug>
28 #include <KStandardDirs>
29 #include <KFileItem>
30 #include <KFileDialog>
31
32 #include <QHBoxLayout>
33 #include <QDomDocument>
34 #include <QTreeWidgetItem>
35 #include <QHeaderView>
36
37 DvdWizardVob::DvdWizardVob(const QString &profile, QWidget *parent) :
38         QWizardPage(parent)
39 {
40     m_view.setupUi(this);
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));
54
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);
59
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);
64
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);
70
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);
76 #else
77     m_view.size_box->setHidden(true);
78 #endif
79     slotCheckVobList();
80 }
81
82 DvdWizardVob::~DvdWizardVob()
83 {
84 #if KDE_IS_VERSION(4,2,0)
85     delete m_capacityBar;
86 #endif
87 }
88
89
90 void DvdWizardVob::slotAddVobFile(KUrl url, const QString &chapters)
91 {
92     if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl("kfiledialog:///projectfolder"), "video/mpeg", this, i18n("Add new video file"));
93     if (url.isEmpty()) return;
94     QFile f(url.path());
95     qint64 fileSize = f.size();
96     QString profilename;
97     switch (m_view.dvd_profile->currentIndex()) {
98     case 1:
99         profilename = "dv_pal_wide";
100         break;
101     case 2:
102         profilename = "dv_ntsc";
103         break;
104     case 3:
105         profilename = "dv_ntsc_wide";
106         break;
107     default:
108         profilename = "dv_pal";
109         break;
110     }
111
112     Mlt::Profile profile(profilename.toUtf8().data());
113     QTreeWidgetItem *item = new QTreeWidgetItem(m_view.vobs_list, QStringList() << url.path() << QString() << KIO::convertSize(fileSize));
114     item->setData(0, Qt::UserRole, fileSize);
115     item->setIcon(0, KIcon("video-x-generic"));
116
117     QPixmap pix(60, 45);
118
119     Mlt::Producer *producer = new Mlt::Producer(profile, url.path().toUtf8().data());
120
121     if (producer->is_blank() == false) {
122         int width = 45.0 * profile.dar();
123         int swidth = 45.0 * profile.width() / profile.height();
124         if (width % 2 == 1) width++;
125         item->setIcon(0, QPixmap::fromImage(KThumb::getFrame(producer, 0, swidth, width, 45)));
126         int playTime = producer->get_playtime();
127         item->setText(1, Timecode::getStringTimecode(playTime, profile.fps()));
128         item->setData(1, Qt::UserRole, playTime);
129     }
130     delete producer;
131
132     if (chapters.isEmpty() == false)
133         item->setData(1, Qt::UserRole + 1, chapters);
134     else if (QFile::exists(url.path() + ".dvdchapter")) {
135         // insert chapters as children
136         QFile file(url.path() + ".dvdchapter");
137         if (file.open(QIODevice::ReadOnly)) {
138             QDomDocument doc;
139             doc.setContent(&file);
140             file.close();
141             QDomNodeList chapters = doc.elementsByTagName("chapter");
142             QStringList chaptersList;
143             for (int j = 0; j < chapters.count(); j++) {
144                 chaptersList.append(QString::number(chapters.at(j).toElement().attribute("time").toInt()));
145             }
146             item->setData(1, Qt::UserRole + 1, chaptersList.join(";"));
147         }
148     } else // Explicitly add a chapter at 00:00:00:00
149         item->setData(1, Qt::UserRole + 1, "0");
150
151     slotCheckVobList();
152 }
153
154 void DvdWizardVob::changeFormat()
155 {
156     int max = m_view.vobs_list->topLevelItemCount();
157     QString profilename;
158     switch (m_view.dvd_profile->currentIndex()) {
159     case 1:
160         profilename = "dv_pal_wide";
161         break;
162     case 2:
163         profilename = "dv_ntsc";
164         break;
165     case 3:
166         profilename = "dv_ntsc_wide";
167         break;
168     default:
169         profilename = "dv_pal";
170         break;
171     }
172
173     Mlt::Profile profile(profilename.toUtf8().data());
174     QPixmap pix(180, 135);
175
176     for (int i = 0; i < max; i++) {
177         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
178         Mlt::Producer *producer = new Mlt::Producer(profile, item->text(0).toUtf8().data());
179
180         if (producer->is_blank() == false) {
181             //pix = KThumb::getFrame(producer, 0, 135 * profile.dar(), 135);
182             //item->setIcon(0, pix);
183             item->setText(1, Timecode::getStringTimecode(producer->get_playtime(), profile.fps()));
184         }
185         delete producer;
186         int submax = item->childCount();
187         for (int j = 0; j < submax; j++) {
188             QTreeWidgetItem *subitem = item->child(j);
189             subitem->setText(1, Timecode::getStringTimecode(subitem->data(1, Qt::UserRole).toInt(), profile.fps()));
190         }
191     }
192     slotCheckVobList();
193 }
194
195 void DvdWizardVob::slotDeleteVobFile()
196 {
197     QTreeWidgetItem *item = m_view.vobs_list->currentItem();
198     if (item == NULL) return;
199     delete item;
200     slotCheckVobList();
201 }
202
203
204 // virtual
205 bool DvdWizardVob::isComplete() const
206 {
207     if (!m_view.error_message->text().isEmpty()) return false;
208     if (m_view.vobs_list->topLevelItemCount() == 0) return false;
209     return true;
210 }
211
212 void DvdWizardVob::setUrl(const QString &url)
213 {
214     slotAddVobFile(KUrl(url));
215 }
216
217 QStringList DvdWizardVob::selectedUrls() const
218 {
219     QStringList result;
220     QString path;
221     int max = m_view.vobs_list->topLevelItemCount();
222     for (int i = 0; i < max; i++) {
223         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
224         if (item) result.append(item->text(0));
225     }
226     return result;
227 }
228
229
230 QStringList DvdWizardVob::durations() const
231 {
232     QStringList result;
233     QString path;
234     int max = m_view.vobs_list->topLevelItemCount();
235     for (int i = 0; i < max; i++) {
236         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
237         if (item) result.append(QString::number(item->data(1, Qt::UserRole).toInt()));
238     }
239     return result;
240 }
241
242 QStringList DvdWizardVob::chapters() const
243 {
244     QStringList result;
245     QString path;
246     int max = m_view.vobs_list->topLevelItemCount();
247     for (int i = 0; i < max; i++) {
248         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
249         if (item) result.append(item->data(1, Qt::UserRole + 1).toString());
250     }
251     return result;
252 }
253
254 void DvdWizardVob::updateChapters(QMap <QString, QString> chaptersdata)
255 {
256     int max = m_view.vobs_list->topLevelItemCount();
257     for (int i = 0; i < max; i++) {
258         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
259         item->setData(1, Qt::UserRole + 1, chaptersdata.value(item->text(0)));
260     }
261 }
262
263 int DvdWizardVob::duration(int ix) const
264 {
265     int result = -1;
266     QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(ix);
267     if (item) {
268         result = item->data(1, Qt::UserRole).toInt();
269     }
270     return result;
271 }
272
273
274 QString DvdWizardVob::introMovie() const
275 {
276     if (!m_view.use_intro->isChecked()) return QString();
277     return m_view.intro_vob->url().path();
278 }
279
280 void DvdWizardVob::setIntroMovie(const QString& path)
281 {
282     m_view.intro_vob->setUrl(KUrl(path));
283     m_view.use_intro->setChecked(path.isEmpty() == false);
284 }
285
286
287 void DvdWizardVob::slotCheckVobList()
288 {
289     emit completeChanged();
290     int max = m_view.vobs_list->topLevelItemCount();
291     QTreeWidgetItem *item = m_view.vobs_list->currentItem();
292     bool hasItem = true;
293     if (item == NULL) hasItem = false;
294     m_view.button_delete->setEnabled(hasItem);
295     if (hasItem && m_view.vobs_list->indexOfTopLevelItem(item) == 0) m_view.button_up->setEnabled(false);
296     else m_view.button_up->setEnabled(hasItem);
297     if (hasItem && m_view.vobs_list->indexOfTopLevelItem(item) == max - 1) m_view.button_down->setEnabled(false);
298     else m_view.button_down->setEnabled(hasItem);
299
300 #if KDE_IS_VERSION(4,2,0)
301     qint64 totalSize = 0;
302     for (int i = 0; i < max; i++) {
303         item = m_view.vobs_list->topLevelItem(i);
304         if (item) totalSize += (qint64) item->data(0, Qt::UserRole).toInt();
305     }
306
307     qint64 maxSize = (qint64) 47000 * 100000;
308     m_capacityBar->setValue(100 * totalSize / maxSize);
309     m_capacityBar->setText(KIO::convertSize(totalSize));
310 #endif
311 }
312
313 void DvdWizardVob::slotItemUp()
314 {
315     QTreeWidgetItem *item = m_view.vobs_list->currentItem();
316     if (item == NULL) return;
317     int index = m_view.vobs_list->indexOfTopLevelItem(item);
318     if (index == 0) return;
319     m_view.vobs_list->insertTopLevelItem(index - 1, m_view.vobs_list->takeTopLevelItem(index));
320 }
321
322 void DvdWizardVob::slotItemDown()
323 {
324     int max = m_view.vobs_list->topLevelItemCount();
325     QTreeWidgetItem *item = m_view.vobs_list->currentItem();
326     if (item == NULL) return;
327     int index = m_view.vobs_list->indexOfTopLevelItem(item);
328     if (index == max - 1) return;
329     m_view.vobs_list->insertTopLevelItem(index + 1, m_view.vobs_list->takeTopLevelItem(index));
330 }
331
332 bool DvdWizardVob::isPal() const
333 {
334     return m_view.dvd_profile->currentIndex() < 2;
335 }
336
337 bool DvdWizardVob::isWide() const
338 {
339     return (m_view.dvd_profile->currentIndex() == 1 || m_view.dvd_profile->currentIndex() == 3);
340 }
341
342 void DvdWizardVob::setProfile(const QString& profile)
343 {
344     if (profile == "dv_pal") m_view.dvd_profile->setCurrentIndex(0);
345     else if (profile == "dv_pal_wide") m_view.dvd_profile->setCurrentIndex(1);
346     else if (profile == "dv_ntsc") m_view.dvd_profile->setCurrentIndex(2);
347     else if (profile == "dv_ntsc_wide") m_view.dvd_profile->setCurrentIndex(3);
348 }
349
350 void DvdWizardVob::clear()
351 {
352     m_view.vobs_list->clear();
353 }