This repository was archived by the owner on Sep 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautosave.cpp
More file actions
95 lines (78 loc) · 2.33 KB
/
autosave.cpp
File metadata and controls
95 lines (78 loc) · 2.33 KB
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
/****************************************************************************
**
** Copyright (C) 2012 Felix Engelmann, Simeon Voelkel
** Contact: felix.engelmann@sfz-bw.de, simeon.voelkel@sfz-bw.de
**
** $QT_BEGIN_LICENSE:GPL$
** GNU General Public License Usage
** This file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QFile>
#include <QtConcurrentRun>
#include "autosave.h"
#include "listcontroller.h"
#include "themeclock.h"
#include "mainwindow.h"
void writeToDisk(QString dest, int step, int time)
{
QFile file(dest);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;
QTextStream out(&file);
out << step << "\n" << time << "\n";
file.close();
}
AutoSave::AutoSave(MainWindow *mw, QString dest, QObject *parent) :
QObject(parent)
{
this->mw = mw;
this->dest = dest;
this->lastsavedtime = 0;
saveJob = QFuture<void>();
}
void AutoSave::save()
{
int current = mw->lc->getCurrentIndex();
int time = mw->thc->getElapsedTime();
if ( time/1000 > 15 ){ // only save times larger than 20s (this allows to revert a double-skip)
lastsavedtime = mw->thc->getElapsedTime();
}
saveJob = QtConcurrent::run(writeToDisk, dest, current, time);
}
void AutoSave::load()
{
QFile file(dest);
if (!file.exists()) return;
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
QTextStream in(&file);
QString line;
int step=0, time=0;
if (in.atEnd()) {
file.close();
return;
}
line = in.readLine();
step = line.toInt();
if (in.atEnd()) {
file.close();
return;
}
line = in.readLine();
time = line.toInt();
file.close();
if (step != mw->lc->getMaxIndex()){ // load autosave only if last stage wasn't finished
mw->lc->setCurrentIndex(step);
mw->thc->setElapsedTime(time);
}
}
int AutoSave::getLastSavedTime()
{
return lastsavedtime;
}