Update examples

This commit is contained in:
Grégory Soutadé 2021-01-18 15:53:41 +01:00
parent 30bfc796b9
commit b85ff4a782
3 changed files with 48 additions and 20 deletions

View File

@ -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

View File

@ -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

View File

@ -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)