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

Skip to content

Commit c7e24a9

Browse files
committed
backend_qt4 improvements, see bug 1288848
svn path=/trunk/matplotlib/; revision=4901
1 parent 1356fd5 commit c7e24a9

3 files changed

Lines changed: 56 additions & 108 deletions

File tree

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2008-01-27 Applied Martin Teichmann's patch to improve the Qt4
2+
backend. Uses Qt's builtin toolbars and statusbars.
3+
See bug 1828848 - DSD
4+
15
2008-01-10 Moved toolkits to mpl_toolkits, made mpl_toolkits
26
a namespace package - JSWHIT
37

lib/matplotlib/backends/backend_qt4.py

Lines changed: 51 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,6 @@ def __init__( self, canvas, num ):
195195
image = os.path.join( matplotlib.rcParams['datapath'],'images','matplotlib.png' )
196196
self.window.setWindowIcon(QtGui.QIcon( image ))
197197

198-
centralWidget = QtGui.QWidget( self.window )
199-
self.canvas.setParent( centralWidget )
200-
201198
# Give the keyboard focus to the figure instead of the manager
202199
self.canvas.setFocusPolicy( QtCore.Qt.ClickFocus )
203200
self.canvas.setFocus()
@@ -206,30 +203,12 @@ def __init__( self, canvas, num ):
206203
self._widgetclosed )
207204
self.window._destroying = False
208205

209-
self.toolbar = self._get_toolbar(self.canvas, centralWidget)
210-
211-
# Use a vertical layout for the plot and the toolbar. Set the
212-
# stretch to all be in the plot so the toolbar doesn't resize.
213-
layout = QtGui.QVBoxLayout( centralWidget )
214-
layout.setMargin( 0 )
215-
layout.addWidget( self.canvas, 1 )
216-
if self.toolbar:
217-
layout.addWidget( self.toolbar, 0 )
218-
219-
self.window.setCentralWidget( centralWidget )
220-
221-
# Reset the window height so the canvas will be the right
222-
# size. This ALMOST works right. The first issue is that the
223-
# reported toolbar height does not include the margin (so
224-
# we add the margin). The second is that the total width/height
225-
# is slightly smaller that we actually want. It seems like
226-
# the border of the window is being included in the size but
227-
# AFAIK there is no way to get that size.
228-
w = self.canvas.width()
229-
h = self.canvas.height()
230-
if self.toolbar:
231-
h += self.toolbar.height() + NavigationToolbar2QT.margin
232-
self.window.resize( w, h )
206+
self.toolbar = self._get_toolbar(self.canvas, self.window)
207+
self.window.addToolBar(self.toolbar)
208+
QtCore.QObject.connect(self.toolbar, QtCore.SIGNAL("message"),
209+
self.window.statusBar().showMessage)
210+
211+
self.window.setCentralWidget(self.canvas)
233212

234213
if matplotlib.is_interactive():
235214
self.window.show()
@@ -251,9 +230,9 @@ def _get_toolbar(self, canvas, parent):
251230
# must be inited after the window, drawingArea and figure
252231
# attrs are set
253232
if matplotlib.rcParams['toolbar'] == 'classic':
254-
print "Classic toolbar is not yet supported"
233+
print "Classic toolbar is not supported"
255234
elif matplotlib.rcParams['toolbar'] == 'toolbar2':
256-
toolbar = NavigationToolbar2QT(canvas, parent)
235+
toolbar = NavigationToolbar2QT(canvas, parent, False)
257236
else:
258237
toolbar = None
259238
return toolbar
@@ -274,68 +253,53 @@ def destroy( self, *args ):
274253
def set_window_title(self, title):
275254
self.window.setWindowTitle(title)
276255

277-
class NavigationToolbar2QT( NavigationToolbar2, QtGui.QWidget ):
278-
# list of toolitems to add to the toolbar, format is:
279-
# text, tooltip_text, image_file, callback(str)
280-
toolitems = (
281-
('Home', 'Reset original view', 'home.ppm', 'home'),
282-
('Back', 'Back to previous view','back.ppm', 'back'),
283-
('Forward', 'Forward to next view','forward.ppm', 'forward'),
284-
(None, None, None, None),
285-
('Pan', 'Pan axes with left mouse, zoom with right', 'move.ppm', 'pan'),
286-
('Zoom', 'Zoom to rectangle','zoom_to_rect.ppm', 'zoom'),
287-
(None, None, None, None),
288-
('Subplots', 'Configure subplots','subplots.png', 'configure_subplots'),
289-
('Save', 'Save the figure','filesave.ppm', 'save_figure'),
290-
)
291-
292-
margin = 12 # extra margin for the toolbar
293-
294-
def __init__(self, canvas, parent):
256+
class NavigationToolbar2QT( NavigationToolbar2, QtGui.QToolBar ):
257+
def __init__(self, canvas, parent, coordinates=True):
258+
""" coordinates: should we show the coordinates on the right? """
295259
self.canvas = canvas
296-
QtGui.QWidget.__init__( self, parent )
297-
298-
# Layout toolbar buttons horizontally.
299-
self.layout = QtGui.QHBoxLayout( self )
300-
self.layout.setMargin( 2 )
301-
self.layout.setSpacing( 0 )
302-
260+
self.coordinates = coordinates
261+
QtGui.QToolBar.__init__( self, parent )
303262
NavigationToolbar2.__init__( self, canvas )
304263

