]> Shamusworld >> Repos - architektonas/blob - src/object.cpp
404d30576113a97ad9516b04677d087f81ad4346
[architektonas] / src / object.cpp
1 //
2 // object.cpp: Base class for all CAD objects
3 //
4 // Part of the Architektonas Project
5 // (C) 2011 Underground Software
6 // See the README and GPLv3 files for licensing and warranty information
7 //
8 // JLH = James Hammons <jlhamm@acm.org>
9 //
10 // WHO  WHEN        WHAT
11 // ---  ----------  ------------------------------------------------------------
12 // JLH  03/22/2011  Created this file
13 // JLH  04/01/2011  Added constructor to allow derived objects to have empty
14 //                  constructor bodies, added state querying
15 // JLH  04/02/2001  Added static methods for global states (fixed angle, etc)
16 //
17
18 #include "object.h"
19 #include <stdlib.h>
20
21 // Initialize static variables
22 bool Object::fixedAngle = false;
23 bool Object::fixedLength = false;
24 QFont * Object::font = 0;
25 int Object::viewportHeight = 0;
26 bool Object::deleteActive = false;
27 bool Object::dimensionActive = false;
28 bool Object::snapToGrid = true;
29 //snapToPoints all well here?
30 bool Object::ignoreClicks = false;
31 bool Object::dontMove = false;
32 bool Object::selectionInProgress = false;
33 QRectF Object::selection;
34
35
36 Object::Object(): position(Vector(0, 0)), parent(0), type(OTObject),
37         state(OSInactive), oldState(OSInactive), needUpdate(false)
38         //, attachedDimension(0)
39 {
40 }
41
42
43 Object::Object(Vector v,  Object * passedInParent/*= 0*/): position(v),
44         parent(passedInParent), state(OSInactive), oldState(OSInactive),
45         needUpdate(false)//, attachedDimension(0)
46 {
47 }
48
49
50 Object::~Object()
51 {
52 printf("Object: Destroyed!\n");
53         for(uint i=0; i<connected.size(); i++)
54                 connected[i].object->DisconnectAll(this);
55 }
56
57
58 /*virtual*/ void Object::Draw(Painter *)
59 {
60 }
61
62
63 /*virtual*/ Vector Object::Center(void)
64 {
65         return Vector();
66 }
67
68
69 /*virtual*/ bool Object::Collided(Vector)
70 {
71         return false;
72 }
73
74
75 /*virtual*/ void Object::PointerMoved(Vector)
76 {
77 }
78
79
80 /*virtual*/ void Object::PointerReleased(void)
81 {
82 }
83
84
85 /*virtual*/ bool Object::NeedsUpdate(void)
86 {
87         return needUpdate;
88 }
89
90
91 /*virtual*/ bool Object::HitTest(Point)
92 {
93         return false;
94 }
95
96
97 // This is intended to be overridden by the Container class, for object morphing
98 /*virtual*/ void Object::Transmute(Object *, Object *)
99 {
100 }
101
102
103 /*virtual*/ Object * Object::GetParent(void)
104 {
105         return parent;
106 }
107
108
109 /*virtual*/ void Object::Add(Object *)
110 {
111 }
112
113
114 // This returns a pointer to the point passed in, if it coincides. Otherwise returns NULL.
115 /*virtual*/ Vector * Object::GetPointAt(Vector)
116 {
117         return 0;
118 }
119
120
121 // This is meant for writing object data to a file.
122 /*virtual*/ void Object::Enumerate(FILE *)
123 {
124 }
125
126
127 /*virtual*/ Object * Object::Copy(void)
128 {
129         return new Object(position, parent);
130 }
131
132
133 // This returns a point on the object at 'parameter', which is between 0 and 1.
134 // Default is to return the object's position.
135 /*virtual*/ Vector Object::GetPointAtParameter(double)
136 {
137         return position;
138 }
139
140
141 // Since these functions are pretty much non-object specific, we can implement
142 // them here. :-)
143 /*virtual*/ void Object::Connect(Object * obj, double parameter)
144 {
145         connected.push_back(Connection(obj, parameter));
146 }
147
148
149 /*virtual*/ void Object::Disconnect(Object * obj, double parameter)
150 {
151 #if 0
152         for(uint i=0; i<connected.size(); i++)
153         {
154                 if (connected[i].object == obj && connected[i].t == parameter)
155                 {
156                         connected.erase(connected.begin() + i);
157                         return;
158                 }
159         }
160 #else
161         std::vector<Connection>::iterator i;
162
163         for(i=connected.begin(); i!=connected.end(); i++)
164         {
165                 if (((*i).object == obj) && ((*i).t == parameter))
166                 {
167                         connected.erase(i);
168                         return;
169                 }
170         }
171 #endif
172 }
173
174
175 /*virtual*/ void Object::DisconnectAll(Object * obj)
176 {
177 #if 0
178         // According the std::vector docs, only items at position i and beyond are
179         // invalidated, everything before i is still valid. So we use that here.
180         for(uint i=0; i<connected.size();)
181         {
182                 // If we found our object, erase it from the vector but don't advance
183                 // the iterator. Otherwise, advance the iterator. :-)
184                 if (connected[i].object == obj)
185                         connected.erase(connected.begin() + i);
186                 else
187                         i++;
188         }
189 #else
190         std::vector<Connection>::iterator i;
191
192         for(i=connected.begin(); i!=connected.end(); )
193         {
194                 if ((*i).object == obj)
195                         connected.erase(i);
196                 else
197                         i++;
198         }
199 #endif
200 }
201
202
203 /*virtual*/ QRectF Object::Extents(void)
204 {
205         // Generic object returns an empty rect...
206         return QRectF();
207 }
208
209
210 #if 0
211 /*virtual*/ ObjectType Object::Type(void)
212 {
213         return OTObject;
214 }
215 #endif
216
217
218 /*virtual*/ void Object::Translate(Vector amount)
219 {
220         position += amount;
221 }
222
223
224 /*virtual*/ void Object::Rotate(Vector, double)
225 {
226 }
227
228
229 /*virtual*/ void Object::Scale(Vector, double)
230 {
231 }
232
233
234 ObjectState Object::GetState(void)
235 {
236         return state;
237 }
238
239
240 void Object::Reparent(Object * newParent)
241 {
242         parent = newParent;
243 }
244
245
246 /*Dimension * Object::GetAttachedDimension(void)
247 {
248         return attachedDimension;
249 }*/
250
251
252 // Class methods...
253
254 void Object::SetFixedAngle(bool state/*= true*/)
255 {
256         fixedAngle = state;
257 }
258
259
260 void Object::SetFixedLength(bool state/*= true*/)
261 {
262         fixedLength = state;
263 }
264
265
266 void Object::SetFont(QFont * f)
267 {
268         font = f;
269 }
270
271
272 void Object::SetViewportHeight(int height)
273 {
274         viewportHeight = height;
275 }
276
277
278 void Object::SetDeleteActive(bool state/*= true*/)
279 {
280         deleteActive = state;
281 }
282
283
284 void Object::SetDimensionActive(bool state/*= true*/)
285 {
286         dimensionActive = state;
287 }
288
289
290 void Object::SetSnapMode(bool state/*= true*/)
291 {
292         snapToGrid = state;
293 }