src/platform/qt/GIFView.cpp (view raw)
1/* Copyright (c) 2013-2014 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "GIFView.h"
7
8#ifdef USE_MAGICK
9
10#include "GBAApp.h"
11#include "LogController.h"
12
13#include <QMap>
14
15#include <mgba/internal/gba/gba.h>
16#include <mgba/internal/gba/video.h>
17
18using namespace QGBA;
19
20GIFView::GIFView(QWidget* parent)
21 : QWidget(parent)
22{
23 m_ui.setupUi(this);
24
25 connect(m_ui.start, &QAbstractButton::clicked, this, &GIFView::startRecording);
26 connect(m_ui.stop, &QAbstractButton::clicked, this, &GIFView::stopRecording);
27
28 connect(m_ui.selectFile, &QAbstractButton::clicked, this, &GIFView::selectFile);
29 connect(m_ui.filename, &QLineEdit::textChanged, this, &GIFView::setFilename);
30
31 connect(m_ui.frameskip, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
32 this, &GIFView::updateDelay);
33 connect(m_ui.delayAuto, &QAbstractButton::clicked, this, &GIFView::updateDelay);
34
35 ImageMagickGIFEncoderInit(&m_encoder);
36}
37
38GIFView::~GIFView() {
39 stopRecording();
40}
41
42void GIFView::startRecording() {
43 int delayMs = m_ui.delayAuto->isChecked() ? -1 : m_ui.delayMs->value();
44 ImageMagickGIFEncoderSetParams(&m_encoder, m_ui.frameskip->value(), delayMs);
45 if (!ImageMagickGIFEncoderOpen(&m_encoder, m_filename.toUtf8().constData())) {
46 LOG(QT, ERROR) << tr("Failed to open output GIF file: %1").arg(m_filename);
47 return;
48 }
49 m_ui.start->setEnabled(false);
50 m_ui.stop->setEnabled(true);
51 m_ui.groupBox->setEnabled(false);
52 emit recordingStarted(&m_encoder.d);
53}
54
55void GIFView::stopRecording() {
56 emit recordingStopped();
57 ImageMagickGIFEncoderClose(&m_encoder);
58 m_ui.stop->setEnabled(false);
59 m_ui.start->setEnabled(true);
60 m_ui.groupBox->setEnabled(true);
61}
62
63void GIFView::selectFile() {
64 QString filename = GBAApp::app()->getSaveFileName(this, tr("Select output file"), tr("Graphics Interchange Format (*.gif)"));
65 if (!filename.isEmpty()) {
66 m_ui.filename->setText(filename);
67 if (!ImageMagickGIFEncoderIsOpen(&m_encoder)) {
68 m_ui.start->setEnabled(true);
69 }
70 }
71}
72
73void GIFView::setFilename(const QString& fname) {
74 m_filename = fname;
75}
76
77void GIFView::updateDelay() {
78 if (!m_ui.delayAuto->isChecked()) {
79 return;
80 }
81
82 uint64_t s = (m_ui.frameskip->value() + 1);
83 s *= VIDEO_TOTAL_LENGTH * 1000;
84 s /= GBA_ARM7TDMI_FREQUENCY;
85 m_ui.delayMs->setValue(s);
86}
87
88#endif