src/ldpaths.cpp

Thu, 09 Mar 2017 00:54:45 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 09 Mar 2017 00:54:45 +0200
changeset 1208
8d4b8a9df724
parent 1125
d8f94e56d42e
permissions
-rw-r--r--

Some cleanup in LDPaths

1006
a6b462051ae0 Copyright header fix, some minor stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1000
diff changeset
1 /*
a6b462051ae0 Copyright header fix, some minor stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1000
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
1006
a6b462051ae0 Copyright header fix, some minor stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1000
diff changeset
4 *
a6b462051ae0 Copyright header fix, some minor stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1000
diff changeset
5 * This program is free software: you can redistribute it and/or modify
a6b462051ae0 Copyright header fix, some minor stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1000
diff changeset
6 * it under the terms of the GNU General Public License as published by
a6b462051ae0 Copyright header fix, some minor stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1000
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
a6b462051ae0 Copyright header fix, some minor stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1000
diff changeset
8 * (at your option) any later version.
a6b462051ae0 Copyright header fix, some minor stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1000
diff changeset
9 *
a6b462051ae0 Copyright header fix, some minor stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1000
diff changeset
10 * This program is distributed in the hope that it will be useful,
a6b462051ae0 Copyright header fix, some minor stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1000
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
a6b462051ae0 Copyright header fix, some minor stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1000
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a6b462051ae0 Copyright header fix, some minor stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1000
diff changeset
13 * GNU General Public License for more details.
a6b462051ae0 Copyright header fix, some minor stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1000
diff changeset
14 *
a6b462051ae0 Copyright header fix, some minor stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1000
diff changeset
15 * You should have received a copy of the GNU General Public License
a6b462051ae0 Copyright header fix, some minor stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1000
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
a6b462051ae0 Copyright header fix, some minor stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1000
diff changeset
17 */
a6b462051ae0 Copyright header fix, some minor stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1000
diff changeset
18
953
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 #include <QDir>
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 #include "ldpaths.h"
970
c8aae45afd85 Commit configuration rework (doesn't work yet, more than most probably doesn't compile either)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 953
diff changeset
21 #include "mainwindow.h"
953
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 #include "dialogs/ldrawpathdialog.h"
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23
1208
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
24 LDPaths::LDPaths(QObject* parent) :
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
25 QObject {parent},
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
26 m_dialog {nullptr} {}
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: 1014
diff changeset
27
1208
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
28 LDPaths::~LDPaths()
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
29 {
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
30 delete m_dialog;
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
31 }
953
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32
1208
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
33 bool LDPaths::checkPaths()
953
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 {
1208
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
35 QString pathconfig = configuration().lDrawPath();
970
c8aae45afd85 Commit configuration rework (doesn't work yet, more than most probably doesn't compile either)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 953
diff changeset
36
1208
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
37 if (configurePaths (pathconfig))
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
38 {
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
39 return true;
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
40 }
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
41 else
953
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 {
970
c8aae45afd85 Commit configuration rework (doesn't work yet, more than most probably doesn't compile either)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 953
diff changeset
43 m_dialog = new LDrawPathDialog (pathconfig, false);
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: 1014
diff changeset
44 connect(m_dialog, &LDrawPathDialog::pathChanged, this, &LDPaths::configurePaths);
953
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45
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: 1014
diff changeset
46 if (m_dialog->exec() != QDialog::Accepted)
1208
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
47 return false;
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
48
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
49 configuration().setLDrawPath(m_dialog->path());
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
50 return true;
953
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 }
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 }
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53
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: 1014
diff changeset
54
953
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 bool LDPaths::isValid (const QDir& dir) const
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 {
999
213a7c7a3ce4 And now it should work again too
Teemu Piippo <crimsondusk64@gmail.com>
parents: 987
diff changeset
57 if (dir.exists())
953
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 {
999
213a7c7a3ce4 And now it should work again too
Teemu Piippo <crimsondusk64@gmail.com>
parents: 987
diff changeset
59 if (dir.isReadable())
213a7c7a3ce4 And now it should work again too
Teemu Piippo <crimsondusk64@gmail.com>
parents: 987
diff changeset
60 {
213a7c7a3ce4 And now it should work again too
Teemu Piippo <crimsondusk64@gmail.com>
parents: 987
diff changeset
61 QStringList mustHave = { "LDConfig.ldr", "parts", "p" };
213a7c7a3ce4 And now it should work again too
Teemu Piippo <crimsondusk64@gmail.com>
parents: 987
diff changeset
62 QStringList contents = dir.entryList (mustHave);
213a7c7a3ce4 And now it should work again too
Teemu Piippo <crimsondusk64@gmail.com>
parents: 987
diff changeset
63
1065
c8ecddbd99e9 Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents: 1063
diff changeset
64 if (countof(contents) == countof(mustHave))
999
213a7c7a3ce4 And now it should work again too
Teemu Piippo <crimsondusk64@gmail.com>
parents: 987
diff changeset
65 m_error = "";
213a7c7a3ce4 And now it should work again too
Teemu Piippo <crimsondusk64@gmail.com>
parents: 987
diff changeset
66 else
213a7c7a3ce4 And now it should work again too
Teemu Piippo <crimsondusk64@gmail.com>
parents: 987
diff changeset
67 m_error = "That is not an LDraw directory! It must<br />have LDConfig.ldr, parts/ and p/.";
213a7c7a3ce4 And now it should work again too
Teemu Piippo <crimsondusk64@gmail.com>
parents: 987
diff changeset
68 }
953
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 else
999
213a7c7a3ce4 And now it should work again too
Teemu Piippo <crimsondusk64@gmail.com>
parents: 987
diff changeset
70 m_error = "That directory cannot be read.";
953
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 }
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 else
999
213a7c7a3ce4 And now it should work again too
Teemu Piippo <crimsondusk64@gmail.com>
parents: 987
diff changeset
73 m_error = "That directory does not exist.";
953
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 return m_error.isEmpty();
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 }
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77
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: 1014
diff changeset
78
953
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 bool LDPaths::configurePaths (QString path)
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 {
1208
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
81 QDir dir {path};
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
82 bool ok = isValid(dir);
953
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 if (ok)
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 {
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 baseDir() = dir;
1208
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
87 ldConfigPath() = format("%1/LDConfig.ldr", path);
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
88 partsDir() = path + "/parts";
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
89 primitivesDir() = path + "/p";
953
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 }
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 if (m_dialog)
1208
8d4b8a9df724 Some cleanup in LDPaths
Teemu Piippo <teemu@hecknology.net>
parents: 1125
diff changeset
93 m_dialog->setStatusText(m_error.isEmpty() ? "OK" : m_error, ok);
953
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 return ok;
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 }
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97
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: 1014
diff changeset
98
953
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 QString& LDPaths::ldConfigPath()
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 {
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 static QString value;
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 return value;
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 }
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104
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: 1014
diff changeset
105
953
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 QDir& LDPaths::primitivesDir()
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 {
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 static QDir value;
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 return value;
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110 }
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111
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: 1014
diff changeset
112
953
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 QDir& LDPaths::partsDir()
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114 {
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 static QDir value;
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116 return value;
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 }
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118
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: 1014
diff changeset
119
953
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 QDir& LDPaths::baseDir()
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 {
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122 static QDir value;
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123 return value;
8349552ee5e9 Refactor LDrawPathDialog and LDPaths
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124 }

mercurial