src/primitives.cpp

Sat, 24 Mar 2018 11:57:24 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 24 Mar 2018 11:57:24 +0200
changeset 1319
39d7a9642eea
parent 1318
568fcfc6da71
child 1320
bdb4804bc09c
permissions
-rw-r--r--

reorganized headers

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
1072
9ce9496427f2 Happy new year 2017!
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
3 * Copyright (C) 2013 - 2017 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>
1156
c20ee66b6705 Removed the Critical and Question functions, replaced with direct QMessageBox uses. Removed unused code from mainwindow.h.
Teemu Piippo <teemu@hecknology.net>
parents: 1149
diff changeset
20 #include <QMessageBox>
1145
02264bf0108d Renamed ldDocument.cpp → lddocument.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1141
diff changeset
21 #include "lddocument.h"
962
a4b463a7ee82 Rename MainWindow files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 958
diff changeset
22 #include "mainwindow.h"
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 #include "primitives.h"
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 #include "colors.h"
998
5be0ce31ce60 Now compiles again
Teemu Piippo <crimsondusk64@gmail.com>
parents: 988
diff changeset
25 #include "documentmanager.h"
1318
568fcfc6da71 removed unnecessary files and includes
Teemu Piippo <teemu@hecknology.net>
parents: 1312
diff changeset
26 #include "editHistory.h"
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
27 #include "algorithms/geometry.h"
1117
efcb47c64a72 Moved LDComment and LDEmpty into their own source file pairs.
Teemu Piippo <teemu@hecknology.net>
parents: 1090
diff changeset
28 #include "linetypes/comment.h"
1141
7dc2c981937e Split LDConditionalEdge and LDEdgeLine into new source pairs
Teemu Piippo <teemu@hecknology.net>
parents: 1139
diff changeset
29 #include "linetypes/conditionaledge.h"
7dc2c981937e Split LDConditionalEdge and LDEdgeLine into new source pairs
Teemu Piippo <teemu@hecknology.net>
parents: 1139
diff changeset
30 #include "linetypes/edgeline.h"
1117
efcb47c64a72 Moved LDComment and LDEmpty into their own source file pairs.
Teemu Piippo <teemu@hecknology.net>
parents: 1090
diff changeset
31 #include "linetypes/empty.h"
1149
502c866b8512 Moved LDQuadrilateral into its own source pair.
Teemu Piippo <teemu@hecknology.net>
parents: 1148
diff changeset
32 #include "linetypes/quadrilateral.h"
1148
96cb15a7611f Moved LDTriangle into its own source pair.
Teemu Piippo <teemu@hecknology.net>
parents: 1145
diff changeset
33 #include "linetypes/triangle.h"
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
34
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
35 PrimitiveManager::PrimitiveManager(QObject* parent) :
1283
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
36 QAbstractItemModel {parent},
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
37 HierarchyElement {parent},
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
38 m_activeScanner {nullptr},
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
39 m_unmatched {nullptr} {}
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
40
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
41
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
42 PrimitiveScanner* PrimitiveManager::activeScanner()
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 {
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
44 return m_activeScanner;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
47
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
48 QString PrimitiveManager::getPrimitivesCfgPath() const
971
c00f9665a9f8 Now compiles and links but crashes shortly after startup.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 970
diff changeset
49 {
c00f9665a9f8 Now compiles and links but crashes shortly after startup.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 970
diff changeset
50 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
51 }
c00f9665a9f8 Now compiles and links but crashes shortly after startup.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 970
diff changeset
52
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
53
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
54 void PrimitiveManager::loadPrimitives()
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 // Try to load prims.cfg
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
57 QFile primitivesFile = {getPrimitivesCfgPath()};
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
59 if (not primitivesFile.open (QIODevice::ReadOnly))
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 // 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
62 startScan();
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 else
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 {
1285
0d165d277562 added signal emissions
Teemu Piippo <teemu@hecknology.net>
parents: 1284
diff changeset
66 emit layoutAboutToBeChanged();
0d165d277562 added signal emissions
Teemu Piippo <teemu@hecknology.net>
parents: 1284
diff changeset
67 m_primitives.clear();
0d165d277562 added signal emissions
Teemu Piippo <teemu@hecknology.net>
parents: 1284
diff changeset
68
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
69 while (not primitivesFile.atEnd())
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 {
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
71 QString line = primitivesFile.readLine().simplified();
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
72 int space = line.indexOf(" ");
790
fe1b83f6ba82 - added primitives toolbox
Santeri Piippo <crimsondusk64@gmail.com>
parents: 784
diff changeset
73
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
74 if (space != -1)
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
75 {
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
76 Primitive info;
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
77 info.name = line.left(space);
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
78 info.title = line.mid(space + 1);
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
79 m_primitives.append(info);
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
80 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
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 populateCategories();
1285
0d165d277562 added signal emissions
Teemu Piippo <teemu@hecknology.net>
parents: 1284
diff changeset
84 emit layoutChanged();
1065
c8ecddbd99e9 Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents: 1064
diff changeset
85 print(tr("%1 primitives loaded.") + "\n", countof(m_primitives));
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
89
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
90 void PrimitiveManager::startScan()
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 {
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
92 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
93 {
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
94 loadCategories();
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
95 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
96 m_activeScanner->work();
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
97 connect(m_activeScanner, &PrimitiveScanner::workDone, this, [&]()
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
98 {
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
99 if (m_activeScanner)
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
100 {
1284
7da74697b7d2 moved emit calls
Teemu Piippo <teemu@hecknology.net>
parents: 1283
diff changeset
101 emit layoutAboutToBeChanged();
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
102 m_primitives = m_activeScanner->scannedPrimitives();
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
103 populateCategories();
1284
7da74697b7d2 moved emit calls
Teemu Piippo <teemu@hecknology.net>
parents: 1283
diff changeset
104 emit layoutChanged();
1065
c8ecddbd99e9 Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents: 1064
diff changeset
105 print(tr("%1 primitives scanned"), countof(m_primitives));
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
106 delete m_activeScanner;
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
107 m_activeScanner = nullptr;
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
108 }
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
109 });
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
110 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
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::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
115 {
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
116 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
117 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
118
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 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
120 }
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
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
122
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
123 void PrimitiveManager::populateCategories()
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124 {
791
b1eae3a56eda - sort primitive categories alphabetically
Santeri Piippo <crimsondusk64@gmail.com>
parents: 790
diff changeset
125 loadCategories();
b1eae3a56eda - sort primitive categories alphabetically
Santeri Piippo <crimsondusk64@gmail.com>
parents: 790
diff changeset
126
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
127 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
128 category->primitives.clear();
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 (Primitive& primitive : m_primitives)
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 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
133 primitive.category = nullptr;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
135 // 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
136 // 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
137 for (PrimitiveCategory* category : m_categories)
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 for (PrimitiveCategory::RegexEntry& entry : category->patterns)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141 switch (entry.type)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 {
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
143 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
144 // 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
145 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
146 break;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147
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
148 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
149 // 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
150 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
151 break;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
152 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
153
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
154 if (matched)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
155 {
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
156 primitive.category = category;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157 break;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
159 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
160
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
161 // 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
162 if (primitive.category)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
163 break;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
164 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
165
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
166 // If there was a match, add the primitive to the category.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
167 // 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
168 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
169 primitive.category->primitives << primitive;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
170 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
171 m_unmatched->primitives << primitive;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
172 }
791
b1eae3a56eda - sort primitive categories alphabetically
Santeri Piippo <crimsondusk64@gmail.com>
parents: 790
diff changeset
173
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
174 // 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
175 qSort (m_categories.begin(), m_categories.end(),
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
176 [](PrimitiveCategory* const& one, PrimitiveCategory* const& other) -> bool
791
b1eae3a56eda - sort primitive categories alphabetically
Santeri Piippo <crimsondusk64@gmail.com>
parents: 790
diff changeset
177 {
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
178 return one->name() < other->name();
791
b1eae3a56eda - sort primitive categories alphabetically
Santeri Piippo <crimsondusk64@gmail.com>
parents: 790
diff changeset
179 });
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
180 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
181
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
182
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
183 void PrimitiveManager::loadCategories()
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
184 {
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
185 clearCategories();
971
c00f9665a9f8 Now compiles and links but crashes shortly after startup.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 970
diff changeset
186 QString path = ":/data/primitive-categories.cfg";
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
187 QFile categoriesFile = {path};
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
188
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
189 if (not categoriesFile.open (QIODevice::ReadOnly))
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
190 {
1156
c20ee66b6705 Removed the Critical and Question functions, replaced with direct QMessageBox uses. Removed unused code from mainwindow.h.
Teemu Piippo <teemu@hecknology.net>
parents: 1149
diff changeset
191 QString message = format(tr("Failed to open primitive categories: %1"), categoriesFile.errorString());
c20ee66b6705 Removed the Critical and Question functions, replaced with direct QMessageBox uses. Removed unused code from mainwindow.h.
Teemu Piippo <teemu@hecknology.net>
parents: 1149
diff changeset
192 QMessageBox::critical(m_window, tr("Cannot open categories"), message);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193 return;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
195
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
196 PrimitiveCategory* category = nullptr;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
197
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
198 while (not categoriesFile.atEnd())
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
199 {
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
200 QString line = QString::fromUtf8(categoriesFile.readLine()).trimmed();
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
201
1064
4c7a353cf583 Replaced '.length()' with 'length()' where appropriate
Teemu Piippo <teemu@hecknology.net>
parents: 1063
diff changeset
202 if (line.isEmpty() or line[0] == '#')
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
203 continue;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
204
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
205 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
206 if (colon == -1)
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 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
209 {
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 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
211 }
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 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
213 {
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
214 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
215 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
216 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
217
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
218 category = new PrimitiveCategory (line);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
219 }
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
220 else if (category)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
221 {
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
222 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
223 PrimitiveCategory::PatternType type = PrimitiveCategory::FilenamePattern;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
224
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
225 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
226 {
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
227 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
228 }
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
229 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
230 {
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 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
232 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
233 else
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
234 {
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
235 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
236 continue;
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
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
239 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
240 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
241 category->patterns << entry;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
242 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
243 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
244 {
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
245 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
246 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
247 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
248
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
249 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
250 m_categories << category;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
251
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
252 // Add a category for unmatched primitives.
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
253 // 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
254 // 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
255 m_unmatched = new PrimitiveCategory {tr("Other")};
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
256 m_categories.append(m_unmatched);
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
257 categoriesFile.close();
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
258 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
259
1080
6dac2d52bd9a Removed the LDSpawn function and replaced its last use cases.
Teemu Piippo <teemu@hecknology.net>
parents: 1072
diff changeset
260 void PrimitiveModel::generateBody(Model& model) const
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
261 {
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
262 QVector<int> conditionalLineSegments;
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
263 QVector<QLineF> circle = makeCircle(segments, divisions, 1);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
264
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
265 for (int i = 0; i < segments; ++i)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
266 {
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
267 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
268 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
269 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
270 double z1 = circle[i].y2();
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
271
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
272 switch (type)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
273 {
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
274 case Circle:
1080
6dac2d52bd9a Removed the LDSpawn function and replaced its last use cases.
Teemu Piippo <teemu@hecknology.net>
parents: 1072
diff changeset
275 {
1139
51303023d651 LDObject subclass renaming:
Teemu Piippo <teemu@hecknology.net>
parents: 1117
diff changeset
276 LDEdgeLine* line = model.emplace<LDEdgeLine>();
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
277 line->setVertex(0, Vertex {x0, 0.0f, z0});
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
278 line->setVertex(1, Vertex {x1, 0.0f, z1});
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
279 line->setColor(EdgeColor);
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
280 }
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
281 break;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
282
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
283 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
284 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
285 case Cone:
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
286 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
287 double x2, x3, z2, z3;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
288 double y0, y1, y2, y3;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
289
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
290 if (type == Cylinder)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
291 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
292 x2 = x1;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
293 x3 = x0;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
294 z2 = z1;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
295 z3 = z0;
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
296 y0 = y1 = 0.0;
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
297 y2 = y3 = 1.0;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
298 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
299 else
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
300 {
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
301 x2 = x1 * (ringNumber + 1);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
302 x3 = x0 * (ringNumber + 1);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
303 z2 = z1 * (ringNumber + 1);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
304 z3 = z0 * (ringNumber + 1);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
305 x0 *= ringNumber;
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
306 x1 *= ringNumber;
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
307 z0 *= ringNumber;
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
308 z1 *= ringNumber;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
309
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
310 if (type == Ring)
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
311 {
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
312 y0 = y1 = y2 = y3 = 0.0;
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
313 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
314 else
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
315 {
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
316 y0 = y1 = 1.0;
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
317 y2 = y3 = 0.0;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
318 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
319 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
320
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
321 Vertex v0 = {x0, y0, z0};
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
322 Vertex v1 = {x1, y1, z1};
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
323 Vertex v2 = {x2, y2, z2};
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
324 Vertex v3 = {x3, y3, z3};
1261
5d2c9d36da9d Removed LDObject::invert, inversion code moved to basic toolset
Santeri Piippo
parents: 1156
diff changeset
325
5d2c9d36da9d Removed LDObject::invert, inversion code moved to basic toolset
Santeri Piippo
parents: 1156
diff changeset
326 if (type == Cylinder)
5d2c9d36da9d Removed LDObject::invert, inversion code moved to basic toolset
Santeri Piippo
parents: 1156
diff changeset
327 qSwap(v1, v3);
5d2c9d36da9d Removed LDObject::invert, inversion code moved to basic toolset
Santeri Piippo
parents: 1156
diff changeset
328
1139
51303023d651 LDObject subclass renaming:
Teemu Piippo <teemu@hecknology.net>
parents: 1117
diff changeset
329 LDQuadrilateral* quad = model.emplace<LDQuadrilateral>(v0, v1, v2, v3);
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
330 quad->setColor(MainColor);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
331
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
332 if (type == Cylinder or type == Cone)
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
333 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
334 }
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
335 break;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
336
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
337 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
338 case DiscNegative:
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
339 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
340 double x2, z2;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
341
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
342 if (type == Disc)
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
343 {
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
344 x2 = z2 = 0.0;
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
345 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
346 else
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
347 {
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
348 x2 = (x0 >= 0.0) ? 1.0 : -1.0;
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
349 z2 = (z0 >= 0.0) ? 1.0 : -1.0;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
350 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
351
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
352 Vertex v0 = {x0, 0.0, z0};
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
353 Vertex v1 = {x1, 0.0, z1};
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
354 Vertex v2 = {x2, 0.0, z2};
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
355
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
356 // Disc negatives need to go the other way around, otherwise
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
357 // they'll end up upside-down.
1080
6dac2d52bd9a Removed the LDSpawn function and replaced its last use cases.
Teemu Piippo <teemu@hecknology.net>
parents: 1072
diff changeset
358 LDTriangle* segment = model.emplace<LDTriangle>();
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
359 segment->setColor(MainColor);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
360 segment->setVertex(type == Disc ? 0 : 2, v0);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
361 segment->setVertex(1, v1);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
362 segment->setVertex(type == Disc ? 2 : 0, v2);
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
363 }
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
364 break;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
365 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
366 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
367
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
368 // 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
369 // end, too.
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
370 if (segments < divisions and not conditionalLineSegments.isEmpty())
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
371 conditionalLineSegments << segments;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
372
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
373 for (int i : conditionalLineSegments)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
374 {
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
375 QPointF p0 = ::pointOnCircumference(i, divisions);
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
376 QPointF p2 = ::pointOnCircumference(i + 1, divisions);
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
377 QPointF p3 = ::pointOnCircumference(i - 1, divisions);
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
378 Vertex v0 = {p0.x(), 0.0, p0.y()};
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
379 Vertex v1;
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
380 Vertex v2 = {p2.x(), 0.0, p2.y()};
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
381 Vertex v3 = {p3.x(), 0.0, p3.y()};
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
382
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
383 if (type == Cylinder)
768
29e6c5552388 - initial overhaul with smart pointers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 757
diff changeset
384 {
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
385 v1 = {v0.x, 1.0f, v0.z};
768
29e6c5552388 - initial overhaul with smart pointers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 757
diff changeset
386 }
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
387 else if (type == Cone)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
388 {
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
389 v1 = {v0.x * (ringNumber + 1), 0.0, v0.z * (ringNumber + 1)};
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
390 v0 = {v0.x * ringNumber, 1.0, v0.z * ringNumber};
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
1139
51303023d651 LDObject subclass renaming:
Teemu Piippo <teemu@hecknology.net>
parents: 1117
diff changeset
393 LDConditionalEdge* line = model.emplace<LDConditionalEdge>();
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
394 line->setColor(EdgeColor);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
395 line->setVertex(0, v0);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
396 line->setVertex(1, v1);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
397 line->setVertex(2, v2);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
398 line->setVertex(3, v3);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
399 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
400 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
401
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
402
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
403 QString PrimitiveModel::typeName() const
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
404 {
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
405 return typeName(type);
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
406 }
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
407
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
408
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
409 QString PrimitiveModel::typeName(PrimitiveModel::Type type)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
410 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
411 // Not translated as primitives are in English.
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
412 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
413
1065
c8ecddbd99e9 Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents: 1064
diff changeset
414 if (type >= 0 and type < countof(names))
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
415 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
416 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
417 return "Unknown";
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
418 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
419
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
420
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
421 QString PrimitiveModel::makeFileName() const
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
422 {
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
423 int numerator = segments;
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
424 int denominator = divisions;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
425
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
426 // 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
427 simplify(numerator, denominator);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
428
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
429 if (denominator < 4)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
430 {
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
431 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
432 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
433 denominator *= factor;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
434 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
435
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
436 // Compose some general information: prefix, fraction, root, ring number
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
437 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
438 QString frac = format ("%1-%2", numerator, denominator);
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
439 static const char* roots[] = {"edge", "cyli", "disc", "ndis", "ring", "con"};
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
440 QString root = roots[type];
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
441 QString numberString = (type == Ring or type == Cone) ? format ("%1", ringNumber) : "";
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
442
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
443 // Truncate the root if necessary (7-16rin4.dat for instance).
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
444 // However, always keep the root at least 2 characters.
1065
c8ecddbd99e9 Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents: 1064
diff changeset
445 int extra = (countof(frac) + countof(numberString) + countof(root)) - 8;
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
446 root.chop(qBound(0, extra, 2));
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
447
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
448 // Stick them all together and return the result.
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
449 return prefix + frac + root + numberString + ".dat";
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
450 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
451
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
452
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
453 LDDocument* PrimitiveManager::generatePrimitive(const PrimitiveModel& spec)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
454 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
455 // 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
456 QString fraction = QString::number ((float) spec.segments / spec.divisions);
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
457 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
458 QString description;
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 // Ensure that there's decimals, even if they're 0.
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
461 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
462 fraction += ".0";
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
463
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
464 if (spec.type == PrimitiveModel::Ring or spec.type == PrimitiveModel::Cone)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
465 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
466 QString spacing =
1019
a2e4286adb0d Split PrimitivePrompt into its new source files, renamed it to GeneratePrimitivePrompt
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1018
diff changeset
467 (spec.ringNumber < 10) ? " " :
a2e4286adb0d Split PrimitivePrompt into its new source files, renamed it to GeneratePrimitivePrompt
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1018
diff changeset
468 (spec.ringNumber < 100) ? " " : "";
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
469 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
470 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
471 else
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
472 description = format("%1 %2", PrimitiveModel::typeName(spec.type), fraction);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
473
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
474 // 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
475 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
476 description.insert (0, "Hi-Res ");
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
477
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
478 LDDocument* document = m_window->newDocument();
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
479 document->setDefaultName(fileName);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
480
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
481 QString author = APPNAME;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
482 QString license = "";
1019
a2e4286adb0d Split PrimitivePrompt into its new source files, renamed it to GeneratePrimitivePrompt
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1018
diff changeset
483 bool hires = (spec.divisions == HighResolution);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
484
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
485 if (not m_config->defaultName().isEmpty())
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
486 {
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
487 license = preferredLicenseText();
1054
1f4c8a369e7c Further cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
488 author = format("%1 [%2]", m_config->defaultName(), m_config->defaultUser());
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
489 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
490
1090
ed73c4f48ca4 The current document pointer may no longer be null.
Teemu Piippo <teemu@hecknology.net>
parents: 1086
diff changeset
491 document->setFrozen(false);
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
492 document->history()->setIgnoring(false);
1312
f2974f3ac1ab remove LDBfc
Teemu Piippo <teemu@hecknology.net>
parents: 1310
diff changeset
493 document->header.name = fileName;
f2974f3ac1ab remove LDBfc
Teemu Piippo <teemu@hecknology.net>
parents: 1310
diff changeset
494 document->header.description = description;
f2974f3ac1ab remove LDBfc
Teemu Piippo <teemu@hecknology.net>
parents: 1310
diff changeset
495 document->header.author = author;
f2974f3ac1ab remove LDBfc
Teemu Piippo <teemu@hecknology.net>
parents: 1310
diff changeset
496
f2974f3ac1ab remove LDBfc
Teemu Piippo <teemu@hecknology.net>
parents: 1310
diff changeset
497 if (hires)
f2974f3ac1ab remove LDBfc
Teemu Piippo <teemu@hecknology.net>
parents: 1310
diff changeset
498 document->header.type = LDHeader::Primitive_48;
f2974f3ac1ab remove LDBfc
Teemu Piippo <teemu@hecknology.net>
parents: 1310
diff changeset
499 else
f2974f3ac1ab remove LDBfc
Teemu Piippo <teemu@hecknology.net>
parents: 1310
diff changeset
500 document->header.type = LDHeader::Primitive_8;
f2974f3ac1ab remove LDBfc
Teemu Piippo <teemu@hecknology.net>
parents: 1310
diff changeset
501
f2974f3ac1ab remove LDBfc
Teemu Piippo <teemu@hecknology.net>
parents: 1310
diff changeset
502 if (::config->useCaLicense())
f2974f3ac1ab remove LDBfc
Teemu Piippo <teemu@hecknology.net>
parents: 1310
diff changeset
503 document->header.license = LDHeader::CaLicense;
f2974f3ac1ab remove LDBfc
Teemu Piippo <teemu@hecknology.net>
parents: 1310
diff changeset
504 else
f2974f3ac1ab remove LDBfc
Teemu Piippo <teemu@hecknology.net>
parents: 1310
diff changeset
505 document->header.license =LDHeader::UnspecifiedLicense;
f2974f3ac1ab remove LDBfc
Teemu Piippo <teemu@hecknology.net>
parents: 1310
diff changeset
506
f2974f3ac1ab remove LDBfc
Teemu Piippo <teemu@hecknology.net>
parents: 1310
diff changeset
507 document->setWinding(CounterClockwise);
1080
6dac2d52bd9a Removed the LDSpawn function and replaced its last use cases.
Teemu Piippo <teemu@hecknology.net>
parents: 1072
diff changeset
508 spec.generateBody(*document);
978
4603d8fd063e Make documents members of the main window
Teemu Piippo <crimsondusk64@gmail.com>
parents: 971
diff changeset
509 document->addHistoryStep();
4603d8fd063e Make documents members of the main window
Teemu Piippo <crimsondusk64@gmail.com>
parents: 971
diff changeset
510 return document;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
511 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
512
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
513 /*
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
514 * PrimitiveManager :: getPrimitive
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
515 *
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
516 * 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
517 */
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
518 LDDocument* PrimitiveManager::getPrimitive(const PrimitiveModel& model)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
519 {
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
520 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
521 LDDocument* document = m_window->documents()->getDocumentByName (name);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
522
1090
ed73c4f48ca4 The current document pointer may no longer be null.
Teemu Piippo <teemu@hecknology.net>
parents: 1086
diff changeset
523 if (not document)
ed73c4f48ca4 The current document pointer may no longer be null.
Teemu Piippo <teemu@hecknology.net>
parents: 1086
diff changeset
524 {
ed73c4f48ca4 The current document pointer may no longer be null.
Teemu Piippo <teemu@hecknology.net>
parents: 1086
diff changeset
525 document = generatePrimitive(model);
ed73c4f48ca4 The current document pointer may no longer be null.
Teemu Piippo <teemu@hecknology.net>
parents: 1086
diff changeset
526 m_window->openDocumentForEditing(document);
ed73c4f48ca4 The current document pointer may no longer be null.
Teemu Piippo <teemu@hecknology.net>
parents: 1086
diff changeset
527 }
ed73c4f48ca4 The current document pointer may no longer be null.
Teemu Piippo <teemu@hecknology.net>
parents: 1086
diff changeset
528
ed73c4f48ca4 The current document pointer may no longer be null.
Teemu Piippo <teemu@hecknology.net>
parents: 1086
diff changeset
529 return document;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
530 }
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
531
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
532 /*
1283
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
533 * Returns the amount of columns in the primitives tree (1)
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1039
diff changeset
534 */
1283
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
535 int PrimitiveManager::columnCount(const QModelIndex&) const
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
536 {
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
537 return 1;
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
538 }
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
539
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
540 /*
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
541 * For an index that points to a primitive, returns the category that contains it
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
542 */
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
543 static PrimitiveCategory* categoryForPrimitiveIndex(const QModelIndex& primitiveIndex)
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 {
1283
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
545 return static_cast<PrimitiveCategory*>(primitiveIndex.internalPointer());
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
546 }
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
1283
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
548 /*
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
549 * Returns data from the tree model.
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
550 */
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
551 QVariant PrimitiveManager::data(const QModelIndex& index, int role) const
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
552 {
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
553 if (index.isValid())
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
554 {
1283
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
555 if (categoryForPrimitiveIndex(index) != nullptr)
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
556 {
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
557 // Index points to a primitive, return primitive information.
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
558 Primitive& primitive = categoryForPrimitiveIndex(index)->primitives[index.row()];
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
559
1283
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
560 switch(role)
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
561 {
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
562 case Qt::DisplayRole:
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
563 return format("%1 - %2", primitive.name, primitive.title);
1297
389516787a4c added subfile reference editor
Teemu Piippo <teemu@hecknology.net>
parents: 1285
diff changeset
564
1283
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
565 case Qt::DecorationRole:
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
566 return MainWindow::getIcon("subfilereference");
1297
389516787a4c added subfile reference editor
Teemu Piippo <teemu@hecknology.net>
parents: 1285
diff changeset
567
389516787a4c added subfile reference editor
Teemu Piippo <teemu@hecknology.net>
parents: 1285
diff changeset
568 case PrimitiveNameRole:
389516787a4c added subfile reference editor
Teemu Piippo <teemu@hecknology.net>
parents: 1285
diff changeset
569 return primitive.name;
389516787a4c added subfile reference editor
Teemu Piippo <teemu@hecknology.net>
parents: 1285
diff changeset
570
389516787a4c added subfile reference editor
Teemu Piippo <teemu@hecknology.net>
parents: 1285
diff changeset
571 case PrimitiveDescriptionRole:
389516787a4c added subfile reference editor
Teemu Piippo <teemu@hecknology.net>
parents: 1285
diff changeset
572 return primitive.title;
389516787a4c added subfile reference editor
Teemu Piippo <teemu@hecknology.net>
parents: 1285
diff changeset
573
1283
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
574 default:
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
575 return {};
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
576 }
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
577 }
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
578 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
579 {
1283
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
580 // Index points to a category, return category information.
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
581 PrimitiveCategory* category = this->m_categories[index.row()];
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
582
1283
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
583 switch (role)
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
584 {
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
585 case Qt::DisplayRole:
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
586 return category->name();
1297
389516787a4c added subfile reference editor
Teemu Piippo <teemu@hecknology.net>
parents: 1285
diff changeset
587
1283
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
588 case Qt::DecorationRole:
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
589 return MainWindow::getIcon("folder");
1297
389516787a4c added subfile reference editor
Teemu Piippo <teemu@hecknology.net>
parents: 1285
diff changeset
590
1283
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
591 default:
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
592 return {};
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
593 }
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
594 }
1283
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
595 }
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
596 else
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
597 {
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
598 // Index is invalid.
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
599 return {};
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
600 }
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
601 }
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
602
1283
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
603 /*
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
604 * For a row and parent index, returns a child index.
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
605 */
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
606 QModelIndex PrimitiveManager::index(int row, int, const QModelIndex& parent) const
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
607 {
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
608 if (parent.isValid())
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
609 {
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
610 if (categoryForPrimitiveIndex(parent))
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
611 {
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
612 // Parent is a primitive index. Primitives cannot have children so return an
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
613 // invalid index.
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
614 return {};
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
615 }
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
616 else
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
617 {
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
618 // Parent is a category, return an index to a primitive
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
619 PrimitiveCategory* category = m_categories[parent.row()];
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
620
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
621 // Create an index inside the category
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
622 if (row >= 0 and row < category->primitives.size())
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
623 return this->createIndex(row, 0, category);
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
624 else
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
625 return {};
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
626 }
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
627 }
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
628 else
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
629 {
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
630 // Create a top-level index pointing to a category
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
631 if (row >= 0 and row < this->m_categories.size())
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
632 return this->createIndex(row, 0, nullptr);
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
633 else
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
634 return {};
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
635 }
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
636 }
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
637
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
638 /*
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
639 * For a primitive index, find the category index that contains it.
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
640 */
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
641 QModelIndex PrimitiveManager::parent(const QModelIndex &index) const
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
642 {
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
643 int row = this->m_categories.indexOf(categoryForPrimitiveIndex(index));
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
644
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
645 if (row != -1)
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
646 return this->createIndex(row, 0, nullptr);
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
647 else
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
648 return {};
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
649 }
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
650
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
651 /*
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
652 * Returns the amount of rows contained inside the given index.
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
653 */
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
654 int PrimitiveManager::rowCount(const QModelIndex& parent) const
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
655 {
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
656 if (parent.isValid())
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
657 {
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
658 if (categoryForPrimitiveIndex(parent))
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
659 {
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
660 // Primitives don't have child nodes, so return 0.
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
661 return 0;
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
662 }
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
663 else
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
664 {
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
665 // For categories, return the amount of primitives contained.
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
666 return this->m_categories[parent.row()]->primitives.size();
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
667 }
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
668 }
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
669 else
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
670 {
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
671 // For top-level, return the amount of categories.
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
672 return this->m_categories.size();
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
673 }
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
674 }
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
675
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
676 /*
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
677 * Returns a static "Primitives" text for the header.
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
678 */
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
679 QVariant PrimitiveManager::headerData(int section, Qt::Orientation, int role) const
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
680 {
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
681 if (section == 0 and role == Qt::DisplayRole)
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
682 return tr("Primitives");
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
683 else
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
684 return {};
3c3a5eb965f7 added an MVC interface to the primitives tree
Teemu Piippo <teemu@hecknology.net>
parents: 1261
diff changeset
685 }
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
686
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
687 //
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
688 // ---------------------------------------------------------------------------------------------------------------------
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
689 //
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
690
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
691
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
692 PrimitiveCategory::PrimitiveCategory (QString name, QObject* parent) :
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
693 QObject (parent),
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
694 m_name (name) {}
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
695
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
696
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
697 bool PrimitiveCategory::isValidToInclude()
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
698 {
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
699 return not patterns.isEmpty();
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
700 }
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
701
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
702
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
703 QString PrimitiveCategory::name() const
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
704 {
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
705 return m_name;
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
706 }
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
707
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
708
1055
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
709 /*
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
710 * PrimitiveScanner :: PrimitiveScanner
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
711 *
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
712 * Constructs a primitive scanner.
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
713 */
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
714 PrimitiveScanner::PrimitiveScanner(PrimitiveManager* parent) :
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
715 QObject(parent),
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
716 HierarchyElement(parent),
1309
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
717 m_manager(parent)
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
718 {
1309
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
719 for (const Library& library : ::config->libraries())
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
720 {
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
721 QDir dir {library.path};
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
722 if (dir.exists("p") and QFileInfo {dir.filePath("p")}.isDir())
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
723 {
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
724 directories.push(dir.filePath("p"));
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
725
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
726 if (dir.exists("p/48") and QFileInfo {dir.filePath("p/48")}.isDir())
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
727 directories.push(dir.filePath("p/48"));
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
728 }
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
729 }
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
730
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
731 print("Scanning primitives...");
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
732 }
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
733
1309
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
734 PrimitiveScanner::~PrimitiveScanner()
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
735 {
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
736 delete this->currentIterator;
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
737 }
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
738
1055
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
739 /*
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
740 * PrimitiveScanner :: scannedPrimitives
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
741 *
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
742 * Returns a vector containing all the primitives found.
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
743 */
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
744 const QVector<Primitive> &PrimitiveScanner::scannedPrimitives() const
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
745 {
1055
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
746 return m_scannedPrimitives;
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
747 }
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
748
1055
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
749 /*
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
750 * PrimitiveScanner :: work
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
751 *
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
752 * Does one step of work, processes up to 100 primitives.
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
753 * If the scanner does not finish work by this function call, it will ask the event loop to call this method again.
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
754 */
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
755 void PrimitiveScanner::work()
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
756 {
1309
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
757 while (this->currentIterator == nullptr or not this->currentIterator->hasNext())
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
758 {
1309
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
759 delete this->currentIterator;
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
760 this->currentIterator = nullptr;
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
761
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
762 if (this->directories.isEmpty())
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
763 {
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
764 // If there are no more primitives to iterate, we're done. Now save this information into a cache file.
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
765 std::sort(
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
766 m_scannedPrimitives.begin(),
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
767 m_scannedPrimitives.end(),
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
768 [](const Primitive& one, const Primitive& other) -> bool
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
769 {
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
770 return one.title < other.title;
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
771 }
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
772 );
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
773 QString path = m_manager->getPrimitivesCfgPath();
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
774 QFile configFile = {path};
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
775
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
776 if (configFile.open(QIODevice::WriteOnly | QIODevice::Text))
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
777 {
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
778 for (Primitive& primitive : m_scannedPrimitives)
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
779 fprint(configFile, "%1 %2\r\n", primitive.name, primitive.title);
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
780
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
781 configFile.close();
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
782 }
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
783 else
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
784 {
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
785 QMessageBox::critical(
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
786 m_window,
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
787 tr("Error"),
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
788 format(
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
789 tr("Couldn't write primitive list %1: %2"),
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
790 path,
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
791 configFile.errorString()
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
792 )
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
793 );
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
794 }
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
795
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
796 emit workDone();
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
797 return;
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
798 }
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
799 else
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
800 {
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
801 this->currentIterator = new QDirIterator {this->directories.pop()};
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
802 this->m_basePathLength = this->currentIterator->path().length();
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
803 }
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
804 }
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
805
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
806 for (int i = 0; this->currentIterator->hasNext() and i < 100; ++i)
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
807 {
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
808 QString filename = this->currentIterator->next();
1055
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
809 QFile file = {filename};
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
810
1055
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
811 if (file.open (QIODevice::ReadOnly))
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
812 {
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
813 Primitive primitive;
1309
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
814 primitive.name = LDDocument::shortenName(filename);
1055
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
815 primitive.category = nullptr;
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
816 primitive.title = QString::fromUtf8(file.readLine().simplified());
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
817
1055
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
818 if (primitive.title[0] == '0')
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
819 {
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
820 primitive.title.remove(0, 1); // remove 0
1309
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
821 primitive.title = primitive.title.trimmed();
1055
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
822 }
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
823
c28d07a583ac Cleaned up PrimitiveScanner
Teemu Piippo <teemu@hecknology.net>
parents: 1054
diff changeset
824 m_scannedPrimitives << primitive;
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
825 }
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
826 }
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
827
1309
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
828 // Defer to event loop, pick up the work later.
c72e7e09bda8 reworked primitive scanning
Teemu Piippo <teemu@hecknology.net>
parents: 1297
diff changeset
829 QMetaObject::invokeMethod (this, "work", Qt::QueuedConnection);
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
830 }
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
831
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
832
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
833 //
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
834 // ---------------------------------------------------------------------------------------------------------------------
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
835 //
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
836
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
837
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
838 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
839 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
840 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
841
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
842
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
843 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
844 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
845 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
846
1020
815a040d4039 Reformatted primitives.cpp
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1019
diff changeset
847
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
848 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
849 {
49358df9495b Transformed primitive management into a new class PrimitiveManager that is a member of MainWindow
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
850 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
851 }

mercurial