1 |
#include "pictureview.h" |
2 |
|
3 |
#include <QPainter> |
4 |
#include <QDebug> |
5 |
|
6 |
#include "httpwrapper.h" |
7 |
|
8 |
PictureView::PictureView(QWidget* parent) |
9 |
: QWidget(parent) |
10 |
{ |
11 |
} |
12 |
|
13 |
|
14 |
void PictureView::paintEvent ( QPaintEvent* event) |
15 |
{ |
16 |
Q_UNUSED(event); |
17 |
|
18 |
QSize s = this->size(); |
19 |
|
20 |
QBrush blackBrush(Qt::black); |
21 |
|
22 |
QPainter painter(this); |
23 |
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(); |
38 |
} |
39 |
|
40 |
void PictureView::loadFromUrl(QString source) |
41 |
{ |
42 |
QMap<QString, QPixmap>::iterator it = map.find(source); |
43 |
if (it != map.end() ) { |
44 |
image = it.value(); |
45 |
this->repaint(); |
46 |
} else { |
47 |
qDebug() << "Loading file " << source; |
48 |
|
49 |
QPixmap pixmap; |
50 |
if (source.at(0) == '/') { //local file |
51 |
pixmap = QPixmap(source); |
52 |
} else { |
53 |
QByteArray data = HttpWrapper::getSyncData(source); |
54 |
pixmap.loadFromData( data ); |
55 |
} |
56 |
|
57 |
QPixmap scaledImage = pixmap.scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation); |
58 |
|
59 |
image = scaledImage; |
60 |
this->repaint(); |
61 |
|
62 |
map.insert(source, scaledImage); |
63 |
|
64 |
} |
65 |
} |