Warning, file /graphics/glaxnimate/src/gui/item_models/gradient_list_model.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 "gradient_list_model.hpp"
0008 
0009 
0010 using namespace glaxnimate::gui;
0011 using namespace glaxnimate;
0012 
0013 
0014 void item_models::GradientListModel::set_defs(model::Assets* defs)
0015 {
0016     beginResetModel();
0017 
0018     if ( this->defs )
0019     {
0020         disconnect(this->defs, nullptr, this, nullptr);
0021     }
0022 
0023     this->defs = defs;
0024 
0025     if ( defs )
0026     {
0027         connect(defs->gradient_colors.get(), &model::GradientColorsList::docnode_child_add_begin, this, &GradientListModel::on_add_begin);
0028         connect(defs->gradient_colors.get(), &model::GradientColorsList::docnode_child_add_end, this, &GradientListModel::on_add_end);
0029         connect(defs->gradient_colors.get(), &model::GradientColorsList::docnode_child_remove_begin, this, &GradientListModel::on_remove_begin);
0030         connect(defs->gradient_colors.get(), &model::GradientColorsList::docnode_child_remove_end, this, &GradientListModel::on_remove_end);
0031         connect(defs->gradient_colors.get(), &model::GradientColorsList::docnode_child_move_begin, this, &GradientListModel::on_move_begin);
0032         connect(defs->gradient_colors.get(), &model::GradientColorsList::docnode_child_move_end, this, &GradientListModel::on_move_end);
0033     }
0034     endResetModel();
0035 }
0036 
0037 void item_models::GradientListModel::on_add_begin(int i)
0038 {
0039     beginInsertRows({}, i, i);
0040 }
0041 
0042 void item_models::GradientListModel::on_add_end(model::DocumentNode* n)
0043 {
0044     auto t = static_cast<model::GradientColors*>(n);
0045     connect(t, &model::DocumentNode::name_changed, this, [this, t]{
0046         auto i = index(defs->gradient_colors->values.index_of(t), Columns::Name);
0047         dataChanged(i, i, {Qt::DisplayRole, Qt::EditRole});
0048     });
0049     connect(t, &model::GradientColors::colors_changed, this, [this, t]{
0050         auto i = index(defs->gradient_colors->values.index_of(t), Columns::Gradient);
0051         dataChanged(i, i, {Qt::DisplayRole, Qt::EditRole});
0052     });
0053     connect(t, &model::BrushStyle::users_changed, this, [this, t]{
0054         auto i = index(defs->gradient_colors->values.index_of(t), Columns::Users);
0055         dataChanged(i, i, {Qt::DisplayRole});
0056     });
0057     endInsertRows();
0058 }
0059 
0060 void item_models::GradientListModel::on_move_begin(int a, int b)
0061 {
0062     beginMoveRows({}, a, a, {}, b);
0063 }
0064 
0065 void item_models::GradientListModel::on_move_end(model::DocumentNode*, int, int)
0066 {
0067     endMoveRows();
0068 }
0069 
0070 void item_models::GradientListModel::on_remove_begin(int i)
0071 {
0072     beginRemoveRows({}, i, i);
0073 }
0074 
0075 void item_models::GradientListModel::on_remove_end(model::DocumentNode* c)
0076 {
0077     disconnect(c, nullptr, this, nullptr);
0078     endRemoveRows();
0079 }
0080 
0081 QVariant item_models::GradientListModel::data(const QModelIndex& index, int role) const
0082 {
0083     if ( !defs || index.row() < 0 || index.row() >= defs->gradient_colors->values.size() )
0084         return {};
0085 
0086     auto item = defs->gradient_colors->values[index.row()];
0087 
0088     switch ( index.column() )
0089     {
0090         case Columns::Gradient:
0091             if ( role == Qt::EditRole || role == Qt::DisplayRole )
0092             {
0093                 QLinearGradient g(QPointF(0, 0), QPointF(1, 0));
0094                 g.setStops(item->colors.get());
0095                 return QBrush(g);
0096             }
0097             else if ( role == Qt::SizeHintRole )
0098             {
0099                 return QSize(128, 32);
0100             }
0101             break;
0102         case Columns::Name:
0103             switch ( role )
0104             {
0105                 case Qt::EditRole:
0106                     return item->name.get();
0107                 case Qt::DisplayRole:
0108                     return item->object_name();
0109             }
0110             break;
0111         case Columns::Users:
0112             if ( role == Qt::DisplayRole )
0113                 return int(item->users().size());
0114             break;
0115     }
0116 
0117     return {};
0118 }
0119 
0120 bool item_models::GradientListModel::setData(const QModelIndex& index, const QVariant& value, int role)
0121 {
0122     if ( !defs || role != Qt::EditRole || index.row() < 0 || index.row() >= defs->gradient_colors->values.size() )
0123         return false;
0124 
0125     auto item = defs->gradient_colors->values[index.row()];
0126 
0127     switch ( index.column() )
0128     {
0129         case Columns::Name:
0130             return item->name.set_undoable(value);
0131         case Columns::Gradient:
0132         {
0133             QBrush b = value.value<QBrush>();
0134             if ( !b.gradient() )
0135                 return false;
0136             return item->colors.set_undoable(QVariant::fromValue(b.gradient()->stops()));
0137         }
0138         default:
0139             return false;
0140     }
0141 }
0142 
0143 Qt::ItemFlags item_models::GradientListModel::flags(const QModelIndex& index) const
0144 {
0145     auto flags = QAbstractTableModel::flags(index);
0146     if ( index.column() == 0 || index.column() == 1 )
0147         flags |= Qt::ItemIsEditable;
0148 
0149     return flags;
0150 }
0151 
0152 int item_models::GradientListModel::rowCount(const QModelIndex&) const
0153 {
0154     return defs ? defs->gradient_colors->values.size() : 0;
0155 }
0156 
0157 int item_models::GradientListModel::columnCount(const QModelIndex&) const
0158 {
0159     return Columns::Count;
0160 }
0161 
0162 QVariant item_models::GradientListModel::headerData(int section, Qt::Orientation orientation, int role) const
0163 {
0164     if ( orientation == Qt::Vertical )
0165         return {};
0166 
0167     switch ( section )
0168     {
0169         case Name:
0170             if ( role == Qt::DisplayRole || role == Qt::ToolTipRole )
0171                 return i18n("Name");
0172             break;
0173         case Gradient:
0174             if ( role == Qt::DisplayRole || role == Qt::ToolTipRole )
0175                 return i18n("Gradient");
0176             break;
0177         case Users:
0178             if ( role == Qt::DisplayRole )
0179                 return i18n("#");
0180             if ( role == Qt::ToolTipRole )
0181                 return i18n("Number of users");
0182             break;
0183     }
0184     return {};
0185 }
0186 
0187 model::GradientColors * item_models::GradientListModel::gradient(const QModelIndex& index) const
0188 {
0189     if ( !index.isValid() || !defs )
0190         return nullptr;
0191 
0192     if ( index.row() < defs->gradient_colors->values.size() )
0193         return defs->gradient_colors->values[index.row()];
0194 
0195     return nullptr;
0196 }
0197 
0198 QModelIndex item_models::GradientListModel::gradient_to_index(model::GradientColors* gradient) const
0199 {
0200     if ( !defs )
0201         return {};
0202     return createIndex(defs->gradient_colors->values.index_of(gradient), 0);
0203 }