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