dvdwizardmenu.cpp 32.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/***************************************************************************
 *   Copyright (C) 2009 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
 ***************************************************************************/

Vincent Pinon's avatar
Vincent Pinon committed
20 21
#include "dvdwizardmenu.h"

22
#include "kdenlivesettings.h"
23
#include "utils/KoIconUtils.h"
24

Laurent Montel's avatar
Laurent Montel committed
25
#include "kdenlive_debug.h"
26
#include "klocalizedstring.h"
Nicolas Carion's avatar
Nicolas Carion committed
27
#include <KColorScheme>
28
#include <QGraphicsDropShadowEffect>
Vincent Pinon's avatar
Vincent Pinon committed
29
#include "doc/kthumb.h"
30

31 32 33
#include <mlt++/MltProducer.h>
#include <mlt++/MltProfile.h>

34 35
enum { DvdButtonItem = QGraphicsItem::UserType + 1, DvdButtonUnderlineItem = QGraphicsItem::UserType + 2 };

36 37 38 39 40
DvdScene::DvdScene(QObject *parent)
    : QGraphicsScene(parent)
    , m_width(0)
    , m_height(0)
    , m_gridSize(1)
Vincent Pinon's avatar
Vincent Pinon committed
41 42
{
}
43 44
void DvdScene::setProfile(int width, int height)
{
Vincent Pinon's avatar
Vincent Pinon committed
45 46 47 48
    m_width = width;
    m_height = height;
    setSceneRect(0, 0, m_width, m_height);
}
Laurent Montel's avatar
Laurent Montel committed
49

50 51
int DvdScene::gridSize() const
{
Vincent Pinon's avatar
Vincent Pinon committed
52 53
    return m_gridSize;
}
Laurent Montel's avatar
Laurent Montel committed
54

55 56
void DvdScene::setGridSize(int gridSize)
{
Vincent Pinon's avatar
Vincent Pinon committed
57 58
    m_gridSize = gridSize;
}
Laurent Montel's avatar
Laurent Montel committed
59

60 61
void DvdScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
Vincent Pinon's avatar
Vincent Pinon committed
62 63 64
    QGraphicsScene::mouseReleaseEvent(mouseEvent);
    emit sceneChanged();
}
Laurent Montel's avatar
Laurent Montel committed
65

66 67
void DvdScene::drawForeground(QPainter *painter, const QRectF &rect)
{
Vincent Pinon's avatar
Vincent Pinon committed
68
    // draw the grid if needed
69
    if (gridSize() <= 1) {
Vincent Pinon's avatar
Vincent Pinon committed
70
        return;
71
    }
Vincent Pinon's avatar
Vincent Pinon committed
72

73
    QPen pen(QColor(255, 0, 0, 100));
Vincent Pinon's avatar
Vincent Pinon committed
74 75 76 77 78
    painter->setPen(pen);

    qreal left = int(rect.left()) - (int(rect.left()) % m_gridSize);
    qreal top = int(rect.top()) - (int(rect.top()) % m_gridSize);
    QVector<QPointF> points;
79 80 81
    for (qreal x = left; x < rect.right(); x += m_gridSize) {
        for (qreal y = top; y < rect.bottom(); y += m_gridSize) {
            points.append(QPointF(x, y));
Vincent Pinon's avatar
Vincent Pinon committed
82 83 84 85 86
        }
    }
    painter->drawPoints(points.data(), points.size());
}

87 88 89 90 91
DvdButton::DvdButton(const QString &text)
    : QGraphicsTextItem(text)
    , m_target(0)
    , m_command(QStringLiteral("jump title 1"))
    , m_backToMenu(false)
92
{
Vincent Pinon's avatar
Vincent Pinon committed
93 94 95
    setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
}
96 97
void DvdButton::setTarget(int t, const QString &c)
{
Vincent Pinon's avatar
Vincent Pinon committed
98 99 100
    m_target = t;
    m_command = c;
}
101 102
int DvdButton::target() const
{
Vincent Pinon's avatar
Vincent Pinon committed
103 104
    return m_target;
}
105 106
QString DvdButton::command() const
{
Vincent Pinon's avatar
Vincent Pinon committed
107 108
    return m_command;
}
109 110
bool DvdButton::backMenu() const
{
Vincent Pinon's avatar
Vincent Pinon committed
111 112
    return m_backToMenu;
}
113 114
int DvdButton::type() const
{
Vincent Pinon's avatar
Vincent Pinon committed
115 116 117
    // Enable the use of qgraphicsitem_cast with this item.
    return UserType + 1;
}
118 119
void DvdButton::setBackMenu(bool back)
{
Vincent Pinon's avatar
Vincent Pinon committed
120 121 122
    m_backToMenu = back;
}

