-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_view.cpp
73 lines (56 loc) · 1.3 KB
/
image_view.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// QML QImage display
// Author: Max Schwarz <[email protected]>
#include "image_view.h"
#include <QPainter>
#include <QDebug>
ImageView::ImageView(QQuickItem* parent)
: QQuickPaintedItem(parent)
{
setAntialiasing(true);
}
ImageView::~ImageView()
{
}
void ImageView::setImage(const QImage& image)
{
m_image = image;
imageChanged();
recalculateImageRect(size());
update();
}
void ImageView::paint(QPainter* painter)
{
if(!m_image.isNull())
painter->drawImage(m_imageRect, m_image);
}
float ImageView::aspectRatio() const
{
if(m_image.isNull())
return 16.0 / 9.0;
return static_cast<float>(m_image.width()) / m_image.height();
}
void ImageView::recalculateImageRect(const QSizeF& outerSize)
{
float oW = outerSize.width();
float oH = outerSize.height();
if(m_image.isNull())
{
m_imageRect = QRectF(0, 0, oW, oH);
imageRectChanged();
return;
}
float scalingFactor = std::min(
oW / m_image.width(),
oH / m_image.height()
);
float imgW = scalingFactor * m_image.width();
float imgH = scalingFactor * m_image.height();
float offX = (oW - imgW) / 2;
float offY = (oH - imgH) / 2;
m_imageRect = QRectF(offX, offY, imgW, imgH);
imageRectChanged();
}
void ImageView::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry)
{
recalculateImageRect(newGeometry.size());
}