-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgifmakerwidget.cpp
220 lines (182 loc) · 6.37 KB
/
gifmakerwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <QDir>
#include <QFile>
#include <QFileDialog>
#include <QFileInfo>
#include <QMessageBox>
#include <QMovie>
#include <QRect>
#include <QSettings>
#include <QString>
#include "gifmakerwidget.hpp"
#include "ptrutil.hpp"
namespace {
QString prettySize(double size) {
if(size < 1000) {
return QString("%1 B").arg(size);
}
size /= 1000;
if(size < 1000) {
return QString("%1 KB").arg(QString::number(size, 'f', 2));
}
size /= 1000;
return QString("%1 MB").arg(QString::number(size, 'f', 2));
}
QString prettyResolution(const QRect& area) {
return QString("%1x%2").arg(area.width()).arg(area.height());
}
} // namespace
namespace vfg {
namespace ui {
GifMakerWidget::GifMakerWidget(QWidget *parent) :
QDialog(parent)
{
ui.setupUi(this);
ui.comboImageMagick->addItems(imageMagick.childGroups());
const auto first = ui.comboImageMagick->currentText();
ui.plainTextPreset->setPlainText(parseImageMagickArgs(first));
ui.comboGifsicle->addItem(tr("None"));
ui.comboGifsicle->addItems(gifsicle.childGroups());
}
GifMakerWidget::~GifMakerWidget()
{
if(preview) {
const auto fileName = preview->fileName();
preview.reset();
QFile::remove(fileName);
}
}
void GifMakerWidget::showPreview(const QString& path)
{
if(preview && ui.checkBoxCompare->isChecked()) {
preview->stop();
previousPreview.reset(preview.release());
ui.labelPreviousGif->setMovie(previousPreview.get());
previousPreview->start();
ui.checkBoxCompare->setChecked(false);
}
preview = vfg::make_unique<QMovie>(path);
preview->setCacheMode(QMovie::CacheAll);
ui.labelPreview->setMovie(preview.get());
preview->start();
const auto bytes = prettySize(preview->device()->size());
const auto res = prettyResolution(preview->frameRect());
ui.labelGifSize->setText(QString("%1 [%2]").arg(bytes).arg(res));
ui.buttonSave->setEnabled(true);
ui.checkBoxCompare->setEnabled(true);
}
void GifMakerWidget::updateStartFrame(const int value)
{
if(value > ui.spinLastFrame->value()) {
ui.spinLastFrame->setValue(value);
config.setValue("gif/endframe", value);
}
ui.spinStartFrame->setValue(value);
config.setValue("gif/startframe", value);
ui.labelTotalFrames->setText(QString::number(totalFrames()));
}
void GifMakerWidget::updateLastFrame(const int value)
{
if(value < ui.spinStartFrame->value()) {
ui.spinStartFrame->setValue(value);
config.setValue("gif/startframe", value);
}
ui.spinLastFrame->setValue(value);
config.setValue("gif/endframe", value);
ui.labelTotalFrames->setText(QString::number(totalFrames()));
}
int GifMakerWidget::totalFrames() const
{
return (ui.spinLastFrame->value() - ui.spinStartFrame->value()) /
(ui.spinSkipFrames->value() + 1);
}
QString GifMakerWidget::parseImageMagickArgs(const QString& argName)
{
auto args = imageMagick.value(QString("%1/args").arg(argName)).toString();
args.replace(QString("%delay%"), QString::number(ui.spinFrameDelay->value()));
return args;
}
void GifMakerWidget::on_spinStartFrame_valueChanged(const int value)
{
updateStartFrame(value);
}
void GifMakerWidget::on_spinLastFrame_valueChanged(const int value)
{
updateLastFrame(value);
}
void GifMakerWidget::on_buttonAutoDelay_clicked()
{
const auto delay = (ui.spinSkipFrames->value() * 4) + 4;
const auto adjusted = delay <= ui.spinFrameDelay->maximum() ?
delay : ui.spinFrameDelay->maximum();
ui.spinFrameDelay->setValue(adjusted);
}
void GifMakerWidget::on_spinSkipFrames_valueChanged(const int value)
{
config.setValue("gif/skipframes", value);
ui.labelTotalFrames->setText(QString::number(totalFrames()));
}
void GifMakerWidget::on_buttonPreviewGif_clicked()
{
if(totalFrames() == 0) {
QMessageBox::information(this, tr("Nothing to process"),
tr("Set start/end frame by right-clicking the video"));
return;
}
config.setValue("gif/delay", ui.spinFrameDelay->value());
config.setValue("gif/skipframes", ui.spinSkipFrames->value());
auto args = ui.plainTextPreset->toPlainText();
if(args.isEmpty()) {
const auto key = ui.comboImageMagick->currentText();
args = imageMagick.value(QString("%1/args").arg(key)).toString();
}
const auto optKey = ui.comboGifsicle->currentText();
const auto optArgs = optKey == "None" ? "" : gifsicle.value(QString("%1/args").arg(optKey)).toString();
emit requestPreview(args, optArgs);
}
void GifMakerWidget::on_buttonSave_clicked()
{
const QDir saveDir(config.value("last_saved_gif_dir").toString());
const QString savePath = QFileDialog::getSaveFileName(this, tr("Select save path"),
saveDir.absoluteFilePath(preview->fileName()),
tr("GIF (*.gif)"));
if(savePath.isEmpty()) {
return;
}
if(QFile::exists(savePath)) {
// QFileDialog will ask to overwrite the output file if it exists
// but the copy operation below will not succeed if it's not removed
// since QFile::copy does not overwrite existing files
QFile::remove(savePath);
}
if(!QFile::copy(preview->fileName(), savePath)) {
QMessageBox::critical(this, tr("Saving failed"),
tr("Try again. If the problem persists, try a new filename."));
return;
}
const QFileInfo saved(savePath);
config.setValue("last_saved_gif_dir", saved.absoluteDir().absolutePath());
}
void GifMakerWidget::on_buttonReset_clicked()
{
ui.spinStartFrame->setValue(0);
ui.spinLastFrame->setValue(0);
ui.spinSkipFrames->setValue(0);
ui.spinFrameDelay->setValue(4);
ui.comboGifsicle->setCurrentIndex(0);
ui.labelPreview->clear();
ui.labelGifSize->setText(tr("n/a"));
ui.labelTotalFrames->clear();
ui.buttonSave->setEnabled(false);
}
void GifMakerWidget::on_comboImageMagick_activated(const QString &arg1)
{
ui.plainTextPreset->setPlainText(parseImageMagickArgs(arg1));
}
void GifMakerWidget::on_spinFrameDelay_valueChanged(const QString &arg1)
{
Q_UNUSED(arg1);
const auto current = ui.comboImageMagick->currentText();
ui.plainTextPreset->setPlainText(parseImageMagickArgs(current));
}
} // namespace ui
} // namespace vfg