File: configuration.py

package info (click to toggle)
python-whiteboard 1.0%2Bgit20170915-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,568 kB
  • sloc: python: 1,702; xml: 61; makefile: 23; sh: 12
file content (347 lines) | stat: -rw-r--r-- 9,975 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# -*- coding: utf-8 -*-

from PyQt5 import QtCore, QtGui, QtWidgets, uic
import PyQt5.Qt as qt


CONFIG_VERSION = 12


class Configuration:

	class __impl:
		""" Implementation of the singleton interface """
		
		def __init__(self):
			self.settings = QtCore.QSettings("pywhiteboard","pywhiteboard")
			
			self.defaults = {
				"fullscreen": "Yes",
				"selectedmac": '*',
				"zone1": "1",
				"zone2": "2",
				"zone3": "3",
				"zone4": "0",
				"autoconnect": "Yes",
				"autocalibration": "Yes",
				"sensitivity": "6",
				"smoothing": "10",
				"moveonly": "No",
				"automatrix": "No",
				"nowaitdevices": "Yes",
			}
			
			version = self.getValueStr("version")
			if version == '' or int(version) < CONFIG_VERSION:
				self.settings.clear()
				self.saveValue("version",str(CONFIG_VERSION))
			
			self.activeGroup = None
			self.setGroup("default")
			
		
		def wipe(self):
			self.settings.clear()
		
		
		def saveValue(self,name,value):
			self.settings.setValue(name,QtCore.QVariant(value))

		
		def getValueStr(self,name):
			v = self.settings.value(name).toString()
			if v != '': return v
			if v == '' and name in self.defaults.keys():
				return self.defaults[name]
			else: return ''
		
		
		def writeArray(self,name,lst):
			self.settings.beginWriteArray(name)
			for i,dct in enumerate(lst):
				self.settings.setArrayIndex(i)
				for k in dct.keys():
					self.settings.setValue(k,dct[k])
			self.settings.endArray()
		
		
		def readArray(self,name):
			n = self.settings.beginReadArray(name)
			result = []
			for i in range(0,n):
				self.settings.setArrayIndex(i)
				kys = self.settings.childKeys()
				d = dict()
				for k in kys:
					d[unicode(k)] = unicode(self.settings.value(k).toString())
				result.append(d)
			self.settings.endArray()
			return result
		
		
		def setGroup(self,name):
			if self.activeGroup:
				self.settings.endGroup()
			pastGroup = self.activeGroup
			self.activeGroup = name
			self.settings.beginGroup(name)
			return pastGroup
		
		
		########### Get and set profile list ########################
		def getProfileList(self):
			activeGroup = self.setGroup("default")
			result = []
			n = self.settings.beginReadArray("profiles")
			for i in range(0,n):
				self.settings.setArrayIndex(i)
				result.append(unicode(self.settings.value('item').toString()))
			self.settings.endArray()
			self.setGroup(activeGroup)
			return result
		
		
		def setProfileList(self, profileList):
			activeGroup = self.setGroup("default")
			self.settings.beginWriteArray("profiles")
			for i, profileName in enumerate(profileList):
				self.settings.setArrayIndex(i)
				self.settings.setValue('item', profileName)
			self.settings.endArray()
			self.setGroup(activeGroup)
		##############################################################
		

	# storage for the instance reference
	__instance = None

	def __init__(self):
		""" Create singleton instance """
		# Check whether we already have an instance
		if Configuration.__instance is None:
			# Create and remember i10nstance
			Configuration.__instance = Configuration.__impl()

		# Store instance reference as the only member in the handle
		self.__dict__['_Configuration__instance'] = Configuration.__instance

	def __getattr__(self, attr):
		""" Delegate access to implementation """
		return getattr(self.__instance, attr)

	def __setattr__(self, attr, value):
		""" Delegate access to implementation """
		return setattr(self.__instance, attr, value)



