""" GEPARD - Gepard-Enabled PARticle Detection Copyright (C) 2018 Lars Bittrich and Josef Brandt, Leibniz-Institut für Polymerforschung Dresden e. V. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program, see COPYING. If not, see . """ from PyQt5 import QtWidgets, QtCore, QtGui from copy import deepcopy from typing import TYPE_CHECKING import numpy as np from ...analysis.particleEditor import ParticleContextMenu from ...analysis.particleCharacterization import FTIRAperture if TYPE_CHECKING: from ...sampleview import SampleView class SeedPoint(QtWidgets.QGraphicsItem): def __init__(self, radius, seedtype, pos=(0, 0)): super().__init__() self.setPos(pos[0], pos[1]) self.seedtype = seedtype self.radius = radius def boundingRect(self): return QtCore.QRectF(-self.radius - 1, -self.radius - 1, 2*self.radius + 2, 2*self.radius + 2) def paint(self, painter, option, widget): if self.seedtype == 'add': painter.setPen(QtCore.Qt.white) painter.setBrush(QtCore.Qt.white) elif self.seedtype == 'remove': painter.setPen(QtCore.Qt.magenta) painter.setBrush(QtCore.Qt.magenta) rect = QtCore.QRectF(-self.radius, -self.radius, 2*self.radius, 2*self.radius) painter.drawEllipse(rect) class SegmentationContour(QtWidgets.QGraphicsItem): def __init__(self, viewparent: 'SampleView', contourData, pos: tuple =(0, 0), particle_uid: int = 0): super().__init__() self.viewparent: 'SampleView' = viewparent self.particleUID: int = particle_uid self.setZValue(1) self.setPos(pos[0], pos[1]) self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable) self.contextMenu = None self.brect = QtCore.QRectF(0, 0, 1, 1) self.contourData = contourData self.polygon = None self.hidden: bool = False self.transparent: bool = False self.color = QtGui.QColor(50, 255, 50, 200) self.particleIndex = None self.isSelected = False self.getBrectAndPolygon() def getBrectAndPolygon(self): """ Calculates the bounding rect (needed for drawing the QGraphicsView) and converts the contourdata to a polygon. :return: """ 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: polygon.append(QtCore.QPointF(point[0, 0], point[0, 1])) self.brect.setCoords(x0, y0, x1, y1) self.polygon = polygon def boundingRect(self): return self.brect def setIndex(self, index): self.particleIndex = index def setIndex(self, index): self.particleIndex = index def setColor(self, color: QtGui.QColor): """ sets color of contour :param QtGui.QColor color: :return: """ self.color = color def getColor(self): """ returns color of contour :return QtGui.QColor: """ return self.color def setHidden(self, hidden): """ sets hidden state of contour """ self.hidden = hidden def getHidden(self): """ returns hidden state of contour :return bool: """ return self.hidden def paint(self, painter, option, widget): if self.polygon is not None: painter.setPen(QtCore.Qt.green) if self.isSelected: alpha = 200 if not self.hidden: painter.setBrush(QtGui.QColor(200, 200, 200, alpha)) painter.setPen(QtCore.Qt.white) else: painter.setPen(QtGui.QColor(int(self.color.red() * 0.7), int(self.color.green() * 0.7), int(self.color.blue() * 0.7))) if not self.hidden: alpha = 128 if self.transparent else 255 painter.setBrush(QtGui.QColor(self.color.red(), self.color.green(), self.color.blue(), alpha)) painter.drawPolygon(self.polygon) def contextMenuEvent(self, event): if self.isSelected: self.contextMenu = ParticleContextMenu(self.viewparent) self.viewparent.particleEditor.connectToSignals(self.contextMenu) self.contextMenu.executeAtScreenPos(event.screenPos()) class FTIRApertureIndicator(QtWidgets.QGraphicsItem): def __init__(self, aperture: FTIRAperture): super(FTIRApertureIndicator, self).__init__() self.aperture = aperture self.hidden: bool = False self.transparent: bool = False self.setZValue(2) self.setPos(aperture.centerX, aperture.centerY) self.polygon: QtGui.QPolygonF = QtGui.QPolygonF() self.getBrectAndPolygon() def getBrectAndPolygon(self) -> None: """ Calculates the bounding rect (needed for drawing the QGraphicsView) and converts the aperture to a polygon. :return: """ meanX: float = self.aperture.width / 2 meanY: float = self.aperture.height / 2 points: np.ndarray = np.array([[-meanX, meanY], [meanX, meanY], [meanX, -meanY], [-meanX, -meanY]]) angleRad: np.ndarray = np.deg2rad(self.aperture.angle) sin, cos = np.sin(angleRad), np.cos(angleRad) for i in range(points.shape[0]): point: np.ndarray = deepcopy(points[i, :]) points[i, 0] = point[0] * cos - point[1] * sin points[i, 1] = point[0] * sin + point[1] * cos self.polygon.append(QtCore.QPointF(points[i, 0], points[i, 1])) def boundingRect(self) -> QtCore.QRectF: return self.polygon.boundingRect() def paint(self, painter, option, widget): if not self.hidden: color: QtGui.QColor = QtGui.QColor(0, 255, 0, 255) if self.transparent: color.setAlpha(128) painter.setPen(color) painter.setBrush(color) painter.setOpacity(0.8) painter.drawPolygon(self.polygon)