123 124
QVariant DvdButton::itemChange(GraphicsItemChange change, const QVariant &value)
{
Nicolas Carion's avatar
Nicolas Carion committed
125
    if (change == ItemPositionChange && (scene() != nullptr)) {
Vincent Pinon's avatar
Vincent Pinon committed
126 127
        QPoint newPos = value.toPoint();

Nicolas Carion's avatar
Nicolas Carion committed
128 129
        if (QApplication::mouseButtons() == Qt::LeftButton && (qobject_cast<DvdScene *>(scene()) != nullptr)) {
            DvdScene *customScene = qobject_cast<DvdScene *>(scene());
Vincent Pinon's avatar
Vincent Pinon committed
130
            int gridSize = customScene->gridSize();
131 132
            int xV = (newPos.x() / gridSize) * gridSize;
            int yV = (newPos.y() / gridSize) * gridSize;
Vincent Pinon's avatar
Vincent Pinon committed
133 134 135 136
            newPos = QPoint(xV, yV);
        }

        QRectF sceneShape = sceneBoundingRect();
Nicolas Carion's avatar
Nicolas Carion committed
137
        DvdScene *sc = static_cast<DvdScene *>(scene());
Vincent Pinon's avatar
Vincent Pinon committed
138 139
        newPos.setX(qMax(newPos.x(), 0));
        newPos.setY(qMax(newPos.y(), 0));
140
        if (newPos.x() + sceneShape.width() > sc->width()) {
Vincent Pinon's avatar
Vincent Pinon committed
141
            newPos.setX(sc->width() - sceneShape.width());
142 143
        }
        if (newPos.y() + sceneShape.height() > sc->height()) {
Vincent Pinon's avatar
Vincent Pinon committed
144
            newPos.setY(sc->height() - sceneShape.height());
145
        }
Vincent Pinon's avatar
Vincent Pinon committed
146 147

        sceneShape.translate(newPos - pos());
148
        QList<QGraphicsItem *> list = scene()->items(sceneShape, Qt::IntersectsItemShape);
Vincent Pinon's avatar
Vincent Pinon committed
149 150 151
        list.removeAll(this);
        if (!list.isEmpty()) {
            for (int i = 0; i < list.count(); ++i) {
152
                if (list.at(i)->type() == Type) {
Vincent Pinon's avatar
Vincent Pinon committed
153
                    return pos();
154
                }
Vincent Pinon's avatar
Vincent Pinon committed
155 156 157 158 159 160 161
            }
        }
        return newPos;
    }
    return QGraphicsItem::itemChange(change, value);
}

Nicolas Carion's avatar
Nicolas Carion committed
162
DvdWizardMenu::DvdWizardMenu(DVDFORMAT format, QWidget *parent)
163 164 165 166 167
    : QWizardPage(parent)
    , m_color(nullptr)
    , m_safeRect(nullptr)
    , m_finalSize(720, 576)
    , m_movieLength(-1)
