viewitems.py 7.93 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
# -*- coding: utf-8 -*-
"""
GEPARD - Gepard-Enabled PARticle Detection
Copyright (C) 2018  Lars Bittrich and Josef Brandt, Leibniz-Institut für 
Polymerforschung Dresden e. V. <bittrich-lars@ipfdd.de>    

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 <https://www.gnu.org/licenses/>.
"""
import numpy as np
from PyQt5 import QtCore, QtWidgets, QtGui

class SegementationContours(QtWidgets.QGraphicsItem):
    def __init__(self, contours=[], pos=(0,0)):
        super().__init__()
        self.setPos(pos[0], pos[1])
        self.setAcceptedMouseButtons(QtCore.Qt.NoButton)
        self.brect = QtCore.QRectF(0,0,1,1)
        self.resetContours(contours)
        
    def boundingRect(self):
        return self.brect
    
    def resetContours(self, contours):
        cp = []
        x0 = None
        for c in contours:
            polygon = QtGui.QPolygonF()
            if x0 is None:
                x0 = c[:,0,0].min()
                x1 = c[:,0,0].max()
                y0 = c[:,0,1].min()
                y1 = c[:,0,1].max()
            else:
                x0 = min(x0, c[:,0,0].min())
                x1 = max(x1, c[:,0,0].max())
                y0 = min(y0, c[:,0,1].min())
                y1 = max(y1, c[:,0,1].max())
            for ci in c:
                polygon.append(QtCore.QPointF(ci[0,0],ci[0,1]))
            cp.append(polygon)
        if x0 is None:
            self.brect = QtCore.QRectF(0,0,1,1)
        else:
            self.brect.setCoords(x0,y0,x1,y1)
        self.contours = cp
        self.update()
    
    def paint(self, painter, option, widget):
        painter.setPen(QtCore.Qt.green)
        for c in self.contours:
            painter.drawPolygon(c)
        

class FitPosIndicator(QtWidgets.QGraphicsItem):
    indicatorSize = 80
    def __init__(self, number, pos=(0,0)):
        super().__init__()
        self.setPos(pos[0], pos[1])
        self.number = number
        self.setAcceptedMouseButtons(QtCore.Qt.NoButton)
    
    def boundingRect(self):
        return QtCore.QRectF(-self.indicatorSize-1,-self.indicatorSize-1,
                             2*self.indicatorSize+2,2*self.indicatorSize+2)
    
    def paint(self, painter, option, widget):
        painter.setPen(QtCore.Qt.green)
        painter.setBrush(QtGui.QColor(250,250,0,150))
        rect = QtCore.QRectF(-self.indicatorSize,-self.indicatorSize,
                             2*self.indicatorSize,2*self.indicatorSize)
        font = painter.font()
        font.setPointSize(40)
        painter.setFont(font)
        painter.drawText(rect, QtCore.Qt.AlignCenter, str(self.number))
        painter.drawEllipse(rect)
        
    def shape(self):
        path = QtGui.QPainterPath()
        path.addEllipse(self.boundingRect())
        return path

class RamanScanIndicator(QtWidgets.QGraphicsItem):
    def __init__(self, view, number, radius, pos=(0,0)):
        super().__init__()
        self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable)
        self.view = view
        self.number = number
        self.radius = radius
        self.highlight = False
        self.setPos(pos[0], pos[1])
        
    def setHighLight(self, highlight):
        if highlight!=self.highlight:
            self.highlight = highlight
            self.update()
    
    def boundingRect(self):
        return QtCore.QRectF(-self.radius-1,-self.radius-1,
                             2*self.radius+2,2*self.radius+2)
        
    def shape(self):
        path = QtGui.QPainterPath()
        path.addEllipse(self.boundingRect())
        return path
    
    def paint(self, painter, option, widget):
        if self.highlight:
            painter.setPen(QtCore.Qt.red)
            painter.setBrush(QtGui.QColor(100,250,100,150))
        else:
            painter.setPen(QtCore.Qt.green)
            painter.setBrush(QtGui.QColor(50,50,250,150))
        rect = QtCore.QRectF(-self.radius, -self.radius, 2*self.radius, 2*self.radius)
        painter.drawEllipse(rect)
        font = painter.font()
        font.setPointSize(10)
        painter.setFont(font)
        painter.drawText(rect, QtCore.Qt.AlignCenter, str(self.number))
        
    def mousePressEvent(self, event):
        p = event.pos()
        x, y = p.x(), p.y()
        r = np.sqrt(x**2+y**2)
        print(self.number, r)
        if r<self.radius:
            self.view.selectRamanScanPoint(self.number, r)
        super().mouseReleaseEvent(event)

    
