src/config.cc

Fri, 17 Jan 2014 16:43:29 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Fri, 17 Jan 2014 16:43:29 +0200
changeset 625
3afe6a110c61
parent 622
622c49e60348
permissions
-rw-r--r--

- removed some pointless typedefs

557
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
600
209e3f1f7b2c - updated copyright year. Best wishes for 2014!
Santeri Piippo <crimsondusk64@gmail.com>
parents: 599
diff changeset
3 * Copyright (C) 2013, 2014 Santeri Piippo
557
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 *
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 * (at your option) any later version.
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 *
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * GNU General Public License for more details.
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 *
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 * =====================================================================
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 *
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 * config.cpp: Configuration management. I don't like how unsafe QSettings
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 * is so this implements a type-safer and identifer-safer wrapping system of
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 * configuration variables. QSettings is used underlyingly, this is a matter
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 * of interface.
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 */
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 #include <errno.h>
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 #include <QDir>
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 #include <QTextStream>
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 #include <QSettings>
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 #include "main.h"
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 #include "config.h"
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 #include "misc.h"
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 #include "gui.h"
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 #include "document.h"
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 #ifdef _WIN32
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 # define EXTENSION ".ini"
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 #else
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 # define EXTENSION ".cfg"
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 #endif // _WIN32
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40
590
7aec744ce97b - refactored config code
Santeri Piippo <crimsondusk64@gmail.com>
parents: 589
diff changeset
41 Config* g_configPointers[MAX_CONFIG];
7aec744ce97b - refactored config code
Santeri Piippo <crimsondusk64@gmail.com>
parents: 589
diff changeset
42 static int g_cfgPointerCursor = 0;
606
3dd6f343ec06 - removed the 'str' typedef, use QString directly
Santeri Piippo <crimsondusk64@gmail.com>
parents: 603
diff changeset
43 static QMap<QString, Config*> g_configsByName;
622
622c49e60348 - refactoring
Santeri Piippo <crimsondusk64@gmail.com>
parents: 613
diff changeset
44 static QList<Config*> g_configs;
557
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 // =============================================================================
589
1417828920e6 - use a local file with QSettings at all times. Registry is such a pain..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 587
diff changeset
47 // Get the QSettings object.
557
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 // -----------------------------------------------------------------------------
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 static QSettings* getSettingsObject()
603
47e7773c7841 - reformatting
Santeri Piippo <crimsondusk64@gmail.com>
parents: 600
diff changeset
50 {
613
2d01590da286 - use QCoreApplication::applicationDirPath instead of a custom hack to get the application path
Santeri Piippo <crimsondusk64@gmail.com>
parents: 611
diff changeset
51 QString path = qApp->applicationDirPath() + "/" UNIXNAME EXTENSION;
611
6679e47b019f - configuration is now always situated and read from the application working directory
Santeri Piippo <crimsondusk64@gmail.com>
parents: 606
diff changeset
52 return new QSettings (path, QSettings::IniFormat);
557
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 }
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54
606
3dd6f343ec06 - removed the 'str' typedef, use QString directly
Santeri Piippo <crimsondusk64@gmail.com>
parents: 603
diff changeset
55 Config::Config (QString name) :
590
7aec744ce97b - refactored config code
Santeri Piippo <crimsondusk64@gmail.com>
parents: 589
diff changeset
56 m_Name (name) {}
557
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 // =============================================================================
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 // Load the configuration from file
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 // -----------------------------------------------------------------------------
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 bool Config::load()
603
47e7773c7841 - reformatting
Santeri Piippo <crimsondusk64@gmail.com>
parents: 600
diff changeset
62 {
47e7773c7841 - reformatting
Santeri Piippo <crimsondusk64@gmail.com>
parents: 600
diff changeset
63 QSettings* settings = getSettingsObject();
557
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 log ("config::load: Loading configuration file from %1\n", settings->fileName());
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 for (Config* cfg : g_configPointers)
603
47e7773c7841 - reformatting
Santeri Piippo <crimsondusk64@gmail.com>
parents: 600
diff changeset
67 {
47e7773c7841 - reformatting
Santeri Piippo <crimsondusk64@gmail.com>
parents: 600
diff changeset
68 if (!cfg)
557
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 break;
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70
590
7aec744ce97b - refactored config code
Santeri Piippo <crimsondusk64@gmail.com>
parents: 589
diff changeset
71 QVariant val = settings->value (cfg->getName(), cfg->getDefaultAsVariant());
557
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 cfg->loadFromVariant (val);
590
7aec744ce97b - refactored config code
Santeri Piippo <crimsondusk64@gmail.com>
parents: 589
diff changeset
73 g_configsByName[cfg->getName()] = cfg;
7aec744ce97b - refactored config code
Santeri Piippo <crimsondusk64@gmail.com>
parents: 589
diff changeset
74 g_configs << cfg;
557
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 }
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 settings->deleteLater();
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 return true;
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 }
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 // =============================================================================
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 // Save the configuration to disk
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 // -----------------------------------------------------------------------------
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 bool Config::save()
603
47e7773c7841 - reformatting
Santeri Piippo <crimsondusk64@gmail.com>
parents: 600
diff changeset
85 {
47e7773c7841 - reformatting
Santeri Piippo <crimsondusk64@gmail.com>
parents: 600
diff changeset
86 QSettings* settings = getSettingsObject();
557
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 log ("Saving configuration to %1...\n", settings->fileName());
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88
590
7aec744ce97b - refactored config code
Santeri Piippo <crimsondusk64@gmail.com>
parents: 589
diff changeset
89 for (Config* cfg : g_configs)
603
47e7773c7841 - reformatting
Santeri Piippo <crimsondusk64@gmail.com>
parents: 600
diff changeset
90 {
47e7773c7841 - reformatting
Santeri Piippo <crimsondusk64@gmail.com>
parents: 600
diff changeset
91 if (!cfg->isDefault())
590
7aec744ce97b - refactored config code
Santeri Piippo <crimsondusk64@gmail.com>
parents: 589
diff changeset
92 settings->setValue (cfg->getName(), cfg->toVariant());
599
b77aecd27fb0 - when configuration options are default, they need to be removed from the settings
Santeri Piippo <crimsondusk64@gmail.com>
parents: 590
diff changeset
93 else
b77aecd27fb0 - when configuration options are default, they need to be removed from the settings
Santeri Piippo <crimsondusk64@gmail.com>
parents: 590
diff changeset
94 settings->remove (cfg->getName());
b77aecd27fb0 - when configuration options are default, they need to be removed from the settings
Santeri Piippo <crimsondusk64@gmail.com>
parents: 590
diff changeset
95 }
557
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 settings->sync();
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 settings->deleteLater();
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 return true;
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 }
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 // =============================================================================
590
7aec744ce97b - refactored config code
Santeri Piippo <crimsondusk64@gmail.com>
parents: 589
diff changeset
103 // Reset configuration to defaults.
557
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 // -----------------------------------------------------------------------------
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 void Config::reset()
603
47e7773c7841 - reformatting
Santeri Piippo <crimsondusk64@gmail.com>
parents: 600
diff changeset
106 {
47e7773c7841 - reformatting
Santeri Piippo <crimsondusk64@gmail.com>
parents: 600
diff changeset
107 for (Config* cfg : g_configs)
557
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 cfg->resetValue();
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 }
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 // =============================================================================
589
1417828920e6 - use a local file with QSettings at all times. Registry is such a pain..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 587
diff changeset
112 // Where is the configuration file located at?
557
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 // -----------------------------------------------------------------------------
606
3dd6f343ec06 - removed the 'str' typedef, use QString directly
Santeri Piippo <crimsondusk64@gmail.com>
parents: 603
diff changeset
114 QString Config::filepath (QString file)
603
47e7773c7841 - reformatting
Santeri Piippo <crimsondusk64@gmail.com>
parents: 600
diff changeset
115 {
47e7773c7841 - reformatting
Santeri Piippo <crimsondusk64@gmail.com>
parents: 600
diff changeset
116 return Config::dirpath() + DIRSLASH + file;
557
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 }
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 // =============================================================================
589
1417828920e6 - use a local file with QSettings at all times. Registry is such a pain..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 587
diff changeset
120 // Directory of the configuration file.
557
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 // -----------------------------------------------------------------------------
606
3dd6f343ec06 - removed the 'str' typedef, use QString directly
Santeri Piippo <crimsondusk64@gmail.com>
parents: 603
diff changeset
122 QString Config::dirpath()
603
47e7773c7841 - reformatting
Santeri Piippo <crimsondusk64@gmail.com>
parents: 600
diff changeset
123 {
47e7773c7841 - reformatting
Santeri Piippo <crimsondusk64@gmail.com>
parents: 600
diff changeset
124 QSettings* cfg = getSettingsObject();
557
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125 return dirname (cfg->fileName());
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126 }
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128 // =============================================================================
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 // We cannot just add config objects to a list or vector because that would rely
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 // on the vector's c-tor being called before the configs' c-tors. With global
590
7aec744ce97b - refactored config code
Santeri Piippo <crimsondusk64@gmail.com>
parents: 589
diff changeset
131 // variables we cannot assume that, therefore we need to use a C-style array here.
557
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132 // -----------------------------------------------------------------------------
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 void Config::addToArray (Config* ptr)
603
47e7773c7841 - reformatting
Santeri Piippo <crimsondusk64@gmail.com>
parents: 600
diff changeset
134 {
47e7773c7841 - reformatting
Santeri Piippo <crimsondusk64@gmail.com>
parents: 600
diff changeset
135 if (g_cfgPointerCursor == 0)
557
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136 memset (g_configPointers, 0, sizeof g_configPointers);
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138 assert (g_cfgPointerCursor < MAX_CONFIG);
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139 g_configPointers[g_cfgPointerCursor++] = ptr;
04e140bdeb0b - changed source file extension from .cpp to .cc
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140 }
587
507e5d5c348c - removed the horrible actions.h hack in favor of usage of Qt's meta object system
Santeri Piippo <crimsondusk64@gmail.com>
parents: 557
diff changeset
141
507e5d5c348c - removed the horrible actions.h hack in favor of usage of Qt's meta object system
Santeri Piippo <crimsondusk64@gmail.com>
parents: 557
diff changeset
142 // =============================================================================
507e5d5c348c - removed the horrible actions.h hack in favor of usage of Qt's meta object system
Santeri Piippo <crimsondusk64@gmail.com>
parents: 557
diff changeset
143 // -----------------------------------------------------------------------------
606
3dd6f343ec06 - removed the 'str' typedef, use QString directly
Santeri Piippo <crimsondusk64@gmail.com>
parents: 603
diff changeset
144 template<class T> T* getConfigByName (QString name, Config::Type type)
603
47e7773c7841 - reformatting
Santeri Piippo <crimsondusk64@gmail.com>
parents: 600
diff changeset
145 {
47e7773c7841 - reformatting
Santeri Piippo <crimsondusk64@gmail.com>
parents: 600
diff changeset
146 auto it = g_configsByName.find (name);
590
7aec744ce97b - refactored config code
Santeri Piippo <crimsondusk64@gmail.com>
parents: 589
diff changeset
147
7aec744ce97b - refactored config code
Santeri Piippo <crimsondusk64@gmail.com>
parents: 589
diff changeset
148 if (it == g_configsByName.end())
7aec744ce97b - refactored config code
Santeri Piippo <crimsondusk64@gmail.com>
parents: 589
diff changeset
149 return null;
587
507e5d5c348c - removed the horrible actions.h hack in favor of usage of Qt's meta object system
Santeri Piippo <crimsondusk64@gmail.com>
parents: 557
diff changeset
150
590
7aec744ce97b - refactored config code
Santeri Piippo <crimsondusk64@gmail.com>
parents: 589
diff changeset
151 Config* cfg = it.value();
7aec744ce97b - refactored config code
Santeri Piippo <crimsondusk64@gmail.com>
parents: 589
diff changeset
152
7aec744ce97b - refactored config code
Santeri Piippo <crimsondusk64@gmail.com>
parents: 589
diff changeset
153 if (cfg->getType() != type)
603
47e7773c7841 - reformatting
Santeri Piippo <crimsondusk64@gmail.com>
parents: 600
diff changeset
154 {
47e7773c7841 - reformatting
Santeri Piippo <crimsondusk64@gmail.com>
parents: 600
diff changeset
155 fprint (stderr, "type of %1 is %2, not %3\n", name, cfg->getType(), type);
590
7aec744ce97b - refactored config code
Santeri Piippo <crimsondusk64@gmail.com>
parents: 589
diff changeset
156 abort();
587
507e5d5c348c - removed the horrible actions.h hack in favor of usage of Qt's meta object system
Santeri Piippo <crimsondusk64@gmail.com>
parents: 557
diff changeset
157 }
507e5d5c348c - removed the horrible actions.h hack in favor of usage of Qt's meta object system
Santeri Piippo <crimsondusk64@gmail.com>
parents: 557
diff changeset
158
590
7aec744ce97b - refactored config code
Santeri Piippo <crimsondusk64@gmail.com>
parents: 589
diff changeset
159 return reinterpret_cast<T*> (cfg);
587
507e5d5c348c - removed the horrible actions.h hack in favor of usage of Qt's meta object system
Santeri Piippo <crimsondusk64@gmail.com>
parents: 557
diff changeset
160 }
507e5d5c348c - removed the horrible actions.h hack in favor of usage of Qt's meta object system
Santeri Piippo <crimsondusk64@gmail.com>
parents: 557
diff changeset
161
590
7aec744ce97b - refactored config code
Santeri Piippo <crimsondusk64@gmail.com>
parents: 589
diff changeset
162 // =============================================================================
7aec744ce97b - refactored config code
Santeri Piippo <crimsondusk64@gmail.com>
parents: 589
diff changeset
163 // -----------------------------------------------------------------------------
587
507e5d5c348c - removed the horrible actions.h hack in favor of usage of Qt's meta object system
Santeri Piippo <crimsondusk64@gmail.com>
parents: 557
diff changeset
164 #undef IMPLEMENT_CONFIG
611
6679e47b019f - configuration is now always situated and read from the application working directory
Santeri Piippo <crimsondusk64@gmail.com>
parents: 606
diff changeset
165
622
622c49e60348 - refactoring
Santeri Piippo <crimsondusk64@gmail.com>
parents: 613
diff changeset
166 #define IMPLEMENT_CONFIG(NAME) \
622c49e60348 - refactoring
Santeri Piippo <crimsondusk64@gmail.com>
parents: 613
diff changeset
167 NAME##Config* NAME##Config::getByName (QString name) \
622c49e60348 - refactoring
Santeri Piippo <crimsondusk64@gmail.com>
parents: 613
diff changeset
168 { \
622c49e60348 - refactoring
Santeri Piippo <crimsondusk64@gmail.com>
parents: 613
diff changeset
169 return getConfigByName<NAME##Config> (name, E##NAME##Type); \
590
7aec744ce97b - refactored config code
Santeri Piippo <crimsondusk64@gmail.com>
parents: 589
diff changeset
170 }
587
507e5d5c348c - removed the horrible actions.h hack in favor of usage of Qt's meta object system
Santeri Piippo <crimsondusk64@gmail.com>
parents: 557
diff changeset
171
507e5d5c348c - removed the horrible actions.h hack in favor of usage of Qt's meta object system
Santeri Piippo <crimsondusk64@gmail.com>
parents: 557
diff changeset
172 IMPLEMENT_CONFIG (Int)
507e5d5c348c - removed the horrible actions.h hack in favor of usage of Qt's meta object system
Santeri Piippo <crimsondusk64@gmail.com>
parents: 557
diff changeset
173 IMPLEMENT_CONFIG (String)
507e5d5c348c - removed the horrible actions.h hack in favor of usage of Qt's meta object system
Santeri Piippo <crimsondusk64@gmail.com>
parents: 557
diff changeset
174 IMPLEMENT_CONFIG (Bool)
507e5d5c348c - removed the horrible actions.h hack in favor of usage of Qt's meta object system
Santeri Piippo <crimsondusk64@gmail.com>
parents: 557
diff changeset
175 IMPLEMENT_CONFIG (Float)
507e5d5c348c - removed the horrible actions.h hack in favor of usage of Qt's meta object system
Santeri Piippo <crimsondusk64@gmail.com>
parents: 557
diff changeset
176 IMPLEMENT_CONFIG (List)
590
7aec744ce97b - refactored config code
Santeri Piippo <crimsondusk64@gmail.com>
parents: 589
diff changeset
177 IMPLEMENT_CONFIG (KeySequence)
622
622c49e60348 - refactoring
Santeri Piippo <crimsondusk64@gmail.com>
parents: 613
diff changeset
178 IMPLEMENT_CONFIG (Vertex)

mercurial