168
{
169 170 171 172
    m_view.setupUi(this);
    m_view.play_text->setText(i18n("Play"));
    m_scene = new DvdScene(this);
    m_view.menu_preview->setScene(m_scene);
173
    m_view.menu_preview->setMouseTracking(true);
Laurent Montel's avatar
Laurent Montel committed
174 175
    connect(m_view.create_menu, &QAbstractButton::toggled, m_view.menu_box, &QWidget::setEnabled);
    connect(m_view.create_menu, &QAbstractButton::toggled, this, &QWizardPage::completeChanged);
176

177 178 179 180
    m_view.add_button->setIcon(KoIconUtils::themedIcon(QStringLiteral("document-new")));
    m_view.delete_button->setIcon(KoIconUtils::themedIcon(QStringLiteral("trash-empty")));
    m_view.zoom_button->setIcon(KoIconUtils::themedIcon(QStringLiteral("zoom-in")));
    m_view.unzoom_button->setIcon(KoIconUtils::themedIcon(QStringLiteral("zoom-out")));
181

182 183
    m_view.add_button->setToolTip(i18n("Add new button"));
    m_view.delete_button->setToolTip(i18n("Delete current button"));
184

185
    changeProfile(format);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
186

187 188 189 190 191 192 193 194 195
    // Create color background
    m_color = new QGraphicsRectItem(0, 0, m_width, m_height);
    m_color->setBrush(m_view.background_color->color());
    m_color->setZValue(2);
    m_scene->addItem(m_color);

    // create background image
    m_background = new QGraphicsPixmapItem();
    m_background->setZValue(3);
Nicolas Carion's avatar
Nicolas Carion committed
196
    // m_scene->addItem(m_background);
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

    // create safe zone rect
    int safeW = m_width / 20;
    int safeH = m_height / 20;
    m_safeRect = new QGraphicsRectItem(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
    QPen pen(Qt::red);
    pen.setStyle(Qt::DashLine);
    pen.setWidth(3);
    m_safeRect->setPen(pen);
    m_safeRect->setZValue(5);
    m_scene->addItem(m_safeRect);
    checkBackgroundType(0);

    // create menu button
    DvdButton *button = new DvdButton(m_view.play_text->text());
    QFont font = m_view.font_family->currentFont();
    font.setPixelSize(m_view.font_size->value());
Nicolas Carion's avatar
Nicolas Carion committed
214
    // font.setStyleStrategy(QFont::NoAntialias);
215
    if (m_view.use_shadow->isChecked()) {
Nicolas Carion's avatar
Nicolas Carion committed
216
        auto *shadow = new QGraphicsDropShadowEffect(this);
217 218 219
        shadow->setBlurRadius(7);
        shadow->setOffset(4, 4);
        button->setGraphicsEffect(shadow);
220
    }
Laurent Montel's avatar
Laurent Montel committed
221
    connect(m_view.use_shadow, &QCheckBox::stateChanged, this, &DvdWizardMenu::slotEnableShadows);
222 223 224 225 226 227 228 229
    button->setFont(font);
    button->setDefaultTextColor(m_view.text_color->color());
    button->setZValue(4);
    QRectF r = button->sceneBoundingRect();
    m_scene->addItem(button);
    button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
    button->setSelected(true);

230
    if (m_view.use_grid->isChecked()) {
231
        m_scene->setGridSize(10);
232
    }
Laurent Montel's avatar
Laurent Montel committed
233
    connect(m_view.use_grid, &QAbstractButton::toggled, this, &DvdWizardMenu::slotUseGrid);
234

Nicolas Carion's avatar
Nicolas Carion committed
235
    // m_view.menu_preview->resizefitInView(0, 0, m_width, m_height);
236

Laurent Montel's avatar
Laurent Montel committed
237
    connect(m_view.play_text, &QLineEdit::textChanged, this, &DvdWizardMenu::buildButton);
Laurent Montel's avatar
Laurent Montel committed
238
    connect(m_view.text_color, SIGNAL(changed(QColor)), this, SLOT(updateColor()));
239
    connect(m_view.font_size, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &DvdWizardMenu::buildButton);
Laurent Montel's avatar
Laurent Montel committed
240 241 242
    connect(m_view.font_family, &QFontComboBox::currentFontChanged, this, &DvdWizardMenu::buildButton);
    connect(m_view.background_image, &KUrlRequester::textChanged, this, &DvdWizardMenu::buildImage);
    connect(m_view.background_color, &KColorButton::changed, this, &DvdWizardMenu::buildColor);
243

244
    connect(m_view.background_list, SIGNAL(currentIndexChanged(int)), this, SLOT(checkBackgroundType(int)));
245 246

    connect(m_view.target_list, SIGNAL(activated(int)), this, SLOT(setButtonTarget(int)));
Laurent Montel's avatar
Laurent Montel committed
247 248 249 250 251 252 253 254
    connect(m_view.back_to_menu, &QAbstractButton::toggled, this, &DvdWizardMenu::setBackToMenu);

    connect(m_view.add_button, &QAbstractButton::pressed, this, &DvdWizardMenu::addButton);
    connect(m_view.delete_button, &QAbstractButton::pressed, this, &DvdWizardMenu::deleteButton);
    connect(m_view.zoom_button, &QAbstractButton::pressed, this, &DvdWizardMenu::slotZoom);
    connect(m_view.unzoom_button, &QAbstractButton::pressed, this, &DvdWizardMenu::slotUnZoom);
    connect(m_scene, &QGraphicsScene::selectionChanged, this, &DvdWizardMenu::buttonChanged);
    connect(m_scene, &DvdScene::sceneChanged, this, &QWizardPage::completeChanged);
255

256
    // red background for error message
257
    KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Window, KSharedConfig::openConfig(KdenliveSettings::colortheme()));
258 259 260 261
    QPalette p = m_view.error_message->palette();
    p.setColor(QPalette::Background, scheme.background(KColorScheme::NegativeBackground).color());
    m_view.error_message->setAutoFillBackground(true);
    m_view.error_message->setPalette(p);
262 263
    m_view.menu_box->setEnabled(false);

264
    m_menuMessage = new KMessageWidget;
Nicolas Carion's avatar
Nicolas Carion committed
265
    QGridLayout *s = static_cast<QGridLayout *>(layout());
266 267 268
    s->addWidget(m_menuMessage, 7, 0, 1, -1);
    m_menuMessage->hide();
    m_view.error_message->hide();
269 270
}

