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)
32 Container::Container(const Container & copy): Object(copy.position, copy.parent)
34 // Use overloaded assignment operator
40 Container::~Container()
46 // Assignment operator
47 Container & Container::operator=(const Container & from)
49 // Take care of self-assignment
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++)
59 Object * object = from.objects[i];
61 // Need to copy the object here...
63 objects.push_back(object);
70 /*virtual*/ void Container::Draw(Painter * painter)
74 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
75 // for(int i=0; i<(int)objects.size(); i++)
78 //printf("Container: About to draw (object = $%X)\n", objects[i]);
79 objects[i]->Draw(painter);
80 bounds = bounds.united(objects[i].RectangularExtents());
83 boundary = boundary.united((*i)->Extents());
87 if (state == OSSelected)
89 painter->SetPen(QPen(Qt::magenta, 2.0, Qt::DashLine));
90 painter->SetBrush(QBrush(Qt::NoBrush));
91 painter->DrawRect(boundary);
96 /*virtual*/ Vector Container::Center(void)
102 We need at least *three* handles for this object:
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?"
111 Also: should put the snap logic into the Object base class (as a static method)...
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).
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).
123 /*virtual*/ bool Container::Collided(Vector point)
125 objectWasDragged = false;
126 // Vector v1 = position - point;
128 bool collision = false;
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...
134 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end();)
136 if ((*i)->Collided(point))
138 printf("Container::Collided: Deleting object ($%X)\n", *i);
139 Object * objectToDelete = *i;
141 delete objectToDelete;
149 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
151 if ((*i)->Collided(point))
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)
163 state = (collision ? OSSelected : OSInactive);
165 if (state == OSSelected)
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.).
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.
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.
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)
194 if (!isTopLevelContainer)
196 // check for selection rectangle too
201 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
203 if ((*i)->HitTest(point))
214 // objectWasDragged = true;
215 //printf("CONTAINER: PointerMoved()\n");
217 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
218 // for(int i=0; i<(int)objects.size(); i++)
220 //// if (objects[i]->GetState() == OSSelected)
221 // objects[i]->PointerMoved(point);
222 (*i)->PointerMoved(point);
225 // Generic container doesn't need this???
226 // needUpdate = false;
230 /*virtual*/ void Container::PointerReleased(void)
233 draggingHandle1 = false;
234 draggingHandle2 = false;
236 // Here we check for just a click: If object was clicked and dragged, then
237 // revert to the old state (OSInactive). Otherwise, keep the new state that
239 /*Maybe it would be better to just check for "object was dragged" state and not have to worry
240 about keeping track of old states...
242 if (objectWasDragged)
244 //Note that the preceeding is unnecessary for a generic container!
246 for(int i=0; i<(int)objects.size(); i++)
247 objects[i]->PointerReleased();
251 /*virtual*/ bool Container::NeedsUpdate(void)
253 // If this is *not* a top level container, then we treat it as an
255 if (!isTopLevelContainer)
260 // Search through objects for one that needs an update; if one is found,
261 // return immediately.
262 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
264 if ((*i)->NeedsUpdate())
272 /*virtual*/ void Container::Add(Object * object)
274 objects.push_back(object);
275 printf("Container: Added object (=$%X). size = %li\n", object, objects.size());
279 /*virtual*/ QRectF Container::Extents(void)
283 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
284 bounds = bounds.united((*i)->Extents());
290 void Container::Delete(Object * objectToDelete)
292 std::vector<Object *>::iterator i = objects.begin();
294 while (i != objects.end())
296 if (*i == objectToDelete)
299 delete objectToDelete;
308 void Container::DeleteSelectedItems(void)
310 std::vector<Object *>::iterator i = objects.begin();
312 while (i != objects.end())
314 if ((*i)->state == OSSelected)
325 void Container::Clear(void)
327 std::vector<Object *>::iterator i = objects.begin();
329 while (i != objects.end())
331 printf("Container: Deleting object ($%X)...\n", *i);
338 void Container::SelectAll(void)
340 for(unsigned int i=0; i<objects.size(); i++)
341 objects[i]->state = OSSelected;
345 void Container::DeselectAll(void)
347 for(unsigned int i=0; i<objects.size(); i++)
348 objects[i]->state = OSInactive;
352 int Container::ItemsSelected(void)
356 for(uint i=0; i<objects.size(); i++)
357 if (objects[i]->state == OSSelected)
364 Object * Container::SelectedItem(unsigned int index)
366 unsigned int selectedIndex = 0;
368 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
370 if ((*i)->state == OSSelected)
372 if (selectedIndex == index)
383 void Container::MoveContentsTo(Container * newContainer)
386 if (newContainer == NULL)
389 // Shuffle the contents of this container to the new one
390 // for(unsigned int i=0; i<objects.size(); i++)
391 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end(); i++)
393 newContainer->Add(*i);
394 (*i)->Reparent(newContainer);
397 // & clear our vector
402 void Container::MoveSelectedContentsTo(Container * newContainer)
405 if (newContainer == NULL)
408 // Shuffle the contents of this container to the new one
409 for(std::vector<Object *>::iterator i=objects.begin(); i!=objects.end();)
411 if ((*i)->state != OSSelected)
417 newContainer->Add(*i);
418 (*i)->Reparent(newContainer);
424 /*virtual*/ void Container::Enumerate(FILE * file)
426 // Only put "CONTAINER" markers if *not* the top level container
428 fprintf(file, "CONTAINER\n");
430 for(uint i=0; i<objects.size(); i++)
431 objects[i]->Enumerate(file);
434 fprintf(file, "ENDCONTAINER\n");