]> git.sesse.net Git - kdenlive/blob - src/dvdwizardmenu.cpp
Cleanup + small fix in dvd wizard menu page
[kdenlive] / src / dvdwizardmenu.cpp
1 /***************************************************************************
2  *   Copyright (C) 2009 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20 #include "dvdwizardmenu.h"
21
22 #include <KDebug>
23
24
25 DvdWizardMenu::DvdWizardMenu(const QString &profile, QWidget *parent): QWizardPage(parent) {
26     m_view.setupUi(this);
27     m_view.play_text->setText(i18n("Play"));
28     m_scene = new DvdScene(this);
29     m_view.menu_preview->setScene(m_scene);
30
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()));
33
34     m_view.add_button->setIcon(KIcon("document-new"));
35     m_view.delete_button->setIcon(KIcon("trash-empty"));
36
37     // TODO: re-enable add button options
38     m_view.add_button->setVisible(false);
39     m_view.delete_button->setVisible(false);
40
41     m_view.menu_profile->addItems(QStringList() << i18n("PAL") << i18n("NTSC"));
42
43     if (profile == "dv_ntsc" || profile == "dv_ntsc_wide") {
44         m_view.menu_profile->setCurrentIndex(1);
45         m_isPal = false;
46     } else m_isPal = true;
47
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);
53
54
55     // create background image
56     m_background = new QGraphicsPixmapItem();
57     m_background->setZValue(3);
58     //m_scene->addItem(m_background);
59
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);
64     QPen pen(Qt::red);
65     pen.setStyle(Qt::DashLine);
66     pen.setWidth(3);
67     m_safeRect->setPen(pen);
68     m_safeRect->setZValue(5);
69     m_scene->addItem(m_safeRect);
70
71     changeProfile(m_view.menu_profile->currentIndex());
72     checkBackgroundType(0);
73
74     connect(m_view.menu_profile, SIGNAL(activated(int)), this, SLOT(changeProfile(int)));
75
76     // create menu button
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());
83     button->setZValue(4);
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);
89
90
91     //m_view.menu_preview->resizefitInView(0, 0, m_width, m_height);
92
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()));
99
100     connect(m_view.background_list, SIGNAL(activated(int)), this, SLOT(checkBackgroundType(int)));
101
102     connect(m_view.target_list, SIGNAL(activated(int)), this, SLOT(setButtonTarget(int)));
103
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()));
108
109     m_view.menu_box->setEnabled(false);
110
111 }
112
113 DvdWizardMenu::~DvdWizardMenu() {
114     delete m_color;
115     delete m_safeRect;
116     delete m_background;
117     delete m_scene;
118 }
119
120 // virtual
121
122 bool DvdWizardMenu::isComplete() const {
123     if (!m_view.create_menu->isChecked()) return true;
124     QList <int> targets;
125     QList<QGraphicsItem *> list = m_scene->items();
126     int buttonCount = 0;
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) {
130             buttonCount++;
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;
136                 }
137             }
138             targets.append(button->target());
139         }
140     }
141     if (buttonCount == 0) {
142         // We need at least one button
143         return false;
144     }
145
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;
149     }
150
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;
156     }
157     return true;
158 }
159
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);
166             break;
167         }
168     }
169     emit completeChanged();
170 }
171
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);
181             foundButton = 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);
193             break;
194         }
195     }
196     if (!foundButton) m_view.tabWidget->widget(0)->setEnabled(false);
197 }
198
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);
213 }
214
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) {
219             delete list.at(i);
220             break;
221         }
222     }
223 }
224
225 void DvdWizardMenu::changeProfile(int ix) {
226     switch (ix) {
227     case 1:
228         m_width = 720;
229         m_height = 480;
230         m_isPal = false;
231         updatePreview();
232         break;
233     default:
234         m_width = 720;
235         m_height = 576;
236         m_isPal = true;
237         updatePreview();
238         break;
239     }
240 }
241
242 void DvdWizardMenu::updatePreview() {
243     m_scene->setProfile(m_width, m_height);
244     QMatrix matrix;
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);
248
249     m_color->setRect(0, 0, m_width, m_height);
250
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);
254 }
255
256 void DvdWizardMenu::setTargets(QStringList /*list*/) {
257     m_targets = QStringList() << i18n("Play All");
258     // TODO: re-enable different targets
259     /*
260     if (list.count() > 1) {
261     m_targets += list;
262     }
263     m_view.target_list->clear();*/
264     m_view.target_list->addItems(m_targets);
265 }
266
267 void DvdWizardMenu::checkBackgroundType(int ix) {
268     if (ix == 0) {
269         m_view.background_color->setVisible(true);
270         m_view.background_image->setVisible(false);
271         m_scene->removeItem(m_background);
272     } else {
273         m_view.background_color->setVisible(false);
274         m_view.background_image->setVisible(true);
275         if (ix == 1) {
276             m_view.background_image->setFilter("*");
277             m_scene->addItem(m_background);
278         } else {
279             m_scene->removeItem(m_background);
280             m_view.background_image->setFilter("video/mpeg");
281         }
282     }
283 }
284
285 void DvdWizardMenu::buildColor() {
286     m_color->setBrush(m_view.background_color->color());
287 }
288
289 void DvdWizardMenu::buildImage() {
290     emit completeChanged();
291     if (m_view.background_image->url().isEmpty()) {
292         m_scene->removeItem(m_background);
293         return;
294     }
295     QPixmap pix;
296     if (!pix.load(m_view.background_image->url().path())) {
297         m_scene->removeItem(m_background);
298         return;
299     }
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);
303 }
304
305 void DvdWizardMenu::checkBackground() {
306     if (m_view.background_list->currentIndex() != 1) {
307         m_scene->removeItem(m_background);
308     } else {
309         m_scene->addItem(m_background);
310     }
311 }
312
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));
319             break;
320         }
321     }
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());
329 }
330
331 void DvdWizardMenu::updateColor() {
332     updateColor(m_view.text_color->color());
333     m_view.menu_preview->viewport()->update();
334 }
335
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);
343         }
344     }
345 }
346
347
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);
352         QPainter p(&img);
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));
357         p.end();
358         QImage saved;
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);
363         saved.save(img1);
364
365         updateColor(m_view.selected_color->color());
366         p.begin(&img);
367         m_scene->render(&p, QRectF(0, 0, m_width, m_height));
368         p.end();
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);
373         saved.save(img2);
374
375
376         updateColor(m_view.highlighted_color->color());
377         p.begin(&img);
378         m_scene->render(&p, QRectF(0, 0, m_width, m_height));
379         p.end();
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);
384         saved.save(img3);
385
386         updateColor();
387
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);
391     }
392 }
393
394
395 void DvdWizardMenu::createBackgroundImage(const QString &img1) {
396     QImage img;
397     if (m_view.background_list->currentIndex() == 0) {
398         // color background
399         if (m_isPal)
400             img = QImage(768, 576, QImage::Format_ARGB32);
401         else
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());
406         if (m_isPal) {
407             if (img.width() != 768 || img.height() != 576)
408                 img = img.scaled(768, 576, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
409         } else {
410             if (img.width() != 720 || img.height() != 540)
411                 img = img.scaled(720, 540, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
412         }
413     } else return;
414     img.save(img1);
415 }
416
417 bool DvdWizardMenu::createMenu() const {
418     return m_view.create_menu->isChecked();
419 }
420
421 bool DvdWizardMenu::menuMovie() const {
422     return m_view.background_list->currentIndex() == 2;
423 }
424
425 QString DvdWizardMenu::menuMoviePath() const {
426     return m_view.background_image->url().path();
427 }
428
429
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);
441         }
442     }
443     return info;
444 }
445
446 bool DvdWizardMenu::isPalMenu() const {
447     return m_isPal;
448 }