-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaintableView.cpp
98 lines (76 loc) · 2.31 KB
/
PaintableView.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
98
#include "PaintableView.h"
#include <QPainter>
#include <QPaintEvent>
PaintableView::PaintableView(QWidget *parent) :
QLabel(parent)
{
_paintable = false;
_rect = new QRect;
_x_ratio = 1.0f;
_y_ratio = 1.0f;
setFixedSize(800, 600);
}
void PaintableView::setPaintingRectLeftTop(uint32_t x, uint32_t y)
{
_rect->setCoords(0, -1, 0, -1);
_rect->setTopLeft(QPoint(x, y));
}
void PaintableView::setPaintingRectRightBottom(uint32_t x, uint32_t y)
{
_rect->setBottomRight(QPoint(x, y));
}
void PaintableView::setPaintable(bool paintable)
{
_paintable = paintable;
}
void PaintableView::paintEvent(QPaintEvent *event)
{
const QPixmap* raw_bg = pixmap();
if( raw_bg != NULL)
{
QPainter widget_painter(this);
_x_ratio = (float)width()/(float)raw_bg->width();
_y_ratio = (float)height()/(float)raw_bg->height();
QPixmap scale_bg = raw_bg->scaled(size());
QPixmap mask = QPixmap(scale_bg.width(), scale_bg.height());
QPainter pixmap_painter(&scale_bg);
QPainter mask_painter(&mask);
mask_painter.setRenderHint(QPainter::Antialiasing);
pixmap_painter.setRenderHint(QPainter::Antialiasing);
widget_painter.setRenderHint(QPainter::Antialiasing);
if( _paintable && _rect->isValid() && _rect->width() > 1 && _rect->height() > 1 )
{
mask_painter.fillRect(QRect(0, 0, scale_bg.width(), scale_bg.height()), QBrush(QColor(128, 128, 128, 128)));
mask_painter.eraseRect(*_rect);
pixmap_painter.setCompositionMode(QPainter::CompositionMode_Multiply);
pixmap_painter.drawPixmap(0, 0, mask);
}
widget_painter.drawPixmap(0, 0, scale_bg);
}
}
void PaintableView::setPaintingRect(QRect rect)
{
*_rect = rect;
}
QRect PaintableView::getPaintingRect()
{
return *_rect;
}
QRect PaintableView::getLogicalRect()
{
QRect rect;
if( _rect->isValid() && _rect->width() > 1 && _rect->height() > 1 )
{
rect.setTopLeft(QPoint( (float)_rect->left() / _x_ratio, (float)_rect->top() / _y_ratio ));
rect.setBottomRight(QPoint( (float)_rect->right() / _x_ratio, (float)_rect->bottom() / _y_ratio));
}
return rect;
}
PaintableView::~PaintableView()
{
if(_rect != NULL)
{
delete _rect;
_rect = NULL;
}
}