src/ldproject.cpp

Thu, 05 Mar 2015 02:24:15 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Thu, 05 Mar 2015 02:24:15 +0200
branch
projects
changeset 937
d5a58674ccd1
parent 936
aee883858c90
child 938
cc8920320184
permissions
-rw-r--r--

- commit more work done on projects

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"
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 LDProject::LDProject() {}
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 LDProject::~LDProject() {}
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 LDProjectPtr LDProject::LoadFromFile (const QString& filename)
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 {
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 FILE* fp = fopen ("log.txt", "w");
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 if (!fp)
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 return LDProjectPtr();
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 archive* arc = archive_read_new();
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 archive_read_support_filter_all (arc);
937
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
34 archive_read_support_format_zip (arc);
936
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 int result = archive_read_open_filename (arc, filename.toLocal8Bit().constData(), 0x4000);
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 if (result != ARCHIVE_OK)
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 {
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 fprint (fp, "unable to open argh.pk3 (%1)\n", archive_error_string (arc));
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 return LDProjectPtr();
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 }
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42
937
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
43 for (archive_entry* arcent; archive_read_next_header(arc, &arcent) == ARCHIVE_OK;)
936
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 {
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 QString pathname = archive_entry_pathname (arcent);
937
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
46 QVector<char> buffer;
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
47 buffer.resize (archive_entry_size (arcent));
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
48 int size = archive_read_data(arc, buffer.data(), buffer.size());
936
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49
937
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
50 if (size >= 0)
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
51 {
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
52 if (pathname.startsWith ("dat/"))
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
53 loadBinaryDocument (pathname.right (4), QByteArray (buffer.constData(), buffer.size()));
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
54 }
936
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 else
937
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
56 fprint (fp, "Unable to read %1: %2", pathname, archive_error_string (arc));
936
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 }
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 if ((result = archive_read_free(arc)) != ARCHIVE_OK)
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 {
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 fprint (fp, "unable to close argh.pk3\n");
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 return LDProjectPtr();
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 }
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 return LDProjectPtr();
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 }
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67
937
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
68 #include "ldDocument.h"
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
69 void LDProject::loadBinaryDocument(const QString &name, const QByteArray &data)
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
70 {
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
71 QDataStream ds (&data, QIODevice::ReadOnly);
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
72 ds.setVersion (QDataStream::Qt_4_8);
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
73 enum { CurrentVersion = 0 };
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
74
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
75 quint16 version;
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
76 ds << version;
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
77
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
78 if (version > CurrentVersion)
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
79 return; // too new
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
80
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
81 qint8 header;
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
82 quint32 color;
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
83 LDDocumentPtr doc = LDDocument::createNew();
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
84 LDObjectPtr obj;
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
85 struct XYZ { double x, y, z; Vertex toVertex() const { return Vertex (x,y,z); }};
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
86 XYZ verts[4];
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
87
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
88 while ((ds << header) != -1)
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
89 {
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
90 switch (header)
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
91 {
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
92 case 0:
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
93 {
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
94 QString message;
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
95 ds >> message;
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
96 doc->addObject (LDSpawn<LDComment> (message));
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
97 }
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
98 break;
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
99
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
100 case 2:
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
101 obj = LDSpawn<LDLine>();
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
102 goto polyobject;
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
103
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
104 case 3:
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
105 obj = LDSpawn<LDTriangle>();
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
106 goto polyobject;
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
107
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
108 case 4:
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
109 obj = LDSpawn<LDQuad>();
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
110 goto polyobject;
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
111
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
112 case 5:
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
113 obj = LDSpawn<LDCondLine>();
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
114 polyobject:
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
115 ds >> color;
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
116 for (int i = 0; i < obj->numVertices(); ++i)
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
117 {
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
118 XYZ v;
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
119 ds >> v.x >> v.y >> v.z;
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
120 obj->setVertex (i, Vertex (v.x, v.y, v.z));
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
121 }
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
122
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
123 doc->addObject (obj);
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
124 break;
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
125 }
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
126 }
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
127
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
128 }
d5a58674ccd1 - commit more work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents: 936
diff changeset
129
936
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 LDProjectPtr LDProject::NewProject()
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 {
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132 return LDProjectPtr (new LDProject());
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 }
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
135 bool LDProject::save (const QString &filename)
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136 {
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137 return false;
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138 }
aee883858c90 - commit work done on projects
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139

mercurial