From f3110724be7ad4d4e8c6b97d350ff8af7fbf3799 Mon Sep 17 00:00:00 2001 From: Shamus Hammons Date: Fri, 5 Jul 2013 21:07:20 -0500 Subject: [PATCH] Fixed delete key to delete selected objects. --- src/applicationwindow.cpp | 21 ++++-- src/container.cpp | 142 ++++++++++---------------------------- src/container.h | 1 + 3 files changed, 54 insertions(+), 110 deletions(-) diff --git a/src/applicationwindow.cpp b/src/applicationwindow.cpp index a9108eb..77767f0 100644 --- a/src/applicationwindow.cpp +++ b/src/applicationwindow.cpp @@ -197,7 +197,17 @@ void ApplicationWindow::FixLength(void) // We want certain tools to be exclusive, and this approach isn't working correctly... void ApplicationWindow::DeleteTool(void) { - + // For this tool, we check first to see if anything is selected. If so, we + // delete those and *don't* select the delete tool. + if (drawing->document.ItemsSelected() > 0) + { + drawing->document.DeleteSelectedItems(); + drawing->update(); + deleteAct->setChecked(false); + return; + } + + // Otherwise, toggle the state of the tool ClearUIToolStatesExcept(deleteAct); SetInternalToolStates(); } @@ -382,10 +392,13 @@ void ApplicationWindow::Settings(void) } +// +// Group a bunch of selected objects (which can include other groups) together +// or ungroup a selected group. +// void ApplicationWindow::HandleGrouping(void) { - // Group a bunch of selected objects together or ungroup a selected group. - + // If nothing selected, do nothing if (drawing->document.ItemsSelected() == 0) { statusBar()->showMessage(tr("No objects selected to make a group from.")); @@ -451,7 +464,7 @@ void ApplicationWindow::CreateActions(void) QIcon(":/res/fix-length.png"), QKeySequence(tr("F,L")), true); connect(fixLengthAct, SIGNAL(triggered()), this, SLOT(FixLength())); - deleteAct = CreateAction(tr("&Delete"), tr("Delete Object"), tr("Deletes selected objects."), QIcon(":/res/delete-tool.png"), QKeySequence(), true); + deleteAct = CreateAction(tr("&Delete"), tr("Delete Object"), tr("Deletes selected objects."), QIcon(":/res/delete-tool.png"), QKeySequence(tr("Delete")), true); connect(deleteAct, SIGNAL(triggered()), this, SLOT(DeleteTool())); addDimensionAct = CreateAction(tr("Add &Dimension"), tr("Add Dimension"), tr("Adds a dimension to the drawing."), QIcon(":/res/dimension-tool.png"), QKeySequence("D,I"), true); diff --git a/src/container.cpp b/src/container.cpp index 7263663..cd9b4f5 100644 --- a/src/container.cpp +++ b/src/container.cpp @@ -114,111 +114,38 @@ Also: should put the snap logic into the Object base class (as a static method). /*virtual*/ bool Container::Collided(Vector point) { objectWasDragged = false; - Vector v1 = position - point; +// Vector v1 = position - point; -#if 0 - if (state == OSInactive) - { -//printf("Circle: pp = %lf, length = %lf, distance = %lf\n", parameterizedPoint, lineSegment.Magnitude(), distance); -//printf(" v1.Magnitude = %lf, v2.Magnitude = %lf\n", v1.Magnitude(), v2.Magnitude()); -//printf(" point = %lf,%lf,%lf; p1 = %lf,%lf,%lf; p2 = %lf,%lf,%lf\n", point.x, point.y, point.z, position.x, position.y, position.z, endpoint.x, endpoint.y, endpoint.z); -//printf(" \n", ); -//How to translate this into pixels from Document space??? -//Maybe we need to pass a scaling factor in here from the caller? That would make sense, as -//the caller knows about the zoom factor and all that good kinda crap - if (v1.Magnitude() < 10.0) - { - oldState = state; - state = OSSelected; - oldPoint = position; //maybe "position"? - draggingHandle1 = true; - return true; - } - else if ((v1.Magnitude() < radius + 2.0) && (v1.Magnitude() > radius - 2.0)) - { - oldState = state; - state = OSSelected; - oldPoint = point; - dragging = true; - return true; - } - } - else if (state == OSSelected) - { - // Here we test for collision with handles as well! (SOON!) -/* -Like so: - if (v1.Magnitude() < 2.0) // Handle #1 - else if (v2.Magnitude() < 2.0) // Handle #2 -*/ - if ((v1.Magnitude() < radius + 2.0) && (v1.Magnitude() > radius - 2.0)) - { - oldState = state; -// state = OSInactive; - oldPoint = point; - dragging = true; - return true; - } - } -#else bool collision = false; // NOTE that this deletes the object on mouse down instead of mouse up. Have to // check to see how it feels to do it that way... if (deleteActive) { - for(int i=0; i<(int)objects.size(); i++) + for(std::vector::iterator i=objects.begin(); i!=objects.end();) { - if (objects[i]->Collided(point)) + if ((*i)->Collided(point)) { -#if 0 -Dimension * dimension = objects[i]->GetAttachedDimension(); -#endif - Object * objectToDelete = objects[i]; - objects.erase(objects.begin() + i); // Calls the destructor, (deletes the object, I presume... O_o) [NOPE! SURE DOESN'T!] +printf("Container::Collided: Deleting object ($%X)\n", *i); + Object * objectToDelete = *i; + objects.erase(i); delete objectToDelete; - -// If this object had an attached dimension, reattach it to another object, if any... -// The only problem with that approach is if the object it gets attached to is deleted, -// it will call the dimension to use a NULL pointer and BLAMMO -#if 0 -if (dimension) -{ - Vector p1 = dimension->GetPoint1(); - Vector p2 = dimension->GetPoint2(); - for(int j=0; j<(int)objects.size(); j++) - { - Vector * objectP1 = objects[i]->GetPointAt(p1); - Vector * objectP2 = objects[i]->GetPointAt(p2); - - if (objectP1) - dimension->SetPoint1(objectP1); - - if (objectP2) - dimension->SetPoint2(objectP2); - } -} -#endif - // This only allows deleting objects one at a time... - break; - // however, this way of doing things could be problematic if we don't - // delete one at a time... Need to come up with a better approach. } + else + i++; } } else { - for(int i=0; i<(int)objects.size(); i++) + for(std::vector::iterator i=objects.begin(); i!=objects.end(); i++) { - if (objects[i]->Collided(point)) + if ((*i)->Collided(point)) collision = true; } } -#endif // Do we decouple the state of the generic container from the objects inside??? Mebbe. state = OSInactive; -// return false; return collision; } @@ -298,37 +225,42 @@ printf("Container: Added object (=$%X). size = %li\n", object, objects.size()); void Container::Delete(Object * objectToDelete) { -#if 0 - //this is wrong - for(unsigned int i=0; i::iterator i = objects.begin(); + + while (i != objects.end()) { - if (objects[i] == objectToDelete) + if (*i == objectToDelete) { objects.erase(i); delete objectToDelete; return; } + + i++; } -#else +} + + +void Container::DeleteSelectedItems(void) +{ std::vector::iterator i = objects.begin(); while (i != objects.end()) { - if (*i == objectToDelete) + if ((*i)->state == OSSelected) { + delete *i; objects.erase(i); - delete objectToDelete; - return; } - - i++; + else + i++; } -#endif } void Container::Clear(void) { +#if 0 // No memory leaks! while (objects.size() > 0) { @@ -336,6 +268,16 @@ printf("Container: Deleting object ($%X)...\n", objects[0]); delete objects[0]; objects.erase(objects.begin()); } +#else + std::vector::iterator i = objects.begin(); + + while (i != objects.end()) + { +printf("Container: Deleting object ($%X)...\n", *i); + delete (*i); + objects.erase(i); + } +#endif } @@ -365,20 +307,8 @@ int Container::ItemsSelected(void) } -/*ObjectType Container::SelectedItemType(unsigned int index) -{ - if (index >= objects.size()) - return OTNone; - - return objects[index]->Type(); -}*/ - - Object * Container::SelectedItem(unsigned int index) { -// if (index >= objects.size()) -// return NULL; - unsigned int selectedIndex = 0; for(std::vector::iterator i=objects.begin(); i!=objects.end(); i++) diff --git a/src/container.h b/src/container.h index a532289..8744f64 100644 --- a/src/container.h +++ b/src/container.h @@ -23,6 +23,7 @@ class Container: public Object virtual void Enumerate(FILE *); // virtual ObjectType Type(void); void Delete(Object *); + void DeleteSelectedItems(void); void Clear(void); void SelectAll(void); void DeselectAll(void); -- 2.37.2