Ejemplo n.º 1
0
    async def analyzeImage(self, ipfsop):
        statInfo = StatInfo(self.item.stat)

        if statInfo.valid:
            size = statInfo.dataSize

            if isinstance(size, int):
                # don't scan anything larger than 4Mb
                if statInfo.dataLargerThan(megabytes(4)):
                    log.debug('{path}: Image too large, not scanning')
                    return
        else:
            # Don't trust this one
            log.debug('No object info for image, bailing out')
            return

        try:
            data = await ipfsop.catObject(self.item.path)

            if data is None:
                return

            icon = getIconFromImageData(data)
            if icon:
                self.updateIcon(icon)

            # Decode the QR codes in the image if there's any
            qrDecoder = IPFSQrDecoder()
            if not qrDecoder:
                return

            urls = qrDecoder.decode(data)
            if isinstance(urls, list):
                # Display the QR codes in a separate menu
                menu = qrCodesMenuBuilder(urls, self.app.resourceOpener,
                                          parent=self)
                self.menu.addSeparator()
                self.menu.addMenu(menu)

        except aioipfs.APIError:
            pass
Ejemplo n.º 2
0
    def __init__(self, parent=None):
        super(ImageView, self).__init__(parent)

        self.app = QApplication.instance()
        self.image = QImage()

        self.clip = QMovie(self)
        self.clipFirstPixmap = None
        self.clipCurrentPixmap = None

        self.currentImgPath = None
        self.setObjectName('ImageView')

        self.qrDecoder = IPFSQrDecoder()
        self.labelImage = QLabel()
        self.setAlignment(Qt.AlignCenter)
        self.labelImage.setAlignment(Qt.AlignCenter)

        self.setBackgroundRole(QPalette.Dark)
        self.labelImage.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
        self.labelImage.setScaledContents(True)
        self.scaleFactor = 1.0
        self.setWidget(self.labelImage)
Ejemplo n.º 3
0
class ImageView(QScrollArea):
    imageLoaded = pyqtSignal(IPFSPath, MIMEType)

    qrCodesPresent = pyqtSignal(list)

    def __init__(self, parent=None):
        super(ImageView, self).__init__(parent)

        self.app = QApplication.instance()
        self.image = QImage()

        self.clip = QMovie(self)
        self.clipFirstPixmap = None
        self.clipCurrentPixmap = None

        self.currentImgPath = None
        self.setObjectName('ImageView')

        self.qrDecoder = IPFSQrDecoder()
        self.labelImage = QLabel()
        self.setAlignment(Qt.AlignCenter)
        self.labelImage.setAlignment(Qt.AlignCenter)

        self.setBackgroundRole(QPalette.Dark)
        self.labelImage.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
        self.labelImage.setScaledContents(True)
        self.scaleFactor = 1.0
        self.setWidget(self.labelImage)

    def wheelEvent(self, event):
        """
        Zoom in/out on the image on mouse wheel events
        """

        degrees = event.angleDelta()

        if degrees.y() == 0:
            return

        yDeg = max(min(60, abs(degrees.y())), 240)

        if degrees.y() > 0:
            factor = 1 + ((yDeg / 30) * 0.05)
            self.zoomIn(factor=factor)
        elif degrees.y() < 0:
            factor = 1 - ((yDeg / 30) * 0.05)
            self.zoomOut(factor=factor)

        event.accept()

    def pinImage(self):
        if self.currentImgPath and self.currentImgPath.valid:
            ensure(self.pinImageFromPath(self.currentImgPath))

    @ipfsOp
    async def pinImageFromPath(self, ipfsop, imgPath):
        await ipfsop.ctx.pin(str(imgPath))

    def fitWindow(self, fit):
        self.setWidgetResizable(fit)

        if not fit:
            self.scaleFactor = 1.0
            self.resizePixmap()

    def zoomOut(self, factor=0.75):
        self.scaleFactor *= factor
        self.resizePixmap()

    def zoomIn(self, factor=1.25):
        self.scaleFactor *= factor
        self.resizePixmap()

    def zoomInClicked(self):
        self.zoomIn()

    def zoomOutClicked(self):
        self.zoomOut()

    def resizePixmap(self):
        if self.labelImage.movie() and self.clipFirstPixmap:
            self.labelImage.resize(self.scaleFactor *
                                   self.clipFirstPixmap.size())
            return

        pixmap = self.labelImage.pixmap()

        if pixmap == 0 or pixmap is None:
            return

        self.labelImage.resize(self.scaleFactor * pixmap.size())

    @ipfsOp
    async def showImage(self, ipfsop, imgPath, fromBlock=False, timeout=10):
        try:
            if fromBlock is True:
                imgData = await ipfsop.waitFor(
                    ipfsop.client.block.get(imgPath), timeout)
            else:
                imgData = await ipfsop.waitFor(ipfsop.catObject(imgPath),
                                               timeout)

            if not imgData:
                raise Exception('Failed to load image')

            mimeType, statInfo = await self.app.rscAnalyzer(IPFSPath(imgPath))

            if mimeType is None:
                raise Exception('Failed to load image')

            stat = StatInfo(statInfo)

            if mimeType.isAnimation:
                fp = self.app.tempDir.filePath(stat.cid)
                tmpFile = QFile(fp)

                if not tmpFile.exists() and tmpFile.open(QFile.ReadWrite):
                    tmpFile.write(imgData)
                    tmpFile.close()

                self.clip.stop()
                self.clip.setFileName(fp)
                self.labelImage.setMovie(self.clip)
                self.clip.frameChanged.connect(self.onMovieFrameChanged)
                self.clip.start()
                self.labelImage.adjustSize()
            else:
                img = QImage()
                img.loadFromData(imgData)
                self.image = img

                self.labelImage.setPixmap(QPixmap.fromImage(self.image))
                self.labelImage.adjustSize()
                self.resizePixmap()

                if self.qrDecoder:
                    # See if we have any QR codes in the image
                    urls = self.qrDecoder.decode(imgData)
                    if urls:
                        self.qrCodesPresent.emit(urls)

            self.labelImage.adjustSize()
            self.currentImgPath = IPFSPath(imgPath)
            self.imageLoaded.emit(self.currentImgPath, mimeType)

        except Exception:
            logUser.debug('Failed to load image: {path}'.format(path=imgPath))
            messageBox(iImageCannotLoad(imgPath))

    def onMovieFrameChanged(self, num):
        self.clipCurrentPixmap = self.clip.currentPixmap()

        if self.clipFirstPixmap is None:
            self.clipFirstPixmap = self.clipCurrentPixmap

    def onMovieError(self, err):
        logUser.info(f'{self.clip.fileName()}: {self.clip.lastErrorString()}')
Ejemplo n.º 4
0
 def __init__(self, parent=None):
     super().__init__(parent)
     self.app = QApplication.instance()
     self.qrDecoder = IPFSQrDecoder()