1 // qpwgraph_command.h
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 #ifndef __qpwgraph_command_h
23 #define __qpwgraph_command_h
24 
25 #include <QUndoCommand>
26 
27 #include <qpwgraph_node.h>
28 
29 
30 // Forward decls.
31 class qpwgraph_canvas;
32 
33 
34 //----------------------------------------------------------------------------
35 // qpwgraph_command -- Generic graph command pattern
36 
37 class qpwgraph_command : public QUndoCommand
38 {
39 public:
40 
41 	// Constructor.
42 	qpwgraph_command(qpwgraph_canvas *canvas, QUndoCommand *parent = nullptr);
43 
44 	// Accessors.
canvas()45 	qpwgraph_canvas *canvas() const
46 		{ return m_canvas; }
47 
48 	// Command methods.
49 	void undo();
50 	void redo();
51 
52 protected:
53 
54 	// Command executive method.
55 	virtual bool execute(bool is_undo = false) = 0;
56 
57 private:
58 
59 	// Command arguments.
60 	qpwgraph_canvas *m_canvas;
61 };
62 
63 
64 //----------------------------------------------------------------------------
65 // qpwgraph_connect command -- Connect graph command
66 
67 class qpwgraph_connect_command : public qpwgraph_command
68 {
69 public:
70 
71 	// Constructor.
72 	qpwgraph_connect_command(qpwgraph_canvas *canvas,
73 		qpwgraph_port *port1, qpwgraph_port *port2,
74 		bool is_connect, qpwgraph_command *parent = nullptr);
75 
76 protected:
77 
78 	// Command item address
79 	struct Addr
80 	{
81 		// Constructors.
AddrAddr82 		Addr(qpwgraph_port *port)
83 		{
84 			qpwgraph_node *node = port->portNode();
85 			node_id   = node->nodeId();
86 			node_type = node->nodeType();
87 			port_id   = port->portId();
88 			port_type = port->portType();
89 		}
90 		// Copy constructor.
AddrAddr91 		Addr(const Addr& addr)
92 		{
93 			node_id   = addr.node_id;
94 			node_type = addr.node_type;
95 			port_id   = addr.port_id;
96 			port_type = addr.port_type;
97 		}
98 		// Member fields.
99 		uint node_id;
100 		uint node_type;
101 		uint port_id;
102 		uint port_type;
103 	};
104 
105 	// Command item descriptor
106 	struct Item
107 	{
108 		// Constructor.
ItemItem109 		Item(qpwgraph_port *port1, qpwgraph_port *port2, bool is_connect)
110 			: addr1(port1), addr2(port2), m_connect(is_connect) {}
111 		// Copy constructor.
ItemItem112 		Item(const Item& item) : addr1(item.addr1), addr2(item.addr2),
113 			m_connect(item.is_connect()) {}
114 
115 		// Accessors.
is_connectItem116 		bool is_connect() const
117 			{ return m_connect; }
118 
119 		// Public member fields.
120 		Addr addr1;
121 		Addr addr2;
122 
123 	private:
124 
125 		// Private member fields.
126 		bool m_connect;
127 	};
128 
129 	// Command executive method.
130 	bool execute(bool is_undo);
131 
132 private:
133 
134 	// Command arguments.
135 	Item m_item;
136 };
137 
138 
139 //----------------------------------------------------------------------------
140 // qpwgraph_move_command -- Move (node) graph command
141 
142 class qpwgraph_move_command : public qpwgraph_command
143 {
144 public:
145 
146 	// Constructor.
147 	qpwgraph_move_command(qpwgraph_canvas *canvas,
148 		const QList<qpwgraph_node *>& nodes,
149 		const QPointF& pos1, const QPointF& pos2,
150 		qpwgraph_command *parent = nullptr);
151 
152 	// Destructor.
153 	~qpwgraph_move_command();
154 
155 protected:
156 
157 	// Command item descriptor
158 	struct Item
159 	{
160 		uint node_id;
161 		qpwgraph_item::Mode node_mode;
162 		uint node_type;
163 	};
164 
165 	// Command executive method.
166 	bool execute(bool is_undo);
167 
168 private:
169 
170 	// Command arguments.
171 	QList<Item *> m_items;
172 
173 	QPointF m_pos1;
174 	QPointF m_pos2;
175 
176 	int m_nexec;
177 };
178 
179 
180 //----------------------------------------------------------------------------
181 // qpwgraph_rename_command -- Rename (item) graph command
182 
183 class qpwgraph_rename_command : public qpwgraph_command
184 {
185 public:
186 
187 	// Constructor.
188 	qpwgraph_rename_command(qpwgraph_canvas *canvas,
189 		qpwgraph_item *item, const QString& name,
190 		qpwgraph_command *parent = nullptr);
191 
192 protected:
193 
194 	// Command item descriptor
195 	struct Item
196 	{
197 		int  item_type;
198 		uint node_id;
199 		qpwgraph_item::Mode node_mode;
200 		uint node_type;
201 		uint port_id;
202 		qpwgraph_item::Mode port_mode;
203 		uint port_type;
204 	};
205 
206 	// Command executive method.
207 	bool execute(bool is_undo);
208 
209 private:
210 
211 	// Command arguments.
212 	Item    m_item;
213 	QString m_name;
214 };
215 
216 
217 #endif	// __qpwgraph_command_h
218 
219 // end of qpwgraph_command.h
220