]> git.sesse.net Git - kdenlive/blob - src/dvdwizardmenu.cpp
68a69a73fd89ca9c2e5ea744bef2429e896b60af
[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) :
26         QWizardPage(parent)
27 {
28     m_view.setupUi(this);
29     m_view.play_text->setText(i18n("Play"));
30     m_scene = new DvdScene(this);
31     m_view.menu_preview->setScene(m_scene);
32
33     connect(m_view.create_menu, SIGNAL(toggled(bool)), m_view.menu_box, SLOT(setEnabled(bool)));
34     connect(m_view.create_menu, SIGNAL(toggled(bool)), this, SIGNAL(completeChanged()));
35
36     m_view.add_button->setIcon(KIcon("document-new"));
37     m_view.delete_button->setIcon(KIcon("trash-empty"));
38
39     // TODO: re-enable add button options
40     /*m_view.add_button->setVisible(false);
41     m_view.delete_button->setVisible(false);*/
42
43     m_view.menu_profile->addItems(QStringList() << i18n("PAL") << i18n("NTSC"));
44
45     if (profile == "dv_ntsc" || profile == "dv_ntsc_wide") {
46         m_view.menu_profile->setCurrentIndex(1);
47         m_isPal = false;
48     } else m_isPal = true;
49
50     // Create color background
51     m_color = new QGraphicsRectItem(0, 0, m_width, m_height);
52     m_color->setBrush(m_view.background_color->color());
53     m_color->setZValue(2);
54     m_scene->addItem(m_color);
55
56
57     // create background image
58     m_background = new QGraphicsPixmapItem();
59     m_background->setZValue(3);
60     //m_scene->addItem(m_background);
61
62     // create safe zone rect
63     int safeW = m_width / 20;
64     int safeH = m_height / 20;
65     m_safeRect = new QGraphicsRectItem(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
66     QPen pen(Qt::red);
67     pen.setStyle(Qt::DashLine);
68     pen.setWidth(3);
69     m_safeRect->setPen(pen);
70     m_safeRect->setZValue(5);
71     m_scene->addItem(m_safeRect);
72
73     changeProfile(m_view.menu_profile->currentIndex());
74     checkBackgroundType(0);
75
76     connect(m_view.menu_profile, SIGNAL(activated(int)), this, SLOT(changeProfile(int)));
77
78     // create menu button
79     DvdButton *button = new DvdButton(m_view.play_text->text());
80     QFont font = m_view.font_family->currentFont();
81     font.setPixelSize(m_view.font_size->value());
82     font.setStyleStrategy(QFont::NoAntialias);
83     button->setFont(font);
84     button->setDefaultTextColor(m_view.text_color->color());
85     button->setZValue(4);
86     button->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
87     QRectF r = button->sceneBoundingRect();
88     m_scene->addItem(button);
89     button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
90     button->setSelected(true);
91
92
93     //m_view.menu_preview->resizefitInView(0, 0, m_width, m_height);
94
95     connect(m_view.play_text, SIGNAL(textChanged(const QString &)), this, SLOT(buildButton()));
96     connect(m_view.text_color, SIGNAL(changed(const QColor &)), this, SLOT(updateColor()));
97     connect(m_view.font_size, SIGNAL(valueChanged(int)), this, SLOT(buildButton()));
98     connect(m_view.font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(buildButton()));
99     connect(m_view.background_image, SIGNAL(textChanged(const QString &)), this, SLOT(buildImage()));
100     connect(m_view.background_color, SIGNAL(changed(const QColor &)), this, SLOT(buildColor()));
101
102     connect(m_view.background_list, SIGNAL(activated(int)), this, SLOT(checkBackgroundType(int)));
103
104     connect(m_view.target_list, SIGNAL(activated(int)), this, SLOT(setButtonTarget(int)));
105
106     connect(m_view.add_button, SIGNAL(pressed()), this, SLOT(addButton()));
107     connect(m_view.delete_button, SIGNAL(pressed()), this, SLOT(deleteButton()));
108     connect(m_scene, SIGNAL(selectionChanged()), this, SLOT(buttonChanged()));
109     connect(m_scene, SIGNAL(changed(const QList<QRectF> &)), this, SIGNAL(completeChanged()));
110
111     m_view.menu_box->setEnabled(false);
112
113 }
114
115 DvdWizardMenu::~DvdWizardMenu()
116 {
117     delete m_color;
118     delete m_safeRect;
119     delete m_background;
120     delete m_scene;
121 }
122
123 // virtual
124
125 bool DvdWizardMenu::isComplete() const
126 {
127     if (!m_view.create_menu->isChecked()) return true;
128     QList <int> targets;
129     QList<QGraphicsItem *> list = m_scene->items();
130     int buttonCount = 0;
131     // check that the menu buttons don't collide
132     for (int i = 0; i < list.count(); i++) {
133         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
134             buttonCount++;
135             DvdButton *button = static_cast < DvdButton* >(list.at(i));
136             QList<QGraphicsItem *> collisions = button->collidingItems();
137             if (!collisions.isEmpty()) {
138                 for (int j = 0; j < collisions.count(); j++) {
139                     if (collisions.at(j)->type() == button->type()) return false;
140                 }
141             }
142             targets.append(button->target());
143         }
144     }
145     if (buttonCount == 0) {
146         // We need at least one button
147         return false;
148     }
149
150     if (!m_view.background_image->isHidden()) {
151         // Make sure user selected a valid image / video file
152         if (!QFile::exists(m_view.background_image->url().path())) return false;
153     }
154
155     // check that we have a "Play all" entry
156     if (targets.contains(0)) return true;
157     // ... or that each video file has a button
158     for (int i = m_view.target_list->count() - 1; i > 0; i--) {
159         if (!targets.contains(i)) return false;
160     }
161     return true;
162 }
163
164 void DvdWizardMenu::setButtonTarget(int ix)
165 {
166     QList<QGraphicsItem *> list = m_scene->selectedItems();
167     for (int i = 0; i < list.count(); i++) {
168         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
169             DvdButton *button = static_cast < DvdButton* >(list.at(i));
170             button->setTarget(ix, m_view.target_list->itemData(ix).toString());
171             break;
172         }
173     }
174     emit completeChanged();
175 }
176
177 void DvdWizardMenu::buttonChanged()
178 {
179     QList<QGraphicsItem *> list = m_scene->selectedItems();
180     bool foundButton = false;
181     for (int i = 0; i < list.count(); i++) {
182         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
183             m_view.play_text->blockSignals(true);
184             m_view.font_size->blockSignals(true);
185             m_view.font_family->blockSignals(true);
186             m_view.target_list->blockSignals(true);
187             foundButton = true;
188             m_view.tabWidget->widget(0)->setEnabled(true);
189             DvdButton *button = static_cast < DvdButton* >(list.at(i));
190             m_view.target_list->setCurrentIndex(button->target());
191             m_view.play_text->setText(button->toPlainText());
192             QFont font = button->font();
193             m_view.font_size->setValue(font.pixelSize());
194             m_view.font_family->setCurrentFont(font);
195             m_view.play_text->blockSignals(false);
196             m_view.font_size->blockSignals(false);
197             m_view.font_family->blockSignals(false);
198             m_view.target_list->blockSignals(false);
199             break;
200         }
201     }
202     if (!foundButton) m_view.tabWidget->widget(0)->setEnabled(false);
203 }
204
205 void DvdWizardMenu::addButton()
206 {
207     m_scene->clearSelection();
208     DvdButton *button = new DvdButton(m_view.play_text->text());
209     QFont font = m_view.font_family->currentFont();
210     font.setPixelSize(m_view.font_size->value());
211     font.setStyleStrategy(QFont::NoAntialias);
212     button->setFont(font);
213     button->setDefaultTextColor(m_view.text_color->color());
214     button->setZValue(4);
215     button->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
216     QRectF r = button->sceneBoundingRect();
217     m_scene->addItem(button);
218     button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
219     button->setSelected(true);
220 }
221
222 void DvdWizardMenu::deleteButton()
223 {
224     QList<QGraphicsItem *> list = m_scene->selectedItems();
225     for (int i = 0; i < list.count(); i++) {
226         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
227             delete list.at(i);
228             break;
229         }
230     }
231 }
232
233 void DvdWizardMenu::changeProfile(int ix)
234 {
235     switch (ix) {
236     case 1:
237         m_width = 720;
238         m_height = 480;
239         m_isPal = false;
240         updatePreview();
241         break;
242     default:
243         m_width = 720;
244         m_height = 576;
245         m_isPal = true;
246         updatePreview();
247         break;
248     }
249 }
250
251 void DvdWizardMenu::updatePreview()
252 {
253     m_scene->setProfile(m_width, m_height);
254     QMatrix matrix;
255     matrix.scale(0.5, 0.5);
256     m_view.menu_preview->setMatrix(matrix);
257     m_view.menu_preview->setMinimumSize(m_width / 2 + 4, m_height / 2 + 8);
258
259     m_color->setRect(0, 0, m_width, m_height);
260
261     int safeW = m_width / 20;
262     int safeH = m_height / 20;
263     m_safeRect->setRect(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
264 }
265
266 void DvdWizardMenu::setTargets(QStringList list, QStringList targetlist)
267 {
268     m_view.target_list->clear();
269     m_view.target_list->addItem(i18n("Play All"), "title 1");
270     for (int i = 0; i < list.count(); i++) {
271         m_view.target_list->addItem(list.at(i), targetlist.at(i));
272     }
273 }
274
275 void DvdWizardMenu::checkBackgroundType(int ix)
276 {
277     if (ix == 0) {
278         m_view.background_color->setVisible(true);
279         m_view.background_image->setVisible(false);
280         m_scene->removeItem(m_background);
281     } else {
282         m_view.background_color->setVisible(false);
283         m_view.background_image->setVisible(true);
284         if (ix == 1) {
285             m_view.background_image->setFilter("*");
286             m_scene->addItem(m_background);
287         } else {
288             m_scene->removeItem(m_background);
289             m_view.background_image->setFilter("video/mpeg");
290         }
291     }
292 }
293
294 void DvdWizardMenu::buildColor()
295 {
296     m_color->setBrush(m_view.background_color->color());
297 }
298
299 void DvdWizardMenu::buildImage()
300 {
301     emit completeChanged();
302     if (m_view.background_image->url().isEmpty()) {
303         m_scene->removeItem(m_background);
304         return;
305     }
306     QPixmap pix;
307     if (!pix.load(m_view.background_image->url().path())) {
308         m_scene->removeItem(m_background);
309         return;
310     }
311     pix = pix.scaled(m_width, m_height);
312     m_background->setPixmap(pix);
313     if (m_view.background_list->currentIndex() == 1) m_scene->addItem(m_background);
314 }
315
316 void DvdWizardMenu::checkBackground()
317 {
318     if (m_view.background_list->currentIndex() != 1) {
319         m_scene->removeItem(m_background);
320     } else {
321         m_scene->addItem(m_background);
322     }
323 }
324
325 void DvdWizardMenu::buildButton()
326 {
327     DvdButton *button = NULL;
328     QList<QGraphicsItem *> list = m_scene->selectedItems();
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             break;
333         }
334     }
335     if (button == NULL) return;
336     button->setPlainText(m_view.play_text->text());
337     QFont font = m_view.font_family->currentFont();
338     font.setPixelSize(m_view.font_size->value());
339     font.setStyleStrategy(QFont::NoAntialias);
340     button->setFont(font);
341     button->setDefaultTextColor(m_view.text_color->color());
342 }
343
344 void DvdWizardMenu::updateColor()
345 {
346     updateColor(m_view.text_color->color());
347     m_view.menu_preview->viewport()->update();
348 }
349
350 void DvdWizardMenu::updateColor(QColor c)
351 {
352     DvdButton *button = NULL;
353     QList<QGraphicsItem *> list = m_scene->items();
354     for (int i = 0; i < list.count(); i++) {
355         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
356             button = static_cast < DvdButton* >(list.at(i));
357             button->setDefaultTextColor(c);
358         }
359     }
360 }
361
362
363 void DvdWizardMenu::createButtonImages(const QString &img1, const QString &img2, const QString &img3)
364 {
365     if (m_view.create_menu->isChecked()) {
366         m_scene->clearSelection();
367         QImage img(m_width, m_height, QImage::Format_ARGB32);
368         QPainter p(&img);
369         m_scene->removeItem(m_safeRect);
370         m_scene->removeItem(m_color);
371         m_scene->removeItem(m_background);
372         m_scene->render(&p, QRectF(0, 0, m_width, m_height));
373         p.end();
374         QImage saved;
375         if (m_view.menu_profile->currentIndex() < 2)
376             saved = img.scaled(720, 576);
377         else saved = img.scaled(720, 480);
378         saved.setNumColors(4);
379         saved.save(img1);
380
381         updateColor(m_view.selected_color->color());
382         p.begin(&img);
383         m_scene->render(&p, QRectF(0, 0, m_width, m_height));
384         p.end();
385         if (m_view.menu_profile->currentIndex() < 2)
386             saved = img.scaled(720, 576);
387         else saved = img.scaled(720, 480);
388         saved.setNumColors(4);
389         saved.save(img2);
390
391
392         updateColor(m_view.highlighted_color->color());
393         p.begin(&img);
394         m_scene->render(&p, QRectF(0, 0, m_width, m_height));
395         p.end();
396         if (m_view.menu_profile->currentIndex() < 2)
397             saved = img.scaled(720, 576);
398         else saved = img.scaled(720, 480);
399         saved.setNumColors(4);
400         saved.save(img3);
401
402         updateColor();
403
404         m_scene->addItem(m_safeRect);
405         m_scene->addItem(m_color);
406         if (m_view.background_list->currentIndex() == 1) m_scene->addItem(m_background);
407     }
408 }
409
410
411 void DvdWizardMenu::createBackgroundImage(const QString &img1)
412 {
413     QImage img;
414     if (m_view.background_list->currentIndex() == 0) {
415         // color background
416         if (m_isPal)
417             img = QImage(768, 576, QImage::Format_ARGB32);
418         else
419             img = QImage(720, 540, QImage::Format_ARGB32);
420         img.fill(m_view.background_color->color().rgb());
421     } else if (m_view.background_list->currentIndex() == 1) {
422         img.load(m_view.background_image->url().path());
423         if (m_isPal) {
424             if (img.width() != 768 || img.height() != 576)
425                 img = img.scaled(768, 576, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
426         } else {
427             if (img.width() != 720 || img.height() != 540)
428                 img = img.scaled(720, 540, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
429         }
430     } else return;
431     img.save(img1);
432 }
433
434 bool DvdWizardMenu::createMenu() const
435 {
436     return m_view.create_menu->isChecked();
437 }
438
439 bool DvdWizardMenu::menuMovie() const
440 {
441     return m_view.background_list->currentIndex() == 2;
442 }
443
444 QString DvdWizardMenu::menuMoviePath() const
445 {
446     return m_view.background_image->url().path();
447 }
448
449
450 QMap <QString, QRect> DvdWizardMenu::buttonsInfo()
451 {
452     QMap <QString, QRect> info;
453     QList<QGraphicsItem *> list = m_scene->items();
454     for (int i = 0; i < list.count(); i++) {
455         if (list.at(i)->type() == QGraphicsItem::UserType + 1) {
456             DvdButton *button = static_cast < DvdButton* >(list.at(i));
457             QRect r = list.at(i)->sceneBoundingRect().toRect();
458             // Make sure y1 is not odd (requested by spumux)
459             if (r.height() % 2 == 1) r.setHeight(r.height() + 1);
460             if (r.y() % 2 == 1) r.setY(r.y() - 1);
461             info.insertMulti(button->command(), r);
462         }
463     }
464     return info;
465 }
466
467 bool DvdWizardMenu::isPalMenu() const
468 {
469     return m_isPal;
470 }