File indexing completed on 2025-03-09 04:00:35

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 "resize_dialog.hpp"
0008 #include "ui_resize_dialog.h"
0009 
0010 #include <QEvent>
0011 
0012 #include "command/property_commands.hpp"
0013 #include "command/shape_commands.hpp"
0014 #include "command/undo_macro_guard.hpp"
0015 
0016 using namespace glaxnimate::gui;
0017 using namespace glaxnimate;
0018 
0019 class ResizeDialog::Private
0020 {
0021 public:
0022     Ui::ResizeDialog ui;
0023     double ratio = 1;
0024 
0025     void resize(model::Composition* comp)
0026     {
0027         comp->push_command(new command::SetMultipleProperties(
0028             i18n("Resize Document"),
0029             true,
0030             {&comp->width, &comp->height},
0031             ui.spin_width->value(),
0032             ui.spin_height->value()
0033         ));
0034     }
0035 };
0036 
0037 ResizeDialog::ResizeDialog(QWidget* parent)
0038     : QDialog(parent), d(std::make_unique<Private>())
0039 {
0040     d->ui.setupUi(this);
0041 }
0042 
0043 ResizeDialog::~ResizeDialog() = default;
0044 
0045 void ResizeDialog::changeEvent ( QEvent* e )
0046 {
0047     QDialog::changeEvent(e);
0048     if ( e->type() == QEvent::LanguageChange)
0049     {
0050         d->ui.retranslateUi(this);
0051     }
0052 }
0053 
0054 void ResizeDialog::width_changed(int w)
0055 {
0056     if ( d->ui.check_aspect->isChecked() )
0057     {
0058         d->ui.spin_height->blockSignals(true);
0059         d->ui.spin_height->setValue(w / d->ratio);
0060         d->ui.spin_height->blockSignals(false);
0061     }
0062 }
0063 
0064 
0065 void ResizeDialog::height_changed(int h)
0066 {
0067     if ( d->ui.check_aspect->isChecked() )
0068     {
0069         d->ui.spin_width->blockSignals(true);
0070         d->ui.spin_width->setValue(h * d->ratio);
0071         d->ui.spin_width->blockSignals(false);
0072     }
0073 }
0074 
0075 void ResizeDialog::resize_composition(model::Composition* comp)
0076 {
0077     auto doc = comp->document();
0078     d->ui.spin_width->setValue(comp->width.get());
0079     d->ui.spin_height->setValue(comp->height.get());
0080     d->ratio = double(comp->width.get()) / comp->height.get();
0081     d->ui.check_aspect->setChecked(true);
0082 
0083     if ( exec() == QDialog::Rejected )
0084         return;
0085 
0086     if ( d->ui.check_scale_layers->isChecked() && !comp->shapes.empty() )
0087     {
0088         command::UndoMacroGuard macro(i18n("Resize Document"), doc);
0089 
0090         auto nl = std::make_unique<model::Layer>(doc);
0091         model::Layer* layer = nl.get();
0092         doc->set_best_name(layer, i18n("Resize"));
0093         doc->push_command(new command::AddShape(&comp->shapes, std::move(nl), comp->shapes.size()));
0094         while ( comp->shapes[0] != layer )
0095             doc->push_command(new command::MoveShape(comp->shapes[0], &comp->shapes, &layer->shapes, layer->shapes.size()));
0096 
0097         qreal scale_w = comp->width.get() != 0 ? double(d->ui.spin_width->value()) / comp->width.get() : 1;
0098         qreal scale_h = comp->height.get() != 0 ? double(d->ui.spin_height->value()) / comp->height.get() : 1;
0099         if ( d->ui.check_layer_ratio->isChecked() )
0100             scale_h = scale_w = std::min(scale_h, scale_w);
0101 
0102         layer->transform.get()->scale.set_undoable(QVector2D(scale_w, scale_h));
0103         layer->animation->first_frame.set(comp->animation->first_frame.get());
0104         layer->animation->last_frame.set(comp->animation->last_frame.get());
0105 
0106         d->resize(comp);
0107     }
0108     else
0109     {
0110         d->resize(comp);
0111     }
0112 }
0113 
0114 void ResizeDialog::lock_changed(bool locked)
0115 {
0116     d->ui.check_aspect->setIcon(QIcon::fromTheme(locked ? "lock" : "unlock"));
0117     width_changed(d->ui.spin_width->value());
0118 }