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