-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathimageviewerbase.cpp
88 lines (65 loc) · 1.93 KB
/
imageviewerbase.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <QtWidgets>
#include <QtGui>
#include <QtCore>
#include "imageviewerbase.h"
ImageViewerBase::ImageViewerBase(QWidget *parent)
: QWidget(parent)
, m_zoom(1)
{
m_timerDelayedUpdate = new QTimer(this);
m_timerDelayedUpdate->setSingleShot(true);
m_timerDelayedUpdate->setInterval(10);
connect(m_timerDelayedUpdate, SIGNAL(timeout()), this, SLOT(updatePixmap()));
setMinimumSize(200, 120);
}
void ImageViewerBase::setImage(const QImage &image)
{
m_timerDelayedUpdate->stop();
m_image = image;
updatePixmap();
}
void ImageViewerBase::reset()
{
m_image = QImage();
m_pixmap = QPixmap();
m_pixmapBoundingRect = QRect();
m_zoom = 1;
update();
}
void ImageViewerBase::resizeEvent(QResizeEvent *event)
{
Q_UNUSED(event)
if(!m_timerDelayedUpdate->isActive())
m_timerDelayedUpdate->start();
}
void ImageViewerBase::paintEvent(QPaintEvent *event)
{
if(m_pixmap.isNull())
return;
QPainter painter(this);
painter.setClipRegion(event->region());
painter.setRenderHint(QPainter::Antialiasing);
// car image
painter.drawPixmap(m_pixmapBoundingRect.x(), m_pixmapBoundingRect.y(), m_pixmap);
// custom paint in subclasses
draw(&painter);
}
void ImageViewerBase::updatePixmap()
{
pixmapAboutToBeChanged();
m_pixmap = QPixmap();
if(m_image.isNull())
return;
// scale with a higher quality
m_pixmap = QPixmap::fromImage(m_image.scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
// calculate scaling
m_zoom = static_cast<double>(m_pixmap.width()) / m_image.width();
// pixmap bounding rectangle
m_pixmapBoundingRect = QRect((width() - m_pixmap.width()) / 2,
(height() - m_pixmap.height()) / 2,
m_pixmap.width(),
m_pixmap.height());
pixmapChanged();
emit zoomChanged(m_zoom);
update();
}