Skip to content

Commit 3b4a2fb

Browse files
committed
Moved parsing of screenshot-template to mpvhandler
It started to show up all over the place so I consolidated it into mpvhandler on loading settings. The only place it's repeated is in preferences dialog which should be modified to not write directly to settings as it is now. Fixed up the screenshot message.
1 parent 42446a7 commit 3b4a2fb

File tree

5 files changed

+52
-47
lines changed

5 files changed

+52
-47
lines changed

src/mpvhandler.cpp

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,20 @@ void MpvHandler::LoadSettings(QSettings *settings, QString version)
148148
else if(parts[1] == "speed")
149149
Speed(settings->value(key).toDouble());
150150
else if(parts[1] == "screenshot-template")
151-
ScreenshotTemplate(settings->value(key).toString());
151+
{
152+
QString temp = settings->value(key).toString();
153+
int i = temp.lastIndexOf('/');
154+
if(i != -1)
155+
{
156+
ScreenshotDirectory(temp.mid(0, i));
157+
ScreenshotTemplate(temp.mid(i+1));
158+
}
159+
else
160+
{
161+
ScreenshotDirectory(".");
162+
ScreenshotTemplate(temp);
163+
}
164+
}
152165
else
153166
{
154167
QByteArray tmp1 = parts[1].toUtf8(),
@@ -167,14 +180,8 @@ void MpvHandler::LoadSettings(QSettings *settings, QString version)
167180
else
168181
setLastFile("");
169182
ScreenshotFormat(settings->value("mpv/screenshotFormat", "jpg").toString());
170-
QString dir = settings->value("mpv/screenshotDir", "").toString(),
171-
temp = settings->value("mpv/screenshotTemplate", "").toString();
172-
if(dir != "" && temp != "")
173-
ScreenshotTemplate(dir+"/"+temp);
174-
else if(dir != "")
175-
ScreenshotTemplate(dir+"/screenshot%#04n");
176-
else if(temp != "")
177-
ScreenshotTemplate(temp);
183+
ScreenshotDirectory(settings->value("mpv/screenshotDir", ".").toString());
184+
ScreenshotTemplate(settings->value("mpv/screenshotTemplate", "screenshot%#04n").toString());
178185
Speed(settings->value("mpv/speed", 1.0).toDouble());
179186
Volume(settings->value("mpv/volume", 100).toInt());
180187
Debug(settings->value("common/debug", false).toBool());
@@ -215,7 +222,7 @@ void MpvHandler::SaveSettings(QSettings *settings)
215222
if(screenshotFormat != "")
216223
settings->setValue("mpv/screenshot-format", screenshotFormat);
217224
if(screenshotTemplate != "")
218-
settings->setValue("mpv/screenshot-template", screenshotTemplate);
225+
settings->setValue("mpv/screenshot-template", screenshotDir+"/"+screenshotTemplate);
219226
}
220227
}
221228

@@ -457,12 +464,22 @@ void MpvHandler::ScreenshotTemplate(QString s)
457464
{
458465
if(mpv)
459466
{
460-
const QByteArray tmp = s.toUtf8();
467+
const QByteArray tmp = (screenshotDir+"/"+s).toUtf8();
461468
mpv_set_option_string(mpv, "screenshot-template", tmp.data());
462469
}
463470
setScreenshotTemplate(s);
464471
}
465472

