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 |
} |
13 |
|
14 |
void PictureView::paintEvent ( QPaintEvent* event) |
15 |
{ |
16 |
// Q_UNUSED(event); |
17 |
QPainter painter(this); |
18 |
painter.drawPixmap(0,0,image); |
19 |
painter.end(); |
20 |
} |
21 |
|
22 |
void PictureView::loadFromUrl(QString source) |
23 |
{ |
24 |
QMap<QString, QPixmap>::iterator it = map.find(source); |
25 |
if (it != map.end() ) { |
26 |
image = it.value(); |
27 |
this->repaint(); |
28 |
} else { |
29 |
requestedUrl = source; |
30 |
|
31 |
QUrl imageUrl(requestedUrl); |
32 |
http = new QHttp(imageUrl.host() ); |
33 |
http->connect(http, SIGNAL(requestFinished(int,bool)),this, SLOT(onLoad(int,bool)) ); |
34 |
|
35 |
http->get(imageUrl.path(), 0); |
36 |
} |
37 |
} |
38 |
|
39 |
void PictureView::onLoad(int id, bool ok) |
40 |
{ |
41 |
// Q_UNUSED(id); |
42 |
// Q_UNUSED(ok); |
43 |
|
44 |
QByteArray data = http->readAll(); |
45 |
|
46 |
QPixmap pixmap; |
47 |
pixmap.loadFromData( data ); |
48 |
delete http; |
49 |
|
50 |
QPixmap scaledImage = pixmap.scaledToHeight(size().height(), Qt::SmoothTransformation ); |
51 |
|
52 |
image = scaledImage; |
53 |
this->repaint(); |
54 |
|
55 |
map.insert(requestedUrl, scaledImage); |
56 |
|
57 |
} |