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"));
41 m_view.add_button->setToolTip(i18n("Add new button"));
42 m_view.delete_button->setToolTip(i18n("Delete current button"));
44 if (profile == "dv_ntsc" || profile == "dv_ntsc_wide") {
46 } else changeProfile(true);
50 // Create color background
51 m_color = new QGraphicsRectItem(0, 0, m_width, m_height);
52 m_color->setBrush(m_view.background_color->color());
53 m_color->setZValue(2);
54 m_scene->addItem(m_color);
57 // create background image
58 m_background = new QGraphicsPixmapItem();
59 m_background->setZValue(3);
60 //m_scene->addItem(m_background);
62 // create safe zone rect
63 int safeW = m_width / 20;
64 int safeH = m_height / 20;
65 m_safeRect = new QGraphicsRectItem(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
67 pen.setStyle(Qt::DashLine);
69 m_safeRect->setPen(pen);
70 m_safeRect->setZValue(5);
71 m_scene->addItem(m_safeRect);
73 checkBackgroundType(0);
77 DvdButton *button = new DvdButton(m_view.play_text->text());
78 QFont font = m_view.font_family->currentFont();
79 font.setPixelSize(m_view.font_size->value());
80 font.setStyleStrategy(QFont::NoAntialias);
81 button->setFont(font);
82 button->setDefaultTextColor(m_view.text_color->color());
84 button->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
85 QRectF r = button->sceneBoundingRect();
86 m_scene->addItem(button);
87 button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
88 button->setSelected(true);
91 //m_view.menu_preview->resizefitInView(0, 0, m_width, m_height);
93 connect(m_view.play_text, SIGNAL(textChanged(const QString &)), this, SLOT(buildButton()));
94 connect(m_view.text_color, SIGNAL(changed(const QColor &)), this, SLOT(updateColor()));
95 connect(m_view.font_size, SIGNAL(valueChanged(int)), this, SLOT(buildButton()));
96 connect(m_view.font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(buildButton()));
97 connect(m_view.background_image, SIGNAL(textChanged(const QString &)), this, SLOT(buildImage()));
98 connect(m_view.background_color, SIGNAL(changed(const QColor &)), this, SLOT(buildColor()));
100 connect(m_view.background_list, SIGNAL(activated(int)), this, SLOT(checkBackgroundType(int)));
102 connect(m_view.target_list, SIGNAL(activated(int)), this, SLOT(setButtonTarget(int)));
103 connect(m_view.back_to_menu, SIGNAL(toggled(bool)), this, SLOT(setBackToMenu(bool)));
105 connect(m_view.add_button, SIGNAL(pressed()), this, SLOT(addButton()));
106 connect(m_view.delete_button, SIGNAL(pressed()), this, SLOT(deleteButton()));
107 connect(m_scene, SIGNAL(selectionChanged()), this, SLOT(buttonChanged()));
108 connect(m_scene, SIGNAL(changed(const QList<QRectF> &)), this, SIGNAL(completeChanged()));
110 m_view.menu_box->setEnabled(false);
114 DvdWizardMenu::~DvdWizardMenu()
124 bool DvdWizardMenu::isComplete() const
126 if (!m_view.create_menu->isChecked()) return true;
128 QList<QGraphicsItem *> list = m_scene->items();
130 // check that the menu buttons don't collide
131 for (int i = 0; i < list.count(); i++) {
132 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
134 DvdButton *button = static_cast < DvdButton* >(list.at(i));
135 QList<QGraphicsItem *> collisions = button->collidingItems();
136 if (!collisions.isEmpty()) {
137 for (int j = 0; j < collisions.count(); j++) {
138 if (collisions.at(j)->type() == button->type()) return false;
141 targets.append(button->target());
144 if (buttonCount == 0) {
145 // We need at least one button
149 if (!m_view.background_image->isHidden()) {
150 // Make sure user selected a valid image / video file
151 if (!QFile::exists(m_view.background_image->url().path())) return false;
154 // check that we have a "Play all" entry
155 if (targets.contains(0)) return true;
156 // ... or that each video file has a button
157 for (int i = m_view.target_list->count() - 1; i > 0; i--) {
158 // If there is a vob file entry and it has no button assigned, don't allow to go further
159 if (m_view.target_list->itemIcon(i).isNull() == false && !targets.contains(i)) return false;
164 void DvdWizardMenu::setButtonTarget(int ix)
166 QList<QGraphicsItem *> list = m_scene->selectedItems();
167 for (int i = 0; i < list.count(); i++) {
168 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
169 DvdButton *button = static_cast < DvdButton* >(list.at(i));
170 button->setTarget(ix, m_view.target_list->itemData(ix).toString());
174 emit completeChanged();
177 void DvdWizardMenu::setBackToMenu(bool backToMenu)
179 QList<QGraphicsItem *> list = m_scene->selectedItems();
180 for (int i = 0; i < list.count(); i++) {
181 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
182 DvdButton *button = static_cast < DvdButton* >(list.at(i));
183 button->setBackMenu(backToMenu);
187 emit completeChanged();
190 void DvdWizardMenu::buttonChanged()
192 QList<QGraphicsItem *> list = m_scene->selectedItems();
193 bool foundButton = false;
194 for (int i = 0; i < list.count(); i++) {
195 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
196 m_view.play_text->blockSignals(true);
197 m_view.font_size->blockSignals(true);
198 m_view.font_family->blockSignals(true);
199 m_view.target_list->blockSignals(true);
200 m_view.back_to_menu->blockSignals(true);
202 m_view.tabWidget->widget(0)->setEnabled(true);
203 DvdButton *button = static_cast < DvdButton* >(list.at(i));
204 m_view.target_list->setCurrentIndex(button->target());
205 m_view.play_text->setText(button->toPlainText());
206 m_view.back_to_menu->setChecked(button->backMenu());
207 QFont font = button->font();
208 m_view.font_size->setValue(font.pixelSize());
209 m_view.font_family->setCurrentFont(font);
210 m_view.play_text->blockSignals(false);
211 m_view.font_size->blockSignals(false);
212 m_view.font_family->blockSignals(false);
213 m_view.target_list->blockSignals(false);
214 m_view.back_to_menu->blockSignals(false);
218 if (!foundButton) m_view.tabWidget->widget(0)->setEnabled(false);
221 void DvdWizardMenu::addButton()
223 m_scene->clearSelection();
224 DvdButton *button = new DvdButton(m_view.play_text->text());
225 QFont font = m_view.font_family->currentFont();
226 font.setPixelSize(m_view.font_size->value());
227 font.setStyleStrategy(QFont::NoAntialias);
228 button->setFont(font);
229 button->setDefaultTextColor(m_view.text_color->color());
230 button->setZValue(4);
231 button->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
232 QRectF r = button->sceneBoundingRect();
233 m_scene->addItem(button);
234 button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
235 button->setSelected(true);
238 void DvdWizardMenu::deleteButton()
240 QList<QGraphicsItem *> list = m_scene->selectedItems();
241 for (int i = 0; i < list.count(); i++) {
242 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
249 void DvdWizardMenu::changeProfile(bool isPal)
252 if (isPal == false) {
265 void DvdWizardMenu::updatePreview()
267 m_scene->setProfile(m_width, m_height);
269 matrix.scale(0.5, 0.5);
270 m_view.menu_preview->setMatrix(matrix);
271 m_view.menu_preview->setMinimumSize(m_width / 2 + 4, m_height / 2 + 8);
273 if (m_color) m_color->setRect(0, 0, m_width, m_height);
275 int safeW = m_width / 20;
276 int safeH = m_height / 20;
277 if (m_safeRect) m_safeRect->setRect(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
280 void DvdWizardMenu::setTargets(QStringList list, QStringList targetlist)
282 m_view.target_list->clear();
283 m_view.target_list->addItem(i18n("Play All"), "title 1");
285 for (int i = 0; i < list.count(); i++) {
286 if (targetlist.at(i).contains("chapter"))
287 m_view.target_list->addItem(list.at(i), targetlist.at(i));
289 m_view.target_list->addItem(KIcon("video-x-generic"), list.at(i), targetlist.at(i));
293 m_view.back_to_menu->setHidden(movieCount == 1);
296 void DvdWizardMenu::checkBackgroundType(int ix)
299 m_view.background_color->setVisible(true);
300 m_view.background_image->setVisible(false);
301 m_scene->removeItem(m_background);
303 m_view.background_color->setVisible(false);
304 m_view.background_image->setVisible(true);
306 m_view.background_image->setFilter("*");
307 m_scene->addItem(m_background);
309 m_scene->removeItem(m_background);
310 m_view.background_image->setFilter("video/mpeg");
315 void DvdWizardMenu::buildColor()
317 m_color->setBrush(m_view.background_color->color());
320 void DvdWizardMenu::buildImage()
322 emit completeChanged();
323 if (m_view.background_image->url().isEmpty()) {
324 m_scene->removeItem(m_background);
328 if (!pix.load(m_view.background_image->url().path())) {
329 m_scene->removeItem(m_background);
332 pix = pix.scaled(m_width, m_height);
333 m_background->setPixmap(pix);
334 if (m_view.background_list->currentIndex() == 1) m_scene->addItem(m_background);
337 void DvdWizardMenu::checkBackground()
339 if (m_view.background_list->currentIndex() != 1) {
340 m_scene->removeItem(m_background);
342 m_scene->addItem(m_background);
346 void DvdWizardMenu::buildButton()
348 DvdButton *button = NULL;
349 QList<QGraphicsItem *> list = m_scene->selectedItems();
350 for (int i = 0; i < list.count(); i++) {
351 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
352 button = static_cast < DvdButton* >(list.at(i));
356 if (button == NULL) return;
357 button->setPlainText(m_view.play_text->text());
358 QFont font = m_view.font_family->currentFont();
359 font.setPixelSize(m_view.font_size->value());
360 font.setStyleStrategy(QFont::NoAntialias);
361 button->setFont(font);
362 button->setDefaultTextColor(m_view.text_color->color());
365 void DvdWizardMenu::updateColor()
367 updateColor(m_view.text_color->color());
368 m_view.menu_preview->viewport()->update();
371 void DvdWizardMenu::updateColor(QColor c)
373 DvdButton *button = NULL;
374 QList<QGraphicsItem *> list = m_scene->items();
375 for (int i = 0; i < list.count(); i++) {
376 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
377 button = static_cast < DvdButton* >(list.at(i));
378 button->setDefaultTextColor(c);
384 void DvdWizardMenu::createButtonImages(const QString &img1, const QString &img2, const QString &img3)
386 if (m_view.create_menu->isChecked()) {
387 m_scene->clearSelection();
388 QImage img(m_width, m_height, QImage::Format_ARGB32);
390 p.setRenderHints(QPainter::Antialiasing, false);
391 p.setRenderHints(QPainter::TextAntialiasing, false);
392 m_scene->removeItem(m_safeRect);
393 m_scene->removeItem(m_color);
394 m_scene->removeItem(m_background);
395 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
400 if (m_view.menu_profile->currentIndex() < 2)
401 saved = img.scaled(720, 576);
402 else saved = img.scaled(720, 480);
403 saved.setNumColors(4);
406 updateColor(m_view.selected_color->color());
408 p.setRenderHints(QPainter::Antialiasing, false);
409 p.setRenderHints(QPainter::TextAntialiasing, false);
410 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
412 /* if (m_view.menu_profile->currentIndex() < 2)
413 saved = img.scaled(720, 576);
414 else saved = img.scaled(720, 480);
415 saved.setNumColors(4);
421 updateColor(m_view.highlighted_color->color());
423 p.setRenderHints(QPainter::Antialiasing, false);
424 p.setRenderHints(QPainter::TextAntialiasing, false);
425 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
427 /*if (m_view.menu_profile->currentIndex() < 2)
428 saved = img.scaled(720, 576);
429 else saved = img.scaled(720, 480);
430 saved.setNumColors(4);
437 m_scene->addItem(m_safeRect);
438 m_scene->addItem(m_color);
439 if (m_view.background_list->currentIndex() == 1) m_scene->addItem(m_background);
444 void DvdWizardMenu::createBackgroundImage(const QString &img1)
447 if (m_view.background_list->currentIndex() == 0) {
450 img = QImage(768, 576, QImage::Format_ARGB32);
452 img = QImage(720, 540, QImage::Format_ARGB32);
453 img.fill(m_view.background_color->color().rgb());
454 } else if (m_view.background_list->currentIndex() == 1) {
455 img.load(m_view.background_image->url().path());
457 if (img.width() != 768 || img.height() != 576)
458 img = img.scaled(768, 576, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
460 if (img.width() != 720 || img.height() != 540)
461 img = img.scaled(720, 540, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
467 bool DvdWizardMenu::createMenu() const
469 return m_view.create_menu->isChecked();
472 bool DvdWizardMenu::menuMovie() const
474 return m_view.background_list->currentIndex() == 2;
477 QString DvdWizardMenu::menuMoviePath() const
479 return m_view.background_image->url().path();
483 QMap <QString, QRect> DvdWizardMenu::buttonsInfo()
485 QMap <QString, QRect> info;
486 QList<QGraphicsItem *> list = m_scene->items();
487 for (int i = 0; i < list.count(); i++) {
488 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
489 DvdButton *button = static_cast < DvdButton* >(list.at(i));
490 QRect r = list.at(i)->sceneBoundingRect().toRect();
491 // Make sure y1 is not odd (requested by spumux)
492 if (r.height() % 2 == 1) r.setHeight(r.height() + 1);
493 if (r.y() % 2 == 1) r.setY(r.y() - 1);
494 QString command = button->command();
495 if (button->backMenu()) command.prepend("g1 = 999;");
496 info.insertMulti(command, r);
502 bool DvdWizardMenu::isPalMenu() const
507 QDomElement DvdWizardMenu::toXml() const
510 if (!m_view.create_menu->isChecked()) return doc.documentElement();
511 QDomElement xml = doc.createElement("menu");
512 doc.appendChild(xml);
513 if (m_view.background_list->currentIndex() == 0) {
515 xml.setAttribute("background_color", m_view.background_color->color().name());
516 } else if (m_view.background_list->currentIndex() == 1) {
518 xml.setAttribute("background_image", m_view.background_image->text());
521 xml.setAttribute("background_video", m_view.background_image->text());
523 xml.setAttribute("text_color", m_view.text_color->color().name());
524 xml.setAttribute("selected_color", m_view.selected_color->color().name());
525 xml.setAttribute("highlighted_color", m_view.highlighted_color->color().name());
527 QList<QGraphicsItem *> list = m_scene->items();
530 for (int i = 0; i < list.count(); i++) {
531 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
533 DvdButton *button = static_cast < DvdButton* >(list.at(i));
534 QDomElement xmlbutton = doc.createElement("button");
535 xmlbutton.setAttribute("target", button->target());
536 xmlbutton.setAttribute("command", button->command());
537 xmlbutton.setAttribute("backtomenu", button->backMenu());
538 xmlbutton.setAttribute("posx", button->pos().x());
539 xmlbutton.setAttribute("posy", button->pos().y());
540 xmlbutton.setAttribute("text", button->toPlainText());
541 QFont font = button->font();
542 xmlbutton.setAttribute("font_size", font.pixelSize());
543 xmlbutton.setAttribute("font_family", font.family());
544 xml.appendChild(xmlbutton);
547 return doc.documentElement();
551 void DvdWizardMenu::loadXml(QDomElement xml)
553 kDebug() << "// LOADING MENU";
554 if (xml.isNull()) return;
555 kDebug() << "// LOADING MENU 1";
556 m_view.create_menu->setChecked(true) ;
557 if (xml.hasAttribute("background_color")) {
558 m_view.background_list->setCurrentIndex(0);
559 m_view.background_color->setColor(xml.attribute("background_color"));
560 } else if (xml.hasAttribute("background_image")) {
561 m_view.background_list->setCurrentIndex(1);
562 m_view.background_image->setPath(xml.attribute("background_image"));
563 } else if (xml.hasAttribute("background_video")) {
564 m_view.background_list->setCurrentIndex(2);
565 m_view.background_image->setPath(xml.attribute("background_video"));
568 m_view.text_color->setColor(xml.attribute("text_color"));
569 m_view.selected_color->setColor(xml.attribute("selected_color"));
570 m_view.highlighted_color->setColor(xml.attribute("highlighted_color"));
572 QDomNodeList buttons = xml.elementsByTagName("button");
573 kDebug() << "// LOADING MENU 2" << buttons.count();
575 if (buttons.count() > 0) {
576 // Clear existing buttons
577 QList<QGraphicsItem *> list = m_scene->items();
579 for (int i = 0; i < list.count(); i++) {
580 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
587 for (int i = 0; i < buttons.count(); i++) {
588 QDomElement e = buttons.at(i).toElement();
589 // create menu button
590 DvdButton *button = new DvdButton(e.attribute("text"));
591 QFont font(e.attribute("font_family"));
592 font.setPixelSize(e.attribute("font_size").toInt());
593 font.setStyleStrategy(QFont::NoAntialias);
594 button->setFont(font);
595 button->setTarget(e.attribute("target").toInt(), e.attribute("command"));
596 button->setBackMenu(e.attribute("backtomenu").toInt());
597 button->setDefaultTextColor(m_view.text_color->color());
598 button->setZValue(4);
599 button->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
600 QRectF r = button->sceneBoundingRect();
601 m_scene->addItem(button);
602 button->setPos(e.attribute("posx").toDouble(), e.attribute("posy").toDouble());