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

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 "lottie_format.hpp"
0008 
0009 #include "lottie_importer.hpp"
0010 #include "lottie_exporter.hpp"
0011 
0012 glaxnimate::io::Autoreg<glaxnimate::io::lottie::LottieFormat> glaxnimate::io::lottie::LottieFormat::autoreg;
0013 
0014 bool glaxnimate::io::lottie::LottieFormat::on_save(QIODevice& file, const QString&,
0015                                                    model::Composition* comp, const QVariantMap& setting_values)
0016 {
0017     file.write(cbor_write_json(to_json(comp, setting_values["strip"].toBool(), false, setting_values), !setting_values["pretty"].toBool()));
0018     return true;
0019 }
0020 
0021 QCborMap glaxnimate::io::lottie::LottieFormat::to_json(model::Composition* comp, bool strip, bool strip_raster, const QVariantMap& settings)
0022 {
0023     detail::LottieExporterState exp(this, comp, strip, strip_raster, settings);
0024     return exp.to_json();
0025 }
0026 
0027 bool glaxnimate::io::lottie::LottieFormat::load_json(const QByteArray& data, model::Document* document)
0028 {
0029     QJsonDocument jdoc;
0030 
0031     try {
0032         jdoc = QJsonDocument::fromJson(data);
0033     } catch ( const QJsonParseError& err ) {
0034         Q_EMIT error(i18n("Could not parse JSON: %1", err.errorString()));
0035         return false;
0036     }
0037 
0038     if ( !jdoc.isObject() )
0039     {
0040         Q_EMIT error(i18n("No JSON object found"));
0041         return false;
0042     }
0043 
0044     QJsonObject top_level = jdoc.object();
0045 
0046     detail::LottieImporterState imp{document, this};
0047     imp.load(top_level);
0048     return true;
0049 }
0050 
0051 bool glaxnimate::io::lottie::LottieFormat::on_open(QIODevice& file, const QString&, model::Document* document, const QVariantMap&)
0052 {
0053     return load_json(file.readAll(), document);
0054 }
0055 
0056 std::unique_ptr<app::settings::SettingsGroup> glaxnimate::io::lottie::LottieFormat::save_settings(model::Composition*) const
0057 {
0058     return std::make_unique<app::settings::SettingsGroup>(app::settings::SettingList{
0059         app::settings::Setting("pretty", i18n("Pretty"), i18n("Pretty print the JSON"), false),
0060         app::settings::Setting("strip", i18n("Strip"), i18n("Strip unused properties"), false),
0061         app::settings::Setting("auto_embed", i18n("Embed Images"), i18n("Automatically embed non-embedded images"), false),
0062         app::settings::Setting("old_kf", i18n("Legacy Keyframes"), i18n("Compatibility with lottie-web versions prior to 5.0.0"), false),
0063     });
0064 }