1/*
2 * Copyright 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "elementitem.h"
19
20ElementItem::ElementItem(Element* p_model) : QGraphicsSvgItem() {
21 m_model = p_model;
22 // Init the view coordinates
23 setPos(p_model->getX() - boundingRect().width() / 2, p_model->getY() - boundingRect().height() / 2);
24 // Connects the model to the view
25 connect(p_model, SIGNAL(moved(qreal,qreal)), this, SLOT(update(qreal,qreal)));
26 setCacheMode(DeviceCoordinateCache);
27 setMaximumCacheSize(QSize(500, 500));
28}
29
30ElementItem::~ElementItem() {
31 delete m_model;
32
33}
34
35Element* ElementItem::getModel() const {
36 return m_model;
37}
38
39QPainterPath ElementItem::shape() const {
40 QPainterPath path;
41 path.addEllipse(boundingRect());
42 return path;
43}
44
45void ElementItem::update(qreal p_x, qreal p_y) {
46 // Compute the top-right coordinates of the item
47 qreal x = p_x - boundingRect().width() / 2;
48 qreal y = p_y - boundingRect().height() / 2;
49
50 // Updates the view coordinates
51 setPos(x, y);
52}
53