src/primitives.cpp

Sun, 07 Jul 2013 16:46:30 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Sun, 07 Jul 2013 16:46:30 +0300
changeset 358
7885fa5b09c5
parent 321
48e429bfd58c
child 363
75583c9f289d
permissions
-rw-r--r--

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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
36 static void populateCategories();
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
42 void loadPrimitives()
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
43 {
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
49 File conf( config::dirpath() + "prims.cfg", File::Read );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
50
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
51 if( !conf )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
54 PrimitiveLister::start();
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
55 }
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
56 else
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
57 {
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
58 for( str line : conf )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
59 {
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
60 int space = line.indexOf( " " );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
61
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
66 info.name = line.left( space );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
78 void recursiveGetFilenames( QDir dir, vector<str>& fnames )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
79 {
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
80 QFileInfoList flist = dir.entryInfoList();
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
81
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
82 for( const QFileInfo & info : flist )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
83 {
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
87 if( info.isDir() )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
97 void PrimitiveLister::work()
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
98 {
292
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 g_activePrimLister = this;
358
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
100 m_prims.clear();
292
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101
358
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
102 QDir dir( LDPaths::prims() );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
103 ulong baselen = dir.absolutePath().length();
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
107 assert( dir.exists() );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
108 recursiveGetFilenames( dir, fnames );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
109 emit starting( fnames.size() );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
110
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
111 for( str fname : fnames )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
112 {
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
116 info.name = fname.mid( baselen + 1 ); // make full path relative
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
117 info.name.replace( '/', '\\' ); // use DOS backslashes, they're expected
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
118 info.cat = null;
292
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119
358
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
125 if( info.title[0] == '0' )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
126 {
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
127 info.title.remove( 0, 1 ); // remove 0
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
138 for( Primitive & info : m_prims )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
139 fprint( conf, "%1 %2\n", info.name, info.title );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
140
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
154 void PrimitiveLister::start()
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
155 {
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
161 lister->moveToThread( listerThread );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
162 connect( lister, SIGNAL( starting( ulong )), g_win, SLOT( primitiveLoaderStart( ulong )) );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
163 connect( lister, SIGNAL( update( ulong )), g_win, SLOT( primitiveLoaderUpdate( ulong )) );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
164 connect( lister, SIGNAL( workDone() ), g_win, SLOT( primitiveLoaderEnd() ));
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
165 connect( listerThread, SIGNAL( started() ), lister, SLOT( work() ));
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
166 connect( listerThread, SIGNAL( finished() ), lister, SLOT( deleteLater() ));
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
170 static PrimitiveCategory* findCategory( str name )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
171 {
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
172 for( PrimitiveCategory & cat : g_PrimitiveCategories )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
182 static void populateCategories()
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
183 {
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
184 for( PrimitiveCategory & cat : g_PrimitiveCategories )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
189 if( !unmatched )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
197 for( Primitive & prim : g_primitives )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
203 for( PrimitiveCategory & cat : g_PrimitiveCategories )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
204 {
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
205 for( PrimitiveCategory::RegexEntry & entry : cat.regexes )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
206 {
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
207 switch( entry.type )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
220 if( matched )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
244 static void loadPrimitiveCatgories()
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
245 {
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
246 g_PrimitiveCategories.clear();
293
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
247
358
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
250 if( !f )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
251 f.open( ":/data/primitive-categories.cfg", File::Read );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
252
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
256 if( f )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
257 {
293
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
258 PrimitiveCategory cat;
358
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
259
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
260 for( str line : f )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
267 if( ( colon = line.indexOf( ":" )) == -1 )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
268 {
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
272 cat.regexes.clear();
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
273 cat.prims.clear();
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
274 cat.setName( line );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
275 }
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
276 else
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
277 {
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
281
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
317 double x0 = cos( ( i * 2 * pi ) / divs ),
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
318 x1 = cos( ( ( i + 1 ) * 2 * pi ) / divs ),
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
319 z0 = sin( ( i * 2 * pi ) / divs ),
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
327 {
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
328 vertex v0( x0, 0.0f, z0 ),
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
329 v1( x1, 0.0f, z1 );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
330
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
331 LDLine* line = new LDLine;
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
332 line->setVertex( 0, v0 );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
333 line->setVertex( 1, v1 );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
334 line->setColor( edgecolor );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
335 obj = line;
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
336 }
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
342 {
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
343 double x2, x3, z2, z3;
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
344 double y0, y1, y2, y3;
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
345
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
348 x2 = x1;
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
349 x3 = x0;
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
350 z2 = z1;
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
353 y0 = y1 = 0.0f;
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
354 y2 = y3 = 1.0f;
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
355 }
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
356 else
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
357 {
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
358 x2 = x1 * ( num + 1 );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
359 x3 = x0 * ( num + 1 );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
360 z2 = z1 * ( num + 1 );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
361 z3 = z0 * ( num + 1 );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
362
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
363 x0 *= num;
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
364 x1 *= num;
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
365 z0 *= num;
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
366 z1 *= num;
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
367
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
368 if( type == Ring )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
369 y0 = y1 = y2 = y3 = 0.0f;
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
370 else
310
c62edce5668c Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents: 306
diff changeset
371 {
358
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
372 y0 = y1 = 1.0f;
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
376
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
377 vertex v0( x0, y0, z0 ),
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
378 v1( x1, y1, z1 ),
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
379 v2( x2, y2, z2 ),
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
380 v3( x3, y3, z3 );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
381
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
382 LDQuad* quad = new LDQuad;
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
383 quad->setColor( maincolor );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
384 quad->setVertex( 0, v0 );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
385 quad->setVertex( 1, v1 );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
386 quad->setVertex( 2, v2 );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
387 quad->setVertex( 3, v3 );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
388 obj = quad;
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
389 }
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
394 {
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
395 double x2, z2;
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
396
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
397 if( type == Disc )
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
398 x2 = z2 = 0.0f;
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
399 else
310
c62edce5668c Begin converting the radial type into a primitive generator
Santeri Piippo <crimsondusk64@gmail.com>
parents: 306
diff changeset
400 {
358
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
401 x2 = ( x0 >= 0.0f ) ? 1.0f : -1.0f;
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
404
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
405 vertex v0( x0, 0.0f, z0 ),
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
406 v1( x1, 0.0f, z1 ),
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
407 v2( x2, 0.0f, z2 );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
408
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
409 // Disc negatives need to go the other way around, otherwise
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
410 // they'll end up upside-down.
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
411 LDTriangle* seg = new LDTriangle;
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
412 seg->setColor( maincolor );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
413 seg->setVertex( type == Disc ? 0 : 2, v0 );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
414 seg->setVertex( 1, v1 );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
415 seg->setVertex( type == Disc ? 2 : 0, v2 );
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
416 obj = seg;
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
417 }
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
442 static const str g_radialNameRoots[] =
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
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
7885fa5b09c5 restyle..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 321
diff changeset
537 }

mercurial