class ConfigDialog(QtWidgets.QDialog):

	def __init__(self, parent, wii=None):
		super(ConfigDialog, self).__init__(parent)
		self.ui = uic.loadUi("configuration.ui",self)
		
		self.wii = wii
		
		self.connect(self.ui.check_fullscreen,
			QtCore.SIGNAL("stateChanged(int)"), self.checkStateChanged)
		self.connect(self.ui.check_autoconnect,
			QtCore.SIGNAL("stateChanged(int)"), self.checkStateChanged)
		self.connect(self.ui.check_autocalibration,
			QtCore.SIGNAL("stateChanged(int)"), self.checkStateChanged)
		self.connect(self.ui.check_automatrix,
			QtCore.SIGNAL("stateChanged(int)"), self.checkStateChanged)
		self.connect(self.ui.check_nowait,
			QtCore.SIGNAL("stateChanged(int)"), self.checkStateChanged)
		
		self.connect(self.ui.button_addDev,
			QtCore.SIGNAL("clicked()"), self.addDevice)
		self.connect(self.ui.button_remDev,
			QtCore.SIGNAL("clicked()"), self.removeDevice)
		
		pixmap = QtGui.QPixmap("screen.png")
		self.areasScene = QtGui.QGraphicsScene()
		self.areasScene.addPixmap(pixmap)
		self.screenAreas.setScene(self.areasScene)
		self.screenAreas.show()
		
		self.connect(self.ui.combo1,
			QtCore.SIGNAL("currentIndexChanged(int)"), self.changeCombo)
		self.connect(self.ui.combo2,
			QtCore.SIGNAL("currentIndexChanged(int)"), self.changeCombo)
		self.connect(self.ui.combo3,
			QtCore.SIGNAL("currentIndexChanged(int)"), self.changeCombo)
		self.connect(self.ui.combo4,
			QtCore.SIGNAL("currentIndexChanged(int)"), self.changeCombo)
		self.updateCombos()
		
		self.ui.slider_ir.setMinimum(1)
		self.ui.slider_ir.setMaximum(6)
		self.connect(self.ui.slider_ir,
			QtCore.SIGNAL("valueChanged(int)"), self.sliderIrMoved)
		
		self.ui.slider_smoothing.setMinimum(1)
		self.ui.slider_smoothing.setMaximum(10)
		self.connect(self.ui.slider_smoothing,
			QtCore.SIGNAL("valueChanged(int)"), self.sliderSmMoved)
		
		self.refreshWidgets()
		self.checkButtons()
	
	
	
	def refreshWidgets(self):
		conf = Configuration()
		self.ui.check_fullscreen.setChecked(conf.getValueStr("fullscreen") == "Yes")
		self.ui.check_autoconnect.setChecked(conf.getValueStr("autoconnect") == "Yes")
		self.ui.check_autocalibration.setChecked(conf.getValueStr("autocalibration") == "Yes")
		self.ui.check_automatrix.setChecked(conf.getValueStr("automatrix") == "Yes")
		self.ui.check_nowait.setChecked(conf.getValueStr("nowaitdevices") == "Yes")
		self.updateCombos()
		self.setupMacTable()
		
		sens = int(conf.getValueStr("sensitivity"))
		self.ui.slider_ir.setValue(sens)
		smth = int(conf.getValueStr("smoothing"))
		self.ui.slider_smoothing.setValue(smth)
		
	
	
	def checkButtons(self):
		if self.wii == None:
			self.ui.button_addDev.setEnabled(False)
		else:
			self.ui.button_addDev.setEnabled(True)
	
	
	
	def setupMacTable(self):
		self.ui.tableMac.setColumnCount(2)
		self.ui.tableMac.setHorizontalHeaderLabels([self.tr('Address'), self.tr('Comment')])
		self.ui.tableMac.setSelectionMode(QtGui.QTableWidget.SingleSelection)
		self.ui.tableMac.setSelectionBehavior(QtGui.QTableWidget.SelectRows)
		self.refreshMacTable()
		header = self.ui.tableMac.horizontalHeader()
		header.setStretchLastSection(True)
		self.connect(self.ui.tableMac,
			QtCore.SIGNAL("cellClicked(int,int)"), self.macTableCellSelected)
	
	
	def macTableCellSelected(self,r,c):
		address = unicode(self.ui.tableMac.item(r,0).text())
		conf = Configuration()
		conf.saveValue('selectedmac',address)
	
	
	def refreshMacTable(self):
		while self.ui.tableMac.item(0,0):
			self.ui.tableMac.removeRow(0)
		
		self.ui.tableMac.insertRow(0)
		item = QtGui.QTableWidgetItem('*')
		self.ui.tableMac.setItem(0,0,item)
		item = QtGui.QTableWidgetItem(self.tr('All devices'))
		self.ui.tableMac.setItem(0,1,item)
		self.ui.tableMac.selectRow(0)
		conf = Configuration()
		lst = conf.readArray('maclist')
		for elem in lst:
			rc = self.ui.tableMac.rowCount()
			self.ui.tableMac.insertRow(rc)
			item = QtGui.QTableWidgetItem(elem['address'])
			self.ui.tableMac.setItem(rc,0,item)
			item = QtGui.QTableWidgetItem(elem['comment'])
			self.ui.tableMac.setItem(rc,1,item)
			selected = conf.getValueStr('selectedmac')
			if selected == elem['address']:
				self.ui.tableMac.selectRow(rc)
	
	
	
	
	def addDevice(self):
		if self.wii == None: return
		conf = Configuration()
		d = conf.readArray('maclist')
		address = self.wii.addr
		for item in d:
			if item['address'] == address: return
		
		comment, ok = QtGui.QInputDialog.getText(self,
			self.tr("Comment"), self.tr('Wii device description'))
		
		if ok:
			d.append( {'address': address, 'comment': comment} )
			conf.writeArray('maclist',d)
			self.refreshMacTable()
	
	
	def removeDevice(self):
		conf = Configuration()
		mlist = conf.readArray('maclist')
		for it in self.ui.tableMac.selectedItems():
			if it.column() == 0:
				address = it.text()
				mlist = [ elem for elem in mlist if elem['address'] != address ]
				conf.writeArray('maclist',mlist)
				self.refreshMacTable()
				conf.saveValue('selectedmac','*')
				return
	
	
	def sliderSmMoved(self,val):
		conf = Configuration()
		conf.saveValue("smoothing",str(val))
		self.ui.label_smoothing.setText(self.tr("Smoothing: ") + str(val))
	
	
	def sliderIrMoved(self, val):
		conf = Configuration()
		conf.saveValue("sensitivity",str(val))
		self.ui.label_sensitivity.setText(self.tr("IR Sensitivity: ") + str(val))
	
		
	def finish(self):
		self.close()
	
	
	def updateCombos(self):
		conf = Configuration()
		for combo,zone in [(self.ui.combo1,"zone1"), (self.ui.combo2,"zone2"), (self.ui.combo3,"zone3"), (self.ui.combo4,"zone4")]:
			ind = int(conf.getValueStr(zone))
			combo.setCurrentIndex(ind)

	def changeCombo(self,i):
		sender = self.sender()
		conf = Configuration()
		if sender == self.ui.combo1:
			conf.saveValue("zone1",str(i))
		elif sender == self.ui.combo2:
			conf.saveValue("zone2",str(i))
		elif sender == self.ui.combo3:
			conf.saveValue("zone3",str(i))
		elif sender == self.ui.combo4:
			conf.saveValue("zone4",str(i))
	
	def checkStateChanged(self,i):
		yesno = 'Yes'
		if i == 0: yesno = 'No'
		sender = self.sender()
		conf = Configuration()
		if sender == self.ui.check_fullscreen:
			conf.saveValue('fullscreen',yesno)
		if sender == self.ui.check_autoconnect:
			conf.saveValue('autoconnect',yesno)
		if sender == self.ui.check_autocalibration:
			conf.saveValue('autocalibration',yesno)
		if sender == self.ui.check_automatrix:
			conf.saveValue('automatrix',yesno)
		if sender == self.ui.check_nowait:
			conf.saveValue('nowaitdevices',yesno)
	
	
	def closeEvent(self,e):
		e.accept()