]> git.sesse.net Git - kdenlive/blob - src/dvdwizardmenu.cpp
Rewrote DVD creation, should now support correctly 4:3 and 16:9 menus, letterbox...
[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 #include "kdenlivesettings.h"
22
23 #include <KDebug>
24 #include <KColorScheme>
25
26 #if KDE_IS_VERSION(4,6,0)
27 #include <QGraphicsDropShadowEffect>
28 #endif
29
30
31 #include "kthumb.h"
32
33 DvdWizardMenu::DvdWizardMenu(DVDFORMAT format, QWidget *parent) :
34         QWizardPage(parent),
35         m_color(NULL),
36         m_safeRect(NULL),
37         m_finalSize(720, 576),
38         m_movieLength(-1)
39 {
40     m_view.setupUi(this);
41     m_view.play_text->setText(i18n("Play"));
42     m_scene = new DvdScene(this);
43     m_view.menu_preview->setScene(m_scene);
44     m_view.menu_preview->setMouseTracking(true);
45     connect(m_view.create_menu, SIGNAL(toggled(bool)), m_view.menu_box, SLOT(setEnabled(bool)));
46     connect(m_view.create_menu, SIGNAL(toggled(bool)), this, SIGNAL(completeChanged()));
47     
48 #if KDE_IS_VERSION(4,7,0)
49     m_menuMessage = new KMessageWidget;
50     QGridLayout *s =  static_cast <QGridLayout*> (layout());
51     s->addWidget(m_menuMessage, 7, 0, 1, -1);
52     m_menuMessage->hide();
53     m_view.error_message->hide();
54 #endif
55
56     m_view.add_button->setIcon(KIcon("document-new"));
57     m_view.delete_button->setIcon(KIcon("trash-empty"));
58     m_view.zoom_button->setIcon(KIcon("zoom-in"));
59     m_view.unzoom_button->setIcon(KIcon("zoom-out"));
60
61     m_view.add_button->setToolTip(i18n("Add new button"));
62     m_view.delete_button->setToolTip(i18n("Delete current button"));
63
64     changeProfile(format);
65
66
67     // Create color background
68     m_color = new QGraphicsRectItem(0, 0, m_width, m_height);
69     m_color->setBrush(m_view.background_color->color());
70     m_color->setZValue(2);
71     m_scene->addItem(m_color);
72
73
74     // create background image
75     m_background = new QGraphicsPixmapItem();
76     m_background->setZValue(3);
77     //m_scene->addItem(m_background);
78
79     // create safe zone rect
80     int safeW = m_width / 20;
81     int safeH = m_height / 20;
82     m_safeRect = new QGraphicsRectItem(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
83     QPen pen(Qt::red);
84     pen.setStyle(Qt::DashLine);
85     pen.setWidth(3);
86     m_safeRect->setPen(pen);
87     m_safeRect->setZValue(5);
88     m_scene->addItem(m_safeRect);
89     checkBackgroundType(0);
90
91     // create menu button
92     DvdButton *button = new DvdButton(m_view.play_text->text());
93     QFont font = m_view.font_family->currentFont();
94     font.setPixelSize(m_view.font_size->value());
95     //font.setStyleStrategy(QFont::NoAntialias);
96 #if KDE_IS_VERSION(4,6,0)
97     if (m_view.use_shadow->isChecked()) {
98         QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
99         shadow->setBlurRadius(7);
100         shadow->setOffset(4, 4);
101         button->setGraphicsEffect(shadow);
102     }
103     connect(m_view.use_shadow, SIGNAL(stateChanged(int)), this, SLOT(slotEnableShadows(int)));
104 #elif KDE_IS_VERSION(4,6,0)
105     m_view.use_shadow->setHidden(true);
106 #endif
107     button->setFont(font);
108     button->setDefaultTextColor(m_view.text_color->color());
109     button->setZValue(4);
110     QRectF r = button->sceneBoundingRect();
111     m_scene->addItem(button);
112     button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
113     button->setSelected(true);
114
115
116     //m_view.menu_preview->resizefitInView(0, 0, m_width, m_height);
117
118     connect(m_view.play_text, SIGNAL(textChanged(const QString &)), this, SLOT(buildButton()));
119     connect(m_view.text_color, SIGNAL(changed(const QColor &)), this, SLOT(updateColor()));
120     connect(m_view.font_size, SIGNAL(valueChanged(int)), this, SLOT(buildButton()));
121     connect(m_view.font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(buildButton()));
122     connect(m_view.background_image, SIGNAL(textChanged(const QString &)), this, SLOT(buildImage()));
123     connect(m_view.background_color, SIGNAL(changed(const QColor &)), this, SLOT(buildColor()));
124
125     connect(m_view.background_list, SIGNAL(activated(int)), this, SLOT(checkBackgroundType(int)));
126
127     connect(m_view.target_list, SIGNAL(activated(int)), this, SLOT(setButtonTarget(int)));
128     connect(m_view.back_to_menu, SIGNAL(toggled(bool)), this, SLOT(setBackToMenu(bool)));
129
130     connect(m_view.add_button, SIGNAL(pressed()), this, SLOT(addButton()));
131     connect(m_view.delete_button, SIGNAL(pressed()), this, SLOT(deleteButton()));
132     connect(m_view.zoom_button, SIGNAL(pressed()), this, SLOT(slotZoom()));
133     connect(m_view.unzoom_button, SIGNAL(pressed()), this, SLOT(slotUnZoom()));
134     connect(m_scene, SIGNAL(selectionChanged()), this, SLOT(buttonChanged()));
135     connect(m_scene, SIGNAL(changed(const QList<QRectF> &)), this, SIGNAL(completeChanged()));
136
137     // red background for error message
138     KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Window, KSharedConfig::openConfig(KdenliveSettings::colortheme()));
139     QPalette p = m_view.error_message->palette();
140     p.setColor(QPalette::Background, scheme.background(KColorScheme::NegativeBackground).color());
141     m_view.error_message->setAutoFillBackground(true);
142     m_view.error_message->setPalette(p);
143
144     m_view.menu_box->setEnabled(false);
145
146 }
147
148 DvdWizardMenu::~DvdWizardMenu()
149 {
150     delete m_color;
151     delete m_safeRect;
152     delete m_background;
153     delete m_scene;
154 }
155
156 void DvdWizardMenu::slotEnableShadows(int enable)
157 {
158 #if KDE_IS_VERSION(4,6,0)
159     QList<QGraphicsItem *> list = m_scene->items();
160     for (int i = 0; i < list.count(); i++) {
161         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
162             if (enable) {
163                 QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
164                 shadow->setBlurRadius(7);
165                 shadow->setOffset(4, 4);
166                 list.at(i)->setGraphicsEffect(shadow);
167             }
168             else list.at(i)->setGraphicsEffect(NULL);
169         }
170     }
171 #endif
172 }
173
174 // virtual
175 bool DvdWizardMenu::isComplete() const
176 {
177     m_view.error_message->setHidden(true);
178     if (!m_view.create_menu->isChecked()) return true;
179     QList <int> targets;
180     QList<QGraphicsItem *> list = m_scene->items();
181     int buttonCount = 0;
182     // check that the menu buttons don't collide
183     for (int i = 0; i < list.count(); i++) {
184         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
185             buttonCount++;
186             DvdButton *button = static_cast < DvdButton* >(list.at(i));
187             QList<QGraphicsItem *> collisions = button->collidingItems();
188             if (!collisions.isEmpty()) {
189                 for (int j = 0; j < collisions.count(); j++) {
190                     if (collisions.at(j)->type() == button->type()) {
191 #if KDE_IS_VERSION(4,7,0)
192                         m_menuMessage->setText(i18n("Buttons overlapping"));
193                         m_menuMessage->setMessageType(KMessageWidget::Warning);
194                         m_menuMessage->animatedShow();
195 #else
196                         m_view.error_message->setText(i18n("Buttons overlapping"));
197                         m_view.error_message->setHidden(false);
198 #endif
199                         return false;
200                     }
201                 }
202             }
203             targets.append(button->target());
204         }
205     }
206     if (buttonCount == 0) {
207         //We need at least one button
208 #if KDE_IS_VERSION(4,7,0)
209         m_menuMessage->setText(i18n("No button in menu"));
210         m_menuMessage->setMessageType(KMessageWidget::Warning);
211         m_menuMessage->animatedShow();
212 #else
213         m_view.error_message->setText(i18n("No button in menu"));
214         m_view.error_message->setHidden(false);
215 #endif
216         return false;
217     }
218
219     if (!m_view.background_image->isHidden()) {
220         // Make sure user selected a valid image / video file
221         if (!QFile::exists(m_view.background_image->url().path())) {
222 #if KDE_IS_VERSION(4,7,0)
223             m_menuMessage->setText(i18n("Missing background image"));
224             m_menuMessage->setMessageType(KMessageWidget::Warning);
225             m_menuMessage->animatedShow();
226 #else
227             m_view.error_message->setText(i18n("Missing background image"));
228             m_view.error_message->setHidden(false);
229 #endif
230             return false;
231         }
232     }
233     
234 #if KDE_IS_VERSION(4,7,0)
235     m_menuMessage->animatedHide();
236 #endif
237
238     // check that we have a "Play all" entry
239     if (targets.contains(0)) return true;
240     // ... or that each video file has a button
241     for (int i = m_view.target_list->count() - 1; i > 0; i--) {
242         // If there is a vob file entry and it has no button assigned, don't allow to go further
243         if (m_view.target_list->itemIcon(i).isNull() == false && !targets.contains(i)) {
244 #if KDE_IS_VERSION(4,7,0)
245             m_menuMessage->setText(i18n("No menu entry for %1", m_view.target_list->itemText(i)));
246             m_menuMessage->setMessageType(KMessageWidget::Warning);
247             m_menuMessage->animatedShow();
248 #else
249             m_view.error_message->setText(i18n("No menu entry for %1", m_view.target_list->itemText(i)));
250             m_view.error_message->setHidden(false);
251 #endif
252             return false;
253         }
254     }
255     return true;
256 }
257
258 void DvdWizardMenu::setButtonTarget(int ix)
259 {
260     QList<QGraphicsItem *> list = m_scene->selectedItems();
261     for (int i = 0; i < list.count(); i++) {
262         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
263             DvdButton *button = static_cast < DvdButton* >(list.at(i));
264             button->setTarget(ix, m_view.target_list->itemData(ix).toString());
265             break;
266         }
267     }
268     emit completeChanged();
269 }
270
271 void DvdWizardMenu::setBackToMenu(bool backToMenu)
272 {
273     QList<QGraphicsItem *> list = m_scene->selectedItems();
274     for (int i = 0; i < list.count(); i++) {
275         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
276             DvdButton *button = static_cast < DvdButton* >(list.at(i));
277             button->setBackMenu(backToMenu);
278             break;
279         }
280     }
281     emit completeChanged();
282 }
283
284 void DvdWizardMenu::buttonChanged()
285 {
286     QList<QGraphicsItem *> list = m_scene->selectedItems();
287     bool foundButton = false;
288     for (int i = 0; i < list.count(); i++) {
289         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
290             m_view.play_text->blockSignals(true);
291             m_view.font_size->blockSignals(true);
292             m_view.font_family->blockSignals(true);
293             m_view.target_list->blockSignals(true);
294             m_view.back_to_menu->blockSignals(true);
295             foundButton = true;
296             m_view.tabWidget->widget(0)->setEnabled(true);
297             DvdButton *button = static_cast < DvdButton* >(list.at(i));
298             m_view.target_list->setCurrentIndex(button->target());
299             m_view.play_text->setText(button->toPlainText());
300             m_view.back_to_menu->setChecked(button->backMenu());
301             QFont font = button->font();
302             m_view.font_size->setValue(font.pixelSize());
303             m_view.font_family->setCurrentFont(font);
304             m_view.play_text->blockSignals(false);
305             m_view.font_size->blockSignals(false);
306             m_view.font_family->blockSignals(false);
307             m_view.target_list->blockSignals(false);
308             m_view.back_to_menu->blockSignals(false);
309             break;
310         }
311     }
312     if (!foundButton) m_view.tabWidget->widget(0)->setEnabled(false);
313 }
314
315 void DvdWizardMenu::addButton()
316 {
317     m_scene->clearSelection();
318     DvdButton *button = new DvdButton(m_view.play_text->text());
319     QFont font = m_view.font_family->currentFont();
320     font.setPixelSize(m_view.font_size->value());
321 #if KDE_IS_VERSION(4,6,0)
322     if (m_view.use_shadow->isChecked()) {
323         QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
324         shadow->setBlurRadius(7);
325         shadow->setOffset(4, 4);
326         button->setGraphicsEffect(shadow);
327     }
328 #endif
329     //font.setStyleStrategy(QFont::NoAntialias);
330     button->setFont(font);
331     button->setDefaultTextColor(m_view.text_color->color());
332     button->setZValue(4);
333     QRectF r = button->sceneBoundingRect();
334     m_scene->addItem(button);
335     button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
336     button->setSelected(true);
337 }
338
339 void DvdWizardMenu::deleteButton()
340 {
341     QList<QGraphicsItem *> list = m_scene->selectedItems();
342     for (int i = 0; i < list.count(); i++) {
343         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
344             delete list.at(i);
345             break;
346         }
347     }
348 }
349
350 void DvdWizardMenu::changeProfile(DVDFORMAT format)
351 {
352     m_format = format;
353     switch (m_format) {
354         case PAL_WIDE:
355             m_finalSize = QSize(720, 576);
356             m_width = 1024;
357             m_height = 576;
358             break;
359         case NTSC_WIDE:
360             m_finalSize = QSize(720, 480);
361             m_width = 853;
362             m_height = 480;
363             break;
364         case NTSC:
365             m_finalSize = QSize(720, 480);
366             m_width = 640;
367             m_height = 480;
368             break;
369         default:
370             m_finalSize = QSize(720, 576);
371             m_width = 768;
372             m_height = 576;
373     }
374     updatePreview();
375 }
376
377 void DvdWizardMenu::updatePreview()
378 {
379     m_scene->setProfile(m_width, m_height);
380     QMatrix matrix;
381     matrix.scale(0.5, 0.5);
382     m_view.menu_preview->setMatrix(matrix);
383     m_view.menu_preview->setMinimumSize(m_width / 2 + 4, m_height / 2 + 8);
384
385     if (m_color) m_color->setRect(0, 0, m_width, m_height);
386
387     int safeW = m_width / 20;
388     int safeH = m_height / 20;
389     if (m_safeRect) m_safeRect->setRect(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
390 }
391
392 void DvdWizardMenu::setTargets(QStringList list, QStringList targetlist)
393 {
394     m_view.target_list->clear();
395     m_view.target_list->addItem(i18n("Play All"), "jump title 1");
396     int movieCount = 0;
397     for (int i = 0; i < list.count(); i++) {
398         if (targetlist.at(i).contains("chapter"))
399             m_view.target_list->addItem(list.at(i), targetlist.at(i));
400         else {
401             m_view.target_list->addItem(KIcon("video-x-generic"), list.at(i), targetlist.at(i));
402             movieCount++;
403         }
404     }
405     m_view.back_to_menu->setHidden(movieCount == 1);
406 }
407
408 void DvdWizardMenu::checkBackgroundType(int ix)
409 {
410     if (ix == 0) {
411         m_view.background_color->setVisible(true);
412         m_view.background_image->setVisible(false);
413         m_view.loop_movie->setVisible(false);
414         if (m_background->scene() != 0) m_scene->removeItem(m_background);
415     } else {
416         m_view.background_color->setVisible(false);
417         m_view.background_image->setVisible(true);
418         if (ix == 1) {
419             m_view.background_image->clear();
420             m_view.background_image->setFilter("*");
421             if (m_background->scene() != 0) m_scene->removeItem(m_background);
422             m_view.loop_movie->setVisible(false);
423         } else {
424             if (m_background->scene() != 0) m_scene->removeItem(m_background);
425             m_view.background_image->clear();
426             m_view.background_image->setFilter("video/mpeg");
427             m_view.loop_movie->setVisible(true);
428         }
429     }
430 }
431
432 void DvdWizardMenu::buildColor()
433 {
434     m_color->setBrush(m_view.background_color->color());
435 }
436
437 void DvdWizardMenu::buildImage()
438 {
439     emit completeChanged();
440     if (m_view.background_image->url().isEmpty()) {
441         if (m_background->scene() != 0) m_scene->removeItem(m_background);
442         return;
443     }
444     QPixmap pix;
445
446     if (m_view.background_list->currentIndex() == 1) {
447         // image background
448         if (!pix.load(m_view.background_image->url().path())) {
449             if (m_background->scene() != 0) m_scene->removeItem(m_background);
450             return;
451         }
452         pix = pix.scaled(m_width, m_height);
453     } else if (m_view.background_list->currentIndex() == 2) {
454         // video background
455         m_movieLength = -1;
456         Mlt::Profile profile;
457         profile.set_explicit(false);
458         Mlt::Producer *producer = new Mlt::Producer(profile, m_view.background_image->url().path().toUtf8().data());
459         if (producer && producer->is_valid()) {
460             pix = QPixmap::fromImage(KThumb::getFrame(producer, 0, m_finalSize.width(), m_width, m_height));
461             m_movieLength = producer->get_length();
462         }
463         if (producer) delete producer;
464     }
465     m_background->setPixmap(pix);
466     m_scene->addItem(m_background);
467 }
468
469 void DvdWizardMenu::checkBackground()
470 {
471     if (m_view.background_list->currentIndex() != 1) {
472         if (m_background->scene() != 0) m_scene->removeItem(m_background);
473     } else {
474         m_scene->addItem(m_background);
475     }
476 }
477
478 void DvdWizardMenu::buildButton()
479 {
480     DvdButton *button = NULL;
481     QList<QGraphicsItem *> list = m_scene->selectedItems();
482     for (int i = 0; i < list.count(); i++) {
483         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
484             button = static_cast < DvdButton* >(list.at(i));
485             break;
486         }
487     }
488     if (button == NULL) return;
489     button->setPlainText(m_view.play_text->text());
490     QFont font = m_view.font_family->currentFont();
491     font.setPixelSize(m_view.font_size->value());
492     //font.setStyleStrategy(QFont::NoAntialias);
493     button->setFont(font);
494     button->setDefaultTextColor(m_view.text_color->color());
495 }
496
497 void DvdWizardMenu::updateColor()
498 {
499     updateColor(m_view.text_color->color());
500     m_view.menu_preview->viewport()->update();
501 }
502
503 void DvdWizardMenu::prepareUnderLines()
504 {
505     QList<QGraphicsItem *> list = m_scene->items();
506     for (int i = 0; i < list.count(); i++) {
507         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
508             QRectF r = list.at(i)->sceneBoundingRect();
509             int bottom = r.bottom() - 1;
510             if (bottom % 2 == 1) bottom = bottom - 1;
511             int underlineHeight = r.height() / 10;
512             if (underlineHeight % 2 == 1) underlineHeight = underlineHeight - 1;
513             underlineHeight = qMin(underlineHeight, 10);
514             underlineHeight = qMax(underlineHeight, 2);
515             r.setTop(bottom - underlineHeight);
516             r.adjust(2, 0, -2, 0);
517             QGraphicsRectItem *underline = new QGraphicsRectItem(r);
518             underline->setData(Qt::UserRole, QString("underline"));
519             m_scene->addItem(underline);
520             list.at(i)->setVisible(false);
521         }
522     }
523 }
524
525 void DvdWizardMenu::resetUnderLines()
526 {
527     QList<QGraphicsItem *> list = m_scene->items();
528     QList<QGraphicsItem *> toDelete;
529     for (int i = 0; i < list.count(); i++) {
530         if (list.at(i)->data(Qt::UserRole).toString() == "underline") {
531             toDelete.append(list.at(i));
532         }
533         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
534             list.at(i)->setVisible(true);
535         }
536     }
537     while (!toDelete.isEmpty()) {
538         QGraphicsItem *item = toDelete.takeFirst();
539         delete item;
540     }
541 }
542
543 void DvdWizardMenu::updateUnderlineColor(QColor c)
544 {
545     QList<QGraphicsItem *> list = m_scene->items();
546     for (int i = 0; i < list.count(); i++) {
547         if (list.at(i)->data(Qt::UserRole).toString() == "underline") {
548             QGraphicsRectItem *underline = static_cast < QGraphicsRectItem* >(list.at(i));
549             underline->setPen(Qt::NoPen);
550             c.setAlpha(150);
551             underline->setBrush(c);
552         }
553     }
554 }
555
556
557 void DvdWizardMenu::updateColor(QColor c)
558 {
559     DvdButton *button = NULL;
560     QList<QGraphicsItem *> list = m_scene->items();
561     for (int i = 0; i < list.count(); i++) {
562         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
563             button = static_cast < DvdButton* >(list.at(i));
564             button->setDefaultTextColor(c);
565         }
566     }
567 }
568
569
570 void DvdWizardMenu::createButtonImages(const QString &img1, const QString &img2, const QString &img3, bool letterbox)
571 {
572     if (m_view.create_menu->isChecked()) {
573         m_scene->clearSelection();
574         QRectF source(0, 0, m_width, m_height);
575         QRectF target;
576         if (!letterbox) target = QRectF(0, 0, m_finalSize.width(), m_finalSize.height());
577         else {
578             // Scale the button images to fit a letterbox image
579             double factor = (double) m_width / m_finalSize.width();
580             int letterboxHeight = m_height / factor;
581             target = QRectF(0, (m_finalSize.height() - letterboxHeight) / 2, m_finalSize.width(), letterboxHeight);
582         }
583         if (m_safeRect->scene() != 0) m_scene->removeItem(m_safeRect);
584         if (m_color->scene() != 0) m_scene->removeItem(m_color);
585         if (m_background->scene() != 0) m_scene->removeItem(m_background);
586         prepareUnderLines();
587 #if QT_VERSION >= 0x040800
588         QImage img(m_finalSize.width(), m_finalSize.height(), QImage::Format_ARGB32);
589         img.fill(Qt::transparent);
590         updateUnderlineColor(m_view.text_color->color());
591 #else
592         QImage img(m_finalSize.width(), m_finalSize.height(), QImage::Format_Mono);
593         img.fill(Qt::white);
594         updateUnderlineColor(Qt::black);
595 #endif
596         QPainter p(&img);
597         //p.setRenderHints(QPainter::Antialiasing, false);
598         //p.setRenderHints(QPainter::TextAntialiasing, false);
599         m_scene->render(&p, target, source, Qt::IgnoreAspectRatio);
600         p.end();
601 #if QT_VERSION >= 0x040800
602 #elif QT_VERSION >= 0x040600 
603         img.setColor(0, m_view.text_color->color().rgb());
604         img.setColor(1, qRgba(0,0,0,0));
605 #else
606         img.setNumColors(4);
607 #endif
608         img.save(img1);
609
610 #if QT_VERSION >= 0x040800
611         img.fill(Qt::transparent);
612         updateUnderlineColor(m_view.highlighted_color->color());
613 #else
614         img.fill(Qt::white);
615 #endif
616         p.begin(&img);
617         //p.setRenderHints(QPainter::Antialiasing, false);
618         //p.setRenderHints(QPainter::TextAntialiasing, false);
619         m_scene->render(&p, target, source, Qt::IgnoreAspectRatio);
620         p.end();
621 #if QT_VERSION >= 0x040800
622 #elif QT_VERSION >= 0x040600
623         img.setColor(0, m_view.highlighted_color->color().rgb());
624         img.setColor(1, qRgba(0,0,0,0));
625 #else
626         img.setNumColors(4);
627 #endif
628         img.save(img3);
629
630 #if QT_VERSION >= 0x040800
631         img.fill(Qt::transparent);
632         updateUnderlineColor(m_view.selected_color->color());
633 #else
634         img.fill(Qt::white);
635 #endif
636         p.begin(&img);
637         //p.setRenderHints(QPainter::Antialiasing, false);
638         //p.setRenderHints(QPainter::TextAntialiasing, false);
639         m_scene->render(&p, target, source, Qt::IgnoreAspectRatio);
640         p.end();
641 #if QT_VERSION >= 0x040800
642 #elif QT_VERSION >= 0x040600
643         img.setColor(0, m_view.selected_color->color().rgb());
644         img.setColor(1, qRgba(0,0,0,0));
645 #else
646         img.setNumColors(4);
647 #endif
648         img.save(img2);
649         resetUnderLines();
650         m_scene->addItem(m_safeRect);
651         m_scene->addItem(m_color);
652         if (m_view.background_list->currentIndex() > 0) m_scene->addItem(m_background);
653     }
654 }
655
656
657 void DvdWizardMenu::createBackgroundImage(const QString &overlayMenu, const QString &img1)
658 {
659     m_scene->clearSelection();
660     if (m_safeRect->scene() != 0) m_scene->removeItem(m_safeRect);
661     bool showBg = false;
662     QImage img(m_width, m_height, QImage::Format_ARGB32);
663     if (menuMovie() && m_background->scene() != 0) {
664         showBg = true;
665         m_scene->removeItem(m_background);
666         if (m_color->scene() != 0) m_scene->removeItem(m_color);
667         if (m_safeRect->scene() != 0) m_scene->removeItem(m_safeRect);
668         img.fill(Qt::transparent);
669     }
670     updateColor(m_view.text_color->color());
671     QPainter p(&img);
672     p.setRenderHints(QPainter::Antialiasing, true);
673     p.setRenderHints(QPainter::TextAntialiasing, true);
674     m_scene->render(&p, QRectF(0, 0, img.width(), img.height()));
675     p.end();
676     img.save(img1);
677     m_scene->addItem(m_safeRect);
678     if (showBg) {
679         m_scene->addItem(m_background);
680         m_scene->addItem(m_color);
681     }
682     return;
683         
684   
685     /*QImage img;
686     if (m_view.background_list->currentIndex() == 0) {
687         // color background
688         if (m_isPal)
689             img = QImage(768, 576, QImage::Format_ARGB32);
690         else
691             img = QImage(720, 540, QImage::Format_ARGB32);
692         img.fill(m_view.background_color->color().rgb());
693     } else if (m_view.background_list->currentIndex() == 1) {
694         img.load(m_view.background_image->url().path());
695         if (m_isPal) {
696             if (img.width() != 768 || img.height() != 576)
697                 img = img.scaled(768, 576, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
698         } else {
699             if (img.width() != 720 || img.height() != 540)
700                 img = img.scaled(720, 540, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
701         }
702     } else return;
703     // Overlay Normal menu
704     QImage menu(overlayMenu);
705     QPainter p(&img);
706     QRectF target(0, 0, img.width(), img.height());
707     QRectF src(0, 0, menu.width(), menu.height());
708     p.drawImage(target, menu, src);
709     p.end();
710     img.save(img1);*/
711 }
712
713 bool DvdWizardMenu::createMenu() const
714 {
715     return m_view.create_menu->isChecked();
716 }
717
718 bool DvdWizardMenu::loopMovie() const
719 {
720     return m_view.loop_movie->isChecked();
721 }
722
723 bool DvdWizardMenu::menuMovie() const
724 {
725     return m_view.background_list->currentIndex() == 2;
726 }
727
728 QString DvdWizardMenu::menuMoviePath() const
729 {
730     return m_view.background_image->url().path();
731 }
732
733 int DvdWizardMenu::menuMovieLength() const
734 {
735   return m_movieLength;
736 }
737
738
739 QMap <QString, QRect> DvdWizardMenu::buttonsInfo(bool letterbox)
740 {
741     QMap <QString, QRect> info;
742     QList<QGraphicsItem *> list = m_scene->items();
743     double ratiox = (double) m_finalSize.width() / m_width;
744     double ratioy = 1;
745     int offset = 0;
746     if (letterbox) {
747         int letterboxHeight = m_height * ratiox;
748         ratioy = (double) letterboxHeight / m_finalSize.height();
749         offset = (m_finalSize.height() - letterboxHeight) / 2;
750     }
751     for (int i = 0; i < list.count(); i++) {
752         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
753             DvdButton *button = static_cast < DvdButton* >(list.at(i));
754             QRectF r = button->sceneBoundingRect();
755             QRect adjustedRect(r.x() * ratiox, offset + r.y() * ratioy, r.width() * ratiox, r.height() * ratioy);
756             // Make sure y1 is not odd (requested by spumux)
757             if (adjustedRect.height() % 2 == 1) adjustedRect.setHeight(adjustedRect.height() + 1);
758             if (adjustedRect.y() % 2 == 1) adjustedRect.setY(adjustedRect.y() - 1);
759             QString command = button->command();
760             if (button->backMenu()) command.prepend("g1 = 999;");
761             info.insertMulti(command, adjustedRect);
762         }
763     }
764     return info;
765 }
766
767 QDomElement DvdWizardMenu::toXml() const
768 {
769     QDomDocument doc;
770     QDomElement xml = doc.createElement("menu");
771     doc.appendChild(xml);
772     xml.setAttribute("enabled", m_view.create_menu->isChecked());
773     if (m_view.background_list->currentIndex() == 0) {
774         // Color bg
775         xml.setAttribute("background_color", m_view.background_color->color().name());
776     } else if (m_view.background_list->currentIndex() == 1) {
777         // Image bg
778         xml.setAttribute("background_image", m_view.background_image->url().path());
779     } else {
780         // Video bg
781         xml.setAttribute("background_video", m_view.background_image->url().path());
782     }
783     xml.setAttribute("text_color", m_view.text_color->color().name());
784     xml.setAttribute("selected_color", m_view.selected_color->color().name());
785     xml.setAttribute("highlighted_color", m_view.highlighted_color->color().name());
786     xml.setAttribute("text_shadow", (int) m_view.use_shadow->isChecked());
787
788     QList<QGraphicsItem *> list = m_scene->items();
789     int buttonCount = 0;
790
791     for (int i = 0; i < list.count(); i++) {
792         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
793             buttonCount++;
794             DvdButton *button = static_cast < DvdButton* >(list.at(i));
795             QDomElement xmlbutton = doc.createElement("button");
796             xmlbutton.setAttribute("target", button->target());
797             xmlbutton.setAttribute("command", button->command());
798             xmlbutton.setAttribute("backtomenu", button->backMenu());
799             xmlbutton.setAttribute("posx", button->pos().x());
800             xmlbutton.setAttribute("posy", button->pos().y());
801             xmlbutton.setAttribute("text", button->toPlainText());
802             QFont font = button->font();
803             xmlbutton.setAttribute("font_size", font.pixelSize());
804             xmlbutton.setAttribute("font_family", font.family());
805             xml.appendChild(xmlbutton);
806         }
807     }
808     return doc.documentElement();
809 }
810
811
812 void DvdWizardMenu::loadXml(QDomElement xml)
813 {
814     kDebug() << "// LOADING MENU";
815     if (xml.isNull()) return;
816     kDebug() << "// LOADING MENU 1";
817     m_view.create_menu->setChecked(xml.attribute("enabled").toInt());
818     if (xml.hasAttribute("background_color")) {
819         m_view.background_list->setCurrentIndex(0);
820         m_view.background_color->setColor(xml.attribute("background_color"));
821     } else if (xml.hasAttribute("background_image")) {
822         m_view.background_list->setCurrentIndex(1);
823         m_view.background_image->setUrl(KUrl(xml.attribute("background_image")));
824     } else if (xml.hasAttribute("background_video")) {
825         m_view.background_list->setCurrentIndex(2);
826         m_view.background_image->setUrl(KUrl(xml.attribute("background_video")));
827     }
828
829     m_view.text_color->setColor(xml.attribute("text_color"));
830     m_view.selected_color->setColor(xml.attribute("selected_color"));
831     m_view.highlighted_color->setColor(xml.attribute("highlighted_color"));
832
833     m_view.use_shadow->setChecked(xml.attribute("text_shadow").toInt());
834
835     QDomNodeList buttons = xml.elementsByTagName("button");
836     kDebug() << "// LOADING MENU 2" << buttons.count();
837
838     if (buttons.count() > 0) {
839         // Clear existing buttons
840         QList<QGraphicsItem *> list = m_scene->items();
841
842         for (int i = 0; i < list.count(); i++) {
843             if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
844                 delete list.at(i);
845                 i--;
846             }
847         }
848     }
849
850     for (int i = 0; i < buttons.count(); i++) {
851         QDomElement e = buttons.at(i).toElement();
852         // create menu button
853         DvdButton *button = new DvdButton(e.attribute("text"));
854         QFont font(e.attribute("font_family"));
855         font.setPixelSize(e.attribute("font_size").toInt());
856 #if KDE_IS_VERSION(4,6,0)
857         if (m_view.use_shadow->isChecked()) {
858             QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
859             shadow->setBlurRadius(7);
860             shadow->setOffset(4, 4);
861             button->setGraphicsEffect(shadow);
862         }
863 #endif
864
865         //font.setStyleStrategy(QFont::NoAntialias);
866         button->setFont(font);
867         button->setTarget(e.attribute("target").toInt(), e.attribute("command"));
868         button->setBackMenu(e.attribute("backtomenu").toInt());
869         button->setDefaultTextColor(m_view.text_color->color());
870         button->setZValue(4);
871         m_scene->addItem(button);
872         button->setPos(e.attribute("posx").toDouble(), e.attribute("posy").toDouble());
873
874     }
875 }
876
877 void DvdWizardMenu::slotZoom()
878 {
879     m_view.menu_preview->scale(2.0, 2.0);
880 }
881
882 void DvdWizardMenu::slotUnZoom()
883 {
884     m_view.menu_preview->scale(0.5, 0.5);
885 }
886