src/primitives.cpp

Wed, 16 Nov 2016 01:43:37 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 16 Nov 2016 01:43:37 +0200
changeset 1054
1f4c8a369e7c
parent 1053
2a48c0fff107
child 1055
c28d07a583ac
permissions
-rw-r--r--

Further cleanup

655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
1014
f0a8ecb6a357 Happy new year 2016!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1013
diff changeset
3 * Copyright (C) 2013 - 2016 Teemu Piippo
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 *
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 * (at your option) any later version.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 *
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * GNU General Public License for more details.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 *
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 */
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
1039
f68ebbae4efb Fixed compilation.
Teemu Piippo <teemu@hecknology.net>
parents: 1028
diff changeset
19 #include <QApplication>
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 #include <QDir>
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 #include <QRegExp>
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 #include <QFileDialog>
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 #include "ldDocument.h"
962
a4b463a7ee82 Rename MainWindow files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 958
diff changeset
24 #include "mainwindow.h"
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 #include "primitives.h"
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 #include "miscallenous.h"
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 #include "colors.h"
953
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
28 #include "ldpaths.h"
998
5be0ce31ce60 Now compiles again
Teemu Piippo <crimsondusk64@gmail.com>
parents: 988
diff changeset
29 #include "documentmanager.h"
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
31
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
32 PrimitiveManager::PrimitiveManager(QObject* parent) :
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
33 QObject(parent),
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
34 HierarchyElement(parent),
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
35 m_activeScanner(nullptr),
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
36 m_unmatched(nullptr) {}
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
37
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
38
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
39 PrimitiveScanner* PrimitiveManager::activeScanner()
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 {
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
41 return m_activeScanner;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
44
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
45 QString PrimitiveManager::getPrimitivesCfgPath() const
971
c00f9665a9f8 Now compiles and links but crashes shortly after startup.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 970
diff changeset
46 {
c00f9665a9f8 Now compiles and links but crashes shortly after startup.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 970
diff changeset
47 return qApp->applicationDirPath() + DIRSLASH "prims.cfg";
c00f9665a9f8 Now compiles and links but crashes shortly after startup.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 970
diff changeset
48 }
c00f9665a9f8 Now compiles and links but crashes shortly after startup.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 970
diff changeset
49
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
50
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
51 void PrimitiveManager::loadPrimitives()
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 // Try to load prims.cfg
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
54 QFile primitivesFile = {getPrimitivesCfgPath()};
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
56 if (not primitivesFile.open (QIODevice::ReadOnly))
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 // No prims.cfg, build it
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
59 startScan();
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 else
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 {
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
63 while (not primitivesFile.atEnd())
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 {
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
65 QString line = primitivesFile.readLine().simplified();
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
66 int space = line.indexOf(" ");
790
fe1b83f6ba82 - added primitives toolbox
Santeri Piippo <crimsondusk64@gmail.com>
parents: 784
diff changeset
67
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
68 if (space != -1)
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
69 {
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
70 Primitive info;
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
71 info.name = line.left(space);
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
72 info.title = line.mid(space + 1);
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
73 m_primitives.append(info);
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
74 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
77 populateCategories();
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
78 print(tr("%1 primitives loaded.") + "\n", m_primitives.size());
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
82
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
83 void PrimitiveManager::startScan()
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 {
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
85 if (m_activeScanner == nullptr)
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
86 {
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
87 loadCategories();
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
88 m_activeScanner = new PrimitiveScanner {this};
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
89 m_activeScanner->work();
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
90 connect(m_activeScanner, &PrimitiveScanner::workDone, this, [&]()
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
91 {
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
92 if (m_activeScanner)
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
93 {
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
94 m_primitives = m_activeScanner->scannedPrimitives();
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
95 populateCategories();
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
96 print(tr("%1 primitives scanned"), m_primitives.size());
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
97 delete m_activeScanner;
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
98 m_activeScanner = nullptr;
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
99 }
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
100 });
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
101 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
105 void PrimitiveManager::clearCategories()
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
106 {
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
107 for (PrimitiveCategory* category : m_categories)
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
108 delete category;
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
109
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
110 m_categories.clear();
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
111 }
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
112
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
113
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
114 void PrimitiveManager::populateCategories()
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 {
791
b1eae3a56eda - sort primitive categories alphabetically
Santeri Piippo <crimsondusk64@gmail.com>
parents: 790
diff changeset
116 loadCategories();
b1eae3a56eda - sort primitive categories alphabetically
Santeri Piippo <crimsondusk64@gmail.com>
parents: 790
diff changeset
117
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
118 for (PrimitiveCategory* category : m_categories)
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
119 category->primitives.clear();
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
121 for (Primitive& primitive : m_primitives)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123 bool matched = false;
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
124 primitive.category = nullptr;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126 // Go over the categories and their regexes, if and when there's a match,
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127 // the primitive's category is set to the category the regex beloings to.
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
128 for (PrimitiveCategory* category : m_categories)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 {
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
130 for (PrimitiveCategory::RegexEntry& entry : category->patterns)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132 switch (entry.type)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 {
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
134 case PrimitiveCategory::FilenamePattern:
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
135 // f-regex, check against filename
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
136 matched = entry.regex.exactMatch (primitive.name);
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
137 break;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
139 case PrimitiveCategory::TitlePattern:
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
140 // t-regex, check against title
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
141 matched = entry.regex.exactMatch (primitive.title);
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
142 break;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
143 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145 if (matched)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
146 {
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
147 primitive.category = category;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
148 break;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
150 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
152 // Drop off if a category was decided on.
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
153 if (primitive.category)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
154 break;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
155 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157 // If there was a match, add the primitive to the category.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158 // Otherwise, add it to the list of unmatched primitives.
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
159 if (primitive.category)
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
160 primitive.category->primitives << primitive;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
161 else
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
162 m_unmatched->primitives << primitive;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
163 }
791
b1eae3a56eda - sort primitive categories alphabetically
Santeri Piippo <crimsondusk64@gmail.com>
parents: 790
diff changeset
164
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
165 // Sort the categories. Note that we only do this here because we needed the original order for pattern matching.
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
166 qSort (m_categories.begin(), m_categories.end(),
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
167 [](PrimitiveCategory* const& one, PrimitiveCategory* const& other) -> bool
791
b1eae3a56eda - sort primitive categories alphabetically
Santeri Piippo <crimsondusk64@gmail.com>
parents: 790
diff changeset
168 {
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
169 return one->name() < other->name();
791
b1eae3a56eda - sort primitive categories alphabetically
Santeri Piippo <crimsondusk64@gmail.com>
parents: 790
diff changeset
170 });
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
171 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
172
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
173
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
174 void PrimitiveManager::loadCategories()
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
175 {
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
176 clearCategories();
971
c00f9665a9f8 Now compiles and links but crashes shortly after startup.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 970
diff changeset
177 QString path = ":/data/primitive-categories.cfg";
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
178 QFile categoriesFile = {path};
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
179
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
180 if (not categoriesFile.open (QIODevice::ReadOnly))
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
181 {
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
182 Critical(format(tr("Failed to open primitive categories: %1"), categoriesFile.errorString()));
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
183 return;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
184 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
185
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
186 PrimitiveCategory* category = nullptr;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
187
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
188 while (not categoriesFile.atEnd())
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
189 {
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
190 QString line = QString::fromUtf8(categoriesFile.readLine()).trimmed();
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
191
840
d077dd19bf9a - changed `&&` and `||` operators to their named variants `and` and `or`
Teemu Piippo <crimsondusk64@gmail.com>
parents: 806
diff changeset
192 if (line.length() == 0 or line[0] == '#')
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193 continue;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
195 int colon = line.indexOf (":");
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
196 if (colon == -1)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
197 {
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
198 if (category and category->isValidToInclude())
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
199 {
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
200 m_categories << category;
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
201 }
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
202 else if (category)
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
203 {
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
204 print (tr ("Warning: Category \"%1\" left without patterns"), category->name());
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
205 delete category;
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
206 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
207
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
208 category = new PrimitiveCategory (line);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
209 }
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
210 else if (category)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
211 {
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
212 QString typechar = line.left (colon);
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
213 PrimitiveCategory::PatternType type = PrimitiveCategory::FilenamePattern;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
214
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
215 if (typechar == "f")
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
216 {
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
217 type = PrimitiveCategory::FilenamePattern;
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
218 }
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
219 else if (typechar == "t")
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
220 {
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
221 type = PrimitiveCategory::TitlePattern;
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
222 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
223 else
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
224 {
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
225 print(tr("Warning: unknown pattern type \"%1\" on line \"%2\""), typechar, line);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
226 continue;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
227 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
228
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
229 QRegExp regex (line.mid (colon + 1));
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
230 PrimitiveCategory::RegexEntry entry = { regex, type };
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
231 category->patterns << entry;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
232 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
233 else
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
234 {
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
235 print("Warning: Rules given before the first category name");
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
236 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
237 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
238
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
239 if (category->isValidToInclude())
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
240 m_categories << category;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
241
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
242 // Add a category for unmatched primitives.
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
243 // Note: if this function is called the second time, m_unmatched has been
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
244 // deleted at the beginning of the function and is dangling at this point.
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
245 m_unmatched = new PrimitiveCategory {tr("Other")};
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
246 m_categories.append(m_unmatched);
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
247 categoriesFile.close();
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
248 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
249
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
250 LDObjectList PrimitiveModel::generateBody() const
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
251 {
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
252 LDObjectList objects;
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
253 QVector<int> conditionalLineSegments;
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
254 QVector<QLineF> circle = makeCircle(segments, divisions, 1);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
255
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
256 for (int i = 0; i < segments; ++i)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
257 {
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
258 double x0 = circle[i].x1();
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
259 double x1 = circle[i].x2();
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
260 double z0 = circle[i].y1();
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
261 double z1 = circle[i].y2();
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
262
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
263 switch (type)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
264 {
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
265 case Circle:
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
266 {
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
267 LDLine* line = LDSpawn<LDLine>();
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
268 line->setVertex(0, Vertex {x0, 0.0f, z0});
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
269 line->setVertex(1, Vertex {x1, 0.0f, z1});
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
270 line->setColor(EdgeColor);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
271 objects.append(line);
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
272 }
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
273 break;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
274
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
275 case Cylinder:
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
276 case Ring:
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
277 case Cone:
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
278 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
279 double x2, x3, z2, z3;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
280 double y0, y1, y2, y3;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
281
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
282 if (type == Cylinder)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
283 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
284 x2 = x1;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
285 x3 = x0;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
286 z2 = z1;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
287 z3 = z0;
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
288 y0 = y1 = 0.0;
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
289 y2 = y3 = 1.0;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
290 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
291 else
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
292 {
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
293 x2 = x1 * (ringNumber + 1);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
294 x3 = x0 * (ringNumber + 1);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
295 z2 = z1 * (ringNumber + 1);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
296 z3 = z0 * (ringNumber + 1);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
297 x0 *= ringNumber;
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
298 x1 *= ringNumber;
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
299 z0 *= ringNumber;
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
300 z1 *= ringNumber;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
301
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
302 if (type == Ring)
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
303 {
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
304 y0 = y1 = y2 = y3 = 0.0;
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
305 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
306 else
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
307 {
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
308 y0 = y1 = 1.0;
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
309 y2 = y3 = 0.0;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
310 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
311 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
312
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
313 Vertex v0 = {x0, y0, z0};
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
314 Vertex v1 = {x1, y1, z1};
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
315 Vertex v2 = {x2, y2, z2};
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
316 Vertex v3 = {x3, y3, z3};
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
317 LDQuad* quad = LDSpawn<LDQuad>(v0, v1, v2, v3);
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
318 quad->setColor(MainColor);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
319
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
320 if (type == Cylinder)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
321 quad->invert();
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
322
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
323 objects.append(quad);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
324
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
325 if (type == Cylinder or type == Cone)
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
326 conditionalLineSegments.append(i);
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
327 }
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
328 break;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
329
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
330 case Disc:
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
331 case DiscNegative:
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
332 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
333 double x2, z2;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
334
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
335 if (type == Disc)
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
336 {
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
337 x2 = z2 = 0.0;
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
338 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
339 else
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
340 {
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
341 x2 = (x0 >= 0.0) ? 1.0 : -1.0;
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
342 z2 = (z0 >= 0.0) ? 1.0 : -1.0;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
343 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
344
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
345 Vertex v0 = {x0, 0.0, z0};
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
346 Vertex v1 = {x1, 0.0, z1};
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
347 Vertex v2 = {x2, 0.0, z2};
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
348
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
349 // Disc negatives need to go the other way around, otherwise
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
350 // they'll end up upside-down.
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
351 LDTriangle* segment = LDSpawn<LDTriangle>();
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
352 segment->setColor(MainColor);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
353 segment->setVertex(type == Disc ? 0 : 2, v0);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
354 segment->setVertex(1, v1);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
355 segment->setVertex(type == Disc ? 2 : 0, v2);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
356 objects.append(segment);
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
357 }
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
358 break;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
359 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
360 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
361
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
362 // If this is not a full circle, we need a conditional line at the other
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
363 // end, too.
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
364 if (segments < divisions and not conditionalLineSegments.isEmpty())
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
365 conditionalLineSegments << segments;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
366
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
367 for (int i : conditionalLineSegments)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
368 {
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
369 Vertex v0 = {getRadialPoint(i, divisions, cos), 0.0f, getRadialPoint(i, divisions, sin)};
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
370 Vertex v1;
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
371 Vertex v2 = {getRadialPoint(i + 1, divisions, cos), 0.0f, getRadialPoint(i + 1, divisions, sin)};
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
372 Vertex v3 = {getRadialPoint(i - 1, divisions, cos), 0.0f, getRadialPoint(i - 1, divisions, sin)};
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
373
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
374 if (type == Cylinder)
768
29e6c5552388 - initial overhaul with smart pointers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 757
diff changeset
375 {
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
376 v1 = {v0[X], 1.0f, v0[Z]};
768
29e6c5552388 - initial overhaul with smart pointers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 757
diff changeset
377 }
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
378 else if (type == Cone)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
379 {
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
380 v1 = {v0[X] * (ringNumber + 1), 0.0, v0[Z] * (ringNumber + 1)};
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
381 v0 = {v0[X] * ringNumber, 1.0, v0[Z] * ringNumber};
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
382 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
383
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
384 LDCondLine* line = LDSpawn<LDCondLine>();
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
385 line->setColor(EdgeColor);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
386 line->setVertex(0, v0);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
387 line->setVertex(1, v1);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
388 line->setVertex(2, v2);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
389 line->setVertex(3, v3);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
390 objects.append(line);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
391 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
392
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
393 return objects;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
394 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
395
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
396
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
397 QString PrimitiveModel::typeName() const
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
398 {
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
399 return typeName(type);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
400 }
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
401
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
402
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
403 QString PrimitiveModel::typeName(PrimitiveModel::Type type)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
404 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
405 // Not translated as primitives are in English.
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
406 const char* names[] = {"Circle", "Cylinder", "Disc", "Disc Negative", "Ring", "Cone"};
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
407
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
408 if (type >= 0 and type < countof(names))
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
409 return names[type];
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
410 else
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
411 return "Unknown";
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
412 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
413
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
414
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
415 QString PrimitiveModel::makeFileName() const
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
416 {
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
417 int numerator = segments;
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
418 int denominator = divisions;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
419
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
420 // Simplify the fractional part, but the denominator must be at least 4.
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
421 simplify(numerator, denominator);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
422
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
423 if (denominator < 4)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
424 {
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
425 int factor = 4 / denominator;
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
426 numerator *= factor;
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
427 denominator *= factor;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
428 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
429
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
430 // Compose some general information: prefix, fraction, root, ring number
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
431 QString prefix = (divisions == LowResolution) ? "" : format ("%1/", divisions);
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
432 QString frac = format ("%1-%2", numerator, denominator);
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
433 static const char* roots[] = {"edge", "cyli", "disc", "ndis", "ring", "con"};
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
434 QString root = roots[type];
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
435 QString numberString = (type == Ring or type == Cone) ? format ("%1", ringNumber) : "";
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
436
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
437 // Truncate the root if necessary (7-16rin4.dat for instance).
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
438 // However, always keep the root at least 2 characters.
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
439 int extra = (frac.length() + numberString.length() + root.length()) - 8;
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
440 root.chop(qBound(0, extra, 2));
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
441
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
442 // Stick them all together and return the result.
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
443 return prefix + frac + root + numberString + ".dat";
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
444 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
445
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
446
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
447 LDDocument* PrimitiveManager::generatePrimitive(const PrimitiveModel& spec)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
448 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
449 // Make the description
1019
a2e4286adb0d Split PrimitivePrompt into its new source files, renamed it to GeneratePrimitivePrompt
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1018
diff changeset
450 QString fraction = QString::number ((float) spec.segments / spec.divisions);
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
451 QString fileName = spec.makeFileName();
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
452 QString description;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
453
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
454 // Ensure that there's decimals, even if they're 0.
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
455 if (fraction.indexOf(".") == -1)
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
456 fraction += ".0";
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
457
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
458 if (spec.type == PrimitiveModel::Ring or spec.type == PrimitiveModel::Cone)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
459 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
460 QString spacing =
1019
a2e4286adb0d Split PrimitivePrompt into its new source files, renamed it to GeneratePrimitivePrompt
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1018
diff changeset
461 (spec.ringNumber < 10) ? " " :
a2e4286adb0d Split PrimitivePrompt into its new source files, renamed it to GeneratePrimitivePrompt
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1018
diff changeset
462 (spec.ringNumber < 100) ? " " : "";
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
463 description = format("%1 %2%3 x %4", PrimitiveModel::typeName(spec.type), spacing, spec.ringNumber, fraction);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
464 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
465 else
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
466 description = format("%1 %2", PrimitiveModel::typeName(spec.type), fraction);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
467
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
468 // Prepend "Hi-Res" if 48/ primitive.
1019
a2e4286adb0d Split PrimitivePrompt into its new source files, renamed it to GeneratePrimitivePrompt
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1018
diff changeset
469 if (spec.divisions == HighResolution)
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
470 description.insert (0, "Hi-Res ");
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
471
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
472 LDDocument* document = m_window->newDocument();
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
473 document->setDefaultName(fileName);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
474
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
475 QString author = APPNAME;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
476 QString license = "";
1019
a2e4286adb0d Split PrimitivePrompt into its new source files, renamed it to GeneratePrimitivePrompt
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1018
diff changeset
477 bool hires = (spec.divisions == HighResolution);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
478
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
479 if (not m_config->defaultName().isEmpty())
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
480 {
1028
5877e49e9a28 Moved PreferredLicenseText into HierarchyElement and made the config pointer be passed to LDPaths. Now I can finally remove the Config global pointer.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1026
diff changeset
481 license = preferredLicenseText();
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
482 author = format("%1 [%2]", m_config->defaultName(), m_config->defaultUser());
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
483 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
484
768
29e6c5552388 - initial overhaul with smart pointers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 757
diff changeset
485 LDObjectList objs;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
486
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
487 objs.append(LDSpawn<LDComment>(description));
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
488 objs.append(LDSpawn<LDComment>(format("Name: %1", fileName)));
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
489 objs.append(LDSpawn<LDComment>(format("Author: %1", author)));
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
490 objs.append(LDSpawn<LDComment>(format("!LDRAW_ORG Unofficial_%1Primitive", hires ? "48_" : "")));
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
491 objs.append(LDSpawn<LDComment>(license));
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
492 objs.append(LDSpawn<LDEmpty>());
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
493 objs.append(LDSpawn<LDBfc>(BfcStatement::CertifyCCW));
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
494 objs.append(LDSpawn<LDEmpty>());
979
880d3fe9ac7c Refactor LDDocument's API
Teemu Piippo <crimsondusk64@gmail.com>
parents: 978
diff changeset
495 document->openForEditing();
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
496 document->history()->setIgnoring(false);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
497 document->addObjects(objs);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
498 document->addObjects(spec.generateBody());
978
4603d8fd063e Make documents members of the main window
Teemu Piippo <crimsondusk64@gmail.com>
parents: 971
diff changeset
499 document->addHistoryStep();
4603d8fd063e Make documents members of the main window
Teemu Piippo <crimsondusk64@gmail.com>
parents: 971
diff changeset
500 return document;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
501 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
502
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
503 /*
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
504 * PrimitiveManager :: getPrimitive
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
505 *
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
506 * Gets a primitive by the given model. If the primitive cannot be found, it will be automatically generated.
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
507 */
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
508 LDDocument* PrimitiveManager::getPrimitive(const PrimitiveModel& model)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
509 {
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
510 QString name = model.makeFileName();
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
511 LDDocument* document = m_window->documents()->getDocumentByName (name);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
512
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
513 if (document)
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
514 return document;
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
515 else
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
516 return generatePrimitive(model);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
517 }
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
518
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
519 /*
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
520 * PrimitiveManager :: populateTreeWidget
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
521 *
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
522 * Fills in a tree widget with all known primitives.
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
523 */
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
524 void PrimitiveManager::populateTreeWidget(QTreeWidget* tree, const QString& selectByDefault)
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
525 {
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
526 tree->clear();
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
527
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
528 for (PrimitiveCategory* category : m_categories)
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
529 {
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
530 PrimitiveTreeItem* parentItem = new PrimitiveTreeItem {tree, nullptr};
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
531 parentItem->setText(0, category->name());
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
532 //QList<QTreeWidgetItem*> subfileItems;
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
533
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
534 for (Primitive& primitive : category->primitives)
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
535 {
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
536 PrimitiveTreeItem* item = new PrimitiveTreeItem {parentItem, &primitive};
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
537 item->setText(0, format("%1 - %2", primitive.name, primitive.title));
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
538 //subfileItems.append(item);
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
539
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
540 // If this primitive is the one the current object points to,
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
541 // select it by default
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
542 if (selectByDefault == primitive.name)
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
543 tree->setCurrentItem(item);
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
544 }
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
545
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
546 tree->addTopLevelItem(parentItem);
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
547 }
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
548 }
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
549
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
550
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
551 //
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
552 // ---------------------------------------------------------------------------------------------------------------------
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
553 //
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
554
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
555
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
556 PrimitiveCategory::PrimitiveCategory (QString name, QObject* parent) :
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
557 QObject (parent),
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
558 m_name (name) {}
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
559
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
560
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
561 bool PrimitiveCategory::isValidToInclude()
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
562 {
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
563 return not patterns.isEmpty();
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
564 }
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
565
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
566
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
567 QString PrimitiveCategory::name() const
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
568 {
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
569 return m_name;
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
570 }
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
571
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
572
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
573 //
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
574 // ---------------------------------------------------------------------------------------------------------------------
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
575 //
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
576
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
577
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
578 PrimitiveScanner::PrimitiveScanner (PrimitiveManager* parent) :
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
579 QObject(parent),
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
580 HierarchyElement(parent),
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
581 m_manager(parent),
1021
f40c792c9334 Replaced GetRecursiveFilenames with QDirIterator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1020
diff changeset
582 m_iterator(LDPaths::primitivesDir(), QDirIterator::Subdirectories)
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
583 {
1021
f40c792c9334 Replaced GetRecursiveFilenames with QDirIterator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1020
diff changeset
584 m_basePathLength = LDPaths::primitivesDir().absolutePath().length();
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
585 print("Scanning primitives...");
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
586 }
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
587
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
588
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
589 const QList<Primitive>& PrimitiveScanner::scannedPrimitives() const
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
590 {
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
591 return m_prims;
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
592 }
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
593
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
594
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
595 void PrimitiveScanner::work()
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
596 {
1021
f40c792c9334 Replaced GetRecursiveFilenames with QDirIterator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1020
diff changeset
597 for (int i = 0; m_iterator.hasNext() and i < 100; ++i)
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
598 {
1021
f40c792c9334 Replaced GetRecursiveFilenames with QDirIterator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1020
diff changeset
599 QString filename = m_iterator.next();
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
600 QFile file (filename);
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
601
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
602 if (not file.open (QIODevice::ReadOnly))
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
603 continue;
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
604
1021
f40c792c9334 Replaced GetRecursiveFilenames with QDirIterator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1020
diff changeset
605 Primitive primitive;
f40c792c9334 Replaced GetRecursiveFilenames with QDirIterator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1020
diff changeset
606 primitive.name = filename.mid (m_basePathLength + 1); // make full path relative
f40c792c9334 Replaced GetRecursiveFilenames with QDirIterator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1020
diff changeset
607 primitive.name.replace ('/', '\\'); // use DOS backslashes, they're expected
f40c792c9334 Replaced GetRecursiveFilenames with QDirIterator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1020
diff changeset
608 primitive.category = nullptr;
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
609 QByteArray titledata = file.readLine();
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
610
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
611 if (titledata != QByteArray())
1021
f40c792c9334 Replaced GetRecursiveFilenames with QDirIterator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1020
diff changeset
612 primitive.title = QString::fromUtf8 (titledata);
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
613
1021
f40c792c9334 Replaced GetRecursiveFilenames with QDirIterator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1020
diff changeset
614 primitive.title = primitive.title.simplified();
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
615
1021
f40c792c9334 Replaced GetRecursiveFilenames with QDirIterator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1020
diff changeset
616 if (primitive.title[0] == '0')
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
617 {
1021
f40c792c9334 Replaced GetRecursiveFilenames with QDirIterator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1020
diff changeset
618 primitive.title.remove (0, 1); // remove 0
f40c792c9334 Replaced GetRecursiveFilenames with QDirIterator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1020
diff changeset
619 primitive.title = primitive.title.simplified();
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
620 }
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
621
1021
f40c792c9334 Replaced GetRecursiveFilenames with QDirIterator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1020
diff changeset
622 m_prims << primitive;
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
623 }
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
624
1021
f40c792c9334 Replaced GetRecursiveFilenames with QDirIterator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1020
diff changeset
625 if (not m_iterator.hasNext())
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
626 {
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
627 // Done with primitives, now save to a config file
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
628 QString path = m_manager->getPrimitivesCfgPath();
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
629 QFile configFile (path);
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
630
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
631 if (configFile.open (QIODevice::WriteOnly | QIODevice::Text))
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
632 {
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
633 for (Primitive& info : m_prims)
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
634 fprint (configFile, "%1 %2\r\n", info.name, info.title);
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
635
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
636 configFile.close();
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
637 }
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
638 else
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
639 {
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
640 errorPrompt(m_window, format("Couldn't write primitive list %1: %2", path, configFile.errorString()));
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
641 }
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
642
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
643 emit workDone();
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
644 }
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
645 else
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
646 {
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
647 // Defer to event loop, pick up the work later
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
648 QMetaObject::invokeMethod (this, "work", Qt::QueuedConnection);
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
649 }
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
650 }
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
651
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
652
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
653 //
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
654 // ---------------------------------------------------------------------------------------------------------------------
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
655 //
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
656
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
657
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
658 PrimitiveTreeItem::PrimitiveTreeItem (QTreeWidgetItem* parent, Primitive* info) :
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
659 QTreeWidgetItem (parent),
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
660 m_primitive (info) {}
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
661
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
662
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
663 PrimitiveTreeItem::PrimitiveTreeItem (QTreeWidget* parent, Primitive* info) :
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
664 QTreeWidgetItem (parent),
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
665 m_primitive (info) {}
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
666
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
667
1018
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
668 Primitive* PrimitiveTreeItem::primitive() const
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
669 {
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
670 return m_primitive;
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
671 }

mercurial