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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
/* Copyright (c) 2012, STANISLAW ADASZEWSKI
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of STANISLAW ADASZEWSKI nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL STANISLAW ADASZEWSKI BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
#include "qneconnection.h"
#include "common.h"
#include "qneport.h"
#include "thememanager.h"
#include <QBrush>
#include <QDebug>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QPen>
#include <QStyleOptionGraphicsItem>
QNEConnection::QNEConnection(QGraphicsItem *parent)
: QGraphicsPathItem(parent)
{
setFlag(QGraphicsItem::ItemIsSelectable);
setCacheMode(QGraphicsItem::DeviceCoordinateCache);
setZValue(-1);
updateTheme();
setPen(QPen(m_invalidColor, 5));
}
QNEConnection::~QNEConnection()
{
if (m_startPort) {
m_startPort->disconnect(this);
}
if (m_endPort) {
m_endPort->disconnect(this);
}
}
void QNEConnection::setStartPos(const QPointF point)
{
m_startPos = point;
}
void QNEConnection::setEndPos(const QPointF point)
{
m_endPos = point;
}
void QNEConnection::setStartPort(QNEOutputPort *port)
{
auto *oldPort = m_startPort;
m_startPort = port;
if (oldPort && (oldPort != port)) {
oldPort->disconnect(this);
}
if (port) {
port->connect(this);
setStartPos(port->scenePos());
setStatus(port->status());
}
}
void QNEConnection::setEndPort(QNEInputPort *port)
{
auto *oldPort = m_endPort;
m_endPort = port;
if (oldPort && (oldPort != port)) {
oldPort->disconnect(this);
}
if (port) {
port->connect(this);
setEndPos(port->scenePos());
port->setStatus(status());
}
}
void QNEConnection::updatePosFromPorts()
{
if (m_startPort) {
m_startPos = m_startPort->scenePos();
}
if (m_endPort) {
m_endPos = m_endPort->scenePos();
}
updatePath();
}
void QNEConnection::updatePath()
{
QPainterPath path;
path.moveTo(m_startPos);
qreal dx = m_endPos.x() - m_startPos.x();
qreal dy = m_endPos.y() - m_startPos.y();
QPointF ctr1(m_startPos.x() + dx * 0.25, m_startPos.y() + dy * 0.1);
QPointF ctr2(m_startPos.x() + dx * 0.75, m_startPos.y() + dy * 0.9);
path.cubicTo(ctr1, ctr2, m_endPos);
setPath(path);
}
QNEOutputPort *QNEConnection::startPort() const
{
return m_startPort;
}
QNEInputPort *QNEConnection::endPort() const
{
return m_endPort;
}
double QNEConnection::angle()
{
QNEPort *port1 = m_startPort;
QNEPort *port2 = m_endPort;
if (port1 && port2) {
if (port2->isOutput()) {
std::swap(port1, port2);
}
return QLineF(port1->scenePos(), port2->scenePos()).angle();
}
return 0.0;
}
void QNEConnection::save(QDataStream &stream) const
{
stream << reinterpret_cast<quint64>(m_startPort);
stream << reinterpret_cast<quint64>(m_endPort);
}
void QNEConnection::load(QDataStream &stream, const QMap<quint64, QNEPort *> &portMap)
{
quint64 ptr1; stream >> ptr1;
quint64 ptr2; stream >> ptr2;
if (portMap.isEmpty()) {
qCDebug(three) << "Empty port map.";
auto *port1 = reinterpret_cast<QNEPort *>(ptr1);
auto *port2 = reinterpret_cast<QNEPort *>(ptr2);
if (port1 && port2) {
if (port1->isInput() && port2->isOutput()) {
setStartPort(dynamic_cast<QNEOutputPort *>(port2));
setEndPort(dynamic_cast<QNEInputPort *>(port1));
} else if (port1->isOutput() && port2->isInput()) {
setStartPort(dynamic_cast<QNEOutputPort *>(port1));
setEndPort(dynamic_cast<QNEInputPort *>(port2));
}
}
}
if (!portMap.isEmpty()) {
if (!portMap.contains(ptr1) || !portMap.contains(ptr2)) {
return;
}
qCDebug(three) << "Port map with elements: ptr1(" << ptr1 << "), ptr2(" << ptr2 << ")";
auto *port1 = portMap.value(ptr1);
auto *port2 = portMap.value(ptr2);
qCDebug(three) << "Before if 1.";
if (port1 && port2) {
qCDebug(three) << "Before if 2.";
if (port1->isInput() && port2->isOutput()) {
qCDebug(three) << "Setting start 1.";
setStartPort(dynamic_cast<QNEOutputPort *>(port2));
qCDebug(three) << "Setting end 1.";
setEndPort(dynamic_cast<QNEInputPort *>(port1));
} else if (port1->isOutput() && port2->isInput()) {
qCDebug(three) << "Setting start 2.";
setStartPort(dynamic_cast<QNEOutputPort *>(port1));
qCDebug(three) << "Setting end 2.";
setEndPort(dynamic_cast<QNEInputPort *>(port2));
}
qCDebug(three) << "After ifs.";
}
}
qCDebug(three) << "Updating pos from ports.";
updatePosFromPorts();
}
QNEPort *QNEConnection::otherPort(const QNEPort *port) const
{
if (port == m_startPort) {
return m_endPort;
}
return m_startPort;
}
Status QNEConnection::status() const
{
return m_status;
}
void QNEConnection::setStatus(const Status status)
{
if (status == m_status) {
return;
}
m_status = status;
switch (status) {
case Status::Invalid: setPen(QPen(m_invalidColor, 5)); break;
case Status::Inactive: setPen(QPen(m_inactiveColor, 3)); break;
case Status::Active: setPen(QPen(m_activeColor, 3)); break;
default:
// Handle unexpected status values gracefully
setPen(QPen(m_invalidColor, 5));
break;
}
if (endPort()) {
endPort()->setStatus(status);
}
}
void QNEConnection::updateTheme()
{
const ThemeAttributes theme = ThemeManager::attributes();
m_invalidColor = theme.m_connectionInvalid;
m_inactiveColor = theme.m_connectionInactive;
m_activeColor = theme.m_connectionActive;
m_selectedColor = theme.m_connectionSelected;
update();
}
void QNEConnection::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(widget)
Q_UNUSED(option)
if (m_highLight) {
painter->save();
painter->setPen(QPen(Qt::blue, 10));
painter->drawPath(path());
painter->restore();
}
painter->setPen(isSelected() ? QPen(m_selectedColor, 5) : pen());
painter->drawPath(path());
}
QVariant QNEConnection::itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemSelectedChange) {
if (value.toBool()) {
if (startPort()) startPort()->hoverEnter();
if (endPort()) endPort()->hoverEnter();
} else {
if (startPort()) startPort()->hoverLeave();
if (endPort()) endPort()->hoverLeave();
}
}
return QGraphicsPathItem::itemChange(change, value);
}
bool QNEConnection::highLight()
{
return m_highLight;
}
void QNEConnection::setHighLight(const bool highLight)
{
m_highLight = highLight;
update();
}
QRectF QNEConnection::boundingRect() const
{
return path().boundingRect().adjusted(-10, -10, 10, 10);
}
bool QNEConnection::sceneEvent(QEvent *event)
{
if (auto mouseEvent = dynamic_cast<QGraphicsSceneMouseEvent *>(event)) {
if (mouseEvent->modifiers().testFlag(Qt::ControlModifier)) {
return true;
}
}
return QGraphicsPathItem::sceneEvent(event);
}
QDataStream &operator<<(QDataStream &stream, const QNEConnection *conn)
{
qCDebug(zero) << "Writing Connection.";
stream << conn->type();
conn->save(stream);
return stream;
}
|