Warning, file /graphics/glaxnimate/src/gui/item_models/asset_proxy_model.hpp 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 #pragma once
0008 
0009 #include "proxy_base.hpp"
0010 #include <QMimeData>
0011 
0012 namespace glaxnimate::gui::item_models {
0013 
0014 class AssetProxyModel : public ProxyBase
0015 {
0016 public:
0017     using ProxyBase::ProxyBase;
0018 
0019     int columnCount(const QModelIndex &parent = QModelIndex()) const override
0020     {
0021         return qMax(0, sourceModel()->columnCount(mapToSource(parent))) - 1;
0022     }
0023 
0024     int rowCount(const QModelIndex &parent = QModelIndex()) const override
0025     {
0026         if ( is_precomp(parent) )
0027             return 0;
0028         return sourceModel()->rowCount(mapToSource(parent));
0029     }
0030 
0031     QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
0032     {
0033         return create_source_index(proxyIndex.row(), proxyIndex.column() + 1, proxyIndex.internalId());
0034     }
0035 
0036     QModelIndex mapFromSource(const QModelIndex & sourceIndex) const override
0037     {
0038         return createIndex(sourceIndex.row(), qMax(0, sourceIndex.column()-1), sourceIndex.internalId());
0039     }
0040 
0041     QVariant headerData(int section, Qt::Orientation orientation, int role) const override
0042     {
0043         if ( orientation == Qt::Horizontal )
0044             section += 1;
0045         return sourceModel()->headerData(section, orientation, role);
0046     }
0047 
0048     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override
0049     {
0050         return mapFromSource(sourceModel()->index(row, column+1, mapToSource(parent)));
0051     }
0052 
0053     Qt::ItemFlags flags ( const QModelIndex & index ) const override
0054     {
0055         auto flags = sourceModel()->flags(mapToSource(index)) & ~Qt::ItemIsDropEnabled;
0056         if ( is_precomp(index) )
0057             flags |= Qt::ItemNeverHasChildren|Qt::ItemIsDragEnabled;
0058         else if ( cast_index<model::Bitmap>(index) )
0059             flags |= Qt::ItemNeverHasChildren|Qt::ItemIsDragEnabled;
0060         return flags;
0061     }
0062 
0063     QMimeData * mimeData(const QModelIndexList& indexes) const override
0064     {
0065         auto data = ProxyBase::mimeData(indexes);
0066         if ( data )
0067             data->setData("application/x.glaxnimate-asset-uuid", data->data("application/x.glaxnimate-node-uuid"));
0068         return data;
0069     }
0070 
0071 protected:
0072     bool is_precomp(const QModelIndex & index) const
0073     {
0074         return cast_index<model::Composition>(index);
0075     }
0076 
0077     template<class T>
0078     T* cast_index(const QModelIndex & index) const
0079     {
0080         return qobject_cast<T*>(static_cast<QObject*>(index.internalPointer()));
0081     }
0082 
0083     void on_rows_add_begin(const QModelIndex &parent, int first, int last)
0084     {
0085         if ( !is_precomp(parent) )
0086             beginInsertRows(parent, first, last);
0087     }
0088 
0089     void on_rows_added(const QModelIndex &parent)
0090     {
0091         if ( !is_precomp(parent) )
0092             endInsertRows();
0093     }
0094 
0095     void source_changed(QAbstractItemModel * source_model)
0096     {
0097         if ( source_model )
0098         {
0099             reconnect(&QAbstractItemModel::rowsAboutToBeInserted, &AssetProxyModel::on_rows_add_begin);
0100             reconnect(&QAbstractItemModel::rowsInserted, &AssetProxyModel::on_rows_added);
0101         }
0102     }
0103 };
0104 
0105 } // namespace glaxnimate::gui::item_models