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 (list.at(j)->type() == button->type()) return false;
138 targets.append(button->target());
141 if (buttonCount == 0) return false;
142 // check that we have a "Play all" entry
143 if (targets.contains(0)) return true;
144 // ... or that each video file has a button
145 for (int i = m_view.target_list->count() - 1; i > 0; i--) {
146 if (!targets.contains(i)) return false;
151 void DvdWizardMenu::setButtonTarget(int ix) {
152 QList<QGraphicsItem *> list = m_scene->selectedItems();
153 for (int i = 0; i < list.count(); i++) {
154 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
155 DvdButton *button = static_cast < DvdButton* >(list.at(i));
156 button->setTarget(ix);
160 emit completeChanged();
163 void DvdWizardMenu::buttonChanged() {
164 QList<QGraphicsItem *> list = m_scene->selectedItems();
165 bool foundButton = false;
166 for (int i = 0; i < list.count(); i++) {
167 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
168 m_view.play_text->blockSignals(true);
169 m_view.font_size->blockSignals(true);
170 m_view.font_family->blockSignals(true);
171 m_view.target_list->blockSignals(true);
173 m_view.tabWidget->widget(0)->setEnabled(true);
174 DvdButton *button = static_cast < DvdButton* >(list.at(i));
175 m_view.target_list->setCurrentIndex(button->target());
176 m_view.play_text->setText(button->toPlainText());
177 QFont font = button->font();
178 m_view.font_size->setValue(font.pixelSize());
179 m_view.font_family->setCurrentFont(font);
180 m_view.play_text->blockSignals(false);
181 m_view.font_size->blockSignals(false);
182 m_view.font_family->blockSignals(false);
183 m_view.target_list->blockSignals(false);
187 if (!foundButton) m_view.tabWidget->widget(0)->setEnabled(false);
190 void DvdWizardMenu::addButton() {
191 m_scene->clearSelection();
192 DvdButton *button = new DvdButton(m_view.play_text->text());
193 QFont font = m_view.font_family->currentFont();
194 font.setPixelSize(m_view.font_size->value());
195 font.setStyleStrategy(QFont::NoAntialias);
196 button->setFont(font);
197 button->setDefaultTextColor(m_view.text_color->color());
198 button->setZValue(4);
199 button->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
200 QRectF r = button->sceneBoundingRect();
201 m_scene->addItem(button);
202 button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
203 button->setSelected(true);
206 void DvdWizardMenu::deleteButton() {
207 QList<QGraphicsItem *> list = m_scene->selectedItems();
208 for (int i = 0; i < list.count(); i++) {
209 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
216 void DvdWizardMenu::changeProfile(int ix) {
233 void DvdWizardMenu::updatePreview() {
234 m_scene->setProfile(m_width, m_height);
236 matrix.scale(0.5, 0.5);
237 m_view.menu_preview->setMatrix(matrix);
238 m_view.menu_preview->setMinimumSize(m_width / 2 + 4, m_height / 2 + 8);
240 m_color->setRect(0, 0, m_width, m_height);
242 int safeW = m_width / 20;
243 int safeH = m_height / 20;
244 m_safeRect->setRect(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
247 void DvdWizardMenu::setTargets(QStringList list) {
248 m_targets = QStringList() << i18n("Play All");
249 // TODO: re-enable different targets
251 if (list.count() > 1) {
254 m_view.target_list->clear();*/
255 m_view.target_list->addItems(m_targets);
258 void DvdWizardMenu::checkBackgroundType(int ix) {
260 m_view.background_color->setVisible(true);
261 m_view.background_image->setVisible(false);
262 m_scene->removeItem(m_background);
264 m_view.background_color->setVisible(false);
265 m_view.background_image->setVisible(true);
267 m_view.background_image->setFilter("*");
268 m_scene->addItem(m_background);
270 m_scene->removeItem(m_background);
271 m_view.background_image->setFilter("video/mpeg");
276 void DvdWizardMenu::buildColor() {
277 m_color->setBrush(m_view.background_color->color());
280 void DvdWizardMenu::buildImage() {
281 if (m_view.background_image->url().isEmpty()) {
282 m_scene->removeItem(m_background);
286 if (!pix.load(m_view.background_image->url().path())) {
287 m_scene->removeItem(m_background);
290 pix = pix.scaled(m_width, m_height);
291 m_background->setPixmap(pix);
292 if (m_view.background_list->currentIndex() == 1) m_scene->addItem(m_background);
295 void DvdWizardMenu::checkBackground() {
296 if (m_view.background_list->currentIndex() != 1) {
297 m_scene->removeItem(m_background);
299 m_scene->addItem(m_background);
303 void DvdWizardMenu::buildButton() {
304 DvdButton *button = NULL;
305 QList<QGraphicsItem *> list = m_scene->selectedItems();
306 for (int i = 0; i < list.count(); i++) {
307 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
308 button = static_cast < DvdButton* >(list.at(i));
312 if (button == NULL) return;
313 button->setPlainText(m_view.play_text->text());
314 QFont font = m_view.font_family->currentFont();
315 font.setPixelSize(m_view.font_size->value());
316 font.setStyleStrategy(QFont::NoAntialias);
317 button->setFont(font);
318 button->setDefaultTextColor(m_view.text_color->color());
321 void DvdWizardMenu::updateColor() {
322 updateColor(m_view.text_color->color());
323 m_view.menu_preview->viewport()->update();
326 void DvdWizardMenu::updateColor(QColor c) {
327 DvdButton *button = NULL;
328 QList<QGraphicsItem *> list = m_scene->items();
329 for (int i = 0; i < list.count(); i++) {
330 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
331 button = static_cast < DvdButton* >(list.at(i));
332 button->setDefaultTextColor(c);
338 void DvdWizardMenu::createButtonImages(const QString &img1, const QString &img2, const QString &img3) {
339 if (m_view.create_menu->isChecked()) {
340 m_scene->clearSelection();
341 QImage img(m_width, m_height, QImage::Format_ARGB32);
343 m_scene->removeItem(m_safeRect);
344 m_scene->removeItem(m_color);
345 m_scene->removeItem(m_background);
346 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
350 if (m_view.menu_profile->currentIndex() < 2)
351 saved = img.scaled(720, 576);
352 else saved = img.scaled(720, 480);
355 updateColor(m_view.selected_color->color());
357 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
360 if (m_view.menu_profile->currentIndex() < 2)
361 saved = img.scaled(720, 576);
362 else saved = img.scaled(720, 480);
366 updateColor(m_view.highlighted_color->color());
368 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
371 if (m_view.menu_profile->currentIndex() < 2)
372 saved = img.scaled(720, 576);
373 else saved = img.scaled(720, 480);
378 m_scene->addItem(m_safeRect);
379 m_scene->addItem(m_color);
380 if (m_view.background_list->currentIndex() == 1) m_scene->addItem(m_background);
385 void DvdWizardMenu::createBackgroundImage(const QString &img1) {
387 if (m_view.background_list->currentIndex() == 0) {
390 img = QImage(768, 576, QImage::Format_ARGB32);
392 img = QImage(720, 540, QImage::Format_ARGB32);
393 img.fill(m_view.background_color->color().rgb());
394 } else if (m_view.background_list->currentIndex() == 1) {
395 img.load(m_view.background_image->url().path());
397 if (img.width() != 768 || img.height() != 576)
398 img = img.scaled(768, 576, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
400 if (img.width() != 720 || img.height() != 540)
401 img = img.scaled(720, 540, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
407 bool DvdWizardMenu::createMenu() const {
408 return m_view.create_menu->isChecked();
411 bool DvdWizardMenu::menuMovie() const {
412 return m_view.background_list->currentIndex() == 2;
415 QString DvdWizardMenu::menuMoviePath() const {
416 return m_view.background_image->url().path();
420 QMap <int, QRect> DvdWizardMenu::buttonsInfo() {
421 QMap <int, QRect> info;
422 QList<QGraphicsItem *> list = m_scene->items();
423 for (int i = 0; i < list.count(); i++) {
424 if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
425 DvdButton *button = static_cast < DvdButton* >(list.at(i));
426 QRect r = list.at(i)->sceneBoundingRect().toRect();
427 // Make sure y1 is not odd (requested by spumux)
428 if (r.height() % 2 == 1) r.setHeight(r.height() + 1);
429 if (r.y() % 2 == 1) r.setY(r.y() - 1);
430 info.insertMulti(button->target(), r);
436 bool DvdWizardMenu::isPalMenu() const {