예제 #1
0
	def __init__(self, parent=None):
		QtGui.QMainWindow.__init__(self, parent)
		self.ui = Ui_MainWindow()
		self.ui.setupUi(self)
		self._Timer = QtCore.QTimer(self)
		self._watchTimer = QtCore.QTimer(self)
		self.connect(self._Timer, QtCore.SIGNAL('timeout()'), self.timer)
		self.connect(self._watchTimer, QtCore.SIGNAL('timeout()'), self.watch)
		QtCore.QObject.connect(self.ui.pushButton,QtCore.SIGNAL("clicked()"), self.startStop)
		QtCore.QObject.connect(self.ui.testPortPushButton,QtCore.SIGNAL("clicked()"), self.testPort)
		QtCore.QObject.connect(self.ui.showFieldsPushButton,QtCore.SIGNAL("clicked()"), self.showFields)
		QtCore.QObject.connect(self.ui.buttonBox,QtCore.SIGNAL("accepted()"), self.saveConfiguration)
		QtCore.QObject.connect(self.ui.buttonBox,QtCore.SIGNAL("rejected()"), self.loadConfiguration)
		QtCore.QObject.connect(self.ui.AutoArrangeCheckBox,QtCore.SIGNAL("clicked()"), self.changePresetsComboBoxEnabled)

		self.setWindowTitle(__project__ + " ver. " + __version__ )

		self.ui.AboutVersionLabel.setText("ver. " + __version__)

		self.labels = [self.ui.label, self.ui.label_2, self.ui.label_3, self.ui.label_4, self.ui.label_5, self.ui.label_6, self.ui.label_7, self.ui.label_8]	# fieldlabels
		self.fieldsWidgets = []
		self.blackout = [0,0,0,0,0,0,0,0]
