1 |
#include "pictureview.h" |
2 |
|
3 |
#include <QGraphicsView> |
4 |
#include <QGraphicsScene> |
5 |
#include <QDebug> |
6 |
#include <QUrl> |
7 |
#include <QHttp> |
8 |
|
9 |
PictureView::PictureView(QWidget* parent) |
10 |
: QWidget(parent) |
11 |
{ |
12 |
http.connect(&http, SIGNAL(requestFinished(int,bool)),this, SLOT(onLoad(int,bool)) ); |
13 |
} |
14 |
|
15 |
void PictureView::paintEvent ( QPaintEvent* event) |
16 |
{ |
17 |
Q_UNUSED(event); |
18 |
QPainter painter(this); |
19 |
painter.drawPixmap(0,0,image); |
20 |
painter.end(); |
21 |
} |
22 |
|
23 |
void PictureView::loadFromUrl(QString source) |
24 |
{ |
25 |
QMap<QString, QPixmap>::iterator it = map.find(source); |
26 |
if (it != map.end() ) { |
27 |
image = it.value(); |
28 |
this->repaint(); |
29 |
} else { |
30 |
requestedUrl = source; |
31 |
|
32 |
QUrl url(requestedUrl); |
33 |
http.setHost(url.host() ); |
34 |
http.get(url.path(), 0); |
35 |
} |
36 |
} |
37 |
|
38 |
void PictureView::onLoad(int id, bool ok) |
39 |
{ |
40 |
Q_UNUSED(id); |
41 |
Q_UNUSED(ok); |
42 |
qint64 byteCount = http.bytesAvailable(); |
43 |
|
44 |
if (byteCount > 0) { |
45 |
|
46 |
QByteArray data = http.readAll(); |
47 |
|
48 |
QPixmap pixmap; |
49 |
pixmap.loadFromData( data ); |
50 |
|
51 |
QPixmap scaledImage = pixmap.scaledToHeight(size().height(), Qt::SmoothTransformation ); |
52 |
|
53 |
image = scaledImage; |
54 |
this->repaint(); |
55 |
|
56 |
map.insert(requestedUrl, scaledImage); |
57 |
} |
58 |
} |