tools/configcollector.py

changeset 1203
7e34dd7559ce
parent 1157
1d6d244bdabd
child 1204
1d25231dd7c9
--- a/tools/configcollector.py	Mon Mar 06 11:49:00 2017 +0200
+++ b/tools/configcollector.py	Mon Mar 06 12:15:33 2017 +0200
@@ -73,6 +73,7 @@
 		self.declarations = OrderedDict()
 		self.qtTypes = set()
 		self.args = args
+		self.includes = []
 
 	def collect(self, filename):
 		with open(filename) as file:
@@ -81,17 +82,27 @@
 				if line and not line.startswith('#'):
 					from re import search
 					match = search('^option (\w+) = (.+)$', line)
-					if not match:
-						raise ValueError('unable to parse: %r' % line)
-					name, value = match.groups()
-					match = search(r'^(\w+)\s*\{(.*)\}$', value)
-					try:
-						typename, value = match.groups()
-						if not value:
-							value = typename + ' {}'
-					except:
-						typename = deduce_type(value)
-					self.declare(name, typename, value)
+					if match:
+						name, value = match.groups()
+						match = search(r'^(\w+)\s*\{(.*)\}$', value)
+						try:
+							typename, value = match.groups()
+							if not value:
+								value = typename + ' {}'
+						except:
+							typename = deduce_type(value)
+						self.declare(name, typename, value)
+					else:
+						match = search('^include (.+)$', line)
+						if match:
+							filename = match.group(1)
+							if filename.startswith('"'):
+								from os.path import join
+								filename = '"' + join(self.args.sourcedir, filename[1:-1]) + '"'
+							self.includes.append(filename)
+						else:
+							raise ValueError('unable to parse: %r' % line)
+
 		# Sort the declarations in alphabetical order
 		self.declarations = OrderedDict(sorted(self.declarations.items(), key = lambda t: t[1]['name']))
 		# Fill in additional information
@@ -118,14 +129,19 @@
 
 	def writeHeader(self, device):
 		device.write('#pragma once\n')
+		device.write('#include <QObject>\n')
 		device.write('#include <QMap>\n')
+		for include in sorted(self.includes):
+			device.write('#include %s\n' % include)
 		for qtType in sorted(self.qtTypes):
 			device.write('#include <%s>\n' % qtType)
 		device.write('\n')
 		formatargs = {}
 		write = lambda value: device.write(value)
-		write('class Configuration\n')
+		write('class Configuration : public QObject\n')
 		write('{\n')
+		write('\tQ_OBJECT\n')
+		write('\n')
 		write('public:\n')
 		write('\tConfiguration();\n')
 		write('\t~Configuration();\n')
@@ -139,6 +155,9 @@
 		for declaration in filter(lambda declaration: declaration['type'] == 'bool', self.declarations.values()):
 			write('\tvoid {togglefunction}();\n'.format(**declaration))
 		write('\n')
+		write('signals:\n')
+		write('\tvoid configurationChanged(QString, QVariant, QVariant);\n')
+		write('\n')
 		write('private:\n')
 		write('\tQMap<QString, QVariant> m_defaults;\n')
 		write('\tclass QSettings* m_settings;\n')
@@ -188,10 +207,15 @@
 		for declaration in self.declarations.values():
 			device.write('void Configuration::{writegate}({typereference} value)\n'.format(**declaration))
 			device.write('{\n')
-			device.write('\tif(value != {default})\n'.format(**declaration))
-			device.write('\t\tm_settings->setValue("{name}", QVariant::fromValue<{type}>(value));\n'.format(**declaration))
-			device.write('\telse\n')
-			device.write('\t\tm_settings->remove("{name}");\n'.format(**declaration))
+			device.write('\tif (value != {readgate}())\n'.format(**declaration))
+			device.write('\t{\n')
+			device.write('\t\tQVariant oldValue = {readgate}();\n'.format(**declaration))
+			device.write('\t\tif(value != {default})\n'.format(**declaration))
+			device.write('\t\t\tm_settings->setValue("{name}", QVariant::fromValue<{type}>(value));\n'.format(**declaration))
+			device.write('\t\telse\n')
+			device.write('\t\t\tm_settings->remove("{name}");\n'.format(**declaration))
+			device.write('\t\temit configurationChanged("{name}", oldValue, value);\n'.format(**declaration))
+			device.write('\t}\n')
 			device.write('}\n')
 			device.write('\n')
 		for declaration in filter(lambda declaration: declaration['type'] == 'bool', self.declarations.values()):

mercurial