예제 #2
0
class MyForm(QtGui.QMainWindow):
	
	def __init__(self, parent=None):
		QtGui.QMainWindow.__init__(self, parent)
		self.ui = Ui_MainWindow()
		self.ui.setupUi(self)
		self._Timer = QtCore.QTimer(self)
		self._watchTimer = QtCore.QTimer(self)
		self.connect(self._Timer, QtCore.SIGNAL('timeout()'), self.timer)
		self.connect(self._watchTimer, QtCore.SIGNAL('timeout()'), self.watch)
		QtCore.QObject.connect(self.ui.pushButton,QtCore.SIGNAL("clicked()"), self.startStop)
		QtCore.QObject.connect(self.ui.testPortPushButton,QtCore.SIGNAL("clicked()"), self.testPort)
		QtCore.QObject.connect(self.ui.showFieldsPushButton,QtCore.SIGNAL("clicked()"), self.showFields)
		QtCore.QObject.connect(self.ui.buttonBox,QtCore.SIGNAL("accepted()"), self.saveConfiguration)
		QtCore.QObject.connect(self.ui.buttonBox,QtCore.SIGNAL("rejected()"), self.loadConfiguration)
		QtCore.QObject.connect(self.ui.AutoArrangeCheckBox,QtCore.SIGNAL("clicked()"), self.changePresetsComboBoxEnabled)

		self.setWindowTitle(__project__ + " ver. " + __version__ )

		self.ui.AboutVersionLabel.setText("ver. " + __version__)

		self.labels = [self.ui.label, self.ui.label_2, self.ui.label_3, self.ui.label_4, self.ui.label_5, self.ui.label_6, self.ui.label_7, self.ui.label_8]	# fieldlabels
		self.fieldsWidgets = []
		self.blackout = [0,0,0,0,0,0,0,0]


	def startStop(self):
		"""
		start/stop Timer and change text on Button
		"""
		global ser
		if not self._Timer.isActive(): 							# if timer doesn't work
			self.loadConfiguration()
			self.ui.pushButton.setText("Stop!")
			self.ui.tab_2.setEnabled(0) 						# no messing with settings during work!
			try:
				if ser.isOpen(): 									# close and open port - hardware likes it
					ser.close()
				ser.open()
			except:
				pass
			self._Timer.start(config.getint('Timer', 'interval'))
			
		else:
			self._Timer.stop()
			self.ui.pushButton.setText("Start!")
			self.ui.tab_2.setEnabled(1)
			self.sendColors(self.blackout)
			try:
				ser.close()
			except:
				pass


	def getColor(self, px, py, w, h ):
		"""
		Grab specific field and resize it to receive average color of field
		
		@return a color value
		"""
		self.originalPixmap = QtGui.QPixmap.grabWindow(QtGui.QApplication.desktop().winId(), px, py, w, h)
		self.destPixmap = self.originalPixmap.scaled(1, 1, QtCore.Qt.IgnoreAspectRatio, QtCore.Qt.SmoothTransformation)
		self.destImage = self.destPixmap.toImage()
		value = self.destImage.pixel(0,0)
		return value


	def timer(self):
		"""
		getColor for every field and display it on appropriate label
		"""
		colors = []
		for field, x, y, w, h in [(1, fieldsconfig.getint('1', 'x'), fieldsconfig.getint('1', 'y'), fieldsconfig.getint('1', 'w'), fieldsconfig.getint('1', 'h')),
									(2, fieldsconfig.getint('2', 'x'), fieldsconfig.getint('2', 'y'), fieldsconfig.getint('2', 'w'), fieldsconfig.getint('2', 'h')),
									(3, fieldsconfig.getint('3', 'x'), fieldsconfig.getint('3', 'y'), fieldsconfig.getint('3', 'w'), fieldsconfig.getint('3', 'h')),
									(4, fieldsconfig.getint('4', 'x'), fieldsconfig.getint('4', 'y'), fieldsconfig.getint('4', 'w'), fieldsconfig.getint('4', 'h')),
									(5, fieldsconfig.getint('5', 'x'), fieldsconfig.getint('5', 'y'), fieldsconfig.getint('5', 'w'), fieldsconfig.getint('5', 'h')),
									(6, fieldsconfig.getint('6', 'x'), fieldsconfig.getint('6', 'y'), fieldsconfig.getint('6', 'w'), fieldsconfig.getint('6', 'h')),
									(7, fieldsconfig.getint('7', 'x'), fieldsconfig.getint('7', 'y'), fieldsconfig.getint('7', 'w'), fieldsconfig.getint('7', 'h')),
									(8, fieldsconfig.getint('8', 'x'), fieldsconfig.getint('8', 'y'), fieldsconfig.getint('8', 'w'), fieldsconfig.getint('8', 'h'))]:
			color = self.getColor(x, y, w, h)
			colors.append(color)
			self.updateLabel(field, x, y, w, h, color)
		self.sendColors(colors)
		


	def addSum(self, value):
		global sum
		sum += value ##???
		if sum > 255:
			sum -=256


	def sendColors(self, colors):
		kod = chr(128) #kod inicjujacy poczatek standardowej paczki + konfig
		global sum
		sum = 0
		self.addSum(128) #suma kontrolna tj. suma wszystkich wartosci, skrocona do 7 bitow (bajt podzielony przez dwa)
		for color in colors:
			red = float(QtGui.qRed(color))/255
			green = float(QtGui.qGreen(color))/255
			blue = float(QtGui.qBlue(color))/255
			verbose("k: %d" % (colors.index(color)+1), 3)
			verbose("\trgb: %f, %f, %f" % (red, green, blue), 3)
			red = int(red*red*100)
			green = int(green*green*100)
			blue = int(blue*blue*100)
			verbose("\t\trgb: %f, %f, %f" % (red, green, blue), 3)
			kod += chr(red)
			kod += chr(green)
			kod += chr(blue)
			self.addSum(red)
			self.addSum(green)
			self.addSum(blue)
		kod += chr(sum/2)
		try:
			ser.write(kod)
		except:
			verbose("--\nCannot send to device. Check your configuration!",1)
		time.sleep(0.009) # hack needed by hardware


	def watch(self):
		"""
		Get remote code and perform an action
		"""
		try:
			temp=ser.read()
			ser.flushInput()
			x=ord(temp)
			command = "echo \'%d - not bind\'" % x ##XXX debug
			if x == 14: command='smplayer -send-action pause'#works as pause/play
			if x == 59: command='smplayer -send-action pause'#works as pause/play
			if x == 10: command='smplayer -send-action stop'
			if x == 0: command='smplayer -send-action play'
			if x == 16: command='smplayer -send-action increase_volume'
			if x == 17: command='smplayer -send-action decrease_volume'
			if x == 13: command='smplayer -send-action mute' #mute/unmute
			if x == 5: command='smplayer -send-action dec_sub_scale' #decrease subtitles
			if x == 8: command='smplayer -send-action inc_sub_scale' #increase subtitles
			if x == 7: command='smplayer -send-action rewind1' #small jump
			if x == 9: command='smplayer -send-action forward1'
			if x == 4: command='smplayer -send-action rewind2' #medium jump
			if x == 6: command='smplayer -send-action forward2'
			if x == 1: command='smplayer -send-action rewind3'#large jump
			if x == 3: command='smplayer -send-action forward3'
			verbose("%s:\t%s" % (x, command), 2)
			os.system(command)
		except:
			verbose("no remote command", 2)


	def getLabel (self, field):
		"""
		@return label corresponding to field
		"""
		return self.labels[field-1]


	def updateLabel (self, field, x, y, w, h, color = 0): # default color is black
		"""
		set label text and color
		"""
		label = self.getLabel(field)
		label.setText(`x` + ", " + `y` + ", " + `w` + ", " + `h`)
		palette = QtGui.QPalette(label.palette())
		palette.setColor(QtGui.QPalette.Window, QtGui.QColor(color))
		label.setPalette(palette)


	def showFields(self):
		"""
		draw fields
		"""
		if  self.ui.showFieldsPushButton.isChecked():
			if (config.get("Fields", "autoarrange") == "on" and self.ui.AutoArrangeCheckBox.checkState() != 2) or (config.get("Fields", "autoarrange") == "off" and self.ui.AutoArrangeCheckBox.checkState() != 0) or (config.getint("Fields", "size") != self.ui.AutoarrangeHorizontalSlider.value()): ###XXX ugly hack
				SAVE = self.tr("Save")
				CANCEL = self.tr("Cancel")
				message = QtGui.QMessageBox(self)
				message.setText(self.tr('Save changes before?'))
				message.setWindowTitle('Mirlight')
				message.setIcon(QtGui.QMessageBox.Question)
				message.addButton(SAVE, QtGui.QMessageBox.AcceptRole)
				message.addButton(CANCEL, QtGui.QMessageBox.RejectRole)
				message.exec_()
				response = message.clickedButton().text()
				if response == SAVE:
					self.saveConfiguration()
				else:
					self.loadConfiguration()

			self.fieldsWidgets = [] ## clear to avoid multiple instances
			self.widget1 = FieldDialog(1)
			self.fieldsWidgets.append(self.widget1)
			self.widget2 = FieldDialog(2)
			self.fieldsWidgets.append(self.widget2)
			self.widget3 = FieldDialog(3)
			self.fieldsWidgets.append(self.widget3)
			self.widget4 = FieldDialog(4)
			self.fieldsWidgets.append(self.widget4)
			self.widget5 = FieldDialog(5)
			self.fieldsWidgets.append(self.widget5)
			self.widget6 = FieldDialog(6)
			self.fieldsWidgets.append(self.widget6)
			self.widget7 = FieldDialog(7)
			self.fieldsWidgets.append(self.widget7)
			self.widget8 = FieldDialog(8)
			self.fieldsWidgets.append(self.widget8)

			for widget in self.fieldsWidgets:
				widget.show()
			self.ui.showFieldsPushButton.setText(self.tr("Hide fields"))
			self.ui.AutoArrangeCheckBox.setEnabled(0)
			self.ui.AutoarrangeHorizontalSlider.setEnabled(0)
			self.ui.buttonBox.setEnabled(0)
			self.ui.PresetsComboBox.setEnabled(0)

		elif config.get("Fields", "autoarrange") == "off" and self.checkFieldsAreChanged():
			SAVE = self.tr("Save")
			CANCEL = self.tr("Cancel")
			presets = ""
			for infile in glob.glob("presets/*.mrl"):
				presets += infile[8:-4] + ", "
			ms1 = self.tr("Existed names:")
			ms2 = self.tr("Enter a new name (or an old one to overwrite):")
			(presetName, state) = QtGui.QInputDialog.getText(self, "Mirlight", "%s %s\n%s" % (ms1, presets, ms2), QtGui.QLineEdit.Normal, self.ui.PresetsComboBox.currentText())
			if state == True and len(presetName) > 0:
				self.saveFields(presetName)
				verbose("--\nSaved as: %s.mrl" % presetName,1)
			else:
				verbose("--\nPreset not saved",1)
			self.closeFields()
		else:
			self.closeFields()


	def closeFields(self):
		"""
		hide fields preview
		"""
		for widget in self.fieldsWidgets:
			widget.close()
		self.changePresetsComboBoxEnabled() 			#to apply an appropriate label on fieldsPushButton
		self.ui.AutoArrangeCheckBox.setEnabled(1)
		self.ui.buttonBox.setEnabled(1)

	def checkFieldsAreChanged(self):
		"""
		check if even one field has changed position or size
		@return True or False
		"""
		for field, widget in zip([1,2,3,4,5,6,7,8], self.fieldsWidgets):
			x, y, w, h = widget.getGeometry()
			if int(fieldsconfig.get(`field`, "x")) == x and \
					int(fieldsconfig.get(`field`, "y")) == y and \
					int(fieldsconfig.get(`field`, "w")) == w and \
					int(fieldsconfig.get(`field`, "h")) == h:
				pass
			else:
				return True
		verbose("--\nThere is no change in fields",1)
		return False