]> git.sesse.net Git - kdenlive/blob - src/dvdwizardchapters.cpp
Improved Dvd Wizard (new chapters dialog)
[kdenlive] / src / dvdwizardchapters.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 "dvdwizardchapters.h"
21
22 #include <KDebug>
23
24 #include <QFile>
25
26 DvdWizardChapters::DvdWizardChapters(bool isPal, QWidget *parent) :
27         QWizardPage(parent),
28         m_isPal(isPal)
29
30 {
31     m_view.setupUi(this);
32     connect(m_view.vob_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateChaptersList()));
33     connect(m_view.button_add, SIGNAL(clicked()), this, SLOT(slotAddChapter()));
34     connect(m_view.button_delete, SIGNAL(clicked()), this, SLOT(slotRemoveChapter()));
35     connect(m_view.chapters_list, SIGNAL(itemSelectionChanged()), this, SLOT(slotGoToChapter()));
36
37     // Build monitor for chapters
38
39     if (m_isPal) m_tc.setFormat(25);
40     else m_tc.setFormat(30, true);
41
42     m_manager = new MonitorManager(this);
43     m_manager->resetProfiles(m_tc);
44     m_monitor = new Monitor("chapter", m_manager, this);
45     m_monitor->start();
46
47     QVBoxLayout *vbox = new QVBoxLayout;
48     vbox->addWidget(m_monitor);
49     m_view.monitor_frame->setLayout(vbox);
50
51
52 }
53
54 DvdWizardChapters::~DvdWizardChapters()
55 {
56     delete m_monitor;
57     delete m_manager;
58 }
59
60 // virtual
61
62 bool DvdWizardChapters::isComplete() const
63 {
64     return true;
65 }
66
67 void DvdWizardChapters::slotUpdateChaptersList()
68 {
69     m_monitor->slotOpenFile(m_view.vob_list->currentText());
70     m_monitor->adjustRulerSize(m_view.vob_list->itemData(m_view.vob_list->currentIndex(), Qt::UserRole).toInt());
71     QStringList currentChaps = m_view.vob_list->itemData(m_view.vob_list->currentIndex(), Qt::UserRole + 1).toStringList();
72
73     // insert chapters
74     QStringList chaptersString;
75     for (int i = 0; i < currentChaps.count(); i++) {
76         chaptersString.append(Timecode::getStringTimecode(currentChaps.at(i).toInt(), m_tc.fps()));
77     }
78     m_view.chapters_list->clear();
79     m_view.chapters_list->addItems(chaptersString);
80 }
81
82 void DvdWizardChapters::slotAddChapter()
83 {
84     int pos = m_monitor->position().frames(m_tc.fps());
85     QStringList currentChaps = m_view.vob_list->itemData(m_view.vob_list->currentIndex(), Qt::UserRole + 1).toStringList();
86     if (currentChaps.contains(QString::number(pos))) return;
87     else currentChaps.append(QString::number(pos));
88     QList <int> chapterTimes;
89     for (int i = 0; i < currentChaps.count(); i++)
90         chapterTimes.append(currentChaps.at(i).toInt());
91     qSort(chapterTimes);
92
93     // rebuild chapters
94     QStringList chaptersString;
95     currentChaps.clear();
96     for (int i = 0; i < chapterTimes.count(); i++) {
97         chaptersString.append(Timecode::getStringTimecode(chapterTimes.at(i), m_tc.fps()));
98         currentChaps.append(QString::number(chapterTimes.at(i)));
99     }
100     m_view.vob_list->setItemData(m_view.vob_list->currentIndex(), currentChaps, Qt::UserRole + 1);
101     m_view.chapters_list->clear();
102     m_view.chapters_list->addItems(chaptersString);
103 }
104
105 void DvdWizardChapters::slotRemoveChapter()
106 {
107     int ix = m_view.chapters_list->currentRow();
108     QStringList currentChaps = m_view.vob_list->itemData(m_view.vob_list->currentIndex(), Qt::UserRole + 1).toStringList();
109     currentChaps.removeAt(ix);
110     m_view.vob_list->setItemData(m_view.vob_list->currentIndex(), currentChaps, Qt::UserRole + 1);
111
112     // rebuild chapters
113     QStringList chaptersString;
114     for (int i = 0; i < currentChaps.count(); i++) {
115         chaptersString.append(Timecode::getStringTimecode(currentChaps.at(i).toInt(), m_tc.fps()));
116     }
117     m_view.chapters_list->clear();
118     m_view.chapters_list->addItems(chaptersString);
119 }
120
121 void DvdWizardChapters::slotGoToChapter()
122 {
123     m_monitor->setTimePos(m_view.chapters_list->currentItem()->text() + ":00");
124 }
125
126 void DvdWizardChapters::slotGetChaptersList(int ix)
127 {
128     QString url = m_view.vob_list->itemText(ix);
129     if (QFile::exists(url + ".dvdchapter")) {
130         // insert chapters as children
131         QFile file(url + ".dvdchapter");
132         if (file.open(QIODevice::ReadOnly)) {
133             QDomDocument doc;
134             doc.setContent(&file);
135             file.close();
136             QDomNodeList chapters = doc.elementsByTagName("chapter");
137             QStringList chaptersList;
138             for (int j = 0; j < chapters.count(); j++) {
139                 chaptersList.append(QString::number(chapters.at(j).toElement().attribute("time").toInt()));
140             }
141             m_view.vob_list->setItemData(ix, chaptersList, Qt::UserRole + 1);
142         }
143     }
144 }
145
146 void DvdWizardChapters::setVobFiles(bool isPal, const QStringList movies, const QStringList durations)
147 {
148     m_isPal = isPal;
149     if (m_isPal) m_tc.setFormat(25);
150     else m_tc.setFormat(30, true);
151     m_manager->resetProfiles(m_tc);
152     m_monitor->resetProfile();
153
154     if (m_view.vob_list->count() == movies.count()) {
155         bool equal = true;
156         for (int i = 0; i < movies.count(); i++) {
157             if (movies.at(i) != m_view.vob_list->itemText(i)) {
158                 equal = false;
159                 break;
160             }
161         }
162         if (equal) return;
163     }
164     m_view.vob_list->clear();
165     for (int i = 0; i < movies.count(); i++) {
166         m_view.vob_list->addItem(movies.at(i), durations.at(i));
167         slotGetChaptersList(i);
168     }
169     slotUpdateChaptersList();
170 }
171
172 QStringList DvdWizardChapters::selectedTitles() const
173 {
174     QStringList result;
175     int max = m_view.vob_list->count();
176     for (int i = 0; i < max; i++) {
177         result.append(m_view.vob_list->itemText(i));
178         QStringList chapters = m_view.vob_list->itemData(i, Qt::UserRole + 1).toStringList();
179         for (int j = 0; j < chapters.count(); j++) {
180             result.append(Timecode::getStringTimecode(chapters.at(j).toInt(), m_tc.fps()));
181         }
182     }
183     return result;
184 }
185
186 QStringList DvdWizardChapters::chapters(int ix) const
187 {
188     QStringList result;
189     QStringList chapters = m_view.vob_list->itemData(ix, Qt::UserRole + 1).toStringList();
190     for (int j = 0; j < chapters.count(); j++) {
191         result.append(Timecode::getStringTimecode(chapters.at(j).toInt(), m_tc.fps()));
192     }
193     return result;
194 }
195
196 QStringList DvdWizardChapters::selectedTargets() const
197 {
198     QStringList result;
199     int max = m_view.vob_list->count();
200     for (int i = 0; i < max; i++) {
201         result.append("jump title " + QString::number(i + 1));
202         QStringList chapters = m_view.vob_list->itemData(i, Qt::UserRole + 1).toStringList();
203         for (int j = 0; j < chapters.count(); j++) {
204             result.append("jump title " + QString::number(i + 1) + " chapter " + QString::number(j + 1));
205         }
206     }
207     return result;
208 }