]> git.sesse.net Git - kdenlive/blob - src/dvdwizardmenu.cpp
Merge branch 'feature/pkey' of git://anongit.kde.org/kdenlive into pkey
[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         QString profileName = DvdWizardVob::getDvdProfile(m_format);
457         Mlt::Profile profile(profileName.toUtf8().constData());
458         profile.set_explicit(true);
459         Mlt::Producer *producer = new Mlt::Producer(profile, m_view.background_image->url().path().toUtf8().data());
460         if (producer && producer->is_valid()) {
461             pix = QPixmap::fromImage(KThumb::getFrame(producer, 0, m_finalSize.width(), m_width, m_height));
462             m_movieLength = producer->get_length();
463         }
464         if (producer) delete producer;
465     }
466     m_background->setPixmap(pix);
467     m_scene->addItem(m_background);
468 }
469
470 void DvdWizardMenu::checkBackground()
471 {
472     if (m_view.background_list->currentIndex() != 1) {
473         if (m_background->scene() != 0) m_scene->removeItem(m_background);
474     } else {
475         m_scene->addItem(m_background);
476     }
477 }
478
479 void DvdWizardMenu::buildButton()
480 {
481     DvdButton *button = NULL;
482     QList<QGraphicsItem *> list = m_scene->selectedItems();
483     for (int i = 0; i < list.count(); i++) {
484         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
485             button = static_cast < DvdButton* >(list.at(i));
486             break;
487         }
488     }
489     if (button == NULL) return;
490     button->setPlainText(m_view.play_text->text());
491     QFont font = m_view.font_family->currentFont();
492     font.setPixelSize(m_view.font_size->value());
493     //font.setStyleStrategy(QFont::NoAntialias);
494     button->setFont(font);
495     button->setDefaultTextColor(m_view.text_color->color());
496 }
497
498 void DvdWizardMenu::updateColor()
499 {
500     updateColor(m_view.text_color->color());
501     m_view.menu_preview->viewport()->update();
502 }
503
504 void DvdWizardMenu::prepareUnderLines()
505 {
506     QList<QGraphicsItem *> list = m_scene->items();
507     for (int i = 0; i < list.count(); i++) {
508         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
509             QRectF r = list.at(i)->sceneBoundingRect();
510             int bottom = r.bottom() - 1;
511             if (bottom % 2 == 1) bottom = bottom - 1;
512             int underlineHeight = r.height() / 10;
513             if (underlineHeight % 2 == 1) underlineHeight = underlineHeight - 1;
514             underlineHeight = qMin(underlineHeight, 10);
515             underlineHeight = qMax(underlineHeight, 2);
516             r.setTop(bottom - underlineHeight);
517             r.adjust(2, 0, -2, 0);
518             QGraphicsRectItem *underline = new QGraphicsRectItem(r);
519             underline->setData(Qt::UserRole, QString("underline"));
520             m_scene->addItem(underline);
521             list.at(i)->setVisible(false);
522         }
523     }
524 }
525
526 void DvdWizardMenu::resetUnderLines()
527 {
528     QList<QGraphicsItem *> list = m_scene->items();
529     QList<QGraphicsItem *> toDelete;
530     for (int i = 0; i < list.count(); i++) {
531         if (list.at(i)->data(Qt::UserRole).toString() == "underline") {
532             toDelete.append(list.at(i));
533         }
534         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
535             list.at(i)->setVisible(true);
536         }
537     }
538     while (!toDelete.isEmpty()) {
539         QGraphicsItem *item = toDelete.takeFirst();
540         delete item;
541     }
542 }
543
544 void DvdWizardMenu::updateUnderlineColor(QColor c)
545 {
546     QList<QGraphicsItem *> list = m_scene->items();
547     for (int i = 0; i < list.count(); i++) {
548         if (list.at(i)->data(Qt::UserRole).toString() == "underline") {
549             QGraphicsRectItem *underline = static_cast < QGraphicsRectItem* >(list.at(i));
550             underline->setPen(Qt::NoPen);
551             c.setAlpha(150);
552             underline->setBrush(c);
553         }
554     }
555 }
556
557
558 void DvdWizardMenu::updateColor(QColor c)
559 {
560     DvdButton *button = NULL;
561     QList<QGraphicsItem *> list = m_scene->items();
562     for (int i = 0; i < list.count(); i++) {
563         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
564             button = static_cast < DvdButton* >(list.at(i));
565             button->setDefaultTextColor(c);
566         }
567     }
568 }
569
570
571 void DvdWizardMenu::createButtonImages(const QString &img1, const QString &img2, const QString &img3, bool letterbox)
572 {
573     if (m_view.create_menu->isChecked()) {
574         m_scene->clearSelection();
575         QRectF source(0, 0, m_width, m_height);
576         QRectF target;
577         if (!letterbox) target = QRectF(0, 0, m_finalSize.width(), m_finalSize.height());
578         else {
579             // Scale the button images to fit a letterbox image
580             double factor = (double) m_width / m_finalSize.width();
581             int letterboxHeight = m_height / factor;
582             target = QRectF(0, (m_finalSize.height() - letterboxHeight) / 2, m_finalSize.width(), letterboxHeight);
583         }
584         if (m_safeRect->scene() != 0) m_scene->removeItem(m_safeRect);
585         if (m_color->scene() != 0) m_scene->removeItem(m_color);
586         if (m_background->scene() != 0) m_scene->removeItem(m_background);
587         prepareUnderLines();
588 #if QT_VERSION >= 0x040800
589         QImage img(m_finalSize.width(), m_finalSize.height(), QImage::Format_ARGB32);
590         img.fill(Qt::transparent);
591         updateUnderlineColor(m_view.text_color->color());
592 #else
593         QImage img(m_finalSize.width(), m_finalSize.height(), QImage::Format_Mono);
594         img.fill(Qt::white);
595         updateUnderlineColor(Qt::black);
596 #endif
597         QPainter p(&img);
598         //p.setRenderHints(QPainter::Antialiasing, false);
599         //p.setRenderHints(QPainter::TextAntialiasing, false);
600         m_scene->render(&p, target, source, Qt::IgnoreAspectRatio);
601         p.end();
602 #if QT_VERSION >= 0x040800
603 #elif QT_VERSION >= 0x040600 
604         img.setColor(0, m_view.text_color->color().rgb());
605         img.setColor(1, qRgba(0,0,0,0));
606 #else
607         img.setNumColors(4);
608 #endif
609         img.save(img1);
610
611 #if QT_VERSION >= 0x040800
612         img.fill(Qt::transparent);
613         updateUnderlineColor(m_view.highlighted_color->color());
614 #else
615         img.fill(Qt::white);
616 #endif
617         p.begin(&img);
618         //p.setRenderHints(QPainter::Antialiasing, false);
619         //p.setRenderHints(QPainter::TextAntialiasing, false);
620         m_scene->render(&p, target, source, Qt::IgnoreAspectRatio);
621         p.end();
622 #if QT_VERSION >= 0x040800
623 #elif QT_VERSION >= 0x040600
624         img.setColor(0, m_view.highlighted_color->color().rgb());
625         img.setColor(1, qRgba(0,0,0,0));
626 #else
627         img.setNumColors(4);
628 #endif
629         img.save(img3);
630
631 #if QT_VERSION >= 0x040800
632         img.fill(Qt::transparent);
633         updateUnderlineColor(m_view.selected_color->color());
634 #else
635         img.fill(Qt::white);
636 #endif
637         p.begin(&img);
638         //p.setRenderHints(QPainter::Antialiasing, false);
639         //p.setRenderHints(QPainter::TextAntialiasing, false);
640         m_scene->render(&p, target, source, Qt::IgnoreAspectRatio);
641         p.end();
642 #if QT_VERSION >= 0x040800
643 #elif QT_VERSION >= 0x040600
644         img.setColor(0, m_view.selected_color->color().rgb());
645         img.setColor(1, qRgba(0,0,0,0));
646 #else
647         img.setNumColors(4);
648 #endif
649         img.save(img2);
650         resetUnderLines();
651         m_scene->addItem(m_safeRect);
652         m_scene->addItem(m_color);
653         if (m_view.background_list->currentIndex() > 0) m_scene->addItem(m_background);
654     }
655 }
656
657
658 void DvdWizardMenu::createBackgroundImage(const QString &overlayMenu, const QString &img1)
659 {
660     m_scene->clearSelection();
661     if (m_safeRect->scene() != 0) m_scene->removeItem(m_safeRect);
662     bool showBg = false;
663     QImage img(m_width, m_height, QImage::Format_ARGB32);
664     if (menuMovie() && m_background->scene() != 0) {
665         showBg = true;
666         m_scene->removeItem(m_background);
667         if (m_color->scene() != 0) m_scene->removeItem(m_color);
668         if (m_safeRect->scene() != 0) m_scene->removeItem(m_safeRect);
669         img.fill(Qt::transparent);
670     }
671     updateColor(m_view.text_color->color());
672     QPainter p(&img);
673     p.setRenderHints(QPainter::Antialiasing, true);
674     p.setRenderHints(QPainter::TextAntialiasing, true);
675     m_scene->render(&p, QRectF(0, 0, img.width(), img.height()));
676     p.end();
677     img.save(img1);
678     m_scene->addItem(m_safeRect);
679     if (showBg) {
680         m_scene->addItem(m_background);
681         m_scene->addItem(m_color);
682     }
683     return;
684         
685   
686     /*QImage img;
687     if (m_view.background_list->currentIndex() == 0) {
688         // color background
689         if (m_isPal)
690             img = QImage(768, 576, QImage::Format_ARGB32);
691         else
692             img = QImage(720, 540, QImage::Format_ARGB32);
693         img.fill(m_view.background_color->color().rgb());
694     } else if (m_view.background_list->currentIndex() == 1) {
695         img.load(m_view.background_image->url().path());
696         if (m_isPal) {
697             if (img.width() != 768 || img.height() != 576)
698                 img = img.scaled(768, 576, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
699         } else {
700             if (img.width() != 720 || img.height() != 540)
701                 img = img.scaled(720, 540, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
702         }
703     } else return;
704     // Overlay Normal menu
705     QImage menu(overlayMenu);
706     QPainter p(&img);
707     QRectF target(0, 0, img.width(), img.height());
708     QRectF src(0, 0, menu.width(), menu.height());
709     p.drawImage(target, menu, src);
710     p.end();
711     img.save(img1);*/
712 }
713
714 bool DvdWizardMenu::createMenu() const
715 {
716     return m_view.create_menu->isChecked();
717 }
718
719 bool DvdWizardMenu::loopMovie() const
720 {
721     return m_view.loop_movie->isChecked();
722 }
723
724 bool DvdWizardMenu::menuMovie() const
725 {
726     return m_view.background_list->currentIndex() == 2;
727 }
728
729 QString DvdWizardMenu::menuMoviePath() const
730 {
731     return m_view.background_image->url().path();
732 }
733
734 int DvdWizardMenu::menuMovieLength() const
735 {
736   return m_movieLength;
737 }
738
739
740 QMap <QString, QRect> DvdWizardMenu::buttonsInfo(bool letterbox)
741 {
742     QMap <QString, QRect> info;
743     QList<QGraphicsItem *> list = m_scene->items();
744     double ratiox = (double) m_finalSize.width() / m_width;
745     double ratioy = 1;
746     int offset = 0;
747     if (letterbox) {
748         int letterboxHeight = m_height * ratiox;
749         ratioy = (double) letterboxHeight / m_finalSize.height();
750         offset = (m_finalSize.height() - letterboxHeight) / 2;
751     }
752     for (int i = 0; i < list.count(); i++) {
753         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
754             DvdButton *button = static_cast < DvdButton* >(list.at(i));
755             QRectF r = button->sceneBoundingRect();
756             QRect adjustedRect(r.x() * ratiox, offset + r.y() * ratioy, r.width() * ratiox, r.height() * ratioy);
757             // Make sure y1 is not odd (requested by spumux)
758             if (adjustedRect.height() % 2 == 1) adjustedRect.setHeight(adjustedRect.height() + 1);
759             if (adjustedRect.y() % 2 == 1) adjustedRect.setY(adjustedRect.y() - 1);
760             QString command = button->command();
761             if (button->backMenu()) command.prepend("g1 = 999;");
762             info.insertMulti(command, adjustedRect);
763         }
764     }
765     return info;
766 }
767
768 QDomElement DvdWizardMenu::toXml() const
769 {
770     QDomDocument doc;
771     QDomElement xml = doc.createElement("menu");
772     doc.appendChild(xml);
773     xml.setAttribute("enabled", m_view.create_menu->isChecked());
774     if (m_view.background_list->currentIndex() == 0) {
775         // Color bg
776         xml.setAttribute("background_color", m_view.background_color->color().name());
777     } else if (m_view.background_list->currentIndex() == 1) {
778         // Image bg
779         xml.setAttribute("background_image", m_view.background_image->url().path());
780     } else {
781         // Video bg
782         xml.setAttribute("background_video", m_view.background_image->url().path());
783     }
784     xml.setAttribute("text_color", m_view.text_color->color().name());
785     xml.setAttribute("selected_color", m_view.selected_color->color().name());
786     xml.setAttribute("highlighted_color", m_view.highlighted_color->color().name());
787     xml.setAttribute("text_shadow", (int) m_view.use_shadow->isChecked());
788
789     QList<QGraphicsItem *> list = m_scene->items();
790     int buttonCount = 0;
791
792     for (int i = 0; i < list.count(); i++) {
793         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
794             buttonCount++;
795             DvdButton *button = static_cast < DvdButton* >(list.at(i));
796             QDomElement xmlbutton = doc.createElement("button");
797             xmlbutton.setAttribute("target", button->target());
798             xmlbutton.setAttribute("command", button->command());
799             xmlbutton.setAttribute("backtomenu", button->backMenu());
800             xmlbutton.setAttribute("posx", button->pos().x());
801             xmlbutton.setAttribute("posy", button->pos().y());
802             xmlbutton.setAttribute("text", button->toPlainText());
803             QFont font = button->font();
804             xmlbutton.setAttribute("font_size", font.pixelSize());
805             xmlbutton.setAttribute("font_family", font.family());
806             xml.appendChild(xmlbutton);
807         }
808     }
809     return doc.documentElement();
810 }
811
812
813 void DvdWizardMenu::loadXml(QDomElement xml)
814 {
815     kDebug() << "// LOADING MENU";
816     if (xml.isNull()) return;
817     kDebug() << "// LOADING MENU 1";
818     m_view.create_menu->setChecked(xml.attribute("enabled").toInt());
819     if (xml.hasAttribute("background_color")) {
820         m_view.background_list->setCurrentIndex(0);
821         m_view.background_color->setColor(xml.attribute("background_color"));
822     } else if (xml.hasAttribute("background_image")) {
823         m_view.background_list->setCurrentIndex(1);
824         m_view.background_image->setUrl(KUrl(xml.attribute("background_image")));
825     } else if (xml.hasAttribute("background_video")) {
826         m_view.background_list->setCurrentIndex(2);
827         m_view.background_image->setUrl(KUrl(xml.attribute("background_video")));
828     }
829
830     m_view.text_color->setColor(xml.attribute("text_color"));
831     m_view.selected_color->setColor(xml.attribute("selected_color"));
832     m_view.highlighted_color->setColor(xml.attribute("highlighted_color"));
833
834     m_view.use_shadow->setChecked(xml.attribute("text_shadow").toInt());
835
836     QDomNodeList buttons = xml.elementsByTagName("button");
837     kDebug() << "// LOADING MENU 2" << buttons.count();
838
839     if (buttons.count() > 0) {
840         // Clear existing buttons
841         QList<QGraphicsItem *> list = m_scene->items();
842
843         for (int i = 0; i < list.count(); i++) {
844             if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
845                 delete list.at(i);
846                 i--;
847             }
848         }
849     }
850
851     for (int i = 0; i < buttons.count(); i++) {
852         QDomElement e = buttons.at(i).toElement();
853         // create menu button
854         DvdButton *button = new DvdButton(e.attribute("text"));
855         QFont font(e.attribute("font_family"));
856         font.setPixelSize(e.attribute("font_size").toInt());
857 #if KDE_IS_VERSION(4,6,0)
858         if (m_view.use_shadow->isChecked()) {
859             QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
860             shadow->setBlurRadius(7);
861             shadow->setOffset(4, 4);
862             button->setGraphicsEffect(shadow);
863         }
864 #endif
865
866         //font.setStyleStrategy(QFont::NoAntialias);
867         button->setFont(font);
868         button->setTarget(e.attribute("target").toInt(), e.attribute("command"));
869         button->setBackMenu(e.attribute("backtomenu").toInt());
870         button->setDefaultTextColor(m_view.text_color->color());
871         button->setZValue(4);
872         m_scene->addItem(button);
873         button->setPos(e.attribute("posx").toDouble(), e.attribute("posy").toDouble());
874
875     }
876 }
877
878 void DvdWizardMenu::slotZoom()
879 {
880     m_view.menu_preview->scale(2.0, 2.0);
881 }
882
883 void DvdWizardMenu::slotUnZoom()
884 {
885     m_view.menu_preview->scale(0.5, 0.5);
886 }
887