Beispiel #1
0
class PreviewArea(QtWidgets.QWidget):
    def __init__(self, parentQScrollArea, beamer_screen):
        QtWidgets.QWidget.__init__(self)
        
        self.parentQScrollArea = parentQScrollArea
        
        self.layout = QtWidgets.QGridLayout()
        self.setLayout(self.layout)
        
        self.current_id = None
        
        self.setStyleSheet('QWidget { background-color: yellow; }')
        
        self.config = Config()
        #self.screen = self.config.getBeamerScreen()
        self.screen = beamer_screen
        self.column_count = self.config.getPreviewColumnCount()
        
        #self.aspect_ratio = self.getBeamerWindowAspect()
        
        self.canvas = None
        
        #self.alertAlreadyShown = False
        
#        print("##########")
#        print(self.width(), self.height())
#        print(self.sizeHint())
#        print(self.size())
#        print("##########")

#        images_list = ["./testimages/facepalm0.jpg",
#                       "./testimages/facepalm00.jpg",
#                       "./testimages/facepalm1.jpg",
#                       "./testimages/facepalm2.jpg",
#                       "./testimages/facepalm3.jpg"]
        #self.showPreviewWithoutWindow()
        
        #self.generatePreviewsForImagePathList(images_list)
        
    #def showPreviewWithoutWindow(self):
        #label = ClickLabel(self)
        #label.resize(QtCore.QSize(PREVIEW_WIDTH, PREVIEW_HEIGHT))
        ##label.setGeometry(QtCore.QRect(0, 0, 400, 400))
        
        #label.setStyleSheet('QLabel { background-color: red; }')
        
        #self.canvas = Canvas()
        ##self.canvas.showImage("./testimages/facepalm0.jpg")
        #pixmap = QtGui.QPixmap("./testimages/facepalm0.jpg")
        
        #self.canvas.showImage(pixmap)
        ##self.canvas.routeCanvasToScreen()
        #preview_pixmap = QtGui.QPixmap(self.generatePreview())
        #pixmap_scaled = preview_pixmap.scaled(label.size(), QtCore.Qt.KeepAspectRatio)
        
        #label.setPixmap(pixmap_scaled)
        #label.clicked.connect(lambda: self.imageToCanvas(pixmap))
        
        #self.layout.addWidget(label, 0, 0)
        
        #self.destroyBeamerWindow()
    
    def generatePreviewsForImagePathList(self, image_path_list):
        self.images_list = image_path_list
        
        preview_pixmap_list = []
        
        for image in image_path_list:
            pixmap = QtGui.QPixmap(image)
            
            self.canvas = Canvas(self.screen)
            self.canvas.showImage(pixmap)
            
            preview_pixmap = QtGui.QPixmap()
            preview_pixmap = QtGui.QPixmap(self.generatePreview())
            preview_pixmap_list.append(preview_pixmap)
            
        self.showPreviews(preview_pixmap_list, image_path_list)
        
    def showPreviews(self, preview_pixmap_list, images_list):
        label_list = []
        for i, pixmap in enumerate(preview_pixmap_list):
            label = ClickLabel(self)
            
            #preview_width = PREVIEW_WIDTH
            #preview_width = self.parentQScrollArea.viewportSize().width()
            preview_width = self.parentQScrollArea.viewport().width()
            preview_height = PREVIEW_HEIGHT
            label.resize(QtCore.QSize(preview_width, preview_height))
            
            pixmap_scaled = pixmap.scaled(label.size(), QtCore.Qt.KeepAspectRatio)
            
            label.setPixmap(pixmap_scaled)
            label.setPixmapPath(images_list[i])
            label.name_id = i
            original_pixmap = QtGui.QPixmap(images_list[i])
            #label.clicked.connect(lambda: self.imageToCanvas(original_pixmap))
            label.clicked.connect(self.imageToCanvas)
            
            label_list.append(label)
            
        for i, label in enumerate(label_list):
            x = int(i / self.column_count)
            y = i % self.column_count
            self.layout.addWidget(label, x, y)
    
    def generatePreview(self):
        return self.canvas.getPreviewImage()
    
    def imageToCanvas(self, id):
        self.current_id = id
        print("CURRENT ID: ", id)
        
        pixmap = QtGui.QPixmap(self.images_list[id])
    
        self.canvas = Canvas(self.screen)
        #self.canvas.setScreen(self.config.getBeamerScreen())
        self.canvas.showImage(pixmap)
        self.canvas.routeCanvasToScreen()
        
        self.connectCanvas()
        
    def destroyBeamerWindow(self):
        self.canvas.destroy(True, True)
        self.canvas = None
        
    def connectCanvas(self):
        self.canvas.beam_prev.connect(self.beamPrev)
        self.canvas.beam_next.connect(self.beamNext)
    
    def closeEvent(self, event):
        self.destroyBeamerWindow()
        
    def beamNext(self):
        next_id = self.current_id + 1
        if next_id >= len(self.images_list):
            next_id = len(self.images_list)-1
        self.imageToCanvas(next_id)
        
    def beamPrev(self):
        next_id = self.current_id - 1
        if next_id < 0:
            next_id = 0
        self.imageToCanvas(next_id)
        
    def keyPressEvent(self, e):
        if e.key() == QtCore.Qt.Key_Escape:
            self.destroyBeamerWindow()
        elif e.key() == QtCore.Qt.Key_Left:
            self.beamPrev()
        elif e.key() == QtCore.Qt.Key_Right:
            self.beamNext()