[6dd4ed]: src / interfaces / component / connectoritem.cpp Maximize Restore History

Download this file

connectoritem.cpp    155 lines (135 with data), 4.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) 2010 Julian B채ume <julian@svg4all.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "connectoritem.h"
#include <QPen>
#include <QGraphicsSceneMouseEvent>
#include <QStyleOption>
#include <QGraphicsScene>
#include "connector.h"
#include <idocumentscene.h>
#include "icomponentitem.h"
#include <KDebug>
using namespace KTechLab;
ConnectorItem::ConnectorItem(IDocumentScene* scene, QGraphicsItem* parent)
: QGraphicsPathItem(parent, scene),
m_connector(0), m_scene(scene)
{
init();
}
ConnectorItem::ConnectorItem(const QVariantMap& connectorData, IDocumentScene* scene, QGraphicsItem* parent)
: QGraphicsPathItem(parent, scene), m_scene(scene)
{
init();
m_connector = new Connector(connectorData);
setPath(m_connector->route());
const Node* s;
const Node* e;
if (connectorData.value("start-node-is-child").toString() == "1"){
const IComponentItem* parent = scene->item(connectorData.value("start-node-parent").toString());
s = parent->node(connectorData.value("start-node-cid").toString());
} else if (connectorData.value("start-node-is-child").toString() == "0"){
s = scene->node(connectorData.value("start-node-id").toString());
}
if (connectorData.value("end-node-is-child").toString() == "1"){
const IComponentItem* parent = scene->item(connectorData.value("end-node-parent").toString());
e = parent->node(connectorData.value("end-node-cid").toString());
} else if (connectorData.value("end-node-is-child").toString() == "0"){
e = scene->node(connectorData.value("end-node-id").toString());
}
m_connector->setStartNode(s);
m_startNode = s;
m_connector->setEndNode(e);
m_endNode = e;
}
void ConnectorItem::init()
{
m_startNode = 0;
m_endNode = 0;
setAcceptHoverEvents(true);
setFlags(ItemIsSelectable | ItemSendsGeometryChanges);
setZValue(-1);
}
ConnectorItem::~ConnectorItem()
{
delete m_connector;
m_connector = 0;
}
void ConnectorItem::setConnector(const Connector& connector)
{
if (m_connector){
delete m_connector;
m_connector = 0;
}
m_connector = new Connector(connector);
setPath(m_connector->route());
}
Connector* ConnectorItem::connector() const
{
return m_connector;
}
void ConnectorItem::setStartNode(const Node* node)
{
m_startNode = node;
}
void ConnectorItem::setEndNode(const Node* node)
{
m_endNode = node;
}
const Node* ConnectorItem::endNode() const
{
return m_endNode;
}
const Node* ConnectorItem::startNode() const
{
return m_startNode;
}
void ConnectorItem::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
{
if (!isSelected()){
setPen(scene()->palette().highlight().color());
event->accept();
}
QGraphicsPathItem::hoverEnterEvent(event);
}
void ConnectorItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
{
if (!isSelected()){
setPen(scene()->palette().windowText().color());
event->accept();
}
QGraphicsPathItem::hoverLeaveEvent(event);
}
QVariant ConnectorItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant& value)
{
if (change == ItemSelectedHasChanged && value.toBool()){
setPen(scene()->palette().highlight().color());
} else if (change == ItemSelectedHasChanged && !value.toBool()){
setPen(scene()->palette().windowText().color());
} else if (change == ItemPositionChange) {
return QPointF(0,0);
}
return QGraphicsItem::itemChange(change, value);
}
void KTechLab::ConnectorItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
//This is quite ugly. we copy the whole object, because it's const
//may be, we should just paint the path on our own.
QStyleOptionGraphicsItem *cOption = new QStyleOptionGraphicsItem(*option);
//reset selected state, since we handle painting of selection-related visualisation
cOption->state &= ~QStyle::State_Selected;
QGraphicsPathItem::paint(painter, cOption, widget);
delete cOption;
}