-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphicsplaneitem.cpp
113 lines (95 loc) · 3.56 KB
/
graphicsplaneitem.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
* Joshua Henderson <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "graphicsplaneitem.h"
#include <planes/plane.h>
#include <QPainter>
#include <QDebug>
#include <QEvent>
#include <QGraphicsSceneMouseEvent>
#include <QStyleOptionGraphicsItem>
GraphicsPlaneItem::GraphicsPlaneItem(struct plane_data* plane, const QRectF& bounding)
: m_bounding(bounding),
m_plane(plane)
{
if (!plane)
qFatal("invalid plane pointer");
/*
* This is not a solid solution, but this prevents painting in most cases. We don't need
* to paint a plane unless the plane contents itself changes. This means, we don't need
* to paint when the item is moved, scaled, etc.
*/
setCacheMode(QGraphicsItem::DeviceCoordinateCache);
/*
* QGraphicsItem::ItemSendsGeometryChanges is how we get ItemPositionChange,
* ItemPositionHasChanged, ItemMatrixChange, ItemTransformChange, ItemTransformHasChanged,
* ItemRotationChange, ItemRotationHasChanged, ItemScaleChange, ItemScaleHasChanged,
* ItemTransformOriginPointChange, and ItemTransformOriginPointHasChanged to itemChange().
*
* QGraphicsItem::ItemHasNoContents is the magic that prevents paint calls from the view/scene.
*/
setFlags(QGraphicsItem::ItemSendsGeometryChanges |
QGraphicsItem::ItemClipsToShape);
moveEvent(pos());
}
QVariant GraphicsPlaneItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
qDebug() << "GraphicsPlaneItem::itemChange " << change;
if (change == GraphicsItemChange::ItemPositionChange)
{
moveEvent(value.toPoint());
return value;
}
else if (change == GraphicsItemChange::ItemPositionHasChanged)
{
moveEvent(value.toPoint());
return value;
}
else if (change == GraphicsItemChange::ItemScaleChange)
{
// TODO: validate scale
}
else if (change == GraphicsItemChange::ItemScaleHasChanged)
{
qDebug() << "scale " << value.toFloat();
plane_set_scale(m_plane, value.toFloat());
plane_apply(m_plane);
}
return QGraphicsItem::itemChange(change, value);
}
void GraphicsPlaneItem::moveEvent(const QPointF& point)
{
qDebug() << "GraphicsPlaneItem::moveEvent " << point;
plane_set_pos(m_plane, point.x(), point.y());
plane_apply(m_plane);
}
void GraphicsPlaneItem::draw(struct plane_data* plane, QImage image, bool horizontal, bool vertical, bool scale)
{
if ((int)plane_width(plane) != image.width() || (int)plane_height(plane) != image.height())
plane_fb_reallocate(plane, image.width(), image.height(), plane_format(plane));
plane_fb_map(plane);
QImage fb(static_cast<uchar*>(plane->bufs[0]),
plane_width(plane), plane_height(plane),
QImage::Format_ARGB32_Premultiplied);
QPainter painter(&fb);
painter.setTransform(transform());
painter.setCompositionMode(QPainter::CompositionMode_Source);
QImage transformedImage(image);
if (scale)
{
QSize imageSize = image.size();
imageSize.scale(QSize(plane_width(plane), plane_height(plane)), Qt::KeepAspectRatio);
transformedImage = image.scaled(imageSize,
Qt::KeepAspectRatio,
Qt::SmoothTransformation);
}
if (horizontal || vertical)
{
transformedImage = transformedImage.mirrored(horizontal, vertical);
}
painter.drawImage(QPoint(0,0), transformedImage);
painter.end();
}