File: ScaleBar.py

package info (click to toggle)
python-pyqtgraph 0.9.8-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,552 kB
  • ctags: 4,262
  • sloc: python: 30,181; makefile: 116; sh: 1
file content (104 lines) | stat: -rw-r--r-- 3,276 bytes parent folder | download
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
from pyqtgraph.Qt import QtGui, QtCore
from .GraphicsObject import *
from .GraphicsWidgetAnchor import *
from .TextItem import TextItem
import numpy as np
import pyqtgraph.functions as fn
import pyqtgraph as pg

__all__ = ['ScaleBar']

class ScaleBar(GraphicsObject, GraphicsWidgetAnchor):
    """
    Displays a rectangular bar to indicate the relative scale of objects on the view.
    """
    def __init__(self, size, width=5, brush=None, pen=None, suffix='m'):
        GraphicsObject.__init__(self)
        GraphicsWidgetAnchor.__init__(self)
        self.setFlag(self.ItemHasNoContents)
        self.setAcceptedMouseButtons(QtCore.Qt.NoButton)
        
        if brush is None:
            brush = pg.getConfigOption('foreground')
        self.brush = fn.mkBrush(brush)
        self.pen = fn.mkPen(pen)
        self._width = width
        self.size = size
        
        self.bar = QtGui.QGraphicsRectItem()
        self.bar.setPen(self.pen)
        self.bar.setBrush(self.brush)
        self.bar.setParentItem(self)
        
        self.text = TextItem(text=fn.siFormat(size, suffix=suffix), anchor=(0.5,1))
        self.text.setParentItem(self)

    def parentChanged(self):
        view = self.parentItem()
        if view is None:
            return
        view.sigRangeChanged.connect(self.updateBar)
        self.updateBar()
        
        
    def updateBar(self):
        view = self.parentItem()
        if view is None:
            return
        p1 = view.mapFromViewToItem(self, QtCore.QPointF(0,0))
        p2 = view.mapFromViewToItem(self, QtCore.QPointF(self.size,0))
        w = (p2-p1).x()
        self.bar.setRect(QtCore.QRectF(-w, 0, w, self._width))
        self.text.setPos(-w/2., 0)

    def boundingRect(self):
        return QtCore.QRectF()





#class ScaleBar(UIGraphicsItem):
    #"""
    #Displays a rectangular bar with 10 divisions to indicate the relative scale of objects on the view.
    #"""
    #def __init__(self, size, width=5, color=(100, 100, 255)):
        #UIGraphicsItem.__init__(self)
        #self.setAcceptedMouseButtons(QtCore.Qt.NoButton)
        
        #self.brush = fn.mkBrush(color)
        #self.pen = fn.mkPen((0,0,0))
        #self._width = width
        #self.size = size
        
    #def paint(self, p, opt, widget):
        #UIGraphicsItem.paint(self, p, opt, widget)
        
        #rect = self.boundingRect()
        #unit = self.pixelSize()
        #y = rect.top() + (rect.bottom()-rect.top()) * 0.02
        #y1 = y + unit[1]*self._width
        #x = rect.right() + (rect.left()-rect.right()) * 0.02
        #x1 = x - self.size
        
        #p.setPen(self.pen)
        #p.setBrush(self.brush)
        #rect = QtCore.QRectF(
            #QtCore.QPointF(x1, y1), 
            #QtCore.QPointF(x, y)
        #)
        #p.translate(x1, y1)
        #p.scale(rect.width(), rect.height())
        #p.drawRect(0, 0, 1, 1)
        
        #alpha = np.clip(((self.size/unit[0]) - 40.) * 255. / 80., 0, 255)
        #p.setPen(QtGui.QPen(QtGui.QColor(0, 0, 0, alpha)))
        #for i in range(1, 10):
            ##x2 = x + (x1-x) * 0.1 * i
            #x2 = 0.1 * i
            #p.drawLine(QtCore.QPointF(x2, 0), QtCore.QPointF(x2, 1))
        

    #def setSize(self, s):
        #self.size = s