forked from KDE/ktuberling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodraw.cpp
117 lines (97 loc) · 3.03 KB
/
todraw.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
114
115
116
117
/***************************************************************************
* Copyright (C) 1999-2006 by Éric Bischoff <[email protected]> *
* Copyright (C) 2007 by Albert Astals Cid <[email protected]> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
/* Object to draw on the game board */
#include "todraw.h"
#include <QDataStream>
#include <QPainter>
#include <QSvgRenderer>
static QImage toImage(const QString &element, int width, int height, QSvgRenderer *renderer)
{
QImage img(width, height, QImage::Format_ARGB32_Premultiplied);
img.fill(Qt::transparent);
QPainter p2(&img);
// don't need quality here
p2.setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing|QPainter::SmoothPixmapTransform, false);
renderer->render(&p2, element);
p2.end();
return img;
}
ToDraw::ToDraw()
: m_beingDragged(false)
{
}
// Load an object from a file
bool ToDraw::load(QDataStream &stream)
{
// NOTE: read error checking?
QPointF pos;
QString element;
qreal zOrder;
stream >> pos;
stream >> element;
stream >> zOrder;
setPos(pos);
setElementId(element);
setZValue(zOrder);
return true;
}
// Save an object to a file
void ToDraw::save(QDataStream &stream) const
{
stream << pos();
stream << elementId();
stream << zValue();
}
QRectF ToDraw::unclippedRect() const
{
return QGraphicsSvgItem::boundingRect();
}
QRectF ToDraw::clippedRectAt(const QPointF &somePos) const
{
if (m_beingDragged)
return unclippedRect();
QRectF backgroundRect = renderer()->boundsOnElement(QStringLiteral( "background" ));
backgroundRect.translate(-somePos);
backgroundRect = transform().inverted().map(backgroundRect).boundingRect();
return unclippedRect().intersected(backgroundRect);
}
void ToDraw::setBeingDragged(bool dragged)
{
prepareGeometryChange();
m_beingDragged = dragged;
}
QRectF ToDraw::boundingRect() const
{
return clippedRectAt(pos());
}
QVariant ToDraw::itemChange(GraphicsItemChange change, const QVariant& value)
{
if (change == QGraphicsItem::ItemPositionChange) {
if (boundingRect() != clippedRectAt(value.toPointF()))
prepareGeometryChange();
}
return QGraphicsSvgItem::itemChange(change, value);
}
bool ToDraw::contains(const QPointF &point) const
{
bool result = QGraphicsSvgItem::contains(point);
if (result)
{
QRectF bounds = transform().mapRect(unclippedRect());
const QImage &img = toImage(elementId(), qRound(bounds.width()), qRound(bounds.height()), renderer());
QPointF transformedPoint = transform().map(point);
result = qAlpha(img.pixel(transformedPoint.toPoint())) != 0;
}
return result;
}
int ToDraw::type() const
{
return Type;
}