]> Shamusworld >> Repos - architektonas/blob - src/container.cpp
Added bounding calcs to Arc, more fixes for groups.
[architektonas] / src / container.cpp
1 // container.cpp: Container object
2 //
3 // Part of the Architektonas Project
4 // (C) 2011 Underground Software
5 // See the README and GPLv3 files for licensing and warranty information
6 //
7 // JLH = James Hammons <jlhamm@acm.org>
8 //
9 // WHO  WHEN        WHAT
10 // ---  ----------  ------------------------------------------------------------
11 // JLH  03/30/2011  Created this file
12 // JLH  06/02/2011  Added code to delete objects in this container when they go
13 //                  out of scope
14 //
15
16 #include "container.h"
17
18 #include <QtGui>
19 #include "dimension.h"
20 #include "painter.h"
21
22
23 Container::Container(Vector p1, Object * p/*= NULL*/): Object(p1, p),
24         isTopLevelContainer(false),
25         dragging(false), draggingHandle1(false), draggingHandle2(false)//, needUpdate(false)
26 {
27         type = OTContainer;
28 }
29
30
31 // Copy constructor
32 Container::Container(const Container & copy): Object(copy.position, copy.parent)
33 {
34         // Use overloaded assignment operator
35         *this = copy;
36         type = OTContainer;
37 }
38
39
40 Container::~Container()
41 {
42         Clear();
43 }
44
45
46 // Assignment operator
47 Container & Container::operator=(const Container & from)
48 {
49         // Take care of self-assignment
50         if (this == &from)
51                 return *this;
52
53         Clear();
54
55         // Small problem with this approach: if the copied object goes out of scope,
56         // all of the objects we copied in here will be deleted. D'oh!
57         for(uint i=0; i<from.objects.size(); i++)
58         {
59                 Object * object = from.objects[i];
60
61                 // Need to copy the object here...
62
63                 objects.push_back(object);
64         }
65
66         return *this;
67 }
68
69
70 /*virtual*/ void Container::Draw(Painter * painter)
71 {
72         QRectF boundary;
73
74         for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
75 //      for(int i=0; i<(int)objects.size(); i++)
76         {
77 #if 0
78 //printf("Container: About to draw (object = $%X)\n", objects[i]);
79                 objects[i]->Draw(painter);
80                 bounds = bounds.united(objects[i].RectangularExtents());
81 #else
82                 (*i)->Draw(painter);
83                 boundary = boundary.united((*i)->Extents());
84 #endif
85         }
86
87         if (state == OSSelected)
88         {
89                 painter->SetPen(QPen(Qt::magenta, 2.0, Qt::DashLine));
90                 painter->SetBrush(QBrush(Qt::NoBrush));
91                 painter->DrawRect(boundary);
92         }
93 }
94
95
96 /*virtual*/ Vector Container::Center(void)
97 {
98         return position;
99 }
100
101 /*
102  We need at least *three* handles for this object:
103  - one for moving
104  - one for resizing
105  - one for rotation
106
107 We need to think about the intuitive way (if there is any) to grab and
108 manipulate a complex object like this... Need to think, "What should happen when
109 I click here and drag there?"
110
111 Also: should put the snap logic into the Object base class (as a static method)...
112 */
113
114
115 // Need to add checking here for clicking on a member of a group (Container),
116 // and checking for if it's a top level container (the DrawingView's document).
117 /*
118 One approach is to check for the parent of the container: If it's NULL, then it's
119 the DrawingView's document. It might be better, though, to set a boolean like
120 isTopLevelContainer so that we can do things like edit members of a group without
121 having to ungroup them first (like Inkscape).
122 */
123 /*virtual*/ bool Container::Collided(Vector point)
124 {
125         objectWasDragged = false;
126 //      Vector v1 = position - point;
127
128         bool collision = false;
129
130         // NOTE that this deletes the object on mouse down instead of mouse up. Have to
131         // check to see how it feels to do it that way...
132         if (deleteActive)
133         {
134                 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end();)
135                 {
136                         if ((*i)->Collided(point))
137                         {
138 printf("Container::Collided: Deleting object ($%X)\n", *i);
139                                 Object * objectToDelete = *i;
140                                 objects.erase(i);
141                                 delete objectToDelete;
142                         }
143                         else
144                                 i++;
145                 }
146         }
147         else
148         {
149                 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
150                 {
151                         if ((*i)->Collided(point))
152                                 collision = true;
153                 }
154         }
155
156         // We check to see if the container we're trying to access is the
157         // DrawingView's document. If so, we ignore the state of the container.
158         // Otherwise, we care about the state of the container. :-)
159         if (isTopLevelContainer)
160                 state = OSInactive;
161         else
162         {
163                 state = (collision ? OSSelected : OSInactive);
164
165                 if (state == OSSelected)
166                         DeselectAll();
167         }
168
169         return collision;
170 }
171
172 /*
173 What we need to do is check for whether or not we're a top level container,
174 and override the passing of stuff into the objects held. So here, if we're *NOT*
175 a top level container, instead of passing PointerMoved to our contained objects,
176 we check to see if our bounds are met (for selection rectangle, e.g.).
177
178 Also, for things like being able to split point's hot spots we need to be able
179 to check for that crap in the top level container. Which means that objects can
180 still know how to move themselves, but they can also defer to their container
181 as well. Which also means that things like HitTest() need to be in the Object
182 class so that we can leverage that stuff here as well.
183 */
184
185 // The TLC is passing all mouse movement here, so we're doing the same here.
186 // Need to adjust all other objects to handle things correctly.
187
188 // One optimization that will need to be done eventually is to subdivide the screen
189 // into parts and keep subdividing until an acceptable number of objects lie within
190 // the slice. This way, the GUI will still be responsive and *not* have to test
191 // every object for collision.
192 /*virtual*/ void Container::PointerMoved(Vector point)
193 {
194 //      objectWasDragged = true;
195 //printf("CONTAINER: PointerMoved()\n");
196
197         for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
198 //      for(int i=0; i<(int)objects.size(); i++)
199         {
200 ////            if (objects[i]->GetState() == OSSelected)
201 //                      objects[i]->PointerMoved(point);
202                 (*i)->PointerMoved(point);
203         }
204
205         // Generic container doesn't need this???
206 //      needUpdate = false;
207 }
208
209
210 /*virtual*/ void Container::PointerReleased(void)
211 {
212         dragging = false;
213         draggingHandle1 = false;
214         draggingHandle2 = false;
215
216         // Here we check for just a click: If object was clicked and dragged, then
217         // revert to the old state (OSInactive). Otherwise, keep the new state that
218         // we set.
219 /*Maybe it would be better to just check for "object was dragged" state and not have to worry
220 about keeping track of old states...
221 */
222         if (objectWasDragged)
223                 state = oldState;
224 //Note that the preceeding is unnecessary for a generic container!
225
226         for(int i=0; i<(int)objects.size(); i++)
227                 objects[i]->PointerReleased();
228 }
229
230
231 /*virtual*/ bool Container::NeedsUpdate(void)
232 {
233         // Search through objects for one that needs an update; if one is found,
234         // return immediately.
235         for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
236         {
237                 if ((*i)->NeedsUpdate())
238                         return true;
239         }
240
241         return false;
242 }
243
244
245 /*virtual*/ void Container::Add(Object * object)
246 {
247         objects.push_back(object);
248 printf("Container: Added object (=$%X). size = %li\n", object, objects.size());
249 }
250
251
252 /*virtual*/ QRectF Container::Extents(void)
253 {
254         QRectF bounds;
255
256         for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
257                 bounds = bounds.united((*i)->Extents());
258
259         return bounds;
260 }
261
262
263 void Container::Delete(Object * objectToDelete)
264 {
265         std::vector<Object *>::iterator i = objects.begin();
266
267         while (i != objects.end())
268         {
269                 if (*i == objectToDelete)
270                 {
271                         objects.erase(i);
272                         delete objectToDelete;
273                         return;
274                 }
275
276                 i++;
277         }
278 }
279
280
281 void Container::DeleteSelectedItems(void)
282 {
283         std::vector<Object *>::iterator i = objects.begin();
284
285         while (i != objects.end())
286         {
287                 if ((*i)->state == OSSelected)
288                 {
289                         delete *i;
290                         objects.erase(i);
291                 }
292                 else
293                         i++;
294         }
295 }
296
297
298 void Container::Clear(void)
299 {
300         std::vector<Object *>::iterator i = objects.begin();
301
302         while (i != objects.end())
303         {
304 printf("Container: Deleting object ($%X)...\n", *i);
305                 delete (*i);
306                 objects.erase(i);
307         }
308 }
309
310
311 void Container::SelectAll(void)
312 {
313         for(unsigned int i=0; i<objects.size(); i++)
314                 objects[i]->state = OSSelected;
315 }
316
317
318 void Container::DeselectAll(void)
319 {
320         for(unsigned int i=0; i<objects.size(); i++)
321                 objects[i]->state = OSInactive;
322 }
323
324
325 int Container::ItemsSelected(void)
326 {
327         int selected = 0;
328
329         for(uint i=0; i<objects.size(); i++)
330                 if (objects[i]->state == OSSelected)
331                         selected++;
332
333         return selected;
334 }
335
336
337 Object * Container::SelectedItem(unsigned int index)
338 {
339         unsigned int selectedIndex = 0;
340
341         for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
342         {
343                 if ((*i)->state == OSSelected)
344                 {
345                         if (selectedIndex == index)
346                                 return *i;
347                         else
348                                 selectedIndex++;
349                 }
350         }
351
352         return NULL;
353 }
354
355
356 void Container::MoveContentsTo(Container * newContainer)
357 {
358         // Sanity check
359         if (newContainer == NULL)
360                 return;
361
362         // Shuffle the contents of this container to the new one
363 //      for(unsigned int i=0; i<objects.size(); i++)
364         for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
365         {
366                 newContainer->Add(*i);
367                 (*i)->Reparent(newContainer);
368         }
369
370         // & clear our vector
371         objects.clear();
372 }
373
374
375 void Container::MoveSelectedContentsTo(Container * newContainer)
376 {
377         // Sanity check
378         if (newContainer == NULL)
379                 return;
380
381         // Shuffle the contents of this container to the new one
382         for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end();)
383         {
384                 if ((*i)->state != OSSelected)
385                 {
386                         i++;
387                         continue;
388                 }
389
390                 newContainer->Add(*i);
391                 (*i)->Reparent(newContainer);
392                 objects.erase(i);
393         }
394 }
395
396
397 /*virtual*/ void Container::Enumerate(FILE * file)
398 {
399         // Only put "CONTAINER" markers if *not* the top level container
400         if (parent != NULL)
401                 fprintf(file, "CONTAINER\n");
402
403         for(uint i=0; i<objects.size(); i++)
404                 objects[i]->Enumerate(file);
405
406         if (parent != NULL)
407                 fprintf(file, "ENDCONTAINER\n");
408 }
409