File indexing completed on 2025-01-05 04:01:16

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 "validation.hpp"
0008 #include "model/visitor.hpp"
0009 #include "model/shapes/image.hpp"
0010 #include "model/assets/composition.hpp"
0011 
0012 
0013 using namespace glaxnimate;
0014 using namespace glaxnimate::io::lottie;
0015 
0016 void glaxnimate::io::lottie::ValidationVisitor::on_visit_document(model::Document*, model::Composition* main)
0017 {
0018     if ( !main )
0019         return;
0020 
0021     if ( fixed_size.isValid() )
0022     {
0023         qreal width = main->height.get();
0024         if ( width != fixed_size.width() )
0025             fmt->error(i18n("Invalid width: %1, should be %2", width, fixed_size.width()));
0026 
0027         qreal height = main->height.get();
0028         if ( height != fixed_size.height() )
0029             fmt->error(i18n("Invalid height: %1, should be %2", height, fixed_size.height()));
0030     }
0031 
0032     if ( !allowed_fps.empty() )
0033     {
0034         qreal fps = main->fps.get();
0035         if ( std::find(allowed_fps.begin(), allowed_fps.end(), fps) == allowed_fps.end() )
0036         {
0037             QStringList allowed;
0038             for ( auto f : allowed_fps )
0039                 allowed.push_back(QString::number(f));
0040 
0041             fmt->error(i18n("Invalid fps: %1, should be %2", fps, allowed.join(" or ")));
0042         }
0043     }
0044 
0045     if ( max_frames > 0 )
0046     {
0047         auto duration = main->animation->duration();
0048         if ( duration > max_frames )
0049             fmt->error(i18n("Too many frames: %1, should be less than %2", duration, max_frames));
0050     }
0051 }
0052 
0053 
0054 namespace {
0055 
0056 class DiscordVisitor : public ValidationVisitor
0057 {
0058 public:
0059     explicit DiscordVisitor(LottieFormat* fmt)
0060         : ValidationVisitor(fmt)
0061     {
0062         allowed_fps.push_back(60);
0063         fixed_size = QSize(320, 320);
0064     }
0065 
0066 private:
0067     void on_visit(model::DocumentNode * node) override
0068     {
0069         if ( qobject_cast<model::Image*>(node) )
0070         {
0071             show_error(node, i18n("Images are not supported"), app::log::Error);
0072         }
0073     }
0074 };
0075 
0076 } // namespace
0077 
0078 void glaxnimate::io::lottie::validate_discord(model::Document* document, model::Composition* main, glaxnimate::io::lottie::LottieFormat* format)
0079 {
0080     DiscordVisitor(format).visit(document, main);
0081 }
0082