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