GnomeShellGenericMonitor/examples/picture.py

102 lines
3.2 KiB
Python
Raw Normal View History

2020-11-18 16:20:48 +01:00
#!/usr/bin/env python3
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
'''
Display random picture from unsplash.com in a popup
* Click : open/close popup
* ScrollUp/ScrollDown/Double click : display next picture
* Right click : exit
'''
import urllib.request
import sys
import signal
from genericmonitor import *
class PicturePopup(GenericMonitor):
def __init__(self):
self.scrolling = False
self.item = None
self.imgs_idx = 0
self.setupMonitor()
2020-11-18 16:32:29 +01:00
def run(self):
2020-11-18 16:20:48 +01:00
self.display_next_img()
self.runMainLoop()
2020-11-18 16:32:29 +01:00
2020-11-18 16:20:48 +01:00
def display_next_img(self):
filedata = urllib.request.urlopen('https://source.unsplash.com/random')
# Get redirected URL without parameters
url = filedata.url.split('?')[0]
filedata = urllib.request.urlopen(url + '?fit=max&width=500&height=500')
datatowrite = filedata.read()
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')
popup = GenericMonitorPopup([url_widget, picture_widget])
signals = {
'on-click':'toggle-popup',
# Could also use this behavior
# 'on-enter':'open-popup',
# 'on-leave':'close-popup',
2020-11-18 16:20:48 +01:00
'on-dblclick':'signal',
'on-rightclick':'signal',
'on-scroll':'signal',
}
self.item = GenericMonitorItem('picturepopup', [widget], signals, popup)
group = GenericMonitorGroup('PicturePopup', [self.item])
self.notify(group)
self.imgs_idx += 1
def _forMe(self, sender):
return sender == self.item.getFullName()
def _onScroll(self, sender):
if not self._forMe(sender): return
if self.scrolling: return
self.scrolling = True
self.display_next_img()
self.scrolling = False
def onScrollUp(self, sender):
self._onScroll(sender)
def onScrollDown(self, sender):
self._onScroll(sender)
def onDblClick(self, sender):
self.onScrollUp(sender)
def onRightClick(self, sender):
if not self._forMe(sender): return
if self.item:
self.deleteItems([self.item.getFullName()])
self.stopMainLoop()
2020-11-18 16:32:29 +01:00
def signal_handler(sig, frame):
picture.deleteGroups(['PicturePopup'])
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
2020-11-18 16:20:48 +01:00
picture = PicturePopup()
2020-11-18 16:32:29 +01:00
picture.run()