scalebar.py 3.19 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
# -*- 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
import numpy as np

WX, WY = 400, 80

class ScaleBar(QtWidgets.QMdiSubWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        
        self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint|QtCore.Qt.FramelessWindowHint)
        
        self.setFixedSize(WX, WY)
        self.divisor = None
        self.drag = None
        
    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)
        
    @QtCore.pyqtSlot(float)
    def updateScale(self, pixelscale):
        if pixelscale<0:
            self.divisor = None
        else:
            tmp = np.log10(pixelscale*WX*.95*.83);
            exponent = np.floor(tmp)
            divisor = 10**(tmp-exponent)
            if divisor>3:
                divisor = np.round(divisor)
            else:
                divisor = np.round(divisor*2)/2
    
            divisor *= 10**exponent
    
            self.divisor = divisor
            self.wscale = int(np.round(divisor/pixelscale))
        self.update()
    
    def paintEvent(self, e):
        qp = QtGui.QPainter()
        qp.begin(self)
        qp.setPen(QtGui.QColor(0,0,0))
        backgroundColor = self.palette().light().color()
        qp.setBrush(backgroundColor)
        qp.drawRect(0,0,WX,WY)
        qp.setPen(QtGui.QColor(0,0,0))
        qp.setBrush(QtGui.QColor(0,0,100))
81
        y0, dy = 50, 25
82 83 84 85 86 87 88
        if self.divisor is None:
            qp.drawRect(10,y0,WX-20,dy)
        else:
            font = qp.font()
            font.setPointSize(30)
            qp.setFont(font)
            qp.drawRect((WX-self.wscale)//2,y0,self.wscale,dy)
89 90
            qp.drawText((WX-self.wscale)//2,5,self.wscale,dy+20, 
                        QtCore.Qt.AlignCenter, str(int(self.divisor))+" µm")
91 92 93 94 95 96 97 98 99 100 101
        qp.end()
        
        
if __name__ == '__main__':

    import sys

    app = QtWidgets.QApplication(sys.argv)
    meas = ScaleBar(None)
    meas.show()
    sys.exit(app.exec_())