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"
25 DvdWizardMenu::DvdWizardMenu(const QString &profile, QWidget *parent) :
31 m_view.play_text->setText(i18n("Play"));
32 m_scene = new DvdScene(this);
33 m_view.menu_preview->setScene(m_scene);
35 connect(m_view.create_menu, SIGNAL(toggled(bool)), m_view.menu_box, SLOT(setEnabled(bool)));
36 connect(m_view.create_menu, SIGNAL(toggled(bool)), this, SIGNAL(completeChanged()));
38 m_view.add_button->setIcon(KIcon("document-new"));
39 m_view.delete_button->setIcon(KIcon("trash-empty"));
40 m_view.zoom_button->setIcon(KIcon("zoom-in"));
41 m_view.unzoom_button->setIcon(KIcon("zoom-out"));
43 m_view.add_button->setToolTip(i18n("Add new button"));
44 m_view.delete_button->setToolTip(i18n("Delete current button"));
46 if (profile == "dv_ntsc" || profile == "dv_ntsc_wide") {
48 } 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 button->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
87 QRectF r = button->sceneBoundingRect();
88 m_scene->addItem(button);
89 button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
90 button->setSelected(true);
93 //m_view.menu_preview->resizefitInView(0, 0, m_width, m_height);
95 connect(m_view.play_text, SIGNAL(textChanged(const QString &)), this, SLOT(buildButton()));
96 connect(m_view.text_color, SIGNAL(changed(const QColor &)), this, SLOT(updateColor()));
97 connect(m_view.font_size, SIGNAL(valueChanged(int)), this, SLOT(buildButton()));
98 connect(m_view.font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(buildButton()));
99 connect(m_view.background_image, SIGNAL(textChanged(const QString &)), this, SLOT(buildImage()));
100 connect(m_view.background_color, SIGNAL(changed(const QColor &)), this, SLOT(buildColor()));
102 connect(m_view.background_list, SIGNAL(activated(int)), this, SLOT(checkBackgroundType(int)));
104 connect(m_view.target_list, SIGNAL(activated(int)), this, SLOT(setButtonTarget(int)));
105 connect(m_view.back_to_menu, SIGNAL(toggled(bool)), this, SLOT(setBackToMenu(bool)));
107 connect(m_view.add_button, SIGNAL(pressed()), this, SLOT(addButton()));
108 connect(m_view.delete_button, SIGNAL(pressed()), this, SLOT(deleteButton()));
109 connect(m_view.zoom_button, SIGNAL(pressed()), this, SLOT(slotZoom()));
110 connect(m_view.unzoom_button, SIGNAL(pressed()), this, SLOT(slotUnZoom()));
111 connect(m_scene, SIGNAL(selectionChanged()), this, SLOT(buttonChanged()));
112 connect(m_scene, SIGNAL(changed(const QList<QRectF> &)), this, SIGNAL(completeChanged()));
114 m_view.menu_box->setEnabled(false);
118 DvdWizardMenu::~DvdWizardMenu()
128 bool DvdWizardMenu::isComplete() const
130 if (!m_view.create_menu->isChecked()) return true;
132 QList<QGraphicsItem *> list = m_scene->items();
134 // check that the menu buttons don't collide
135 for (int i = 0; i < list.count(); i++) {
136 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
138 DvdButton *button = static_cast < DvdButton* >(list.at(i));
139 QList<QGraphicsItem *> collisions = button->collidingItems();
140 if (!collisions.isEmpty()) {
141 for (int j = 0; j < collisions.count(); j++) {
142 if (collisions.at(j)->type() == button->type()) return false;
145 targets.append(button->target());
148 if (buttonCount == 0) {
149 // We need at least one button
153 if (!m_view.background_image->isHidden()) {
154 // Make sure user selected a valid image / video file
155 if (!QFile::exists(m_view.background_image->url().path())) return false;
158 // check that we have a "Play all" entry
159 if (targets.contains(0)) return true;
160 // ... or that each video file has a button
161 for (int i = m_view.target_list->count() - 1; i > 0; i--) {
162 // If there is a vob file entry and it has no button assigned, don't allow to go further
163 if (m_view.target_list->itemIcon(i).isNull() == false && !targets.contains(i)) return false;
168 void DvdWizardMenu::setButtonTarget(int ix)
170 QList<QGraphicsItem *> list = m_scene->selectedItems();
171 for (int i = 0; i < list.count(); i++) {
172 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
173 DvdButton *button = static_cast < DvdButton* >(list.at(i));
174 button->setTarget(ix, m_view.target_list->itemData(ix).toString());
178 emit completeChanged();
181 void DvdWizardMenu::setBackToMenu(bool backToMenu)
183 QList<QGraphicsItem *> list = m_scene->selectedItems();
184 for (int i = 0; i < list.count(); i++) {
185 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
186 DvdButton *button = static_cast < DvdButton* >(list.at(i));
187 button->setBackMenu(backToMenu);
191 emit completeChanged();
194 void DvdWizardMenu::buttonChanged()
196 QList<QGraphicsItem *> list = m_scene->selectedItems();
197 bool foundButton = false;
198 for (int i = 0; i < list.count(); i++) {
199 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
200 m_view.play_text->blockSignals(true);
201 m_view.font_size->blockSignals(true);
202 m_view.font_family->blockSignals(true);
203 m_view.target_list->blockSignals(true);
204 m_view.back_to_menu->blockSignals(true);
206 m_view.tabWidget->widget(0)->setEnabled(true);
207 DvdButton *button = static_cast < DvdButton* >(list.at(i));
208 m_view.target_list->setCurrentIndex(button->target());
209 m_view.play_text->setText(button->toPlainText());
210 m_view.back_to_menu->setChecked(button->backMenu());
211 QFont font = button->font();
212 m_view.font_size->setValue(font.pixelSize());
213 m_view.font_family->setCurrentFont(font);
214 m_view.play_text->blockSignals(false);
215 m_view.font_size->blockSignals(false);
216 m_view.font_family->blockSignals(false);
217 m_view.target_list->blockSignals(false);
218 m_view.back_to_menu->blockSignals(false);
222 if (!foundButton) m_view.tabWidget->widget(0)->setEnabled(false);
225 void DvdWizardMenu::addButton()
227 m_scene->clearSelection();
228 DvdButton *button = new DvdButton(m_view.play_text->text());
229 QFont font = m_view.font_family->currentFont();
230 font.setPixelSize(m_view.font_size->value());
231 font.setStyleStrategy(QFont::NoAntialias);
232 button->setFont(font);
233 button->setDefaultTextColor(m_view.text_color->color());
234 button->setZValue(4);
235 button->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
236 QRectF r = button->sceneBoundingRect();
237 m_scene->addItem(button);
238 button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
239 button->setSelected(true);
242 void DvdWizardMenu::deleteButton()
244 QList<QGraphicsItem *> list = m_scene->selectedItems();
245 for (int i = 0; i < list.count(); i++) {
246 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
253 void DvdWizardMenu::changeProfile(bool isPal)
256 if (isPal == false) {
269 void DvdWizardMenu::updatePreview()
271 m_scene->setProfile(m_width, m_height);
273 matrix.scale(0.5, 0.5);
274 m_view.menu_preview->setMatrix(matrix);
275 m_view.menu_preview->setMinimumSize(m_width / 2 + 4, m_height / 2 + 8);
277 if (m_color) m_color->setRect(0, 0, m_width, m_height);
279 int safeW = m_width / 20;
280 int safeH = m_height / 20;
281 if (m_safeRect) m_safeRect->setRect(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
284 void DvdWizardMenu::setTargets(QStringList list, QStringList targetlist)
286 m_view.target_list->clear();
287 m_view.target_list->addItem(i18n("Play All"), "title 1");
289 for (int i = 0; i < list.count(); i++) {
290 if (targetlist.at(i).contains("chapter"))
291 m_view.target_list->addItem(list.at(i), targetlist.at(i));
293 m_view.target_list->addItem(KIcon("video-x-generic"), list.at(i), targetlist.at(i));
297 m_view.back_to_menu->setHidden(movieCount == 1);
300 void DvdWizardMenu::checkBackgroundType(int ix)
303 m_view.background_color->setVisible(true);
304 m_view.background_image->setVisible(false);
305 if (m_background->scene() != 0) m_scene->removeItem(m_background);
307 m_view.background_color->setVisible(false);
308 m_view.background_image->setVisible(true);
310 m_view.background_image->setFilter("*");
311 m_scene->addItem(m_background);
313 if (m_background->scene() != 0) m_scene->removeItem(m_background);
314 m_view.background_image->setFilter("video/mpeg");
319 void DvdWizardMenu::buildColor()
321 m_color->setBrush(m_view.background_color->color());
324 void DvdWizardMenu::buildImage()
326 emit completeChanged();
327 if (m_view.background_image->url().isEmpty()) {
328 if (m_background->scene() != 0) m_scene->removeItem(m_background);
332 if (!pix.load(m_view.background_image->url().path())) {
333 if (m_background->scene() != 0) m_scene->removeItem(m_background);
336 pix = pix.scaled(m_width, m_height);
337 m_background->setPixmap(pix);
338 if (m_view.background_list->currentIndex() == 1) m_scene->addItem(m_background);
341 void DvdWizardMenu::checkBackground()
343 if (m_view.background_list->currentIndex() != 1) {
344 if (m_background->scene() != 0) m_scene->removeItem(m_background);
346 m_scene->addItem(m_background);
350 void DvdWizardMenu::buildButton()
352 DvdButton *button = NULL;
353 QList<QGraphicsItem *> list = m_scene->selectedItems();
354 for (int i = 0; i < list.count(); i++) {
355 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
356 button = static_cast < DvdButton* >(list.at(i));
360 if (button == NULL) return;
361 button->setPlainText(m_view.play_text->text());
362 QFont font = m_view.font_family->currentFont();
363 font.setPixelSize(m_view.font_size->value());
364 font.setStyleStrategy(QFont::NoAntialias);
365 button->setFont(font);
366 button->setDefaultTextColor(m_view.text_color->color());
369 void DvdWizardMenu::updateColor()
371 updateColor(m_view.text_color->color());
372 m_view.menu_preview->viewport()->update();
375 void DvdWizardMenu::updateColor(QColor c)
377 DvdButton *button = NULL;
378 QList<QGraphicsItem *> list = m_scene->items();
379 for (int i = 0; i < list.count(); i++) {
380 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
381 button = static_cast < DvdButton* >(list.at(i));
382 button->setDefaultTextColor(c);
388 void DvdWizardMenu::createButtonImages(const QString &img1, const QString &img2, const QString &img3)
390 if (m_view.create_menu->isChecked()) {
391 m_scene->clearSelection();
392 QImage img(m_width, m_height, QImage::Format_ARGB32);
394 p.setRenderHints(QPainter::Antialiasing, false);
395 p.setRenderHints(QPainter::TextAntialiasing, false);
396 if (m_safeRect->scene() != 0) m_scene->removeItem(m_safeRect);
397 if (m_color->scene() != 0) m_scene->removeItem(m_color);
398 if (m_background->scene() != 0) m_scene->removeItem(m_background);
399 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
404 if (m_view.menu_profile->currentIndex() < 2)
405 saved = img.scaled(720, 576);
406 else saved = img.scaled(720, 480);
407 saved.setNumColors(4);
410 updateColor(m_view.selected_color->color());
412 p.setRenderHints(QPainter::Antialiasing, false);
413 p.setRenderHints(QPainter::TextAntialiasing, false);
414 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
416 /* if (m_view.menu_profile->currentIndex() < 2)
417 saved = img.scaled(720, 576);
418 else saved = img.scaled(720, 480);
419 saved.setNumColors(4);
425 updateColor(m_view.highlighted_color->color());
427 p.setRenderHints(QPainter::Antialiasing, false);
428 p.setRenderHints(QPainter::TextAntialiasing, false);
429 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
431 /*if (m_view.menu_profile->currentIndex() < 2)
432 saved = img.scaled(720, 576);
433 else saved = img.scaled(720, 480);
434 saved.setNumColors(4);
441 m_scene->addItem(m_safeRect);
442 m_scene->addItem(m_color);
443 if (m_view.background_list->currentIndex() == 1) m_scene->addItem(m_background);
448 void DvdWizardMenu::createBackgroundImage(const QString &img1)
451 if (m_view.background_list->currentIndex() == 0) {
454 img = QImage(768, 576, QImage::Format_ARGB32);
456 img = QImage(720, 540, QImage::Format_ARGB32);
457 img.fill(m_view.background_color->color().rgb());
458 } else if (m_view.background_list->currentIndex() == 1) {
459 img.load(m_view.background_image->url().path());
461 if (img.width() != 768 || img.height() != 576)
462 img = img.scaled(768, 576, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
464 if (img.width() != 720 || img.height() != 540)
465 img = img.scaled(720, 540, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
471 bool DvdWizardMenu::createMenu() const
473 return m_view.create_menu->isChecked();
476 bool DvdWizardMenu::menuMovie() const
478 return m_view.background_list->currentIndex() == 2;
481 QString DvdWizardMenu::menuMoviePath() const
483 return m_view.background_image->url().path();
487 QMap <QString, QRect> DvdWizardMenu::buttonsInfo()
489 QMap <QString, QRect> info;
490 QList<QGraphicsItem *> list = m_scene->items();
491 for (int i = 0; i < list.count(); i++) {
492 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
493 DvdButton *button = static_cast < DvdButton* >(list.at(i));
494 QRect r = list.at(i)->sceneBoundingRect().toRect();
495 // Make sure y1 is not odd (requested by spumux)
496 if (r.height() % 2 == 1) r.setHeight(r.height() + 1);
497 if (r.y() % 2 == 1) r.setY(r.y() - 1);
498 QString command = button->command();
499 if (button->backMenu()) command.prepend("g1 = 999;");
500 info.insertMulti(command, r);
506 bool DvdWizardMenu::isPalMenu() const
511 QDomElement DvdWizardMenu::toXml() const
514 QDomElement xml = doc.createElement("menu");
515 doc.appendChild(xml);
516 xml.setAttribute("enabled", m_view.create_menu->isChecked());
517 if (m_view.background_list->currentIndex() == 0) {
519 xml.setAttribute("background_color", m_view.background_color->color().name());
520 } else if (m_view.background_list->currentIndex() == 1) {
522 xml.setAttribute("background_image", m_view.background_image->url().path());
525 xml.setAttribute("background_video", m_view.background_image->url().path());
527 xml.setAttribute("text_color", m_view.text_color->color().name());
528 xml.setAttribute("selected_color", m_view.selected_color->color().name());
529 xml.setAttribute("highlighted_color", m_view.highlighted_color->color().name());
531 QList<QGraphicsItem *> list = m_scene->items();
534 for (int i = 0; i < list.count(); i++) {
535 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
537 DvdButton *button = static_cast < DvdButton* >(list.at(i));
538 QDomElement xmlbutton = doc.createElement("button");
539 xmlbutton.setAttribute("target", button->target());
540 xmlbutton.setAttribute("command", button->command());
541 xmlbutton.setAttribute("backtomenu", button->backMenu());
542 xmlbutton.setAttribute("posx", button->pos().x());
543 xmlbutton.setAttribute("posy", button->pos().y());
544 xmlbutton.setAttribute("text", button->toPlainText());
545 QFont font = button->font();
546 xmlbutton.setAttribute("font_size", font.pixelSize());
547 xmlbutton.setAttribute("font_family", font.family());
548 xml.appendChild(xmlbutton);
551 return doc.documentElement();
555 void DvdWizardMenu::loadXml(QDomElement xml)
557 kDebug() << "// LOADING MENU";
558 if (xml.isNull()) return;
559 kDebug() << "// LOADING MENU 1";
560 m_view.create_menu->setChecked(xml.attribute("enabled").toInt());
561 if (xml.hasAttribute("background_color")) {
562 m_view.background_list->setCurrentIndex(0);
563 m_view.background_color->setColor(xml.attribute("background_color"));
564 } else if (xml.hasAttribute("background_image")) {
565 m_view.background_list->setCurrentIndex(1);
566 m_view.background_image->setPath(xml.attribute("background_image"));
567 } else if (xml.hasAttribute("background_video")) {
568 m_view.background_list->setCurrentIndex(2);
569 m_view.background_image->setPath(xml.attribute("background_video"));
572 m_view.text_color->setColor(xml.attribute("text_color"));
573 m_view.selected_color->setColor(xml.attribute("selected_color"));
574 m_view.highlighted_color->setColor(xml.attribute("highlighted_color"));
576 QDomNodeList buttons = xml.elementsByTagName("button");
577 kDebug() << "// LOADING MENU 2" << buttons.count();
579 if (buttons.count() > 0) {
580 // Clear existing buttons
581 QList<QGraphicsItem *> list = m_scene->items();
583 for (int i = 0; i < list.count(); i++) {
584 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
591 for (int i = 0; i < buttons.count(); i++) {
592 QDomElement e = buttons.at(i).toElement();
593 // create menu button
594 DvdButton *button = new DvdButton(e.attribute("text"));
595 QFont font(e.attribute("font_family"));
596 font.setPixelSize(e.attribute("font_size").toInt());
597 font.setStyleStrategy(QFont::NoAntialias);
598 button->setFont(font);
599 button->setTarget(e.attribute("target").toInt(), e.attribute("command"));
600 button->setBackMenu(e.attribute("backtomenu").toInt());
601 button->setDefaultTextColor(m_view.text_color->color());
602 button->setZValue(4);
603 button->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
604 QRectF r = button->sceneBoundingRect();
605 m_scene->addItem(button);
606 button->setPos(e.attribute("posx").toDouble(), e.attribute("posy").toDouble());
611 void DvdWizardMenu::slotZoom()
613 m_view.menu_preview->scale(2.0, 2.0);
616 void DvdWizardMenu::slotUnZoom()
618 m_view.menu_preview->scale(0.5, 0.5);