1 /***************************************************************************
2 * Copyright (C) 2009 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
20 #include "dvdwizardchapters.h"
26 DvdWizardChapters::DvdWizardChapters(bool isPal, QWidget *parent) :
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.button_save, SIGNAL(clicked()), this, SLOT(slotSaveChapter()));
36 connect(m_view.chapters_list, SIGNAL(itemSelectionChanged()), this, SLOT(slotGoToChapter()));
38 // Build monitor for chapters
40 if (m_isPal) m_tc.setFormat(25);
41 else m_tc.setFormat(30, true);
43 m_manager = new MonitorManager(this);
44 m_manager->resetProfiles(m_tc);
45 m_monitor = new Monitor("chapter", m_manager, this);
48 QVBoxLayout *vbox = new QVBoxLayout;
49 vbox->addWidget(m_monitor);
50 m_view.monitor_frame->setLayout(vbox);
55 DvdWizardChapters::~DvdWizardChapters()
63 bool DvdWizardChapters::isComplete() const
68 void DvdWizardChapters::slotUpdateChaptersList()
70 m_monitor->slotOpenFile(m_view.vob_list->currentText());
71 m_monitor->adjustRulerSize(m_view.vob_list->itemData(m_view.vob_list->currentIndex(), Qt::UserRole).toInt());
72 QStringList currentChaps = m_view.vob_list->itemData(m_view.vob_list->currentIndex(), Qt::UserRole + 1).toStringList();
75 QStringList chaptersString;
76 for (int i = 0; i < currentChaps.count(); i++) {
77 chaptersString.append(Timecode::getStringTimecode(currentChaps.at(i).toInt(), m_tc.fps()));
79 m_view.chapters_list->clear();
80 m_view.chapters_list->addItems(chaptersString);
82 bool modified = m_view.vob_list->itemData(m_view.vob_list->currentIndex(), Qt::UserRole + 2).toInt();
83 m_view.button_save->setEnabled(modified);
86 void DvdWizardChapters::slotAddChapter()
88 int pos = m_monitor->position().frames(m_tc.fps());
89 QStringList currentChaps = m_view.vob_list->itemData(m_view.vob_list->currentIndex(), Qt::UserRole + 1).toStringList();
90 if (currentChaps.contains(QString::number(pos))) return;
91 else currentChaps.append(QString::number(pos));
92 QList <int> chapterTimes;
93 for (int i = 0; i < currentChaps.count(); i++)
94 chapterTimes.append(currentChaps.at(i).toInt());
98 QStringList chaptersString;
100 for (int i = 0; i < chapterTimes.count(); i++) {
101 chaptersString.append(Timecode::getStringTimecode(chapterTimes.at(i), m_tc.fps()));
102 currentChaps.append(QString::number(chapterTimes.at(i)));
104 // Save item chapters
105 m_view.vob_list->setItemData(m_view.vob_list->currentIndex(), currentChaps, Qt::UserRole + 1);
106 // Mark item as modified
107 m_view.vob_list->setItemData(m_view.vob_list->currentIndex(), 1, Qt::UserRole + 2);
108 m_view.chapters_list->clear();
109 m_view.chapters_list->addItems(chaptersString);
110 m_view.button_save->setEnabled(true);
113 void DvdWizardChapters::slotRemoveChapter()
115 int ix = m_view.chapters_list->currentRow();
116 QStringList currentChaps = m_view.vob_list->itemData(m_view.vob_list->currentIndex(), Qt::UserRole + 1).toStringList();
117 currentChaps.removeAt(ix);
119 // Save item chapters
120 m_view.vob_list->setItemData(m_view.vob_list->currentIndex(), currentChaps, Qt::UserRole + 1);
121 // Mark item as modified
122 m_view.vob_list->setItemData(m_view.vob_list->currentIndex(), 1, Qt::UserRole + 2);
125 QStringList chaptersString;
126 for (int i = 0; i < currentChaps.count(); i++) {
127 chaptersString.append(Timecode::getStringTimecode(currentChaps.at(i).toInt(), m_tc.fps()));
129 m_view.chapters_list->clear();
130 m_view.chapters_list->addItems(chaptersString);
131 m_view.button_save->setEnabled(true);
134 void DvdWizardChapters::slotGoToChapter()
136 m_monitor->setTimePos(m_view.chapters_list->currentItem()->text() + ":00");
139 void DvdWizardChapters::slotGetChaptersList(int ix)
141 QString url = m_view.vob_list->itemText(ix);
142 if (QFile::exists(url + ".dvdchapter")) {
143 // insert chapters as children
144 QFile file(url + ".dvdchapter");
145 if (file.open(QIODevice::ReadOnly)) {
147 doc.setContent(&file);
149 QDomNodeList chapters = doc.elementsByTagName("chapter");
150 QStringList chaptersList;
151 for (int j = 0; j < chapters.count(); j++) {
152 chaptersList.append(QString::number(chapters.at(j).toElement().attribute("time").toInt()));
154 m_view.vob_list->setItemData(ix, chaptersList, Qt::UserRole + 1);
159 void DvdWizardChapters::setVobFiles(bool isPal, const QStringList movies, const QStringList durations)
162 if (m_isPal) m_tc.setFormat(25);
163 else m_tc.setFormat(30, true);
164 m_manager->resetProfiles(m_tc);
165 m_monitor->resetProfile();
167 if (m_view.vob_list->count() == movies.count()) {
169 for (int i = 0; i < movies.count(); i++) {
170 if (movies.at(i) != m_view.vob_list->itemText(i)) {
177 m_view.vob_list->clear();
178 for (int i = 0; i < movies.count(); i++) {
179 m_view.vob_list->addItem(movies.at(i), durations.at(i));
180 slotGetChaptersList(i);
182 slotUpdateChaptersList();
185 QStringList DvdWizardChapters::selectedTitles() const
188 int max = m_view.vob_list->count();
189 for (int i = 0; i < max; i++) {
190 result.append(m_view.vob_list->itemText(i));
191 QStringList chapters = m_view.vob_list->itemData(i, Qt::UserRole + 1).toStringList();
192 for (int j = 0; j < chapters.count(); j++) {
193 result.append(Timecode::getStringTimecode(chapters.at(j).toInt(), m_tc.fps()));
199 QStringList DvdWizardChapters::chapters(int ix) const
202 QStringList chapters = m_view.vob_list->itemData(ix, Qt::UserRole + 1).toStringList();
203 for (int j = 0; j < chapters.count(); j++) {
204 result.append(Timecode::getStringTimecode(chapters.at(j).toInt(), m_tc.fps()));
209 QStringList DvdWizardChapters::selectedTargets() const
212 int max = m_view.vob_list->count();
213 for (int i = 0; i < max; i++) {
214 result.append("jump title " + QString::number(i + 1));
215 QStringList chapters = m_view.vob_list->itemData(i, Qt::UserRole + 1).toStringList();
216 for (int j = 0; j < chapters.count(); j++) {
217 result.append("jump title " + QString::number(i + 1) + " chapter " + QString::number(j + 1));
223 void DvdWizardChapters::slotSaveChapter()
226 QDomElement chapters = doc.createElement("chapters");
227 chapters.setAttribute("fps", m_tc.fps());
228 doc.appendChild(chapters);
230 QStringList chaptersList = m_view.vob_list->itemData(m_view.vob_list->currentIndex(), Qt::UserRole + 1).toStringList();
232 for (int i = 0; i < chaptersList.count(); i++) {
233 QDomElement chapter = doc.createElement("chapter");
234 chapters.appendChild(chapter);
235 chapter.setAttribute("title", i18n("Chapter %1", i));
236 chapter.setAttribute("time", chaptersList.at(i));
238 // save chapters file
239 QFile file(m_view.vob_list->currentText() + ".dvdchapter");
240 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
241 kWarning() << "////// ERROR writing DVD CHAPTER file: " << m_view.vob_list->currentText() + ".dvdchapter";
243 file.write(doc.toString().toUtf8());
244 if (file.error() != QFile::NoError)
245 kWarning() << "////// ERROR writing DVD CHAPTER file: " << m_view.vob_list->currentText() + ".dvdchapter";
247 m_view.vob_list->setItemData(m_view.vob_list->currentIndex(), 0, Qt::UserRole + 2);
248 m_view.button_save->setEnabled(false);