]> git.sesse.net Git - kdenlive/blob - src/dvdwizardvob.cpp
Fix missing header for QHeaderView
[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.", i18n("dvdauthor")));
56     if (KStandardDirs::findExe("mkisofs").isEmpty()) m_errorMessage.append(i18n("<strong>Program %1 is required for the DVD wizard.", i18n("mkisofs")));
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     char *tmp = (char*) qstrdup(profilename.toUtf8().data());
113     Mlt::Profile profile(tmp);
114     delete[] 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"));
118
119     QPixmap pix(60, 45);
120
121     tmp = (char *) qstrdup(url.path().toUtf8().data());
122     Mlt::Producer *producer = new Mlt::Producer(profile, tmp);
123     delete[] tmp;
124
125     if (producer->is_blank() == false) {
126         pix = KThumb::getFrame(producer, 0, 60, 45);
127         item->setIcon(0, pix);
128         int playTime = producer->get_playtime();
129         item->setText(1, Timecode::getStringTimecode(playTime, profile.fps()));
130         item->setData(1, Qt::UserRole, playTime);
131     }
132     delete producer;
133
134     if (chapters.isEmpty() == false) {
135         item->setData(1, Qt::UserRole + 1, chapters);
136     } else if (QFile::exists(url.path() + ".dvdchapter")) {
137         // insert chapters as children
138         QFile file(url.path() + ".dvdchapter");
139         if (file.open(QIODevice::ReadOnly)) {
140             QDomDocument doc;
141             doc.setContent(&file);
142             file.close();
143             QDomNodeList chapters = doc.elementsByTagName("chapter");
144             QStringList chaptersList;
145             for (int j = 0; j < chapters.count(); j++) {
146                 chaptersList.append(QString::number(chapters.at(j).toElement().attribute("time").toInt()));
147             }
148             item->setData(1, Qt::UserRole + 1, chaptersList.join(";"));
149         }
150     }
151
152     slotCheckVobList();
153 }
154
155 void DvdWizardVob::changeFormat()
156 {
157     int max = m_view.vobs_list->topLevelItemCount();
158     QString profilename;
159     switch (m_view.dvd_profile->currentIndex()) {
160     case 1:
161         profilename = "dv_pal_wide";
162         break;
163     case 2:
164         profilename = "dv_ntsc";
165         break;
166     case 3:
167         profilename = "dv_ntsc_wide";
168         break;
169     default:
170         profilename = "dv_pal";
171         break;
172     }
173
174     char *tmp = (char*) qstrdup(profilename.toUtf8().data());
175     Mlt::Profile profile(tmp);
176     delete[] tmp;
177     QPixmap pix(180, 135);
178
179     for (int i = 0; i < max; i++) {
180         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
181         tmp = (char *) qstrdup(item->text(0).toUtf8().data());
182         Mlt::Producer *producer = new Mlt::Producer(profile, tmp);
183         delete[] tmp;
184
185         if (producer->is_blank() == false) {
186             //pix = KThumb::getFrame(producer, 0, 180, 135);
187             //item->setIcon(0, pix);
188             item->setText(1, Timecode::getStringTimecode(producer->get_playtime(), profile.fps()));
189         }
190         delete producer;
191         int submax = item->childCount();
192         for (int j = 0; j < submax; j++) {
193             QTreeWidgetItem *subitem = item->child(j);
194             subitem->setText(1, Timecode::getStringTimecode(subitem->data(1, Qt::UserRole).toInt(), profile.fps()));
195         }
196     }
197     slotCheckVobList();
198 }
199
200 void DvdWizardVob::slotDeleteVobFile()
201 {
202     QTreeWidgetItem *item = m_view.vobs_list->currentItem();
203     if (item == NULL) return;
204     delete item;
205     slotCheckVobList();
206 }
207
208
209 // virtual
210 bool DvdWizardVob::isComplete() const
211 {
212     if (!m_view.error_message->text().isEmpty()) return false;
213     if (m_view.vobs_list->topLevelItemCount() == 0) return false;
214     return true;
215 }
216
217 void DvdWizardVob::setUrl(const QString &url)
218 {
219     slotAddVobFile(KUrl(url));
220 }
221
222 QStringList DvdWizardVob::selectedUrls() const
223 {
224     QStringList result;
225     QString path;
226     int max = m_view.vobs_list->topLevelItemCount();
227     for (int i = 0; i < max; i++) {
228         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
229         if (item) result.append(item->text(0));
230     }
231     return result;
232 }
233
234
235 QStringList DvdWizardVob::durations() const
236 {
237     QStringList result;
238     QString path;
239     int max = m_view.vobs_list->topLevelItemCount();
240     for (int i = 0; i < max; i++) {
241         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
242         if (item) result.append(QString::number(item->data(1, Qt::UserRole).toInt()));
243     }
244     return result;
245 }
246
247 QStringList DvdWizardVob::chapters() const
248 {
249     QStringList result;
250     QString path;
251     int max = m_view.vobs_list->topLevelItemCount();
252     for (int i = 0; i < max; i++) {
253         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
254         if (item) result.append(item->data(1, Qt::UserRole + 1).toString());
255     }
256     return result;
257 }
258
259 void DvdWizardVob::updateChapters(QMap <QString, QString> chaptersdata)
260 {
261     int max = m_view.vobs_list->topLevelItemCount();
262     for (int i = 0; i < max; i++) {
263         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
264         item->setData(1, Qt::UserRole + 1, chaptersdata.value(item->text(0)));
265     }
266 }
267
268 int DvdWizardVob::duration(int ix) const
269 {
270     int result = -1;
271     QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(ix);
272     if (item) {
273         result = item->data(1, Qt::UserRole).toInt();
274     }
275     return result;
276 }
277
278
279 QString DvdWizardVob::introMovie() const
280 {
281     if (!m_view.use_intro->isChecked()) return QString();
282     return m_view.intro_vob->url().path();
283 }
284
285 void DvdWizardVob::setIntroMovie(const QString path)
286 {
287     m_view.intro_vob->setPath(path);
288     m_view.use_intro->setChecked(path.isEmpty() == false);
289 }
290
291
292 void DvdWizardVob::slotCheckVobList()
293 {
294     emit completeChanged();
295     int max = m_view.vobs_list->topLevelItemCount();
296     QTreeWidgetItem *item = m_view.vobs_list->currentItem();
297     bool hasItem = true;
298     if (item == NULL) hasItem = false;
299     m_view.button_delete->setEnabled(hasItem);
300     if (hasItem && m_view.vobs_list->indexOfTopLevelItem(item) == 0) m_view.button_up->setEnabled(false);
301     else m_view.button_up->setEnabled(hasItem);
302     if (hasItem && m_view.vobs_list->indexOfTopLevelItem(item) == max - 1) m_view.button_down->setEnabled(false);
303     else m_view.button_down->setEnabled(hasItem);
304
305 #if KDE_IS_VERSION(4,2,0)
306     qint64 totalSize = 0;
307     for (int i = 0; i < max; i++) {
308         item = m_view.vobs_list->topLevelItem(i);
309         if (item) totalSize += (qint64) item->data(0, Qt::UserRole).toInt();
310     }
311
312     qint64 maxSize = (qint64) 47000 * 100000;
313     m_capacityBar->setValue(100 * totalSize / maxSize);
314     m_capacityBar->setText(KIO::convertSize(totalSize));
315 #endif
316 }
317
318 void DvdWizardVob::slotItemUp()
319 {
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 == 0) return;
324     m_view.vobs_list->insertTopLevelItem(index - 1, m_view.vobs_list->takeTopLevelItem(index));
325 }
326
327 void DvdWizardVob::slotItemDown()
328 {
329     int max = m_view.vobs_list->topLevelItemCount();
330     QTreeWidgetItem *item = m_view.vobs_list->currentItem();
331     if (item == NULL) return;
332     int index = m_view.vobs_list->indexOfTopLevelItem(item);
333     if (index == max - 1) return;
334     m_view.vobs_list->insertTopLevelItem(index + 1, m_view.vobs_list->takeTopLevelItem(index));
335 }
336
337 bool DvdWizardVob::isPal() const
338 {
339     return m_view.dvd_profile->currentIndex() < 2;
340 }
341
342 bool DvdWizardVob::isWide() const
343 {
344     return (m_view.dvd_profile->currentIndex() == 1 || m_view.dvd_profile->currentIndex() == 3);
345 }
346
347 void DvdWizardVob::setProfile(const QString profile)
348 {
349     if (profile == "dv_pal") m_view.dvd_profile->setCurrentIndex(0);
350     else if (profile == "dv_pal_wide") m_view.dvd_profile->setCurrentIndex(1);
351     else if (profile == "dv_ntsc") m_view.dvd_profile->setCurrentIndex(2);
352     else if (profile == "dv_ntsc_wide") m_view.dvd_profile->setCurrentIndex(3);
353 }
354
355 void DvdWizardVob::clear()
356 {
357     m_view.vobs_list->clear();
358 }