src/configuration.cc

changeset 845
fec7023e91a7
parent 844
11587d419d2f
child 848
68410477c8bb
equal deleted inserted replaced
844:11587d419d2f 845:fec7023e91a7
29 #include "main.h" 29 #include "main.h"
30 #include "configuration.h" 30 #include "configuration.h"
31 #include "miscallenous.h" 31 #include "miscallenous.h"
32 #include "mainWindow.h" 32 #include "mainWindow.h"
33 #include "ldDocument.h" 33 #include "ldDocument.h"
34 #include "glRenderer.h"
35 #include "configuration.inc"
34 36
35 #ifdef _WIN32 37 #ifdef _WIN32
36 # define EXTENSION ".ini" 38 # define EXTENSION ".ini"
37 #else 39 #else
38 # define EXTENSION ".cfg" 40 # define EXTENSION ".cfg"
39 #endif // _WIN32 41 #endif // _WIN32
40 42
41 #define MAX_CONFIG 512 43 #define MAX_CONFIG 512
42 44
43 ConfigEntry* g_configPointers[MAX_CONFIG];
44 static int g_cfgPointerCursor = 0;
45 static QMap<QString, ConfigEntry*> g_configsByName; 45 static QMap<QString, ConfigEntry*> g_configsByName;
46 static QList<ConfigEntry*> g_configs; 46 static QList<ConfigEntry*> g_configs;
47 47
48 ConfigEntry::ConfigEntry (QString name) : 48 ConfigEntry::ConfigEntry (QString name) :
49 m_name (name) {} 49 m_name (name) {}
50
51 void Config::init()
52 {
53 setupConfigurationLists();
54 print ("Configuration initialized with %1 entries\n", g_configs.size());
55 }
56
57 static void initConfigurationEntry (ConfigEntry* entry)
58 {
59 g_configs << entry;
60 g_configsByName[entry->name()] = entry;
61 }
50 62
51 // 63 //
52 // Load the configuration from file 64 // Load the configuration from file
53 // 65 //
54 bool Config::load() 66 bool Config::load()
55 { 67 {
56 QSettings* settings = settingsObject(); 68 QSettings* settings = settingsObject();
57 print ("config::load: Loading configuration file from %1\n", settings->fileName()); 69 print ("Loading configuration file from %1\n", settings->fileName());
58 70
59 for (ConfigEntry* cfg : g_configPointers) 71 for (ConfigEntry* cfg : g_configs)
60 { 72 {
61 if (cfg == null) 73 if (cfg == null)
62 break; 74 break;
63 75
64 QVariant val = settings->value (cfg->name(), cfg->getDefaultAsVariant()); 76 QVariant val = settings->value (cfg->name(), cfg->getDefaultAsVariant());
65 cfg->loadFromVariant (val); 77 cfg->loadFromVariant (val);
66 g_configsByName[cfg->name()] = cfg;
67 g_configs << cfg;
68 } 78 }
69 79
70 if (g_win != null) 80 if (g_win != null)
71 g_win->loadShortcuts (settings); 81 g_win->loadShortcuts (settings);
72 82
133 { 143 {
134 QString path = qApp->applicationDirPath() + "/" UNIXNAME EXTENSION; 144 QString path = qApp->applicationDirPath() + "/" UNIXNAME EXTENSION;
135 return new QSettings (path, QSettings::IniFormat); 145 return new QSettings (path, QSettings::IniFormat);
136 } 146 }
137 147
138 //
139 // We cannot just add config objects to a list or vector because that would rely
140 // on the vector's c-tor being called before the configs' c-tors. With global
141 // variables we cannot assume that, therefore we need to use a C-style array here.
142 //
143 void ConfigEntry::addToArray (ConfigEntry* ptr)
144 {
145 if (g_cfgPointerCursor == 0)
146 memset (g_configPointers, 0, sizeof g_configPointers);
147
148 assert (g_cfgPointerCursor < MAX_CONFIG);
149 g_configPointers[g_cfgPointerCursor++] = ptr;
150 }
151
152 template<typename T> 148 template<typename T>
153 T* getConfigByName (QString name, ConfigEntry::Type type) 149 T* getConfigByName (QString name, ConfigEntry::Type type)
154 { 150 {
155 auto it = g_configsByName.find (name); 151 auto it = g_configsByName.find (name);
156 152

mercurial