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

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 "fill_style_widget.hpp"
0008 
0009 #include "model/assets/named_color.hpp"
0010 #include "model/document.hpp"
0011 #include "command/animation_commands.hpp"
0012 
0013 using namespace glaxnimate::gui;
0014 using namespace glaxnimate;
0015 
0016 
0017 FillStyleWidget::FillStyleWidget(QWidget* parent )
0018 : ColorSelector(parent)
0019 {
0020     connect(this, &ColorSelector::current_color_changed,
0021             this, &FillStyleWidget::set_target_color);
0022     connect(this, &ColorSelector::current_color_committed,
0023             this, &FillStyleWidget::commit_target_color);
0024     connect(this, &ColorSelector::current_color_cleared,
0025             this, &FillStyleWidget::clear_target_color);
0026 }
0027 
0028 
0029 void FillStyleWidget::set_targets(const std::vector<model::Fill*>& new_targets)
0030 {
0031     targets.clear();
0032     targets.assign(new_targets.begin(), new_targets.end());
0033 }
0034 
0035 void glaxnimate::gui::FillStyleWidget::before_set_target()
0036 {
0037     if ( current_target )
0038     {
0039         disconnect(current_target, &model::Object::property_changed,
0040                     this, &FillStyleWidget::property_changed);
0041     }
0042 }
0043 
0044 void glaxnimate::gui::FillStyleWidget::after_set_target()
0045 {
0046     if ( current_target )
0047     {
0048         update_from_target();
0049         connect(current_target, &model::Object::property_changed,
0050                 this, &FillStyleWidget::property_changed);
0051     }
0052 }
0053 
0054 void glaxnimate::gui::FillStyleWidget::set_current(model::Fill* current)
0055 {
0056     before_set_target();
0057 
0058     current_target = current;
0059     stop = -1;
0060 
0061     after_set_target();
0062 
0063 }
0064 
0065 model::Fill * FillStyleWidget::current() const
0066 {
0067     return static_cast<model::Fill*>(current_target);
0068 }
0069 
0070 void FillStyleWidget::update_from_target()
0071 {
0072     auto lock = updating.get_lock();
0073     from_styler(current_target, stop);
0074     Q_EMIT current_color_changed(current_color());
0075     update();
0076 }
0077 
0078 void FillStyleWidget::set_target_color(const QColor& color)
0079 {
0080     set_color(color, false);
0081 }
0082 
0083 void FillStyleWidget::commit_target_color()
0084 {
0085     if ( current_target )
0086         set_color(current_target->color.get(), true);
0087 }
0088 
0089 void FillStyleWidget::property_changed(const model::BaseProperty* prop)
0090 {
0091     if ( prop == &current_target->color || prop == &current_target->use )
0092     {
0093         update_from_target();
0094     }
0095 }
0096 
0097 void FillStyleWidget::set_color(const QColor&, bool commit)
0098 {
0099     if ( updating )
0100         return;
0101 
0102     apply_to_targets(i18n("Update Fill Color"), targets, stop, commit);
0103 }
0104 
0105 void FillStyleWidget::set_gradient_stop(model::Styler* styler, int index)
0106 {
0107     if ( auto fill = styler->cast<model::Fill>() )
0108     {
0109         before_set_target();
0110         current_target = fill;
0111         targets.push_back(fill);
0112         targets.clear();
0113         stop = index;
0114         after_set_target();
0115     }
0116 }
0117 
0118 void FillStyleWidget::clear_target_color()
0119 {
0120     clear_targets(i18n("Clear Fill Color"), targets);
0121 }