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"
21 #include "kdenlivesettings.h"
24 #include <KColorScheme>
27 DvdWizardMenu::DvdWizardMenu(const QString &profile, QWidget *parent) :
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()));
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();
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"));
53 m_view.add_button->setToolTip(i18n("Add new button"));
54 m_view.delete_button->setToolTip(i18n("Delete current button"));
56 if (profile == "dv_ntsc" || profile == "dv_ntsc_wide") {
58 } else changeProfile(true);
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);
68 // create background image
69 m_background = new QGraphicsPixmapItem();
70 m_background->setZValue(3);
71 //m_scene->addItem(m_background);
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);
78 pen.setStyle(Qt::DashLine);
80 m_safeRect->setPen(pen);
81 m_safeRect->setZValue(5);
82 m_scene->addItem(m_safeRect);
84 checkBackgroundType(0);
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());
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);
101 //m_view.menu_preview->resizefitInView(0, 0, m_width, m_height);
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()));
110 connect(m_view.background_list, SIGNAL(activated(int)), this, SLOT(checkBackgroundType(int)));
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)));
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()));
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);
129 m_view.menu_box->setEnabled(false);
133 DvdWizardMenu::~DvdWizardMenu()
143 bool DvdWizardMenu::isComplete() const
145 m_view.error_message->setHidden(true);
146 if (!m_view.create_menu->isChecked()) return true;
148 QList<QGraphicsItem *> list = m_scene->items();
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) {
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();
164 m_view.error_message->setText(i18n("Buttons overlapping"));
165 m_view.error_message->setHidden(false);
171 targets.append(button->target());
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();
181 m_view.error_message->setText(i18n("No button in menu"));
182 m_view.error_message->setHidden(false);
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();
195 m_view.error_message->setText(i18n("Missing background image"));
196 m_view.error_message->setHidden(false);
202 #if KDE_IS_VERSION(4,7,0)
203 m_menuMessage->animatedHide();
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();
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);
226 void DvdWizardMenu::setButtonTarget(int ix)
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());
236 emit completeChanged();
239 void DvdWizardMenu::setBackToMenu(bool backToMenu)
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);
249 emit completeChanged();
252 void DvdWizardMenu::buttonChanged()
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);
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);
280 if (!foundButton) m_view.tabWidget->widget(0)->setEnabled(false);
283 void DvdWizardMenu::addButton()
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);
299 void DvdWizardMenu::deleteButton()
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) {
310 void DvdWizardMenu::changeProfile(bool isPal)
313 if (isPal == false) {
323 void DvdWizardMenu::updatePreview()
325 m_scene->setProfile(m_width, m_height);
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);
331 if (m_color) m_color->setRect(0, 0, m_width, m_height);
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);
338 void DvdWizardMenu::setTargets(QStringList list, QStringList targetlist)
340 m_view.target_list->clear();
341 m_view.target_list->addItem(i18n("Play All"), "jump title 1");
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));
347 m_view.target_list->addItem(KIcon("video-x-generic"), list.at(i), targetlist.at(i));
351 m_view.back_to_menu->setHidden(movieCount == 1);
354 void DvdWizardMenu::checkBackgroundType(int ix)
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);
362 m_view.background_color->setVisible(false);
363 m_view.background_image->setVisible(true);
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);
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);
378 void DvdWizardMenu::buildColor()
380 m_color->setBrush(m_view.background_color->color());
383 void DvdWizardMenu::buildImage()
385 emit completeChanged();
386 if (m_view.background_image->url().isEmpty()) {
387 if (m_background->scene() != 0) m_scene->removeItem(m_background);
392 if (m_view.background_list->currentIndex() == 1) {
394 if (!pix.load(m_view.background_image->url().path())) {
395 if (m_background->scene() != 0) m_scene->removeItem(m_background);
398 pix = pix.scaled(m_width, m_height);
399 } else if (m_view.background_list->currentIndex() == 2) {
402 if (m_isPal) w = 768;
404 pix = KThumb::getImage(m_view.background_image->url(), 0, w, m_height);
405 pix = pix.scaled(m_width, m_height);
407 m_background->setPixmap(pix);
408 m_scene->addItem(m_background);
411 void DvdWizardMenu::checkBackground()
413 if (m_view.background_list->currentIndex() != 1) {
414 if (m_background->scene() != 0) m_scene->removeItem(m_background);
416 m_scene->addItem(m_background);
420 void DvdWizardMenu::buildButton()
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));
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());
439 void DvdWizardMenu::updateColor()
441 updateColor(m_view.text_color->color());
442 m_view.menu_preview->viewport()->update();
445 void DvdWizardMenu::updateColor(QColor c)
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);
458 void DvdWizardMenu::createButtonImages(const QString &img1, const QString &img2, const QString &img3)
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());
470 QImage img(m_width, m_height, QImage::Format_Mono);
472 updateColor(Qt::black);
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 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));
488 #if QT_VERSION >= 0x040800
489 img.fill(Qt::transparent);
490 updateColor(m_view.highlighted_color->color());
495 p.setRenderHints(QPainter::Antialiasing, false);
496 p.setRenderHints(QPainter::TextAntialiasing, false);
497 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
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));
508 #if QT_VERSION >= 0x040800
509 img.fill(Qt::transparent);
510 updateColor(m_view.selected_color->color());
515 p.setRenderHints(QPainter::Antialiasing, false);
516 p.setRenderHints(QPainter::TextAntialiasing, false);
517 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
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));
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);
536 void DvdWizardMenu::createBackgroundImage(const QString &overlayMenu, const QString &img1)
539 if (m_view.background_list->currentIndex() == 0) {
542 img = QImage(768, 576, QImage::Format_ARGB32);
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());
549 if (img.width() != 768 || img.height() != 576)
550 img = img.scaled(768, 576, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
552 if (img.width() != 720 || img.height() != 540)
553 img = img.scaled(720, 540, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
556 // Overlay Normal menu
557 QImage menu(overlayMenu);
559 QRectF target(0, 0, img.width(), img.height());
560 QRectF src(0, 0, menu.width(), menu.height());
561 p.drawImage(target, menu, src);
566 bool DvdWizardMenu::createMenu() const
568 return m_view.create_menu->isChecked();
571 bool DvdWizardMenu::loopMovie() const
573 return m_view.loop_movie->isChecked();
576 bool DvdWizardMenu::menuMovie() const
578 return m_view.background_list->currentIndex() == 2;
581 QString DvdWizardMenu::menuMoviePath() const
583 return m_view.background_image->url().path();
587 QMap <QString, QRect> DvdWizardMenu::buttonsInfo()
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);
606 bool DvdWizardMenu::isPalMenu() const
611 QDomElement DvdWizardMenu::toXml() const
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) {
619 xml.setAttribute("background_color", m_view.background_color->color().name());
620 } else if (m_view.background_list->currentIndex() == 1) {
622 xml.setAttribute("background_image", m_view.background_image->url().path());
625 xml.setAttribute("background_video", m_view.background_image->url().path());
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());
631 QList<QGraphicsItem *> list = m_scene->items();
634 for (int i = 0; i < list.count(); i++) {
635 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
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);
651 return doc.documentElement();
655 void DvdWizardMenu::loadXml(QDomElement xml)
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")));
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"));
676 QDomNodeList buttons = xml.elementsByTagName("button");
677 kDebug() << "// LOADING MENU 2" << buttons.count();
679 if (buttons.count() > 0) {
680 // Clear existing buttons
681 QList<QGraphicsItem *> list = m_scene->items();
683 for (int i = 0; i < list.count(); i++) {
684 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
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());
709 void DvdWizardMenu::slotZoom()
711 m_view.menu_preview->scale(2.0, 2.0);
714 void DvdWizardMenu::slotUnZoom()
716 m_view.menu_preview->scale(0.5, 0.5);