126 } |
126 } |
127 # Keep a file of any Qt types, we'll need to #include them. |
127 # Keep a file of any Qt types, we'll need to #include them. |
128 self.qtTypes.update(findall(r'Q\w+', typename)) |
128 self.qtTypes.update(findall(r'Q\w+', typename)) |
129 |
129 |
130 def writeHeader(self, device): |
130 def writeHeader(self, device): |
131 device.write('#pragma once\n') |
131 write = lambda value: device.write(value + '\n') |
132 device.write('#include <QObject>\n') |
132 write('#pragma once') |
133 device.write('#include <QMap>\n') |
133 write('#include "{sourcedir}/baseconfiguration.h"'.format(sourcedir = self.args.sourcedir)) |
134 for include in sorted(self.includes): |
134 write('class Configuration : public BaseConfiguration') |
135 device.write('#include %s\n' % include) |
135 write('{') |
136 for qtType in sorted(self.qtTypes): |
136 write('public:') |
137 device.write('#include <%s>\n' % qtType) |
137 write('\tvoid initDefaults() override;') |
138 device.write('\n') |
138 for declaration in self.declarations.values(): |
139 formatargs = {} |
139 write('\t{type} {readgate}() const;'.format(**declaration)) |
140 write = lambda value: device.write(value) |
140 for declaration in self.declarations.values(): |
141 write('class Configuration : public QObject\n') |
141 write('\tvoid {writegate}({typereference} value);'.format(**declaration)) |
142 write('{\n') |
|
143 write('\tQ_OBJECT\n') |
|
144 write('\n') |
|
145 write('public:\n') |
|
146 write('\tConfiguration();\n') |
|
147 write('\t~Configuration();\n') |
|
148 write('\tbool existsEntry(const QString& name);\n') |
|
149 write('\tQVariant defaultValueByName(const QString& name);\n') |
|
150 for declaration in self.declarations.values(): |
|
151 write('\t{type} {readgate}() const;\n'.format(**declaration)) |
|
152 for declaration in self.declarations.values(): |
|
153 write('\tvoid {writegate}({typereference} value);\n'.format(**declaration)) |
|
154 |
|
155 for declaration in filter(lambda declaration: declaration['type'] == 'bool', self.declarations.values()): |
142 for declaration in filter(lambda declaration: declaration['type'] == 'bool', self.declarations.values()): |
156 write('\tvoid {togglefunction}();\n'.format(**declaration)) |
143 write('\tvoid {togglefunction}();'.format(**declaration)) |
157 write('\n') |
144 write('};') |
158 write('signals:\n') |
145 write('') |
159 write('\tvoid configurationChanged(QString, QVariant, QVariant);\n') |
|
160 write('\n') |
|
161 write('private:\n') |
|
162 write('\tQMap<QString, QVariant> m_defaults;\n') |
|
163 write('\tclass QSettings* m_settings;\n') |
|
164 write('};\n') |
|
165 |
146 |
166 def writeSource(self, device, headername): |
147 def writeSource(self, device, headername): |
167 device.write('#include <QSet>\n') |
148 for qttype in self.qtTypes: |
168 device.write('#include <QSettings>\n') |
149 device.write('#include <%s>\n' % qttype) |
169 device.write('#include <QVariant>\n') |
150 device.write('#include "configuration.h"\n') |
170 device.write('#include "%s/mainwindow.h"\n' % (self.args.sourcedir)) |
151 device.write('void Configuration::initDefaults()\n') |
171 device.write('#include "%s"\n' % headername) |
152 device.write('{\n') |
172 device.write( |
153 device.write('\tBaseConfiguration::initDefaults();\n') |
173 '\n' |
|
174 'Configuration::Configuration() :\n' |
|
175 '\tm_settings(MainWindow::makeSettings(nullptr))\n' |
|
176 '{\n') |
|
177 for declaration in self.declarations.values(): |
154 for declaration in self.declarations.values(): |
178 device.write('\tm_defaults["{name}"] = QVariant::fromValue<{type}>({default});\n'.format(**declaration)) |
155 device.write('\tm_defaults["{name}"] = QVariant::fromValue<{type}>({default});\n'.format(**declaration)) |
179 device.write('}\n' |
|
180 '\n' |
|
181 'Configuration::~Configuration()\n' |
|
182 '{\n' |
|
183 '\tm_settings->deleteLater();\n' |
|
184 '}\n' |
|
185 '\n') |
|
186 device.write('QVariant Configuration::defaultValueByName(const QString& name)\n') |
|
187 device.write('{\n') |
|
188 device.write('\tQMap<QString, QVariant>::iterator it = m_defaults.find(name);\n') |
|
189 device.write('\tif(it != m_defaults.end())\n') |
|
190 device.write('\t\treturn *it;\n') |
|
191 device.write('\telse\n') |
|
192 device.write('\t\treturn {};\n') |
|
193 device.write('}\n') |
156 device.write('}\n') |
194 device.write('\n') |
|
195 device.write('bool Configuration::existsEntry(const QString& name)\n') |
|
196 device.write('{\n') |
|
197 device.write('\treturn m_defaults.find(name) != m_defaults.end();\n') |
|
198 device.write('}\n') |
|
199 device.write('\n') |
|
200 for declaration in self.declarations.values(): |
157 for declaration in self.declarations.values(): |
201 device.write('{type} Configuration::{readgate}() const\n'.format(**declaration)) |
158 device.write('{type} Configuration::{readgate}() const\n'.format(**declaration)) |
202 device.write('{\n') |
159 device.write('{\n') |
203 device.write('\tstatic const QVariant defaultvalue = QVariant::fromValue<{type}>({default});\n'.format(**declaration)) |
160 device.write('\tstatic const QVariant defaultvalue = QVariant::fromValue<{type}>({default});\n'.format(**declaration)) |
204 device.write('\treturn m_settings->value("{name}", defaultvalue).value<{type}>();\n'.format(**declaration)) |
161 device.write('\treturn m_settings->value("{name}", defaultvalue).value<{type}>();\n'.format(**declaration)) |