Warning, file /graphics/glaxnimate/src/gui/item_models/property_model_single.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 "property_model_single.hpp"
0008 #include "property_model_private.hpp"
0009 
0010 
0011 #include "command/property_commands.hpp"
0012 #include "model/shapes/styler.hpp"
0013 #include "model/stretchable_time.hpp"
0014 
0015 using namespace glaxnimate::gui;
0016 using namespace glaxnimate;
0017 
0018 
0019 class item_models::PropertyModelSingle::Private : public PropertyModelBase::Private
0020 {
0021 public:
0022     using PropertyModelBase::Private::Private;
0023 
0024     void on_connect(model::Object* object, Subtree* this_node, bool insert_row, ReferencedPropertiesMap* referenced) override
0025     {
0026         for ( model::BaseProperty* prop : object->properties() )
0027         {
0028 
0029             // skip object lists
0030             if (
0031                 (prop->traits().flags & model::PropertyTraits::List) &&
0032                 prop->traits().type == model::PropertyTraits::Object
0033             )
0034                 continue;
0035 
0036             // add the property node to the internal structures
0037             Subtree* prop_node = add_property(prop, this_node->id, insert_row, referenced);
0038 
0039             // connect references / sub-objects
0040             if ( prop->traits().is_object() )
0041             {
0042                 model::Object* subobj = prop->value().value<model::Object*>();
0043 
0044                 if ( prop->name() != "parent" && prop->name() != "composition" )
0045                     connect_subobject(subobj, prop_node, insert_row);
0046             }
0047         }
0048     }
0049 };
0050 
0051 item_models::PropertyModelSingle::PropertyModelSingle()
0052     : PropertyModelBase(std::make_unique<Private>(this))
0053 {
0054 }
0055 
0056 void item_models::PropertyModelSingle::set_object(model::Object* object)
0057 {
0058     beginResetModel();
0059     d->clear();
0060     if ( object )
0061         d->add_object(object, nullptr, false);
0062     endResetModel();
0063 }
0064 
0065 int item_models::PropertyModelSingle::columnCount(const QModelIndex&) const
0066 {
0067     return ColumnCount;
0068 }
0069 
0070 Qt::ItemFlags item_models::PropertyModelSingle::flags(const QModelIndex& index) const
0071 {
0072     if ( d->roots.empty() || !index.isValid() )
0073         return {};
0074 
0075     Private::Subtree* tree = d->node_from_index(index);
0076     if ( !tree )
0077         return {};
0078 
0079     Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
0080 
0081     switch ( index.column() )
0082     {
0083         case ColumnName:
0084             return flags;
0085         case ColumnValue:
0086             if ( tree->prop )
0087             {
0088                 model::PropertyTraits traits = tree->prop->traits();
0089 
0090                 if ( (traits.flags & (model::PropertyTraits::List|model::PropertyTraits::ReadOnly))
0091                     || traits.type == model::PropertyTraits::Object || traits.type == model::PropertyTraits::Unknown )
0092                     return flags;
0093 
0094                 if ( traits.type == model::PropertyTraits::Bool )
0095                     return flags | Qt::ItemIsUserCheckable;
0096 
0097                 return flags | Qt::ItemIsEditable;
0098             }
0099             return flags;
0100     }
0101 
0102     return {};
0103 }
0104 
0105 QVariant item_models::PropertyModelSingle::data(const QModelIndex& index, int role) const
0106 {
0107     if ( d->roots.empty() || !index.isValid() )
0108         return {};
0109 
0110 
0111     Private::Subtree* tree = d->node_from_index(index);
0112     if ( !tree )
0113         return {};
0114 
0115     switch ( index.column() )
0116     {
0117         case ColumnName: return d->data_name(tree, role);
0118         case ColumnValue: return d->data_value(tree, role);
0119     }
0120     return {};
0121 }
0122 
0123 bool item_models::PropertyModelSingle::setData(const QModelIndex& index, const QVariant& value, int role)
0124 {
0125     if ( d->roots.empty() || !index.isValid() )
0126         return false;
0127 
0128     Private::Subtree* tree = d->node_from_index(index);
0129     if ( !tree )
0130         return false;
0131 
0132     if ( index.column() == ColumnValue )
0133         return d->set_prop_data(tree, value, role);
0134 
0135     return false;
0136 }
0137 
0138 QVariant item_models::PropertyModelSingle::headerData(int section, Qt::Orientation orientation, int role) const
0139 {
0140     if ( orientation == Qt::Horizontal )
0141     {
0142         switch ( section )
0143         {
0144             case ColumnName:
0145                 if ( role == Qt::DisplayRole )
0146                     return i18n("Name");
0147                 break;
0148             case ColumnValue:
0149                 if ( role == Qt::DisplayRole )
0150                     return i18n("Value");
0151                 break;
0152         }
0153     }
0154     return {};
0155 }
0156 
0157 std::pair<model::VisualNode *, int> item_models::PropertyModelSingle::drop_position(const QModelIndex&, int, int) const
0158 {
0159     return {};
0160 }