]> git.sesse.net Git - kdenlive/blob - src/dvdwizardvob.cpp
b16d45c190aa2097e1f07754b8210643f76c8224
[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     
54     m_view.vobs_list->setIconSize(QSize(60, 45));
55
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);
60
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);
65
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);
72
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);
77
78     m_view.vobs_list->setItemDelegate(new DvdViewDelegate(m_view.vobs_list));
79
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();
87 #endif
88     
89     slotCheckVobList();
90 }
91
92 DvdWizardVob::~DvdWizardVob()
93 {
94     delete m_capacityBar;
95 }
96
97 void DvdWizardVob::slotCheckProfiles()
98 {
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) {
105             conflict = true;
106             break;
107         }
108     }
109
110     if (conflict) {
111         m_warnMessage->animatedShow();
112     }
113     else m_warnMessage->animatedHide();
114 #endif
115 }
116
117 void DvdWizardVob::slotAddVobFile(KUrl url, const QString &chapters)
118 {
119     if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl("kfiledialog:///projectfolder"), "video/mpeg", this, i18n("Add new video file"));
120     if (url.isEmpty()) return;
121     QFile f(url.path());
122     qint64 fileSize = f.size();
123
124     Mlt::Profile profile;
125     profile.set_explicit(false);
126     QTreeWidgetItem *item = new QTreeWidgetItem(m_view.vobs_list, QStringList() << url.path() << QString() << KIO::convertSize(fileSize));
127     item->setData(0, Qt::UserRole, fileSize);
128     item->setData(0, Qt::DecorationRole, KIcon("video-x-generic").pixmap(60, 45));
129     item->setToolTip(0, url.path());
130     
131     Mlt::Producer *producer = new Mlt::Producer(profile, url.path().toUtf8().data());
132     if (producer && producer->is_valid() && !producer->is_blank()) {
133         //Mlt::Frame *frame = producer->get_frame();
134         //delete frame;
135         profile.from_producer(*producer);
136         int width = 45.0 * profile.dar();
137         int swidth = 45.0 * profile.width() / profile.height();
138         if (width % 2 == 1) width++;
139         item->setData(0, Qt::DecorationRole, QPixmap::fromImage(KThumb::getFrame(producer, 0, swidth, width, 45)));
140         int playTime = producer->get_playtime();
141         item->setText(1, Timecode::getStringTimecode(playTime, profile.fps()));
142         item->setData(1, Qt::UserRole, playTime);
143         int standard = -1;
144         int aspect = profile.dar() * 100;
145         if (profile.height() == 576) {
146             if (aspect > 150) standard = 1;
147             else standard = 0;
148         }
149         else if (profile.height() == 480) {
150             if (aspect > 150) standard = 3;
151             else standard = 2;
152         }
153         QString standardName;
154         switch (standard) {
155           case 3:
156               standardName = i18n("NTSC 16:9");
157               break;
158           case 2:
159               standardName = i18n("NTSC 4:3");
160               break;
161           case 1:
162               standardName = i18n("PAL 16:9");
163               break;
164           case 0:
165               standardName = i18n("PAL 4:3");
166               break;
167           default:
168               standardName = i18n("Unknown");
169         }
170         item->setData(0, Qt::UserRole, standardName);
171         item->setData(0, Qt::UserRole + 1, standard);
172         if (m_view.vobs_list->topLevelItemCount() == 1) {
173             // This is the first added movie, auto select DVD format
174             if (standard >= 0) {
175                 m_view.dvd_profile->blockSignals(true);
176                 m_view.dvd_profile->setCurrentIndex(standard);
177                 m_view.dvd_profile->blockSignals(false);
178             }
179         }
180         
181     }
182     if (producer) delete producer;
183
184     if (chapters.isEmpty() == false) {
185         item->setData(1, Qt::UserRole + 1, chapters);
186     }
187     else if (QFile::exists(url.path() + ".dvdchapter")) {
188         // insert chapters as children
189         QFile file(url.path() + ".dvdchapter");
190         if (file.open(QIODevice::ReadOnly)) {
191             QDomDocument doc;
192             if (doc.setContent(&file) == false) {
193                 file.close();
194                 return;
195             }
196             file.close();
197             QDomNodeList chapters = doc.elementsByTagName("chapter");
198             QStringList chaptersList;
199             for (int j = 0; j < chapters.count(); j++) {
200                 chaptersList.append(QString::number(chapters.at(j).toElement().attribute("time").toInt()));
201             }
202             item->setData(1, Qt::UserRole + 1, chaptersList.join(";"));
203         }
204     } else // Explicitly add a chapter at 00:00:00:00
205         item->setData(1, Qt::UserRole + 1, "0");
206
207     slotCheckVobList();
208     slotCheckProfiles();
209 }
210
211 void DvdWizardVob::changeFormat()
212 {
213     int max = m_view.vobs_list->topLevelItemCount();
214     QString profilename;
215     switch (m_view.dvd_profile->currentIndex()) {
216     case 1:
217         profilename = "dv_pal_wide";
218         break;
219     case 2:
220         profilename = "dv_ntsc";
221         break;
222     case 3:
223         profilename = "dv_ntsc_wide";
224         break;
225     default:
226         profilename = "dv_pal";
227         break;
228     }
229
230     Mlt::Profile profile(profilename.toUtf8().constData());
231     QPixmap pix(180, 135);
232
233     for (int i = 0; i < max; i++) {
234         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
235         Mlt::Producer *producer = new Mlt::Producer(profile, item->text(0).toUtf8().data());
236
237         if (producer->is_blank() == false) {
238             //pix = KThumb::getFrame(producer, 0, 135 * profile.dar(), 135);
239             //item->setIcon(0, pix);
240             item->setText(1, Timecode::getStringTimecode(producer->get_playtime(), profile.fps()));
241         }
242         delete producer;
243         int submax = item->childCount();
244         for (int j = 0; j < submax; j++) {
245             QTreeWidgetItem *subitem = item->child(j);
246             subitem->setText(1, Timecode::getStringTimecode(subitem->data(1, Qt::UserRole).toInt(), profile.fps()));
247         }
248     }
249     slotCheckVobList();
250 }
251
252 void DvdWizardVob::slotDeleteVobFile()
253 {
254     QTreeWidgetItem *item = m_view.vobs_list->currentItem();
255     if (item == NULL) return;
256     delete item;
257     slotCheckVobList();
258     slotCheckProfiles();
259 }
260
261
262 // virtual
263 bool DvdWizardVob::isComplete() const
264 {
265     if (!m_view.error_message->text().isEmpty()) return false;
266     if (m_view.vobs_list->topLevelItemCount() == 0) return false;
267     return true;
268 }
269
270 void DvdWizardVob::setUrl(const QString &url)
271 {
272     slotAddVobFile(KUrl(url));
273 }
274
275 QStringList DvdWizardVob::selectedUrls() const
276 {
277     QStringList result;
278     QString path;
279     int max = m_view.vobs_list->topLevelItemCount();
280     for (int i = 0; i < max; i++) {
281         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
282         if (item) result.append(item->text(0));
283     }
284     return result;
285 }
286
287
288 QStringList DvdWizardVob::durations() const
289 {
290     QStringList result;
291     QString path;
292     int max = m_view.vobs_list->topLevelItemCount();
293     for (int i = 0; i < max; i++) {
294         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
295         if (item) result.append(QString::number(item->data(1, Qt::UserRole).toInt()));
296     }
297     return result;
298 }
299
300 QStringList DvdWizardVob::chapters() const
301 {
302     QStringList result;
303     QString path;
304     int max = m_view.vobs_list->topLevelItemCount();
305     for (int i = 0; i < max; i++) {
306         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
307         if (item) {
308             result.append(item->data(1, Qt::UserRole + 1).toString());
309         }
310     }
311     return result;
312 }
313
314 void DvdWizardVob::updateChapters(QMap <QString, QString> chaptersdata)
315 {
316     int max = m_view.vobs_list->topLevelItemCount();
317     for (int i = 0; i < max; i++) {
318         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
319         if (chaptersdata.contains(item->text(0))) item->setData(1, Qt::UserRole + 1, chaptersdata.value(item->text(0)));
320     }
321 }
322
323 int DvdWizardVob::duration(int ix) const
324 {
325     int result = -1;
326     QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(ix);
327     if (item) {
328         result = item->data(1, Qt::UserRole).toInt();
329     }
330     return result;
331 }
332
333
334 QString DvdWizardVob::introMovie() const
335 {
336     if (!m_view.use_intro->isChecked()) return QString();
337     return m_view.intro_vob->url().path();
338 }
339
340 void DvdWizardVob::setIntroMovie(const QString& path)
341 {
342     m_view.intro_vob->setUrl(KUrl(path));
343     m_view.use_intro->setChecked(path.isEmpty() == false);
344 }
345
346
347 void DvdWizardVob::slotCheckVobList()
348 {
349     emit completeChanged();
350     int max = m_view.vobs_list->topLevelItemCount();
351     QTreeWidgetItem *item = m_view.vobs_list->currentItem();
352     bool hasItem = true;
353     if (item == NULL) hasItem = false;
354     m_view.button_delete->setEnabled(hasItem);
355     if (hasItem && m_view.vobs_list->indexOfTopLevelItem(item) == 0) m_view.button_up->setEnabled(false);
356     else m_view.button_up->setEnabled(hasItem);
357     if (hasItem && m_view.vobs_list->indexOfTopLevelItem(item) == max - 1) m_view.button_down->setEnabled(false);
358     else m_view.button_down->setEnabled(hasItem);
359
360     qint64 totalSize = 0;
361     for (int i = 0; i < max; i++) {
362         item = m_view.vobs_list->topLevelItem(i);
363         if (item) totalSize += (qint64) item->data(0, Qt::UserRole).toInt();
364     }
365
366     qint64 maxSize = (qint64) 47000 * 100000;
367     m_capacityBar->setValue(100 * totalSize / maxSize);
368     m_capacityBar->setText(KIO::convertSize(totalSize));
369 }
370
371 void DvdWizardVob::slotItemUp()
372 {
373     QTreeWidgetItem *item = m_view.vobs_list->currentItem();
374     if (item == NULL) return;
375     int index = m_view.vobs_list->indexOfTopLevelItem(item);
376     if (index == 0) return;
377     m_view.vobs_list->insertTopLevelItem(index - 1, m_view.vobs_list->takeTopLevelItem(index));
378 }
379
380 void DvdWizardVob::slotItemDown()
381 {
382     int max = m_view.vobs_list->topLevelItemCount();
383     QTreeWidgetItem *item = m_view.vobs_list->currentItem();
384     if (item == NULL) return;
385     int index = m_view.vobs_list->indexOfTopLevelItem(item);
386     if (index == max - 1) return;
387     m_view.vobs_list->insertTopLevelItem(index + 1, m_view.vobs_list->takeTopLevelItem(index));
388 }
389
390 bool DvdWizardVob::isPal() const
391 {
392     return m_view.dvd_profile->currentIndex() < 2;
393 }
394
395 bool DvdWizardVob::isWide() const
396 {
397     return (m_view.dvd_profile->currentIndex() == 1 || m_view.dvd_profile->currentIndex() == 3);
398 }
399
400 void DvdWizardVob::setProfile(const QString& profile)
401 {
402     if (profile == "dv_pal") m_view.dvd_profile->setCurrentIndex(0);
403     else if (profile == "dv_pal_wide") m_view.dvd_profile->setCurrentIndex(1);
404     else if (profile == "dv_ntsc") m_view.dvd_profile->setCurrentIndex(2);
405     else if (profile == "dv_ntsc_wide") m_view.dvd_profile->setCurrentIndex(3);
406 }
407
408 void DvdWizardVob::clear()
409 {
410     m_view.vobs_list->clear();
411 }