1 /***************************************************************************
2 * Copyright (C) 2009 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
20 #include "dvdwizardmenu.h"
23 #include <KColorScheme>
26 DvdWizardMenu::DvdWizardMenu(const QString &profile, QWidget *parent) :
32 m_view.play_text->setText(i18n("Play"));
33 m_scene = new DvdScene(this);
34 m_view.menu_preview->setScene(m_scene);
35 m_view.menu_preview->setMouseTracking(true);
36 connect(m_view.create_menu, SIGNAL(toggled(bool)), m_view.menu_box, SLOT(setEnabled(bool)));
37 connect(m_view.create_menu, SIGNAL(toggled(bool)), this, SIGNAL(completeChanged()));
39 m_view.add_button->setIcon(KIcon("document-new"));
40 m_view.delete_button->setIcon(KIcon("trash-empty"));
41 m_view.zoom_button->setIcon(KIcon("zoom-in"));
42 m_view.unzoom_button->setIcon(KIcon("zoom-out"));
44 m_view.add_button->setToolTip(i18n("Add new button"));
45 m_view.delete_button->setToolTip(i18n("Delete current button"));
47 if (profile == "dv_ntsc" || profile == "dv_ntsc_wide") {
49 } else changeProfile(true);
52 // Create color background
53 m_color = new QGraphicsRectItem(0, 0, m_width, m_height);
54 m_color->setBrush(m_view.background_color->color());
55 m_color->setZValue(2);
56 m_scene->addItem(m_color);
59 // create background image
60 m_background = new QGraphicsPixmapItem();
61 m_background->setZValue(3);
62 //m_scene->addItem(m_background);
64 // create safe zone rect
65 int safeW = m_width / 20;
66 int safeH = m_height / 20;
67 m_safeRect = new QGraphicsRectItem(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
69 pen.setStyle(Qt::DashLine);
71 m_safeRect->setPen(pen);
72 m_safeRect->setZValue(5);
73 m_scene->addItem(m_safeRect);
75 checkBackgroundType(0);
79 DvdButton *button = new DvdButton(m_view.play_text->text());
80 QFont font = m_view.font_family->currentFont();
81 font.setPixelSize(m_view.font_size->value());
82 font.setStyleStrategy(QFont::NoAntialias);
83 button->setFont(font);
84 button->setDefaultTextColor(m_view.text_color->color());
86 QRectF r = button->sceneBoundingRect();
87 m_scene->addItem(button);
88 button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
89 button->setSelected(true);
92 //m_view.menu_preview->resizefitInView(0, 0, m_width, m_height);
94 connect(m_view.play_text, SIGNAL(textChanged(const QString &)), this, SLOT(buildButton()));
95 connect(m_view.text_color, SIGNAL(changed(const QColor &)), this, SLOT(updateColor()));
96 connect(m_view.font_size, SIGNAL(valueChanged(int)), this, SLOT(buildButton()));
97 connect(m_view.font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(buildButton()));
98 connect(m_view.background_image, SIGNAL(textChanged(const QString &)), this, SLOT(buildImage()));
99 connect(m_view.background_color, SIGNAL(changed(const QColor &)), this, SLOT(buildColor()));
101 connect(m_view.background_list, SIGNAL(activated(int)), this, SLOT(checkBackgroundType(int)));
103 connect(m_view.target_list, SIGNAL(activated(int)), this, SLOT(setButtonTarget(int)));
104 connect(m_view.back_to_menu, SIGNAL(toggled(bool)), this, SLOT(setBackToMenu(bool)));
106 connect(m_view.add_button, SIGNAL(pressed()), this, SLOT(addButton()));
107 connect(m_view.delete_button, SIGNAL(pressed()), this, SLOT(deleteButton()));
108 connect(m_view.zoom_button, SIGNAL(pressed()), this, SLOT(slotZoom()));
109 connect(m_view.unzoom_button, SIGNAL(pressed()), this, SLOT(slotUnZoom()));
110 connect(m_scene, SIGNAL(selectionChanged()), this, SLOT(buttonChanged()));
111 connect(m_scene, SIGNAL(changed(const QList<QRectF> &)), this, SIGNAL(completeChanged()));
113 // red background for error message
114 KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Window);
115 QPalette p = m_view.error_message->palette();
116 p.setColor(QPalette::Background, scheme.background(KColorScheme::NegativeBackground).color());
117 m_view.error_message->setAutoFillBackground(true);
118 m_view.error_message->setPalette(p);
120 m_view.menu_box->setEnabled(false);
124 DvdWizardMenu::~DvdWizardMenu()
134 bool DvdWizardMenu::isComplete() const
136 m_view.error_message->setHidden(true);
137 if (!m_view.create_menu->isChecked()) return true;
139 QList<QGraphicsItem *> list = m_scene->items();
141 // check that the menu buttons don't collide
142 for (int i = 0; i < list.count(); i++) {
143 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
145 DvdButton *button = static_cast < DvdButton* >(list.at(i));
146 QList<QGraphicsItem *> collisions = button->collidingItems();
147 if (!collisions.isEmpty()) {
148 for (int j = 0; j < collisions.count(); j++) {
149 if (collisions.at(j)->type() == button->type()) {
150 m_view.error_message->setText(i18n("Buttons overlapping"));
151 m_view.error_message->setHidden(false);
156 targets.append(button->target());
159 if (buttonCount == 0) {
160 //We need at least one button
161 m_view.error_message->setText(i18n("No button in menu"));
162 m_view.error_message->setHidden(false);
166 if (!m_view.background_image->isHidden()) {
167 // Make sure user selected a valid image / video file
168 if (!QFile::exists(m_view.background_image->url().path())) {
169 m_view.error_message->setText(i18n("Missing background image"));
170 m_view.error_message->setHidden(false);
175 // check that we have a "Play all" entry
176 if (targets.contains(0)) return true;
177 // ... or that each video file has a button
178 for (int i = m_view.target_list->count() - 1; i > 0; i--) {
179 // If there is a vob file entry and it has no button assigned, don't allow to go further
180 if (m_view.target_list->itemIcon(i).isNull() == false && !targets.contains(i)) {
181 m_view.error_message->setText(i18n("No menu entry for %1", m_view.target_list->itemText(i)));
182 m_view.error_message->setHidden(false);
189 void DvdWizardMenu::setButtonTarget(int ix)
191 QList<QGraphicsItem *> list = m_scene->selectedItems();
192 for (int i = 0; i < list.count(); i++) {
193 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
194 DvdButton *button = static_cast < DvdButton* >(list.at(i));
195 button->setTarget(ix, m_view.target_list->itemData(ix).toString());
199 emit completeChanged();
202 void DvdWizardMenu::setBackToMenu(bool backToMenu)
204 QList<QGraphicsItem *> list = m_scene->selectedItems();
205 for (int i = 0; i < list.count(); i++) {
206 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
207 DvdButton *button = static_cast < DvdButton* >(list.at(i));
208 button->setBackMenu(backToMenu);
212 emit completeChanged();
215 void DvdWizardMenu::buttonChanged()
217 QList<QGraphicsItem *> list = m_scene->selectedItems();
218 bool foundButton = false;
219 for (int i = 0; i < list.count(); i++) {
220 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
221 m_view.play_text->blockSignals(true);
222 m_view.font_size->blockSignals(true);
223 m_view.font_family->blockSignals(true);
224 m_view.target_list->blockSignals(true);
225 m_view.back_to_menu->blockSignals(true);
227 m_view.tabWidget->widget(0)->setEnabled(true);
228 DvdButton *button = static_cast < DvdButton* >(list.at(i));
229 m_view.target_list->setCurrentIndex(button->target());
230 m_view.play_text->setText(button->toPlainText());
231 m_view.back_to_menu->setChecked(button->backMenu());
232 QFont font = button->font();
233 m_view.font_size->setValue(font.pixelSize());
234 m_view.font_family->setCurrentFont(font);
235 m_view.play_text->blockSignals(false);
236 m_view.font_size->blockSignals(false);
237 m_view.font_family->blockSignals(false);
238 m_view.target_list->blockSignals(false);
239 m_view.back_to_menu->blockSignals(false);
243 if (!foundButton) m_view.tabWidget->widget(0)->setEnabled(false);
246 void DvdWizardMenu::addButton()
248 m_scene->clearSelection();
249 DvdButton *button = new DvdButton(m_view.play_text->text());
250 QFont font = m_view.font_family->currentFont();
251 font.setPixelSize(m_view.font_size->value());
252 font.setStyleStrategy(QFont::NoAntialias);
253 button->setFont(font);
254 button->setDefaultTextColor(m_view.text_color->color());
255 button->setZValue(4);
256 QRectF r = button->sceneBoundingRect();
257 m_scene->addItem(button);
258 button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
259 button->setSelected(true);
262 void DvdWizardMenu::deleteButton()
264 QList<QGraphicsItem *> list = m_scene->selectedItems();
265 for (int i = 0; i < list.count(); i++) {
266 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
273 void DvdWizardMenu::changeProfile(bool isPal)
276 if (isPal == false) {
289 void DvdWizardMenu::updatePreview()
291 m_scene->setProfile(m_width, m_height);
293 matrix.scale(0.5, 0.5);
294 m_view.menu_preview->setMatrix(matrix);
295 m_view.menu_preview->setMinimumSize(m_width / 2 + 4, m_height / 2 + 8);
297 if (m_color) m_color->setRect(0, 0, m_width, m_height);
299 int safeW = m_width / 20;
300 int safeH = m_height / 20;
301 if (m_safeRect) m_safeRect->setRect(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
304 void DvdWizardMenu::setTargets(QStringList list, QStringList targetlist)
306 m_view.target_list->clear();
307 m_view.target_list->addItem(i18n("Play All"), "title 1");
309 for (int i = 0; i < list.count(); i++) {
310 if (targetlist.at(i).contains("chapter"))
311 m_view.target_list->addItem(list.at(i), targetlist.at(i));
313 m_view.target_list->addItem(KIcon("video-x-generic"), list.at(i), targetlist.at(i));
317 m_view.back_to_menu->setHidden(movieCount == 1);
320 void DvdWizardMenu::checkBackgroundType(int ix)
323 m_view.background_color->setVisible(true);
324 m_view.background_image->setVisible(false);
325 m_view.loop_movie->setVisible(false);
326 if (m_background->scene() != 0) m_scene->removeItem(m_background);
328 m_view.background_color->setVisible(false);
329 m_view.background_image->setVisible(true);
331 m_view.background_image->clear();
332 m_view.background_image->setFilter("*");
333 if (m_background->scene() != 0) m_scene->removeItem(m_background);
334 m_view.loop_movie->setVisible(false);
336 if (m_background->scene() != 0) m_scene->removeItem(m_background);
337 m_view.background_image->clear();
338 m_view.background_image->setFilter("video/mpeg");
339 m_view.loop_movie->setVisible(true);
344 void DvdWizardMenu::buildColor()
346 m_color->setBrush(m_view.background_color->color());
349 void DvdWizardMenu::buildImage()
351 emit completeChanged();
352 if (m_view.background_image->url().isEmpty()) {
353 if (m_background->scene() != 0) m_scene->removeItem(m_background);
358 if (m_view.background_list->currentIndex() == 1) {
360 if (!pix.load(m_view.background_image->url().path())) {
361 if (m_background->scene() != 0) m_scene->removeItem(m_background);
364 pix = pix.scaled(m_width, m_height);
365 } else if (m_view.background_list->currentIndex() == 2) {
368 if (m_isPal) w = 768;
370 pix = KThumb::getImage(m_view.background_image->url(), 0, w, m_height);
371 pix = pix.scaled(m_width, m_height);
373 m_background->setPixmap(pix);
374 m_scene->addItem(m_background);
377 void DvdWizardMenu::checkBackground()
379 if (m_view.background_list->currentIndex() != 1) {
380 if (m_background->scene() != 0) m_scene->removeItem(m_background);
382 m_scene->addItem(m_background);
386 void DvdWizardMenu::buildButton()
388 DvdButton *button = NULL;
389 QList<QGraphicsItem *> list = m_scene->selectedItems();
390 for (int i = 0; i < list.count(); i++) {
391 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
392 button = static_cast < DvdButton* >(list.at(i));
396 if (button == NULL) return;
397 button->setPlainText(m_view.play_text->text());
398 QFont font = m_view.font_family->currentFont();
399 font.setPixelSize(m_view.font_size->value());
400 font.setStyleStrategy(QFont::NoAntialias);
401 button->setFont(font);
402 button->setDefaultTextColor(m_view.text_color->color());
405 void DvdWizardMenu::updateColor()
407 updateColor(m_view.text_color->color());
408 m_view.menu_preview->viewport()->update();
411 void DvdWizardMenu::updateColor(QColor c)
413 DvdButton *button = NULL;
414 QList<QGraphicsItem *> list = m_scene->items();
415 for (int i = 0; i < list.count(); i++) {
416 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
417 button = static_cast < DvdButton* >(list.at(i));
418 button->setDefaultTextColor(c);
424 void DvdWizardMenu::createButtonImages(const QString &img1, const QString &img2, const QString &img3)
426 if (m_view.create_menu->isChecked()) {
427 m_scene->clearSelection();
428 QImage img(m_width, m_height, QImage::Format_ARGB32_Premultiplied);
429 img.fill(Qt::transparent);
430 if (m_safeRect->scene() != 0) m_scene->removeItem(m_safeRect);
431 if (m_color->scene() != 0) m_scene->removeItem(m_color);
432 if (m_background->scene() != 0) m_scene->removeItem(m_background);
434 p.setRenderHints(QPainter::Antialiasing, false);
435 p.setRenderHints(QPainter::TextAntialiasing, false);
436 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
438 #if QT_VERSION >= 0x040600
439 img.setColorCount(4);
446 if (m_view.menu_profile->currentIndex() < 2)
447 saved = img.scaled(720, 576);
448 else saved = img.scaled(720, 480);
449 saved.setNumColors(4);
452 updateColor(m_view.selected_color->color());
453 img.fill(Qt::transparent);
455 p.setRenderHints(QPainter::Antialiasing, false);
456 p.setRenderHints(QPainter::TextAntialiasing, false);
457 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
459 /* if (m_view.menu_profile->currentIndex() < 2)
460 saved = img.scaled(720, 576);
461 else saved = img.scaled(720, 480);
462 saved.setNumColors(4);
464 #if QT_VERSION >= 0x040600
465 img.setColorCount(4);
472 updateColor(m_view.highlighted_color->color());
473 img.fill(Qt::transparent);
475 p.setRenderHints(QPainter::Antialiasing, false);
476 p.setRenderHints(QPainter::TextAntialiasing, false);
477 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
479 /*if (m_view.menu_profile->currentIndex() < 2)
480 saved = img.scaled(720, 576);
481 else saved = img.scaled(720, 480);
482 saved.setNumColors(4);
484 #if QT_VERSION >= 0x040600
485 img.setColorCount(4);
493 m_scene->addItem(m_safeRect);
494 m_scene->addItem(m_color);
495 if (m_view.background_list->currentIndex() > 0) m_scene->addItem(m_background);
500 void DvdWizardMenu::createBackgroundImage(const QString &img1)
503 if (m_view.background_list->currentIndex() == 0) {
506 img = QImage(768, 576, QImage::Format_ARGB32);
508 img = QImage(720, 540, QImage::Format_ARGB32);
509 img.fill(m_view.background_color->color().rgb());
510 } else if (m_view.background_list->currentIndex() == 1) {
511 img.load(m_view.background_image->url().path());
513 if (img.width() != 768 || img.height() != 576)
514 img = img.scaled(768, 576, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
516 if (img.width() != 720 || img.height() != 540)
517 img = img.scaled(720, 540, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
523 bool DvdWizardMenu::createMenu() const
525 return m_view.create_menu->isChecked();
528 bool DvdWizardMenu::loopMovie() const
530 return m_view.loop_movie->isChecked();
533 bool DvdWizardMenu::menuMovie() const
535 return m_view.background_list->currentIndex() == 2;
538 QString DvdWizardMenu::menuMoviePath() const
540 return m_view.background_image->url().path();
544 QMap <QString, QRect> DvdWizardMenu::buttonsInfo()
546 QMap <QString, QRect> info;
547 QList<QGraphicsItem *> list = m_scene->items();
548 for (int i = 0; i < list.count(); i++) {
549 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
550 DvdButton *button = static_cast < DvdButton* >(list.at(i));
551 QRect r = list.at(i)->sceneBoundingRect().toRect();
552 // Make sure y1 is not odd (requested by spumux)
553 if (r.height() % 2 == 1) r.setHeight(r.height() + 1);
554 if (r.y() % 2 == 1) r.setY(r.y() - 1);
555 QString command = button->command();
556 if (button->backMenu()) command.prepend("g1 = 999;");
557 info.insertMulti(command, r);
563 bool DvdWizardMenu::isPalMenu() const
568 QDomElement DvdWizardMenu::toXml() const
571 QDomElement xml = doc.createElement("menu");
572 doc.appendChild(xml);
573 xml.setAttribute("enabled", m_view.create_menu->isChecked());
574 if (m_view.background_list->currentIndex() == 0) {
576 xml.setAttribute("background_color", m_view.background_color->color().name());
577 } else if (m_view.background_list->currentIndex() == 1) {
579 xml.setAttribute("background_image", m_view.background_image->url().path());
582 xml.setAttribute("background_video", m_view.background_image->url().path());
584 xml.setAttribute("text_color", m_view.text_color->color().name());
585 xml.setAttribute("selected_color", m_view.selected_color->color().name());
586 xml.setAttribute("highlighted_color", m_view.highlighted_color->color().name());
588 QList<QGraphicsItem *> list = m_scene->items();
591 for (int i = 0; i < list.count(); i++) {
592 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
594 DvdButton *button = static_cast < DvdButton* >(list.at(i));
595 QDomElement xmlbutton = doc.createElement("button");
596 xmlbutton.setAttribute("target", button->target());
597 xmlbutton.setAttribute("command", button->command());
598 xmlbutton.setAttribute("backtomenu", button->backMenu());
599 xmlbutton.setAttribute("posx", button->pos().x());
600 xmlbutton.setAttribute("posy", button->pos().y());
601 xmlbutton.setAttribute("text", button->toPlainText());
602 QFont font = button->font();
603 xmlbutton.setAttribute("font_size", font.pixelSize());
604 xmlbutton.setAttribute("font_family", font.family());
605 xml.appendChild(xmlbutton);
608 return doc.documentElement();
612 void DvdWizardMenu::loadXml(QDomElement xml)
614 kDebug() << "// LOADING MENU";
615 if (xml.isNull()) return;
616 kDebug() << "// LOADING MENU 1";
617 m_view.create_menu->setChecked(xml.attribute("enabled").toInt());
618 if (xml.hasAttribute("background_color")) {
619 m_view.background_list->setCurrentIndex(0);
620 m_view.background_color->setColor(xml.attribute("background_color"));
621 } else if (xml.hasAttribute("background_image")) {
622 m_view.background_list->setCurrentIndex(1);
623 m_view.background_image->setUrl(KUrl(xml.attribute("background_image")));
624 } else if (xml.hasAttribute("background_video")) {
625 m_view.background_list->setCurrentIndex(2);
626 m_view.background_image->setUrl(KUrl(xml.attribute("background_video")));
629 m_view.text_color->setColor(xml.attribute("text_color"));
630 m_view.selected_color->setColor(xml.attribute("selected_color"));
631 m_view.highlighted_color->setColor(xml.attribute("highlighted_color"));
633 QDomNodeList buttons = xml.elementsByTagName("button");
634 kDebug() << "// LOADING MENU 2" << buttons.count();
636 if (buttons.count() > 0) {
637 // Clear existing buttons
638 QList<QGraphicsItem *> list = m_scene->items();
640 for (int i = 0; i < list.count(); i++) {
641 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
648 for (int i = 0; i < buttons.count(); i++) {
649 QDomElement e = buttons.at(i).toElement();
650 // create menu button
651 DvdButton *button = new DvdButton(e.attribute("text"));
652 QFont font(e.attribute("font_family"));
653 font.setPixelSize(e.attribute("font_size").toInt());
654 font.setStyleStrategy(QFont::NoAntialias);
655 button->setFont(font);
656 button->setTarget(e.attribute("target").toInt(), e.attribute("command"));
657 button->setBackMenu(e.attribute("backtomenu").toInt());
658 button->setDefaultTextColor(m_view.text_color->color());
659 button->setZValue(4);
660 QRectF r = button->sceneBoundingRect();
661 m_scene->addItem(button);
662 button->setPos(e.attribute("posx").toDouble(), e.attribute("posy").toDouble());
667 void DvdWizardMenu::slotZoom()
669 m_view.menu_preview->scale(2.0, 2.0);
672 void DvdWizardMenu::slotUnZoom()
674 m_view.menu_preview->scale(0.5, 0.5);