SF.net SVN: fclient:[845] trunk/fclient/fclient/impl/lib/qt4ex/led.py
Status: Pre-Alpha
Brought to you by:
jurner
|
From: <jU...@us...> - 2008-08-01 23:51:47
|
Revision: 845
http://fclient.svn.sourceforge.net/fclient/?rev=845&view=rev
Author: jUrner
Date: 2008-08-01 23:51:57 +0000 (Fri, 01 Aug 2008)
Log Message:
-----------
experimental, add a led widget
Added Paths:
-----------
trunk/fclient/fclient/impl/lib/qt4ex/led.py
Added: trunk/fclient/fclient/impl/lib/qt4ex/led.py
===================================================================
--- trunk/fclient/fclient/impl/lib/qt4ex/led.py (rev 0)
+++ trunk/fclient/fclient/impl/lib/qt4ex/led.py 2008-08-01 23:51:57 UTC (rev 845)
@@ -0,0 +1,121 @@
+"""2 state Led widget mostly taken from Eric4's E4Led, thanks detlev
+(kde4s KLed is incredibly slow...)
+"""
+
+
+from PyQt4 import QtCore, QtGui
+#**********************************************************************************
+#
+#**********************************************************************************
+
+class Led(QtGui.QWidget):
+
+
+ def __init__(self, parent=None, colorOn=QtGui.QColor('green'), colorOff=QtGui.QColor('red'), isOn=True):
+ QtGui.QWidget.__init__(self, parent)
+
+ self.__isOn = isOn
+ self._colorOn = colorOn
+ self._colorOff =colorOff
+ self._timer = None
+
+ def isOn(self):
+ return self.__isOn
+ def setOn(self, flag):
+ self.__isOn = flag
+ self.update()
+ return True
+ isOn = QtCore.pyqtProperty("bool",isOn, setOn)
+
+
+ def paintEvent(self, event):
+ self.paintRaised()
+
+
+ def ledWidth(self):
+ width = self.width()
+
+ # enshure Led is round
+ if width > self.height():
+ width = self.height()
+
+ # leave one pixel border
+ width -= 2
+ width = 0 if width < 0 else width
+ return width
+
+ def paintRaised(self):
+ """"""
+ # Initialize coordinates, width and height of the LED
+ width = self.ledWidth()
+ palette = self.palette()
+
+ # Calculate the gradient for the LED
+ wh = int(width / 2)
+ color = self.colorOn() if self.isOn else self.colorOff()
+
+ gradient = QtGui.QRadialGradient(wh, wh, wh, 0.8 * wh, 0.8 * wh)
+ gradient.setColorAt(0.0, color.light(200))
+ gradient.setColorAt(0.6, color)
+ gradient.setColorAt(1.0, color.dark())
+
+ # now do the drawing
+ paint = QtGui.QPainter(self)
+ paint.setRenderHint(paint.Antialiasing, True)
+ paint.setBrush(QtGui.QBrush(gradient))
+ paint.setPen(QtCore.Qt.NoPen)
+ paint.drawEllipse(1, 1, width, width)
+ paint.end()
+
+
+ def colorOn(self):
+ return self._colorOn
+
+ def colorOff(self):
+ return self._colorOff
+
+ def setColorOn(self, color):
+ self.colorOn = color
+ if self.isOn():
+ self.update()
+
+ def setColorOff(self, color):
+ self.colorOff = color
+ if self.isOff():
+ self.update()
+
+
+ def flashing(self):
+ if self._timer is not None:
+ return self._timer.isActive()
+ return False
+
+ def setFlashing(self, flag, interval=300):
+ if flag:
+ if not self._timer:
+ self._timer = QtCore.QTimer(self)
+ self.connect(self._timer, QtCore.SIGNAL('timeout()'), self.onTimerTimeout)
+ self._timer.setInterval(interval)
+ self._timer.start()
+ else:
+ if self._timer is not None:
+ self._timer.stop()
+
+
+ def onTimerTimeout(self):
+ if self._timer.isActive():
+ self.setOn(not self.isOn)
+
+
+
+#**********************************************************************************
+#
+#**********************************************************************************
+if __name__ == '__main__':
+ import sys
+
+ app = QtGui.QApplication(sys.argv)
+ w = Led(None)
+ w.show()
+ res = app.exec_()
+ sys.exit(res)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|