Verbessertes Zeichnen mit mehreren Drawing-Areas

This commit is contained in:
2025-09-14 17:19:45 +02:00
parent 20882feeca
commit f3c9ced477
5 changed files with 72 additions and 40 deletions

75
led.py
View File

@@ -39,9 +39,11 @@ class LED():
self._brightness = 0.0
self._color_off = (0.3725, 0.4275, 0.6078) # RGB(95, 109, 155)
self._pos_x = 30
self._pos_y = 16
self._radius = 4
self._pos_y = 14
self._radius = 5
self._flashcounter = 0
self._flashcolor = (0, 0, 0)
self._enabled = False
# Box oberhalb des Displays
self._da = Gtk.DrawingArea()
@@ -50,11 +52,17 @@ class LED():
self._da.connect("draw", self.on_draw)
def on_draw(self, widget, ctx):
#Debug
# Debug um Zeichenbereich zu sehen
#ctx.set_line_width(1.0)
#ctx.set_source_rgb(1, 0, 0)
#ctx.rectangle(0, 0, 400, 32)
#ctx.rectangle(0.5, 0.5, 400, 32)
#ctx.stroke()
ctx.set_source_rgb(*self._color_off)
print("LED draw")
ctx.set_line_width(2.0)
if self._brightness == 0:
ctx.set_source_rgb(*self._color_off)
else:
ctx.set_source_rgb(*self._color)
ctx.arc(self._pos_x, self._pos_y, self._radius, 0, 2*math.pi)
ctx.fill_preserve()
ctx.set_source_rgb(0, 0, 0) # Schwarzer Rand
@@ -63,39 +71,46 @@ class LED():
ctx.stroke()
def on_timer(self):
print("Timer")
self.switchOff()
return True
return False
def on_blink(self):
print("Blink-Timer")
return False
def on_flash(self):
if self._flashcounter > 0:
self._flashcounter -=1
# TODO do not wait for drawing event but draw immediately
print("Flash-Timer", self._brightness)
self._da.queue_draw()
if self._brightness == 1:
self._brightness = 0
else:
self._brightness = 1
if self._flashcounter > 0:
if self._brightness == 0:
self._flashcounter -=1
print("Flash timer ongoing")
return True
print("Flash finished")
self._brightness = 0
return False
def switchOn(self, duration=None):
self._brightness = 1.0
self._ctx.save()
self._ctx.set_source_rgb(*self._color)
self._ctx.arc(self._pos_x, self._pos_y, self._radius, 0, 2*math.pi)
self._ctx.fill_preserve()
self._ctx.set_source_rgb((0, 0, 0)) # Schwarzer Rand
self._ctx.stroke()
self._ctx.restore()
if self._duration:
GLib.timeout_add_seconds(duration, self.on_flash)
self._da.queue_draw()
if duration:
GLib.timeout_add_seconds(duration, self.on_timer)
def switchOff(self):
self._brightness = 0.0
self._ctx.save()
self._ctx.set_source_rgb(*self._color_off)
self._ctx.arc(self._pos_x, self._pos_y, self._radius, 0, 2*math.pi)
self._ctx.fill_preserve()
self._ctx.set_source_rgb((0, 0, 0)) # Schwarzer Rand
self._ctx.stroke()
self._ctx.restore()
self._da.queue_draw()
def doBlink(self, duration=None):
pass
def doFlash(self, count=1):
self._cr = self._da.get_window().cairo_create() # Context for immediate drawing
if (self._brightness > 0) or (count < 0):
# Wenn aktiv oder falscher Parameter, dann passiert nichts
return
@@ -104,7 +119,9 @@ class LED():
self._flashcounter = 10
else:
self._flashcounter = count
GLib.timeout_add_seconds(0.2, self.on_flash)
self._brightness = 1
self._da.queue_draw()
GLib.timeout_add_seconds(0.5, self.on_flash)
def setColor(self, colorname, brightness=1.0):
#TODO Helligkeit einstellen
@@ -118,8 +135,10 @@ class LED():
return self._color
def getColorAsHex(self):
# TODO
return None
return '#{:02x}{:02x}{:02x}'.format(
int(self._color[0] * 255),
int(self._color[1] * 255),
int(self._color[2] * 255))
def setBrightness(self, newbrightness):
# 0% bis 100%, ausgehend von der aktuellen Basisfarbe