473+
void MpvHandler::ScreenshotDirectory(QString s)
474+
{
475+
// if(mpv)
476+
// {
477+
// const QByteArray tmp = (s+"/"+screenshotTemplate).toUtf8();
478+
// mpv_set_option_string(mpv, "screenshot-template", tmp.data());
479+
// }
480+
setScreenshotDir(s);
481+
}
482+
466483
void MpvHandler::AddSubtitleTrack(QString f)
467484
{
468485
const QByteArray tmp = f.toUtf8();

src/mpvhandler.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class MpvHandler : public QObject
2424
QString getLastFile() { return lastFile; }
2525
QString getScreenshotFormat() { return screenshotFormat; }
2626
QString getScreenshotTemplate() { return screenshotTemplate; }
27+
QString getScreenshotDir() { return screenshotDir; }
2728
double getSpeed() { return speed; }
2829
int getTime() { return time; }
2930
int getVolume() { return volume; }
@@ -73,6 +74,7 @@ public slots:
7374

7475
void ScreenshotFormat(QString);
7576
void ScreenshotTemplate(QString);
77+
void ScreenshotDirectory(QString);
7678

7779
void AddSubtitleTrack(QString);
7880
void ShowSubtitles(bool);
@@ -103,6 +105,7 @@ private slots:
103105
void setLastFile(QString s) { emit lastFileChanged(lastFile = s); }
104106
void setScreenshotFormat(QString s) { emit screenshotFormatChanged(screenshotFormat = s); }
105107
void setScreenshotTemplate(QString s) { emit screenshotTemplateChanged(screenshotTemplate = s); }
108+
void setScreenshotDir(QString s) { emit screenshotDirChanged(screenshotDir = s); }
106109
void setSpeed(double d) { emit speedChanged(speed = d); }
107110
void setTime(int i) { emit timeChanged(time = i); }
108111
void setVolume(int i) { emit volumeChanged(volume = i); }
@@ -126,6 +129,7 @@ private slots:
126129
void lastFileChanged(QString);
127130
void screenshotFormatChanged(QString);
128131
void screenshotTemplateChanged(QString);
132+
void screenshotDirChanged(QString);
129133
void speedChanged(double);
130134
void timeChanged(int);
131135
void volumeChanged(int);
@@ -156,6 +160,7 @@ private slots:
156160
lastFile,
157161
screenshotFormat,
158162
screenshotTemplate,
163+
screenshotDir,
159164
suffix;
160165
double speed = 1;
161166
int time = 0,

src/ui/mainwindow.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,8 @@ void MainWindow::TakeScreenshot(bool subs)
16681668
if(screenshotDialog)
16691669
{
16701670
mpv->Pause();
1671-
subs = ScreenshotDialog::showScreenshotDialog(screenshotDialog, subs, mpv);
1671+
if(ScreenshotDialog::showScreenshotDialog(screenshotDialog, subs, mpv) != QDialog::Accepted)
1672+
return;
16721673
}
16731674
else
16741675
mpv->Screenshot(subs);
@@ -1677,20 +1678,9 @@ void MainWindow::TakeScreenshot(bool subs)
16771678

16781679
void MainWindow::ShowScreenshotMessage(bool subs)
16791680
{
1680-
QString dir = mpv->getScreenshotTemplate();
1681+
QString dir = mpv->getScreenshotDir();
16811682
int i = dir.lastIndexOf('/');
16821683
if(i != -1)
1683-
{
1684-
dir.truncate(i);
1685-
i = dir.lastIndexOf('/');
1686-
if(i != -1)
1687-
dir.remove(0, i+1);
1688-
}
1689-
else
1690-
{
1691-
dir = QApplication::applicationDirPath();
1692-
i = dir.lastIndexOf('/');
16931684
dir.remove(0, i+1);
1694-
}
16951685
mpv->ShowText("Saved to \""+dir+"\", "+(subs?"with":"without")+" subs");
16961686
}

src/ui/screenshotdialog.cpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
#include <QFileDialog>
55
#include <QString>
66

7-
ScreenshotDialog::ScreenshotDialog(bool &_always, bool screenshot, MpvHandler *mpv, QWidget *parent) :
7+
ScreenshotDialog::ScreenshotDialog(bool &_always, bool &_screenshot, MpvHandler *mpv, QWidget *parent) :
88
QDialog(parent),
99
ui(new Ui::ScreenshotDialog),
10-
always(_always)
10+
always(_always),
11+
screenshot(_screenshot)
1112
{
1213
ui->setupUi(this);
1314

@@ -16,18 +17,8 @@ ScreenshotDialog::ScreenshotDialog(bool &_always, bool screenshot, MpvHandler *m
1617
ui->templateEdit->setText(mpv->getScreenshotTemplate());
1718
ui->formatComboBox->setCurrentText(mpv->getScreenshotFormat());
1819

19-
QString screenshotTemplate = mpv->getScreenshotTemplate();
20-
int i = screenshotTemplate.lastIndexOf('/');
21-
if(i != -1)
22-
{
23-
ui->locationEdit->setText(screenshotTemplate.mid(0, i));
24-
ui->templateEdit->setText(screenshotTemplate.mid(i+1));
25-
}
26-
else
27-
{
28-
ui->templateEdit->setText(screenshotTemplate);
29-
ui->locationEdit->setText(".");
30-
}
20+
ui->locationEdit->setText(mpv->getScreenshotDir());
21+
ui->templateEdit->setText(mpv->getScreenshotTemplate());
3122

3223
connect(ui->browseButton, &QPushButton::clicked,
3324
[=]
@@ -41,9 +32,11 @@ ScreenshotDialog::ScreenshotDialog(bool &_always, bool screenshot, MpvHandler *m
4132
[=]
4233
{
4334
mpv->ScreenshotFormat(ui->formatComboBox->currentText());
44-
mpv->ScreenshotTemplate(ui->locationEdit->text()+"/"+ui->templateEdit->text());
35+
mpv->ScreenshotDirectory(ui->locationEdit->text());
36+
mpv->ScreenshotTemplate(ui->templateEdit->text());
4537
always = ui->showCheckBox->isChecked();
46-
mpv->Screenshot(ui->subtitlesCheckBox->isChecked());
38+
screenshot = ui->subtitlesCheckBox->isChecked();
39+
mpv->Screenshot(screenshot);
4740
accept();
4841
});
4942
}
@@ -53,9 +46,8 @@ ScreenshotDialog::~ScreenshotDialog()
5346
delete ui;
5447
}
5548

56-
bool ScreenshotDialog::showScreenshotDialog(bool &always, bool screenshot, MpvHandler *mpv, QWidget *parent)
49+
int ScreenshotDialog::showScreenshotDialog(bool &always, bool &screenshot, MpvHandler *mpv, QWidget *parent)
5750
{
5851
ScreenshotDialog dialog(always, screenshot, mpv, parent);
59-
dialog.exec();
60-
return dialog.ui->subtitlesCheckBox->isChecked();
52+
return dialog.exec();
6153
}

src/ui/screenshotdialog.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ class ScreenshotDialog : public QDialog
1414
{
1515
Q_OBJECT
1616

17-
public://ScreenshotDialog::showDialog(screenshotDialog, true, mpv);
18-
explicit ScreenshotDialog(bool &always, bool screenshot, MpvHandler *mpv, QWidget *parent = 0);
17+
public:
18+
explicit ScreenshotDialog(bool &always, bool &screenshot, MpvHandler *mpv, QWidget *parent = 0);
1919
~ScreenshotDialog();
2020

21-
static bool showScreenshotDialog(bool &always, bool screenshot, MpvHandler *mpv, QWidget *parent = 0);
21+
static int showScreenshotDialog(bool &always, bool &screenshot, MpvHandler *mpv, QWidget *parent = 0);
2222
private:
2323
Ui::ScreenshotDialog *ui;
24-
bool &always;
24+
bool &always,
25+
&screenshot;
2526
};
2627

2728
#endif // SCREENSHOTDIALOG_H

0 commit comments

Comments
 (0)