]> git.sesse.net Git - kdenlive/blob - src/dvdwizardmenu.cpp
Krazy fixes: cleanup all headers
[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 (list.at(j)->type() == button->type()) return false;
136                 }
137             }
138             targets.append(button->target());
139         }
140     }
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;
147     }
148     return true;
149 }
150
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);
157             break;
158         }
159     }
160     emit completeChanged();
161 }
162
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);
172             foundButton = 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);
184             break;
185         }
186     }
187     if (!foundButton) m_view.tabWidget->widget(0)->setEnabled(false);
188 }
189
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);
204 }
205
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) {
210             delete list.at(i);
211             break;
212         }
213     }
214 }
215
216 void DvdWizardMenu::changeProfile(int ix) {
217     switch (ix) {
218     case 1:
219         m_width = 720;
220         m_height = 480;
221         m_isPal = false;
222         updatePreview();
223         break;
224     default:
225         m_width = 720;
226         m_height = 576;
227         m_isPal = true;
228         updatePreview();
229         break;
230     }
231 }
232
233 void DvdWizardMenu::updatePreview() {
234     m_scene->setProfile(m_width, m_height);
235     QMatrix matrix;
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);
239
240     m_color->setRect(0, 0, m_width, m_height);
241
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);
245 }
246
247 void DvdWizardMenu::setTargets(QStringList list) {
248     m_targets = QStringList() << i18n("Play All");
249     // TODO: re-enable different targets
250     /*
251     if (list.count() > 1) {
252     m_targets += list;
253     }
254     m_view.target_list->clear();*/
255     m_view.target_list->addItems(m_targets);
256 }
257
258 void DvdWizardMenu::checkBackgroundType(int ix) {
259     if (ix == 0) {
260         m_view.background_color->setVisible(true);
261         m_view.background_image->setVisible(false);
262         m_scene->removeItem(m_background);
263     } else {
264         m_view.background_color->setVisible(false);
265         m_view.background_image->setVisible(true);
266         if (ix == 1) {
267             m_view.background_image->setFilter("*");
268             m_scene->addItem(m_background);
269         } else {
270             m_scene->removeItem(m_background);
271             m_view.background_image->setFilter("video/mpeg");
272         }
273     }
274 }
275
276 void DvdWizardMenu::buildColor() {
277     m_color->setBrush(m_view.background_color->color());
278 }
279
280 void DvdWizardMenu::buildImage() {
281     if (m_view.background_image->url().isEmpty()) {
282         m_scene->removeItem(m_background);
283         return;
284     }
285     QPixmap pix;
286     if (!pix.load(m_view.background_image->url().path())) {
287         m_scene->removeItem(m_background);
288         return;
289     }
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);
293 }
294
295 void DvdWizardMenu::checkBackground() {
296     if (m_view.background_list->currentIndex() != 1) {
297         m_scene->removeItem(m_background);
298     } else {
299         m_scene->addItem(m_background);
300     }
301 }
302
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));
309             break;
310         }
311     }
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());
319 }
320
321 void DvdWizardMenu::updateColor() {
322     updateColor(m_view.text_color->color());
323     m_view.menu_preview->viewport()->update();
324 }
325
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);
333         }
334     }
335 }
336
337
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);
342         QPainter p(&img);
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));
347         p.end();
348         img.setNumColors(4);
349         QImage saved;
350         if (m_view.menu_profile->currentIndex() < 2)
351             saved = img.scaled(720, 576);
352         else saved = img.scaled(720, 480);
353         saved.save(img1);
354
355         updateColor(m_view.selected_color->color());
356         p.begin(&img);
357         m_scene->render(&p, QRectF(0, 0, m_width, m_height));
358         p.end();
359         img.setNumColors(4);
360         if (m_view.menu_profile->currentIndex() < 2)
361             saved = img.scaled(720, 576);
362         else saved = img.scaled(720, 480);
363         saved.save(img2);
364
365
366         updateColor(m_view.highlighted_color->color());
367         p.begin(&img);
368         m_scene->render(&p, QRectF(0, 0, m_width, m_height));
369         p.end();
370         img.setNumColors(4);
371         if (m_view.menu_profile->currentIndex() < 2)
372             saved = img.scaled(720, 576);
373         else saved = img.scaled(720, 480);
374         saved.save(img3);
375
376         updateColor();
377
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);
381     }
382 }
383
384
385 void DvdWizardMenu::createBackgroundImage(const QString &img1) {
386     QImage img;
387     if (m_view.background_list->currentIndex() == 0) {
388         // color background
389         if (m_isPal)
390             img = QImage(768, 576, QImage::Format_ARGB32);
391         else
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());
396         if (m_isPal) {
397             if (img.width() != 768 || img.height() != 576)
398                 img = img.scaled(768, 576, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
399         } else {
400             if (img.width() != 720 || img.height() != 540)
401                 img = img.scaled(720, 540, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
402         }
403     } else return;
404     img.save(img1);
405 }
406
407 bool DvdWizardMenu::createMenu() const {
408     return m_view.create_menu->isChecked();
409 }
410
411 bool DvdWizardMenu::menuMovie() const {
412     return m_view.background_list->currentIndex() == 2;
413 }
414
415 QString DvdWizardMenu::menuMoviePath() const {
416     return m_view.background_image->url().path();
417 }
418
419
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);
431         }
432     }
433     return info;
434 }
435
436 bool DvdWizardMenu::isPalMenu() const {
437     return m_isPal;
438 }