viewitems.py 14.5 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
# -*- 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

JosefBrandt's avatar
 
JosefBrandt committed
24 25 26 27 28 29
class SegmentationContour(QtWidgets.QGraphicsItem):
    def __init__(self, parent, contourData, pos=(0,0)):
        super().__init__()
        self.parent = parent
        self.setPos(pos[0], pos[1])
        self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable)
JosefBrandt's avatar
 
JosefBrandt committed
30
        self.setAcceptedMouseButtons(QtCore.Qt.AllButtons)
JosefBrandt's avatar
 
JosefBrandt committed
31
        self.brect = QtCore.QRectF(0,0,1,1)
JosefBrandt's avatar
 
JosefBrandt committed
32
        
JosefBrandt's avatar
 
JosefBrandt committed
33 34
        self.contourData = contourData
        self.polygon = None
JosefBrandt's avatar
 
JosefBrandt committed
35 36 37 38
        
        self.hidden = False
        self.color = QtGui.QColor(50, 255, 50, 200)
        self.particleIndex = None
JosefBrandt's avatar
 
JosefBrandt committed
39 40
        self.isSelected = False
        self.getBrectAndPolygon()
JosefBrandt's avatar
 
JosefBrandt committed
41

JosefBrandt's avatar
 
JosefBrandt committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    def getBrectAndPolygon(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:
            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
    
JosefBrandt's avatar
 
JosefBrandt committed
57 58 59 60 61 62 63 64 65 66 67 68
    def setIndex(self, index):
        self.particleIndex = index
    
    def setColor(self, color):
        self.color = color
        
    def setHidden(self, hidden):
        self.hidden = hidden
        
    def mousePressEvent(self, event):            
        if event.button()==QtCore.Qt.LeftButton:
            print('selected particle index', self.particleIndex)
JosefBrandt's avatar
 
JosefBrandt committed
69 70
#            self.isSelected = True            
#            self.signalEmitter.contourSelected.emit(self.particleIndex)
JosefBrandt's avatar
 
JosefBrandt committed
71
    
JosefBrandt's avatar
 
JosefBrandt committed
72
    def paint(self, painter, option, widget):           
JosefBrandt's avatar
 
JosefBrandt committed
73 74
        self.isSelected = (self.particleIndex in self.parent.selectedContours)
        
JosefBrandt's avatar
 
JosefBrandt committed
75
        if self.polygon is not None and not self.hidden:
JosefBrandt's avatar
 
JosefBrandt committed
76 77 78
            painter.setPen(QtCore.Qt.green)
            if self.isSelected:
                alpha = 200
JosefBrandt's avatar
 
JosefBrandt committed
79
                if not self.hidden:
JosefBrandt's avatar
 
JosefBrandt committed
80 81
                    painter.setBrush(QtGui.QColor(200, 200, 200, alpha))
                painter.setPen(QtCore.Qt.white)
JosefBrandt's avatar
 
JosefBrandt committed
82 83 84 85 86
            else:
                painter.setPen(QtGui.QColor(int(self.color.red()*0.7), int(self.color.green()*0.7), int(self.color.blue()*0.7), self.color.alpha()))
                if not self.hidden:
                    painter.setBrush(self.color)
                
JosefBrandt's avatar
 
JosefBrandt committed
87 88 89
            painter.drawPolygon(self.polygon)


90
class SegmentationContours(QtWidgets.QGraphicsItem):
91
    def __init__(self, parent, contours=[], pos=(0,0)):
92
        super().__init__()
93
        self.parent = parent
94
        self.setPos(pos[0], pos[1])
95 96
        self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable)
        self.setAcceptedMouseButtons(QtCore.Qt.AllButtons)
97 98
        self.brect = QtCore.QRectF(0,0,1,1)
        self.resetContours(contours)
99 100
        self.colorList = []
        self.selectedContours = []
101 102 103 104
        
    def boundingRect(self):
        return self.brect
    
Hackmet's avatar
Hackmet committed
105
    def resetContours(self, contours=[]):
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        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()
    
130
    def paint(self, painter, option, widget):           
131
        painter.setPen(QtCore.Qt.green)
Hackmet's avatar
Hackmet committed
132
        lenColorList = len(self.colorList)
Hackmet's avatar
Hackmet committed
133 134 135 136
        if self.parent.analysiswidget is not None:
            nonePaintMode = self.parent.analysiswidget.noOverlayAct.isChecked()
        else:
            nonePaintMode = False
137 138
        for index, c in enumerate(self.contours):
            if index not in self.selectedContours:
Hackmet's avatar
Hackmet committed
139 140 141 142 143 144
                if lenColorList > 0:
                    color = self.colorList[index]
                    painter.setPen(QtGui.QColor(int(color.red()*0.7), int(color.green()*0.7), int(color.blue()*0.7), color.alpha()))
                else:
                    color = QtCore.Qt.green
                    painter.setPen(color)
