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 m_view.add_button->setIcon(KIcon("document-new"));
41 m_view.delete_button->setIcon(KIcon("trash-empty"));
42 m_view.zoom_button->setIcon(KIcon("zoom-in"));
43 m_view.unzoom_button->setIcon(KIcon("zoom-out"));
45 m_view.add_button->setToolTip(i18n("Add new button"));
46 m_view.delete_button->setToolTip(i18n("Delete current button"));
48 if (profile == "dv_ntsc" || profile == "dv_ntsc_wide") {
50 } else changeProfile(true);
53 // Create color background
54 m_color = new QGraphicsRectItem(0, 0, m_width, m_height);
55 m_color->setBrush(m_view.background_color->color());
56 m_color->setZValue(2);
57 m_scene->addItem(m_color);
60 // create background image
61 m_background = new QGraphicsPixmapItem();
62 m_background->setZValue(3);
63 //m_scene->addItem(m_background);
65 // create safe zone rect
66 int safeW = m_width / 20;
67 int safeH = m_height / 20;
68 m_safeRect = new QGraphicsRectItem(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
70 pen.setStyle(Qt::DashLine);
72 m_safeRect->setPen(pen);
73 m_safeRect->setZValue(5);
74 m_scene->addItem(m_safeRect);
76 checkBackgroundType(0);
80 DvdButton *button = new DvdButton(m_view.play_text->text());
81 QFont font = m_view.font_family->currentFont();
82 font.setPixelSize(m_view.font_size->value());
83 font.setStyleStrategy(QFont::NoAntialias);
84 button->setFont(font);
85 button->setDefaultTextColor(m_view.text_color->color());
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 // red background for error message
115 KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Window, KSharedConfig::openConfig(KdenliveSettings::colortheme()));
116 QPalette p = m_view.error_message->palette();
117 p.setColor(QPalette::Background, scheme.background(KColorScheme::NegativeBackground).color());
118 m_view.error_message->setAutoFillBackground(true);
119 m_view.error_message->setPalette(p);
121 m_view.menu_box->setEnabled(false);
125 DvdWizardMenu::~DvdWizardMenu()
135 bool DvdWizardMenu::isComplete() const
137 m_view.error_message->setHidden(true);
138 if (!m_view.create_menu->isChecked()) return true;
140 QList<QGraphicsItem *> list = m_scene->items();
142 // check that the menu buttons don't collide
143 for (int i = 0; i < list.count(); i++) {
144 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
146 DvdButton *button = static_cast < DvdButton* >(list.at(i));
147 QList<QGraphicsItem *> collisions = button->collidingItems();
148 if (!collisions.isEmpty()) {
149 for (int j = 0; j < collisions.count(); j++) {
150 if (collisions.at(j)->type() == button->type()) {
151 m_view.error_message->setText(i18n("Buttons overlapping"));
152 m_view.error_message->setHidden(false);
157 targets.append(button->target());
160 if (buttonCount == 0) {
161 //We need at least one button
162 m_view.error_message->setText(i18n("No button in menu"));
163 m_view.error_message->setHidden(false);
167 if (!m_view.background_image->isHidden()) {
168 // Make sure user selected a valid image / video file
169 if (!QFile::exists(m_view.background_image->url().path())) {
170 m_view.error_message->setText(i18n("Missing background image"));
171 m_view.error_message->setHidden(false);
176 // check that we have a "Play all" entry
177 if (targets.contains(0)) return true;
178 // ... or that each video file has a button
179 for (int i = m_view.target_list->count() - 1; i > 0; i--) {
180 // If there is a vob file entry and it has no button assigned, don't allow to go further
181 if (m_view.target_list->itemIcon(i).isNull() == false && !targets.contains(i)) {
182 m_view.error_message->setText(i18n("No menu entry for %1", m_view.target_list->itemText(i)));
183 m_view.error_message->setHidden(false);
190 void DvdWizardMenu::setButtonTarget(int ix)
192 QList<QGraphicsItem *> list = m_scene->selectedItems();
193 for (int i = 0; i < list.count(); i++) {
194 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
195 DvdButton *button = static_cast < DvdButton* >(list.at(i));
196 button->setTarget(ix, m_view.target_list->itemData(ix).toString());
200 emit completeChanged();
203 void DvdWizardMenu::setBackToMenu(bool backToMenu)
205 QList<QGraphicsItem *> list = m_scene->selectedItems();
206 for (int i = 0; i < list.count(); i++) {
207 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
208 DvdButton *button = static_cast < DvdButton* >(list.at(i));
209 button->setBackMenu(backToMenu);
213 emit completeChanged();
216 void DvdWizardMenu::buttonChanged()
218 QList<QGraphicsItem *> list = m_scene->selectedItems();
219 bool foundButton = false;
220 for (int i = 0; i < list.count(); i++) {
221 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
222 m_view.play_text->blockSignals(true);
223 m_view.font_size->blockSignals(true);
224 m_view.font_family->blockSignals(true);
225 m_view.target_list->blockSignals(true);
226 m_view.back_to_menu->blockSignals(true);
228 m_view.tabWidget->widget(0)->setEnabled(true);
229 DvdButton *button = static_cast < DvdButton* >(list.at(i));
230 m_view.target_list->setCurrentIndex(button->target());
231 m_view.play_text->setText(button->toPlainText());
232 m_view.back_to_menu->setChecked(button->backMenu());
233 QFont font = button->font();
234 m_view.font_size->setValue(font.pixelSize());
235 m_view.font_family->setCurrentFont(font);
236 m_view.play_text->blockSignals(false);
237 m_view.font_size->blockSignals(false);
238 m_view.font_family->blockSignals(false);
239 m_view.target_list->blockSignals(false);
240 m_view.back_to_menu->blockSignals(false);
244 if (!foundButton) m_view.tabWidget->widget(0)->setEnabled(false);
247 void DvdWizardMenu::addButton()
249 m_scene->clearSelection();
250 DvdButton *button = new DvdButton(m_view.play_text->text());
251 QFont font = m_view.font_family->currentFont();
252 font.setPixelSize(m_view.font_size->value());
253 font.setStyleStrategy(QFont::NoAntialias);
254 button->setFont(font);
255 button->setDefaultTextColor(m_view.text_color->color());
256 button->setZValue(4);
257 QRectF r = button->sceneBoundingRect();
258 m_scene->addItem(button);
259 button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
260 button->setSelected(true);
263 void DvdWizardMenu::deleteButton()
265 QList<QGraphicsItem *> list = m_scene->selectedItems();
266 for (int i = 0; i < list.count(); i++) {
267 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
274 void DvdWizardMenu::changeProfile(bool isPal)
277 if (isPal == false) {
287 void DvdWizardMenu::updatePreview()
289 m_scene->setProfile(m_width, m_height);
291 matrix.scale(0.5, 0.5);
292 m_view.menu_preview->setMatrix(matrix);
293 m_view.menu_preview->setMinimumSize(m_width / 2 + 4, m_height / 2 + 8);
295 if (m_color) m_color->setRect(0, 0, m_width, m_height);
297 int safeW = m_width / 20;
298 int safeH = m_height / 20;
299 if (m_safeRect) m_safeRect->setRect(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
302 void DvdWizardMenu::setTargets(QStringList list, QStringList targetlist)
304 m_view.target_list->clear();
305 m_view.target_list->addItem(i18n("Play All"), "jump title 1");
307 for (int i = 0; i < list.count(); i++) {
308 if (targetlist.at(i).contains("chapter"))
309 m_view.target_list->addItem(list.at(i), targetlist.at(i));
311 m_view.target_list->addItem(KIcon("video-x-generic"), list.at(i), targetlist.at(i));
315 m_view.back_to_menu->setHidden(movieCount == 1);
318 void DvdWizardMenu::checkBackgroundType(int ix)
321 m_view.background_color->setVisible(true);
322 m_view.background_image->setVisible(false);
323 m_view.loop_movie->setVisible(false);
324 if (m_background->scene() != 0) m_scene->removeItem(m_background);
326 m_view.background_color->setVisible(false);
327 m_view.background_image->setVisible(true);
329 m_view.background_image->clear();
330 m_view.background_image->setFilter("*");
331 if (m_background->scene() != 0) m_scene->removeItem(m_background);
332 m_view.loop_movie->setVisible(false);
334 if (m_background->scene() != 0) m_scene->removeItem(m_background);
335 m_view.background_image->clear();
336 m_view.background_image->setFilter("video/mpeg");
337 m_view.loop_movie->setVisible(true);
342 void DvdWizardMenu::buildColor()
344 m_color->setBrush(m_view.background_color->color());
347 void DvdWizardMenu::buildImage()
349 emit completeChanged();
350 if (m_view.background_image->url().isEmpty()) {
351 if (m_background->scene() != 0) m_scene->removeItem(m_background);
356 if (m_view.background_list->currentIndex() == 1) {
358 if (!pix.load(m_view.background_image->url().path())) {
359 if (m_background->scene() != 0) m_scene->removeItem(m_background);
362 pix = pix.scaled(m_width, m_height);
363 } else if (m_view.background_list->currentIndex() == 2) {
366 if (m_isPal) w = 768;
368 pix = KThumb::getImage(m_view.background_image->url(), 0, w, m_height);
369 pix = pix.scaled(m_width, m_height);
371 m_background->setPixmap(pix);
372 m_scene->addItem(m_background);
375 void DvdWizardMenu::checkBackground()
377 if (m_view.background_list->currentIndex() != 1) {
378 if (m_background->scene() != 0) m_scene->removeItem(m_background);
380 m_scene->addItem(m_background);
384 void DvdWizardMenu::buildButton()
386 DvdButton *button = NULL;
387 QList<QGraphicsItem *> list = m_scene->selectedItems();
388 for (int i = 0; i < list.count(); i++) {
389 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
390 button = static_cast < DvdButton* >(list.at(i));
394 if (button == NULL) return;
395 button->setPlainText(m_view.play_text->text());
396 QFont font = m_view.font_family->currentFont();
397 font.setPixelSize(m_view.font_size->value());
398 font.setStyleStrategy(QFont::NoAntialias);
399 button->setFont(font);
400 button->setDefaultTextColor(m_view.text_color->color());
403 void DvdWizardMenu::updateColor()
405 updateColor(m_view.text_color->color());
406 m_view.menu_preview->viewport()->update();
409 void DvdWizardMenu::updateColor(QColor c)
411 DvdButton *button = NULL;
412 QList<QGraphicsItem *> list = m_scene->items();
413 for (int i = 0; i < list.count(); i++) {
414 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
415 button = static_cast < DvdButton* >(list.at(i));
416 button->setDefaultTextColor(c);
422 void DvdWizardMenu::createButtonImages(const QString &img1, const QString &img2, const QString &img3)
424 if (m_view.create_menu->isChecked()) {
425 m_scene->clearSelection();
426 if (m_safeRect->scene() != 0) m_scene->removeItem(m_safeRect);
427 if (m_color->scene() != 0) m_scene->removeItem(m_color);
428 if (m_background->scene() != 0) m_scene->removeItem(m_background);
430 QImage img(m_width, m_height, QImage::Format_Mono);
432 updateColor(Qt::black);
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.setColor(0, m_view.text_color->color().rgb());
440 img.setColor(1, qRgba(0,0,0,0));
448 p.setRenderHints(QPainter::Antialiasing, false);
449 p.setRenderHints(QPainter::TextAntialiasing, false);
450 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
452 #if QT_VERSION >= 0x040600
453 img.setColor(0, m_view.highlighted_color->color().rgb());
454 img.setColor(1, qRgba(0,0,0,0));
462 p.setRenderHints(QPainter::Antialiasing, false);
463 p.setRenderHints(QPainter::TextAntialiasing, false);
464 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
467 #if QT_VERSION >= 0x040600
468 img.setColor(0, m_view.selected_color->color().rgb());
469 img.setColor(1, qRgba(0,0,0,0));
476 m_scene->addItem(m_safeRect);
477 m_scene->addItem(m_color);
478 if (m_view.background_list->currentIndex() > 0) m_scene->addItem(m_background);
483 void DvdWizardMenu::createBackgroundImage(const QString &img1)
486 if (m_view.background_list->currentIndex() == 0) {
489 img = QImage(768, 576, QImage::Format_ARGB32);
491 img = QImage(720, 540, QImage::Format_ARGB32);
492 img.fill(m_view.background_color->color().rgb());
493 } else if (m_view.background_list->currentIndex() == 1) {
494 img.load(m_view.background_image->url().path());
496 if (img.width() != 768 || img.height() != 576)
497 img = img.scaled(768, 576, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
499 if (img.width() != 720 || img.height() != 540)
500 img = img.scaled(720, 540, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
506 bool DvdWizardMenu::createMenu() const
508 return m_view.create_menu->isChecked();
511 bool DvdWizardMenu::loopMovie() const
513 return m_view.loop_movie->isChecked();
516 bool DvdWizardMenu::menuMovie() const
518 return m_view.background_list->currentIndex() == 2;
521 QString DvdWizardMenu::menuMoviePath() const
523 return m_view.background_image->url().path();
527 QMap <QString, QRect> DvdWizardMenu::buttonsInfo()
529 QMap <QString, QRect> info;
530 QList<QGraphicsItem *> list = m_scene->items();
531 for (int i = 0; i < list.count(); i++) {
532 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
533 DvdButton *button = static_cast < DvdButton* >(list.at(i));
534 QRect r = list.at(i)->sceneBoundingRect().toRect();
535 // Make sure y1 is not odd (requested by spumux)
536 if (r.height() % 2 == 1) r.setHeight(r.height() + 1);
537 if (r.y() % 2 == 1) r.setY(r.y() - 1);
538 QString command = button->command();
539 if (button->backMenu()) command.prepend("g1 = 999;");
540 info.insertMulti(command, r);
546 bool DvdWizardMenu::isPalMenu() const
551 QDomElement DvdWizardMenu::toXml() const
554 QDomElement xml = doc.createElement("menu");
555 doc.appendChild(xml);
556 xml.setAttribute("enabled", m_view.create_menu->isChecked());
557 if (m_view.background_list->currentIndex() == 0) {
559 xml.setAttribute("background_color", m_view.background_color->color().name());
560 } else if (m_view.background_list->currentIndex() == 1) {
562 xml.setAttribute("background_image", m_view.background_image->url().path());
565 xml.setAttribute("background_video", m_view.background_image->url().path());
567 xml.setAttribute("text_color", m_view.text_color->color().name());
568 xml.setAttribute("selected_color", m_view.selected_color->color().name());
569 xml.setAttribute("highlighted_color", m_view.highlighted_color->color().name());
571 QList<QGraphicsItem *> list = m_scene->items();
574 for (int i = 0; i < list.count(); i++) {
575 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
577 DvdButton *button = static_cast < DvdButton* >(list.at(i));
578 QDomElement xmlbutton = doc.createElement("button");
579 xmlbutton.setAttribute("target", button->target());
580 xmlbutton.setAttribute("command", button->command());
581 xmlbutton.setAttribute("backtomenu", button->backMenu());
582 xmlbutton.setAttribute("posx", button->pos().x());
583 xmlbutton.setAttribute("posy", button->pos().y());
584 xmlbutton.setAttribute("text", button->toPlainText());
585 QFont font = button->font();
586 xmlbutton.setAttribute("font_size", font.pixelSize());
587 xmlbutton.setAttribute("font_family", font.family());
588 xml.appendChild(xmlbutton);
591 return doc.documentElement();
595 void DvdWizardMenu::loadXml(QDomElement xml)
597 kDebug() << "// LOADING MENU";
598 if (xml.isNull()) return;
599 kDebug() << "// LOADING MENU 1";
600 m_view.create_menu->setChecked(xml.attribute("enabled").toInt());
601 if (xml.hasAttribute("background_color")) {
602 m_view.background_list->setCurrentIndex(0);
603 m_view.background_color->setColor(xml.attribute("background_color"));
604 } else if (xml.hasAttribute("background_image")) {
605 m_view.background_list->setCurrentIndex(1);
606 m_view.background_image->setUrl(KUrl(xml.attribute("background_image")));
607 } else if (xml.hasAttribute("background_video")) {
608 m_view.background_list->setCurrentIndex(2);
609 m_view.background_image->setUrl(KUrl(xml.attribute("background_video")));
612 m_view.text_color->setColor(xml.attribute("text_color"));
613 m_view.selected_color->setColor(xml.attribute("selected_color"));
614 m_view.highlighted_color->setColor(xml.attribute("highlighted_color"));
616 QDomNodeList buttons = xml.elementsByTagName("button");
617 kDebug() << "// LOADING MENU 2" << buttons.count();
619 if (buttons.count() > 0) {
620 // Clear existing buttons
621 QList<QGraphicsItem *> list = m_scene->items();
623 for (int i = 0; i < list.count(); i++) {
624 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
631 for (int i = 0; i < buttons.count(); i++) {
632 QDomElement e = buttons.at(i).toElement();
633 // create menu button
634 DvdButton *button = new DvdButton(e.attribute("text"));
635 QFont font(e.attribute("font_family"));
636 font.setPixelSize(e.attribute("font_size").toInt());
637 font.setStyleStrategy(QFont::NoAntialias);
638 button->setFont(font);
639 button->setTarget(e.attribute("target").toInt(), e.attribute("command"));
640 button->setBackMenu(e.attribute("backtomenu").toInt());
641 button->setDefaultTextColor(m_view.text_color->color());
642 button->setZValue(4);
643 QRectF r = button->sceneBoundingRect();
644 m_scene->addItem(button);
645 button->setPos(e.attribute("posx").toDouble(), e.attribute("posy").toDouble());
650 void DvdWizardMenu::slotZoom()
652 m_view.menu_preview->scale(2.0, 2.0);
655 void DvdWizardMenu::slotUnZoom()
657 m_view.menu_preview->scale(0.5, 0.5);