Skip to content

Commit

Permalink
Map: Create single undo step in importMap()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg0yt committed Oct 30, 2018
1 parent 19a3dfc commit a398980
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/core/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,7 @@ QHash<const Symbol*, Symbol*> Map::importMap(
// Import parts like this:
// - if the other map has only one part, import it into the current part
// - else check if there is already a part with an equal name for every part to import and import into this part if found, else create a new part
auto* undo_step = new CombinedUndoStep(this);
for (const auto* part_to_import : imported_map.parts)
{
MapPart* dest_part = nullptr;
Expand All @@ -993,7 +994,7 @@ QHash<const Symbol*, Symbol*> Map::importMap(
// Import as new part
dest_part = new MapPart(part_to_import->getName(), this);
addPart(dest_part, 0);
push(new MapPartUndoStep(this, MapPartUndoStep::RemoveMapPart, 0));
undo_step->push(new MapPartUndoStep(this, MapPartUndoStep::RemoveMapPart, 0));
}
}

Expand All @@ -1002,12 +1003,16 @@ QHash<const Symbol*, Symbol*> Map::importMap(
current_part_index = std::size_t(findPartIndex(dest_part));

bool select_and_center_objects = dest_part == temp_current_part;
dest_part->importPart(part_to_import, symbol_map, transform, select_and_center_objects);
if (select_and_center_objects)
ensureVisibilityOfSelectedObjects(Map::FullVisibility);
if (auto import_undo = dest_part->importPart(part_to_import, symbol_map, transform, select_and_center_objects))
{
undo_step->push(import_undo.release());
if (select_and_center_objects)
ensureVisibilityOfSelectedObjects(Map::FullVisibility);
}

current_part_index = std::size_t(findPartIndex(temp_current_part));
}
push(undo_step);
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/core/map_part.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ bool MapPart::deleteObject(Object* object, bool remove_only)
return false;
}

void MapPart::importPart(const MapPart* other, const QHash<const Symbol*, Symbol*>& symbol_map, const QTransform& transform, bool select_new_objects)
std::unique_ptr<UndoStep> MapPart::importPart(const MapPart* other, const QHash<const Symbol*, Symbol*>& symbol_map, const QTransform& transform, bool select_new_objects)
{
if (other->getNumObjects() == 0)
return;
return {};

bool first_objects = map->getNumObjects() == 0;
auto undo_step = new DeleteObjectsUndoStep(map);
Expand All @@ -242,7 +242,6 @@ void MapPart::importPart(const MapPart* other, const QHash<const Symbol*, Symbol
map->addObjectToSelection(new_object, false);
}

map->push(undo_step);
map->setObjectsDirty();
if (select_new_objects)
{
Expand All @@ -252,6 +251,8 @@ void MapPart::importPart(const MapPart* other, const QHash<const Symbol*, Symbol
}
if (first_objects)
map->updateAllMapWidgets();

return std::unique_ptr<UndoStep>{undo_step};
}

void MapPart::findObjectsAt(
Expand Down
4 changes: 3 additions & 1 deletion src/core/map_part.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <cstddef>
#include <functional>
#include <memory>
#include <vector>
#include <utility>

Expand All @@ -44,6 +45,7 @@ class MapCoordF;
class Object;
class Symbol;
using SymbolDictionary = QHash<QString, Symbol*>; // from symbol.h
class UndoStep;


using SelectionInfoVector = std::vector<std::pair<int, Object*>> ;
Expand Down Expand Up @@ -180,7 +182,7 @@ friend class OCAD8FileImport;
* Uses symbol_map to replace all symbols contained there.
* No replacement is done for symbols which are not in the symbol_map.
*/
void importPart(const MapPart* other, const QHash<const Symbol*, Symbol*>& symbol_map,
std::unique_ptr<UndoStep> importPart(const MapPart* other, const QHash<const Symbol*, Symbol*>& symbol_map,
const QTransform& transform, bool select_new_objects);


Expand Down

0 comments on commit a398980

Please sign in to comment.