Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 73ed530

Browse files
committed
added blitting for qt4agg
svn path=/trunk/matplotlib/; revision=2585
1 parent dac8047 commit 73ed530

4 files changed

Lines changed: 107 additions & 25 deletions

File tree

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2006-07-19 Added blitting for Qt4Agg - CM
2+
13
2006-07-19 Added lasso widget and example examples/lasso_demo.py - JDH
24

35
2006-07-18 Added blitting for QtAgg backend - CM

examples/animation_blit_qt4.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# For detailed comments on animation and the techniqes used here, see
2+
# the wiki entry http://www.scipy.org/Cookbook/Matplotlib/Animations
3+
4+
import os, sys
5+
import matplotlib
6+
matplotlib.use('Qt4Agg') # qt4 example
7+
8+
from PyQt4 import QtCore, QtGui
9+
10+
ITERS = 1000
11+
12+
import pylab as p
13+
import matplotlib.numerix as nx
14+
import time
15+
16+
class BlitQT(QtCore.QObject):
17+
def __init__(self):
18+
QtCore.QObject.__init__(self, None)
19+
20+
self.ax = p.subplot(111)
21+
self.canvas = self.ax.figure.canvas
22+
self.cnt = 0
23+
24+
# create the initial line
25+
self.x = nx.arange(0,2*nx.pi,0.01)
26+
self.line, = p.plot(self.x, nx.sin(self.x), animated=True, lw=2)
27+
28+
self.background = None
29+
30+
def timerEvent(self, evt):
31+
if self.background is None:
32+
self.background = self.canvas.copy_from_bbox(self.ax.bbox)
33+
34+
# restore the clean slate background
35+
self.canvas.restore_region(self.background)
36+
# update the data
37+
self.line.set_ydata(nx.sin(self.x+self.cnt/10.0))
38+
# just draw the animated artist
39+
self.ax.draw_artist(self.line)
40+
# just redraw the axes rectangle
41+
self.canvas.blit(self.ax.bbox)
42+
43+
if self.cnt==ITERS:
44+
# print the timing info and quit
45+
print 'FPS:' , ITERS/(time.time()-self.tstart)
46+
sys.exit()
47+
48+
else:
49+
self.cnt += 1
50+
51+
p.subplots_adjust(left=0.3, bottom=0.3) # check for flipy bugs
52+
p.grid() # to ensure proper background restore
53+
54+
app = BlitQT()
55+
# for profiling
56+
app.tstart = time.time()
57+
app.startTimer(0)
58+
59+
p.show()

lib/matplotlib/backends/backend_qt4agg.py

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,36 +67,47 @@ def paintEvent( self, e ):
6767
shown onscreen.
6868
"""
6969

70-
FigureCanvasQT.paintEvent( self, e )
70+
#FigureCanvasQT.paintEvent( self, e )
7171
if DEBUG: print 'FigureCanvasQtAgg.paintEvent: ', \
7272
self.get_width_height()
7373

7474
p = QtGui.QPainter( self )
75-
FigureCanvasAgg.draw( self )
7675

7776
# only replot data when needed
78-
if ( self.replot ):
79-
stringBuffer = str( self.buffer_rgba(0,0) )
80-
81-
82-
# matplotlib is in rgba byte order.
83-
# qImage wants to put the bytes into argb format and
84-
# is in a 4 byte unsigned int. little endian system is LSB first
85-
# and expects the bytes in reverse order (bgra).
86-
if ( QtCore.QSysInfo.ByteOrder == QtCore.QSysInfo.LittleEndian ):
87-
stringBuffer = self.renderer._renderer.tostring_bgra()
88-
else:
89-
stringBuffer = self.renderer._renderer.tostring_argb()
90-
qImage = QtGui.QImage( stringBuffer, self.renderer.width,
91-
self.renderer.height,
92-
QtGui.QImage.Format_ARGB32)
77+
if type(self.replot) is bool: # might be a bbox for blitting
78+
if ( self.replot ):
79+
#stringBuffer = str( self.buffer_rgba(0,0) )
80+
FigureCanvasAgg.draw( self )
81+
82+
# matplotlib is in rgba byte order.
83+
# qImage wants to put the bytes into argb format and
84+
# is in a 4 byte unsigned int. little endian system is LSB first
85+
# and expects the bytes in reverse order (bgra).
86+
if ( QtCore.QSysInfo.ByteOrder == QtCore.QSysInfo.LittleEndian ):
87+
stringBuffer = self.renderer._renderer.tostring_bgra()
88+
else:
89+
stringBuffer = self.renderer._renderer.tostring_argb()
90+
qImage = QtGui.QImage( stringBuffer, self.renderer.width,
91+
self.renderer.height,
92+
QtGui.QImage.Format_ARGB32)
93+
self.pixmap = self.pixmap.fromImage( qImage )
94+
p.drawPixmap( QtCore.QPoint( 0, 0 ), self.pixmap )
95+
96+
# draw the zoom rectangle to the QPainter
97+
if ( self.drawRect ):
98+
p.setPen( QtGui.QPen( QtCore.Qt.black, 1, QtCore.Qt.DotLine ) )
99+
p.drawRect( self.rect[0], self.rect[1], self.rect[2], self.rect[3] )
100+
101+
# we are blitting here
102+
else:
103+
bbox = self.replot
104+
w, h = int(bbox.width()), int(bbox.height())
105+
l, t = bbox.ll().x().get(), bbox.ur().y().get()
106+
reg = self.copy_from_bbox(bbox)
107+
stringBuffer = reg.to_string()
108+
qImage = QtGui.QImage(stringBuffer, w, h, QtGui.QImage.Format_ARGB32)
93109
self.pixmap = self.pixmap.fromImage( qImage )
94-
p.drawPixmap( QtCore.QPoint( 0, 0 ), self.pixmap )
95-
96-
# draw the zoom rectangle to the QPainter
97-
if ( self.drawRect ):
98-
p.setPen( QtGui.QPen( QtCore.Qt.black, 1, QtCore.Qt.DotLine ) )
99-
p.drawRect( self.rect[0], self.rect[1], self.rect[2], self.rect[3] )
110+
p.drawPixmap(QtCore.QPoint(l, self.renderer.height-t), self.pixmap)
100111

101112
p.end()
102113
self.replot = False
@@ -109,7 +120,17 @@ def draw( self ):
109120

110121
if DEBUG: print "FigureCanvasQtAgg.draw"
111122
self.replot = True
112-
self.repaint( )
123+
self.update( )
124+
125+
def blit(self, bbox=None):
126+
"""
127+
Blit the region in bbox
128+
"""
129+
130+
self.replot = bbox
131+
w, h = int(bbox.width()), int(bbox.height())
132+
l, t = bbox.ll().x().get(), bbox.ur().y().get()
133+
self.update(l, self.renderer.height-t, w, h)
113134

114135
def print_figure( self, filename, dpi=150, facecolor='w', edgecolor='w',
115136
orientation='portrait', **kwargs ):

lib/matplotlib/backends/backend_qtagg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def paintEvent( self, e ):
7878
if type(self.replot) is bool: # might be a bbox for blitting
7979
if self.replot:
8080
FigureCanvasAgg.draw( self )
81-
stringBuffer = str( self.buffer_rgba(0,0) )
81+
#stringBuffer = str( self.buffer_rgba(0,0) )
8282

8383
# matplotlib is in rgba byte order.
8484
# qImage wants to put the bytes into argb format and

0 commit comments

Comments
 (0)