271 272
DvdWizardMenu::~DvdWizardMenu()
{
273 274 275 276 277 278
    delete m_color;
    delete m_safeRect;
    delete m_background;
    delete m_scene;
}

279 280 281
void DvdWizardMenu::slotEnableShadows(int enable)
{
    QList<QGraphicsItem *> list = m_scene->items();
282
    for (int i = 0; i < list.count(); ++i) {
283
        if (list.at(i)->type() == DvdButtonItem) {
Nicolas Carion's avatar
Nicolas Carion committed
284 285
            if (enable != 0) {
                auto *shadow = new QGraphicsDropShadowEffect(this);
286 287 288
                shadow->setBlurRadius(7);
                shadow->setOffset(4, 4);
                list.at(i)->setGraphicsEffect(shadow);
289
            } else {
Laurent Montel's avatar
Laurent Montel committed
290
                list.at(i)->setGraphicsEffect(nullptr);
291
            }
292 293 294
        }
    }
}
295

296 297
void DvdWizardMenu::setButtonTarget(int ix)
{
298
    QList<QGraphicsItem *> list = m_scene->selectedItems();
299
    for (int i = 0; i < list.count(); ++i) {
300
        if (list.at(i)->type() == DvdButtonItem) {
Nicolas Carion's avatar
Nicolas Carion committed
301
            DvdButton *button = static_cast<DvdButton *>(list.at(i));
302
            button->setTarget(ix, m_view.target_list->itemData(ix).toString());
303 304 305 306 307 308
            break;
        }
    }
    emit completeChanged();
}

309 310 311
void DvdWizardMenu::setBackToMenu(bool backToMenu)
{
    QList<QGraphicsItem *> list = m_scene->selectedItems();
312
    for (int i = 0; i < list.count(); ++i) {
313
        if (list.at(i)->type() == DvdButtonItem) {
Nicolas Carion's avatar
Nicolas Carion committed
314
            DvdButton *button = static_cast<DvdButton *>(list.at(i));
315 316 317 318 319 320 321
            button->setBackMenu(backToMenu);
            break;
        }
    }
    emit completeChanged();
}

322 323
void DvdWizardMenu::buttonChanged()
{
324 325
    QList<QGraphicsItem *> list = m_scene->selectedItems();
    bool foundButton = false;
326
    for (int i = 0; i < list.count(); ++i) {
327
        if (list.at(i)->type() == DvdButtonItem) {
328 329 330 331
            m_view.play_text->blockSignals(true);
            m_view.font_size->blockSignals(true);
            m_view.font_family->blockSignals(true);
            m_view.target_list->blockSignals(true);
332
            m_view.back_to_menu->blockSignals(true);
333
            foundButton = true;
334
            m_view.tabWidget->widget(0)->setEnabled(true);
Nicolas Carion's avatar
Nicolas Carion committed
335
            DvdButton *button = static_cast<DvdButton *>(list.at(i));
336 337
            m_view.target_list->setCurrentIndex(button->target());
            m_view.play_text->setText(button->toPlainText());
338
            m_view.back_to_menu->setChecked(button->backMenu());
339 340 341 342 343 344 345
            QFont font = button->font();
            m_view.font_size->setValue(font.pixelSize());
            m_view.font_family->setCurrentFont(font);
            m_view.play_text->blockSignals(false);
            m_view.font_size->blockSignals(false);
            m_view.font_family->blockSignals(false);
            m_view.target_list->blockSignals(false);
346
            m_view.back_to_menu->blockSignals(false);
347 348 349
            break;
        }
    }
350 351 352
    if (!foundButton) {
        m_view.tabWidget->widget(0)->setEnabled(false);
    }
353 354
}

