Sun, 07 Jul 2013 16:46:30 +0300
restyle..
297
2cbd8ac8293b
Added license header to primitives.cpp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
294
diff
changeset
|
1 | /* |
2cbd8ac8293b
Added license header to primitives.cpp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
294
diff
changeset
|
2 | * LDForge: LDraw parts authoring CAD |
2cbd8ac8293b
Added license header to primitives.cpp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
294
diff
changeset
|
3 | * Copyright (C) 2013 Santeri Piippo |
358 | 4 | * |
297
2cbd8ac8293b
Added license header to primitives.cpp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
294
diff
changeset
|
5 | * This program is free software: you can redistribute it and/or modify |
2cbd8ac8293b
Added license header to primitives.cpp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
294
diff
changeset
|
6 | * it under the terms of the GNU General Public License as published by |
2cbd8ac8293b
Added license header to primitives.cpp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
294
diff
changeset
|
7 | * the Free Software Foundation, either version 3 of the License, or |
2cbd8ac8293b
Added license header to primitives.cpp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
294
diff
changeset
|
8 | * (at your option) any later version. |
358 | 9 | * |
297
2cbd8ac8293b
Added license header to primitives.cpp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
294
diff
changeset
|
10 | * This program is distributed in the hope that it will be useful, |
2cbd8ac8293b
Added license header to primitives.cpp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
294
diff
changeset
|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
2cbd8ac8293b
Added license header to primitives.cpp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
294
diff
changeset
|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2cbd8ac8293b
Added license header to primitives.cpp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
294
diff
changeset
|
13 | * GNU General Public License for more details. |
358 | 14 | * |
297
2cbd8ac8293b
Added license header to primitives.cpp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
294
diff
changeset
|
15 | * You should have received a copy of the GNU General Public License |
2cbd8ac8293b
Added license header to primitives.cpp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
294
diff
changeset
|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
2cbd8ac8293b
Added license header to primitives.cpp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
294
diff
changeset
|
17 | */ |
2cbd8ac8293b
Added license header to primitives.cpp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
294
diff
changeset
|
18 | |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
19 | #include <QDir> |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
20 | #include <QThread> |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
21 | #include <QRegExp> |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
22 | #include <QFileDialog> |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
23 | #include "file.h" |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
24 | #include "gui.h" |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
25 | #include "primitives.h" |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
26 | #include "ui_makeprim.h" |
311
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
27 | #include "misc.h" |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
28 | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
29 | vector<PrimitiveCategory> g_PrimitiveCategories; |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
30 | static PrimitiveLister* g_activePrimLister = null; |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
31 | static bool g_primListerMutex = false; |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
32 | vector<Primitive> g_primitives; |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
33 | |
321
48e429bfd58c
Apply tr() for possible future translations
Santeri Piippo <crimsondusk64@gmail.com>
parents:
319
diff
changeset
|
34 | static const str g_Other = QObject::tr( "Other" ); |
48e429bfd58c
Apply tr() for possible future translations
Santeri Piippo <crimsondusk64@gmail.com>
parents:
319
diff
changeset
|
35 | |
358 | 36 | static void populateCategories(); |
37 | static void loadPrimitiveCatgories(); | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
38 | |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
39 | // ============================================================================= |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
40 | // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
41 | // ============================================================================= |
358 | 42 | void loadPrimitives() |
43 | { | |
44 | print( "Loading primitives...\n" ); | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
45 | |
358 | 46 | loadPrimitiveCatgories(); |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
47 | |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
48 | // Try to load prims.cfg |
358 | 49 | File conf( config::dirpath() + "prims.cfg", File::Read ); |
50 | ||
51 | if( !conf ) | |
52 | { | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
53 | // No prims.cfg, build it |
358 | 54 | PrimitiveLister::start(); |
55 | } | |
56 | else | |
57 | { | |
58 | for( str line : conf ) | |
59 | { | |
60 | int space = line.indexOf( " " ); | |
61 | ||
62 | if( space == -1 ) | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
63 | continue; |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
64 | |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
65 | Primitive info; |
358 | 66 | info.name = line.left( space ); |
67 | info.title = line.mid( space + 1 ); | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
68 | g_primitives << info; |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
69 | } |
306
fef00a6cbff0
minor improvements to primitive handling
Santeri Piippo <crimsondusk64@gmail.com>
parents:
297
diff
changeset
|
70 | |
358 | 71 | populateCategories(); |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
72 | } |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
73 | } |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
74 | |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
75 | // ============================================================================= |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
76 | // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
77 | // ============================================================================= |
358 | 78 | void recursiveGetFilenames( QDir dir, vector<str>& fnames ) |
79 | { | |
80 | QFileInfoList flist = dir.entryInfoList(); | |
81 | ||
82 | for( const QFileInfo & info : flist ) | |
83 | { | |
84 | if( info.fileName() == "." || info.fileName() == ".." ) | |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
85 | continue; // skip . and .. |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
86 | |
358 | 87 | if( info.isDir() ) |
88 | recursiveGetFilenames( QDir( info.absoluteFilePath() ), fnames ); | |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
89 | else |
358 | 90 | fnames << info.absoluteFilePath(); |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
91 | } |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
92 | } |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
93 | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
94 | // ============================================================================= |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
95 | // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
96 | // ============================================================================= |
358 | 97 | void PrimitiveLister::work() |
98 | { | |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
99 | g_activePrimLister = this; |
358 | 100 | m_prims.clear(); |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
101 | |
358 | 102 | QDir dir( LDPaths::prims() ); |
103 | ulong baselen = dir.absolutePath().length(); | |
104 | ulong i = 0; | |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
105 | vector<str> fnames; |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
106 | |
358 | 107 | assert( dir.exists() ); |
108 | recursiveGetFilenames( dir, fnames ); | |
109 | emit starting( fnames.size() ); | |
110 | ||
111 | for( str fname : fnames ) | |
112 | { | |
113 | File f( fname, File::Read ); | |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
114 | |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
115 | Primitive info; |
358 | 116 | info.name = fname.mid( baselen + 1 ); // make full path relative |
117 | info.name.replace( '/', '\\' ); // use DOS backslashes, they're expected | |
118 | info.cat = null; | |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
119 | |
358 | 120 | if( !f.readLine( info.title )) |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
121 | info.title = ""; |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
122 | |
358 | 123 | info.title = info.title.simplified(); |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
124 | |
358 | 125 | if( info.title[0] == '0' ) |
126 | { | |
127 | info.title.remove( 0, 1 ); // remove 0 | |
128 | info.title = info.title.simplified(); | |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
129 | } |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
130 | |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
131 | m_prims << info; |
358 | 132 | emit update( ++i ); |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
133 | } |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
134 | |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
135 | // Save to a config file |
358 | 136 | File conf( config::dirpath() + "prims.cfg", File::Write ); |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
137 | |
358 | 138 | for( Primitive & info : m_prims ) |
139 | fprint( conf, "%1 %2\n", info.name, info.title ); | |
140 | ||
141 | conf.close(); | |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
142 | |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
143 | g_primListerMutex = true; |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
144 | g_primitives = m_prims; |
358 | 145 | populateCategories(); |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
146 | g_primListerMutex = false; |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
147 | g_activePrimLister = null; |
358 | 148 | emit workDone(); |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
149 | } |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
150 | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
151 | // ============================================================================= |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
152 | // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
153 | // ============================================================================= |
358 | 154 | void PrimitiveLister::start() |
155 | { | |
156 | if( g_activePrimLister ) | |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
157 | return; |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
158 | |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
159 | PrimitiveLister* lister = new PrimitiveLister; |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
160 | QThread* listerThread = new QThread; |
358 | 161 | lister->moveToThread( listerThread ); |
162 | connect( lister, SIGNAL( starting( ulong )), g_win, SLOT( primitiveLoaderStart( ulong )) ); | |
163 | connect( lister, SIGNAL( update( ulong )), g_win, SLOT( primitiveLoaderUpdate( ulong )) ); | |
164 | connect( lister, SIGNAL( workDone() ), g_win, SLOT( primitiveLoaderEnd() )); | |
165 | connect( listerThread, SIGNAL( started() ), lister, SLOT( work() )); | |
166 | connect( listerThread, SIGNAL( finished() ), lister, SLOT( deleteLater() )); | |
167 | listerThread->start(); | |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
168 | } |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
169 | |
358 | 170 | static PrimitiveCategory* findCategory( str name ) |
171 | { | |
172 | for( PrimitiveCategory & cat : g_PrimitiveCategories ) | |
173 | if( cat.name() == name ) | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
174 | return &cat; |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
175 | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
176 | return null; |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
177 | } |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
178 | |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
179 | // ============================================================================= |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
180 | // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
181 | // ============================================================================= |
358 | 182 | static void populateCategories() |
183 | { | |
184 | for( PrimitiveCategory & cat : g_PrimitiveCategories ) | |
185 | cat.prims.clear(); | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
186 | |
321
48e429bfd58c
Apply tr() for possible future translations
Santeri Piippo <crimsondusk64@gmail.com>
parents:
319
diff
changeset
|
187 | PrimitiveCategory* unmatched = findCategory( g_Other ); |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
188 | |
358 | 189 | if( !unmatched ) |
190 | { | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
191 | // Shouldn't happen.. but catch it anyway. |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
192 | PrimitiveCategory cat; |
321
48e429bfd58c
Apply tr() for possible future translations
Santeri Piippo <crimsondusk64@gmail.com>
parents:
319
diff
changeset
|
193 | cat.setName( g_Other ); |
358 | 194 | unmatched = &( g_PrimitiveCategories << cat ); |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
195 | } |
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
196 | |
358 | 197 | for( Primitive & prim : g_primitives ) |
198 | { | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
199 | bool matched = false; |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
200 | |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
201 | // Go over the categories and their regexes, if and when there's a match, |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
202 | // the primitive's category is set to the category the regex beloings to. |
358 | 203 | for( PrimitiveCategory & cat : g_PrimitiveCategories ) |
204 | { | |
205 | for( PrimitiveCategory::RegexEntry & entry : cat.regexes ) | |
206 | { | |
207 | switch( entry.type ) | |
208 | { | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
209 | case PrimitiveCategory::Filename: |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
210 | // f-regex, check against filename |
358 | 211 | matched = entry.regex.exactMatch( prim.name ); |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
212 | break; |
358 | 213 | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
214 | case PrimitiveCategory::Title: |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
215 | // t-regex, check against title |
358 | 216 | matched = entry.regex.exactMatch( prim.title ); |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
217 | break; |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
218 | } |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
219 | |
358 | 220 | if( matched ) |
221 | { | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
222 | prim.cat = &cat; |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
223 | break; |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
224 | } |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
225 | } |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
226 | |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
227 | // Drop out if a category was decided on. |
358 | 228 | if( prim.cat ) |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
229 | break; |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
230 | } |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
231 | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
232 | // If there was a match, add the primitive to the category. |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
233 | // Otherwise, add it to the list of unmatched primitives. |
358 | 234 | if( prim.cat ) |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
235 | prim.cat->prims << prim; |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
236 | else |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
237 | unmatched->prims << prim; |
292
4779ca562d5e
Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
238 | } |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
239 | } |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
240 | |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
241 | // ============================================================================= |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
242 | // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
243 | // ============================================================================= |
358 | 244 | static void loadPrimitiveCatgories() |
245 | { | |
246 | g_PrimitiveCategories.clear(); | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
247 | |
358 | 248 | File f( config::dirpath() + "primregexps.cfg", File::Read ); |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
249 | |
358 | 250 | if( !f ) |
251 | f.open( ":/data/primitive-categories.cfg", File::Read ); | |
252 | ||
253 | if( !f ) | |
321
48e429bfd58c
Apply tr() for possible future translations
Santeri Piippo <crimsondusk64@gmail.com>
parents:
319
diff
changeset
|
254 | critical( QObject::tr( "Failed to open primitive categories!" )); |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
255 | |
358 | 256 | if( f ) |
257 | { | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
258 | PrimitiveCategory cat; |
358 | 259 | |
260 | for( str line : f ) | |
261 | { | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
262 | int colon; |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
263 | |
358 | 264 | if( line.length() == 0 || line[0] == '#' ) |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
265 | continue; |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
266 | |
358 | 267 | if( ( colon = line.indexOf( ":" )) == -1 ) |
268 | { | |
269 | if( cat.regexes.size() > 0 ) | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
270 | g_PrimitiveCategories << cat; |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
271 | |
358 | 272 | cat.regexes.clear(); |
273 | cat.prims.clear(); | |
274 | cat.setName( line ); | |
275 | } | |
276 | else | |
277 | { | |
278 | str cmd = line.left( colon ); | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
279 | |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
280 | PrimitiveCategory::Type type = PrimitiveCategory::Filename; |
358 | 281 | |
282 | if( cmd == "f" ) | |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
283 | type = PrimitiveCategory::Filename; |
358 | 284 | elif( cmd == "t" ) |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
285 | type = PrimitiveCategory::Title; |
358 | 286 | else |
294
2892deee4c1b
Better categorized Technic primitives
Santeri Piippo <crimsondusk64@gmail.com>
parents:
293
diff
changeset
|
287 | continue; |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
288 | |
358 | 289 | QRegExp regex( line.mid( colon + 1 )); |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
290 | PrimitiveCategory::RegexEntry entry = { regex, type }; |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
291 | cat.regexes << entry; |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
292 | } |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
293 | } |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
294 | |
358 | 295 | if( cat.regexes.size() > 0 ) |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
296 | g_PrimitiveCategories << cat; |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
297 | } |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
298 | |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
299 | // Add a category for unmatched primitives |
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
300 | PrimitiveCategory cat; |
321
48e429bfd58c
Apply tr() for possible future translations
Santeri Piippo <crimsondusk64@gmail.com>
parents:
319
diff
changeset
|
301 | cat.setName( g_Other ); |
293
a0ed563e14b2
Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents:
292
diff
changeset
|
302 | g_PrimitiveCategories << cat; |
306
fef00a6cbff0
minor improvements to primitive handling
Santeri Piippo <crimsondusk64@gmail.com>
parents:
297
diff
changeset
|
303 | } |
fef00a6cbff0
minor improvements to primitive handling
Santeri Piippo <crimsondusk64@gmail.com>
parents:
297
diff
changeset
|
304 | |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
305 | // ============================================================================= |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
306 | bool primitiveLoaderBusy() |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
307 | { |
306
fef00a6cbff0
minor improvements to primitive handling
Santeri Piippo <crimsondusk64@gmail.com>
parents:
297
diff
changeset
|
308 | return g_primListerMutex; |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
309 | } |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
310 | |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
311 | vector<LDObject*> makePrimitive( PrimitiveType type, int segs, int divs, int num ) |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
312 | { |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
313 | vector<LDObject*> objs; |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
314 | |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
315 | for( int i = 0; i < segs; ++i ) |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
316 | { |
358 | 317 | double x0 = cos( ( i * 2 * pi ) / divs ), |
318 | x1 = cos( ( ( i + 1 ) * 2 * pi ) / divs ), | |
319 | z0 = sin( ( i * 2 * pi ) / divs ), | |
320 | z1 = sin( ( ( i + 1 ) * 2 * pi ) / divs ); | |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
321 | |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
322 | LDObject* obj = null; |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
323 | |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
324 | switch( type ) |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
325 | { |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
326 | case Circle: |
358 | 327 | { |
328 | vertex v0( x0, 0.0f, z0 ), | |
329 | v1( x1, 0.0f, z1 ); | |
330 | ||
331 | LDLine* line = new LDLine; | |
332 | line->setVertex( 0, v0 ); | |
333 | line->setVertex( 1, v1 ); | |
334 | line->setColor( edgecolor ); | |
335 | obj = line; | |
336 | } | |
337 | break; | |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
338 | |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
339 | case Cylinder: |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
340 | case Ring: |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
341 | case Cone: |
358 | 342 | { |
343 | double x2, x3, z2, z3; | |
344 | double y0, y1, y2, y3; | |
345 | ||
346 | if( type == Cylinder ) | |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
347 | { |
358 | 348 | x2 = x1; |
349 | x3 = x0; | |
350 | z2 = z1; | |
351 | z3 = z0; | |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
352 | |
358 | 353 | y0 = y1 = 0.0f; |
354 | y2 = y3 = 1.0f; | |
355 | } | |
356 | else | |
357 | { | |
358 | x2 = x1 * ( num + 1 ); | |
359 | x3 = x0 * ( num + 1 ); | |
360 | z2 = z1 * ( num + 1 ); | |
361 | z3 = z0 * ( num + 1 ); | |
362 | ||
363 | x0 *= num; | |
364 | x1 *= num; | |
365 | z0 *= num; | |
366 | z1 *= num; | |
367 | ||
368 | if( type == Ring ) | |
369 | y0 = y1 = y2 = y3 = 0.0f; | |
370 | else | |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
371 | { |
358 | 372 | y0 = y1 = 1.0f; |
373 | y2 = y3 = 0.0f; | |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
374 | } |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
375 | } |
358 | 376 | |
377 | vertex v0( x0, y0, z0 ), | |
378 | v1( x1, y1, z1 ), | |
379 | v2( x2, y2, z2 ), | |
380 | v3( x3, y3, z3 ); | |
381 | ||
382 | LDQuad* quad = new LDQuad; | |
383 | quad->setColor( maincolor ); | |
384 | quad->setVertex( 0, v0 ); | |
385 | quad->setVertex( 1, v1 ); | |
386 | quad->setVertex( 2, v2 ); | |
387 | quad->setVertex( 3, v3 ); | |
388 | obj = quad; | |
389 | } | |
390 | break; | |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
391 | |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
392 | case Disc: |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
393 | case DiscNeg: |
358 | 394 | { |
395 | double x2, z2; | |
396 | ||
397 | if( type == Disc ) | |
398 | x2 = z2 = 0.0f; | |
399 | else | |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
400 | { |
358 | 401 | x2 = ( x0 >= 0.0f ) ? 1.0f : -1.0f; |
402 | z2 = ( z0 >= 0.0f ) ? 1.0f : -1.0f; | |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
403 | } |
358 | 404 | |
405 | vertex v0( x0, 0.0f, z0 ), | |
406 | v1( x1, 0.0f, z1 ), | |
407 | v2( x2, 0.0f, z2 ); | |
408 | ||
409 | // Disc negatives need to go the other way around, otherwise | |
410 | // they'll end up upside-down. | |
411 | LDTriangle* seg = new LDTriangle; | |
412 | seg->setColor( maincolor ); | |
413 | seg->setVertex( type == Disc ? 0 : 2, v0 ); | |
414 | seg->setVertex( 1, v1 ); | |
415 | seg->setVertex( type == Disc ? 2 : 0, v2 ); | |
416 | obj = seg; | |
417 | } | |
418 | break; | |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
419 | |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
420 | default: |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
421 | break; |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
422 | } |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
423 | |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
424 | if( obj ) |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
425 | objs << obj; |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
426 | } |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
427 | |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
428 | return objs; |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
429 | } |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
430 | |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
431 | str primitiveTypeName( PrimitiveType type ) |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
432 | { |
321
48e429bfd58c
Apply tr() for possible future translations
Santeri Piippo <crimsondusk64@gmail.com>
parents:
319
diff
changeset
|
433 | // Not translated as primitives are in English. |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
434 | return type == Circle ? "Circle" : |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
435 | type == Cylinder ? "Cylinder" : |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
436 | type == Disc ? "Disc" : |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
437 | type == DiscNeg ? "Disc Negative" : |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
438 | type == Ring ? "Ring" : |
358 | 439 | "Cone"; |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
440 | } |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
441 | |
358 | 442 | static const str g_radialNameRoots[] = |
443 | { | |
311
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
444 | "edge", |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
445 | "cyli", |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
446 | "disc", |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
447 | "ndis", |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
448 | "ring", |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
449 | "con" |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
450 | }; |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
451 | |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
452 | // ============================================================================= |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
453 | // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
454 | // ============================================================================= |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
455 | str radialFileName( PrimitiveType type, int segs, int divs, int num ) |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
456 | { |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
457 | short numer = segs, |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
458 | denom = divs; |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
459 | |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
460 | // Simplify the fractional part, but the denominator must be at least 4. |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
461 | simplify( numer, denom ); |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
462 | |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
463 | if( denom < 4 ) |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
464 | { |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
465 | const short factor = 4 / denom; |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
466 | |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
467 | numer *= factor; |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
468 | denom *= factor; |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
469 | } |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
470 | |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
471 | // Compose some general information: prefix, fraction, root, ring number |
358 | 472 | str prefix = ( divs == lores ) ? "" : fmt( "%1/", divs ); |
311
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
473 | str frac = fmt( "%1-%2", numer, denom ); |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
474 | str root = g_radialNameRoots[type]; |
358 | 475 | str numstr = ( type == Ring || type == Cone ) ? fmt( "%1", num ) : ""; |
311
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
476 | |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
477 | // Truncate the root if necessary (7-16rin4.dat for instance). |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
478 | // However, always keep the root at least 2 characters. |
358 | 479 | int extra = ( frac.length() + numstr.length() + root.length()) - 8; |
311
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
480 | root.chop( min<short>( max<short>( extra, 0 ), 2 )); |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
481 | |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
482 | // Stick them all together and return the result. |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
483 | return prefix + frac + root + numstr + ".dat"; |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
484 | } |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
485 | |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
486 | // ============================================================================= |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
487 | // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
488 | // ============================================================================= |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
489 | void generatePrimitive() |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
490 | { |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
491 | QDialog* dlg = new QDialog( g_win ); |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
492 | Ui::MakePrimUI ui; |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
493 | ui.setupUi( dlg ); |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
494 | |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
495 | if( !dlg->exec() ) |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
496 | return; |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
497 | |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
498 | int segs = ui.sb_segs->value(); |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
499 | int divs = ui.cb_hires->isChecked() ? hires : lores; |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
500 | int num = ui.sb_ringnum->value(); |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
501 | PrimitiveType type = |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
502 | ui.rb_circle->isChecked() ? Circle : |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
503 | ui.rb_cylinder->isChecked() ? Cylinder : |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
504 | ui.rb_disc->isChecked() ? Disc : |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
505 | ui.rb_ndisc->isChecked() ? DiscNeg : |
358 | 506 | ui.rb_ring->isChecked() ? Ring : Cone; |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
507 | |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
508 | // Make the description |
358 | 509 | str frac = ftoa( ( ( float ) segs ) / divs ); |
311
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
510 | str name = radialFileName( type, segs, divs, num ); |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
511 | str descr; |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
512 | |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
513 | // Ensure that there's decimals, even if they're 0. |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
514 | if( frac.indexOf( "." ) == -1 ) |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
515 | frac += ".0"; |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
516 | |
358 | 517 | if( type == Ring || type == Cone ) |
311
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
518 | descr = fmt( "%1 %2 x %3", primitiveTypeName( type ), num, frac ); |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
519 | else |
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
520 | descr = fmt( "%1 %2", primitiveTypeName( type ), frac ); |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
521 | |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
522 | LDOpenFile* f = new LDOpenFile; |
311
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
523 | f->setName( QFileDialog::getSaveFileName( null, QObject::tr( "Save Primitive" ), name )); |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
524 | |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
525 | *f << new LDComment( descr ); |
311
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
526 | *f << new LDComment( fmt( "Name: %1", name )); |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
527 | *f << new LDComment( fmt( "Author: LDForge" )); |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
528 | *f << new LDComment( fmt( "!LDRAW_ORG Unofficial_%1Primitive", divs == hires ? "48_" : "" )); |
311
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
529 | *f << new LDComment( CALicense ); |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
530 | *f << new LDEmpty; |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
531 | *f << new LDBFC( LDBFC::CertifyCCW ); |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
532 | *f << new LDEmpty; |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
533 | *f << makePrimitive( type, segs, divs, num ); |
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
534 | |
311
c6e38e5e4f33
Further work on the primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
310
diff
changeset
|
535 | g_win->save( f, false ); |
310
c62edce5668c
Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents:
306
diff
changeset
|
536 | delete f; |
358 | 537 | } |