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