355 356
void DvdWizardMenu::addButton()
{
357 358 359 360
    m_scene->clearSelection();
    DvdButton *button = new DvdButton(m_view.play_text->text());
    QFont font = m_view.font_family->currentFont();
    font.setPixelSize(m_view.font_size->value());
361
    if (m_view.use_shadow->isChecked()) {
Nicolas Carion's avatar
Nicolas Carion committed
362
        auto *shadow = new QGraphicsDropShadowEffect(this);
363 364 365
        shadow->setBlurRadius(7);
        shadow->setOffset(4, 4);
        button->setGraphicsEffect(shadow);
366
    }
Nicolas Carion's avatar
Nicolas Carion committed
367
    // font.setStyleStrategy(QFont::NoAntialias);
368
    button->setFont(font);
369
    button->setDefaultTextColor(m_view.text_color->color());
370 371 372 373 374 375 376
    button->setZValue(4);
    QRectF r = button->sceneBoundingRect();
    m_scene->addItem(button);
    button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
    button->setSelected(true);
}

377 378
void DvdWizardMenu::deleteButton()
{
379
    QList<QGraphicsItem *> list = m_scene->selectedItems();
380
    for (int i = 0; i < list.count(); ++i) {
381
        if (list.at(i)->type() == DvdButtonItem) {
382 383 384 385 386 387
            delete list.at(i);
            break;
        }
    }
}

388 389 390 391
void DvdWizardMenu::changeProfile(DVDFORMAT format)
{
    m_format = format;
    switch (m_format) {
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
    case PAL_WIDE:
        m_finalSize = QSize(720, 576);
        m_width = 1024;
        m_height = 576;
        break;
    case NTSC_WIDE:
        m_finalSize = QSize(720, 480);
        m_width = 853;
        m_height = 480;
        break;
    case NTSC:
        m_finalSize = QSize(720, 480);
        m_width = 640;
        m_height = 480;
        break;
    default:
        m_finalSize = QSize(720, 576);
        m_width = 768;
        m_height = 576;
411
    }
412
    updatePreview();
413 414
}

