Mon, 27 Mar 2017 14:56:05 +0300
LibraryCollection now derives from QObject and QVector<Library>
1204
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
1 | /* |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
2 | * LDForge: LDraw parts authoring CAD |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
3 | * Copyright (C) 2013 - 2017 Teemu Piippo |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
4 | * |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
5 | * This program is free software: you can redistribute it and/or modify |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
6 | * it under the terms of the GNU General Public License as published by |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
7 | * the Free Software Foundation, either version 3 of the License, or |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
8 | * (at your option) any later version. |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
9 | * |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
10 | * This program is distributed in the hope that it will be useful, |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
13 | * GNU General Public License for more details. |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
14 | * |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
15 | * You should have received a copy of the GNU General Public License |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
17 | */ |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
18 | |
1206
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
19 | #include <QApplication> |
1204
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
20 | #include <QSettings> |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
21 | #include <QVariant> |
1205 | 22 | #include "baseconfiguration.h" |
1204
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
23 | #include "mainwindow.h" |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
24 | |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
25 | BaseConfiguration::BaseConfiguration() : |
1206
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
26 | m_settings {qApp->applicationDirPath() + "/" UNIXNAME ".ini", QSettings::IniFormat, this} {} |
1204
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
27 | |
1206
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
28 | /* |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
29 | * Returns the default value for the provided configuration name. |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
30 | */ |
1204
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
31 | QVariant BaseConfiguration::defaultValueByName(const QString& name) |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
32 | { |
1206
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
33 | if (not m_isInitialized) |
1204
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
34 | initDefaults(); |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
35 | |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
36 | return m_defaults.value(name); |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
37 | } |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
38 | |
1206
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
39 | /* |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
40 | * Returns whether or not a configuration entry exists for the provided value. |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
41 | */ |
1204
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
42 | bool BaseConfiguration::existsEntry(const QString& name) |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
43 | { |
1206
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
44 | if (not m_isInitialized) |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
45 | initDefaults(); |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
46 | |
1204
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
47 | return m_defaults.find(name) != m_defaults.end(); |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
48 | } |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
49 | |
1206
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
50 | /* |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
51 | * Initializes the default value map in the configuration. |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
52 | */ |
1204
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
53 | void BaseConfiguration::initDefaults() |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
54 | { |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
55 | m_isInitialized = true; |
1d25231dd7c9
Split the configuration main interface to a new class, BaseInterface, so that moc can properly deal with it.
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
56 | } |
1206
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
57 | |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
58 | /* |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
59 | * Returns a configuration value by name. |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
60 | */ |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
61 | QVariant BaseConfiguration::value(const QString& name) |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
62 | { |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
63 | return m_settings.value(name, defaultValueByName(name)); |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
64 | } |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
65 | |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
66 | /* |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
67 | * Returns a configuration value by name. This particular overload allows overriding the default value. |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
68 | */ |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
69 | QVariant BaseConfiguration::value(const QString& name, const QVariant& defaultValue) |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
70 | { |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
71 | return m_settings.value(name, defaultValue); |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
72 | } |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
73 | |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
74 | /* |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
75 | * Sets a configuration value by name. |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
76 | */ |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
77 | void BaseConfiguration::setValue(const QString& name, const QVariant& value) |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
78 | { |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
79 | if (value != defaultValueByName(name)) |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
80 | m_settings.setValue(name, value); |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
81 | else |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
82 | m_settings.remove(name); |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
83 | } |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
84 | |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
85 | |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
86 | /* |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
87 | * Sets a configuration value by name and overridden default value. Please only use this if you indeed are in charge of the default value. |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
88 | */ |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
89 | void BaseConfiguration::setValue(const QString& name, const QVariant& value, const QVariant& defaultValue) |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
90 | { |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
91 | if (value != defaultValue) |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
92 | m_settings.setValue(name, value); |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
93 | else |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
94 | m_settings.remove(name); |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
95 | } |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
96 | |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
97 | /* |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
98 | * Syncs settings now. |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
99 | */ |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
100 | void BaseConfiguration::sync() |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
101 | { |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
102 | m_settings.sync(); |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
103 | } |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
104 | |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
105 | /* |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
106 | * Called by subclasses to register configuration entries. |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
107 | */ |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
108 | void BaseConfiguration::registerConfigurationEntry(const QString& name, const QVariant& value) |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
109 | { |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
110 | m_defaults[name] = value; |
743dc95e0be6
Better encapsulated the BaseConfiguration class.
Teemu Piippo <teemu@hecknology.net>
parents:
1205
diff
changeset
|
111 | } |