File indexing completed on 2024-05-26 04:33:36

0001 /*
0002  *  SPDX-FileCopyrightText: 2013 Sven Langkamp <sven.langkamp@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "qml_converter.h"
0008 
0009 #include <QFileInfo>
0010 #include <QDir>
0011 
0012 #include <kis_image.h>
0013 #include <kis_group_layer.h>
0014 
0015 #define SPACE "    "
0016 
0017 QMLConverter::QMLConverter()
0018 {
0019 }
0020 
0021 QMLConverter::~QMLConverter()
0022 {
0023 }
0024 
0025 KisImportExportErrorCode QMLConverter::buildFile(const QString &filename, const QString &realFilename, QIODevice *io, KisImageSP image)
0026 {
0027     QTextStream out(io);
0028     out.setCodec("UTF-8");
0029     out << "import QtQuick 1.1" << "\n\n";
0030     out << "Rectangle {\n";
0031     writeInt(out, 1, "width", image->width());
0032     writeInt(out, 1, "height", image->height());
0033     out << "\n";
0034 
0035     QFileInfo info(filename);
0036     QFileInfo infoRealFile(realFilename);
0037     KisNodeSP node = image->rootLayer()->firstChild();
0038     QString imageDir = infoRealFile.completeBaseName() + "_images";
0039     QString imagePath = infoRealFile.absolutePath() + '/' + imageDir;
0040     if (node) {
0041         QDir dir;
0042         bool success = dir.mkpath(imagePath);
0043         if (!success)
0044         {
0045             return ImportExportCodes::CannotCreateFile;
0046         }
0047     }
0048 
0049     dbgFile << "Saving images to " << imagePath;
0050     while(node) {
0051         KisPaintDeviceSP projection = node->projection();
0052         QRect rect = projection->exactBounds();
0053         QImage qmlImage = projection->convertToQImage(0, rect.x(), rect.y(), rect.width(), rect.height());
0054         QString name = node->name().replace(' ', '_').toLower();
0055         QString fileName = name + ".png";
0056         qmlImage.save(imagePath +'/'+ fileName);
0057 
0058         out << SPACE << "Image {\n";
0059         writeString(out, 2, "id", name);
0060         writeInt(out, 2, "x", rect.x());
0061         writeInt(out, 2, "y", rect.y());
0062         writeInt(out, 2, "width", rect.width());
0063         writeInt(out, 2, "height", rect.height());
0064         writeString(out, 2, "source", "\"" + imageDir + '/' + fileName + "\"" );
0065         writeString(out, 2, "opacity", QString().setNum(node->opacity()/255.0));
0066         out << SPACE << "}\n";
0067         node = node->nextSibling();
0068     }
0069     out << "}\n";
0070 
0071     return ImportExportCodes::OK;
0072 }
0073 
0074 void QMLConverter::writeString(QTextStream&  out, int spacing, const QString& setting, const QString& value) {
0075     for (int space = 0; space < spacing; space++) {
0076         out << SPACE;
0077     }
0078     out << setting << ": " << value << "\n";
0079 }
0080 
0081 void QMLConverter::writeInt(QTextStream&  out, int spacing, const QString& setting, int value) {
0082     writeString(out, spacing, setting, QString::number(value));
0083 }
0084 
0085