File indexing completed on 2024-12-15 04:01:01

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include "structure_commands.hpp"
0008 #include "shape_commands.hpp"
0009 
0010 using namespace glaxnimate;
0011 
0012 command::DeferredCommandBase::~DeferredCommandBase() = default;
0013 
0014 bool command::DeferredCommandBase::has_action() const
0015 {
0016     return bool(d);
0017 }
0018 
0019 void command::DeferredCommandBase::redo()
0020 {
0021     if ( d )
0022         d->redo();
0023 }
0024 
0025 void command::DeferredCommandBase::undo()
0026 {
0027     if ( d )
0028         d->undo();
0029 }
0030 
0031 
0032 bool command::ReorderCommand::resolve_position(model::ShapeElement* shape, int& new_position)
0033 {
0034     if ( new_position < 0 )
0035     {
0036         switch ( command::ReorderCommand::SpecialPosition(new_position) )
0037         {
0038             case command::ReorderCommand::MoveUp:
0039                 new_position = shape->position() + 1;
0040                 break;
0041             case command::ReorderCommand::MoveDown:
0042                 new_position = shape->position() - 1;
0043                 break;
0044             case command::ReorderCommand::MoveTop:
0045                 new_position = shape->owner()->size() - 1;
0046                 break;
0047             case command::ReorderCommand::MoveBottom:
0048                 new_position = 0;
0049                 break;
0050         }
0051     }
0052 
0053     if ( new_position == shape->position() || new_position < 0 || new_position >= shape->owner()->size() )
0054         return false;
0055     return true;
0056 }
0057 
0058 std::unique_ptr<QUndoCommand> reorder_shape(model::ShapeElement* shape, int new_position)
0059 {
0060     if ( !command::ReorderCommand::resolve_position(shape, new_position) )
0061         return {};
0062     return std::make_unique<command::MoveShape>(shape, shape->owner(), shape->owner(), new_position);
0063 }
0064 
0065 command::ReorderCommand::ReorderCommand(model::ShapeElement* shape, int new_position)
0066     : DeferredCommandBase(name(shape))
0067 {
0068     d = reorder_shape(shape, new_position);
0069 }
0070 
0071 QString command::ReorderCommand::name(model::DocumentNode* node)
0072 {
0073     return i18n("Move %1", node->object_name());
0074 }
0075