-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathimagewidget.cpp
97 lines (84 loc) · 1.94 KB
/
imagewidget.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
91
92
93
94
95
96
97
#include "imagewidget.h"
#include <math.h>
namespace tvseg_ui {
ImageWidget::ImageWidget(QWidget *parent) :
QLabel(parent),
scaling_(true)
{
setMinimumSize(1,1);
setAlignment(Qt::AlignLeft | Qt::AlignTop);
//setStyleSheet("QLabel { background-color : white}");
}
bool ImageWidget::scaling()
{
return scaling_;
}
void ImageWidget::resetSize()
{
if (!pixmap_.isNull()) {
resize(pixmap_.size());
}
}
QSize ImageWidget::imageSize()
{
if (!pixmap_.isNull()) {
return pixmap_.size();
} else {
return QSize(0,0);
}
}
double ImageWidget::scaleFactor()
{
QSize size = imageSize();
if (size.width() <= 0 || size.height() <= 0) {
return 1;
}
int w = width();
int h = height();
QSize scale = size.scaled(w, h, Qt::KeepAspectRatio);
return ((double)scale.width()) / size.width();
}
QPoint ImageWidget::imagePosFromWidgetPos(QPoint widgetPos)
{
QPoint pos(floor(widgetPos.x() / scaleFactor()),
floor(widgetPos.y() / scaleFactor()));
QSize size = imageSize();
if (pos.x() >= size.width() || pos.y() >= size.height() || pos.x() < 0 || pos.y() < 0) {
return QPoint(-1,-1);
} else {
return pos;
}
}
void ImageWidget::setPixmap(const QPixmap &p)
{
pixmap_ = p;
updatePixmap();
}
void ImageWidget::setScaling(bool scaling)
{
scaling_ = scaling;
updatePixmap();
}
void ImageWidget::resizeEvent(QResizeEvent *event)
{
QLabel::resizeEvent(event);
updatePixmap();
}
void ImageWidget::updatePixmap()
{
if (!pixmap_.isNull())
{
if (scaling_) {
int w = width();
int h = height();
QLabel::setPixmap(pixmap_.scaled(w, h, Qt::KeepAspectRatio));
} else {
QLabel::setPixmap(pixmap_);
}
} else {
if (QLabel::pixmap() && !QLabel::pixmap()->isNull()) {
QLabel::setPixmap(pixmap_);
}
}
}
} // namespace tvseg_ui