File indexing completed on 2024-05-19 05:42:24

0001 // ct_lvtqtc_undo_move.cpp                                       -*-C++-*-
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #include <ct_lvtqtc_graphicsscene.h>
0021 #include <ct_lvtqtc_graphicsview.h>
0022 #include <ct_lvtqtc_lakosentity.h>
0023 #include <ct_lvtqtc_undo_move.h>
0024 #include <oup/observable_unique_ptr.hpp>
0025 
0026 #include <QPointF>
0027 
0028 using namespace Codethink::lvtqtc;
0029 
0030 struct UndoMove::Private {
0031     oup::observer_ptr<GraphicsScene> scene;
0032     std::string nodeQualifiedName;
0033     QPointF originPos;
0034     QPointF newPos;
0035 };
0036 
0037 UndoMove::UndoMove(GraphicsScene *scene, const std::string& nodeId, const QPointF& origPos, const QPointF& newPos):
0038     d(std::make_unique<UndoMove::Private>(Private{scene->observer_from_this(), nodeId, origPos, newPos}))
0039 {
0040     setText(QObject::tr("Undo move"));
0041 }
0042 
0043 UndoMove::~UndoMove() = default;
0044 
0045 namespace {
0046 void updateScene(LakosEntity *entity)
0047 {
0048     // redraw all edges on the current position.
0049     entity->recursiveEdgeRelayout();
0050 
0051     // triggers a recalculation of the parent's boundaries.
0052     Q_EMIT entity->moving();
0053 
0054     // Tells the system tha the graph updated.
0055     Q_EMIT entity->graphUpdate();
0056 }
0057 } // namespace
0058 
0059 void UndoMove::undo()
0060 {
0061     if (d->scene == nullptr) {
0062         return;
0063     }
0064 
0065     auto *thisEntity = d->scene->entityByQualifiedName(d->nodeQualifiedName);
0066     if (thisEntity) {
0067         thisEntity->setPos(d->originPos);
0068         updateScene(thisEntity);
0069     }
0070 }
0071 
0072 void UndoMove::redo()
0073 {
0074     if (d->scene == nullptr) {
0075         return;
0076     }
0077 
0078     auto *thisEntity = d->scene->entityByQualifiedName(d->nodeQualifiedName);
0079     if (thisEntity) {
0080         thisEntity->setPos(d->newPos);
0081         updateScene(thisEntity);
0082     }
0083 }