]> git.sesse.net Git - kdenlive/blob - src/dvdwizardmenu.cpp
fb162c89378850a422ce2ddce584a64d8c6b51c6
[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(const QString &profile, 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     if (profile == "dv_ntsc" || profile == "dv_ntsc_wide") {
65         changeProfile(false);
66     } else changeProfile(true);
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(activated(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() == QGraphicsItem::UserType + 1) {
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() == QGraphicsItem::UserType + 1) {
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() == QGraphicsItem::UserType + 1) {
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() == QGraphicsItem::UserType + 1) {
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() == QGraphicsItem::UserType + 1) {
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() == QGraphicsItem::UserType + 1) {
346             delete list.at(i);
347             break;
348         }
349     }
350 }
351
352 void DvdWizardMenu::changeProfile(bool isPal)
353 {
354     m_isPal = isPal;
355     if (isPal == false) {
356         m_finalSize = QSize(720, 480);
357         m_width = 640;
358         m_height = 480;
359     } else {
360         m_finalSize = QSize(720, 576);
361         m_width = 768;
362         m_height = 576;
363     }
364     updatePreview();
365 }
366
367 void DvdWizardMenu::updatePreview()
368 {
369     m_scene->setProfile(m_width, m_height);
370     QMatrix matrix;
371     matrix.scale(0.5, 0.5);
372     m_view.menu_preview->setMatrix(matrix);
373     m_view.menu_preview->setMinimumSize(m_width / 2 + 4, m_height / 2 + 8);
374
375     if (m_color) m_color->setRect(0, 0, m_width, m_height);
376
377     int safeW = m_width / 20;
378     int safeH = m_height / 20;
379     if (m_safeRect) m_safeRect->setRect(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
380 }
381
382 void DvdWizardMenu::setTargets(QStringList list, QStringList targetlist)
383 {
384     m_view.target_list->clear();
385     m_view.target_list->addItem(i18n("Play All"), "jump title 1");
386     int movieCount = 0;
387     for (int i = 0; i < list.count(); i++) {
388         if (targetlist.at(i).contains("chapter"))
389             m_view.target_list->addItem(list.at(i), targetlist.at(i));
390         else {
391             m_view.target_list->addItem(KIcon("video-x-generic"), list.at(i), targetlist.at(i));
392             movieCount++;
393         }
394     }
395     m_view.back_to_menu->setHidden(movieCount == 1);
396 }
397
398 void DvdWizardMenu::checkBackgroundType(int ix)
399 {
400     if (ix == 0) {
401         m_view.background_color->setVisible(true);
402         m_view.background_image->setVisible(false);
403         m_view.loop_movie->setVisible(false);
404         if (m_background->scene() != 0) m_scene->removeItem(m_background);
405     } else {
406         m_view.background_color->setVisible(false);
407         m_view.background_image->setVisible(true);
408         if (ix == 1) {
409             m_view.background_image->clear();
410             m_view.background_image->setFilter("*");
411             if (m_background->scene() != 0) m_scene->removeItem(m_background);
412             m_view.loop_movie->setVisible(false);
413         } else {
414             if (m_background->scene() != 0) m_scene->removeItem(m_background);
415             m_view.background_image->clear();
416             m_view.background_image->setFilter("video/mpeg");
417             m_view.loop_movie->setVisible(true);
418         }
419     }
420 }
421
422 void DvdWizardMenu::buildColor()
423 {
424     m_color->setBrush(m_view.background_color->color());
425 }
426
427 void DvdWizardMenu::buildImage()
428 {
429     emit completeChanged();
430     if (m_view.background_image->url().isEmpty()) {
431         if (m_background->scene() != 0) m_scene->removeItem(m_background);
432         return;
433     }
434     QPixmap pix;
435
436     if (m_view.background_list->currentIndex() == 1) {
437         // image background
438         if (!pix.load(m_view.background_image->url().path())) {
439             if (m_background->scene() != 0) m_scene->removeItem(m_background);
440             return;
441         }
442         pix = pix.scaled(m_width, m_height);
443     } else if (m_view.background_list->currentIndex() == 2) {
444         // video background
445         m_movieLength = -1;
446         QString standard = "dv_pal";
447         if (!m_isPal) standard = "dv_ntsc";
448         Mlt::Profile profile(standard.toUtf8().constData());
449         Mlt::Producer *producer = new Mlt::Producer(profile, m_view.background_image->url().path().toUtf8().data());
450         if (producer && producer->is_valid()) {
451             pix = QPixmap::fromImage(KThumb::getFrame(producer, 0, m_finalSize.width(), m_width, m_height));
452             m_movieLength = producer->get_length();
453         }
454         if (producer) delete producer;
455     }
456     m_background->setPixmap(pix);
457     m_scene->addItem(m_background);
458 }
459
460 void DvdWizardMenu::checkBackground()
461 {
462     if (m_view.background_list->currentIndex() != 1) {
463         if (m_background->scene() != 0) m_scene->removeItem(m_background);
464     } else {
465         m_scene->addItem(m_background);
466     }
467 }
468
469 void DvdWizardMenu::buildButton()
470 {
471     DvdButton *button = NULL;
472     QList<QGraphicsItem *> list = m_scene->selectedItems();
473     for (int i = 0; i < list.count(); i++) {
474         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
475             button = static_cast < DvdButton* >(list.at(i));
476             break;
477         }
478     }
479     if (button == NULL) return;
480     button->setPlainText(m_view.play_text->text());
481     QFont font = m_view.font_family->currentFont();
482     font.setPixelSize(m_view.font_size->value());
483     //font.setStyleStrategy(QFont::NoAntialias);
484     button->setFont(font);
485     button->setDefaultTextColor(m_view.text_color->color());
486 }
487
488 void DvdWizardMenu::updateColor()
489 {
490     updateColor(m_view.text_color->color());
491     m_view.menu_preview->viewport()->update();
492 }
493
494 void DvdWizardMenu::prepareUnderLines()
495 {
496     QList<QGraphicsItem *> list = m_scene->items();
497     for (int i = 0; i < list.count(); i++) {
498         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
499             QRectF r = list.at(i)->sceneBoundingRect();
500             int bottom = r.bottom() - 1;
501             if (bottom % 2 == 1) bottom = bottom - 1;
502             int underlineHeight = r.height() / 10;
503             if (underlineHeight % 2 == 1) underlineHeight = underlineHeight - 1;
504             underlineHeight = qMin(underlineHeight, 10);
505             underlineHeight = qMax(underlineHeight, 2);
506             r.setTop(bottom - underlineHeight);
507             r.adjust(2, 0, -2, 0);
508             QGraphicsRectItem *underline = new QGraphicsRectItem(r);
509             underline->setData(Qt::UserRole, QString("underline"));
510             m_scene->addItem(underline);
511             list.at(i)->setVisible(false);
512         }
513     }
514 }
515
516 void DvdWizardMenu::resetUnderLines()
517 {
518     QList<QGraphicsItem *> list = m_scene->items();
519     QList<QGraphicsItem *> toDelete;
520     for (int i = 0; i < list.count(); i++) {
521         if (list.at(i)->data(Qt::UserRole).toString() == "underline") {
522             toDelete.append(list.at(i));
523         }
524         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
525             list.at(i)->setVisible(true);
526         }
527     }
528     while (!toDelete.isEmpty()) {
529         QGraphicsItem *item = toDelete.takeFirst();
530         delete item;
531     }
532 }
533
534 void DvdWizardMenu::updateUnderlineColor(QColor c)
535 {
536     QList<QGraphicsItem *> list = m_scene->items();
537     for (int i = 0; i < list.count(); i++) {
538         if (list.at(i)->data(Qt::UserRole).toString() == "underline") {
539             QGraphicsRectItem *underline = static_cast < QGraphicsRectItem* >(list.at(i));
540             underline->setPen(Qt::NoPen);
541             c.setAlpha(150);
542             underline->setBrush(c);
543         }
544     }
545 }
546
547
548 void DvdWizardMenu::updateColor(QColor c)
549 {
550     DvdButton *button = NULL;
551     QList<QGraphicsItem *> list = m_scene->items();
552     for (int i = 0; i < list.count(); i++) {
553         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
554             button = static_cast < DvdButton* >(list.at(i));
555             button->setDefaultTextColor(c);
556         }
557     }
558 }
559
560
561 void DvdWizardMenu::createButtonImages(const QString &img1, const QString &img2, const QString &img3)
562 {
563     if (m_view.create_menu->isChecked()) {
564         m_scene->clearSelection();
565         if (m_safeRect->scene() != 0) m_scene->removeItem(m_safeRect);
566         if (m_color->scene() != 0) m_scene->removeItem(m_color);
567         if (m_background->scene() != 0) m_scene->removeItem(m_background);
568         prepareUnderLines();
569 #if QT_VERSION >= 0x040800
570         QImage img(m_finalSize.width(), m_finalSize.height(), QImage::Format_ARGB32);
571         img.fill(Qt::transparent);
572         updateUnderlineColor(m_view.text_color->color());
573 #else
574         QImage img(m_finalSize.width(), m_finalSize.height(), QImage::Format_Mono);
575         img.fill(Qt::white);
576         updateUnderlineColor(Qt::black);
577 #endif
578         QPainter p(&img);
579         //p.setRenderHints(QPainter::Antialiasing, false);
580         //p.setRenderHints(QPainter::TextAntialiasing, false);
581         m_scene->render(&p, QRectF(0, 0, m_finalSize.width(), m_finalSize.height()), QRectF(0, 0, m_width, m_height), Qt::IgnoreAspectRatio);
582         p.end();
583 #if QT_VERSION >= 0x040800
584 #elif QT_VERSION >= 0x040600 
585         img.setColor(0, m_view.text_color->color().rgb());
586         img.setColor(1, qRgba(0,0,0,0));
587 #else
588         img.setNumColors(4);
589 #endif
590         img.save(img1);
591
592 #if QT_VERSION >= 0x040800
593         img.fill(Qt::transparent);
594         updateUnderlineColor(m_view.highlighted_color->color());
595 #else
596         img.fill(Qt::white);
597 #endif
598         p.begin(&img);
599         //p.setRenderHints(QPainter::Antialiasing, false);
600         //p.setRenderHints(QPainter::TextAntialiasing, false);
601         m_scene->render(&p, QRectF(0, 0, m_finalSize.width(), m_finalSize.height()), QRectF(0, 0, m_width, m_height), Qt::IgnoreAspectRatio);
602         p.end();
603 #if QT_VERSION >= 0x040800
604 #elif QT_VERSION >= 0x040600
605         img.setColor(0, m_view.highlighted_color->color().rgb());
606         img.setColor(1, qRgba(0,0,0,0));
607 #else
608         img.setNumColors(4);
609 #endif
610         img.save(img3);
611
612 #if QT_VERSION >= 0x040800
613         img.fill(Qt::transparent);
614         updateUnderlineColor(m_view.selected_color->color());
615 #else
616         img.fill(Qt::white);
617 #endif
618         p.begin(&img);
619         //p.setRenderHints(QPainter::Antialiasing, false);
620         //p.setRenderHints(QPainter::TextAntialiasing, false);
621         m_scene->render(&p, QRectF(0, 0, m_finalSize.width(), m_finalSize.height()), QRectF(0, 0, m_width, m_height), Qt::IgnoreAspectRatio);
622         p.end();
623 #if QT_VERSION >= 0x040800
624 #elif QT_VERSION >= 0x040600
625         img.setColor(0, m_view.selected_color->color().rgb());
626         img.setColor(1, qRgba(0,0,0,0));
627 #else
628         img.setNumColors(4);
629 #endif
630         img.save(img2);
631         resetUnderLines();
632         m_scene->addItem(m_safeRect);
633         m_scene->addItem(m_color);
634         if (m_view.background_list->currentIndex() > 0) m_scene->addItem(m_background);
635     }
636 }
637
638
639 void DvdWizardMenu::createBackgroundImage(const QString &overlayMenu, const QString &img1)
640 {
641     m_scene->clearSelection();
642     if (m_safeRect->scene() != 0) m_scene->removeItem(m_safeRect);
643     bool showBg = false;
644     QImage img(m_width, m_height, QImage::Format_ARGB32);
645     if (menuMovie() && m_background->scene() != 0) {
646         showBg = true;
647         m_scene->removeItem(m_background);
648         if (m_color->scene() != 0) m_scene->removeItem(m_color);
649         if (m_safeRect->scene() != 0) m_scene->removeItem(m_safeRect);
650         img.fill(Qt::transparent);
651     }
652     updateColor(m_view.text_color->color());
653     QPainter p(&img);
654     p.setRenderHints(QPainter::Antialiasing, true);
655     p.setRenderHints(QPainter::TextAntialiasing, true);
656     m_scene->render(&p, QRectF(0, 0, img.width(), img.height()));
657     p.end();
658     img.save(img1);
659     m_scene->addItem(m_safeRect);
660     if (showBg) {
661         m_scene->addItem(m_background);
662         m_scene->addItem(m_color);
663     }
664     return;
665         
666   
667     /*QImage img;
668     if (m_view.background_list->currentIndex() == 0) {
669         // color background
670         if (m_isPal)
671             img = QImage(768, 576, QImage::Format_ARGB32);
672         else
673             img = QImage(720, 540, QImage::Format_ARGB32);
674         img.fill(m_view.background_color->color().rgb());
675     } else if (m_view.background_list->currentIndex() == 1) {
676         img.load(m_view.background_image->url().path());
677         if (m_isPal) {
678             if (img.width() != 768 || img.height() != 576)
679                 img = img.scaled(768, 576, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
680         } else {
681             if (img.width() != 720 || img.height() != 540)
682                 img = img.scaled(720, 540, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
683         }
684     } else return;
685     // Overlay Normal menu
686     QImage menu(overlayMenu);
687     QPainter p(&img);
688     QRectF target(0, 0, img.width(), img.height());
689     QRectF src(0, 0, menu.width(), menu.height());
690     p.drawImage(target, menu, src);
691     p.end();
692     img.save(img1);*/
693 }
694
695 bool DvdWizardMenu::createMenu() const
696 {
697     return m_view.create_menu->isChecked();
698 }
699
700 bool DvdWizardMenu::loopMovie() const
701 {
702     return m_view.loop_movie->isChecked();
703 }
704
705 bool DvdWizardMenu::menuMovie() const
706 {
707     return m_view.background_list->currentIndex() == 2;
708 }
709
710 QString DvdWizardMenu::menuMoviePath() const
711 {
712     return m_view.background_image->url().path();
713 }
714
715 int DvdWizardMenu::menuMovieLength() const
716 {
717   return m_movieLength;
718 }
719
720
721 QMap <QString, QRect> DvdWizardMenu::buttonsInfo()
722 {
723     QMap <QString, QRect> info;
724     QList<QGraphicsItem *> list = m_scene->items();
725     double ratio = (double) m_finalSize.width() / m_width;
726     for (int i = 0; i < list.count(); i++) {
727         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
728             DvdButton *button = static_cast < DvdButton* >(list.at(i));
729             QRectF r = button->sceneBoundingRect();
730             QRect adjustedRect(r.x() * ratio, r.y(), r.width() * ratio, r.height());
731             // Make sure y1 is not odd (requested by spumux)
732             if (adjustedRect.height() % 2 == 1) adjustedRect.setHeight(adjustedRect.height() + 1);
733             if (adjustedRect.y() % 2 == 1) adjustedRect.setY(adjustedRect.y() - 1);
734             QString command = button->command();
735             if (button->backMenu()) command.prepend("g1 = 999;");
736             info.insertMulti(command, adjustedRect);
737         }
738     }
739     return info;
740 }
741
742 bool DvdWizardMenu::isPalMenu() const
743 {
744     return m_isPal;
745 }
746
747 QDomElement DvdWizardMenu::toXml() const
748 {
749     QDomDocument doc;
750     QDomElement xml = doc.createElement("menu");
751     doc.appendChild(xml);
752     xml.setAttribute("enabled", m_view.create_menu->isChecked());
753     if (m_view.background_list->currentIndex() == 0) {
754         // Color bg
755         xml.setAttribute("background_color", m_view.background_color->color().name());
756     } else if (m_view.background_list->currentIndex() == 1) {
757         // Image bg
758         xml.setAttribute("background_image", m_view.background_image->url().path());
759     } else {
760         // Video bg
761         xml.setAttribute("background_video", m_view.background_image->url().path());
762     }
763     xml.setAttribute("text_color", m_view.text_color->color().name());
764     xml.setAttribute("selected_color", m_view.selected_color->color().name());
765     xml.setAttribute("highlighted_color", m_view.highlighted_color->color().name());
766     xml.setAttribute("text_shadow", (int) m_view.use_shadow->isChecked());
767
768     QList<QGraphicsItem *> list = m_scene->items();
769     int buttonCount = 0;
770
771     for (int i = 0; i < list.count(); i++) {
772         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
773             buttonCount++;
774             DvdButton *button = static_cast < DvdButton* >(list.at(i));
775             QDomElement xmlbutton = doc.createElement("button");
776             xmlbutton.setAttribute("target", button->target());
777             xmlbutton.setAttribute("command", button->command());
778             xmlbutton.setAttribute("backtomenu", button->backMenu());
779             xmlbutton.setAttribute("posx", button->pos().x());
780             xmlbutton.setAttribute("posy", button->pos().y());
781             xmlbutton.setAttribute("text", button->toPlainText());
782             QFont font = button->font();
783             xmlbutton.setAttribute("font_size", font.pixelSize());
784             xmlbutton.setAttribute("font_family", font.family());
785             xml.appendChild(xmlbutton);
786         }
787     }
788     return doc.documentElement();
789 }
790
791
792 void DvdWizardMenu::loadXml(QDomElement xml)
793 {
794     kDebug() << "// LOADING MENU";
795     if (xml.isNull()) return;
796     kDebug() << "// LOADING MENU 1";
797     m_view.create_menu->setChecked(xml.attribute("enabled").toInt());
798     if (xml.hasAttribute("background_color")) {
799         m_view.background_list->setCurrentIndex(0);
800         m_view.background_color->setColor(xml.attribute("background_color"));
801     } else if (xml.hasAttribute("background_image")) {
802         m_view.background_list->setCurrentIndex(1);
803         m_view.background_image->setUrl(KUrl(xml.attribute("background_image")));
804     } else if (xml.hasAttribute("background_video")) {
805         m_view.background_list->setCurrentIndex(2);
806         m_view.background_image->setUrl(KUrl(xml.attribute("background_video")));
807     }
808
809     m_view.text_color->setColor(xml.attribute("text_color"));
810     m_view.selected_color->setColor(xml.attribute("selected_color"));
811     m_view.highlighted_color->setColor(xml.attribute("highlighted_color"));
812
813     m_view.use_shadow->setChecked(xml.attribute("text_shadow").toInt());
814
815     QDomNodeList buttons = xml.elementsByTagName("button");
816     kDebug() << "// LOADING MENU 2" << buttons.count();
817
818     if (buttons.count() > 0) {
819         // Clear existing buttons
820         QList<QGraphicsItem *> list = m_scene->items();
821
822         for (int i = 0; i < list.count(); i++) {
823             if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
824                 delete list.at(i);
825                 i--;
826             }
827         }
828     }
829
830     for (int i = 0; i < buttons.count(); i++) {
831         QDomElement e = buttons.at(i).toElement();
832         // create menu button
833         DvdButton *button = new DvdButton(e.attribute("text"));
834         QFont font(e.attribute("font_family"));
835         font.setPixelSize(e.attribute("font_size").toInt());
836 #if KDE_IS_VERSION(4,6,0)
837         if (m_view.use_shadow->isChecked()) {
838             QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
839             shadow->setBlurRadius(7);
840             shadow->setOffset(4, 4);
841             button->setGraphicsEffect(shadow);
842         }
843 #endif
844
845         //font.setStyleStrategy(QFont::NoAntialias);
846         button->setFont(font);
847         button->setTarget(e.attribute("target").toInt(), e.attribute("command"));
848         button->setBackMenu(e.attribute("backtomenu").toInt());
849         button->setDefaultTextColor(m_view.text_color->color());
850         button->setZValue(4);
851         m_scene->addItem(button);
852         button->setPos(e.attribute("posx").toDouble(), e.attribute("posy").toDouble());
853
854     }
855 }
856
857 void DvdWizardMenu::slotZoom()
858 {
859     m_view.menu_preview->scale(2.0, 2.0);
860 }
861
862 void DvdWizardMenu::slotUnZoom()
863 {
864     m_view.menu_preview->scale(0.5, 0.5);
865 }
866