ocr_view.md

2021/04/06 categories:Code| tags:Code|

記事に戻る

# -*- coding: utf-8 -*-
from PyQt5 import QtWidgets, QtCore, QtGui

class OCRView(QtWidgets.QWidget):
    mouseLeftReleasedSignal = QtCore.pyqtSignal(QtCore.QRectF)
    def __init__(self, parent=None):
        super(OCRView, self).__init__(parent)
        self.drawRectFlag = False
        self.scene = QtWidgets.QGraphicsScene()
        self.graphicsView = GraphicsView(self)
        self.graphicsView.setScene(self.scene)
        self.graphicsView.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        self.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)

        self.setLayout( QtWidgets.QVBoxLayout(self) )
        self.layout().setContentsMargins(0, 0, 0, 0)
        self.layout().addWidget(self.graphicsView)

        self.graphicsView.mouseLeftReleased.connect(self.mouseLeftReleased)

    def appendRect(self, rect, pen):
        rectItem = QtWidgets.QGraphicsRectItem(rect)
        rectItem.setPen(pen)
        self.scene.addItem(rectItem)

    def mouseLeftReleased(self, rect):
        if self.drawRectFlag:
            self.mouseLeftReleasedSignal.emit(rect)

    def removeRect(self, rect):
        for rectItem in self.scene.items():
            if type(rectItem) is QtWidgets.QGraphicsRectItem:
                if rect == rectItem.rect():
                    self.scene.removeItem(rectItem)

    def removeAllRect(self):
        for rectItem in self.scene.items():
            if type(rectItem) is QtWidgets.QGraphicsRectItem:
                self.scene.removeItem(rectItem)

    def setDrawRectFlag(self, flag):
        self.drawRectFlag = flag

    def addPixmap(self, pixmap):
        self.scene.addPixmap(pixmap)

    def clear(self):
        self.scene.clear()

class GraphicsView(QtWidgets.QGraphicsView):
    mouseLeftReleased = QtCore.pyqtSignal(QtCore.QRectF)
    def __init__(self, parent=None):
        super(GraphicsView, self).__init__(parent)
        self.numScheduledScalings = 0
        self.rectF = QtCore.QRectF(0.0, 0.0, 0.0, 0.0)
        self.setBackgroundBrush(QtGui.QColor(210, 210, 210))

    def animation_finished(self):
        if self.numScheduledScalings > 0:
            self.numScheduledScalings -= 1
        else:
            self.numScheduledScalings += 1

    def wheelEvent(self, event):
        numDegrees = event.angleDelta().y() / 8
        numSteps = numDegrees / 15
        self.numScheduledScalings += numSteps
        if self.numScheduledScalings * numSteps < 0:
            self.numScheduledScalings = numSteps
        anim = QtCore.QTimeLine(350, self)
        anim.setUpdateInterval(20)
        anim.valueChanged.connect(self.scaling_time)
        anim.finished.connect(self.animation_finished)
        anim.start()

    def mousePressEvent(self, event):
        if event.button() == QtCore.Qt.MidButton:
            self.setDragMode(QtWidgets.QGraphicsView.ScrollHandDrag)

            event = QtGui.QMouseEvent(
                QtCore.QEvent.GraphicsSceneDragMove, 
                event.pos(), 
                QtCore.Qt.MouseButton.LeftButton, 
                QtCore.Qt.MouseButton.LeftButton, 
                QtCore.Qt.KeyboardModifier.NoModifier
            )

        elif event.button() == QtCore.Qt.LeftButton:
            self.setDragMode(QtWidgets.QGraphicsView.RubberBandDrag)
            point = self.mapToScene( event.pos() )
            self.rectF = QtCore.QRectF(point, point)

        super().mousePressEvent(event)
        
    def mouseReleaseEvent(self, event):
        super().mouseReleaseEvent(event)

        self.setDragMode(QtWidgets.QGraphicsView.NoDrag)

        if event.button() == QtCore.Qt.LeftButton:
            p2 = self.mapToScene( event.pos() )
            self.rectF.setBottomRight(p2)
            self.mouseLeftReleased.emit(self.rectF)

    def scaling_time(self, x):
        factor = 1.0 + float(self.numScheduledScalings) / 300.0
        self.scale(factor, factor)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    view = OCRView()
    view.setPixmap( QtGui.QPixmap('table1.png') )
    view.resize(600, 300)
    view.show()
    app.exec()

Share post

Related Posts

Comments

comments powered by Disqus