]> git.sesse.net Git - kdenlive/blob - src/dvdwizardmenu.cpp
986ff507ec82c58e585c8ba5c97bf3e285213a26
[kdenlive] / src / dvdwizardmenu.cpp
1 /***************************************************************************
2  *   Copyright (C) 2009 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20 #include "dvdwizardmenu.h"
21 #include "kdenlivesettings.h"
22
23 #include <KDebug>
24 #include <KColorScheme>
25
26 #if KDE_IS_VERSION(4,6,0)
27 #include <QGraphicsDropShadowEffect>
28 #endif
29
30
31 #include "kthumb.h"
32
33 enum { DvdButtonItem = QGraphicsItem::UserType + 1, DvdButtonUnderlineItem = QGraphicsItem::UserType + 2 };
34
35 DvdWizardMenu::DvdWizardMenu(DVDFORMAT format, QWidget *parent) :
36     QWizardPage(parent),
37     m_color(NULL),
38     m_safeRect(NULL),
39     m_finalSize(720, 576),
40     m_movieLength(-1)
41 {
42     m_view.setupUi(this);
43     m_view.play_text->setText(i18n("Play"));
44     m_scene = new DvdScene(this);
45     m_view.menu_preview->setScene(m_scene);
46     m_view.menu_preview->setMouseTracking(true);
47     connect(m_view.create_menu, SIGNAL(toggled(bool)), m_view.menu_box, SLOT(setEnabled(bool)));
48     connect(m_view.create_menu, SIGNAL(toggled(bool)), this, SIGNAL(completeChanged()));
49
50     m_view.add_button->setIcon(KIcon("document-new"));
51     m_view.delete_button->setIcon(KIcon("trash-empty"));
52     m_view.zoom_button->setIcon(KIcon("zoom-in"));
53     m_view.unzoom_button->setIcon(KIcon("zoom-out"));
54
55     m_view.add_button->setToolTip(i18n("Add new button"));
56     m_view.delete_button->setToolTip(i18n("Delete current button"));
57
58     changeProfile(format);
59
60
61     // Create color background
62     m_color = new QGraphicsRectItem(0, 0, m_width, m_height);
63     m_color->setBrush(m_view.background_color->color());
64     m_color->setZValue(2);
65     m_scene->addItem(m_color);
66
67
68     // create background image
69     m_background = new QGraphicsPixmapItem();
70     m_background->setZValue(3);
71     //m_scene->addItem(m_background);
72
73     // create safe zone rect
74     int safeW = m_width / 20;
75     int safeH = m_height / 20;
76     m_safeRect = new QGraphicsRectItem(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
77     QPen pen(Qt::red);
78     pen.setStyle(Qt::DashLine);
79     pen.setWidth(3);
80     m_safeRect->setPen(pen);
81     m_safeRect->setZValue(5);
82     m_scene->addItem(m_safeRect);
83     checkBackgroundType(0);
84
85     // create menu button
86     DvdButton *button = new DvdButton(m_view.play_text->text());
87     QFont font = m_view.font_family->currentFont();
88     font.setPixelSize(m_view.font_size->value());
89     //font.setStyleStrategy(QFont::NoAntialias);
90 #if KDE_IS_VERSION(4,6,0)
91     if (m_view.use_shadow->isChecked()) {
92         QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
93         shadow->setBlurRadius(7);
94         shadow->setOffset(4, 4);
95         button->setGraphicsEffect(shadow);
96     }
97     connect(m_view.use_shadow, SIGNAL(stateChanged(int)), this, SLOT(slotEnableShadows(int)));
98 #elif KDE_IS_VERSION(4,6,0)
99     m_view.use_shadow->setHidden(true);
100 #endif
101     button->setFont(font);
102     button->setDefaultTextColor(m_view.text_color->color());
103     button->setZValue(4);
104     QRectF r = button->sceneBoundingRect();
105     m_scene->addItem(button);
106     button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
107     button->setSelected(true);
108
109
110     //m_view.menu_preview->resizefitInView(0, 0, m_width, m_height);
111
112     connect(m_view.play_text, SIGNAL(textChanged(QString)), this, SLOT(buildButton()));
113     connect(m_view.text_color, SIGNAL(changed(QColor)), this, SLOT(updateColor()));
114     connect(m_view.font_size, SIGNAL(valueChanged(int)), this, SLOT(buildButton()));
115     connect(m_view.font_family, SIGNAL(currentFontChanged(QFont)), this, SLOT(buildButton()));
116     connect(m_view.background_image, SIGNAL(textChanged(QString)), this, SLOT(buildImage()));
117     connect(m_view.background_color, SIGNAL(changed(QColor)), this, SLOT(buildColor()));
118
119     connect(m_view.background_list, SIGNAL(currentIndexChanged(int)), this, SLOT(checkBackgroundType(int)));
120
121     connect(m_view.target_list, SIGNAL(activated(int)), this, SLOT(setButtonTarget(int)));
122     connect(m_view.back_to_menu, SIGNAL(toggled(bool)), this, SLOT(setBackToMenu(bool)));
123
124     connect(m_view.add_button, SIGNAL(pressed()), this, SLOT(addButton()));
125     connect(m_view.delete_button, SIGNAL(pressed()), this, SLOT(deleteButton()));
126     connect(m_view.zoom_button, SIGNAL(pressed()), this, SLOT(slotZoom()));
127     connect(m_view.unzoom_button, SIGNAL(pressed()), this, SLOT(slotUnZoom()));
128     connect(m_scene, SIGNAL(selectionChanged()), this, SLOT(buttonChanged()));
129     connect(m_scene, SIGNAL(sceneChanged()), this, SIGNAL(completeChanged()));
130
131     // red background for error message
132     KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Window, KSharedConfig::openConfig(KdenliveSettings::colortheme()));
133     QPalette p = m_view.error_message->palette();
134     p.setColor(QPalette::Background, scheme.background(KColorScheme::NegativeBackground).color());
135     m_view.error_message->setAutoFillBackground(true);
136     m_view.error_message->setPalette(p);
137     m_view.menu_box->setEnabled(false);
138
139 #if KDE_IS_VERSION(4,7,0)
140     m_menuMessage = new KMessageWidget;
141     QGridLayout *s =  static_cast <QGridLayout*> (layout());
142     s->addWidget(m_menuMessage, 7, 0, 1, -1);
143     m_menuMessage->hide();
144     m_view.error_message->hide();
145 #endif
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() == DvdButtonItem) {
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() == DvdButtonItem) {
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->show();
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->show();
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->show();
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->hide();
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->show();
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() == DvdButtonItem) {
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() == DvdButtonItem) {
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() == DvdButtonItem) {
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->setDefaultTextColor(m_view.text_color->color());
333     button->setZValue(4);
334     QRectF r = button->sceneBoundingRect();
335     m_scene->addItem(button);
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() == DvdButtonItem) {
345             delete list.at(i);
346             break;
347         }
348     }
349 }
350
351 void DvdWizardMenu::changeProfile(DVDFORMAT format)
352 {
353     m_format = format;
354     switch (m_format) {
355     case PAL_WIDE:
356         m_finalSize = QSize(720, 576);
357         m_width = 1024;
358         m_height = 576;
359         break;
360     case NTSC_WIDE:
361         m_finalSize = QSize(720, 480);
362         m_width = 853;
363         m_height = 480;
364         break;
365     case NTSC:
366         m_finalSize = QSize(720, 480);
367         m_width = 640;
368         m_height = 480;
369         break;
370     default:
371         m_finalSize = QSize(720, 576);
372         m_width = 768;
373         m_height = 576;
374     }
375     updatePreview();
376 }
377
378 void DvdWizardMenu::updatePreview()
379 {
380     m_scene->setProfile(m_width, m_height);
381     QMatrix matrix;
382     matrix.scale(0.5, 0.5);
383     m_view.menu_preview->setMatrix(matrix);
384     m_view.menu_preview->setMinimumSize(m_width / 2 + 4, m_height / 2 + 8);
385
386     if (m_color) m_color->setRect(0, 0, m_width, m_height);
387
388     int safeW = m_width / 20;
389     int safeH = m_height / 20;
390     if (m_safeRect) m_safeRect->setRect(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
391 }
392
393 void DvdWizardMenu::setTargets(const QStringList &list, const QStringList &targetlist)
394 {
395     m_view.target_list->clear();
396     m_view.target_list->addItem(i18n("Play All"), "jump title 1");
397     int movieCount = 0;
398     for (int i = 0; i < list.count(); ++i) {
399         if (targetlist.at(i).contains("chapter"))
400             m_view.target_list->addItem(list.at(i), targetlist.at(i));
401         else {
402             m_view.target_list->addItem(KIcon("video-x-generic"), list.at(i), targetlist.at(i));
403             movieCount++;
404         }
405     }
406     m_view.back_to_menu->setHidden(movieCount == 1);
407 }
408
409 void DvdWizardMenu::checkBackgroundType(int ix)
410 {
411     if (ix == 0) {
412         m_view.background_color->setVisible(true);
413         m_view.background_image->setVisible(false);
414         m_view.loop_movie->setVisible(false);
415         if (m_background->scene() != 0) m_scene->removeItem(m_background);
416     } else {
417         m_view.background_color->setVisible(false);
418         m_view.background_image->setVisible(true);
419         if (ix == 1) {
420             m_view.background_image->clear();
421             m_view.background_image->setFilter("*");
422             if (m_background->scene() != 0) m_scene->removeItem(m_background);
423             m_view.loop_movie->setVisible(false);
424         } else {
425             if (m_background->scene() != 0) m_scene->removeItem(m_background);
426             m_view.background_image->clear();
427             m_view.background_image->setFilter("video/mpeg");
428             m_view.loop_movie->setVisible(true);
429         }
430     }
431     emit completeChanged();
432 }
433
434 void DvdWizardMenu::buildColor()
435 {
436     m_color->setBrush(m_view.background_color->color());
437 }
438
439 void DvdWizardMenu::buildImage()
440 {
441     emit completeChanged();
442     if (m_view.background_image->url().isEmpty()) {
443         if (m_background->scene() != 0) m_scene->removeItem(m_background);
444         return;
445     }
446     QPixmap pix;
447
448     if (m_view.background_list->currentIndex() == 1) {
449         // image background
450         if (!pix.load(m_view.background_image->url().path())) {
451             if (m_background->scene() != 0) m_scene->removeItem(m_background);
452             return;
453         }
454         pix = pix.scaled(m_width, m_height);
455     } else if (m_view.background_list->currentIndex() == 2) {
456         // video background
457         m_movieLength = -1;
458         QString profileName = DvdWizardVob::getDvdProfile(m_format);
459         Mlt::Profile profile(profileName.toUtf8().constData());
460         profile.set_explicit(true);
461         Mlt::Producer *producer = new Mlt::Producer(profile, m_view.background_image->url().path().toUtf8().constData());
462         if (producer && producer->is_valid()) {
463             pix = QPixmap::fromImage(KThumb::getFrame(producer, 0, m_finalSize.width(), m_width, m_height));
464             m_movieLength = producer->get_length();
465         }
466         if (producer) delete producer;
467     }
468     m_background->setPixmap(pix);
469     m_scene->addItem(m_background);
470 }
471
472 void DvdWizardMenu::checkBackground()
473 {
474     if (m_view.background_list->currentIndex() != 1) {
475         if (m_background->scene() != 0) m_scene->removeItem(m_background);
476     } else {
477         m_scene->addItem(m_background);
478     }
479 }
480
481 void DvdWizardMenu::buildButton()
482 {
483     DvdButton *button = NULL;
484     QList<QGraphicsItem *> list = m_scene->selectedItems();
485     for (int i = 0; i < list.count(); ++i) {
486         if (list.at(i)->type() == DvdButtonItem) {
487             button = static_cast < DvdButton* >(list.at(i));
488             break;
489         }
490     }
491     if (button == NULL) return;
492     button->setPlainText(m_view.play_text->text());
493     QFont font = m_view.font_family->currentFont();
494     font.setPixelSize(m_view.font_size->value());
495     //font.setStyleStrategy(QFont::NoAntialias);
496     button->setFont(font);
497     button->setDefaultTextColor(m_view.text_color->color());
498     // Check for button overlapping in case we changed text / size
499     emit completeChanged();
500 }
501
502 void DvdWizardMenu::updateColor()
503 {
504     updateColor(m_view.text_color->color());
505     m_view.menu_preview->viewport()->update();
506 }
507
508 void DvdWizardMenu::prepareUnderLines()
509 {
510     QList<QGraphicsItem *> list = m_scene->items();
511     for (int i = 0; i < list.count(); ++i) {
512         if (list.at(i)->type() == DvdButtonItem) {
513             QRectF r = list.at(i)->sceneBoundingRect();
514             int bottom = r.bottom() - 1;
515             if (bottom % 2 == 1) bottom = bottom - 1;
516             int underlineHeight = r.height() / 10;
517             if (underlineHeight % 2 == 1) underlineHeight = underlineHeight - 1;
518             underlineHeight = qMin(underlineHeight, 10);
519             underlineHeight = qMax(underlineHeight, 2);
520             r.setTop(bottom - underlineHeight);
521             r.setBottom(bottom);
522             r.adjust(2, 0, -2, 0);
523             DvdButtonUnderline *underline = new DvdButtonUnderline(r);
524             m_scene->addItem(underline);
525             list.at(i)->setVisible(false);
526         }
527     }
528 }
529
530 void DvdWizardMenu::resetUnderLines()
531 {
532     QList<QGraphicsItem *> list = m_scene->items();
533     QList<QGraphicsItem *> toDelete;
534     for (int i = 0; i < list.count(); ++i) {
535         if (list.at(i)->type() == DvdButtonUnderlineItem) {
536             toDelete.append(list.at(i));
537         }
538         if (list.at(i)->type() == DvdButtonItem) {
539             list.at(i)->setVisible(true);
540         }
541     }
542     while (!toDelete.isEmpty()) {
543         QGraphicsItem *item = toDelete.takeFirst();
544         delete item;
545     }
546 }
547
548 void DvdWizardMenu::updateUnderlineColor(QColor c)
549 {
550     QList<QGraphicsItem *> list = m_scene->items();
551     for (int i = 0; i < list.count(); ++i) {
552         if (list.at(i)->type() == DvdButtonUnderlineItem) {
553             DvdButtonUnderline *underline = static_cast < DvdButtonUnderline* >(list.at(i));
554             underline->setPen(Qt::NoPen);
555             c.setAlpha(150);
556             underline->setBrush(c);
557         }
558     }
559 }
560
561
562 void DvdWizardMenu::updateColor(const QColor &c)
563 {
564     DvdButton *button = NULL;
565     QList<QGraphicsItem *> list = m_scene->items();
566     for (int i = 0; i < list.count(); ++i) {
567         if (list.at(i)->type() == DvdButtonItem) {
568             button = static_cast < DvdButton* >(list.at(i));
569             button->setDefaultTextColor(c);
570         }
571     }
572 }
573
574
575 void DvdWizardMenu::createButtonImages(const QString &selected_image, const QString &highlighted_image, bool letterbox)
576 {
577     if (m_view.create_menu->isChecked()) {
578         m_scene->clearSelection();
579         QRectF source(0, 0, m_width, m_height);
580         QRectF target;
581         if (!letterbox) target = QRectF(0, 0, m_finalSize.width(), m_finalSize.height());
582         else {
583             // Scale the button images to fit a letterbox image
584             double factor = (double) m_width / m_finalSize.width();
585             int letterboxHeight = m_height / factor;
586             target = QRectF(0, (m_finalSize.height() - letterboxHeight) / 2, m_finalSize.width(), letterboxHeight);
587         }
588         if (m_safeRect->scene() != 0) m_scene->removeItem(m_safeRect);
589         if (m_color->scene() != 0) m_scene->removeItem(m_color);
590         if (m_background->scene() != 0) m_scene->removeItem(m_background);
591         prepareUnderLines();
592 #if QT_VERSION >= 0x040800
593         QImage img(m_finalSize.width(), m_finalSize.height(), QImage::Format_ARGB32);
594         img.fill(Qt::transparent);
595         updateUnderlineColor(m_view.highlighted_color->color());
596 #else
597         QImage img(m_finalSize.width(), m_finalSize.height(), QImage::Format_Mono);
598         img.fill(Qt::white);
599         updateUnderlineColor(Qt::black);
600 #endif
601         QPainter p;
602         p.begin(&img);
603         //p.setRenderHints(QPainter::Antialiasing, false);
604         //p.setRenderHints(QPainter::TextAntialiasing, false);
605         m_scene->render(&p, target, source, Qt::IgnoreAspectRatio);
606         p.end();
607 #if QT_VERSION >= 0x040800
608 #elif QT_VERSION >= 0x040600
609         img.setColor(0, m_view.highlighted_color->color().rgb());
610         img.setColor(1, qRgba(0,0,0,0));
611 #else
612         img.setNumColors(4);
613 #endif
614         img.save(highlighted_image);
615
616 #if QT_VERSION >= 0x040800
617         img.fill(Qt::transparent);
618         updateUnderlineColor(m_view.selected_color->color());
619 #else
620         img.fill(Qt::white);
621 #endif
622         p.begin(&img);
623         //p.setRenderHints(QPainter::Antialiasing, false);
624         //p.setRenderHints(QPainter::TextAntialiasing, false);
625         m_scene->render(&p, target, source, Qt::IgnoreAspectRatio);
626         p.end();
627 #if QT_VERSION >= 0x040800
628 #elif QT_VERSION >= 0x040600
629         img.setColor(0, m_view.selected_color->color().rgb());
630         img.setColor(1, qRgba(0,0,0,0));
631 #else
632         img.setNumColors(4);
633 #endif
634         img.save(selected_image);
635         resetUnderLines();
636         m_scene->addItem(m_safeRect);
637         m_scene->addItem(m_color);
638         if (m_view.background_list->currentIndex() > 0) m_scene->addItem(m_background);
639     }
640 }
641
642
643 void DvdWizardMenu::createBackgroundImage(const QString &img1, bool letterbox)
644 {
645     m_scene->clearSelection();
646     if (m_safeRect->scene() != 0) m_scene->removeItem(m_safeRect);
647     bool showBg = false;
648     QImage img(m_width, m_height, QImage::Format_ARGB32);
649
650     //TODO: Should the image be scaled when letterboxing?
651     /*
652     QRectF source(0, 0, m_width, m_height);
653     QRectF target;
654     if (!letterbox) target = QRectF(0, 0, m_finalSize.width(), m_finalSize.height());
655     else {
656     // Scale the button images to fit a letterbox image
657     double factor = (double) m_width / m_finalSize.width();
658     int letterboxHeight = m_height / factor;
659     target = QRectF(0, (m_finalSize.height() - letterboxHeight) / 2, m_finalSize.width(), letterboxHeight);
660     }*/
661
662     if (menuMovie()) {
663         showBg = true;
664         if (m_background->scene() != 0) m_scene->removeItem(m_background);
665         if (m_color->scene() != 0) m_scene->removeItem(m_color);
666         img.fill(Qt::transparent);
667     }
668     updateColor(m_view.text_color->color());
669     QPainter p(&img);
670     p.setRenderHints(QPainter::Antialiasing, true);
671     p.setRenderHints(QPainter::TextAntialiasing, true);
672     m_scene->render(&p, QRectF(0, 0, img.width(), img.height()));
673     //m_scene->render(&p, target, source, Qt::IgnoreAspectRatio);
674     p.end();
675     img.save(img1);
676     m_scene->addItem(m_safeRect);
677     if (showBg) {
678         m_scene->addItem(m_background);
679         m_scene->addItem(m_color);
680     }
681 }
682
683 bool DvdWizardMenu::createMenu() const
684 {
685     return m_view.create_menu->isChecked();
686 }
687
688 bool DvdWizardMenu::loopMovie() const
689 {
690     return m_view.loop_movie->isChecked();
691 }
692
693 bool DvdWizardMenu::menuMovie() const
694 {
695     return m_view.background_list->currentIndex() == 2;
696 }
697
698 QString DvdWizardMenu::menuMoviePath() const
699 {
700     return m_view.background_image->url().path();
701 }
702
703 int DvdWizardMenu::menuMovieLength() const
704 {
705     return m_movieLength;
706 }
707
708
709 QMap <QString, QRect> DvdWizardMenu::buttonsInfo(bool letterbox)
710 {
711     QMap <QString, QRect> info;
712     QList<QGraphicsItem *> list = m_scene->items();
713     double ratiox = (double) m_finalSize.width() / m_width;
714     double ratioy = 1;
715     int offset = 0;
716     if (letterbox) {
717         int letterboxHeight = m_height * ratiox;
718         ratioy = (double) letterboxHeight / m_finalSize.height();
719         offset = (m_finalSize.height() - letterboxHeight) / 2;
720     }
721     for (int i = 0; i < list.count(); ++i) {
722         if (list.at(i)->type() == DvdButtonItem) {
723             DvdButton *button = static_cast < DvdButton* >(list.at(i));
724             QRectF r = button->sceneBoundingRect();
725             QRect adjustedRect(r.x() * ratiox, offset + r.y() * ratioy, r.width() * ratiox, r.height() * ratioy);
726             // Make sure y1 is not odd (requested by spumux)
727             if (adjustedRect.height() % 2 == 1) adjustedRect.setHeight(adjustedRect.height() + 1);
728             if (adjustedRect.y() % 2 == 1) adjustedRect.setY(adjustedRect.y() - 1);
729             QString command = button->command();
730             if (button->backMenu()) command.prepend("g1 = 999;");
731             info.insertMulti(command, adjustedRect);
732         }
733     }
734     return info;
735 }
736
737 QDomElement DvdWizardMenu::toXml() const
738 {
739     QDomDocument doc;
740     QDomElement xml = doc.createElement("menu");
741     doc.appendChild(xml);
742     xml.setAttribute("enabled", m_view.create_menu->isChecked());
743     if (m_view.background_list->currentIndex() == 0) {
744         // Color bg
745         xml.setAttribute("background_color", m_view.background_color->color().name());
746     } else if (m_view.background_list->currentIndex() == 1) {
747         // Image bg
748         xml.setAttribute("background_image", m_view.background_image->url().path());
749     } else {
750         // Video bg
751         xml.setAttribute("background_video", m_view.background_image->url().path());
752     }
753     xml.setAttribute("text_color", m_view.text_color->color().name());
754     xml.setAttribute("selected_color", m_view.selected_color->color().name());
755     xml.setAttribute("highlighted_color", m_view.highlighted_color->color().name());
756     xml.setAttribute("text_shadow", (int) m_view.use_shadow->isChecked());
757
758     QList<QGraphicsItem *> list = m_scene->items();
759     int buttonCount = 0;
760
761     for (int i = 0; i < list.count(); ++i) {
762         if (list.at(i)->type() == DvdButtonItem) {
763             buttonCount++;
764             DvdButton *button = static_cast < DvdButton* >(list.at(i));
765             QDomElement xmlbutton = doc.createElement("button");
766             xmlbutton.setAttribute("target", button->target());
767             xmlbutton.setAttribute("command", button->command());
768             xmlbutton.setAttribute("backtomenu", button->backMenu());
769             xmlbutton.setAttribute("posx", (int) button->pos().x());
770             xmlbutton.setAttribute("posy", (int) button->pos().y());
771             xmlbutton.setAttribute("text", button->toPlainText());
772             QFont font = button->font();
773             xmlbutton.setAttribute("font_size", font.pixelSize());
774             xmlbutton.setAttribute("font_family", font.family());
775             xml.appendChild(xmlbutton);
776         }
777     }
778     return doc.documentElement();
779 }
780
781
782 void DvdWizardMenu::loadXml(DVDFORMAT format, const QDomElement &xml)
783 {
784     kDebug() << "// LOADING MENU";
785     if (xml.isNull()) return;
786     kDebug() << "// LOADING MENU 1";
787     changeProfile(format);
788     m_view.create_menu->setChecked(xml.attribute("enabled").toInt());
789     if (xml.hasAttribute("background_color")) {
790         m_view.background_list->setCurrentIndex(0);
791         m_view.background_color->setColor(xml.attribute("background_color"));
792     } else if (xml.hasAttribute("background_image")) {
793         m_view.background_list->setCurrentIndex(1);
794         m_view.background_image->setUrl(KUrl(xml.attribute("background_image")));
795     } else if (xml.hasAttribute("background_video")) {
796         m_view.background_list->setCurrentIndex(2);
797         m_view.background_image->setUrl(KUrl(xml.attribute("background_video")));
798     }
799
800     m_view.text_color->setColor(xml.attribute("text_color"));
801     m_view.selected_color->setColor(xml.attribute("selected_color"));
802     m_view.highlighted_color->setColor(xml.attribute("highlighted_color"));
803
804     m_view.use_shadow->setChecked(xml.attribute("text_shadow").toInt());
805
806     QDomNodeList buttons = xml.elementsByTagName("button");
807     kDebug() << "// LOADING MENU 2" << buttons.count();
808
809     if (buttons.count() > 0) {
810         // Clear existing buttons
811         QList<QGraphicsItem *> list = m_scene->items();
812
813         for (int i = 0; i < list.count(); ++i) {
814             if (list.at(i)->type() == DvdButtonItem) {
815                 delete list.at(i);
816                 --i;
817             }
818         }
819     }
820
821     for (int i = 0; i < buttons.count(); ++i) {
822         QDomElement e = buttons.at(i).toElement();
823         // create menu button
824         DvdButton *button = new DvdButton(e.attribute("text"));
825         QFont font(e.attribute("font_family"));
826         font.setPixelSize(e.attribute("font_size").toInt());
827 #if KDE_IS_VERSION(4,6,0)
828         if (m_view.use_shadow->isChecked()) {
829             QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
830             shadow->setBlurRadius(7);
831             shadow->setOffset(4, 4);
832             button->setGraphicsEffect(shadow);
833         }
834 #endif
835
836         //font.setStyleStrategy(QFont::NoAntialias);
837         button->setFont(font);
838         button->setTarget(e.attribute("target").toInt(), e.attribute("command"));
839         button->setBackMenu(e.attribute("backtomenu").toInt());
840         button->setDefaultTextColor(m_view.text_color->color());
841         button->setZValue(4);
842         m_scene->addItem(button);
843         button->setPos(e.attribute("posx").toInt(), e.attribute("posy").toInt());
844
845     }
846 }
847
848 void DvdWizardMenu::slotZoom()
849 {
850     m_view.menu_preview->scale(2.0, 2.0);
851 }
852
853 void DvdWizardMenu::slotUnZoom()
854 {
855     m_view.menu_preview->scale(0.5, 0.5);
856 }
857
858
859 #include "dvdwizardmenu.moc"