File indexing completed on 2024-02-18 04:03:24

0001 /*
0002     SPDX-FileCopyrightText: 2009 Mathias Kraus <k.hias@gmx.de>
0003     SPDX-FileCopyrightText: 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "elementitem.h"
0009 #include "element.h"
0010 
0011 #include <QGraphicsView>
0012 
0013 #include <KGameRenderer>
0014 
0015 ElementItem::ElementItem(Element* p_model, KGameRenderer* renderer) : KGameRenderedItem(renderer, QStringLiteral(""))
0016 {
0017     m_model = p_model;
0018     // Init the view coordinates
0019     update(p_model->getX(), p_model->getY());
0020     // Connects the model to the view
0021     connect(p_model, &Element::moved, this, &ElementItem::update);
0022 
0023     m_renderSize = QSize(1, 1);
0024     m_itemSizeSet = QSize(static_cast<int>(Granatier::CellSize), static_cast<int>(Granatier::CellSize));
0025     m_itemSizeReal = m_itemSizeSet;
0026 }
0027 
0028 ElementItem::~ElementItem()
0029 = default;
0030 
0031 Element* ElementItem::getModel() const
0032 {
0033     return m_model;
0034 }
0035 
0036 QPainterPath ElementItem::shape() const
0037 {
0038     QPainterPath path;
0039     path.addEllipse(boundingRect());
0040     return path;
0041 }
0042 
0043 void ElementItem::update(qreal p_x, qreal p_y)
0044 {
0045     // Compute the top-right coordinates of the item
0046     qreal x = p_x - m_itemSizeSet.width() / 2;
0047     qreal y = p_y - m_itemSizeSet.height() / 2;
0048 
0049     // Updates the view coordinates
0050     setPos(x, y);
0051 }
0052 
0053 void ElementItem::updateGraphics(qreal svgScaleFactor)
0054 {
0055     updateGraphicsInternal(svgScaleFactor);
0056 }
0057 
0058 void ElementItem::updateGraphicsInternal(qreal svgScaleFactor)
0059 {
0060     if(scene()->views().isEmpty())
0061     {
0062         return;
0063     }
0064 
0065     //calculate the size of the item on the view
0066     QPoint viewTopLeft = scene()->views().first()->mapFromScene(0, 0);
0067     QPoint viewBottomRight = scene()->views().first()->mapFromScene(m_itemSizeSet.width(), m_itemSizeSet.height());
0068 
0069     int viewWidth = viewBottomRight.x() - viewTopLeft.x();
0070     int viewHeight = viewBottomRight.y() - viewTopLeft.y();
0071 
0072     //for better alignment, if the item size is different from the cell size, make the difference between the cell size and item size always even
0073     if(m_itemSizeSet.width() != Granatier::CellSize || m_itemSizeSet.height() != Granatier::CellSize)
0074     {
0075         viewBottomRight = scene()->views().first()->mapFromScene(Granatier::CellSize, Granatier::CellSize);
0076         int viewCellWidth = viewBottomRight.x() - viewTopLeft.x();
0077         int viewCellHeight = viewBottomRight.y() - viewTopLeft.y();
0078         if((viewCellWidth - viewWidth) % 2 != 0)
0079         {
0080             viewWidth--;
0081         }
0082         if((viewCellHeight - viewHeight) % 2 != 0)
0083         {
0084             viewHeight--;
0085         }
0086 
0087         //calculate the real item size after change of the render size
0088         QPointF sceneTopLeft = scene()->views().first()->mapToScene(0, 0);
0089         QPointF sceneBottomRight = scene()->views().first()->mapToScene(viewWidth, viewHeight);
0090 
0091         int sceneWidth = static_cast<int>(sceneBottomRight.x() - sceneTopLeft.x());
0092         int sceneHeight = static_cast<int>(sceneBottomRight.y() - sceneTopLeft.y());
0093         m_itemSizeReal = QSize(sceneWidth, sceneHeight);
0094     }
0095 
0096     setRenderSize(QSize(viewWidth, viewHeight));
0097     setScale(svgScaleFactor);
0098     m_renderSize = renderSize();
0099 }
0100 
0101 #include "moc_elementitem.cpp"