File indexing completed on 2024-12-01 11:20:52

0001 /***************************************************************************
0002  *   Copyright (C) 2003-2005 by David Saxton                               *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "node.h"
0012 #include "cnitem.h"
0013 #include "connector.h"
0014 #include "icndocument.h"
0015 #include "itemdocumentdata.h"
0016 
0017 #include <QPainter>
0018 
0019 #include <ktechlab_debug.h>
0020 
0021 QColor Node::m_selectedColor = QColor(101, 134, 192);
0022 
0023 Node::Node(ICNDocument *icnDocument, Node::node_type type, int dir, const QPoint &pos, QString *id)
0024     : // QObject(),
0025     KtlQCanvasPolygon(icnDocument ? icnDocument->canvas() : nullptr)
0026 {
0027     QString name("Node");
0028     if (id) {
0029         name.append(QString("-%1").arg(*id));
0030     } else {
0031         name.append("-Unknown");
0032     }
0033     setObjectName(name.toLatin1().data());
0034     qCDebug(KTL_LOG) << " this=" << this;
0035 
0036     m_length = 8;
0037     p_nodeGroup = nullptr;
0038     p_parentItem = nullptr;
0039     b_deleted = false;
0040     m_dir = dir;
0041     m_type = type;
0042     p_icnDocument = icnDocument;
0043     m_level = 0;
0044 
0045     if (p_icnDocument) {
0046         if (id) {
0047             m_id = *id;
0048             if (!p_icnDocument->registerUID(*id))
0049                 qCCritical(KTL_LOG) << "Could not register id " << *id;
0050         } else
0051             m_id = p_icnDocument->generateUID("node" + QString::number(type));
0052     }
0053 
0054     initPoints();
0055     move(pos.x(), pos.y());
0056     setBrush(Qt::black);
0057     setPen(QPen(Qt::black));
0058     show();
0059 
0060     emit(moved(this));
0061 }
0062 
0063 Node::~Node()
0064 {
0065     if (p_icnDocument)
0066         p_icnDocument->unregisterUID(id());
0067 }
0068 
0069 void Node::setLevel(const int level)
0070 {
0071     m_level = level;
0072 }
0073 
0074 void Node::setLength(int length)
0075 {
0076     if (m_length == length)
0077         return;
0078     m_length = length;
0079     initPoints();
0080 }
0081 
0082 void Node::setOrientation(int dir)
0083 {
0084     if (m_dir == dir)
0085         return;
0086     m_dir = dir;
0087     initPoints();
0088 }
0089 
0090 void Node::initPoints()
0091 {
0092     int l = m_length;
0093 
0094     // Bounding rectangle, facing right
0095     QPolygon pa(QRect(0, -8, l, 16));
0096 
0097     QTransform m;
0098     m.rotate(m_dir);
0099     pa = m.map(pa);
0100     setPoints(pa);
0101 }
0102 
0103 void Node::setVisible(bool yes)
0104 {
0105     if (isVisible() == yes)
0106         return;
0107 
0108     KtlQCanvasPolygon::setVisible(yes);
0109 }
0110 
0111 void Node::setParentItem(CNItem *parentItem)
0112 {
0113     if (!parentItem) {
0114         qCCritical(KTL_LOG) << "no parent item";
0115         return;
0116     }
0117 
0118     p_parentItem = parentItem;
0119 
0120     setLevel(p_parentItem->level());
0121 
0122     connect(p_parentItem, qOverload<double, double>(&CNItem::movedBy), this, qOverload<double, double>(&Node::moveBy));
0123     connect(p_parentItem, &CNItem::removed, this, qOverload<Item *>(&Node::removeNode));
0124 }
0125 
0126 void Node::removeNode()
0127 {
0128     if (b_deleted)
0129         return;
0130     b_deleted = true;
0131 
0132     emit removed(this);
0133     p_icnDocument->appendDeleteList(this);
0134 }
0135 
0136 void Node::setICNDocument(ICNDocument *documentPtr)
0137 {
0138     p_icnDocument = documentPtr;
0139 }
0140 
0141 void Node::moveBy(double dx, double dy)
0142 {
0143     if (dx == 0 && dy == 0)
0144         return;
0145     KtlQCanvasPolygon::moveBy(dx, dy);
0146     emit moved(this);
0147 }
0148 
0149 NodeData Node::nodeData() const
0150 {
0151     NodeData data;
0152     data.x = x();
0153     data.y = y();
0154     return data;
0155 }
0156 
0157 void Node::setNodeSelected(bool yes)
0158 {
0159     if (isSelected() == yes)
0160         return;
0161 
0162     KtlQCanvasItem::setSelected(yes);
0163 
0164     setPen(yes ? m_selectedColor : Qt::black);
0165     setBrush(yes ? m_selectedColor : Qt::black);
0166 }
0167 
0168 void Node::initPainter(QPainter &p)
0169 {
0170     p.translate(int(x()), int(y()));
0171     p.rotate(m_dir);
0172 }
0173 
0174 void Node::deinitPainter(QPainter &p)
0175 {
0176     p.rotate(-m_dir);
0177     p.translate(-int(x()), -int(y()));
0178 }
0179 
0180 #include "moc_node.cpp"