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): QWizardPage(parent)
28 m_view.play_text->setText(i18n("Play"));
29 m_scene = new DvdScene(this);
30 m_view.menu_preview->setScene(m_scene);
32 connect(m_view.create_menu, SIGNAL(toggled(bool)), m_view.menu_box, SLOT(setEnabled(bool)));
33 connect(m_view.create_menu, SIGNAL(toggled(bool)), this, SIGNAL(completeChanged()));
35 m_view.add_button->setIcon(KIcon("document-new"));
36 m_view.delete_button->setIcon(KIcon("trash-empty"));
38 // TODO: re-enable add button options
39 m_view.add_button->setVisible(false);
40 m_view.delete_button->setVisible(false);
42 m_view.menu_profile->addItems(QStringList() << i18n("PAL") << i18n("NTSC"));
44 if (profile == "dv_ntsc" || profile == "dv_ntsc_wide") {
45 m_view.menu_profile->setCurrentIndex(1);
47 } else m_isPal = true;
49 // Create color background
50 m_color = new QGraphicsRectItem(0, 0, m_width, m_height);
51 m_color->setBrush(m_view.background_color->color());
52 m_color->setZValue(2);
53 m_scene->addItem(m_color);
56 // create background image
57 m_background = new QGraphicsPixmapItem();
58 m_background->setZValue(3);
59 //m_scene->addItem(m_background);
61 // create safe zone rect
62 int safeW = m_width / 20;
63 int safeH = m_height / 20;
64 m_safeRect = new QGraphicsRectItem(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
66 pen.setStyle(Qt::DashLine);
68 m_safeRect->setPen(pen);
69 m_safeRect->setZValue(5);
70 m_scene->addItem(m_safeRect);
72 changeProfile(m_view.menu_profile->currentIndex());
73 checkBackgroundType(0);
75 connect(m_view.menu_profile, SIGNAL(activated(int)), this, SLOT(changeProfile(int)));
78 DvdButton *button = new DvdButton(m_view.play_text->text());
79 QFont font = m_view.font_family->currentFont();
80 font.setPixelSize(m_view.font_size->value());
81 font.setStyleStrategy(QFont::NoAntialias);
82 button->setFont(font);
83 button->setDefaultTextColor(m_view.text_color->color());
85 button->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
86 QRectF r = button->sceneBoundingRect();
87 m_scene->addItem(button);
88 button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
89 button->setSelected(true);
92 //m_view.menu_preview->resizefitInView(0, 0, m_width, m_height);
94 connect(m_view.play_text, SIGNAL(textChanged(const QString &)), this, SLOT(buildButton()));
95 connect(m_view.text_color, SIGNAL(changed(const QColor &)), this, SLOT(updateColor()));
96 connect(m_view.font_size, SIGNAL(valueChanged(int)), this, SLOT(buildButton()));
97 connect(m_view.font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(buildButton()));
98 connect(m_view.background_image, SIGNAL(textChanged(const QString &)), this, SLOT(buildImage()));
99 connect(m_view.background_color, SIGNAL(changed(const QColor &)), this, SLOT(buildColor()));
101 connect(m_view.background_list, SIGNAL(activated(int)), this, SLOT(checkBackgroundType(int)));
103 connect(m_view.target_list, SIGNAL(activated(int)), this, SLOT(setButtonTarget(int)));
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 (!targets.contains(i)) return false;
163 void DvdWizardMenu::setButtonTarget(int ix)
165 QList<QGraphicsItem *> list = m_scene->selectedItems();
166 for (int i = 0; i < list.count(); i++) {
167 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
168 DvdButton *button = static_cast < DvdButton* >(list.at(i));
169 button->setTarget(ix);
173 emit completeChanged();
176 void DvdWizardMenu::buttonChanged()
178 QList<QGraphicsItem *> list = m_scene->selectedItems();
179 bool foundButton = false;
180 for (int i = 0; i < list.count(); i++) {
181 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
182 m_view.play_text->blockSignals(true);
183 m_view.font_size->blockSignals(true);
184 m_view.font_family->blockSignals(true);
185 m_view.target_list->blockSignals(true);
187 m_view.tabWidget->widget(0)->setEnabled(true);
188 DvdButton *button = static_cast < DvdButton* >(list.at(i));
189 m_view.target_list->setCurrentIndex(button->target());
190 m_view.play_text->setText(button->toPlainText());
191 QFont font = button->font();
192 m_view.font_size->setValue(font.pixelSize());
193 m_view.font_family->setCurrentFont(font);
194 m_view.play_text->blockSignals(false);
195 m_view.font_size->blockSignals(false);
196 m_view.font_family->blockSignals(false);
197 m_view.target_list->blockSignals(false);
201 if (!foundButton) m_view.tabWidget->widget(0)->setEnabled(false);
204 void DvdWizardMenu::addButton()
206 m_scene->clearSelection();
207 DvdButton *button = new DvdButton(m_view.play_text->text());
208 QFont font = m_view.font_family->currentFont();
209 font.setPixelSize(m_view.font_size->value());
210 font.setStyleStrategy(QFont::NoAntialias);
211 button->setFont(font);
212 button->setDefaultTextColor(m_view.text_color->color());
213 button->setZValue(4);
214 button->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
215 QRectF r = button->sceneBoundingRect();
216 m_scene->addItem(button);
217 button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
218 button->setSelected(true);
221 void DvdWizardMenu::deleteButton()
223 QList<QGraphicsItem *> list = m_scene->selectedItems();
224 for (int i = 0; i < list.count(); i++) {
225 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
232 void DvdWizardMenu::changeProfile(int ix)
250 void DvdWizardMenu::updatePreview()
252 m_scene->setProfile(m_width, m_height);
254 matrix.scale(0.5, 0.5);
255 m_view.menu_preview->setMatrix(matrix);
256 m_view.menu_preview->setMinimumSize(m_width / 2 + 4, m_height / 2 + 8);
258 m_color->setRect(0, 0, m_width, m_height);
260 int safeW = m_width / 20;
261 int safeH = m_height / 20;
262 m_safeRect->setRect(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
265 void DvdWizardMenu::setTargets(QStringList /*list*/)
267 m_targets = QStringList() << i18n("Play All");
268 // TODO: re-enable different targets
270 if (list.count() > 1) {
273 m_view.target_list->clear();*/
274 m_view.target_list->addItems(m_targets);
277 void DvdWizardMenu::checkBackgroundType(int ix)
280 m_view.background_color->setVisible(true);
281 m_view.background_image->setVisible(false);
282 m_scene->removeItem(m_background);
284 m_view.background_color->setVisible(false);
285 m_view.background_image->setVisible(true);
287 m_view.background_image->setFilter("*");
288 m_scene->addItem(m_background);
290 m_scene->removeItem(m_background);
291 m_view.background_image->setFilter("video/mpeg");
296 void DvdWizardMenu::buildColor()
298 m_color->setBrush(m_view.background_color->color());
301 void DvdWizardMenu::buildImage()
303 emit completeChanged();
304 if (m_view.background_image->url().isEmpty()) {
305 m_scene->removeItem(m_background);
309 if (!pix.load(m_view.background_image->url().path())) {
310 m_scene->removeItem(m_background);
313 pix = pix.scaled(m_width, m_height);
314 m_background->setPixmap(pix);
315 if (m_view.background_list->currentIndex() == 1) m_scene->addItem(m_background);
318 void DvdWizardMenu::checkBackground()
320 if (m_view.background_list->currentIndex() != 1) {
321 m_scene->removeItem(m_background);
323 m_scene->addItem(m_background);
327 void DvdWizardMenu::buildButton()
329 DvdButton *button = NULL;
330 QList<QGraphicsItem *> list = m_scene->selectedItems();
331 for (int i = 0; i < list.count(); i++) {
332 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
333 button = static_cast < DvdButton* >(list.at(i));
337 if (button == NULL) return;
338 button->setPlainText(m_view.play_text->text());
339 QFont font = m_view.font_family->currentFont();
340 font.setPixelSize(m_view.font_size->value());
341 font.setStyleStrategy(QFont::NoAntialias);
342 button->setFont(font);
343 button->setDefaultTextColor(m_view.text_color->color());
346 void DvdWizardMenu::updateColor()
348 updateColor(m_view.text_color->color());
349 m_view.menu_preview->viewport()->update();
352 void DvdWizardMenu::updateColor(QColor c)
354 DvdButton *button = NULL;
355 QList<QGraphicsItem *> list = m_scene->items();
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));
359 button->setDefaultTextColor(c);
365 void DvdWizardMenu::createButtonImages(const QString &img1, const QString &img2, const QString &img3)
367 if (m_view.create_menu->isChecked()) {
368 m_scene->clearSelection();
369 QImage img(m_width, m_height, QImage::Format_ARGB32);
371 m_scene->removeItem(m_safeRect);
372 m_scene->removeItem(m_color);
373 m_scene->removeItem(m_background);
374 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
377 if (m_view.menu_profile->currentIndex() < 2)
378 saved = img.scaled(720, 576);
379 else saved = img.scaled(720, 480);
380 saved.setNumColors(4);
383 updateColor(m_view.selected_color->color());
385 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
387 if (m_view.menu_profile->currentIndex() < 2)
388 saved = img.scaled(720, 576);
389 else saved = img.scaled(720, 480);
390 saved.setNumColors(4);
394 updateColor(m_view.highlighted_color->color());
396 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
398 if (m_view.menu_profile->currentIndex() < 2)
399 saved = img.scaled(720, 576);
400 else saved = img.scaled(720, 480);
401 saved.setNumColors(4);
406 m_scene->addItem(m_safeRect);
407 m_scene->addItem(m_color);
408 if (m_view.background_list->currentIndex() == 1) m_scene->addItem(m_background);
413 void DvdWizardMenu::createBackgroundImage(const QString &img1)
416 if (m_view.background_list->currentIndex() == 0) {
419 img = QImage(768, 576, QImage::Format_ARGB32);
421 img = QImage(720, 540, QImage::Format_ARGB32);
422 img.fill(m_view.background_color->color().rgb());
423 } else if (m_view.background_list->currentIndex() == 1) {
424 img.load(m_view.background_image->url().path());
426 if (img.width() != 768 || img.height() != 576)
427 img = img.scaled(768, 576, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
429 if (img.width() != 720 || img.height() != 540)
430 img = img.scaled(720, 540, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
436 bool DvdWizardMenu::createMenu() const
438 return m_view.create_menu->isChecked();
441 bool DvdWizardMenu::menuMovie() const
443 return m_view.background_list->currentIndex() == 2;
446 QString DvdWizardMenu::menuMoviePath() const
448 return m_view.background_image->url().path();
452 QMap <int, QRect> DvdWizardMenu::buttonsInfo()
454 QMap <int, QRect> info;
455 QList<QGraphicsItem *> list = m_scene->items();
456 for (int i = 0; i < list.count(); i++) {
457 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
458 DvdButton *button = static_cast < DvdButton* >(list.at(i));
459 QRect r = list.at(i)->sceneBoundingRect().toRect();
460 // Make sure y1 is not odd (requested by spumux)
461 if (r.height() % 2 == 1) r.setHeight(r.height() + 1);
462 if (r.y() % 2 == 1) r.setY(r.y() - 1);
463 info.insertMulti(button->target(), r);
469 bool DvdWizardMenu::isPalMenu() const