1 |
#include "pictureview.h" |
#include "pictureview.h" |
2 |
|
|
3 |
#include <QGraphicsView> |
#include <QPainter> |
|
#include <QGraphicsScene> |
|
4 |
#include <QDebug> |
#include <QDebug> |
5 |
#include <QUrl> |
|
6 |
#include <QHttp> |
#include "httpwrapper.h" |
7 |
|
|
8 |
PictureView::PictureView(QWidget* parent) |
PictureView::PictureView(QWidget* parent) |
9 |
: QWidget(parent) |
: QWidget(parent) |
10 |
{ |
{ |
11 |
} |
} |
12 |
|
|
13 |
|
|
14 |
void PictureView::paintEvent ( QPaintEvent* event) |
void PictureView::paintEvent ( QPaintEvent* event) |
15 |
{ |
{ |
16 |
// Q_UNUSED(event); |
Q_UNUSED(event); |
17 |
|
|
18 |
|
QSize s = this->size(); |
19 |
|
|
20 |
|
QBrush blackBrush(Qt::black); |
21 |
|
|
22 |
QPainter painter(this); |
QPainter painter(this); |
23 |
painter.drawPixmap(0,0,image); |
painter.fillRect( QRect(0,0,s.width(), s.height()), blackBrush); |
24 |
|
|
25 |
|
int x = 0; |
26 |
|
int y = 0; |
27 |
|
|
28 |
|
if (image.width() < s.width() ) { |
29 |
|
x = (s.width() - image.width()) / 2; |
30 |
|
} |
31 |
|
if (image.height() < s.height() ) { |
32 |
|
y = (s.height() - image.height()) / 2; |
33 |
|
} |
34 |
|
|
35 |
|
|
36 |
|
painter.drawPixmap(x,y,image); |
37 |
painter.end(); |
painter.end(); |
38 |
} |
} |
39 |
|
|
44 |
image = it.value(); |
image = it.value(); |
45 |
this->repaint(); |
this->repaint(); |
46 |
} else { |
} else { |
47 |
requestedUrl = source; |
qDebug() << "Loading file " << source; |
48 |
|
|
49 |
QUrl imageUrl(requestedUrl); |
QPixmap pixmap; |
50 |
http = new QHttp(imageUrl.host() ); |
if (source.at(0) == '/') { //local file |
51 |
http->connect(http, SIGNAL(requestFinished(int,bool)),this, SLOT(onLoad(int,bool)) ); |
pixmap = QPixmap(source); |
52 |
|
} else { |
53 |
|
QByteArray data = HttpWrapper::getSyncData(source); |
54 |
|
pixmap.loadFromData( data ); |
55 |
|
} |
56 |
|
|
57 |
http->get(imageUrl.path(), 0); |
QPixmap scaledImage = pixmap.scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation); |
|
} |
|
|
} |
|
|
|
|
|
void PictureView::onLoad(int id, bool ok) |
|
|
{ |
|
|
// Q_UNUSED(id); |
|
|
// Q_UNUSED(ok); |
|
|
|
|
|
QByteArray data = http->readAll(); |
|
58 |
|
|
59 |
QPixmap pixmap; |
image = scaledImage; |
60 |
pixmap.loadFromData( data ); |
this->repaint(); |
|
delete http; |
|
|
|
|
|
QPixmap scaledImage = pixmap.scaledToHeight(size().height(), Qt::SmoothTransformation ); |
|
|
|
|
|
image = scaledImage; |
|
|
this->repaint(); |
|
61 |
|
|
62 |
map.insert(requestedUrl, scaledImage); |
map.insert(source, scaledImage); |
63 |
|
|
64 |
|
} |
65 |
} |
} |
66 |
|
|