analysisWidgets.py 3.97 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
# -*- 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/>.
"""
from PyQt5 import QtCore, QtWidgets, QtGui
from PIL import ImageFont
import numpy as np


WX, WY = 1024, 200
        
class Legend(QtWidgets.QMdiSubWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        
        self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint|QtCore.Qt.FramelessWindowHint)
        
        self.setFixedSize(200, 800)
        self.wscale = 1
        self.drag = None
        self.items = []     	   #list of items to display (text, color)
        self.tileSize = 12
        self.fontSize = 15
        self.spacer = 10
        
    def mousePressEvent(self, event):
        if event.button()==QtCore.Qt.LeftButton:
            self.drag = event.pos()
            
    def mouseMoveEvent(self, event):
        if self.drag is not None:
            p0 = event.pos()
            self.move(self.mapToParent(p0-self.drag))
            self.parentWidget().update()
        else:
            super().mouseMoveEvent(event)
                        
    def mouseReleaseEvent(self, event):
        self.drag = None
        super().mouseReleaseEvent(event)        

    def paintEvent(self, event):
        numEntries = len(self.items)
        if numEntries > 0:
            
            def getSize(fontsize, text, tileSize, spacer):
#                font = ImageFont.truetype('arial.ttf', fontsize)
#                size = font.getsize(text)
                size = 5*len(text), fontsize+2
                width, height = size[0]*1.5 + tileSize + spacer, numEntries * (tileSize+1*spacer) + 2*spacer
                
                return width, height
            
            
            fontSize, tileSize, spacer = self.fontSize, self.tileSize, self.spacer
            longestEntry = max([i[0] for i in self.items], key=len)
            width, height = getSize(fontSize, longestEntry, tileSize, spacer)
            
            #scale smaller, if necessary
            if height > 1024:
                factor = 1024/height
                #prevent text getting tooo small:
                factor = np.clip(factor, 0.6, 1)        #0.6*15   would be fontSize 9
                
                height, tileSize, fontSize, spacer = height*factor, tileSize*factor, int(fontSize*factor), spacer*factor
                
                width, height = getSize(fontSize, longestEntry, tileSize, spacer)
                
            self.setFixedSize(width, height)
            
            qp = QtGui.QPainter()
            qp.begin(self)
            font = QtGui.QFont()
            font.setPixelSize(fontSize)
            qp.setFont(font)
            
            for index, item in enumerate(self.items):
                #draw Text
                x0 = tileSize+2*spacer
                y0 = index*(fontSize+spacer)
                rect= QtCore.QRectF(x0, y0, width, float(fontSize+spacer))
                qp.drawText(rect, QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter, item[0])
                
                #draw colored Box
                qp.setBrush(item[1])
                x0 = spacer
                y0 = index*(fontSize+spacer) + (fontSize - tileSize)/2 +spacer/2
                qp.drawRect(x0, y0, tileSize, tileSize)
            
            qp.end()