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

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 #include "base.hpp"
0007 #include "model/assets/assets.hpp"
0008 
0009 QString glaxnimate::io::ImportExport::name_filter() const
0010 {
0011     QString ext_str;
0012     for ( const QString& ext : extensions() )
0013     {
0014         ext_str += "*." + ext + " ";
0015     }
0016 
0017     if ( ext_str.isEmpty() )
0018         return {};
0019 
0020     ext_str.resize(ext_str.size() - 1);
0021     //: Open/Save file dialog file filter eg: "Text files (.txt)"
0022     return i18n("%1 (%2)", name(), ext_str);
0023 }
0024 
0025 
0026 QByteArray glaxnimate::io::ImportExport::save(model::Composition* comp, const QVariantMap& setting_values, const QString& filename)
0027 {
0028     QByteArray data;
0029     QBuffer file(&data);
0030     file.open(QIODevice::WriteOnly);
0031 
0032     QVariantMap clean_setting_values = setting_values;
0033 
0034     if ( auto settings = save_settings(comp) )
0035     {
0036         for ( const auto& setting : *settings )
0037             clean_setting_values[setting.slug] = setting.get_variant(clean_setting_values);
0038     }
0039 
0040     if ( !save(file, filename, comp, clean_setting_values) )
0041         return {};
0042 
0043     return data;
0044 }
0045 
0046 bool glaxnimate::io::ImportExport::save ( QIODevice& file, const QString& filename, model::Document* document, const QVariantMap& setting_values )
0047 {
0048     if ( document->assets()->compositions->values.empty() )
0049         return false;
0050     return save(file, filename, document->assets()->compositions->values[0], setting_values);
0051 }
0052 
0053 bool glaxnimate::io::ImportExport::open(QIODevice& file, const QString& filename,
0054                     model::Document* document, const QVariantMap& setting_values)
0055 {
0056     if ( !file.isOpen() && auto_open() )
0057         if ( !file.open(QIODevice::ReadOnly) )
0058             return false;
0059 
0060     bool ok = on_open(file, filename, document, setting_values);
0061     Q_EMIT completed(ok);
0062     return ok;
0063 }
0064 
0065 bool glaxnimate::io::ImportExport::save(QIODevice& file, const QString& filename,
0066                     model::Composition* comp, const QVariantMap& setting_values)
0067 {
0068     if ( !file.isOpen() && auto_open() )
0069         if ( !file.open(QIODevice::WriteOnly) )
0070             return false;
0071 
0072     bool ok = on_save(file, filename, comp, setting_values);
0073     Q_EMIT completed(ok);
0074     return ok;
0075 }
0076 
0077 bool glaxnimate::io::ImportExport::load(model::Document* document, const QByteArray& data,
0078                             const QVariantMap& setting_values, const QString& filename)
0079 {
0080     if ( !document )
0081         return false;
0082 
0083     QBuffer file(const_cast<QByteArray*>(&data));
0084     file.open(QIODevice::ReadOnly);
0085 
0086     QVariantMap clean_setting_values = setting_values;
0087 
0088     if ( auto settings = open_settings() )
0089     {
0090         for ( const auto& setting : *settings )
0091             clean_setting_values[setting.slug] = setting.get_variant(clean_setting_values);
0092     }
0093 
0094     return open(file, filename, document, clean_setting_values);
0095 }