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 m_view.menu_profile->addItems(QStringList() << i18n("PAL") << i18n("NTSC"));
46 if (profile == "dv_ntsc" || profile == "dv_ntsc_wide") {
47 m_view.menu_profile->setCurrentIndex(1);
49 } else m_isPal = true;
51 changeProfile(m_view.menu_profile->currentIndex());
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);
78 connect(m_view.menu_profile, SIGNAL(activated(int)), this, SLOT(changeProfile(int)));
81 DvdButton *button = new DvdButton(m_view.play_text->text());
82 QFont font = m_view.font_family->currentFont();
83 font.setPixelSize(m_view.font_size->value());
84 font.setStyleStrategy(QFont::NoAntialias);
85 button->setFont(font);
86 button->setDefaultTextColor(m_view.text_color->color());
88 button->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
89 QRectF r = button->sceneBoundingRect();
90 m_scene->addItem(button);
91 button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
92 button->setSelected(true);
95 //m_view.menu_preview->resizefitInView(0, 0, m_width, m_height);
97 connect(m_view.play_text, SIGNAL(textChanged(const QString &)), this, SLOT(buildButton()));
98 connect(m_view.text_color, SIGNAL(changed(const QColor &)), this, SLOT(updateColor()));
99 connect(m_view.font_size, SIGNAL(valueChanged(int)), this, SLOT(buildButton()));
100 connect(m_view.font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(buildButton()));
101 connect(m_view.background_image, SIGNAL(textChanged(const QString &)), this, SLOT(buildImage()));
102 connect(m_view.background_color, SIGNAL(changed(const QColor &)), this, SLOT(buildColor()));
104 connect(m_view.background_list, SIGNAL(activated(int)), this, SLOT(checkBackgroundType(int)));
106 connect(m_view.target_list, SIGNAL(activated(int)), this, SLOT(setButtonTarget(int)));
107 connect(m_view.back_to_menu, SIGNAL(toggled(bool)), this, SLOT(setBackToMenu(bool)));
109 connect(m_view.add_button, SIGNAL(pressed()), this, SLOT(addButton()));
110 connect(m_view.delete_button, SIGNAL(pressed()), this, SLOT(deleteButton()));
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(int ix)
271 void DvdWizardMenu::updatePreview()
273 m_scene->setProfile(m_width, m_height);
275 matrix.scale(0.5, 0.5);
276 m_view.menu_preview->setMatrix(matrix);
277 m_view.menu_preview->setMinimumSize(m_width / 2 + 4, m_height / 2 + 8);
279 if (m_color) m_color->setRect(0, 0, m_width, m_height);
281 int safeW = m_width / 20;
282 int safeH = m_height / 20;
283 if (m_safeRect) m_safeRect->setRect(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
286 void DvdWizardMenu::setTargets(QStringList list, QStringList targetlist)
288 m_view.target_list->clear();
289 m_view.target_list->addItem(i18n("Play All"), "title 1");
291 for (int i = 0; i < list.count(); i++) {
292 if (targetlist.at(i).contains("chapter"))
293 m_view.target_list->addItem(list.at(i), targetlist.at(i));
295 m_view.target_list->addItem(KIcon("video-x-generic"), list.at(i), targetlist.at(i));
299 m_view.back_to_menu->setHidden(movieCount == 1);
302 void DvdWizardMenu::checkBackgroundType(int ix)
305 m_view.background_color->setVisible(true);
306 m_view.background_image->setVisible(false);
307 m_scene->removeItem(m_background);
309 m_view.background_color->setVisible(false);
310 m_view.background_image->setVisible(true);
312 m_view.background_image->setFilter("*");
313 m_scene->addItem(m_background);
315 m_scene->removeItem(m_background);
316 m_view.background_image->setFilter("video/mpeg");
321 void DvdWizardMenu::buildColor()
323 m_color->setBrush(m_view.background_color->color());
326 void DvdWizardMenu::buildImage()
328 emit completeChanged();
329 if (m_view.background_image->url().isEmpty()) {
330 m_scene->removeItem(m_background);
334 if (!pix.load(m_view.background_image->url().path())) {
335 m_scene->removeItem(m_background);
338 pix = pix.scaled(m_width, m_height);
339 m_background->setPixmap(pix);
340 if (m_view.background_list->currentIndex() == 1) m_scene->addItem(m_background);
343 void DvdWizardMenu::checkBackground()
345 if (m_view.background_list->currentIndex() != 1) {
346 m_scene->removeItem(m_background);
348 m_scene->addItem(m_background);
352 void DvdWizardMenu::buildButton()
354 DvdButton *button = NULL;
355 QList<QGraphicsItem *> list = m_scene->selectedItems();
356 for (int i = 0; i < list.count(); i++) {
357 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
358 button = static_cast < DvdButton* >(list.at(i));
362 if (button == NULL) return;
363 button->setPlainText(m_view.play_text->text());
364 QFont font = m_view.font_family->currentFont();
365 font.setPixelSize(m_view.font_size->value());
366 font.setStyleStrategy(QFont::NoAntialias);
367 button->setFont(font);
368 button->setDefaultTextColor(m_view.text_color->color());
371 void DvdWizardMenu::updateColor()
373 updateColor(m_view.text_color->color());
374 m_view.menu_preview->viewport()->update();
377 void DvdWizardMenu::updateColor(QColor c)
379 DvdButton *button = NULL;
380 QList<QGraphicsItem *> list = m_scene->items();
381 for (int i = 0; i < list.count(); i++) {
382 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
383 button = static_cast < DvdButton* >(list.at(i));
384 button->setDefaultTextColor(c);
390 void DvdWizardMenu::createButtonImages(const QString &img1, const QString &img2, const QString &img3)
392 if (m_view.create_menu->isChecked()) {
393 m_scene->clearSelection();
394 QImage img(m_width, m_height, QImage::Format_ARGB32);
396 p.setRenderHints(QPainter::Antialiasing, false);
397 p.setRenderHints(QPainter::TextAntialiasing, false);
398 m_scene->removeItem(m_safeRect);
399 m_scene->removeItem(m_color);
400 m_scene->removeItem(m_background);
401 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
406 if (m_view.menu_profile->currentIndex() < 2)
407 saved = img.scaled(720, 576);
408 else saved = img.scaled(720, 480);
409 saved.setNumColors(4);
412 updateColor(m_view.selected_color->color());
414 p.setRenderHints(QPainter::Antialiasing, false);
415 p.setRenderHints(QPainter::TextAntialiasing, false);
416 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
418 /* if (m_view.menu_profile->currentIndex() < 2)
419 saved = img.scaled(720, 576);
420 else saved = img.scaled(720, 480);
421 saved.setNumColors(4);
427 updateColor(m_view.highlighted_color->color());
429 p.setRenderHints(QPainter::Antialiasing, false);
430 p.setRenderHints(QPainter::TextAntialiasing, false);
431 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
433 /*if (m_view.menu_profile->currentIndex() < 2)
434 saved = img.scaled(720, 576);
435 else saved = img.scaled(720, 480);
436 saved.setNumColors(4);
443 m_scene->addItem(m_safeRect);
444 m_scene->addItem(m_color);
445 if (m_view.background_list->currentIndex() == 1) m_scene->addItem(m_background);
450 void DvdWizardMenu::createBackgroundImage(const QString &img1)
453 if (m_view.background_list->currentIndex() == 0) {
456 img = QImage(768, 576, QImage::Format_ARGB32);
458 img = QImage(720, 540, QImage::Format_ARGB32);
459 img.fill(m_view.background_color->color().rgb());
460 } else if (m_view.background_list->currentIndex() == 1) {
461 img.load(m_view.background_image->url().path());
463 if (img.width() != 768 || img.height() != 576)
464 img = img.scaled(768, 576, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
466 if (img.width() != 720 || img.height() != 540)
467 img = img.scaled(720, 540, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
473 bool DvdWizardMenu::createMenu() const
475 return m_view.create_menu->isChecked();
478 bool DvdWizardMenu::menuMovie() const
480 return m_view.background_list->currentIndex() == 2;
483 QString DvdWizardMenu::menuMoviePath() const
485 return m_view.background_image->url().path();
489 QMap <QString, QRect> DvdWizardMenu::buttonsInfo()
491 QMap <QString, QRect> info;
492 QList<QGraphicsItem *> list = m_scene->items();
493 for (int i = 0; i < list.count(); i++) {
494 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
495 DvdButton *button = static_cast < DvdButton* >(list.at(i));
496 QRect r = list.at(i)->sceneBoundingRect().toRect();
497 // Make sure y1 is not odd (requested by spumux)
498 if (r.height() % 2 == 1) r.setHeight(r.height() + 1);
499 if (r.y() % 2 == 1) r.setY(r.y() - 1);
500 QString command = button->command();
501 if (button->backMenu()) command.prepend("g1 = 999;");
502 info.insertMulti(command, r);
508 bool DvdWizardMenu::isPalMenu() const