]> git.sesse.net Git - kdenlive/blob - src/dvdwizardmenu.cpp
Fix handling of several movies in dvd, improve chapters handling
[kdenlive] / src / dvdwizardmenu.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 "dvdwizardmenu.h"
21
22 #include <KDebug>
23
24
25 DvdWizardMenu::DvdWizardMenu(const QString &profile, QWidget *parent) :
26         QWizardPage(parent),
27         m_color(NULL),
28         m_safeRect(NULL)
29 {
30     m_view.setupUi(this);
31     m_view.play_text->setText(i18n("Play"));
32     m_scene = new DvdScene(this);
33     m_view.menu_preview->setScene(m_scene);
34
35     connect(m_view.create_menu, SIGNAL(toggled(bool)), m_view.menu_box, SLOT(setEnabled(bool)));
36     connect(m_view.create_menu, SIGNAL(toggled(bool)), this, SIGNAL(completeChanged()));
37
38     m_view.add_button->setIcon(KIcon("document-new"));
39     m_view.delete_button->setIcon(KIcon("trash-empty"));
40
41     m_view.add_button->setToolTip(i18n("Add new button"));
42     m_view.delete_button->setToolTip(i18n("Delete current button"));
43
44     m_view.menu_profile->addItems(QStringList() << i18n("PAL") << i18n("NTSC"));
45
46     if (profile == "dv_ntsc" || profile == "dv_ntsc_wide") {
47         m_view.menu_profile->setCurrentIndex(1);
48         m_isPal = false;
49     } else m_isPal = true;
50
51     changeProfile(m_view.menu_profile->currentIndex());
52
53     // Create color background
54     m_color = new QGraphicsRectItem(0, 0, m_width, m_height);
55     m_color->setBrush(m_view.background_color->color());
56     m_color->setZValue(2);
57     m_scene->addItem(m_color);
58
59
60     // create background image
61     m_background = new QGraphicsPixmapItem();
62     m_background->setZValue(3);
63     //m_scene->addItem(m_background);
64
65     // create safe zone rect
66     int safeW = m_width / 20;
67     int safeH = m_height / 20;
68     m_safeRect = new QGraphicsRectItem(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
69     QPen pen(Qt::red);
70     pen.setStyle(Qt::DashLine);
71     pen.setWidth(3);
72     m_safeRect->setPen(pen);
73     m_safeRect->setZValue(5);
74     m_scene->addItem(m_safeRect);
75
76     checkBackgroundType(0);
77
78     connect(m_view.menu_profile, SIGNAL(activated(int)), this, SLOT(changeProfile(int)));
79
80     // create menu button
81     DvdButton *button = new DvdButton(m_view.play_text->text());
82     QFont font = m_view.font_family->currentFont();
83     font.setPixelSize(m_view.font_size->value());
84     font.setStyleStrategy(QFont::NoAntialias);
85     button->setFont(font);
86     button->setDefaultTextColor(m_view.text_color->color());
87     button->setZValue(4);
88     button->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
89     QRectF r = button->sceneBoundingRect();
90     m_scene->addItem(button);
91     button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
92     button->setSelected(true);
93
94
95     //m_view.menu_preview->resizefitInView(0, 0, m_width, m_height);
96
97     connect(m_view.play_text, SIGNAL(textChanged(const QString &)), this, SLOT(buildButton()));
98     connect(m_view.text_color, SIGNAL(changed(const QColor &)), this, SLOT(updateColor()));
99     connect(m_view.font_size, SIGNAL(valueChanged(int)), this, SLOT(buildButton()));
100     connect(m_view.font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(buildButton()));
101     connect(m_view.background_image, SIGNAL(textChanged(const QString &)), this, SLOT(buildImage()));
102     connect(m_view.background_color, SIGNAL(changed(const QColor &)), this, SLOT(buildColor()));
103
104     connect(m_view.background_list, SIGNAL(activated(int)), this, SLOT(checkBackgroundType(int)));
105
106     connect(m_view.target_list, SIGNAL(activated(int)), this, SLOT(setButtonTarget(int)));
107     connect(m_view.back_to_menu, SIGNAL(toggled(bool)), this, SLOT(setBackToMenu(bool)));
108
109     connect(m_view.add_button, SIGNAL(pressed()), this, SLOT(addButton()));
110     connect(m_view.delete_button, SIGNAL(pressed()), this, SLOT(deleteButton()));
111     connect(m_scene, SIGNAL(selectionChanged()), this, SLOT(buttonChanged()));
112     connect(m_scene, SIGNAL(changed(const QList<QRectF> &)), this, SIGNAL(completeChanged()));
113
114     m_view.menu_box->setEnabled(false);
115
116 }
117
118 DvdWizardMenu::~DvdWizardMenu()
119 {
120     delete m_color;
121     delete m_safeRect;
122     delete m_background;
123     delete m_scene;
124 }
125
126 // virtual
127
128 bool DvdWizardMenu::isComplete() const
129 {
130     if (!m_view.create_menu->isChecked()) return true;
131     QList <int> targets;
132     QList<QGraphicsItem *> list = m_scene->items();
133     int buttonCount = 0;
134     // check that the menu buttons don't collide
135     for (int i = 0; i < list.count(); i++) {
136         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
137             buttonCount++;
138             DvdButton *button = static_cast < DvdButton* >(list.at(i));
139             QList<QGraphicsItem *> collisions = button->collidingItems();
140             if (!collisions.isEmpty()) {
141                 for (int j = 0; j < collisions.count(); j++) {
142                     if (collisions.at(j)->type() == button->type()) return false;
143                 }
144             }
145             targets.append(button->target());
146         }
147     }
148     if (buttonCount == 0) {
149         // We need at least one button
150         return false;
151     }
152
153     if (!m_view.background_image->isHidden()) {
154         // Make sure user selected a valid image / video file
155         if (!QFile::exists(m_view.background_image->url().path())) return false;
156     }
157
158     // check that we have a "Play all" entry
159     if (targets.contains(0)) return true;
160     // ... or that each video file has a button
161     for (int i = m_view.target_list->count() - 1; i > 0; i--) {
162         // If there is a vob file entry and it has no button assigned, don't allow to go further
163         if (m_view.target_list->itemIcon(i).isNull() == false && !targets.contains(i)) return false;
164     }
165     return true;
166 }
167
168 void DvdWizardMenu::setButtonTarget(int ix)
169 {
170     QList<QGraphicsItem *> list = m_scene->selectedItems();
171     for (int i = 0; i < list.count(); i++) {
172         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
173             DvdButton *button = static_cast < DvdButton* >(list.at(i));
174             button->setTarget(ix, m_view.target_list->itemData(ix).toString());
175             break;
176         }
177     }
178     emit completeChanged();
179 }
180
181 void DvdWizardMenu::setBackToMenu(bool backToMenu)
182 {
183     QList<QGraphicsItem *> list = m_scene->selectedItems();
184     for (int i = 0; i < list.count(); i++) {
185         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
186             DvdButton *button = static_cast < DvdButton* >(list.at(i));
187             button->setBackMenu(backToMenu);
188             break;
189         }
190     }
191     emit completeChanged();
192 }
193
194 void DvdWizardMenu::buttonChanged()
195 {
196     QList<QGraphicsItem *> list = m_scene->selectedItems();
197     bool foundButton = false;
198     for (int i = 0; i < list.count(); i++) {
199         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
200             m_view.play_text->blockSignals(true);
201             m_view.font_size->blockSignals(true);
202             m_view.font_family->blockSignals(true);
203             m_view.target_list->blockSignals(true);
204             m_view.back_to_menu->blockSignals(true);
205             foundButton = true;
206             m_view.tabWidget->widget(0)->setEnabled(true);
207             DvdButton *button = static_cast < DvdButton* >(list.at(i));
208             m_view.target_list->setCurrentIndex(button->target());
209             m_view.play_text->setText(button->toPlainText());
210             m_view.back_to_menu->setChecked(button->backMenu());
211             QFont font = button->font();
212             m_view.font_size->setValue(font.pixelSize());
213             m_view.font_family->setCurrentFont(font);
214             m_view.play_text->blockSignals(false);
215             m_view.font_size->blockSignals(false);
216             m_view.font_family->blockSignals(false);
217             m_view.target_list->blockSignals(false);
218             m_view.back_to_menu->blockSignals(false);
219             break;
220         }
221     }
222     if (!foundButton) m_view.tabWidget->widget(0)->setEnabled(false);
223 }
224
225 void DvdWizardMenu::addButton()
226 {
227     m_scene->clearSelection();
228     DvdButton *button = new DvdButton(m_view.play_text->text());
229     QFont font = m_view.font_family->currentFont();
230     font.setPixelSize(m_view.font_size->value());
231     font.setStyleStrategy(QFont::NoAntialias);
232     button->setFont(font);
233     button->setDefaultTextColor(m_view.text_color->color());
234     button->setZValue(4);
235     button->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
236     QRectF r = button->sceneBoundingRect();
237     m_scene->addItem(button);
238     button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
239     button->setSelected(true);
240 }
241
242 void DvdWizardMenu::deleteButton()
243 {
244     QList<QGraphicsItem *> list = m_scene->selectedItems();
245     for (int i = 0; i < list.count(); i++) {
246         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
247             delete list.at(i);
248             break;
249         }
250     }
251 }
252
253 void DvdWizardMenu::changeProfile(int ix)
254 {
255     switch (ix) {
256     case 1:
257         m_width = 720;
258         m_height = 480;
259         m_isPal = false;
260         updatePreview();
261         break;
262     default:
263         m_width = 720;
264         m_height = 576;
265         m_isPal = true;
266         updatePreview();
267         break;
268     }
269 }
270
271 void DvdWizardMenu::updatePreview()
272 {
273     m_scene->setProfile(m_width, m_height);
274     QMatrix matrix;
275     matrix.scale(0.5, 0.5);
276     m_view.menu_preview->setMatrix(matrix);
277     m_view.menu_preview->setMinimumSize(m_width / 2 + 4, m_height / 2 + 8);
278
279     if (m_color) m_color->setRect(0, 0, m_width, m_height);
280
281     int safeW = m_width / 20;
282     int safeH = m_height / 20;
283     if (m_safeRect) m_safeRect->setRect(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
284 }
285
286 void DvdWizardMenu::setTargets(QStringList list, QStringList targetlist)
287 {
288     m_view.target_list->clear();
289     m_view.target_list->addItem(i18n("Play All"), "title 1");
290     int movieCount = 0;
291     for (int i = 0; i < list.count(); i++) {
292         if (targetlist.at(i).contains("chapter"))
293             m_view.target_list->addItem(list.at(i), targetlist.at(i));
294         else {
295             m_view.target_list->addItem(KIcon("video-x-generic"), list.at(i), targetlist.at(i));
296             movieCount++;
297         }
298     }
299     m_view.back_to_menu->setHidden(movieCount == 1);
300 }
301
302 void DvdWizardMenu::checkBackgroundType(int ix)
303 {
304     if (ix == 0) {
305         m_view.background_color->setVisible(true);
306         m_view.background_image->setVisible(false);
307         m_scene->removeItem(m_background);
308     } else {
309         m_view.background_color->setVisible(false);
310         m_view.background_image->setVisible(true);
311         if (ix == 1) {
312             m_view.background_image->setFilter("*");
313             m_scene->addItem(m_background);
314         } else {
315             m_scene->removeItem(m_background);
316             m_view.background_image->setFilter("video/mpeg");
317         }
318     }
319 }
320
321 void DvdWizardMenu::buildColor()
322 {
323     m_color->setBrush(m_view.background_color->color());
324 }
325
326 void DvdWizardMenu::buildImage()
327 {
328     emit completeChanged();
329     if (m_view.background_image->url().isEmpty()) {
330         m_scene->removeItem(m_background);
331         return;
332     }
333     QPixmap pix;
334     if (!pix.load(m_view.background_image->url().path())) {
335         m_scene->removeItem(m_background);
336         return;
337     }
338     pix = pix.scaled(m_width, m_height);
339     m_background->setPixmap(pix);
340     if (m_view.background_list->currentIndex() == 1) m_scene->addItem(m_background);
341 }
342
343 void DvdWizardMenu::checkBackground()
344 {
345     if (m_view.background_list->currentIndex() != 1) {
346         m_scene->removeItem(m_background);
347     } else {
348         m_scene->addItem(m_background);
349     }
350 }
351
352 void DvdWizardMenu::buildButton()
353 {
354     DvdButton *button = NULL;
355     QList<QGraphicsItem *> list = m_scene->selectedItems();
356     for (int i = 0; i < list.count(); i++) {
357         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
358             button = static_cast < DvdButton* >(list.at(i));
359             break;
360         }
361     }
362     if (button == NULL) return;
363     button->setPlainText(m_view.play_text->text());
364     QFont font = m_view.font_family->currentFont();
365     font.setPixelSize(m_view.font_size->value());
366     font.setStyleStrategy(QFont::NoAntialias);
367     button->setFont(font);
368     button->setDefaultTextColor(m_view.text_color->color());
369 }
370
371 void DvdWizardMenu::updateColor()
372 {
373     updateColor(m_view.text_color->color());
374     m_view.menu_preview->viewport()->update();
375 }
376
377 void DvdWizardMenu::updateColor(QColor c)
378 {
379     DvdButton *button = NULL;
380     QList<QGraphicsItem *> list = m_scene->items();
381     for (int i = 0; i < list.count(); i++) {
382         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
383             button = static_cast < DvdButton* >(list.at(i));
384             button->setDefaultTextColor(c);
385         }
386     }
387 }
388
389
390 void DvdWizardMenu::createButtonImages(const QString &img1, const QString &img2, const QString &img3)
391 {
392     if (m_view.create_menu->isChecked()) {
393         m_scene->clearSelection();
394         QImage img(m_width, m_height, QImage::Format_ARGB32);
395         QPainter p(&img);
396         p.setRenderHints(QPainter::Antialiasing, false);
397         p.setRenderHints(QPainter::TextAntialiasing, false);
398         m_scene->removeItem(m_safeRect);
399         m_scene->removeItem(m_color);
400         m_scene->removeItem(m_background);
401         m_scene->render(&p, QRectF(0, 0, m_width, m_height));
402         p.end();
403         img.setNumColors(4);
404         img.save(img1);
405         /*QImage saved;
406         if (m_view.menu_profile->currentIndex() < 2)
407             saved = img.scaled(720, 576);
408         else saved = img.scaled(720, 480);
409         saved.setNumColors(4);
410         saved.save(img1);*/
411
412         updateColor(m_view.selected_color->color());
413         p.begin(&img);
414         p.setRenderHints(QPainter::Antialiasing, false);
415         p.setRenderHints(QPainter::TextAntialiasing, false);
416         m_scene->render(&p, QRectF(0, 0, m_width, m_height));
417         p.end();
418         /*        if (m_view.menu_profile->currentIndex() < 2)
419                     saved = img.scaled(720, 576);
420                 else saved = img.scaled(720, 480);
421                 saved.setNumColors(4);
422                 saved.save(img2);*/
423         img.setNumColors(4);
424         img.save(img2);
425
426
427         updateColor(m_view.highlighted_color->color());
428         p.begin(&img);
429         p.setRenderHints(QPainter::Antialiasing, false);
430         p.setRenderHints(QPainter::TextAntialiasing, false);
431         m_scene->render(&p, QRectF(0, 0, m_width, m_height));
432         p.end();
433         /*if (m_view.menu_profile->currentIndex() < 2)
434             saved = img.scaled(720, 576);
435         else saved = img.scaled(720, 480);
436         saved.setNumColors(4);
437         saved.save(img3);*/
438         img.setNumColors(4);
439         img.save(img3);
440
441         updateColor();
442
443         m_scene->addItem(m_safeRect);
444         m_scene->addItem(m_color);
445         if (m_view.background_list->currentIndex() == 1) m_scene->addItem(m_background);
446     }
447 }
448
449
450 void DvdWizardMenu::createBackgroundImage(const QString &img1)
451 {
452     QImage img;
453     if (m_view.background_list->currentIndex() == 0) {
454         // color background
455         if (m_isPal)
456             img = QImage(768, 576, QImage::Format_ARGB32);
457         else
458             img = QImage(720, 540, QImage::Format_ARGB32);
459         img.fill(m_view.background_color->color().rgb());
460     } else if (m_view.background_list->currentIndex() == 1) {
461         img.load(m_view.background_image->url().path());
462         if (m_isPal) {
463             if (img.width() != 768 || img.height() != 576)
464                 img = img.scaled(768, 576, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
465         } else {
466             if (img.width() != 720 || img.height() != 540)
467                 img = img.scaled(720, 540, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
468         }
469     } else return;
470     img.save(img1);
471 }
472
473 bool DvdWizardMenu::createMenu() const
474 {
475     return m_view.create_menu->isChecked();
476 }
477
478 bool DvdWizardMenu::menuMovie() const
479 {
480     return m_view.background_list->currentIndex() == 2;
481 }
482
483 QString DvdWizardMenu::menuMoviePath() const
484 {
485     return m_view.background_image->url().path();
486 }
487
488
489 QMap <QString, QRect> DvdWizardMenu::buttonsInfo()
490 {
491     QMap <QString, QRect> info;
492     QList<QGraphicsItem *> list = m_scene->items();
493     for (int i = 0; i < list.count(); i++) {
494         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
495             DvdButton *button = static_cast < DvdButton* >(list.at(i));
496             QRect r = list.at(i)->sceneBoundingRect().toRect();
497             // Make sure y1 is not odd (requested by spumux)
498             if (r.height() % 2 == 1) r.setHeight(r.height() + 1);
499             if (r.y() % 2 == 1) r.setY(r.y() - 1);
500             QString command = button->command();
501             if (button->backMenu()) command.prepend("g1 = 999;");
502             info.insertMulti(command, r);
503         }
504     }
505     return info;
506 }
507
508 bool DvdWizardMenu::isPalMenu() const
509 {
510     return m_isPal;
511 }