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