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, SIGNAL(clicked()), this, SLOT(startRecording()));
26 connect(m_ui.stop, SIGNAL(clicked()), this, SLOT(stopRecording()));
27
28 connect(m_ui.selectFile, SIGNAL(clicked()), this, SLOT(selectFile()));
29 connect(m_ui.filename, SIGNAL(textChanged(const QString&)), this, SLOT(setFilename(const QString&)));
30
31 connect(m_ui.frameskip, SIGNAL(valueChanged(int)), this, SLOT(updateDelay()));
32 connect(m_ui.delayAuto, SIGNAL(clicked(bool)), this, SLOT(updateDelay()));
33
34 ImageMagickGIFEncoderInit(&m_encoder);
35}
36
37GIFView::~GIFView() {
38 stopRecording();
39}
40
41void GIFView::startRecording() {
42 int delayMs = m_ui.delayAuto->isChecked() ? -1 : m_ui.delayMs->value();
43 ImageMagickGIFEncoderSetParams(&m_encoder, m_ui.frameskip->value(), delayMs);
44 if (!ImageMagickGIFEncoderOpen(&m_encoder, m_filename.toUtf8().constData())) {
45 LOG(QT, ERROR) << tr("Failed to open output GIF file: %1").arg(m_filename);
46 return;
47 }
48 m_ui.start->setEnabled(false);
49 m_ui.stop->setEnabled(true);
50 m_ui.groupBox->setEnabled(false);
51 emit recordingStarted(&m_encoder.d);
52}
53
54void GIFView::stopRecording() {
55 emit recordingStopped();
56 ImageMagickGIFEncoderClose(&m_encoder);
57 m_ui.stop->setEnabled(false);
58 m_ui.start->setEnabled(true);
59 m_ui.groupBox->setEnabled(true);
60}
61
62void GIFView::selectFile() {
63 QString filename = GBAApp::app()->getSaveFileName(this, tr("Select output file"), tr("Graphics Interchange Format (*.gif)"));
64 if (!filename.isEmpty()) {
65 m_ui.filename->setText(filename);
66 if (!ImageMagickGIFEncoderIsOpen(&m_encoder)) {
67 m_ui.start->setEnabled(true);
68 }
69 }
70}
71
72void GIFView::setFilename(const QString& fname) {
73 m_filename = fname;
74}
75
76void GIFView::updateDelay() {
77 if (!m_ui.delayAuto->isChecked()) {
78 return;
79 }
80
81 uint64_t s = (m_ui.frameskip->value() + 1);
82 s *= VIDEO_TOTAL_LENGTH * 1000;
83 s /= GBA_ARM7TDMI_FREQUENCY;
84 m_ui.delayMs->setValue(s);
85}
86
87#endif