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