-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimageview.cpp
More file actions
114 lines (104 loc) · 3.17 KB
/
imageview.cpp
File metadata and controls
114 lines (104 loc) · 3.17 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "imageview.h"
#include <QImageReader>
#include <QPainter>
#include <QDebug>
ImageView::ImageView(QNetworkReply* data, QWidget *parent) : QWidget(parent) {
imageData = data;
frameIndex = percent = 0;
frameTimer.setSingleShot(true);
errored = false;
drawHuge = false;
setMinimumSize(DEFAULT_SIZE_X, DEFAULT_SIZE_Y);
data->setParent(this);
_width = width();
_height = height();
connect(data, SIGNAL(finished()), this, SLOT(recievedData()));
connect(data, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(progress(qint64,qint64)));
connect(data, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(downloadError(QNetworkReply::NetworkError)));
}
void ImageView::recievedData() {
QImageReader imageReader(imageData);
if(imageData->error())
goto errorcleanup;
while(imageReader.canRead()) {
ImageFrame* frame = new ImageFrame;
frame->pixelsNom = QPixmap::fromImageReader(&imageReader);
if(frame->pixelsNom.size() == QSize(0, 0))
goto errorcleanup;
frame->thumbnail = frame->pixelsNom.scaled(QSize(DEFAULT_SIZE_X, DEFAULT_SIZE_Y), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
if(frame->thumbnail.size() == QSize(0, 0))
goto errorcleanup;
frame->frameDelay = imageReader.nextImageDelay();
frames.append(frame);
}
if(frames.size() > 1) {
frameTimer.start(frames.at(0)->frameDelay);
connect(&frameTimer, SIGNAL(timeout()), this, SLOT(nextFrame()));
}
repaint();
goto cleanup;
errorcleanup:
while(!frames.isEmpty())
delete frames.takeFirst();
errored = true;
cleanup:
imageData->deleteLater();
}
void ImageView::nextFrame() {
frameTimer.start(frames.at(++frameIndex >= frames.size() ? (frameIndex = 0) : frameIndex)->frameDelay);
repaint();
}
void ImageView::paintEvent(QPaintEvent*) {
QPainter p(this);
if(errored) {
p.setPen(Qt::darkRed);
p.drawText(QRect(0, 0, width(), height()), Qt::AlignCenter | Qt::AlignHCenter, "Error Downloading.");
} else {
p.setPen(Qt::darkBlue);
if(frames.isEmpty()) {
p.drawText(QRect(0, 0, width(), height()), Qt::AlignCenter | Qt::AlignHCenter, QString("Downloading\n%1%").arg((int)(percent * 100.0)));
} else {
QPixmap image;
if(drawHuge)
image = frames.at(frameIndex)->pixelsNom;
else
image = frames.at(frameIndex)->thumbnail;
p.drawPixmap(0, 0, width(), height(), image);
}
}
p.setPen(Qt::gray);
p.drawRect(QRect(0, 0, width() - 1, height() - 1));
}
void ImageView::progress(qint64 cur, qint64 tot) {
if(tot == 0) {
errored = true;
repaint();
return;
}
percent = (double)cur / (double)tot;
repaint();
}
void ImageView::mouseReleaseEvent(QMouseEvent*) {
if(!frames.isEmpty() && !drawHuge) {
drawHuge = true;
resize(frames.at(frameIndex)->pixelsNom.size());
} else {
drawHuge = false;
resize(DEFAULT_SIZE_X, DEFAULT_SIZE_Y);
}
raise();
repaint();
}
void ImageView::moveAnimated(int x, int y){
_x = x;
_y = y;
move(x, y); //FIXME: temporary replacement of a previous library usage.
}
void ImageView::downloadError(QNetworkReply::NetworkError qNE) {
qDebug() << QString("%1 failed to load. Error # %2").arg(imageData->url().toString()).arg(qNE);
errored = true;
}
ImageView::~ImageView(){ // cleanup resources
foreach(ImageFrame* frame, frames)
delete frame;
}