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