415 416
void DvdWizardMenu::updatePreview()
{
417 418 419 420 421 422
    m_scene->setProfile(m_width, m_height);
    QMatrix matrix;
    matrix.scale(0.5, 0.5);
    m_view.menu_preview->setMatrix(matrix);
    m_view.menu_preview->setMinimumSize(m_width / 2 + 4, m_height / 2 + 8);

423 424 425
    if (m_color) {
        m_color->setRect(0, 0, m_width, m_height);
    }
426 427 428

    int safeW = m_width / 20;
    int safeH = m_height / 20;
429 430 431
    if (m_safeRect) {
        m_safeRect->setRect(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
    }
432 433
}

434
void DvdWizardMenu::setTargets(const QStringList &list, const QStringList &targetlist)
435
{
436
    m_view.target_list->clear();
437
    m_view.target_list->addItem(i18n("Play All"), "jump title 1");
438
    int movieCount = 0;
439
    for (int i = 0; i < list.count(); ++i) {
440
        if (targetlist.at(i).contains(QStringLiteral("chapter"))) {
441
            m_view.target_list->addItem(list.at(i), targetlist.at(i));
442
        } else {
443
            m_view.target_list->addItem(KoIconUtils::themedIcon(QStringLiteral("video-x-generic")), list.at(i), targetlist.at(i));
444 445
            movieCount++;
        }
446
    }
447
    m_view.back_to_menu->setHidden(movieCount == 1);
448 449
}

450 451
void DvdWizardMenu::checkBackgroundType(int ix)
{
452 453 454
    if (ix == 0) {
        m_view.background_color->setVisible(true);
        m_view.background_image->setVisible(false);
455
        m_view.loop_movie->setVisible(false);
Laurent Montel's avatar
Laurent Montel committed
456
        if (m_background->scene() != nullptr) {
457 458
            m_scene->removeItem(m_background);
        }
459 460 461 462
    } else {
        m_view.background_color->setVisible(false);
        m_view.background_image->setVisible(true);
        if (ix == 1) {
463
            m_view.background_image->clear();
464
            m_view.background_image->setFilter(QStringLiteral("*"));
Laurent Montel's avatar
Laurent Montel committed
465
            if (m_background->scene() != nullptr) {
466 467
                m_scene->removeItem(m_background);
            }
468
            m_view.loop_movie->setVisible(false);
469
        } else {
Laurent Montel's avatar
Laurent Montel committed
470
            if (m_background->scene() != nullptr) {
471 472
                m_scene->removeItem(m_background);
            }
473
            m_view.background_image->clear();
474
            m_view.background_image->setFilter(QStringLiteral("video/mpeg"));
475
            m_view.loop_movie->setVisible(true);
476 477
        }
    }
478
    emit completeChanged();
479 480
}

481 482
void DvdWizardMenu::buildColor()
{
483 484 485
    m_color->setBrush(m_view.background_color->color());
}

486 487
void DvdWizardMenu::slotUseGrid(bool useGrid)
{
488 489 490 491 492 493
    if (useGrid) {
        m_scene->setGridSize(10);
    } else {
        m_scene->setGridSize(1);
    }
    m_scene->update();
494 495
}

496 497
void DvdWizardMenu::buildImage()
{
498
    emit completeChanged();
499
    if (m_view.background_image->url().isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
500
        if (m_background->scene() != nullptr) {
501 502
            m_scene->removeItem(m_background);
        }
503 504 505
        return;
    }
    QPixmap pix;
506 507 508

    if (m_view.background_list->currentIndex() == 1) {
        // image background
509
        if (!pix.load(m_view.background_image->url().toLocalFile())) {
Laurent Montel's avatar
Laurent Montel committed
510
            if (m_background->scene() != nullptr) {
511 512
                m_scene->removeItem(m_background);
            }
513 514 515 516 517
            return;
        }
        pix = pix.scaled(m_width, m_height);
    } else if (m_view.background_list->currentIndex() == 2) {
        // video background
518
        m_movieLength = -1;
519 520
        QString profileName = DvdWizardVob::getDvdProfile(m_format);
        Mlt::Profile profile(profileName.toUtf8().constData());
Nicolas Carion's avatar
Nicolas Carion committed
521
        profile.set_explicit(1);
522
        Mlt::Producer *producer = new Mlt::Producer(profile, m_view.background_image->url().toLocalFile().toUtf8().constData());
Nicolas Carion's avatar
Nicolas Carion committed
523
        if ((producer != nullptr) && producer->is_valid()) {
524
            pix = QPixmap::fromImage(KThumb::getFrame(producer, 0, m_width, m_height));
525 526
            m_movieLength = producer->get_length();
        }
527
        delete producer;
528 529
    }
    m_background->setPixmap(pix);
530
    m_scene->addItem(m_background);
531 532
}

533 534
void DvdWizardMenu::buildButton()
{
Laurent Montel's avatar
Laurent Montel committed
535
    DvdButton *button = nullptr;
536
    QList<QGraphicsItem *> list = m_scene->selectedItems();
537
    for (int i = 0; i < list.count(); ++i) {
538
        if (list.at(i)->type() == DvdButtonItem) {
Nicolas Carion's avatar
Nicolas Carion committed
539
            button = static_cast<DvdButton *>(list.at(i));
540 541 542
            break;
        }
    }
Laurent Montel's avatar
Laurent Montel committed
543
    if (button == nullptr) {
544 545
        return;
    }
546 547 548
    button->setPlainText(m_view.play_text->text());
    QFont font = m_view.font_family->currentFont();
    font.setPixelSize(m_view.font_size->value());
Nicolas Carion's avatar
Nicolas Carion committed
549
    // font.setStyleStrategy(QFont::NoAntialias);
550 551
    button->setFont(font);
    button->setDefaultTextColor(m_view.text_color->color());
552 553
    // Check for button overlapping in case we changed text / size
    emit completeChanged();
554 555
}

556 557
void DvdWizardMenu::updateColor()
{
558 559 560 561
    updateColor(m_view.text_color->color());
    m_view.menu_preview->viewport()->update();
}

562 563 564
void DvdWizardMenu::prepareUnderLines()
{
    QList<QGraphicsItem *> list = m_scene->items();
565
    for (int i = 0; i < list.count(); ++i) {
566
        if (list.at(i)->type() == DvdButtonItem) {
567 568
            QRectF r = list.at(i)->sceneBoundingRect();
            int bottom = r.bottom() - 1;
569 570 571
            if (bottom % 2 == 1) {
                bottom = bottom - 1;
            }
572
            int underlineHeight = r.height() / 10;
573 574 575
            if (underlineHeight % 2 == 1) {
                underlineHeight = underlineHeight - 1;
            }
576 577 578 579 580
            underlineHeight = qMin(underlineHeight, 10);
            underlineHeight = qMax(underlineHeight, 2);
            r.setTop(bottom - underlineHeight);
            r.setBottom(bottom);
            r.adjust(2, 0, -2, 0);
Nicolas Carion's avatar
Nicolas Carion committed
581
            auto *underline = new DvdButtonUnderline(r);
582 583 584
            m_scene->addItem(underline);
            list.at(i)->setVisible(false);
        }
585 586 587 588 589 590 591
    }
}

void DvdWizardMenu::resetUnderLines()
{
    QList<QGraphicsItem *> list = m_scene->items();
    QList<QGraphicsItem *> toDelete;
592
    for (int i = 0; i < list.count(); ++i) {
593 594 595
        if (list.at(i)->type() == DvdButtonUnderlineItem) {
            toDelete.append(list.at(i));
        }
596
        if (list.at(i)->type() == DvdButtonItem) {
597 598
            list.at(i)->setVisible(true);
        }
599 600
    }
    while (!toDelete.isEmpty()) {
601 602
        QGraphicsItem *item = toDelete.takeFirst();
        delete item;
603 604 605 606 607 608
    }
}

void DvdWizardMenu::updateUnderlineColor(QColor c)
{
    QList<QGraphicsItem *> list = m_scene->items();
609
    for (int i = 0; i < list.count(); ++i) {
610
        if (list.at(i)->type() == DvdButtonUnderlineItem) {
Nicolas Carion's avatar
Nicolas Carion committed
611
            DvdButtonUnderline *underline = static_cast<DvdButtonUnderline *>(list.at(i));
612 613 614
            underline->setPen(Qt::NoPen);
            c.setAlpha(150);
            underline->setBrush(c);
615 616 617 618
        }
    }
}

619
void DvdWizardMenu::updateColor(const QColor &c)
620
{
Laurent Montel's avatar
Laurent Montel committed
621
    DvdButton *button = nullptr;
622
    QList<QGraphicsItem *> list = m_scene->items();
623
    for (int i = 0; i < list.count(); ++i) {
624
        if (list.at(i)->type() == DvdButtonItem) {
Nicolas Carion's avatar
Nicolas Carion committed
625
            button = static_cast<DvdButton *>(list.at(i));
626 627 628 629 630
            button->setDefaultTextColor(c);
        }
    }
}

631
void DvdWizardMenu::createButtonImages(const QString &selected_image, const QString &highlighted_image, bool letterbox)
632
{
633 634
    if (m_view.create_menu->isChecked()) {
        m_scene->clearSelection();
635 636
        QRectF source(0, 0, m_width, m_height);
        QRectF target;
637 638 639
        if (!letterbox) {
            target = QRectF(0, 0, m_finalSize.width(), m_finalSize.height());
        } else {
640
            // Scale the button images to fit a letterbox image
Nicolas Carion's avatar
Nicolas Carion committed
641
            double factor = (double)m_width / m_finalSize.width();
642 643 644
            int letterboxHeight = m_height / factor;
            target = QRectF(0, (m_finalSize.height() - letterboxHeight) / 2, m_finalSize.width(), letterboxHeight);
        }
Laurent Montel's avatar
Laurent Montel committed
645
        if (m_safeRect->scene() != nullptr) {
646 647
            m_scene->removeItem(m_safeRect);
        }
Laurent Montel's avatar
Laurent Montel committed
648
        if (m_color->scene() != nullptr) {
649 650
            m_scene->removeItem(m_color);
        }
Laurent Montel's avatar
Laurent Montel committed
651
        if (m_background->scene() != nullptr) {
652 653
            m_scene->removeItem(m_background);
        }
654
        prepareUnderLines();
655
        QImage img(m_finalSize.width(), m_finalSize.height(), QImage::Format_ARGB32);
656
        img.fill(Qt::transparent);
657
        updateUnderlineColor(m_view.highlighted_color->color());
658

659
        QPainter p;
660
        p.begin(&img);
Nicolas Carion's avatar
Nicolas Carion committed
661 662
        // p.setRenderHints(QPainter::Antialiasing, false);
        // p.setRenderHints(QPainter::TextAntialiasing, false);
663
        m_scene->render(&p, target, source, Qt::IgnoreAspectRatio);
664
        p.end();
665
        img.setColor(0, m_view.highlighted_color->color().rgb());
666
        img.setColor(1, qRgba(0, 0, 0, 0));
667
        img.save(highlighted_image);
668
        img.fill(Qt::transparent);
669
        updateUnderlineColor(m_view.selected_color->color());
670

671
        p.begin(&img);
Nicolas Carion's avatar
Nicolas Carion committed
672 673
        // p.setRenderHints(QPainter::Antialiasing, false);
        // p.setRenderHints(QPainter::TextAntialiasing, false);
674
        m_scene->render(&p, target, source, Qt::IgnoreAspectRatio);
675
        p.end();
676
        img.setColor(0, m_view.selected_color->color().rgb());
677
        img.setColor(1, qRgba(0, 0, 0, 0));
678
        img.save(selected_image);
679
        resetUnderLines();
680 681
        m_scene->addItem(m_safeRect);
        m_scene->addItem(m_color);
682 683 684
        if (m_view.background_list->currentIndex() > 0) {
            m_scene->addItem(m_background);
        }
685 686 687
    }
}

688
void DvdWizardMenu::createBackgroundImage(const QString &img1, bool letterbox)
689
{
Vincent Pinon's avatar
Vincent Pinon committed
690 691
    Q_UNUSED(letterbox)

692
    m_scene->clearSelection();
Laurent Montel's avatar
Laurent Montel committed
693
    if (m_safeRect->scene() != nullptr) {
694 695
        m_scene->removeItem(m_safeRect);
    }
696
    bool showBg = false;
697
    QImage img(m_width, m_height, QImage::Format_ARGB32);
698

Nicolas Carion's avatar
Nicolas Carion committed
699
    // TODO: Should the image be scaled when letterboxing?
700 701 702 703 704
    /*
    QRectF source(0, 0, m_width, m_height);
    QRectF target;
    if (!letterbox) target = QRectF(0, 0, m_finalSize.width(), m_finalSize.height());
    else {
705 706 707 708
    // Scale the button images to fit a letterbox image
    double factor = (double) m_width / m_finalSize.width();
    int letterboxHeight = m_height / factor;
    target = QRectF(0, (m_finalSize.height() - letterboxHeight) / 2, m_finalSize.width(), letterboxHeight);
709
    }*/
710

711
    if (menuMovie()) {
712
        showBg = true;
Laurent Montel's avatar
Laurent Montel committed
713
        if (m_background->scene() != nullptr) {
714 715
            m_scene->removeItem(m_background);
        }
Laurent Montel's avatar
Laurent Montel committed
716
        if (m_color->scene() != nullptr) {
717 718
            m_scene->removeItem(m_color);
        }
719
        img.fill(Qt::transparent);
720
    }
721 722 723 724
    updateColor(m_view.text_color->color());
    QPainter p(&img);
    p.setRenderHints(QPainter::Antialiasing, true);
    p.setRenderHints(QPainter::TextAntialiasing, true);
725 726 727 728
    // set image grid to "1" to ensure we don't display dots all over
    // the image when rendering
    int oldSize = m_scene->gridSize();
    m_scene->setGridSize(1);
729
    m_scene->render(&p, QRectF(0, 0, img.width(), img.height()));
730
    m_scene->setGridSize(oldSize);
Nicolas Carion's avatar
Nicolas Carion committed
731
    // m_scene->render(&p, target, source, Qt::IgnoreAspectRatio);
732 733 734
    p.end();
    img.save(img1);
    m_scene->addItem(m_safeRect);
735
    if (showBg) {
736 737
        m_scene->addItem(m_background);
        m_scene->addItem(m_color);
738
    }
739 740
}

741 742
bool DvdWizardMenu::createMenu() const
{
743 744 745
    return m_view.create_menu->isChecked();
}

746 747 748 749 750
bool DvdWizardMenu::loopMovie() const
{
    return m_view.loop_movie->isChecked();
}

751 752
bool DvdWizardMenu::menuMovie() const
{
753 754 755
    return m_view.background_list->currentIndex() == 2;
}

756 757
QString DvdWizardMenu::menuMoviePath() const
{
758
    return m_view.background_image->url().toLocalFile();
759 760
}

761 762
int DvdWizardMenu::menuMovieLength() const
{
763
    return m_movieLength;
764 765
}

Laurent Montel's avatar
Laurent Montel committed
766
QMap<QString, QRect> DvdWizardMenu::buttonsInfo(bool letterbox)
767
{
Laurent Montel's avatar
Laurent Montel committed
768
    QMap<QString, QRect> info;
769
    QList<QGraphicsItem *> list = m_scene->items();
Nicolas Carion's avatar
Nicolas Carion committed
770
    double ratiox = (double)m_finalSize.width() / m_width;
771 772 773
    double ratioy = 1;
    int offset = 0;
    if (letterbox) {
774
        int letterboxHeight = m_height * ratiox;
Nicolas Carion's avatar
Nicolas Carion committed
775
        ratioy = (double)letterboxHeight / m_finalSize.height();
776
        offset = (m_finalSize.height() - letterboxHeight) / 2;
777
    }