forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
80 lines (72 loc) · 2.75 KB
/
tools.py
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
#!/usr/bin/env python
# encoding: utf-8
'''
Created on 2019年5月21日
@author: weike32
@site: https://pyqt5.com ,https://github.com/weike32
@email: [email protected]
@file: CopyContent
@description: 工具类
'''
from pyqtgraph.exporters.ImageExporter import ImageExporter, Exporter
from pyqtgraph.parametertree import Parameter
import pyqtgraph as pg
#不用修改源码,重加载,解决右键保存图片异常
def widthChanged(self):
sr = self.getSourceRect()
ar = float(sr.height()) / sr.width()
self.params.param('height').setValue(int(self.params['width'] * ar), blockSignal=self.heightChanged)
def heightChanged(self):
sr = self.getSourceRect()
ar = float(sr.width()) / sr.height()
self.params.param('width').setValue(int(self.params['height'] * ar), blockSignal=self.widthChanged)
def New__init__(self, item):
Exporter.__init__(self, item)
tr = self.getTargetRect()
if isinstance(item, pg.Qt.QtGui.QGraphicsItem):
scene = item.scene()
else:
scene = item
bgbrush = scene.views()[0].backgroundBrush()
bg = bgbrush.color()
if bgbrush.style() == pg.Qt.QtCore.Qt.NoBrush:
bg.setAlpha(0)
self.params = Parameter(name='params', type='group', children=[
{'name': 'width', 'type': 'int', 'value': int(tr.width()), 'limits': (0, None)},
{'name': 'height', 'type': 'int', 'value': int(tr.height()), 'limits': (0, None)},
{'name': 'antialias', 'type': 'bool', 'value': True},
{'name': 'background', 'type': 'color', 'value': bg},
])
self.params.param('width').sigValueChanged.connect(self.widthChanged)
self.params.param('height').sigValueChanged.connect(self.heightChanged)
ImageExporter.heightChanged = heightChanged
ImageExporter.widthChanged = widthChanged
ImageExporter.__init__ = New__init__
#解决自定义坐标轴密集显示
class MyStringAxis(pg.AxisItem):
def __init__(self, xdict, *args, **kwargs):
pg.AxisItem.__init__(self, *args, **kwargs)
self.xdict = xdict
def tickStrings(self, values, scale, spacing):
strings = []
for v in values:
vs = v * scale
if vs in self.xdict.keys():
vstr = self.xdict[vs]
else:
vstr = ""
strings.append(vstr)
return strings
# 禁止鼠标事件
class CustomViewBox(pg.ViewBox):
def __init__(self, *args, **kwds):
pg.ViewBox.__init__(self, *args, **kwds)
self.RectMode = 3
self.setMouseMode(self.RectMode)
def mouseClickEvent(self, ev):
if ev.button() == pg.QtCore.Qt.RightButton:
self.autoRange()
def mouseDragEvent(self, ev):
pg.ViewBox.mouseDragEvent(self, ev)
def wheelEvent(self,ev, axis=None):
pg.ViewBox.wheelEvent(self, ev, axis)