1 /*
2 	Copyright 2006-2019 The QElectroTech Team
3 	This file is part of QElectroTech.
4 
5 	QElectroTech is free software: you can redistribute it and/or modify
6 	it under the terms of the GNU General Public License as published by
7 	the Free Software Foundation, either version 2 of the License, or
8 	(at your option) any later version.
9 
10 	QElectroTech is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License
16 	along with QElectroTech.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 #ifndef DIAGRAM_COMMANDS_H
19 #define DIAGRAM_COMMANDS_H
20 
21 #include "borderproperties.h"
22 #include "qetgraphicsitem/conductor.h"
23 #include "diagramcontent.h"
24 #include "qet.h"
25 #include "qetgraphicsitem/qetshapeitem.h"
26 #include "conductorprofile.h"
27 #include "diagram.h"
28 #include "undocommand/deleteqgraphicsitemcommand.h"
29 
30 class DiagramTextItem;
31 class Element;
32 class IndependentTextItem;
33 class DiagramImageItem;
34 class QetGraphicsItem;
35 
36 /**
37  * @brief The AddItemCommand class
38  * This command add an item in a diagram
39  * The item to add is template, but must be QGraphicsItem or derived.
40  */
41 template <typename QGI>
42 class AddItemCommand : public QUndoCommand {
43 	public:
44 		AddItemCommand(QGI item, Diagram *diagram, const QPointF &pos = QPointF(), QUndoCommand *parent = nullptr) :
QUndoCommand(parent)45 			QUndoCommand (parent),
46 			m_item (item),
47 			m_diagram (diagram),
48 			m_pos(pos)
49 		{
50 			setText(QObject::tr("Ajouter ") + itemText(item));
51 			m_diagram -> qgiManager().manage(m_item);
52 		}
53 
~AddItemCommand()54 		~AddItemCommand() override {
55 			m_diagram -> qgiManager().release(m_item);
56 		}
57 
undo()58 		void undo() override {
59 			m_diagram -> showMe();
60 			m_diagram -> removeItem(m_item);
61 			QUndoCommand::undo();
62 		}
63 
redo()64 		void redo() override {
65 			m_diagram -> showMe();
66 			m_diagram -> addItem(m_item);
67 			m_item    -> setPos(m_pos);
68 			QUndoCommand::redo();
69 		}
70 
71 	private:
72 		QGI m_item;
73 		Diagram *m_diagram;
74 		QPointF m_pos;
75 };
76 
77 //Return a string to describe a QGraphicsItem
78 QString itemText(const QetGraphicsItem     *item);
79 QString itemText(const IndependentTextItem *item);
80 QString itemText(const Conductor           *item);
81 
82 /**
83 	This command pastes some content onto a particular diagram.
84 */
85 class PasteDiagramCommand : public QUndoCommand {
86 	// constructors, destructor
87 	public:
88 	PasteDiagramCommand(Diagram *, const DiagramContent &, QUndoCommand * = nullptr);
89 	~PasteDiagramCommand() override;
90 	private:
91 	PasteDiagramCommand(const PasteDiagramCommand &);
92 
93 	// methods
94 	public:
95 	void undo() override;
96 	void redo() override;
97 
98 	// attributes
99 	private:
100 	/// pasted content
101 	DiagramContent content;
102 	/// diagram content is pasted onto
103 	Diagram *diagram;
104 	/// filter stating what kinds of items should be pasted
105 	int filter;
106 	/// prevent the first call to redo()
107 	bool first_redo;
108 };
109 
110 /**
111 	This command cuts content from a particular diagram.
112 */
113 class CutDiagramCommand : public DeleteQGraphicsItemCommand {
114 	// constructors, destructor
115 	public:
116 	CutDiagramCommand(Diagram *, const DiagramContent &, QUndoCommand * = nullptr);
117 	~CutDiagramCommand() override;
118 	private:
119 	CutDiagramCommand(const CutDiagramCommand &);
120 };
121 
122 /**
123 	This command moves some content on a particular diagram.
124 */
125 class MoveElementsCommand : public QUndoCommand {
126 		// constructors, destructor
127 	public:
128 		MoveElementsCommand(Diagram *, const DiagramContent &, const QPointF &m, QUndoCommand * = nullptr);
129 		~MoveElementsCommand() override;
130 	private:
131 		MoveElementsCommand(const MoveElementsCommand &);
132 
133 		// methods
134 	public:
135 		void undo() override;
136 		void redo() override;
137 		virtual void move(const QPointF &);
138 
139 	private:
140 		void setupAnimation (QObject * target, const QByteArray &propertyName, const QVariant& start, const QVariant& end);
141 
142 	// attributes
143 	private:
144 	/// diagram the movement takes place on.
145 	Diagram *diagram;
146 	/// moved content
147 	DiagramContent content_to_move;
148 	/// applied movement
149 	QPointF movement;
150 	///animation group
151 	QParallelAnimationGroup *m_anim_group;
152 	/// prevent the first call to redo()
153 	bool first_redo;
154 };
155 
156 /**
157 	This command moves text items related to conductors on a particular
158 	diagram.
159 */
160 class MoveConductorsTextsCommand : public QUndoCommand {
161 	// constructors, destructor
162 	public:
163 	MoveConductorsTextsCommand(Diagram *, QUndoCommand * = nullptr);
164 	~MoveConductorsTextsCommand() override;
165 	private:
166 	MoveConductorsTextsCommand(const MoveConductorsTextsCommand &);
167 
168 	// methods
169 	public:
170 	void undo() override;
171 	void redo() override;
172 	virtual void addTextMovement(ConductorTextItem *, const QPointF &, const QPointF &, bool = false);
173 
174 	private:
175 	void regenerateTextLabel();
176 
177 	// attributes
178 	private:
179 	/// diagram the movement takes place on.
180 	Diagram *diagram;
181 	/// text items to be moved
182 	QHash<ConductorTextItem *, QPair<QPointF, bool> > texts_to_move_;
183 	/// prevent the first call to redo()
184 	bool first_redo;
185 };
186 
187 /**
188 	This commad modifies a text item.
189 */
190 class ChangeDiagramTextCommand : public QUndoCommand {
191 	// constructors, destructor
192 	public:
193 	ChangeDiagramTextCommand(DiagramTextItem *, const QString &before, const QString &after, QUndoCommand * = nullptr);
194 	~ChangeDiagramTextCommand() override;
195 	private:
196 	ChangeDiagramTextCommand(const ChangeDiagramTextCommand &);
197 
198 	// methods
199 	public:
200 	void undo() override;
201 	void redo() override;
202 
203 	// attributes
204 	private:
205 	/// modified text item
206 	DiagramTextItem *text_item;
207 	/// former text
208 	QString text_before;
209 	/// new text
210 	QString text_after;
211 	/// prevent the first call to redo()
212 	bool first_redo;
213 	Diagram *diagram;
214 };
215 
216 /**
217 	This command changes a particular conductor.
218 */
219 class ChangeConductorCommand : public QUndoCommand {
220 	// constructors, destructor
221 	public:
222 	ChangeConductorCommand(Conductor *, const ConductorProfile &, const ConductorProfile &, Qt::Corner, QUndoCommand * = nullptr);
223 	~ChangeConductorCommand() override;
224 	private:
225 	ChangeConductorCommand(const ChangeConductorCommand &);
226 
227 	// methods
228 	public:
229 	void undo() override;
230 	void redo() override;
231 	virtual void setConductorTextItemMove(const QPointF &, const QPointF &);
232 
233 	// attributes
234 	private:
235 	/// changed conductor
236 	Conductor *conductor;
237 	/// profile before the change
238 	ConductorProfile old_profile;
239 	/// profile after the change
240 	ConductorProfile new_profile;
241 	/// Path type of the modified conductor
242 	Qt::Corner path_type;
243 	/// position of the text item before the change
244 	QPointF text_pos_before_mov_;
245 	/// position of the text item after the change
246 	QPointF text_pos_after_mov_;
247 	/// prevent the first call to redo()
248 	bool first_redo;
249 	Diagram *diagram;
250 };
251 
252 /**
253 	This command resets conductor paths.
254 */
255 class ResetConductorCommand : public QUndoCommand {
256 	// constructors, destructor
257 	public:
258 	ResetConductorCommand(const QHash<Conductor *, ConductorProfilesGroup> &, QUndoCommand * = nullptr);
259 	~ResetConductorCommand() override;
260 	private:
261 	ResetConductorCommand(const ResetConductorCommand &);
262 
263 	// methods
264 	public:
265 	void undo() override;
266 	void redo() override;
267 
268 	// attributes
269 	private:
270 	/// impacted conductors along with their former profiles
271 	QHash<Conductor *, ConductorProfilesGroup> conductors_profiles;
272 	Diagram *diagram;
273 };
274 
275 
276 
277 /**
278 	This command changes the border properties of a particular diagram.
279 */
280 class ChangeBorderCommand : public QUndoCommand {
281 	// constructors, destructor
282 	public:
283 	ChangeBorderCommand(Diagram *, const BorderProperties &, const BorderProperties &, QUndoCommand * = nullptr);
284 	~ChangeBorderCommand() override;
285 	private:
286 	ChangeBorderCommand(const ChangeBorderCommand &);
287 
288 	// methods
289 	public:
290 	void undo() override;
291 	void redo() override;
292 
293 	// attributes
294 	private:
295 	/// modified diagram
296 	Diagram *diagram;
297 	public:
298 	/// properties before the change
299 	BorderProperties old_properties;
300 	/// properties after the change
301 	BorderProperties new_properties;
302 };
303 
304 #endif
305