1 // qpwgraph_sect.cpp
2 //
3 /****************************************************************************
4 Copyright (C) 2021, rncbc aka Rui Nuno Capela. All rights reserved.
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 *****************************************************************************/
21
22 #include "qpwgraph_sect.h"
23
24 #include "qpwgraph_canvas.h"
25 #include "qpwgraph_connect.h"
26
27
28 //----------------------------------------------------------------------------
29 // qpwgraph_sect -- Generic graph driver
30
31 // Constructor.
qpwgraph_sect(qpwgraph_canvas * canvas)32 qpwgraph_sect::qpwgraph_sect ( qpwgraph_canvas *canvas )
33 : QObject(canvas), m_canvas(canvas)
34 {
35 }
36
37
38 // Accessors.
canvas(void) const39 qpwgraph_canvas *qpwgraph_sect::canvas (void) const
40 {
41 return m_canvas;
42 }
43
44
45 // Generic sect/graph methods.
addItem(qpwgraph_item * item)46 void qpwgraph_sect::addItem ( qpwgraph_item *item )
47 {
48 m_canvas->addItem(item);
49
50 if (item->type() == qpwgraph_connect::Type) {
51 qpwgraph_connect *connect = static_cast<qpwgraph_connect *> (item);
52 if (connect)
53 m_connects.append(connect);
54 }
55 }
56
57
removeItem(qpwgraph_item * item)58 void qpwgraph_sect::removeItem ( qpwgraph_item *item )
59 {
60 if (item->type() == qpwgraph_connect::Type) {
61 qpwgraph_connect *connect = static_cast<qpwgraph_connect *> (item);
62 if (connect)
63 m_connects.removeAll(connect);
64 }
65
66 m_canvas->removeItem(item);
67 }
68
69
70 // Clean-up all un-marked items...
resetItems(uint node_type)71 void qpwgraph_sect::resetItems ( uint node_type )
72 {
73 const QList<qpwgraph_connect *> connects(m_connects);
74
75 foreach (qpwgraph_connect *connect, connects) {
76 if (connect->isMarked()) {
77 connect->setMarked(false);
78 } else {
79 removeItem(connect);
80 delete connect;
81 }
82 }
83
84 m_canvas->resetNodes(node_type);
85 }
86
87
clearItems(uint node_type)88 void qpwgraph_sect::clearItems ( uint node_type )
89 {
90 qpwgraph_sect::resetItems(node_type);
91
92 // qDeleteAll(m_connects);
93 m_connects.clear();
94
95 m_canvas->clearNodes(node_type);
96 }
97
98
99 // Special node finder.
findNode(uint id,qpwgraph_item::Mode mode,int type) const100 qpwgraph_node *qpwgraph_sect::findNode (
101 uint id, qpwgraph_item::Mode mode, int type ) const
102 {
103 return m_canvas->findNode(id, mode, type);
104 }
105
106
107 // Client/port renaming method.
renameItem(qpwgraph_item * item,const QString & name)108 void qpwgraph_sect::renameItem (
109 qpwgraph_item *item, const QString& name )
110 {
111 qpwgraph_node *node = nullptr;
112
113 if (item->type() == qpwgraph_node::Type) {
114 qpwgraph_node *node = static_cast<qpwgraph_node *> (item);
115 if (node)
116 node->setNodeTitle(name);
117 }
118 else
119 if (item->type() == qpwgraph_port::Type) {
120 qpwgraph_port *port = static_cast<qpwgraph_port *> (item);
121 if (port)
122 node = port->portNode();
123 if (port && node)
124 port->setPortTitle(name);
125 }
126
127 if (node)
128 node->updatePath();
129 }
130
131
132 // end of qpwgraph_sect.cpp
133