1 // container.cpp: Container object
3 // Part of the Architektonas Project
4 // (C) 2011 Underground Software
5 // See the README and GPLv3 files for licensing and warranty information
7 // JLH = James Hammons <jlhamm@acm.org>
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
16 #include "container.h"
19 #include "dimension.h"
23 Container::Container(Vector p1, Object * p/*= NULL*/): Object(p1, p),
24 isTopLevelContainer(false),
25 dragging(false), draggingHandle1(false), draggingHandle2(false)//, needUpdate(false)
33 Container::Container(const Container & copy): Object(copy.position, copy.parent)
35 // Use overloaded assignment operator
42 Container::~Container()
48 // Assignment operator
49 Container & Container::operator=(const Container & from)
51 // Take care of self-assignment
57 // Small problem with this approach: if the copied object goes out of scope,
58 // all of the objects we copied in here will be deleted. D'oh!
59 // For this COPY constructor to be meaningful, we have to actually COPY the
60 // objects in this Container, not just MOVE a copy of the POINTER! D-:
61 std::vector<Object *>::const_iterator i;
63 // for(uint i=0; i<from.objects.size(); i++)
64 for(i=from.objects.begin(); i!=from.objects.end(); i++)
66 // Object * object = from.objects[i];
67 Object * object = (*i)->Copy();
69 // Need to actually COPY the object here, not copy the pointer only!!
70 // (which we do now, above :-P)
72 objects.push_back(object);
79 /*virtual*/ void Container::Draw(Painter * painter)
84 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
86 //printf("Containter::Draw item #%i [%X]...\n", a++, *i);
88 boundary = boundary.united((*i)->Extents());
91 if ((state == OSSelected) || hit)
94 painter->SetPen(QPen(Qt::magenta, 1.0, Qt::DashLine));
96 painter->SetPen(QPen(Qt::blue, 2.0, Qt::DashLine));
98 painter->SetBrush(QBrush(Qt::NoBrush));
99 painter->DrawPaddedRect(boundary);
104 /*virtual*/ Vector Container::Center(void)
111 We need at least *three* handles for this object:
116 We need to think about the intuitive way (if there is any) to grab and
117 manipulate a complex object like this... Need to think, "What should happen when
118 I click here and drag there?"
120 Also: should put the snap logic into the Object base class (as a static method)...
124 // Need to add checking here for clicking on a member of a group (Container),
125 // and checking for if it's a top level container (the DrawingView's document).
127 One approach is to check for the parent of the container: If it's NULL, then it's
128 the DrawingView's document. It might be better, though, to set a boolean like
129 isTopLevelContainer so that we can do things like edit members of a group without
130 having to ungroup them first (like Inkscape).
132 /*virtual*/ bool Container::Collided(Vector point)
134 objectWasDragged = false;
135 // Vector v1 = position - point;
137 bool collision = false;
139 // NOTE that this deletes the object on mouse down instead of mouse up. Have to
140 // check to see how it feels to do it that way...
143 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end();)
145 if ((*i)->Collided(point))
147 printf("Container::Collided: Deleting object ($%X)\n", *i);
148 Object * objectToDelete = *i;
150 delete objectToDelete;
158 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
160 if ((*i)->Collided(point))
165 // We check to see if the container we're trying to access is the
166 // DrawingView's document. If so, we ignore the state of the container.
167 // Otherwise, we care about the state of the container. :-)
168 if (isTopLevelContainer)
172 state = (collision ? OSSelected : OSInactive);
174 if (state == OSSelected)
186 What we need to do is check for whether or not we're a top level container,
187 and override the passing of stuff into the objects held. So here, if we're *NOT*
188 a top level container, instead of passing PointerMoved to our contained objects,
189 we check to see if our bounds are met (for selection rectangle, e.g.).
191 Also, for things like being able to split point's hot spots we need to be able
192 to check for that crap in the top level container. Which means that objects can
193 still know how to move themselves, but they can also defer to their container
194 as well. Which also means that things like HitTest() need to be in the Object
195 class so that we can leverage that stuff here as well.
198 // The TLC is passing all mouse movement here, so we're doing the same here.
199 // Need to adjust all other objects to handle things correctly.
201 // One optimization that will need to be done eventually is to subdivide the screen
202 // into parts and keep subdividing until an acceptable number of objects lie within
203 // the slice. This way, the GUI will still be responsive and *not* have to test
204 // every object for collision.
205 /*virtual*/ void Container::PointerMoved(Vector point)
207 std::vector<Object *>::iterator i;
209 if (!isTopLevelContainer)
211 // check for selection rectangle too
212 if (selectionInProgress)
214 if (selection.contains(Extents()))
222 // No need to do any checking if we're already selected...
223 // if (state == OSSelected)
227 // needUpdate = true;
232 for(i=objects.begin(); i!=objects.end(); i++)
234 if ((*i)->HitTest(point))
236 // state = OSSelected;
243 needUpdate = (oldHit != hit ? true : false);
248 Vector delta = point - oldPoint;
250 for(i=objects.begin(); i!=objects.end(); i++)
251 (*i)->Translate(delta);
260 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
262 // if (objects[i]->GetState() == OSSelected)
263 (*i)->PointerMoved(point);
266 // Generic container doesn't need this???
267 // needUpdate = false;
271 /*virtual*/ void Container::PointerReleased(void)
273 if (!isTopLevelContainer)
280 draggingHandle1 = false;
281 draggingHandle2 = false;
283 // Here we check for just a click: If object was clicked and dragged, then
284 // revert to the old state (OSInactive). Otherwise, keep the new state that
287 Maybe it would be better to just check for "object was dragged" state and not
288 have to worry about keeping track of old states...
290 if (objectWasDragged)
292 //Note that the preceeding is unnecessary for a generic container!
295 // for(int i=0; i<(int)objects.size(); i++)
296 // objects[i]->PointerReleased();
297 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
298 (*i)->PointerReleased();
302 /*virtual*/ bool Container::NeedsUpdate(void)
304 // If this is *not* a top level container, then we treat it as an
306 if (!isTopLevelContainer)
311 // Search through objects for one that needs an update; if one is found,
312 // return immediately.
313 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
315 if ((*i)->NeedsUpdate())
323 /*virtual*/ void Container::Add(Object * object)
325 objects.push_back(object);
326 printf("Container: Added object (=$%X). size = %li\n", object, objects.size());
330 /*virtual*/ QRectF Container::Extents(void)
334 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
335 bounds = bounds.united((*i)->Extents());
341 void Container::Delete(Object * objectToDelete)
343 std::vector<Object *>::iterator i = objects.begin();
345 while (i != objects.end())
347 if (*i == objectToDelete)
350 delete objectToDelete;
359 void Container::DeleteSelectedItems(void)
361 std::vector<Object *>::iterator i = objects.begin();
363 while (i != objects.end())
365 if ((*i)->state == OSSelected)
376 void Container::Clear(void)
378 std::vector<Object *>::iterator i = objects.begin();
380 while (i != objects.end())
382 printf("Container: Deleting object ($%X)...\n", *i);
389 void Container::SelectAll(void)
391 for(unsigned int i=0; i<objects.size(); i++)
392 objects[i]->state = OSSelected;
396 void Container::DeselectAll(void)
398 for(unsigned int i=0; i<objects.size(); i++)
399 objects[i]->state = OSInactive;
403 int Container::ItemsSelected(void)
407 for(uint i=0; i<objects.size(); i++)
408 if (objects[i]->state == OSSelected)
415 Object * Container::SelectedItem(unsigned int index)
417 unsigned int selectedIndex = 0;
419 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
421 if ((*i)->state == OSSelected)
423 if (selectedIndex == index)
434 void Container::MoveContentsTo(Container * newContainer)
437 if (newContainer == NULL)
440 // Shuffle the contents of this container to the new one
441 // for(unsigned int i=0; i<objects.size(); i++)
442 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
444 newContainer->Add(*i);
445 (*i)->Reparent(newContainer);
448 // & clear our vector
453 void Container::MoveSelectedContentsTo(Container * newContainer)
456 if (newContainer == NULL)
459 // Shuffle the contents of this container to the new one
460 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end();)
462 if ((*i)->state != OSSelected)
468 newContainer->Add(*i);
469 (*i)->Reparent(newContainer);
475 void Container::ResizeAllDimensions(double newSize)
477 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
479 if ((*i)->type == OTDimension)
480 ((Dimension *)(*i))->size = newSize;
481 if ((*i)->type == OTContainer)
482 ((Container *)(*i))->ResizeAllDimensions(newSize);
487 /*virtual*/ void Container::Enumerate(FILE * file)
489 // Only put "CONTAINER" markers if *not* the top level container
490 // if (parent != NULL)
491 if (!isTopLevelContainer)
492 fprintf(file, "CONTAINER\n");
494 for(uint i=0; i<objects.size(); i++)
495 objects[i]->Enumerate(file);
497 // if (parent != NULL)
498 if (!isTopLevelContainer)
499 fprintf(file, "ENDCONTAINER\n");