305-
def _init_toolbar( self ):
306-
basedir = os.path.join(matplotlib.rcParams[ 'datapath' ],'images')
307-
self.buttons = {}
308-
309-
for text, tooltip_text, image_file, callback in self.toolitems:
310-
if text == None:
311-
self.layout.addSpacing( 8 )
312-
continue
264+
def _icon(self, name):
265+
return QtGui.QIcon(os.path.join(self.basedir, name))
266+
267+
def _init_toolbar(self):
268+
self.basedir = os.path.join(matplotlib.rcParams[ 'datapath' ],'images')
269+
270+
a = self.addAction(self._icon('home.svg'), 'Home', self.home)
271+
a.setToolTip('Reset original view')
272+
a = self.addAction(self._icon('back.svg'), 'Back', self.back)
273+
a.setToolTip('Back to previous view')
274+
a = self.addAction(self._icon('forward.svg'), 'Forward', self.forward)
275+
a.setToolTip('Forward to next view')
276+
self.addSeparator()
277+
a = self.addAction(self._icon('move.svg'), 'Pan', self.pan)
278+
a.setToolTip('Pan axes with left mouse, zoom with right')
279+
a = self.addAction(self._icon('zoom_to_rect.svg'), 'Zoom', self.zoom)
280+
a.setToolTip('Zoom to rectangle')
281+
self.addSeparator()
282+
a = self.addAction(self._icon('subplots.png'), 'Subplots',
283+
self.configure_subplots)
284+
a.setToolTip('Configure subplots')
285+
a = self.addAction(self._icon('filesave.svg'), 'Save',
286+
self.save_figure)
287+
a.setToolTip('Save the figure')
313288

314-
fname = os.path.join( basedir, image_file )
315-
image = QtGui.QPixmap()
316-
image.load( fname )
317-
318-
button = QtGui.QPushButton( QtGui.QIcon( image ), "", self )
319-
button.setToolTip(tooltip_text)
320-
self.buttons[ text ] = button
321-
322-
# The automatic layout doesn't look that good - it's too close
323-
# to the images so add a margin around it.
324-
margin = self.margin
325-
button.setFixedSize( image.width()+margin, image.height()+margin )
326-
327-
QtCore.QObject.connect( button, QtCore.SIGNAL( 'clicked()' ),
328-
getattr( self, callback ) )
329-
self.layout.addWidget( button )
289+
self.buttons = {}
330290

331291
# Add the x,y location widget at the right side of the toolbar
332292
# The stretch factor is 1 which means any resizing of the toolbar
333293
# will resize this label instead of the buttons.
334-
self.locLabel = QtGui.QLabel( "", self )
335-
self.locLabel.setAlignment( QtCore.Qt.AlignRight | QtCore.Qt.AlignTop )
336-
self.locLabel.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Ignored,
337-
QtGui.QSizePolicy.Ignored))
338-
self.layout.addWidget( self.locLabel, 1 )
294+
if self.coordinates:
295+
self.locLabel = QtGui.QLabel( "", self )
296+
self.locLabel.setAlignment(
297+
QtCore.Qt.AlignRight | QtCore.Qt.AlignTop )
298+
self.locLabel.setSizePolicy(
299+
QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding,
300+
QtGui.QSizePolicy.Ignored))
301+
labelAction = self.addWidget(self.locLabel)
302+
labelAction.setVisible(True)
339303

340304
# reference holder for subplots_adjust window
341305
self.adj_window = None
@@ -351,7 +315,9 @@ def dynamic_update( self ):
351315
self.canvas.draw()
352316

353317
def set_message( self, s ):
354-
self.locLabel.setText( s.replace(', ', '\n') )
318+
self.emit(QtCore.SIGNAL("message"), s)
319+
if self.coordinates:
320+
self.locLabel.setText(s.replace(', ', '\n'))
355321

356322
def set_cursor( self, cursor ):
357323
if DEBUG: print 'Set cursor' , cursor
@@ -384,28 +350,6 @@ def configure_subplots(self):
384350

385351
win.show()
386352

387-
# self.adj_window = QtGui.QDialog()
388-
# win = self.adj_window
389-
# win.setAttribute(QtCore.Qt.WA_DeleteOnClose)
390-
# win.setWindowTitle("Subplot Configuration Tool")
391-
# image = os.path.join( matplotlib.rcParams['datapath'],'images','matplotlib.png' )
392-
# win.setWindowIcon(QtGui.QIcon( image ))
393-
#
394-
# toolfig = Figure(figsize=(6,3))
395-
# toolfig.subplots_adjust(top=0.9)
396-
# canvas = self._get_canvas(toolfig)
397-
# tool = SubplotTool(self.canvas.figure, toolfig)
398-
#
399-
# canvas.setParent(win)
400-
# w = int (toolfig.bbox.width())
401-
# h = int (toolfig.bbox.height())
402-
#
403-
# win.resize(w, h)
404-
# canvas.setFocus()
405-
#
406-
# canvas.show()
407-
# win.show()
408-
409353
def _get_canvas(self, fig):
410354
return FigureCanvasQT(fig)
411355

@@ -425,7 +369,7 @@ def save_figure( self ):
425369
selectedFilter = filter
426370
filters.append(filter)
427371
filters = ';;'.join(filters)
428-
372+
429373
fname = QtGui.QFileDialog.getSaveFileName(
430374
self, "Choose a filename to save to", start, filters, selectedFilter)
431375
if fname:

lib/matplotlib/backends/backend_qt4agg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def _get_toolbar(self, canvas, parent):
3535
# must be inited after the window, drawingArea and figure
3636
# attrs are set
3737
if matplotlib.rcParams['toolbar']=='classic':
38-
print "Classic toolbar is not yet supported"
38+
print "Classic toolbar is not supported"
3939
elif matplotlib.rcParams['toolbar']=='toolbar2':
4040
toolbar = NavigationToolbar2QTAgg(canvas, parent)
4141
else:

0 commit comments

Comments
 (0)