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