From 1e016920769ae524575e40b1ec317c700ba0daa3 Mon Sep 17 00:00:00 2001 From: "Armin Le Grand (Allotropia)" Date: Tue, 10 May 2022 15:59:29 +0200 Subject: Advanced Diagram support: UNDO/REDO support for Diagram DataModel Added support for UNDO/REDO for changes in Diagram ModelData. This is currenly applied/used in the DiagramDialog for it's Add/Remove actions (also supports Cancel of that dialog 1st time ever). But it is defined more general to add/support manipulating actions like clone/change_text etc. Also the UI/dialog at he end will not be/stay modal, so this is a test implemenation how to use it. It uses an extract/apply mechanism to get/set the Diagram ModelData at/for the UNDO action. That may be expanded as needed for additional data in he future. It may also be considered to modify the Connection/Point ModelData to shared_ptr internally completely to avoid copying these at all. OTOH it is not that much data to handle at all. Change-Id: I4702ed908b79a476177fe66c0e3284898c6adda5 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134118 Tested-by: Jenkins Reviewed-by: Armin Le Grand --- svx/source/diagram/datamodel.cxx | 27 ++++++++++++++++++++ svx/source/svdraw/svdundo.cxx | 53 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) (limited to 'svx') diff --git a/svx/source/diagram/datamodel.cxx b/svx/source/diagram/datamodel.cxx index fd562645eaa0..e45fe7fb4c3a 100644 --- a/svx/source/diagram/datamodel.cxx +++ b/svx/source/diagram/datamodel.cxx @@ -141,6 +141,33 @@ bool DiagramData::removeNode(const OUString& rNodeId) return true; } +DiagramDataState::DiagramDataState(const Connections& rConnections, const Points& rPoints) +: maConnections(rConnections) +, maPoints(rPoints) +{ +} + +DiagramDataStatePtr DiagramData::extractDiagramDataState() const +{ + // Just copy all Connections && Points. The shared_ptr data in + // Point-entries is no problem, it just continues exiting shared + return std::make_shared< DiagramDataState >(maConnections, maPoints); +} + +void DiagramData::applyDiagramDataState(const DiagramDataStatePtr& rState) +{ + if(rState) + { + maConnections = rState->getConnections(); + maPoints = rState->getPoints(); + + // Reset temporary buffered ModelData association lists & rebuild them + // and the Diagram DataModel. Do that here *immediately* to prevent + // re-usage of potentially invalid Connection/Point objects + buildDiagramDataModel(true); + } +} + void DiagramData::getChildrenString( OUStringBuffer& rBuf, const svx::diagram::Point* pPoint, diff --git a/svx/source/svdraw/svdundo.cxx b/svx/source/svdraw/svdundo.cxx index f1c97c4c16a8..b61ad93fde90 100644 --- a/svx/source/svdraw/svdundo.cxx +++ b/svx/source/svdraw/svdundo.cxx @@ -45,6 +45,7 @@ #include #include #include +#include // iterates over all views and unmarks this SdrObject if it is marked @@ -628,6 +629,52 @@ OUString SdrUndoGeoObj::GetComment() const return ImpGetDescriptionStr(STR_DragMethObjOwn); } +SdrUndoDiagramModelData::SdrUndoDiagramModelData(SdrObject& rNewObj, svx::diagram::DiagramDataStatePtr& rStartState) +: SdrUndoObj(rNewObj) +, m_aStartState(rStartState) +, m_aEndState() +{ + SdrObjGroup* pDiagram(dynamic_cast(&rNewObj)); + + if(pDiagram && pDiagram->isDiagram()) + { + m_aEndState = pDiagram->getDiagramHelper()->extractDiagramDataState(); + } +} + +SdrUndoDiagramModelData::~SdrUndoDiagramModelData() +{ +} + +void SdrUndoDiagramModelData::implUndoRedo(bool bUndo) +{ + if(nullptr == pObj) + return; + + SdrObjGroup* pDiagram(dynamic_cast(pObj)); + + if(nullptr == pDiagram || !pDiagram->isDiagram()) + return; + + pDiagram->getDiagramHelper()->applyDiagramDataState( + bUndo ? m_aStartState : m_aEndState); + pDiagram->getDiagramHelper()->reLayout(*pDiagram); +} + +void SdrUndoDiagramModelData::Undo() +{ + implUndoRedo(true); +} + +void SdrUndoDiagramModelData::Redo() +{ + implUndoRedo(false); +} + +OUString SdrUndoDiagramModelData::GetComment() const +{ + return ImpGetDescriptionStr(STR_DiagramModelDataChange); +} SdrUndoObjList::SdrUndoObjList(SdrObject& rNewObj, bool bOrdNumDirect) : SdrUndoObj(rNewObj) @@ -1676,6 +1723,12 @@ std::unique_ptr SdrUndoFactory::CreateUndoGeoObject( SdrObject& r return std::make_unique( rObject ); } +// Diagram ModelData changes +std::unique_ptr SdrUndoFactory::CreateUndoDiagramModelData( SdrObject& rObject, std::shared_ptr< svx::diagram::DiagramDataState >& rStartState ) +{ + return std::make_unique( rObject, rStartState ); +} + std::unique_ptr SdrUndoFactory::CreateUndoAttrObject( SdrObject& rObject, bool bStyleSheet1, bool bSaveText ) { return std::make_unique( rObject, bStyleSheet1, bSaveText ); -- cgit