]> Shamusworld >> Repos - architektonas/blob - src/object.cpp
Initial stab at getting Circle to respond to selection rectangle.
[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), parent(passedInParent),
44         state(OSInactive), oldState(OSInactive), needUpdate(false)//, attachedDimension(0)
45 {
46 }
47
48
49 Object::~Object()
50 {
51 printf("Object: Destroyed!\n");
52         for(uint i=0; i<connected.size(); i++)
53                 connected[i].object->DisconnectAll(this);
54 }
55
56
57 /*virtual*/ void Object::Draw(Painter *)
58 {
59 }
60
61
62 /*virtual*/ Vector Object::Center(void)
63 {
64         return Vector();
65 }
66
67
68 /*virtual*/ bool Object::Collided(Vector)
69 {
70         return false;
71 }
72
73
74 /*virtual*/ void Object::PointerMoved(Vector)
75 {
76 }
77
78
79 /*virtual*/ void Object::PointerReleased(void)
80 {
81 }
82
83
84 /*virtual*/ bool Object::NeedsUpdate(void)
85 {
86         return needUpdate;
87 }
88
89
90 // This is intended to be overridden by the Container class, for object morphing
91 /*virtual*/ void Object::Transmute(Object *, Object *)
92 {
93 }
94
95
96 /*virtual*/ Object * Object::GetParent(void)
97 {
98         return parent;
99 }
100
101
102 /*virtual*/ void Object::Add(Object *)
103 {
104 }
105
106
107 // This returns a pointer to the point passed in, if it coincides. Otherwise returns NULL.
108 /*virtual*/ Vector * Object::GetPointAt(Vector)
109 {
110         return 0;
111 }
112
113
114 // This is meant for writing object data to a file.
115 /*virtual*/ void Object::Enumerate(FILE *)
116 {
117 }
118
119
120 /*virtual*/ Object * Object::Copy(void)
121 {
122         return new Object(position, parent);
123 }
124
125
126 // This returns a point on the object at 'parameter', which is between 0 and 1.
127 // Default is to return the object's position.
128 /*virtual*/ Vector Object::GetPointAtParameter(double)
129 {
130         return position;
131 }
132
133
134 // Since these functions are pretty much non-object specific, we can implement
135 // them here. :-)
136 /*virtual*/ void Object::Connect(Object * obj, double parameter)
137 {
138         connected.push_back(Connection(obj, parameter));
139 }
140
141
142 /*virtual*/ void Object::Disconnect(Object * obj, double parameter)
143 {
144         for(uint i=0; i<connected.size(); i++)
145         {
146                 if (connected[i].object == obj && connected[i].t == parameter)
147                 {
148                         connected.erase(connected.begin() + i);
149                         return;
150                 }
151         }
152 }
153
154
155 /*virtual*/ void Object::DisconnectAll(Object * obj)
156 {
157         // According the std::vector docs, only items at position i and beyond are
158         // invalidated, everything before i is still valid. So we use that here.
159         for(uint i=0; i<connected.size();)
160         {
161                 // If we found our object, erase it from the vector but don't advance
162                 // the iterator. Otherwise, advance the iterator. :-)
163                 if (connected[i].object == obj)
164                         connected.erase(connected.begin() + i);
165                 else
166                         i++;
167         }
168 }
169
170
171 /*virtual*/ QRectF Object::Extents(void)
172 {
173         // Generic object returns an empty rect...
174         return QRectF();
175 }
176
177
178 #if 0
179 /*virtual*/ ObjectType Object::Type(void)
180 {
181         return OTObject;
182 }
183 #endif
184
185
186 ObjectState Object::GetState(void)
187 {
188         return state;
189 }
190
191
192 void Object::Reparent(Object * newParent)
193 {
194         parent = newParent;
195 }
196
197
198 /*Dimension * Object::GetAttachedDimension(void)
199 {
200         return attachedDimension;
201 }*/
202
203
204 // Class methods...
205
206 void Object::SetFixedAngle(bool state/*= true*/)
207 {
208         fixedAngle = state;
209 }
210
211
212 void Object::SetFixedLength(bool state/*= true*/)
213 {
214         fixedLength = state;
215 }
216
217
218 void Object::SetFont(QFont * f)
219 {
220         font = f;
221 }
222
223
224 void Object::SetViewportHeight(int height)
225 {
226         viewportHeight = height;
227 }
228
229
230 void Object::SetDeleteActive(bool state/*= true*/)
231 {
232         deleteActive = state;
233 }
234
235
236 void Object::SetDimensionActive(bool state/*= true*/)
237 {
238         dimensionActive = state;
239 }
240
241
242 void Object::SetSnapMode(bool state/*= true*/)
243 {
244         snapToGrid = state;
245 }