File indexing completed on 2024-09-08 06:49:28
0001 /* 0002 SPDX-FileCopyrightText: 1999-2006 Éric Bischoff <ebischoff@nerim.net> 0003 SPDX-FileCopyrightText: 2007 Albert Astals Cid <aacid@kde.org> 0004 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 /* Object to draw on the game board */ 0009 0010 #include "todraw.h" 0011 0012 #include <QDataStream> 0013 #include <QPainter> 0014 #include <QSvgRenderer> 0015 0016 static QImage toImage(const QString &element, int width, int height, QSvgRenderer *renderer) 0017 { 0018 QImage img(width, height, QImage::Format_ARGB32_Premultiplied); 0019 img.fill(Qt::transparent); 0020 QPainter p2(&img); 0021 // don't need quality here 0022 p2.setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing|QPainter::SmoothPixmapTransform, false); 0023 renderer->render(&p2, element); 0024 p2.end(); 0025 return img; 0026 } 0027 0028 0029 ToDraw::ToDraw() 0030 : m_beingDragged(false) 0031 { 0032 } 0033 0034 // Load an object from a file 0035 bool ToDraw::load(QDataStream &stream) 0036 { 0037 // NOTE: read error checking? 0038 QPointF pos; 0039 QString element; 0040 qreal zOrder; 0041 0042 stream >> pos; 0043 stream >> element; 0044 stream >> zOrder; 0045 0046 setPos(pos); 0047 setElementId(element); 0048 setZValue(zOrder); 0049 0050 return true; 0051 } 0052 0053 // Save an object to a file 0054 void ToDraw::save(QDataStream &stream) const 0055 { 0056 stream << pos(); 0057 stream << elementId(); 0058 stream << zValue(); 0059 } 0060 0061 QRectF ToDraw::unclippedRect() const 0062 { 0063 return QGraphicsSvgItem::boundingRect(); 0064 } 0065 0066 QRectF ToDraw::clippedRectAt(const QPointF &somePos) const 0067 { 0068 if (m_beingDragged) 0069 return unclippedRect(); 0070 0071 QRectF backgroundRect = renderer()->boundsOnElement(QStringLiteral( "background" )); 0072 backgroundRect.translate(-somePos); 0073 backgroundRect = transform().inverted().map(backgroundRect).boundingRect(); 0074 0075 return unclippedRect().intersected(backgroundRect); 0076 } 0077 0078 void ToDraw::setBeingDragged(bool dragged) 0079 { 0080 prepareGeometryChange(); 0081 m_beingDragged = dragged; 0082 } 0083 0084 QRectF ToDraw::boundingRect() const 0085 { 0086 return clippedRectAt(pos()); 0087 } 0088 0089 QVariant ToDraw::itemChange(GraphicsItemChange change, const QVariant& value) 0090 { 0091 if (change == QGraphicsItem::ItemPositionChange) { 0092 if (boundingRect() != clippedRectAt(value.toPointF())) 0093 prepareGeometryChange(); 0094 } 0095 return QGraphicsSvgItem::itemChange(change, value); 0096 } 0097 0098 bool ToDraw::contains(const QPointF &point) const 0099 { 0100 bool result = QGraphicsSvgItem::contains(point); 0101 if (result) 0102 { 0103 QRectF bounds = transform().mapRect(unclippedRect()); 0104 const QImage &img = toImage(elementId(), qRound(bounds.width()), qRound(bounds.height()), renderer()); 0105 QPointF transformedPoint = transform().map(point); 0106 result = qAlpha(img.pixel(transformedPoint.toPoint())) != 0; 0107 } 0108 return result; 0109 } 0110 0111 int ToDraw::type() const 0112 { 0113 return Type; 0114 }