]> git.sesse.net Git - kdenlive/blob - src/dvdwizardvob.cpp
Update DVD Wizard: support for 16:9 profiles and GUI improvements
[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
36 DvdWizardVob::DvdWizardVob(const QString &profile, QWidget *parent) :
37         QWizardPage(parent)
38 {
39     m_view.setupUi(this);
40     m_view.intro_vob->setEnabled(false);
41     m_view.intro_vob->setFilter("video/mpeg");
42     m_view.button_add->setIcon(KIcon("document-new"));
43     m_view.button_delete->setIcon(KIcon("edit-delete"));
44     m_view.button_up->setIcon(KIcon("go-up"));
45     m_view.button_down->setIcon(KIcon("go-down"));
46     connect(m_view.use_intro, SIGNAL(toggled(bool)), m_view.intro_vob, SLOT(setEnabled(bool)));
47     connect(m_view.button_add, SIGNAL(clicked()), this, SLOT(slotAddVobFile()));
48     connect(m_view.button_delete, SIGNAL(clicked()), this, SLOT(slotDeleteVobFile()));
49     connect(m_view.button_up, SIGNAL(clicked()), this, SLOT(slotItemUp()));
50     connect(m_view.button_down, SIGNAL(clicked()), this, SLOT(slotItemDown()));
51     connect(m_view.vobs_list, SIGNAL(itemSelectionChanged()), this, SLOT(slotCheckVobList()));
52     m_view.vobs_list->setIconSize(QSize(60, 45));
53
54     if (KStandardDirs::findExe("dvdauthor").isEmpty()) m_errorMessage.append(i18n("<strong>Program %1 is required for the DVD wizard.", i18n("dvdauthor")));
55     if (KStandardDirs::findExe("mkisofs").isEmpty()) m_errorMessage.append(i18n("<strong>Program %1 is required for the DVD wizard.", i18n("mkisofs")));
56     if (m_errorMessage.isEmpty()) m_view.error_message->setVisible(false);
57     else m_view.error_message->setText(m_errorMessage);
58
59     m_view.dvd_profile->addItems(QStringList() << i18n("PAL 4:3") << i18n("PAL 16:9") << i18n("NTSC 4:3") << i18n("NTSC 16:9"));
60     if (profile == "dv_pal_wide") m_view.dvd_profile->setCurrentIndex(1);
61     else if (profile == "dv_ntsc") m_view.dvd_profile->setCurrentIndex(2);
62     else if (profile == "dv_ntsc_wide") m_view.dvd_profile->setCurrentIndex(3);
63
64     connect(m_view.dvd_profile, SIGNAL(activated(int)), this, SLOT(changeFormat()));
65     m_view.vobs_list->header()->setStretchLastSection(false);
66     m_view.vobs_list->header()->setResizeMode(0, QHeaderView::Stretch);
67     m_view.vobs_list->header()->setResizeMode(1, QHeaderView::Custom);
68     m_view.vobs_list->header()->setResizeMode(2, QHeaderView::Custom);
69
70 #if KDE_IS_VERSION(4,2,0)
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);
75 #else
76     m_view.size_box->setHidden(true);
77 #endif
78     slotCheckVobList();
79 }
80
81 DvdWizardVob::~DvdWizardVob()
82 {
83 #if KDE_IS_VERSION(4,2,0)
84     delete m_capacityBar;
85 #endif
86 }
87
88
89 void DvdWizardVob::slotAddVobFile(KUrl url)
90 {
91     if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl("kfiledialog:///projectfolder"), "video/mpeg", this, i18n("Add new video file"));
92     if (url.isEmpty()) return;
93     QFile f(url.path());
94     qint64 fileSize = f.size();
95     QString profilename;
96     if (m_view.dvd_profile->currentIndex() == 0) profilename = "dv_pal";
97     else profilename = "dv_ntsc";
98     Mlt::Profile profile((char*) profilename.data());
99     QTreeWidgetItem *item = new QTreeWidgetItem(m_view.vobs_list, QStringList() << url.path() << QString() << KIO::convertSize(fileSize));
100     item->setData(0, Qt::UserRole, fileSize);
101     item->setIcon(0, KIcon("video-x-generic"));
102
103     QPixmap pix(60, 45);
104
105     char *tmp = (char *) qstrdup(url.path().toUtf8().data());
106     Mlt::Producer *producer = new Mlt::Producer(profile, tmp);
107     delete[] tmp;
108
109     if (producer->is_blank() == false) {
110         pix = KThumb::getFrame(producer, 0, 60, 45);
111         item->setIcon(0, pix);
112         int playTime = producer->get_playtime();
113         item->setText(1, Timecode::getStringTimecode(playTime, profile.fps()));
114         item->setData(1, Qt::UserRole, playTime);
115     }
116     delete producer;
117
118     slotCheckVobList();
119 }
120
121 void DvdWizardVob::changeFormat()
122 {
123     int max = m_view.vobs_list->topLevelItemCount();
124     QString profilename;
125     if (m_view.dvd_profile->currentIndex() == 0) profilename = "dv_pal";
126     else profilename = "dv_ntsc";
127     Mlt::Profile profile((char*) profilename.data());
128     QPixmap pix(180, 135);
129
130     for (int i = 0; i < max; i++) {
131         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
132         char *tmp = (char *) qstrdup(item->text(0).toUtf8().data());
133         Mlt::Producer *producer = new Mlt::Producer(profile, tmp);
134         delete[] tmp;
135
136         if (producer->is_blank() == false) {
137             //pix = KThumb::getFrame(producer, 0, 180, 135);
138             //item->setIcon(0, pix);
139             item->setText(1, Timecode::getStringTimecode(producer->get_playtime(), profile.fps()));
140         }
141         delete producer;
142         int submax = item->childCount();
143         for (int j = 0; j < submax; j++) {
144             QTreeWidgetItem *subitem = item->child(j);
145             subitem->setText(1, Timecode::getStringTimecode(subitem->data(1, Qt::UserRole).toInt(), profile.fps()));
146         }
147     }
148     slotCheckVobList();
149 }
150
151 void DvdWizardVob::slotDeleteVobFile()
152 {
153     QTreeWidgetItem *item = m_view.vobs_list->currentItem();
154     if (item == NULL) return;
155     delete item;
156     slotCheckVobList();
157 }
158
159
160 // virtual
161 bool DvdWizardVob::isComplete() const
162 {
163     if (!m_view.error_message->text().isEmpty()) return false;
164     if (m_view.vobs_list->topLevelItemCount() == 0) return false;
165     return true;
166 }
167
168 void DvdWizardVob::setUrl(const QString &url)
169 {
170     slotAddVobFile(KUrl(url));
171 }
172
173 QStringList DvdWizardVob::selectedUrls() const
174 {
175     QStringList result;
176     QString path;
177     int max = m_view.vobs_list->topLevelItemCount();
178     for (int i = 0; i < max; i++) {
179         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
180         if (item) result.append(item->text(0));
181     }
182     return result;
183 }
184
185
186 QStringList DvdWizardVob::durations() const
187 {
188     QStringList result;
189     QString path;
190     int max = m_view.vobs_list->topLevelItemCount();
191     for (int i = 0; i < max; i++) {
192         QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(i);
193         if (item) result.append(QString::number(item->data(1, Qt::UserRole).toInt()));
194     }
195     return result;
196 }
197
198 int DvdWizardVob::duration(int ix) const
199 {
200     int result = -1;
201     QTreeWidgetItem *item = m_view.vobs_list->topLevelItem(ix);
202     if (item) {
203         result = item->data(1, Qt::UserRole).toInt();
204     }
205     return result;
206 }
207
208
209 QString DvdWizardVob::introMovie() const
210 {
211     if (!m_view.use_intro->isChecked()) return QString();
212     return m_view.intro_vob->url().path();
213 }
214
215 void DvdWizardVob::slotCheckVobList()
216 {
217     emit completeChanged();
218     int max = m_view.vobs_list->topLevelItemCount();
219     QTreeWidgetItem *item = m_view.vobs_list->currentItem();
220     bool hasItem = true;
221     if (item == NULL) hasItem = false;
222     m_view.button_delete->setEnabled(hasItem);
223     if (hasItem && m_view.vobs_list->indexOfTopLevelItem(item) == 0) m_view.button_up->setEnabled(false);
224     else m_view.button_up->setEnabled(hasItem);
225     if (hasItem && m_view.vobs_list->indexOfTopLevelItem(item) == max - 1) m_view.button_down->setEnabled(false);
226     else m_view.button_down->setEnabled(hasItem);
227
228 #if KDE_IS_VERSION(4,2,0)
229     qint64 totalSize = 0;
230     for (int i = 0; i < max; i++) {
231         item = m_view.vobs_list->topLevelItem(i);
232         if (item) totalSize += (qint64) item->data(0, Qt::UserRole).toInt();
233     }
234
235     qint64 maxSize = (qint64) 47000 * 100000;
236     m_capacityBar->setValue(100 * totalSize / maxSize);
237     m_capacityBar->setText(KIO::convertSize(totalSize));
238 #endif
239 }
240
241 void DvdWizardVob::slotItemUp()
242 {
243     QTreeWidgetItem *item = m_view.vobs_list->currentItem();
244     if (item == NULL) return;
245     int index = m_view.vobs_list->indexOfTopLevelItem(item);
246     if (index == 0) return;
247     m_view.vobs_list->insertTopLevelItem(index - 1, m_view.vobs_list->takeTopLevelItem(index));
248 }
249
250 void DvdWizardVob::slotItemDown()
251 {
252     int max = m_view.vobs_list->topLevelItemCount();
253     QTreeWidgetItem *item = m_view.vobs_list->currentItem();
254     if (item == NULL) return;
255     int index = m_view.vobs_list->indexOfTopLevelItem(item);
256     if (index == max - 1) return;
257     m_view.vobs_list->insertTopLevelItem(index + 1, m_view.vobs_list->takeTopLevelItem(index));
258 }
259
260 bool DvdWizardVob::isPal() const
261 {
262     return m_view.dvd_profile->currentIndex() < 2;
263 }
264
265 bool DvdWizardVob::isWide() const
266 {
267     return (m_view.dvd_profile->currentIndex() == 1 || m_view.dvd_profile->currentIndex() == 3);
268 }
269
270
271