def initWins(self): # viewer main form newWin = QMainWindow(self.mainWin) newWin.setAttribute(Qt.WA_DeleteOnClose) newWin.setContextMenuPolicy(Qt.CustomContextMenu) self.newWin = newWin # image list listWdg = dragQListWidget() listWdg.setWrapping(False) listWdg.setSelectionMode(QAbstractItemView.ExtendedSelection) listWdg.setContextMenuPolicy(Qt.CustomContextMenu) listWdg.label = None listWdg.setViewMode(QListWidget.IconMode) # set icon and listWdg sizes listWdg.setIconSize(QSize(self.iconSize, self.iconSize)) listWdg.setMaximumSize(160000, self.iconSize + 20) listWdg.setDragDropMode(QAbstractItemView.DragDrop) listWdg.customContextMenuRequested.connect(self.contextMenu) # dock the form dock = stateAwareQDockWidget(window) dock.setWidget(newWin) dock.setWindowFlags(newWin.windowFlags()) dock.setWindowTitle(newWin.windowTitle()) dock.setAttribute(Qt.WA_DeleteOnClose) self.dock = dock window.addDockWidget(Qt.BottomDockWidgetArea, dock) newWin.setCentralWidget(listWdg) self.listWdg = listWdg self.newWin.setWhatsThis("""<b>Library Viewer</b><br> To <b>open context menu</b> right click on an icon or a selection.<br> To <b>open an image</b> drag it onto the main window.<br> <b>Rating</b> is shown as 0 to 5 stars below each icon.<br> """) # end setWhatsThis
def viewImage(self): """ display full size image in a new window Unused yet """ parent = window newWin = QMainWindow(parent) newWin.setAttribute(Qt.WA_DeleteOnClose) newWin.setContextMenuPolicy(Qt.CustomContextMenu) label = imageLabel(parent=newWin) label.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) label.img = None newWin.setCentralWidget(label) sel = self.listWdg.selectedItems() item = sel[0] filename = item.data(Qt.UserRole)[0] newWin.setWindowTitle(filename) imImg = imImage.loadImageFromFile(filename, createsidecar=False, window=window) label.img = imImg newWin.showMaximized()
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