• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

granatier

  • sources
  • kde-4.14
  • kdegames
  • granatier
  • src
elementitem.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2009 Mathias Kraus <k.hias@gmx.de>
3  * Copyright 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "elementitem.h"
20 #include "element.h"
21 
22 #include <QGraphicsView>
23 
24 #include <KGameRenderer>
25 
26 ElementItem::ElementItem(Element* p_model, KGameRenderer* renderer) : KGameRenderedItem(renderer, "")
27 {
28  m_model = p_model;
29  // Init the view coordinates
30  update(p_model->getX(), p_model->getY());
31  // Connects the model to the view
32  connect(p_model, SIGNAL(moved(qreal,qreal)), this, SLOT(update(qreal,qreal)));
33 
34  m_renderSize = QSize(1, 1);
35  m_itemSizeSet = QSize(Granatier::CellSize, Granatier::CellSize);
36  m_itemSizeReal = m_itemSizeSet;
37 }
38 
39 ElementItem::~ElementItem()
40 {
41 }
42 
43 Element* ElementItem::getModel() const
44 {
45  return m_model;
46 }
47 
48 QPainterPath ElementItem::shape() const
49 {
50  QPainterPath path;
51  path.addEllipse(boundingRect());
52  return path;
53 }
54 
55 void ElementItem::update(qreal p_x, qreal p_y)
56 {
57  // Compute the top-right coordinates of the item
58  qreal x = p_x - m_itemSizeSet.width() / 2;
59  qreal y = p_y - m_itemSizeSet.height() / 2;
60 
61  // Updates the view coordinates
62  setPos(x, y);
63 }
64 
65 void ElementItem::updateGraphics(qreal svgScaleFactor)
66 {
67  updateGraphicsInternal(svgScaleFactor);
68 }
69 
70 void ElementItem::updateGraphicsInternal(qreal svgScaleFactor)
71 {
72  if(scene()->views().isEmpty())
73  {
74  return;
75  }
76 
77  //calculate the size of the item on the view
78  QPoint viewTopLeft = scene()->views().first()->mapFromScene(0, 0);
79  QPoint viewBottomRight = scene()->views().first()->mapFromScene(m_itemSizeSet.width(), m_itemSizeSet.height());
80 
81  int viewWidth = viewBottomRight.x() - viewTopLeft.x();
82  int viewHeight = viewBottomRight.y() - viewTopLeft.y();
83 
84  //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
85  if(m_itemSizeSet.width() != Granatier::CellSize || m_itemSizeSet.height() != Granatier::CellSize)
86  {
87  viewBottomRight = scene()->views().first()->mapFromScene(Granatier::CellSize, Granatier::CellSize);
88  int viewCellWidth = viewBottomRight.x() - viewTopLeft.x();
89  int viewCellHeight = viewBottomRight.y() - viewTopLeft.y();
90  if((viewCellWidth - viewWidth) % 2 != 0)
91  {
92  viewWidth--;
93  }
94  if((viewCellHeight - viewHeight) % 2 != 0)
95  {
96  viewHeight--;
97  }
98 
99  //calculate the real item size after change of the render size
100  QPointF sceneTopLeft = scene()->views().first()->mapToScene(0, 0);
101  QPointF sceneBottomRight = scene()->views().first()->mapToScene(viewWidth, viewHeight);
102 
103  qreal sceneWidth = sceneBottomRight.x() - sceneTopLeft.x();
104  qreal sceneHeight = sceneBottomRight.y() - sceneTopLeft.y();
105  m_itemSizeReal = QSize(sceneWidth, sceneHeight);
106  }
107 
108  setRenderSize(QSize(viewWidth, viewHeight));
109  setScale(svgScaleFactor);
110  m_renderSize = renderSize();
111 }
QPainterPath::addEllipse
void addEllipse(const QRectF &boundingRectangle)
element.h
QSize::width
int width() const
ElementItem::ElementItem
ElementItem(Element *p_model, KGameRenderer *renderer)
Creates a new ElementItem instance.
Definition: elementitem.cpp:26
Element::getX
qreal getX() const
Gets the Element x-coordinate.
Definition: element.cpp:46
ElementItem::update
virtual void update(qreal p_x, qreal p_y)
Updates the ElementItem coordinates.
Definition: elementitem.cpp:55
ElementItem::~ElementItem
~ElementItem()
Deletes the ElementItem instance.
Definition: elementitem.cpp:39
QPoint
QPoint::x
int x() const
QPoint::y
int y() const
ElementItem::getModel
Element * getModel() const
Gets the Element model.
Definition: elementitem.cpp:43
ElementItem::m_itemSizeSet
QSize m_itemSizeSet
Definition: elementitem.h:44
QPointF
Granatier::CellSize
const qreal CellSize
The Cell size.
Definition: granatierglobals.h:31
QPointF::x
qreal x() const
QPointF::y
qreal y() const
ElementItem::m_model
Element * m_model
The instance of Element the ElementItem will represent.
Definition: elementitem.h:40
ElementItem::updateGraphicsInternal
virtual void updateGraphicsInternal(qreal svgScaleFactor)
Definition: elementitem.cpp:70
Element
This class describes the common characteristics and behaviour of any game Element (character or item)...
Definition: element.h:32
QSize
Element::getY
qreal getY() const
Gets the Element y-coordinate.
Definition: element.cpp:51
QPainterPath
elementitem.h
ElementItem::shape
QPainterPath shape() const
Reimplement QGraphicsItem::shape() to return an ellipse to improve collisions.
Definition: elementitem.cpp:48
QSize::height
int height() const
KGameRenderedItem
ElementItem::m_renderSize
QSize m_renderSize
Definition: elementitem.h:42
ElementItem::updateGraphics
virtual void updateGraphics(qreal svgScaleFactor)
Updates the graphics after a resize.
Definition: elementitem.cpp:65
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
ElementItem::m_itemSizeReal
QSize m_itemSizeReal
Definition: elementitem.h:45
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:10 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

granatier

Skip menu "granatier"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal