]> git.sesse.net Git - kdenlive/blob - src/dvdwizardchapters.cpp
Fix Dvd wizard monitor layout and loading issues
[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(MonitorManager *manager, DVDFORMAT format, QWidget *parent) :
27         QWizardPage(parent),
28         m_format(format),
29         m_monitor(NULL),
30         m_manager(manager)
31
32 {
33     m_view.setupUi(this);
34     connect(m_view.vob_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateChaptersList()));
35     connect(m_view.button_add, SIGNAL(clicked()), this, SLOT(slotAddChapter()));
36     connect(m_view.button_delete, SIGNAL(clicked()), this, SLOT(slotRemoveChapter()));
37     connect(m_view.chapters_list, SIGNAL(itemSelectionChanged()), this, SLOT(slotGoToChapter()));
38
39     // Build monitor for chapters
40
41     if (m_format == PAL || m_format == PAL_WIDE) m_tc.setFormat(25);
42     else m_tc.setFormat(30000.0 / 1001);
43     show();
44     setVobFiles(format, QStringList(), QStringList(), QStringList());
45
46     //m_view.monitor_frame->setVisible(false);
47 }
48
49 DvdWizardChapters::~DvdWizardChapters()
50 {
51     if (m_monitor) {
52         m_monitor->stop();
53         delete m_monitor;
54     }
55 }
56
57 // virtual
58
59 bool DvdWizardChapters::isComplete() const
60 {
61     return true;
62 }
63
64 void DvdWizardChapters::stopMonitor()
65 {
66     if (m_monitor) m_monitor->stop();
67 }
68
69 void DvdWizardChapters::slotUpdateChaptersList()
70 {
71     m_monitor->slotOpenFile(m_view.vob_list->currentText());
72     m_monitor->adjustRulerSize(m_view.vob_list->itemData(m_view.vob_list->currentIndex(), Qt::UserRole).toInt());
73     QStringList currentChaps = m_view.vob_list->itemData(m_view.vob_list->currentIndex(), Qt::UserRole + 1).toStringList();
74
75     // insert chapters
76     QStringList chaptersString;
77     for (int i = 0; i < currentChaps.count(); i++) {
78         chaptersString.append(Timecode::getStringTimecode(currentChaps.at(i).toInt(), m_tc.fps(), true));
79     }
80     m_view.chapters_list->clear();
81     m_view.chapters_list->addItems(chaptersString);
82
83     //bool modified = m_view.vob_list->itemData(m_view.vob_list->currentIndex(), Qt::UserRole + 2).toInt();
84 }
85
86 void DvdWizardChapters::slotAddChapter()
87 {
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());
95     qSort(chapterTimes);
96
97     // rebuild chapters
98     QStringList chaptersString;
99     currentChaps.clear();
100     for (int i = 0; i < chapterTimes.count(); i++) {
101         chaptersString.append(Timecode::getStringTimecode(chapterTimes.at(i), m_tc.fps(), true));
102         currentChaps.append(QString::number(chapterTimes.at(i)));
103     }
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 }
111
112 void DvdWizardChapters::slotRemoveChapter()
113 {
114     int ix = m_view.chapters_list->currentRow();
115     QStringList currentChaps = m_view.vob_list->itemData(m_view.vob_list->currentIndex(), Qt::UserRole + 1).toStringList();
116     currentChaps.removeAt(ix);
117
118     // Save item chapters
119     m_view.vob_list->setItemData(m_view.vob_list->currentIndex(), currentChaps, Qt::UserRole + 1);
120     // Mark item as modified
121     m_view.vob_list->setItemData(m_view.vob_list->currentIndex(), 1, Qt::UserRole + 2);
122
123     // rebuild chapters
124     QStringList chaptersString;
125     for (int i = 0; i < currentChaps.count(); i++) {
126         chaptersString.append(Timecode::getStringTimecode(currentChaps.at(i).toInt(), m_tc.fps(), true));
127     }
128     m_view.chapters_list->clear();
129     m_view.chapters_list->addItems(chaptersString);
130 }
131
132 void DvdWizardChapters::slotGoToChapter()
133 {
134     if (m_view.chapters_list->currentItem()) m_monitor->setTimePos(m_tc.reformatSeparators(m_view.chapters_list->currentItem()->text()));
135 }
136
137 void DvdWizardChapters::setVobFiles(DVDFORMAT format, const QStringList &movies, const QStringList &durations, const QStringList &chapters)
138 {
139     m_format = format;
140     QString profile = DvdWizardVob::getDvdProfile(format);
141     if (m_format == PAL || m_format == PAL_WIDE) {
142         m_tc.setFormat(25);
143     } else {
144         m_tc.setFormat(30000.0 / 1001);
145     }
146     if (m_monitor == NULL) {
147         m_monitor = new Monitor(Kdenlive::dvdMonitor, m_manager, profile, this);
148         //m_monitor->start();
149         /*QVBoxLayout *vbox = new QVBoxLayout;
150         vbox->addWidget(m_monitor);
151         m_view.monitor_frame->setLayout(vbox);*/
152         QVBoxLayout *lay = static_cast<QVBoxLayout *> (layout());
153         m_monitor->setSizePolicy(QSizePolicy ( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding));
154         lay->insertWidget(0, m_monitor, 10);
155     } else m_monitor->resetProfile(profile);
156     m_manager->activateMonitor(Kdenlive::dvdMonitor);
157
158     m_view.vob_list->blockSignals(true);
159     m_view.vob_list->clear();
160     for (int i = 0; i < movies.count(); i++) {
161         m_view.vob_list->addItem(movies.at(i), durations.at(i));
162         m_view.vob_list->setItemData(i, chapters.at(i).split(';'), Qt::UserRole + 1);
163     }
164     m_view.vob_list->blockSignals(false);
165     slotUpdateChaptersList();
166 }
167
168 QMap <QString, QString> DvdWizardChapters::chaptersData() const
169 {
170     QMap <QString, QString> result;
171     int max = m_view.vob_list->count();
172     for (int i = 0; i < max; i++) {
173         QString chapters = m_view.vob_list->itemData(i, Qt::UserRole + 1).toStringList().join(";");
174         result.insert(m_view.vob_list->itemText(i), chapters);
175     }
176     return result;
177 }
178
179 QStringList DvdWizardChapters::selectedTitles() const
180 {
181     QStringList result;
182     int max = m_view.vob_list->count();
183     for (int i = 0; i < max; i++) {
184         result.append(m_view.vob_list->itemText(i));
185         QStringList chapters = m_view.vob_list->itemData(i, Qt::UserRole + 1).toStringList();
186         for (int j = 0; j < chapters.count(); j++) {
187             result.append(Timecode::getStringTimecode(chapters.at(j).toInt(), m_tc.fps(), true));
188         }
189     }
190     return result;
191 }
192
193 QStringList DvdWizardChapters::chapters(int ix) const
194 {
195     QStringList result;
196     QStringList chapters = m_view.vob_list->itemData(ix, Qt::UserRole + 1).toStringList();
197     for (int j = 0; j < chapters.count(); j++) {
198         result.append(Timecode::getStringTimecode(chapters.at(j).toInt(), m_tc.fps(), true));
199     }
200     return result;
201 }
202
203 QStringList DvdWizardChapters::selectedTargets() const
204 {
205     QStringList result;
206     int max = m_view.vob_list->count();
207     for (int i = 0; i < max; i++) {
208         // rightJustified: fill with 0s to make menus with more than 9 buttons work (now up to 99 buttons possible)
209         result.append("jump title " + QString::number(i + 1).rightJustified(2, '0'));
210         QStringList chapters = m_view.vob_list->itemData(i, Qt::UserRole + 1).toStringList();
211         for (int j = 0; j < chapters.count(); j++) {
212             result.append("jump title " + QString::number(i + 1).rightJustified(2, '0') + " chapter " + QString::number(j + 1).rightJustified(2, '0'));
213         }
214     }
215     return result;
216 }
217
218
219 QDomElement DvdWizardChapters::toXml() const
220 {
221     QDomDocument doc;
222     QDomElement xml = doc.createElement("xml");
223     doc.appendChild(xml);
224     for (int i = 0; i < m_view.vob_list->count(); i++) {
225         QDomElement vob = doc.createElement("vob");
226         vob.setAttribute("file", m_view.vob_list->itemText(i));
227         vob.setAttribute("chapters", m_view.vob_list->itemData(i, Qt::UserRole + 1).toStringList().join(";"));
228         xml.appendChild(vob);
229     }
230     return doc.documentElement();
231 }