This repository was archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy pathprogressslideshowdialog.cpp
205 lines (176 loc) · 5.83 KB
/
progressslideshowdialog.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
#include "progressslideshowdialog.h"
#include "ui_progressslideshowdialog.h"
#include "util.h"
#include <limits.h>
#include <QDir>
#include <QFile>
#include <QPixmap>
#include <QDesktopWidget>
#include <QDebug>
/* Progress dialog with slideshow
*
* Initial author: Floris Bos
* Maintained by Raspberry Pi
*
* See LICENSE.txt for license details
*
*/
ProgressSlideshowDialog::ProgressSlideshowDialog(const QStringList &slidesDirectories, const QString &statusMsg, int changeInterval, const QString &drive, QWidget *parent) :
QDialog(parent),
_drive(drive),
_pos(0),
_changeInterval(changeInterval),
_maxSectors(0),
_pausedAt(0),
ui(new Ui::ProgressSlideshowDialog)
{
ui->setupUi(this);
setLabelText(statusMsg);
QRect s = QApplication::desktop()->screenGeometry();
if (s.height() < 400)
setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
else
setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
foreach (QString slidesDirectory, slidesDirectories)
{
QDir dir(slidesDirectory, "*.jpg *.jpeg *.png");
if (dir.exists())
{
QStringList s = dir.entryList();
s.sort();
foreach (QString slide, s)
{
_slides.append(slidesDirectory+"/"+slide);
}
}
}
qDebug() << "Available slides" << _slides;
if (_slides.isEmpty())
{
/* Resize window to just show progress bar */
ui->imagespace->setMinimumSize(0, 0);
resize(this->width(), 50);
}
else
{
/* Resize window to size of first image in slide directory */
QPixmap pixmap(_slides.first());
ui->imagespace->setMinimumSize(pixmap.width(), pixmap.height());
resize(pixmap.width(), pixmap.height()+50);
ui->imagespace->setPixmap(pixmap);
connect(&_timer, SIGNAL(timeout()), this, SLOT(nextSlide()));
_timer.start(changeInterval * 1000);
}
connect(&_iotimer, SIGNAL(timeout()), this, SLOT(updateIOstats()));
enableIOaccounting();
}
ProgressSlideshowDialog::~ProgressSlideshowDialog()
{
delete ui;
}
void ProgressSlideshowDialog::reject()
{
/* prevent closing. */
}
void ProgressSlideshowDialog::setLabelText(const QString &text)
{
QString txt = text;
txt.replace('\n',' ');
ui->statusLabel->setText(txt);
qDebug() << text;
}
void ProgressSlideshowDialog::nextSlide()
{
if (++_pos >= _slides.size())
_pos = 0;
QString newSlide = _slides.at(_pos);
if (QFile::exists(newSlide))
ui->imagespace->setPixmap(QPixmap(newSlide));
}
/* IO accounting functionality for analyzing SD card write speed / showing progress */
void ProgressSlideshowDialog::enableIOaccounting()
{
_sectorsStart = sectorsWritten();
_t1.start();
_iotimer.start(1000);
}
void ProgressSlideshowDialog::disableIOaccounting()
{
_iotimer.stop();
ui->mbwrittenLabel->setText("");
}
void ProgressSlideshowDialog::pauseIOaccounting()
{
_iotimer.stop();
_pausedAt = sectorsWritten();
}
void ProgressSlideshowDialog::resumeIOaccounting()
{
_sectorsStart += sectorsWritten()-_pausedAt;
_iotimer.start(1000);
}
void ProgressSlideshowDialog::changeDrive(const QString &drive)
{
pauseIOaccounting();
_drive = drive;
resumeIOaccounting();
}
void ProgressSlideshowDialog::setMaximum(qint64 bytes)
{
/* restrict to size of 1TB since the progressbar expects an int32 */
/* to prevent overflow */
if (bytes > 1099511627775LL) /* == 2147483648 * 512 -1*/
bytes = 1099511627775LL;
_maxSectors = bytes/512;
ui->progressBar->setMaximum(_maxSectors);
}
void ProgressSlideshowDialog::updateIOstats()
{
uint sectors = sectorsWritten()-_sectorsStart;
double sectorsPerSec = sectors * 1000.0 / _t1.elapsed();
if (_maxSectors)
{
sectors = qMin(_maxSectors, sectors);
ui->progressBar->setValue(sectors);
ui->mbwrittenLabel->setText(tr("%1 MB of %2 MB written (%3 MB/sec)")
.arg(QString::number(sectors/2048), QString::number(_maxSectors/2048), QString::number(sectorsPerSec/2048.0, 'f', 1)));
}
else
{
ui->mbwrittenLabel->setText(tr("%1 MB written (%2 MB/sec)")
.arg(QString::number(sectors/2048), QString::number(sectorsPerSec/2048.0, 'f', 1)));
}
}
uint ProgressSlideshowDialog::sectorsWritten()
{
/* Poll kernel counters to get number of bytes written
*
* Fields available in /sys/block/<DEVICE>/stat
* (taken from https://www.kernel.org/doc/Documentation/block/stat.txt )
*
* Name units description
* ---- ----- -----------
* read I/Os requests number of read I/Os processed
* read merges requests number of read I/Os merged with in-queue I/O
* read sectors sectors number of sectors read
* read ticks milliseconds total wait time for read requests
* write I/Os requests number of write I/Os processed
* write merges requests number of write I/Os merged with in-queue I/O
* write sectors sectors number of sectors written
* write ticks milliseconds total wait time for write requests
* in_flight requests number of I/Os currently in flight
* io_ticks milliseconds total time this block device has been active
* time_in_queue milliseconds total wait time for all requests
*/
uint numsectors=0;
QFile f(sysclassblock(_drive)+"/stat");
f.open(f.ReadOnly);
QByteArray ioline = f.readAll().simplified();
f.close();
QList<QByteArray> stats = ioline.split(' ');
if (stats.count() >= 6)
numsectors = stats.at(6).toUInt(); /* write sectors */
if (numsectors > INT_MAX)
numsectors = INT_MAX;
return numsectors;
}