Warning, file /graphics/glaxnimate/src/gui/item_models/document_model_base.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 "document_model_base.hpp"
0008 
0009 #include <QMimeData>
0010 #include <QDataStream>
0011 #include <QSet>
0012 
0013 #include "command/object_list_commands.hpp"
0014 #include "command/undo_macro_guard.hpp"
0015 
0016 #include "drag_data.hpp"
0017 
0018 using namespace glaxnimate::gui;
0019 using namespace glaxnimate;
0020 
0021 Qt::DropActions item_models::DocumentModelBase::supportedDropActions() const
0022 {
0023     return Qt::MoveAction;
0024 }
0025 
0026 QStringList item_models::DocumentModelBase::mimeTypes() const
0027 {
0028     return {"application/x.glaxnimate-node-uuid"};
0029 }
0030 
0031 QMimeData * item_models::DocumentModelBase::mimeData(const QModelIndexList& indexes) const
0032 {
0033     if ( !document() )
0034         return nullptr;
0035 
0036     QMimeData *data = new QMimeData();
0037     item_models::DragEncoder encoder;
0038     for ( const auto& index : indexes )
0039         encoder.add_node(node(index));
0040 
0041     data->setData("application/x.glaxnimate-node-uuid", encoder.data());
0042     return data;
0043 }
0044 
0045 std::pair<model::VisualNode *, int> item_models::DocumentModelBase::drop_position(const QModelIndex& parent, int row, int column) const
0046 {
0047     Q_UNUSED(column);
0048     return {visual_node(parent), row};
0049 }
0050 
0051 
0052 std::tuple<model::VisualNode *, int, model::ShapeListProperty*> item_models::DocumentModelBase::cleaned_drop_position(const QMimeData* data, Qt::DropAction action, const QModelIndex& parent, int row, int column) const
0053 {
0054     if ( !data || action != Qt::MoveAction || !document() )
0055         return {};
0056 
0057     if ( !data->hasFormat("application/x.glaxnimate-node-uuid") )
0058         return {};
0059 
0060     auto position = drop_position(parent, row, column);
0061     model::VisualNode* parent_node = position.first;
0062 
0063     if ( !parent_node )
0064         return {};
0065 
0066     auto dest = static_cast<model::ShapeListProperty*>(parent_node->get_property("shapes"));
0067     if ( !dest )
0068         return {};
0069 
0070     if ( !parent_node || parent_node->docnode_locked_recursive() )
0071         return {};
0072 
0073     return {position.first, position.second, dest};
0074 }
0075 
0076 
0077 bool item_models::DocumentModelBase::dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent)
0078 {
0079     Q_UNUSED(column);
0080 
0081     model::DocumentNode* parent_node;
0082     model::ShapeListProperty* dest;
0083     std::tie(parent_node, row, dest) = cleaned_drop_position(data, action, parent, row, column);
0084     if ( !parent_node )
0085         return false;
0086 
0087     int max_child = parent_node->docnode_child_count();
0088     int insert = max_child - row;
0089 
0090     if ( row > max_child || row < 0)
0091         insert = max_child;
0092 
0093     DragDecoder<model::ShapeElement> decoder(data->data("application/x.glaxnimate-node-uuid"), document());
0094     std::vector<model::ShapeElement*> items;
0095     for ( const auto& it : decoder )
0096         items.push_back(it);
0097 
0098     if ( items.empty() )
0099         return false;
0100 
0101     command::UndoMacroGuard guard(i18n("Move Layers"), document());
0102 
0103     for ( auto shape : items )
0104     {
0105 
0106         document()->push_command(new command::MoveObject(
0107             shape, shape->owner(), dest, insert
0108         ));
0109     }
0110 
0111     return true;
0112 }
0113 
0114 bool item_models::DocumentModelBase::canDropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) const
0115 {
0116     return std::get<model::VisualNode*>(cleaned_drop_position(data, action, parent, row, column));
0117 }