class ScanIndicator(QtWidgets.QGraphicsItem):
    def __init__(self, number, wx, wy, pos=(0,0)):
        super().__init__()
        self.number = number
        self.wx = wx
        self.wy = wy
        self.setPos(pos[0], pos[1])
        self.setAcceptedMouseButtons(QtCore.Qt.NoButton)
    
    def boundingRect(self):
        return QtCore.QRectF(-self.wx/2-1,-self.wy/2-1,
                             self.wx+2,self.wy+2)
    
    def paint(self, painter, option, widget):
        painter.setPen(QtCore.Qt.blue)
        painter.setBrush(QtGui.QColor(0,0,250,80))
        rect = QtCore.QRectF(-self.wx/2, -self.wy/2, self.wx, self.wy)
        font = painter.font()
        font.setPointSize(40)
        painter.setFont(font)
        painter.drawText(rect, QtCore.Qt.AlignCenter, str(self.number))
        painter.drawRect(rect)
            
class Edge(QtWidgets.QGraphicsItem):
    
    def __init__(self, n1, n2):
        self.n1 = n1
        self.n2 = n2
        super().__init__()
        self.setPos(0, 0)
        n1.edges.append(self)
        n2.edges.append(self)
        self.setAcceptedMouseButtons(QtCore.Qt.NoButton)
    
    def boundingRect(self):
        return QtCore.QRectF(self.n1.pos(), self.n2.pos()).normalized().adjusted(-1,-1,1,1)
    
    def paint(self, painter, option, widget):
        painter.setPen(QtCore.Qt.green)
        painter.drawLine(self.n1.pos(), self.n2.pos()) 

class Node(QtWidgets.QGraphicsItem):
    def __init__(self, point, view):
        self.view = view
        self.point = point
        self.edges = []
        self.rectSize = 60
        super().__init__()
        self.setPos(self.point[0], self.point[1])
        self.rectSize = 2*self.rectSize
        self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable)
        self.setFlag(QtWidgets.QGraphicsItem.ItemSendsGeometryChanges)
        self.setCacheMode(QtWidgets.QGraphicsItem.DeviceCoordinateCache)
        self.poschanged = False
        
    def boundingRect(self):
        return QtCore.QRectF(-self.rectSize-1,-self.rectSize-1,2*self.rectSize+2,2*self.rectSize+2)
        
    def shape(self):
        path = QtGui.QPainterPath()
        path.addEllipse(self.boundingRect())
        return path
    
    def paint(self, painter, option, widget):
        painter.setBrush(QtGui.QColor(255,0,0,80))
        painter.setPen(QtCore.Qt.red)
        painter.drawLine(0,-self.rectSize/2.,0,self.rectSize/2)
        painter.drawLine(-self.rectSize/2.,0,self.rectSize/2,0)
        rect = QtCore.QRectF(-self.rectSize,-self.rectSize,2*self.rectSize,2*self.rectSize)
        painter.drawEllipse(rect)        

    def itemChange(self, change, value):
        if change == QtWidgets.QGraphicsItem.ItemPositionHasChanged:
            for edge in self.edges:
                edge.prepareGeometryChange()
            self.poschanged = True

        return super(Node, self).itemChange(change, value)

    def mousePressEvent(self, event):
        self.poschanged = False
        super().mousePressEvent(event)

    def mouseReleaseEvent(self, event):
        if self.poschanged:
            self.view.resetScanPositions()
        super().mouseReleaseEvent(event)