filterView.py 7.2 KB
Newer Older
1
from PyQt5 import QtGui, QtWidgets, QtCore
2 3 4 5 6
import sys
sys.path.append("C://Users//xbrjos//Desktop//Python")
import gepard
from gepard import dataset
import helpers
7
import numpy as np
8 9 10 11 12 13 14 15


class FilterView(QtWidgets.QGraphicsView):

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

16 17
        self.dataset: dataset.DataSet = None

18 19 20 21 22 23 24
        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)
25 26

        self.drag = None
27

28
        self.filter: FilterGraphItem = FilterGraphItem()
29 30
        self.scene().addItem(self.filter)
        self.measuringBoxes: list = []
31
        self.contourItems: list = []
32

33
    def update_measure_boxes(self, topLefts: list, boxSize: float) -> None:
34
        self._remove_measure_boxes()
35
        for x, y in topLefts:
36
            newBox: MeasureBoxGraphItem = MeasureBoxGraphItem(x, y, boxSize, boxSize)
37 38 39
            self.measuringBoxes.append(newBox)
            self.scene().addItem(newBox)

40 41 42 43 44
    def _remove_measure_boxes(self) -> None:
        for item in self.measuringBoxes:
            self.scene().removeItem(item)
        self.measuringBoxes = []

45 46 47 48
    def update_from_dataset(self, dset: dataset.DataSet) -> None:
        self.dataset = dset
        self._update_particle_contours()
        self._fit_to_window()
49 50 51 52 53 54 55 56 57 58 59 60 61 62

    def _update_particle_contours(self) -> None:
        self._remove_particle_contours()
        if self.dataset is not None:
            for particle in self.dataset.particleContainer.particles:
                newContour: ParticleContour = ParticleContour(particle.contour)
                self.scene().addItem(newContour)
                self.contourItems.append(newContour)

    def _remove_particle_contours(self) -> None:
        for cntItem in self.contourItems:
            self.scene().removeItem(cntItem)
        self.contourItems = []

63 64 65 66
    def update_measured_particles(self, measuredParticles: list) -> None:
        measuredIndices: list = [particle.index for particle in measuredParticles]
        for index, contourItem in enumerate(self.contourItems):
            contourItem.isMeasured = (index in measuredIndices)
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

    def wheelEvent(self, event: QtGui.QWheelEvent) -> None:
        factor: float = 1.01 ** (event.angleDelta().y() / 8)
        newScale: float = self.filter.scale() * factor
        self.scale(newScale, newScale)

    def mousePressEvent(self, event):
        if event.button() == QtCore.Qt.MiddleButton:
            self.drag = event.pos()

    def mouseMoveEvent(self, event):
        if self.drag is not None:
            p0 = event.pos()
            move = self.drag - p0
            self.horizontalScrollBar().setValue(move.x() + self.horizontalScrollBar().value())
            self.verticalScrollBar().setValue(move.y() + self.verticalScrollBar().value())

            self.drag = p0

    def mouseReleaseEvent(self, event: QtGui.QMouseEvent) -> None:
        self.drag = None

    def _fit_to_window(self) -> None:
        brect = self.scene().itemsBoundingRect()
        self.fitInView(0, 0, brect.width(), brect.height(), QtCore.Qt.KeepAspectRatio)

93 94 95 96 97

class FilterGraphItem(QtWidgets.QGraphicsItem):
    """
    The Graphical Representation of the actual filter
    """
98
    def __init__(self, filterWidth: float = 500, filterHeight: float = 500, diameter: float = 500):
99
        super(FilterGraphItem, self).__init__()
100 101 102 103
        self.width: float = filterWidth
        self.height: float = filterHeight
        self.diameter: float = diameter
        self.circleOffset: tuple = (0, 0)
104 105
        self.setPos(0, 0)
        self.rect = QtCore.QRectF(0, 0, self.width, self.height)
106
        self.circleRect = QtCore.QRectF(self.circleOffset[0], self.circleOffset[1], self.diameter, self.diameter)
107 108 109 110

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

111 112 113 114 115 116 117 118 119
    def update_filterSize(self, width: float, height: float, diameter: float, offset: tuple) -> None:
        self.width = width
        self.height = height
        self.rect = QtCore.QRectF(0, 0, self.width, self.height)

        self.diameter = diameter
        self.circleOffset = offset
        self.circleRect = QtCore.QRectF(self.circleOffset[0], self.circleOffset[1], self.diameter, self.diameter)

120 121 122 123 124 125 126
    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)
127
        painter.drawEllipse(self.circleRect)
128 129 130 131 132 133


class MeasureBoxGraphItem(QtWidgets.QGraphicsItem):
    """
    Displays a box in which particles will be measured
    """
134
    def __init__(self, posX: float = 50, posY: float = 50, width: float = 50, height: float = 50) -> None:
135
        super(MeasureBoxGraphItem, self).__init__()
136
        self.setZValue(5)
137 138 139 140 141 142 143 144 145 146 147 148 149
        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:
150
        painter.setBrush(QtGui.QColor(0, 255, 0, 180))
151
        painter.setPen(QtCore.Qt.darkGreen)
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
        painter.drawRects(self.rect)


class ParticleContour(QtWidgets.QGraphicsItem):
    def __init__(self, contourData, pos=(0, 0)) -> None:
        super(ParticleContour, self).__init__()
        self.setZValue(1)
        self.setPos(pos[0], pos[1])
        self.brect: QtCore.QRectF = QtCore.QRectF(0, 0, 1, 1)

        self.isMeasured: bool = False  # Wether the particle overlaps with a measuring box or nt
        self.contourData = contourData
        self.polygon = None

        self.getBrectAndPolygon()

    def getBrectAndPolygon(self) -> None:
        """
        Calculates the bounding rect (needed for drawing the QGraphicsView) and converts the contourdata to a polygon.
        :return:
        """
        self.polygon = QtGui.QPolygonF()
        x0 = self.contourData[:, 0, 0].min()
        x1 = self.contourData[:, 0, 0].max()
        y0 = self.contourData[:, 0, 1].min()
        y1 = self.contourData[:, 0, 1].max()
        for point in self.contourData:
            self.polygon.append(QtCore.QPointF(point[0, 0], point[0, 1]))

        self.brect.setCoords(x0, y0, x1, y1)

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

    def paint(self, painter, option, widget) -> None:
        if self.polygon is not None:
            if self.isMeasured:
                painter.setPen(QtCore.Qt.darkRed)
                painter.setBrush(QtCore.Qt.red)
            else:
                painter.setPen(QtCore.Qt.darkCyan)
                painter.setBrush(QtCore.Qt.cyan)

            painter.drawPolygon(self.polygon)