1 /*
2 * Copyright (c) 2013 Sven Langkamp <sven.langkamp@gmail.com>
3 *
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include "qml_converter.h"
20
21 #include <QFileInfo>
22 #include <QDir>
23
24 #include <kis_image.h>
25 #include <kis_group_layer.h>
26
27 #define SPACE " "
28
QMLConverter()29 QMLConverter::QMLConverter()
30 {
31 }
32
~QMLConverter()33 QMLConverter::~QMLConverter()
34 {
35 }
36
buildFile(const QString & filename,const QString & realFilename,QIODevice * io,KisImageSP image)37 KisImportExportErrorCode QMLConverter::buildFile(const QString &filename, const QString &realFilename, QIODevice *io, KisImageSP image)
38 {
39 QTextStream out(io);
40 out.setCodec("UTF-8");
41 out << "import QtQuick 1.1" << "\n\n";
42 out << "Rectangle {\n";
43 writeInt(out, 1, "width", image->width());
44 writeInt(out, 1, "height", image->height());
45 out << "\n";
46
47 QFileInfo info(filename);
48 QFileInfo infoRealFile(realFilename);
49 KisNodeSP node = image->rootLayer()->firstChild();
50 QString imageDir = infoRealFile.completeBaseName() + "_images";
51 QString imagePath = infoRealFile.absolutePath() + '/' + imageDir;
52 if (node) {
53 QDir dir;
54 bool success = dir.mkpath(imagePath);
55 if (!success)
56 {
57 return ImportExportCodes::CannotCreateFile;
58 }
59 }
60
61 dbgFile << "Saving images to " << imagePath;
62 while(node) {
63 KisPaintDeviceSP projection = node->projection();
64 QRect rect = projection->exactBounds();
65 QImage qmlImage = projection->convertToQImage(0, rect.x(), rect.y(), rect.width(), rect.height());
66 QString name = node->name().replace(' ', '_').toLower();
67 QString fileName = name + ".png";
68 qmlImage.save(imagePath +'/'+ fileName);
69
70 out << SPACE << "Image {\n";
71 writeString(out, 2, "id", name);
72 writeInt(out, 2, "x", rect.x());
73 writeInt(out, 2, "y", rect.y());
74 writeInt(out, 2, "width", rect.width());
75 writeInt(out, 2, "height", rect.height());
76 writeString(out, 2, "source", "\"" + imageDir + '/' + fileName + "\"" );
77 writeString(out, 2, "opacity", QString().setNum(node->opacity()/255.0));
78 out << SPACE << "}\n";
79 node = node->nextSibling();
80 }
81 out << "}\n";
82
83 return ImportExportCodes::OK;
84 }
85
writeString(QTextStream & out,int spacing,const QString & setting,const QString & value)86 void QMLConverter::writeString(QTextStream& out, int spacing, const QString& setting, const QString& value) {
87 for (int space = 0; space < spacing; space++) {
88 out << SPACE;
89 }
90 out << setting << ": " << value << "\n";
91 }
92
writeInt(QTextStream & out,int spacing,const QString & setting,int value)93 void QMLConverter::writeInt(QTextStream& out, int spacing, const QString& setting, int value) {
94 writeString(out, spacing, setting, QString::number(value));
95 }
96
97
98