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) {
27 m_view.play_text->setText(i18n("Play"));
28 m_scene = new DvdScene(this);
29 m_view.menu_preview->setScene(m_scene);
31 connect(m_view.create_menu, SIGNAL(toggled(bool)), m_view.menu_box, SLOT(setEnabled(bool)));
32 connect(m_view.create_menu, SIGNAL(toggled(bool)), this, SIGNAL(completeChanged()));
34 m_view.add_button->setIcon(KIcon("document-new"));
35 m_view.delete_button->setIcon(KIcon("trash-empty"));
37 // TODO: re-enable add button options
38 m_view.add_button->setVisible(false);
39 m_view.delete_button->setVisible(false);
41 m_view.menu_profile->addItems(QStringList() << i18n("PAL") << i18n("NTSC"));
43 if (profile == "dv_ntsc" || profile == "dv_ntsc_wide") {
44 m_view.menu_profile->setCurrentIndex(1);
46 } else m_isPal = true;
48 // Create color background
49 m_color = new QGraphicsRectItem(0, 0, m_width, m_height);
50 m_color->setBrush(m_view.background_color->color());
51 m_color->setZValue(2);
52 m_scene->addItem(m_color);
55 // create background image
56 m_background = new QGraphicsPixmapItem();
57 m_background->setZValue(3);
58 //m_scene->addItem(m_background);
60 // create safe zone rect
61 int safeW = m_width / 20;
62 int safeH = m_height / 20;
63 m_safeRect = new QGraphicsRectItem(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
65 pen.setStyle(Qt::DashLine);
67 m_safeRect->setPen(pen);
68 m_safeRect->setZValue(5);
69 m_scene->addItem(m_safeRect);
71 changeProfile(m_view.menu_profile->currentIndex());
72 checkBackgroundType(0);
74 connect(m_view.menu_profile, SIGNAL(activated(int)), this, SLOT(changeProfile(int)));
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)));
104 connect(m_view.add_button, SIGNAL(pressed()), this, SLOT(addButton()));
105 connect(m_view.delete_button, SIGNAL(pressed()), this, SLOT(deleteButton()));
106 connect(m_scene, SIGNAL(selectionChanged()), this, SLOT(buttonChanged()));
107 connect(m_scene, SIGNAL(changed(const QList<QRectF> &)), this, SIGNAL(completeChanged()));
109 m_view.menu_box->setEnabled(false);
113 DvdWizardMenu::~DvdWizardMenu() {
122 bool DvdWizardMenu::isComplete() const {
123 if (!m_view.create_menu->isChecked()) return true;
125 QList<QGraphicsItem *> list = m_scene->items();
127 // check that the menu buttons don't collide
128 for (int i = 0; i < list.count(); i++) {
129 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
131 DvdButton *button = static_cast < DvdButton* >(list.at(i));
132 QList<QGraphicsItem *> collisions = button->collidingItems();
133 if (!collisions.isEmpty()) {
134 for (int j = 0; j < collisions.count(); j++) {
135 if (collisions.at(j)->type() == button->type()) return false;
138 targets.append(button->target());
141 if (buttonCount == 0) {
142 // We need at least one button
146 if (!m_view.background_image->isHidden()) {
147 // Make sure user selected a valid image / video file
148 if (!QFile::exists(m_view.background_image->url().path())) return false;
151 // check that we have a "Play all" entry
152 if (targets.contains(0)) return true;
153 // ... or that each video file has a button
154 for (int i = m_view.target_list->count() - 1; i > 0; i--) {
155 if (!targets.contains(i)) return false;
160 void DvdWizardMenu::setButtonTarget(int ix) {
161 QList<QGraphicsItem *> list = m_scene->selectedItems();
162 for (int i = 0; i < list.count(); i++) {
163 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
164 DvdButton *button = static_cast < DvdButton* >(list.at(i));
165 button->setTarget(ix);
169 emit completeChanged();
172 void DvdWizardMenu::buttonChanged() {
173 QList<QGraphicsItem *> list = m_scene->selectedItems();
174 bool foundButton = false;
175 for (int i = 0; i < list.count(); i++) {
176 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
177 m_view.play_text->blockSignals(true);
178 m_view.font_size->blockSignals(true);
179 m_view.font_family->blockSignals(true);
180 m_view.target_list->blockSignals(true);
182 m_view.tabWidget->widget(0)->setEnabled(true);
183 DvdButton *button = static_cast < DvdButton* >(list.at(i));
184 m_view.target_list->setCurrentIndex(button->target());
185 m_view.play_text->setText(button->toPlainText());
186 QFont font = button->font();
187 m_view.font_size->setValue(font.pixelSize());
188 m_view.font_family->setCurrentFont(font);
189 m_view.play_text->blockSignals(false);
190 m_view.font_size->blockSignals(false);
191 m_view.font_family->blockSignals(false);
192 m_view.target_list->blockSignals(false);
196 if (!foundButton) m_view.tabWidget->widget(0)->setEnabled(false);
199 void DvdWizardMenu::addButton() {
200 m_scene->clearSelection();
201 DvdButton *button = new DvdButton(m_view.play_text->text());
202 QFont font = m_view.font_family->currentFont();
203 font.setPixelSize(m_view.font_size->value());
204 font.setStyleStrategy(QFont::NoAntialias);
205 button->setFont(font);
206 button->setDefaultTextColor(m_view.text_color->color());
207 button->setZValue(4);
208 button->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
209 QRectF r = button->sceneBoundingRect();
210 m_scene->addItem(button);
211 button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
212 button->setSelected(true);
215 void DvdWizardMenu::deleteButton() {
216 QList<QGraphicsItem *> list = m_scene->selectedItems();
217 for (int i = 0; i < list.count(); i++) {
218 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
225 void DvdWizardMenu::changeProfile(int ix) {
242 void DvdWizardMenu::updatePreview() {
243 m_scene->setProfile(m_width, m_height);
245 matrix.scale(0.5, 0.5);
246 m_view.menu_preview->setMatrix(matrix);
247 m_view.menu_preview->setMinimumSize(m_width / 2 + 4, m_height / 2 + 8);
249 m_color->setRect(0, 0, m_width, m_height);
251 int safeW = m_width / 20;
252 int safeH = m_height / 20;
253 m_safeRect->setRect(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
256 void DvdWizardMenu::setTargets(QStringList /*list*/) {
257 m_targets = QStringList() << i18n("Play All");
258 // TODO: re-enable different targets
260 if (list.count() > 1) {
263 m_view.target_list->clear();*/
264 m_view.target_list->addItems(m_targets);
267 void DvdWizardMenu::checkBackgroundType(int ix) {
269 m_view.background_color->setVisible(true);
270 m_view.background_image->setVisible(false);
271 m_scene->removeItem(m_background);
273 m_view.background_color->setVisible(false);
274 m_view.background_image->setVisible(true);
276 m_view.background_image->setFilter("*");
277 m_scene->addItem(m_background);
279 m_scene->removeItem(m_background);
280 m_view.background_image->setFilter("video/mpeg");
285 void DvdWizardMenu::buildColor() {
286 m_color->setBrush(m_view.background_color->color());
289 void DvdWizardMenu::buildImage() {
290 emit completeChanged();
291 if (m_view.background_image->url().isEmpty()) {
292 m_scene->removeItem(m_background);
296 if (!pix.load(m_view.background_image->url().path())) {
297 m_scene->removeItem(m_background);
300 pix = pix.scaled(m_width, m_height);
301 m_background->setPixmap(pix);
302 if (m_view.background_list->currentIndex() == 1) m_scene->addItem(m_background);
305 void DvdWizardMenu::checkBackground() {
306 if (m_view.background_list->currentIndex() != 1) {
307 m_scene->removeItem(m_background);
309 m_scene->addItem(m_background);
313 void DvdWizardMenu::buildButton() {
314 DvdButton *button = NULL;
315 QList<QGraphicsItem *> list = m_scene->selectedItems();
316 for (int i = 0; i < list.count(); i++) {
317 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
318 button = static_cast < DvdButton* >(list.at(i));
322 if (button == NULL) return;
323 button->setPlainText(m_view.play_text->text());
324 QFont font = m_view.font_family->currentFont();
325 font.setPixelSize(m_view.font_size->value());
326 font.setStyleStrategy(QFont::NoAntialias);
327 button->setFont(font);
328 button->setDefaultTextColor(m_view.text_color->color());
331 void DvdWizardMenu::updateColor() {
332 updateColor(m_view.text_color->color());
333 m_view.menu_preview->viewport()->update();
336 void DvdWizardMenu::updateColor(QColor c) {
337 DvdButton *button = NULL;
338 QList<QGraphicsItem *> list = m_scene->items();
339 for (int i = 0; i < list.count(); i++) {
340 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
341 button = static_cast < DvdButton* >(list.at(i));
342 button->setDefaultTextColor(c);
348 void DvdWizardMenu::createButtonImages(const QString &img1, const QString &img2, const QString &img3) {
349 if (m_view.create_menu->isChecked()) {
350 m_scene->clearSelection();
351 QImage img(m_width, m_height, QImage::Format_ARGB32);
353 m_scene->removeItem(m_safeRect);
354 m_scene->removeItem(m_color);
355 m_scene->removeItem(m_background);
356 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
359 if (m_view.menu_profile->currentIndex() < 2)
360 saved = img.scaled(720, 576);
361 else saved = img.scaled(720, 480);
362 saved.setNumColors(4);
365 updateColor(m_view.selected_color->color());
367 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
369 if (m_view.menu_profile->currentIndex() < 2)
370 saved = img.scaled(720, 576);
371 else saved = img.scaled(720, 480);
372 saved.setNumColors(4);
376 updateColor(m_view.highlighted_color->color());
378 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
380 if (m_view.menu_profile->currentIndex() < 2)
381 saved = img.scaled(720, 576);
382 else saved = img.scaled(720, 480);
383 saved.setNumColors(4);
388 m_scene->addItem(m_safeRect);
389 m_scene->addItem(m_color);
390 if (m_view.background_list->currentIndex() == 1) m_scene->addItem(m_background);
395 void DvdWizardMenu::createBackgroundImage(const QString &img1) {
397 if (m_view.background_list->currentIndex() == 0) {
400 img = QImage(768, 576, QImage::Format_ARGB32);
402 img = QImage(720, 540, QImage::Format_ARGB32);
403 img.fill(m_view.background_color->color().rgb());
404 } else if (m_view.background_list->currentIndex() == 1) {
405 img.load(m_view.background_image->url().path());
407 if (img.width() != 768 || img.height() != 576)
408 img = img.scaled(768, 576, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
410 if (img.width() != 720 || img.height() != 540)
411 img = img.scaled(720, 540, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
417 bool DvdWizardMenu::createMenu() const {
418 return m_view.create_menu->isChecked();
421 bool DvdWizardMenu::menuMovie() const {
422 return m_view.background_list->currentIndex() == 2;
425 QString DvdWizardMenu::menuMoviePath() const {
426 return m_view.background_image->url().path();
430 QMap <int, QRect> DvdWizardMenu::buttonsInfo() {
431 QMap <int, QRect> info;
432 QList<QGraphicsItem *> list = m_scene->items();
433 for (int i = 0; i < list.count(); i++) {
434 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
435 DvdButton *button = static_cast < DvdButton* >(list.at(i));
436 QRect r = list.at(i)->sceneBoundingRect().toRect();
437 // Make sure y1 is not odd (requested by spumux)
438 if (r.height() % 2 == 1) r.setHeight(r.height() + 1);
439 if (r.y() % 2 == 1) r.setY(r.y() - 1);
440 info.insertMulti(button->target(), r);
446 bool DvdWizardMenu::isPalMenu() const {