filterView.py 2.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
from PyQt5 import QtGui, QtWidgets, QtCore


class FilterView(QtWidgets.QGraphicsView):

    def __init__(self):
        super(FilterView, self).__init__()
        self.setWindowTitle('FilterView')

        scene = QtWidgets.QGraphicsScene(self)
        scene.setItemIndexMethod(QtWidgets.QGraphicsScene.NoIndex)
        scene.setBackgroundBrush(QtCore.Qt.darkGray)
        self.setScene(scene)
        self.setCacheMode(QtWidgets.QGraphicsView.CacheBackground)
        self.setViewportUpdateMode(QtWidgets.QGraphicsView.BoundingRectViewportUpdate)
        self.setRenderHint(QtGui.QPainter.Antialiasing)

        self.filter = FilterGraphItem()
        self.scene().addItem(self.filter)
        self.measuringBoxes: list = []

    def update_measure_boxes(self, viewItems: list) -> None:
        self._remove_measure_boxes()
        for item in viewItems:
            self.measuringBoxes.append(item)
            self.scene().addItem(item)

    def _remove_measure_boxes(self) -> None:
        for item in self.measuringBoxes:
            self.scene().removeItem(item)
        self.measuringBoxes = []


class FilterGraphItem(QtWidgets.QGraphicsItem):
    """
    The Graphical Representation of the actual filter
    """
    def __init__(self, filterWidth:float=500, filterHeight:float=500):
        super(FilterGraphItem, self).__init__()
        self.width = filterWidth
        self.height = filterHeight
        self.setPos(0, 0)
        self.rect = QtCore.QRectF(0, 0, self.width, self.height)

    def boundingRect(self) -> QtCore.QRectF:
        return self.rect

    def paint(self, painter: QtGui.QPainter, option, widget) -> None:
        painter.setPen(QtCore.Qt.black)
        painter.setBrush(QtCore.Qt.white)
        painter.drawRect(self.rect)

        painter.setPen(QtCore.Qt.darkGray)
        painter.setBrush(QtCore.Qt.lightGray)
        painter.drawEllipse(self.rect)


class MeasureBoxGraphItem(QtWidgets.QGraphicsItem):
    """
    Displays a box in which particles will be measured
    """
    def __init__(self, posX:float=50, posY:float=50, width:float=50, height:float=50) -> None:
        super(MeasureBoxGraphItem, self).__init__()
        self.posX: float = posX
        self.posY: float = posY
        self.height: float = height
        self.width: float = width
        self.setPos(posX, posY)
        self.rect = QtCore.QRectF(0, 0, self.width, self.height)
        self.setToolTip(f'x0: {round(self.posX)}, y0: {round(self.posY)}, \n'
                        f'x1: {round(self.posX + self.width)}, y1: {round(self.posY + self.height)}')

    def boundingRect(self) -> QtCore.QRectF:
        return self.rect

    def paint(self, painter, option, widget) -> None:
        painter.setBrush(QtCore.Qt.green)
        painter.setPen(QtCore.Qt.darkGreen)
        painter.drawRects(self.rect)


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    filterView = FilterView()
    filterView.show()
    ret = app.exec_()