src/primitives.cpp

Thu, 27 Jun 2013 12:13:15 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Thu, 27 Jun 2013 12:13:15 +0300
changeset 304
a808f95b6536
parent 297
2cbd8ac8293b
child 306
fef00a6cbff0
permissions
-rw-r--r--

..

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
2cbd8ac8293b Added license header to primitives.cpp
Santeri Piippo <crimsondusk64@gmail.com>
parents: 294
diff changeset
4 *
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.
2cbd8ac8293b Added license header to primitives.cpp
Santeri Piippo <crimsondusk64@gmail.com>
parents: 294
diff changeset
9 *
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.
2cbd8ac8293b Added license header to primitives.cpp
Santeri Piippo <crimsondusk64@gmail.com>
parents: 294
diff changeset
14 *
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>
292
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 #include "file.h"
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 #include "gui.h"
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 #include "primitives.h"
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25
293
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
26 vector<PrimitiveCategory> g_PrimitiveCategories;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
27 static PrimitiveLister* g_activePrimLister = null;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
28 static bool g_primListerMutex = false;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
29 vector<Primitive> g_primitives;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
30
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
31 static void populateCategories ();
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
32 static void loadPrimitiveCatgories ();
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
33
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
34 // =============================================================================
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
36 // =============================================================================
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
37 void loadPrimitives () {
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
38 print ("Loading primitives...\n");
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 loadPrimitiveCatgories ();
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
41
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
42 // Try to load prims.cfg
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
43 File conf (config::dirpath () + "prims.cfg", File::Read);
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
44 if (!conf) {
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
45 // No prims.cfg, build it
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
46 PrimitiveLister::start ();
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
47 } else {
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
48 for (str line : conf) {
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
49 int space = line.indexOf (" ");
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
50 if (space == -1)
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
51 continue;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
52
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
53 Primitive info;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
54 info.name = line.left (space);
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
55 info.title = line.mid (space + 1);
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
56 g_primitives << info;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
57 }
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
58 }
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
59
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
60 populateCategories ();
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
61 }
292
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 // =============================================================================
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 // =============================================================================
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 void recursiveGetFilenames (QDir dir, vector<str>& fnames) {
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 QFileInfoList flist = dir.entryInfoList ();
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 for (const QFileInfo& info : flist) {
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 if (info.fileName () == "." || info.fileName () == "..")
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 continue; // skip . and ..
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 if (info.isDir ())
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 recursiveGetFilenames (QDir (info.absoluteFilePath ()), fnames);
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 else
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 fnames << info.absoluteFilePath ();
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 }
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78
293
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
79 // =============================================================================
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
80 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
81 // =============================================================================
292
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 void PrimitiveLister::work () {
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 g_activePrimLister = this;
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 m_prims.clear ();
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 QDir dir (LDPaths::prims ());
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 assert (dir.exists ());
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 ulong baselen = dir.absolutePath ().length ();
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 vector<str> fnames;
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 recursiveGetFilenames (dir, fnames);
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 emit starting (fnames.size ());
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 ulong i = 0;
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 for (str fname : fnames) {
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 File f (fname, File::Read);
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 Primitive info;
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 info.name = fname.mid (baselen + 1); // make full path relative
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 info.name.replace ('/', '\\'); // use DOS backslashes, they're expected
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 if (!f.readLine (info.title))
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 info.title = "";
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 info.title = info.title.simplified ();
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 if (info.title[0] == '0') {
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 info.title.remove (0, 1); // remove 0
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110 info.title = info.title.simplified ();
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 }
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112
293
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
113 // Figure which category to use
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
114 info.cat = null;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
115
292
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116 m_prims << info;
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 emit update (++i);
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118 }
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 // Save to a config file
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 File conf (config::dirpath () + "prims.cfg", File::Write);
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122 for (Primitive& info : m_prims)
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123 fprint (conf, "%1 %2\n", info.name, info.title);
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125 conf.close ();
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127 g_primListerMutex = true;
293
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
128 g_primitives = m_prims;
292
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 g_primListerMutex = false;
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 g_activePrimLister = null;
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 emit workDone ();
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132 }
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133
293
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
134 // =============================================================================
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
135 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
136 // =============================================================================
292
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137 void PrimitiveLister::start () {
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138 if (g_activePrimLister)
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139 return;
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141 PrimitiveLister* lister = new PrimitiveLister;
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 QThread* listerThread = new QThread;
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
143 lister->moveToThread (listerThread);
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144 connect (lister, SIGNAL (starting (ulong)), g_win, SLOT (primitiveLoaderStart (ulong)));
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145 connect (lister, SIGNAL (update (ulong)), g_win, SLOT (primitiveLoaderUpdate (ulong)));
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
146 connect (lister, SIGNAL (workDone ()), g_win, SLOT (primitiveLoaderEnd ()));
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147 connect (listerThread, SIGNAL (started ()), lister, SLOT (work ()));
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
148 connect (listerThread, SIGNAL (finished ()), lister, SLOT (deleteLater ()));
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149 listerThread->start ();
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
150 }
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151
293
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
152 static PrimitiveCategory* findCategory (str name) {
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
153 for (PrimitiveCategory& cat : g_PrimitiveCategories)
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
154 if (cat.name () == name)
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
155 return &cat;
292
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156
293
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
157 return null;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
158 }
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
159
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
160 // =============================================================================
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
161 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
162 // =============================================================================
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
163 static void populateCategories () {
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
164 for (PrimitiveCategory& cat : g_PrimitiveCategories)
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
165 cat.prims.clear ();
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
166
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
167 PrimitiveCategory* unmatched = findCategory ("Other");
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
168
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
169 if (!unmatched) {
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
170 // Shouldn't happen.. but catch it anyway.
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
171 print ("No `Other` category found! Creating one...\n");
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
172 PrimitiveCategory cat;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
173 cat.setName ("Other");
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
174 unmatched = &(g_PrimitiveCategories << cat);
292
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
175 }
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176
293
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
177 for (Primitive& prim : g_primitives) {
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
178 bool matched = false;
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 // 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
181 // the primitive's category is set to the category the regex beloings to.
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
182 for (PrimitiveCategory& cat : g_PrimitiveCategories) {
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
183 for (PrimitiveCategory::RegexEntry& entry : cat.regexes) {
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
184 switch (entry.type) {
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
185 case PrimitiveCategory::Filename:
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
186 // f-regex, check against filename
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
187 matched = entry.regex.exactMatch (prim.name);
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
188 break;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
189
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
190 case PrimitiveCategory::Title:
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
191 // t-regex, check against title
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
192 matched = entry.regex.exactMatch (prim.title);
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
193 break;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
194 }
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
195
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
196 if (matched) {
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
197 prim.cat = &cat;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
198 break;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
199 }
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
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
202 // Drop out if a category was decided on.
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
203 if (prim.cat)
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
204 break;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
205 }
292
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
206
293
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
207 // 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
208 // Otherwise, add it to the list of unmatched primitives.
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
209 if (prim.cat)
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
210 prim.cat->prims << prim;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
211 else
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
212 unmatched->prims << prim;
292
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
213 }
293
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
214 }
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
215
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
216 // =============================================================================
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
217 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
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 static void loadPrimitiveCatgories () {
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
220 g_PrimitiveCategories.clear ();
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
221
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
222 File f (config::dirpath () + "primregexps.cfg", File::Read);
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
223
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
224 if (!f)
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
225 f.open (":/data/primitive-categories.cfg", File::Read);
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 if (!f)
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
228 critical ("Failed to open primitive categories!");
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
229
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
230 if (f) {
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
231 PrimitiveCategory cat;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
232
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
233 for (str line : f) {
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
234 int colon;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
235
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
236 if (line.length () == 0 || line[0] == '#')
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
237 continue;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
238
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
239 if ((colon = line.indexOf (":")) == -1) {
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
240 if (cat.regexes.size () > 0)
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
241 g_PrimitiveCategories << cat;
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 cat.regexes.clear ();
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
244 cat.prims.clear ();
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
245 cat.setName (line);
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
246 } else {
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
247 str cmd = line.left (colon);
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
248
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
249 PrimitiveCategory::Type type = PrimitiveCategory::Filename;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
250 if (cmd == "f") {
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
251 type = PrimitiveCategory::Filename;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
252 } else if (cmd == "t") {
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
253 type = PrimitiveCategory::Title;
294
2892deee4c1b Better categorized Technic primitives
Santeri Piippo <crimsondusk64@gmail.com>
parents: 293
diff changeset
254 } else
2892deee4c1b Better categorized Technic primitives
Santeri Piippo <crimsondusk64@gmail.com>
parents: 293
diff changeset
255 continue;
293
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
256
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
257 QRegExp regex (line.mid (colon + 1));
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
258 PrimitiveCategory::RegexEntry entry = { regex, type };
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
259 cat.regexes << entry;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
260 }
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
261 }
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
262
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
263 if (cat.regexes.size () > 0)
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
264 g_PrimitiveCategories << cat;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
265 }
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
266
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
267 // Add a category for unmatched primitives
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
268 PrimitiveCategory cat;
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
269 cat.setName ("Other");
a0ed563e14b2 Added support for regex-based primitive categorization
Santeri Piippo <crimsondusk64@gmail.com>
parents: 292
diff changeset
270 g_PrimitiveCategories << cat;
292
4779ca562d5e Moved primitive handling to new code files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
271 }

mercurial