#!/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 . # ''' 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 * def signal_handler(sig, frame): sys.exit(0) signal.signal(signal.SIGINT, signal_handler) class PicturePopup(GenericMonitor): def __init__(self): self.scrolling = False self.item = None self.imgs_idx = 0 self.setupMonitor() self.display_next_img() self.runMainLoop() 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', '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() picture = PicturePopup()