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

Skip to content

Commit 0f473a2

Browse files
committed
Cleaning up cocoaagg and trying to implement blitting
svn path=/trunk/matplotlib/; revision=1649
1 parent a0f9178 commit 0f473a2

2 files changed

Lines changed: 49 additions & 36 deletions

File tree

lib/matplotlib/backends/backend_cocoaagg.py

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
55
A native Cocoa backend via PyObjC in OSX.
66
7-
Author: Charles Moad (cmoad@indiana.edu)
7+
Author: Charles Moad (cmoad@users.sourceforge.net)
88
99
Notes:
1010
- THIS IS STILL IN DEVELOPMENT!
11-
- Requires PyObjC (currently testing v1.3.6)
11+
- Requires PyObjC (currently testing v1.3.7)
1212
"""
1313

1414
import os, sys
@@ -17,6 +17,7 @@
1717
import objc
1818
except:
1919
print >>sys.stderr, 'The CococaAgg backend required PyObjC to be installed!'
20+
print >>sys.stderr, ' (currently testing v1.3.7)'
2021
sys.exit()
2122

2223
from Foundation import *
@@ -27,27 +28,35 @@
2728
from matplotlib.figure import Figure
2829
from matplotlib.backend_bases import FigureManagerBase
2930
from backend_agg import FigureCanvasAgg
31+
from matplotlib._pylab_helpers import Gcf
3032
import pylab
3133

32-
DEBUG = False
33-
3434
mplBundle = NSBundle.bundleWithPath_(matplotlib.get_data_path())
3535

3636
def new_figure_manager(num, *args, **kwargs):
37-
if DEBUG: print >>sys.stderr, 'new_figure_manager'
3837
thisFig = Figure( *args, **kwargs )
39-
canvas = FigureCanvasAgg(thisFig)
38+
canvas = FigureCanvasCocoaAgg(thisFig)
4039
return FigureManagerCocoaAgg(canvas, num)
41-
40+
4241
def show():
43-
if DEBUG: print >>sys.stderr, 'show'
42+
for manager in Gcf.get_all_fig_managers():
43+
manager.show()
4444
# Let the cocoa run loop take over
4545
NSApplication.sharedApplication().run()
46-
46+
4747
def draw_if_interactive():
4848
if matplotlib.is_interactive():
49-
print >>sys.stderr, 'Interactive not implemented yet'
50-
49+
figManager = Gcf.get_active()
50+
if figManager is not None:
51+
figManager.show()
52+
53+
class FigureCanvasCocoaAgg(FigureCanvasAgg):
54+
def draw(self):
55+
FigureCanvasAgg.draw(self)
56+
57+
def blit(self, bbox):
58+
pass
59+
5160
NibClassBuilder.extractClasses('Matplotlib.nib', mplBundle)
5261

5362
class MatplotlibController(NibClassBuilder.AutoBaseClass):
@@ -56,15 +65,14 @@ class MatplotlibController(NibClassBuilder.AutoBaseClass):
5665
# PlotView plotView
5766

5867
def awakeFromNib(self):
59-
if DEBUG: print 'MPLController awakeFromNib'
6068
# Get a reference to the active canvas
6169
self.canvas = pylab.get_current_fig_manager().canvas
62-
self.plotWindow.plotView = self.plotView
6370
self.plotView.canvas = self.canvas
71+
self.canvas.plotView = self.plotView
6472

6573
self.plotWindow.setAcceptsMouseMovedEvents_(True)
6674
self.plotWindow.makeKeyAndOrderFront_(self)
67-
self.plotWindow.setDelegate_(self.plotWindow)
75+
self.plotWindow.setDelegate_(self.plotView)
6876

6977
self.plotView.setImageFrameStyle_(NSImageFrameGroove)
7078
self.plotView.image = NSImage.alloc().initWithSize_((0,0))
@@ -74,29 +82,25 @@ def awakeFromNib(self):
7482
self.plotWindow.makeFirstResponder_(self.plotView)
7583

7684
# Force the first update
77-
self.plotWindow.windowDidResize_(self)
85+
self.plotView.windowDidResize_(self)
7886

7987
def saveFigure_(self, sender):
80-
if DEBUG: print >>sys.stderr, 'saveFigure_'
88+
print >>sys.stderr, 'Not Implented Yet'
8189

8290
class PlotWindow(NibClassBuilder.AutoBaseClass):
83-
def windowDidResize_(self, sender):
84-
w,h = self.plotView.bounds().size
85-
dpi = self.plotView.canvas.figure.dpi.get()
86-
self.plotView.canvas.figure.set_figsize_inches(w / dpi, h / dpi)
87-
self.plotView.updatePlot()
91+
pass
8892

8993
class PlotView(NibClassBuilder.AutoBaseClass):
9094
def updatePlot(self):
91-
self.canvas.draw() # tell the agg to render
92-
9395
w,h = self.canvas.get_width_height()
9496

95-
if (hasattr(self, 'brep')):
96-
self.image.removeRepresentation_(self.brep)
97+
# Remove all previous images
98+
for i in xrange(self.image.representations().count()):
99+
self.image.removeRepresentation_(self.image.representations().objectAtIndex_(i))
100+
97101
self.image.setSize_((w,h))
98102

99-
self.brep = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(
103+
brep = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(
100104
(self.canvas.buffer_rgba(),'','','',''), # Image data
101105
w, # width
102106
h, # height
@@ -108,13 +112,19 @@ def updatePlot(self):
108112
w*4, # row bytes
109113
32) # bits per pixel
110114

111-
self.image.addRepresentation_(self.brep)
115+
self.image.addRepresentation_(brep)
112116
self.setNeedsDisplay_(True)
113117

118+
def windowDidResize_(self, sender):
119+
w,h = self.bounds().size
120+
dpi = self.canvas.figure.dpi.get()
121+
self.canvas.figure.set_figsize_inches(w / dpi, h / dpi)
122+
self.canvas.draw()
123+
self.updatePlot()
124+
114125
def mouseDown_(self, event):
115126
loc = self.convertPoint_fromView_(event.locationInWindow(), None)
116127
type = event.type()
117-
if DEBUG: print >>sys.stderr, 'mouseDown_:', loc, type
118128
if (type == NSLeftMouseDown):
119129
button = 1
120130
else:
@@ -125,14 +135,12 @@ def mouseDown_(self, event):
125135

126136
def mouseDragged_(self, event):
127137
loc = self.convertPoint_fromView_(event.locationInWindow(), None)
128-
if DEBUG: print >>sys.stderr, 'mouseDragged_:', loc
129138
self.canvas.motion_notify_event(loc.x, loc.y)
130139
self.updatePlot()
131140

132141
def mouseUp_(self, event):
133142
loc = self.convertPoint_fromView_(event.locationInWindow(), None)
134143
type = event.type()
135-
if DEBUG: print >>sys.stderr, 'mouseUp_:', loc, type
136144
if (type == NSLeftMouseUp):
137145
button = 1
138146
else:
@@ -142,12 +150,12 @@ def mouseUp_(self, event):
142150
self.updatePlot()
143151

144152
def keyDown_(self, event):
145-
if DEBUG: print >>sys.stderr, 'keyDown_', event.keyCode()
146-
self.canvas.key_press_event(event.keyCode())
153+
self.canvas.key_press_event(event.characters())
154+
self.updatePlot()
147155

148156
def keyUp_(self, event):
149-
if DEBUG: print >>sys.stderr, 'keyUp_', event.keyCode()
150-
self.canvas.key_release_event(event.keyCode())
157+
self.canvas.key_release_event(event.characters())
158+
self.updatePlot()
151159

152160
class MPLBootstrap(NSObject):
153161
# Loads the nib containing the PlotWindow and PlotView
@@ -164,12 +172,13 @@ class FigureManagerCocoaAgg(FigureManagerBase):
164172
def __init__(self, canvas, num):
165173
FigureManagerBase.__init__(self, canvas, num)
166174

167-
# If there are multiple figures we only need to enable once
168175
try:
169176
WMEnable('Matplotlib')
170177
except:
171-
pass
178+
# MULTIPLE FIGURES ARE BUGGY!
179+
pass # If there are multiple figures we only need to enable once
172180

181+
def show(self):
173182
# Load a new PlotWindow
174183
MPLBootstrap.alloc().init().performSelectorOnMainThread_withObject_waitUntilDone_(
175184
'startWithBundle:',

setup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@
8686

8787
data_files=[('share/matplotlib', data),]
8888

89+
# Needed for CocoaAgg
90+
data_files.append(('share/matplotlib/Matplotlib.nib',
91+
glob.glob('lib/matplotlib/backends/Matplotlib.nib/*.nib')))
92+
8993
# Figure out which array packages to provide binary support for
9094
# and define the NUMERIX value: Numeric, numarray, or both.
9195
try:

0 commit comments

Comments
 (0)