def application_window(qtbot): """This is only a stand-in for the main application window.""" widget = QMainWindow() widget.setAttribute(Qt.WA_DeleteOnClose) widget.show() qtbot.wait_for_window_shown(widget) yield widget def is_dead(): try: widget.objectName() except RuntimeError as exception_instance: return True else: return False widget.close() qtbot.waitUntil(is_dead, timeout=500)
def playDiaporama(diaporamaGenerator, parent=None): """ Open a new window and play a slide show. @param diaporamaGenerator: generator for file names @type diaporamaGenerator: iterator object @param parent: @type parent: """ global isSuspended isSuspended = False # init diaporama window newWin = QMainWindow(parent) newWin.setAttribute(Qt.WA_DeleteOnClose) newWin.setContextMenuPolicy(Qt.CustomContextMenu) newWin.setWindowTitle(parent.tr('Slide show')) label = imageLabel(mainForm=parent) label.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) label.img = None newWin.setCentralWidget(label) newWin.showFullScreen() # Pause key shortcut actionEsc = QAction('Pause', None) actionEsc.setShortcut(QKeySequence(Qt.Key_Escape)) newWin.addAction(actionEsc) # context menu event handler def contextMenuHandler(action): global isSuspended if action.text() == 'Pause': isSuspended = True # quit full screen mode newWin.showMaximized() elif action.text() == 'Full Screen': if action.isChecked(): newWin.showFullScreen() else: newWin.showMaximized() elif action.text() == 'Resume': newWin.close() isSuspended = False playDiaporama(diaporamaGenerator, parent=window) # rating : the tag is written into the .mie file; the file is # created if needed. elif action.text() in ['0', '1', '2', '3', '4', '5']: with exiftool.ExifTool() as e: e.writeXMPTag(name, 'XMP:rating', int(action.text())) # connect shortkey action actionEsc.triggered.connect( lambda checked=False, name=actionEsc: contextMenuHandler( name)) # named arg checked is sent # context menu def contextMenu(position): menu = QMenu() actionEsc.setEnabled(not isSuspended) action2 = QAction('Full Screen', None) action2.setCheckable(True) action2.setChecked(newWin.windowState() & Qt.WindowFullScreen) action3 = QAction('Resume', None) action3.setEnabled(isSuspended) for action in [actionEsc, action2, action3]: menu.addAction(action) action.triggered.connect( lambda checked=False, name=action: contextMenuHandler( name)) # named arg checked is sent subMenuRating = menu.addMenu('Rating') for i in range(6): action = QAction(str(i), None) subMenuRating.addAction(action) action.triggered.connect( lambda checked=False, name=action: contextMenuHandler( name)) # named arg checked is sent menu.exec_(position) # connect contextMenuRequested newWin.customContextMenuRequested.connect(contextMenu) newWin.setToolTip("Esc to exit full screen mode") newWin.setWhatsThis(""" <b>Slide Show</b><br> The slide show cycles through the starting directory and its subfolders to display images. Photos rated 0 or 1 star are not shown (by default, all photos are rated 5 stars).<br> Hit the Esc key to <b>exit full screen mode and pause.</b><br> Use the Context Menu for <b>rating and resuming.</b> The rating is saved in the .mie sidecar and the image file is not modified. """) # end of setWhatsThis # play diaporama window.modeDiaporama = True while True: if isSuspended: newWin.setWindowTitle(newWin.windowTitle() + ' Paused') break try: if not newWin.isVisible(): break name = next(diaporamaGenerator) # search rating in metadata rating = 5 # default with exiftool.ExifTool() as e: try: rt = e.readXMPTag( name, 'XMP:rating') # raise ValueError if sidecar not found r = search("\d", rt) if r is not None: rating = int(r.group(0)) except ValueError: rating = 5 # don't display image with low rating if rating < 2: app.processEvents() imImg = imImage.loadImageFromFile(name, createsidecar=False, cmsConfigure=True, window=window) # zoom might be modified by the mouse wheel : remember if label.img is not None: imImg.Zoom_coeff = label.img.Zoom_coeff coeff = imImg.resize_coeff(label) imImg.yOffset -= (imImg.height() * coeff - label.height()) / 2.0 imImg.xOffset -= (imImg.width() * coeff - label.width()) / 2.0 app.processEvents() if isSuspended: newWin.setWindowTitle(newWin.windowTitle() + ' Paused') break newWin.setWindowTitle( parent.tr('Slide show') + ' ' + name + ' ' + ' '.join(['*'] * imImg.meta.rating)) gc.collect() label.img = imImg label.repaint() app.processEvents() gc.collect() sleep(2) app.processEvents() except StopIteration: newWin.close() window.diaporamaGenerator = None break except ValueError: continue except RuntimeError: window.diaporamaGenerator = None break except: window.diaporamaGenerator = None window.modeDiaporama = False raise app.processEvents() window.modeDiaporama = False
def closeEvent(self, *args, **kwargs): QMainWindow.close(self.win) self.stop_running()