]> git.sesse.net Git - kdenlive/blob - src/dvdwizardmenu.cpp
Reindent the codebase using 'linux' bracket placement.
[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 {
27     m_view.setupUi(this);
28     m_view.play_text->setText(i18n("Play"));
29     m_scene = new DvdScene(this);
30     m_view.menu_preview->setScene(m_scene);
31
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()));
34
35     m_view.add_button->setIcon(KIcon("document-new"));
36     m_view.delete_button->setIcon(KIcon("trash-empty"));
37
38     // TODO: re-enable add button options
39     m_view.add_button->setVisible(false);
40     m_view.delete_button->setVisible(false);
41
42     m_view.menu_profile->addItems(QStringList() << i18n("PAL") << i18n("NTSC"));
43
44     if (profile == "dv_ntsc" || profile == "dv_ntsc_wide") {
45         m_view.menu_profile->setCurrentIndex(1);
46         m_isPal = false;
47     } else m_isPal = true;
48
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);
54
55
56     // create background image
57     m_background = new QGraphicsPixmapItem();
58     m_background->setZValue(3);
59     //m_scene->addItem(m_background);
60
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);
65     QPen pen(Qt::red);
66     pen.setStyle(Qt::DashLine);
67     pen.setWidth(3);
68     m_safeRect->setPen(pen);
69     m_safeRect->setZValue(5);
70     m_scene->addItem(m_safeRect);
71
72     changeProfile(m_view.menu_profile->currentIndex());
73     checkBackgroundType(0);
74
75     connect(m_view.menu_profile, SIGNAL(activated(int)), this, SLOT(changeProfile(int)));
76
77     // create menu button
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());
84     button->setZValue(4);
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);
90
91
92     //m_view.menu_preview->resizefitInView(0, 0, m_width, m_height);
93
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()));
100
101     connect(m_view.background_list, SIGNAL(activated(int)), this, SLOT(checkBackgroundType(int)));
102
103     connect(m_view.target_list, SIGNAL(activated(int)), this, SLOT(setButtonTarget(int)));
104
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()));
109
110     m_view.menu_box->setEnabled(false);
111
112 }
113
114 DvdWizardMenu::~DvdWizardMenu()
115 {
116     delete m_color;
117     delete m_safeRect;
118     delete m_background;
119     delete m_scene;
120 }
121
122 // virtual
123
124 bool DvdWizardMenu::isComplete() const
125 {
126     if (!m_view.create_menu->isChecked()) return true;
127     QList <int> targets;
128     QList<QGraphicsItem *> list = m_scene->items();
129     int buttonCount = 0;
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) {
133             buttonCount++;
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;
139                 }
140             }
141             targets.append(button->target());
142         }
143     }
144     if (buttonCount == 0) {
145         // We need at least one button
146         return false;
147     }
148
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;
152     }
153
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;
159     }
160     return true;
161 }
162
163 void DvdWizardMenu::setButtonTarget(int ix)
164 {
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);
170             break;
171         }
172     }
173     emit completeChanged();
174 }
175
176 void DvdWizardMenu::buttonChanged()
177 {
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);
186             foundButton = 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);
198             break;
199         }
200     }
201     if (!foundButton) m_view.tabWidget->widget(0)->setEnabled(false);
202 }
203
204 void DvdWizardMenu::addButton()
205 {
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);
219 }
220
221 void DvdWizardMenu::deleteButton()
222 {
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) {
226             delete list.at(i);
227             break;
228         }
229     }
230 }
231
232 void DvdWizardMenu::changeProfile(int ix)
233 {
234     switch (ix) {
235     case 1:
236         m_width = 720;
237         m_height = 480;
238         m_isPal = false;
239         updatePreview();
240         break;
241     default:
242         m_width = 720;
243         m_height = 576;
244         m_isPal = true;
245         updatePreview();
246         break;
247     }
248 }
249
250 void DvdWizardMenu::updatePreview()
251 {
252     m_scene->setProfile(m_width, m_height);
253     QMatrix matrix;
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);
257
258     m_color->setRect(0, 0, m_width, m_height);
259
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);
263 }
264
265 void DvdWizardMenu::setTargets(QStringList /*list*/)
266 {
267     m_targets = QStringList() << i18n("Play All");
268     // TODO: re-enable different targets
269     /*
270     if (list.count() > 1) {
271     m_targets += list;
272     }
273     m_view.target_list->clear();*/
274     m_view.target_list->addItems(m_targets);
275 }
276
277 void DvdWizardMenu::checkBackgroundType(int ix)
278 {
279     if (ix == 0) {
280         m_view.background_color->setVisible(true);
281         m_view.background_image->setVisible(false);
282         m_scene->removeItem(m_background);
283     } else {
284         m_view.background_color->setVisible(false);
285         m_view.background_image->setVisible(true);
286         if (ix == 1) {
287             m_view.background_image->setFilter("*");
288             m_scene->addItem(m_background);
289         } else {
290             m_scene->removeItem(m_background);
291             m_view.background_image->setFilter("video/mpeg");
292         }
293     }
294 }
295
296 void DvdWizardMenu::buildColor()
297 {
298     m_color->setBrush(m_view.background_color->color());
299 }
300
301 void DvdWizardMenu::buildImage()
302 {
303     emit completeChanged();
304     if (m_view.background_image->url().isEmpty()) {
305         m_scene->removeItem(m_background);
306         return;
307     }
308     QPixmap pix;
309     if (!pix.load(m_view.background_image->url().path())) {
310         m_scene->removeItem(m_background);
311         return;
312     }
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);
316 }
317
318 void DvdWizardMenu::checkBackground()
319 {
320     if (m_view.background_list->currentIndex() != 1) {
321         m_scene->removeItem(m_background);
322     } else {
323         m_scene->addItem(m_background);
324     }
325 }
326
327 void DvdWizardMenu::buildButton()
328 {
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));
334             break;
335         }
336     }
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());
344 }
345
346 void DvdWizardMenu::updateColor()
347 {
348     updateColor(m_view.text_color->color());
349     m_view.menu_preview->viewport()->update();
350 }
351
352 void DvdWizardMenu::updateColor(QColor c)
353 {
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);
360         }
361     }
362 }
363
364
365 void DvdWizardMenu::createButtonImages(const QString &img1, const QString &img2, const QString &img3)
366 {
367     if (m_view.create_menu->isChecked()) {
368         m_scene->clearSelection();
369         QImage img(m_width, m_height, QImage::Format_ARGB32);
370         QPainter p(&img);
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));
375         p.end();
376         QImage saved;
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);
381         saved.save(img1);
382
383         updateColor(m_view.selected_color->color());
384         p.begin(&img);
385         m_scene->render(&p, QRectF(0, 0, m_width, m_height));
386         p.end();
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);
391         saved.save(img2);
392
393
394         updateColor(m_view.highlighted_color->color());
395         p.begin(&img);
396         m_scene->render(&p, QRectF(0, 0, m_width, m_height));
397         p.end();
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);
402         saved.save(img3);
403
404         updateColor();
405
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);
409     }
410 }
411
412
413 void DvdWizardMenu::createBackgroundImage(const QString &img1)
414 {
415     QImage img;
416     if (m_view.background_list->currentIndex() == 0) {
417         // color background
418         if (m_isPal)
419             img = QImage(768, 576, QImage::Format_ARGB32);
420         else
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());
425         if (m_isPal) {
426             if (img.width() != 768 || img.height() != 576)
427                 img = img.scaled(768, 576, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
428         } else {
429             if (img.width() != 720 || img.height() != 540)
430                 img = img.scaled(720, 540, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
431         }
432     } else return;
433     img.save(img1);
434 }
435
436 bool DvdWizardMenu::createMenu() const
437 {
438     return m_view.create_menu->isChecked();
439 }
440
441 bool DvdWizardMenu::menuMovie() const
442 {
443     return m_view.background_list->currentIndex() == 2;
444 }
445
446 QString DvdWizardMenu::menuMoviePath() const
447 {
448     return m_view.background_image->url().path();
449 }
450
451
452 QMap <int, QRect> DvdWizardMenu::buttonsInfo()
453 {
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);
464         }
465     }
466     return info;
467 }
468
469 bool DvdWizardMenu::isPalMenu() const
470 {
471     return m_isPal;
472 }