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