-
Notifications
You must be signed in to change notification settings - Fork 24
/
previewwidget.cpp
90 lines (83 loc) · 2.75 KB
/
previewwidget.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
89
90
#include "previewwidget.h"
#include <QPainter>
#include <QDebug>
#include <QSvgRenderer>
namespace {
const int ZOOM_BIG_INCREMENT = 200; // used when m_zoomScale > ZOOM_ORIGINAL_SCALE
const int ZOOM_SMALL_INCREMENT = 25; // used when m_zoomScale < ZOOM_ORIGINAL_SCALE
const int MAX_ZOOM_SCALE = 900;
const int MIN_ZOOM_SCALE = 25;
}
PreviewWidget::PreviewWidget(QWidget *parent)
: QWidget(parent)
, m_mode(NoMode)
, m_zoomScale(ZOOM_ORIGINAL_SCALE)
{
m_svgRenderer = new QSvgRenderer(this);
}
void PreviewWidget::load(const QByteArray &data)
{
if (m_mode == PngMode) {
m_image.loadFromData(data);
setMinimumSize(m_image.rect().size());
} else if (m_mode == SvgMode) {
m_svgRenderer->load(data);
}
zoomImage();
update();
}
void PreviewWidget::setZoomScale(int zoom_scale)
{
if (m_zoomScale != zoom_scale) {
m_zoomScale = zoom_scale;
zoomImage();
update();
}
}
void PreviewWidget::zoomIn()
{
int new_scale = m_zoomScale + ((m_zoomScale >= ZOOM_ORIGINAL_SCALE) ? ZOOM_BIG_INCREMENT : ZOOM_SMALL_INCREMENT);
if (new_scale > MAX_ZOOM_SCALE)
new_scale = MAX_ZOOM_SCALE;
setZoomScale(new_scale);
}
void PreviewWidget::zoomOut()
{
int new_scale = m_zoomScale - ((m_zoomScale <= ZOOM_ORIGINAL_SCALE) ? ZOOM_SMALL_INCREMENT : ZOOM_BIG_INCREMENT);
if (new_scale < MIN_ZOOM_SCALE)
new_scale = MIN_ZOOM_SCALE;
setZoomScale(new_scale);
}
void PreviewWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QSize output_size;
if (m_mode == PngMode) {
output_size = m_zoomedImage.size();
QRect output_rect(QPoint(), output_size);
output_rect.translate(rect().center() - output_rect.center());
painter.drawImage(output_rect.topLeft(), m_zoomedImage);
} else if (m_mode == SvgMode) {
output_size = m_svgRenderer->defaultSize();
if (m_zoomScale != ZOOM_ORIGINAL_SCALE) {
float zoom = float(m_zoomScale) / ZOOM_ORIGINAL_SCALE;
output_size.scale(output_size.width() * zoom, output_size.height() * zoom, Qt::IgnoreAspectRatio);
}
QRect output_rect(QPoint(), output_size);
output_rect.translate(rect().center() - output_rect.center());
m_svgRenderer->render(&painter, output_rect);
}
setMinimumSize(output_size);
}
void PreviewWidget::zoomImage()
{
if (m_mode == PngMode) {
if (m_zoomScale == ZOOM_ORIGINAL_SCALE) {
m_zoomedImage = m_image;
} else {
float zoom = float(m_zoomScale) / ZOOM_ORIGINAL_SCALE;
m_zoomedImage = m_image.scaled(m_image.width() * zoom, m_image.height() * zoom,
Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
}
}
}