src/ldproject.cpp

Thu, 02 Apr 2015 11:25:58 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Thu, 02 Apr 2015 11:25:58 +0300
branch
projects
changeset 939
9ac1042beadc
parent 938
cc8920320184
permissions
-rw-r--r--

- commit ldproject stuff

936
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2013 - 2015 Teemu Piippo
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 *
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 * (at your option) any later version.
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 *
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * GNU General Public License for more details.
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 *
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 */
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 #include <archive.h>
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 #include <archive_entry.h>
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 #include "ldproject.h"
938
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
22 #include "ldDocument.h"
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
23
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
24 enum { CurrentBinaryVersion = 0 };
936
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 LDProject::LDProject() {}
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 LDProject::~LDProject() {}
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 LDProjectPtr LDProject::LoadFromFile (const QString& filename)
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 {
939
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
31 QVector<QPair<LDSubfilePtr, QString>> referenceNames;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
32 std::function<LDProjectPtr (const QString&)> loadProject;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
33 std::function<void (const QString &name, const QByteArray &data)> loadDocument;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
34
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
35 loadProject = [&](const QString& filename) -> LDProjectPtr
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
36 {
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
37 archive* arc = archive_read_new();
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
38 archive_read_support_filter_all (arc);
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
39 archive_read_support_format_zip (arc);
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
40 int result = archive_read_open_filename (arc, filename.toLocal8Bit().constData(), 0x4000);
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
41
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
42 if (result != ARCHIVE_OK)
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
43 {
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
44 fprint (stderr, "unable to open argh.pk3 (%1)\n", archive_error_string (arc));
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
45 return LDProjectPtr();
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
46 }
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
47
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
48 for (archive_entry* arcent; archive_read_next_header(arc, &arcent) == ARCHIVE_OK;)
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
49 {
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
50 QString pathname = archive_entry_pathname (arcent);
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
51 QVector<char> buffer;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
52 buffer.resize (archive_entry_size (arcent));
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
53 int size = archive_read_data(arc, buffer.data(), buffer.size());
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
54
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
55 if (size >= 0)
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
56 {
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
57 if (pathname.startsWith ("dat/"))
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
58 loadDocument (pathname.right (4), QByteArray (buffer.constData(), buffer.size()));
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
59 }
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
60 else
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
61 fprint (stderr, "Unable to read %1: %2", pathname, archive_error_string (arc));
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
62 }
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
63
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
64 if ((result = archive_read_free(arc)) != ARCHIVE_OK)
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
65 {
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
66 fprint (stderr, "unable to close argh.pk3\n");
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
67 return LDProjectPtr();
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
68 }
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
69
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
70 return LDProjectPtr();
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
71 };
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
72
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
73 loadDocument = [&](const QString &name, const QByteArray &data) -> void
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
74 {
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
75 QDataStream ds (&data, QIODevice::ReadOnly);
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
76 ds.setVersion (QDataStream::Qt_4_8);
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
77
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
78 quint16 version;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
79 ds << version;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
80
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
81 if (version > CurrentBinaryVersion)
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
82 return; // too new
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
83
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
84 qint8 header;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
85 quint32 color;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
86 LDDocumentPtr doc = LDDocument::createNew();
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
87 doc->setName (name);
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
88 LDObjectPtr obj;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
89 Vertex vertex;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
90 Matrix matrix;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
91
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
92 while ((ds << header) != -1)
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
93 {
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
94 switch (header)
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
95 {
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
96 case 0:
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
97 {
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
98 QString message;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
99 ds >> message;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
100 doc->addObject (LDSpawn<LDComment> (message));
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
101 }
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
102 break;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
103
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
104 case 1:
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
105 {
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
106 LDSubfilePtr ref = LDSpawn<LDSubfile>();
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
107 QString name;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
108 ds >> color;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
109 ds >> vertex;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
110
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
111 for (int i = 0; i < 9; ++i)
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
112 ds >> matrix[i];
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
113
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
114 ds >> name;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
115 ref->setColor (LDColor::fromIndex (color));
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
116 ref->setPosition (vertex);
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
117 ref->setTransform (matrix);
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
118
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
119 // We leave the fileInfo null for now, references are resolved during
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
120 // post-process. If this object references a document that we'll parse
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
121 // later, trying to find it now would yield null.
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
122 referenceNames.append ({ref, name});
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
123 }
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
124 break;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
125
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
126 case 2:
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
127 obj = LDSpawn<LDLine>();
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
128 goto polyobject;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
129
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
130 case 3:
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
131 obj = LDSpawn<LDTriangle>();
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
132 goto polyobject;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
133
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
134 case 4:
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
135 obj = LDSpawn<LDQuad>();
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
136 goto polyobject;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
137
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
138 case 5:
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
139 obj = LDSpawn<LDCondLine>();
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
140 polyobject:
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
141 ds >> color;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
142 obj->setColor (LDColor::fromIndex (color));
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
143
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
144 for (int i = 0; i < obj->numVertices(); ++i)
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
145 {
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
146 ds >> vertex;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
147 obj->setVertex (i, vertex);
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
148 }
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
149
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
150 doc->addObject (obj);
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
151 break;
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
152 }
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
153 }
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
154 };
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
155
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
156 return loadProject (filename);
938
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
157 }
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
158
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
159 struct Xyz
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
160 {
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
161 double x, y, z;
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
162 Xyz (Vertex const& a) :
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
163 x (a.x()),
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
164 y (a.y()),
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
165 z (a.z()) {}
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
166 Vertex toVertex() const { return Vertex (x, y, z); }
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
167 };
936
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
168
938
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
169 QDataStream& operator>> (QDataStream& ds, Xyz& a)
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
170 {
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
171 return ds >> a.x >> a.y >> a.z;
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
172 }
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
173
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
174 QDataStream& operator<< (QDataStream& ds, const Xyz& a)
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
175 {
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
176 return ds << a.x << a.y << a.z;
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
177 }
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
178
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
179 struct LDProjectLoader
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
180 {
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
181
939
9ac1042beadc - commit ldproject stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 938
diff changeset
182 LDProjectPtr loadProject();
938
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
183 void loadDocument (const QString &name, const QByteArray &data);
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
184
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
185 };
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
186
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
187 struct ArchiveEntry
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
188 {
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
189 archive_entry* entry;
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
190 ArchiveEntry() : entry (archive_entry_new()) {}
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
191 ArchiveEntry (const ArchiveEntry&) = delete;
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
192 ~ArchiveEntry() { archive_entry_free (entry); }
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
193 void operator= (const ArchiveEntry&) = delete;
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
194 operator archive_entry*() { return entry; }
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
195
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
196 void clear() { archive_entry_clear (entry); }
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
197 void setSize (size_t size) { archive_entry_set_size (entry, size); }
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
198 void setPathName (const char* name) { archive_entry_set_pathname (entry, name); }
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
199 void setFileType (unsigned type) { archive_entry_set_filetype (entry, type); }
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
200 void setPermissions (int perms) { archive_entry_set_perm (entry, perms); }
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
201 };
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
202
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
203 void LDProject::saveBinaryDocuments (archive* arc)
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
204 {
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
205 ArchiveEntry ent;
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
206
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
207 for (LDDocumentPtr doc : m_documents)
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
208 {
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
209 QByteArray buffer;
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
210 QDataStream ds (&buffer, QIODevice::WriteOnly);
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
211 ds << CurrentBinaryVersion;
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
212
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
213 for (LDObjectPtr obj : doc->objects())
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
214 {
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
215 int number;
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
216
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
217 switch (obj->type())
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
218 {
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
219 case OBJ_Comment:
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
220 ds << 0
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
221 << obj.staticCast<LDCommentPtr>()->text();
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
222 break;
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
223
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
224 case OBJ_Subfile:
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
225 {
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
226 LDSubfilePtr ref = obj.staticCast<LDSubfilePtr>();
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
227 ds << 1
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
228 << ref->color().index()
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
229 << ref->position();
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
230
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
231 for (int i = 0; i < 9; ++i)
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
232 ds << ref->transform()[i];
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
233
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
234 ds << ref->fileInfo()->name();
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
235 }
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
236 break;
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
237
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
238 case OBJ_Line:
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
239 number = 2;
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
240 goto polyobj;
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
241
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
242 case OBJ_Triangle:
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
243 number = 3;
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
244 goto polyobj;
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
245
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
246 case OBJ_Quad:
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
247 number = 4;
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
248 goto polyobj;
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
249
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
250 case OBJ_CondLine:
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
251 number = 5;
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
252 polyobj:
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
253 ds << obj->color().index();
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
254
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
255 for (int i = 0; i < obj->numVertices(); ++i)
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
256 ds << obj->vertex (i);
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
257 break;
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
258 }
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
259 }
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
260
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
261 ent.clear();
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
262 ent.setSize (buffer.size());
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
263 ent.setPathName (QString ("doc/" + doc->name() + ".dat").toLocal8Bit());
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
264 ent.setFileType (AE_IFREG);
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
265 ent.setPermissions (0644);
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
266 archive_write_header (arc, ent);
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
267 archive_write_data (arc, buffer.constData(), buffer.size());
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
268 }
937
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
269 }
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
270
936
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
271 LDProjectPtr LDProject::NewProject()
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
272 {
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
273 return LDProjectPtr (new LDProject());
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
274 }
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
275
938
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
276 bool LDProject::save (const QString& filename)
936
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
277 {
938
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
278 QString tempname = filename;
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
279
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
280 if (tempname.endsWith (".ldforge"))
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
281 tempname.chop (strlen (".ldforge"));
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
282
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
283 if (not tempname.endsWith (".zip"))
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
284 tempname += ".zip";
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
285
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
286 archive* arc = archive_write_new();
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
287 archive_write_open_filename (arc, filename);
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
288 saveBinaryDocuments (arc);
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
289 archive_write_close (arc);
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
290 m_lastErrorString = archive_error_string (arc);
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
291 archive_write_free (arc);
cc8920320184 - more work on project files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 937
diff changeset
292 return true;
936
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
293 }

mercurial