From b85ff4a782e5d97f7096a386c4d8f01726827fc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Mon, 18 Jan 2021 15:53:41 +0100 Subject: [PATCH] Update examples --- examples/genericmonitor.py | 55 +++++++++++++++++++++++++++----------- examples/picture.py | 11 +++++--- examples/timer.py | 2 +- 3 files changed, 48 insertions(+), 20 deletions(-) diff --git a/examples/genericmonitor.py b/examples/genericmonitor.py index ac973bb..5a3df31 100644 --- a/examples/genericmonitor.py +++ b/examples/genericmonitor.py @@ -235,11 +235,25 @@ class GenericMonitor: class GenericMonitorGenericWidget: """ Generic widget class, parent of all widgets """ - def __init__(self): - self.valuesToMap = [] + def __init__(self, style='', name='', signals={}): + """ + Parameters + ---------- + name : str, optional + Widget name + signals : dictionary, optional + Dictionary of signals and their action + """ + self.valuesToMap = ['name', 'style'] self.mapValues = {} self.mapName = '' + self.style = style + self.name = name + self.signals = signals + def setStyle(self, style): + self.style = style + def _toMap(self): """ Return dictionary of class elements to send to addon """ @@ -247,12 +261,14 @@ class GenericMonitorGenericWidget: for p in self.valuesToMap: if self.__dict__[p]: self.mapValues[p] = self.__dict__[p] + for (name, value) in self.signals.items(): + self.mapValues[name] = value return {self.mapName:self.mapValues} class GenericMonitorTextWidget(GenericMonitorGenericWidget): """ Text widget """ - def __init__(self, text, style=''): + def __init__(self, text, style='', name='', signals={}): """ Parameters ---------- @@ -260,19 +276,20 @@ class GenericMonitorTextWidget(GenericMonitorGenericWidget): Text to display style : str, optional CSS style + name : str, optional + Widget name + signals : dictionary, optional + Dictionary of signals and their action """ - self.valuesToMap = ('text', 'style') + super().__init__(style, name, signals) + self.valuesToMap += ['text'] self.mapName = 'text' self.text = text - self.style = style def setText(self, text): self.text = text - def setStyle(self, style): - self.style = style - class GenericMonitorIconWidget(GenericMonitorGenericWidget): """ Icon widget """ @@ -285,7 +302,8 @@ class GenericMonitorIconWidget(GenericMonitorGenericWidget): style : str, optional CSS style """ - self.valuesToMap = ('path', 'style') + super().__init__(style=style) + self.valuesToMap += ['path'] self.mapName = 'icon' self.path = path @@ -293,15 +311,12 @@ class GenericMonitorIconWidget(GenericMonitorGenericWidget): def setPath(self, path): self.path = path - - def setStyle(self, style): - self.style = style -class GenericMonitorPictureWidget(GenericMonitorIconWidget): +class GenericMonitorPictureWidget(GenericMonitorGenericWidget): """ Picture widget """ - def __init__(self, path, style='', width=-1, height=-1): + def __init__(self, path, style='', width=-1, height=-1, name='', signals={}): """ Parameters ---------- @@ -313,15 +328,23 @@ class GenericMonitorPictureWidget(GenericMonitorIconWidget): Width of displayed picture (-1 for default width) height : int, optional Width of displayed picture (-1 for default width) + name : str, optional + Widget name + signals : dictionary, optional + Dictionary of signals and their action """ - super().__init__(path, style) - self.valuesToMap = ('path', 'style', 'width', 'height') + super().__init__(style, name, signals) + self.valuesToMap += ['path', 'width', 'height'] self.mapName = 'picture' + self.path = path self.width = width self.height = height + def setPath(self, path): + self.path = path + def setWidth(self, width): self.width = width diff --git a/examples/picture.py b/examples/picture.py index 3688bde..0fb3d9e 100755 --- a/examples/picture.py +++ b/examples/picture.py @@ -19,6 +19,7 @@ Display random picture from unsplash.com in a popup * Click : open/close popup + * Popup item click : display who clicked * ScrollUp/ScrollDown/Double click : display next picture * Right click : exit ''' @@ -50,8 +51,8 @@ class PicturePopup(GenericMonitor): with open('/tmp/cat2.jpg', 'wb') as f: f.write(datatowrite) widget = GenericMonitorTextWidget('#%d' % self.imgs_idx, 'color:purple') - url_widget = GenericMonitorTextWidget(url, 'color:white;font-weight:bold') - picture_widget = GenericMonitorPictureWidget('/tmp/cat2.jpg') + url_widget = GenericMonitorTextWidget(url, 'color:white;font-weight:bold', signals={'on-click':'signal'}) # No name here + picture_widget = GenericMonitorPictureWidget('/tmp/cat2.jpg', name='NestedWidget', signals={'on-click':'signal'}) popup = GenericMonitorPopup([url_widget, picture_widget]) signals = { 'on-click':'toggle-popup', @@ -68,7 +69,11 @@ class PicturePopup(GenericMonitor): self.imgs_idx += 1 def _forMe(self, sender): - return sender == self.item.getFullName() + return str(sender).endswith(self.item.getFullName()) + + def onClick(self, sender): + if not self._forMe(sender): return + print('Click from {}'.format(sender)) def _onScroll(self, sender): if not self._forMe(sender): return diff --git a/examples/timer.py b/examples/timer.py index 624ef81..77aee68 100755 --- a/examples/timer.py +++ b/examples/timer.py @@ -65,7 +65,7 @@ class TimerThread(Thread,GenericMonitor): self._stopLoop = False self.textWidget = GenericMonitorTextWidget('') - signals = {'on-click':'signal'} + signals = {'on-click':'signal','on-dblclick':'signal','on-rightclick':'signal'} self.monitorItem = GenericMonitorItem('timer', [self.textWidget], signals, box='right') self.monitorGroup = GenericMonitorGroup('Timer', self.monitorItem)