Hackmet's avatar
Hackmet committed
145 146
                if not nonePaintMode:
                    painter.setBrush(color)
147 148
                painter.drawPolygon(c)
            else:
Hackmet's avatar
Hackmet committed
149 150 151 152
                if lenColorList > 0:
                    alpha = self.colorList[index].alpha()
                else:
                    alpha = 255
Hackmet's avatar
Hackmet committed
153 154
                if not nonePaintMode:
                    painter.setBrush(QtGui.QColor(200, 200, 200, alpha))
155 156
                painter.setPen(QtCore.Qt.white)
                painter.drawPolygon(c)
157

158 159 160 161 162 163 164 165 166
            
    def mousePressEvent(self, event):            
        if event.button()==QtCore.Qt.LeftButton:
            p = event.pos()
            p = QtCore.QPointF(p.x(), p.y())
            for index, cnt in enumerate(self.contours):
                if cnt.containsPoint(p, QtCore.Qt.OddEvenFill):
                     
                    if event.modifiers()==QtCore.Qt.ShiftModifier:
Hackmet's avatar
Hackmet committed
167 168 169 170
                        if index not in self.selectedContours:
                            self.selectedContours.append(index)
                        else:
                            self.selectedContours.remove(index)
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
                    else:
                        self.selectedContours = [index]
                        
                    self.parent.selectContour(index, centerOn=False)
                    
                    self.update()
                    return
            self.selectedContours = []   #reset selection, if nothing was hit...
            self.update()
    
    def contextMenuEvent(self, event):
        contextMenu = QtWidgets.QMenu("Particle options")

        combineMenu = QtWidgets.QMenu("Combine Particles into")
        
        combineActs = []
        assignments = []
        for index in self.selectedContours:
            partIndex = index
JosefBrandt's avatar
JosefBrandt committed
190
            assignments.append(self.parent.dataset.particleContainer.particles[partIndex].getParticleAssignment())    #TODO: Entangle...
191
        assignments.append("other")
JosefBrandt's avatar
JosefBrandt committed
192
        
193 194 195 196 197
        for assignment in np.unique(np.array(assignments)):
            combineActs.append(combineMenu.addAction(assignment))
        
        reassignActs = []
        reassignMenu = QtWidgets.QMenu("Reassign particle(s) into")
Lars Bittrich's avatar
Lars Bittrich committed
198
        for polymer in self.parent.analysiswidget.datastats.getUniquePolymers():
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
            reassignActs.append(reassignMenu.addAction(polymer))
        reassignActs.append(reassignMenu.addAction("other"))
        
        contextMenu.addMenu(combineMenu)        
        contextMenu.addMenu(reassignMenu)
        deleteAct = contextMenu.addAction("Delete Particle(s)")

        numParticles = len(self.selectedContours)
        if numParticles == 0:
            reassignMenu.setDisabled(True)
            combineMenu.setDisabled(True)
            deleteAct.setDisabled(True)
        elif numParticles == 1:
            combineMenu.setDisabled(True)

        action = contextMenu.exec_(event.screenPos())

        if action == deleteAct:
            print('deleting')
        elif action in combineActs:
            newAssignment = action.text()
Lars Bittrich's avatar
Lars Bittrich committed
220
            self.parent.analysiswidget.editor.combineParticles(self.selectedContours, newAssignment)
221 222
            
        elif action in reassignActs:
Lars Bittrich's avatar
Lars Bittrich committed
223
            newAssignment = action.text()                
Lars Bittrich's avatar
Lars Bittrich committed
224
            self.parent.analysiswidget.editor.reassignParticles(self.selectedContours, newAssignment)
225 226
            
    
227 228 229 230 231 232 233 234 235 236 237 238 239
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):
240 241 242 243 244 245 246 247 248
            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)
249 250 251 252 253 254 255 256 257
        
    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__()
258 259
#        self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable)
        self.setAcceptedMouseButtons(QtCore.Qt.NoButton)
260 261 262 263
        self.view = view
        self.number = number
        self.radius = radius
        self.highlight = False
264
        self.hidden = False
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
        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):
282 283 284 285 286 287 288 289 290 291 292 293 294
        if not self.hidden:
            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))
295
        
296 297 298 299 300 301 302
#    def mousePressEvent(self, event):
#        p = event.pos()
#        x, y = p.x(), p.y()
#        r = np.sqrt(x**2+y**2)
#        if r<self.radius:
#            self.view.selectRamanScanPoint(self.number, r)
#        super().mouseReleaseEvent(event)
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353

    
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])
354
        self.rectSize = 2*self.rectSize                #<--- why that? Why not set it to 120 right in the first place??
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
        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)