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 |
|
|
// TODO: if the picture doesn't fill the screen, fill the canvas with black before drawing the picture and |
15 |
|
|
// place the image mid-screen |
16 |
torben |
524 |
void PictureView::paintEvent ( QPaintEvent* event) |
17 |
|
|
{ |
18 |
torben |
525 |
Q_UNUSED(event); |
19 |
torben |
707 |
|
20 |
|
|
QSize s = this->size(); |
21 |
|
|
|
22 |
|
|
QBrush blackBrush(Qt::black); |
23 |
|
|
|
24 |
torben |
524 |
QPainter painter(this); |
25 |
torben |
707 |
painter.fillRect( QRect(0,0,s.width(), s.height()), blackBrush); |
26 |
|
|
|
27 |
|
|
int x = 0; |
28 |
|
|
int y = 0; |
29 |
|
|
|
30 |
|
|
if (image.width() < s.width() ) { |
31 |
|
|
x = (s.width() - image.width()) / 2; |
32 |
|
|
} |
33 |
|
|
if (image.height() < s.height() ) { |
34 |
|
|
y = (s.height() - image.height()) / 2; |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
|
38 |
|
|
painter.drawPixmap(x,y,image); |
39 |
torben |
524 |
painter.end(); |
40 |
|
|
} |
41 |
|
|
|
42 |
|
|
void PictureView::loadFromUrl(QString source) |
43 |
|
|
{ |
44 |
|
|
QMap<QString, QPixmap>::iterator it = map.find(source); |
45 |
|
|
if (it != map.end() ) { |
46 |
|
|
image = it.value(); |
47 |
|
|
this->repaint(); |
48 |
|
|
} else { |
49 |
torben |
639 |
qDebug() << "Loading file " << source; |
50 |
torben |
524 |
|
51 |
torben |
526 |
QPixmap pixmap; |
52 |
torben |
639 |
if (source.at(0) == '/') { //local file |
53 |
|
|
pixmap = QPixmap(source); |
54 |
|
|
} else { |
55 |
|
|
QByteArray data = HttpWrapper::getSyncData(source); |
56 |
|
|
pixmap.loadFromData( data ); |
57 |
|
|
} |
58 |
torben |
524 |
|
59 |
torben |
707 |
QPixmap scaledImage = pixmap.scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation); |
60 |
torben |
524 |
|
61 |
torben |
526 |
image = scaledImage; |
62 |
|
|
this->repaint(); |
63 |
torben |
524 |
|
64 |
torben |
528 |
map.insert(source, scaledImage); |
65 |
|
|
|
66 |
torben |
526 |
} |
67 |
torben |
524 |
} |