1 | /*************************************************************************** |
2 | * Copyright (C) 1999-2006 by Éric Bischoff <ebischoff@nerim.net> * |
3 | * Copyright (C) 2007 by Albert Astals Cid <aacid@kde.org> * |
4 | * * |
5 | * This program is free software; you can redistribute it and/or modify * |
6 | * it under the terms of the GNU General Public License as published by * |
7 | * the Free Software Foundation; either version 2 of the License, or * |
8 | * (at your option) any later version. * |
9 | ***************************************************************************/ |
10 | |
11 | /* Object to draw on the game board */ |
12 | |
13 | #include "todraw.h" |
14 | |
15 | #include <QDataStream> |
16 | #include <QPainter> |
17 | #include <QSvgRenderer> |
18 | |
19 | #include <kdeversion.h> |
20 | |
21 | static QImage toImage(const QString &element, int width, int height, QSvgRenderer *renderer) |
22 | { |
23 | QImage img(width, height, QImage::Format_ARGB32_Premultiplied); |
24 | img.fill(Qt::transparent); |
25 | QPainter p2(&img); |
26 | // don't need quality here |
27 | p2.setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing|QPainter::SmoothPixmapTransform, false); |
28 | renderer->render(&p2, element); |
29 | p2.end(); |
30 | return img; |
31 | } |
32 | |
33 | QPixmap toPixmap(const QString &element, int width, int height, QSvgRenderer *renderer) |
34 | { |
35 | QPixmap pix(width, height); |
36 | pix.fill(Qt::transparent); |
37 | QPainter p2(&pix); |
38 | renderer->render(&p2, element); |
39 | p2.end(); |
40 | return pix; |
41 | } |
42 | |
43 | ToDraw::ToDraw() |
44 | { |
45 | } |
46 | |
47 | // Load an object from a file |
48 | bool ToDraw::load(QDataStream &stream) |
49 | { |
50 | // NOTE: read error checking? |
51 | QPointF pos; |
52 | QString element; |
53 | qreal zOrder; |
54 | |
55 | stream >> pos; |
56 | stream >> element; |
57 | stream >> zOrder; |
58 | |
59 | setPos(pos); |
60 | setElementId(element); |
61 | setZValue(zOrder); |
62 | |
63 | return true; |
64 | } |
65 | |
66 | // Save an object to a file |
67 | void ToDraw::save(QDataStream &stream) const |
68 | { |
69 | stream << pos(); |
70 | stream << elementId(); |
71 | stream << zValue(); |
72 | } |
73 | |
74 | QRectF ToDraw::unclippedRect() const |
75 | { |
76 | return QGraphicsSvgItem::boundingRect(); |
77 | } |
78 | |
79 | QRectF ToDraw::clippedRectAt(const QPointF &somePos) const |
80 | { |
81 | QRectF backgroundRect = renderer()->boundsOnElement(QLatin1String( "background" )); |
82 | backgroundRect.translate(-somePos); |
83 | backgroundRect = transform().inverted().map(backgroundRect).boundingRect(); |
84 | |
85 | return unclippedRect().intersected(backgroundRect); |
86 | } |
87 | |
88 | QRectF ToDraw::boundingRect() const |
89 | { |
90 | return clippedRectAt(pos()); |
91 | } |
92 | |
93 | QVariant ToDraw::itemChange(GraphicsItemChange change, const QVariant& value) |
94 | { |
95 | if (change == QGraphicsItem::ItemPositionChange) { |
96 | if (boundingRect() != clippedRectAt(value.toPointF())) |
97 | prepareGeometryChange(); |
98 | } |
99 | return QGraphicsSvgItem::itemChange(change, value); |
100 | } |
101 | |
102 | bool ToDraw::contains(const QPointF &point) const |
103 | { |
104 | bool result = QGraphicsSvgItem::contains(point); |
105 | if (result) |
106 | { |
107 | QRectF bounds = transform().mapRect(unclippedRect()); |
108 | const QImage &img = toImage(elementId(), qRound(bounds.width()), qRound(bounds.height()), renderer()); |
109 | QPointF transformedPoint = transform().map(point); |
110 | result = qAlpha(img.pixel(transformedPoint.toPoint())) != 0; |
111 | } |
112 | return result; |
113 | } |
114 | |
115 | int ToDraw::type() const |
116 | { |
117 | return Type; |
118 | } |
119 | |