File indexing completed on 2025-02-02 04:11:04

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 "assets.hpp"
0008 #include "model/document.hpp"
0009 #include "command/object_list_commands.hpp"
0010 
0011 GLAXNIMATE_OBJECT_IMPL(glaxnimate::model::NamedColorList)
0012 GLAXNIMATE_OBJECT_IMPL(glaxnimate::model::GradientColorsList)
0013 GLAXNIMATE_OBJECT_IMPL(glaxnimate::model::GradientList)
0014 GLAXNIMATE_OBJECT_IMPL(glaxnimate::model::BitmapList)
0015 GLAXNIMATE_OBJECT_IMPL(glaxnimate::model::CompositionList)
0016 GLAXNIMATE_OBJECT_IMPL(glaxnimate::model::FontList)
0017 GLAXNIMATE_OBJECT_IMPL(glaxnimate::model::Assets)
0018 
0019 
0020 void glaxnimate::model::NamedColorList::on_added(glaxnimate::model::NamedColor* color, int position)
0021 {
0022     connect(color, &Object::property_changed, this, [position, color, this]{
0023         Q_EMIT color_changed(position, color);
0024     });
0025     Ctor::on_added(color, position);
0026     Q_EMIT color_added(position, color);
0027 }
0028 
0029 void glaxnimate::model::NamedColorList::on_removed(glaxnimate::model::NamedColor* color, int position)
0030 {
0031     disconnect(color, nullptr, this, nullptr);
0032     Ctor::on_removed(color, position);
0033     Q_EMIT color_removed(position, color);
0034 }
0035 
0036 QIcon glaxnimate::model::NamedColorList::tree_icon() const
0037 {
0038     return QIcon::fromTheme("paint-swatch");
0039 }
0040 
0041 
0042 QIcon glaxnimate::model::GradientColorsList::tree_icon() const
0043 {
0044     return QIcon::fromTheme("paint-gradient-linear");
0045 }
0046 
0047 QIcon glaxnimate::model::GradientList::tree_icon() const
0048 {
0049     return QIcon::fromTheme("gradient");
0050 }
0051 
0052 
0053 QIcon glaxnimate::model::BitmapList::tree_icon() const
0054 {
0055     return QIcon::fromTheme("folder-images");
0056 }
0057 
0058 QIcon glaxnimate::model::CompositionList::tree_icon() const
0059 {
0060     return QIcon::fromTheme("folder-videos");
0061 }
0062 
0063 void glaxnimate::model::CompositionList::on_added(glaxnimate::model::Composition* obj, int position)
0064 {
0065     obj->attach();
0066     document()->comp_graph().add_composition(obj);
0067     Q_EMIT docnode_child_add_end(obj, position);
0068     Q_EMIT precomp_added(obj, position);
0069 }
0070 
0071 
0072 void glaxnimate::model::CompositionList::on_removed(glaxnimate::model::Composition* obj, int position)
0073 {
0074     obj->detach();
0075     document()->comp_graph().remove_composition(obj);
0076     Q_EMIT docnode_child_remove_end(obj, position);
0077 }
0078 
0079 void glaxnimate::model::FontList::on_added ( model::EmbeddedFont* obj, int position )
0080 {
0081     obj->attach();
0082     Q_EMIT docnode_child_add_end(obj, position);
0083     Q_EMIT font_added(obj);
0084 }
0085 
0086 
0087 glaxnimate::model::NamedColor* glaxnimate::model::Assets::add_color(const QColor& color, const QString& name)
0088 {
0089     auto ptr = std::make_unique<glaxnimate::model::NamedColor>(document());
0090     ptr->color.set(color);
0091     ptr->name.set(name);
0092     auto raw = ptr.get();
0093     push_command(new command::AddObject(&colors->values, std::move(ptr), colors->values.size()));
0094     return raw;
0095 }
0096 
0097 glaxnimate::model::Bitmap * glaxnimate::model::Assets::add_image_file(const QString& filename, bool embed)
0098 {
0099     auto image = std::make_unique<glaxnimate::model::Bitmap>(document());
0100     image->filename.set(filename);
0101     if ( image->pixmap().isNull() )
0102         return nullptr;
0103     image->embed(embed);
0104     auto ptr = image.get();
0105     push_command(new command::AddObject(&images->values, std::move(image), images->values.size()));
0106     return ptr;
0107 }
0108 
0109 glaxnimate::model::Bitmap * glaxnimate::model::Assets::add_image(const QImage& qimage, const QString& store_as)
0110 {
0111     auto image = std::make_unique<glaxnimate::model::Bitmap>(document());
0112     image->set_pixmap(qimage, store_as);
0113     auto ptr = image.get();
0114     push_command(new command::AddObject(&images->values, std::move(image), images->values.size()));
0115     return ptr;
0116 }
0117 
0118 glaxnimate::model::GradientColors* glaxnimate::model::Assets::add_gradient_colors(int index)
0119 {
0120     glaxnimate::model::GradientColors *ptr = new glaxnimate::model::GradientColors(document());
0121     ptr->name.set(ptr->type_name_human());
0122     push_command(new command::AddObject(&gradient_colors->values, std::unique_ptr<glaxnimate::model::GradientColors>(ptr), index));
0123     return ptr;
0124 }
0125 
0126 glaxnimate::model::Gradient* glaxnimate::model::Assets::add_gradient(int index)
0127 {
0128     glaxnimate::model::Gradient *ptr = new glaxnimate::model::Gradient(document());
0129     ptr->name.set(ptr->type_name_human());
0130     push_command(new command::AddObject(&gradients->values, std::unique_ptr<glaxnimate::model::Gradient>(ptr), index));
0131     return ptr;
0132 }
0133 
0134 /*glaxnimate::model::Composition* glaxnimate::model::Assets::add_composition()
0135 {
0136     auto comp = std::make_unique<glaxnimate::model::Composition>(document());
0137     auto ptr = comp.get();
0138     push_command(new command::AddObject(&compositions->values, std::move(comp), compositions->values.size()));
0139     return ptr;
0140 }*/
0141 
0142 glaxnimate::model::Composition* glaxnimate::model::Assets::add_comp_no_undo()
0143 {
0144     auto comp = std::make_unique<glaxnimate::model::Composition>(document());
0145     return compositions->values.insert(std::move(comp));
0146 }
0147 
0148 QIcon glaxnimate::model::Assets::tree_icon() const
0149 {
0150     return QIcon::fromTheme("folder-stash");
0151 }
0152 
0153 QIcon glaxnimate::model::Assets::instance_icon() const
0154 {
0155     return tree_icon();
0156 }
0157 
0158 glaxnimate::model::DocumentNode* glaxnimate::model::detail::defs(glaxnimate::model::Document* doc)
0159 {
0160     return doc->assets();
0161 }
0162 
0163 glaxnimate::model::DocumentNode * glaxnimate::model::Assets::docnode_parent() const
0164 {
0165     return nullptr;
0166 }
0167 
0168 int glaxnimate::model::Assets::docnode_child_count() const
0169 {
0170     return 6;
0171 }
0172 
0173 glaxnimate::model::DocumentNode * glaxnimate::model::Assets::docnode_child(int index) const
0174 {
0175     switch ( index )
0176     {
0177         case 0:
0178             return const_cast<glaxnimate::model::DocumentNode*>(static_cast<const glaxnimate::model::DocumentNode*>(colors.get()));
0179         case 1:
0180             return const_cast<glaxnimate::model::DocumentNode*>(static_cast<const glaxnimate::model::DocumentNode*>(images.get()));
0181         case 2:
0182             return const_cast<glaxnimate::model::DocumentNode*>(static_cast<const glaxnimate::model::DocumentNode*>(gradient_colors.get()));
0183         case 3:
0184             return const_cast<glaxnimate::model::DocumentNode*>(static_cast<const glaxnimate::model::DocumentNode*>(gradients.get()));
0185         case 4:
0186             return const_cast<glaxnimate::model::DocumentNode*>(static_cast<const glaxnimate::model::DocumentNode*>(compositions.get()));
0187         case 5:
0188             return const_cast<glaxnimate::model::DocumentNode*>(static_cast<const glaxnimate::model::DocumentNode*>(fonts.get()));
0189         default:
0190             return nullptr;
0191     }
0192 }
0193 
0194 int glaxnimate::model::Assets::docnode_child_index(glaxnimate::model::DocumentNode* dn) const
0195 {
0196     if ( dn == colors.get() )
0197         return 0;
0198     if ( dn == images.get() )
0199         return 1;
0200     if ( dn == gradient_colors.get() )
0201         return 2;
0202     if ( dn == gradients.get() )
0203         return 3;
0204     if ( dn == compositions.get() )
0205         return 4;
0206     if ( dn == fonts.get() )
0207         return 5;
0208     return -1;
0209 }
0210 
0211 glaxnimate::model::EmbeddedFont* glaxnimate::model::Assets::add_font(const QByteArray& ttf_data)
0212 {
0213     auto font = std::make_unique<glaxnimate::model::EmbeddedFont>(document());
0214     font->data.set(ttf_data);
0215     if ( auto old = font_by_index(font->database_index()) )
0216         return old;
0217     auto ptr = font.get();
0218     push_command(new command::AddObject(&fonts->values, std::move(font), fonts->values.size()));
0219     return ptr;
0220 }
0221 
0222 glaxnimate::model::EmbeddedFont* glaxnimate::model::Assets::add_font(const CustomFont& custom_font)
0223 {
0224     if ( auto old = font_by_index(custom_font.database_index()) )
0225         return old;
0226 
0227     auto font = std::make_unique<glaxnimate::model::EmbeddedFont>(document(), custom_font);
0228     auto ptr = font.get();
0229     push_command(new command::AddObject(&fonts->values, std::move(font), fonts->values.size()));
0230     return ptr;
0231 }
0232 
0233 
0234 glaxnimate::model::EmbeddedFont * glaxnimate::model::Assets::font_by_index(int database_index) const
0235 {
0236     for ( const auto& font : fonts->values )
0237         if ( font->database_index() == database_index )
0238             return font.get();
0239     return nullptr;
0240 }