From: <ds...@us...> - 2008-01-27 15:17:44
|
Revision: 4901 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=4901&view=rev Author: dsdale Date: 2008-01-27 07:17:28 -0800 (Sun, 27 Jan 2008) Log Message: ----------- backend_qt4 improvements, see bug 1288848 Modified Paths: -------------- trunk/matplotlib/CHANGELOG trunk/matplotlib/lib/matplotlib/backends/backend_qt4.py trunk/matplotlib/lib/matplotlib/backends/backend_qt4agg.py Modified: trunk/matplotlib/CHANGELOG =================================================================== --- trunk/matplotlib/CHANGELOG 2008-01-26 13:22:33 UTC (rev 4900) +++ trunk/matplotlib/CHANGELOG 2008-01-27 15:17:28 UTC (rev 4901) @@ -1,3 +1,7 @@ +2008-01-27 Applied Martin Teichmann's patch to improve the Qt4 + backend. Uses Qt's builtin toolbars and statusbars. + See bug 1828848 - DSD + 2008-01-10 Moved toolkits to mpl_toolkits, made mpl_toolkits a namespace package - JSWHIT Modified: trunk/matplotlib/lib/matplotlib/backends/backend_qt4.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backends/backend_qt4.py 2008-01-26 13:22:33 UTC (rev 4900) +++ trunk/matplotlib/lib/matplotlib/backends/backend_qt4.py 2008-01-27 15:17:28 UTC (rev 4901) @@ -195,9 +195,6 @@ image = os.path.join( matplotlib.rcParams['datapath'],'images','matplotlib.png' ) self.window.setWindowIcon(QtGui.QIcon( image )) - centralWidget = QtGui.QWidget( self.window ) - self.canvas.setParent( centralWidget ) - # Give the keyboard focus to the figure instead of the manager self.canvas.setFocusPolicy( QtCore.Qt.ClickFocus ) self.canvas.setFocus() @@ -206,31 +203,13 @@ self._widgetclosed ) self.window._destroying = False - self.toolbar = self._get_toolbar(self.canvas, centralWidget) + self.toolbar = self._get_toolbar(self.canvas, self.window) + self.window.addToolBar(self.toolbar) + QtCore.QObject.connect(self.toolbar, QtCore.SIGNAL("message"), + self.window.statusBar().showMessage) - # Use a vertical layout for the plot and the toolbar. Set the - # stretch to all be in the plot so the toolbar doesn't resize. - layout = QtGui.QVBoxLayout( centralWidget ) - layout.setMargin( 0 ) - layout.addWidget( self.canvas, 1 ) - if self.toolbar: - layout.addWidget( self.toolbar, 0 ) + self.window.setCentralWidget(self.canvas) - self.window.setCentralWidget( centralWidget ) - - # Reset the window height so the canvas will be the right - # size. This ALMOST works right. The first issue is that the - # reported toolbar height does not include the margin (so - # we add the margin). The second is that the total width/height - # is slightly smaller that we actually want. It seems like - # the border of the window is being included in the size but - # AFAIK there is no way to get that size. - w = self.canvas.width() - h = self.canvas.height() - if self.toolbar: - h += self.toolbar.height() + NavigationToolbar2QT.margin - self.window.resize( w, h ) - if matplotlib.is_interactive(): self.window.show() @@ -251,9 +230,9 @@ # must be inited after the window, drawingArea and figure # attrs are set if matplotlib.rcParams['toolbar'] == 'classic': - print "Classic toolbar is not yet supported" + print "Classic toolbar is not supported" elif matplotlib.rcParams['toolbar'] == 'toolbar2': - toolbar = NavigationToolbar2QT(canvas, parent) + toolbar = NavigationToolbar2QT(canvas, parent, False) else: toolbar = None return toolbar @@ -274,68 +253,53 @@ def set_window_title(self, title): self.window.setWindowTitle(title) -class NavigationToolbar2QT( NavigationToolbar2, QtGui.QWidget ): - # list of toolitems to add to the toolbar, format is: - # text, tooltip_text, image_file, callback(str) - toolitems = ( - ('Home', 'Reset original view', 'home.ppm', 'home'), - ('Back', 'Back to previous view','back.ppm', 'back'), - ('Forward', 'Forward to next view','forward.ppm', 'forward'), - (None, None, None, None), - ('Pan', 'Pan axes with left mouse, zoom with right', 'move.ppm', 'pan'), - ('Zoom', 'Zoom to rectangle','zoom_to_rect.ppm', 'zoom'), - (None, None, None, None), - ('Subplots', 'Configure subplots','subplots.png', 'configure_subplots'), - ('Save', 'Save the figure','filesave.ppm', 'save_figure'), - ) - - margin = 12 # extra margin for the toolbar - - def __init__(self, canvas, parent): +class NavigationToolbar2QT( NavigationToolbar2, QtGui.QToolBar ): + def __init__(self, canvas, parent, coordinates=True): + """ coordinates: should we show the coordinates on the right? """ self.canvas = canvas - QtGui.QWidget.__init__( self, parent ) - - # Layout toolbar buttons horizontally. - self.layout = QtGui.QHBoxLayout( self ) - self.layout.setMargin( 2 ) - self.layout.setSpacing( 0 ) - + self.coordinates = coordinates + QtGui.QToolBar.__init__( self, parent ) NavigationToolbar2.__init__( self, canvas ) - def _init_toolbar( self ): - basedir = os.path.join(matplotlib.rcParams[ 'datapath' ],'images') - self.buttons = {} + def _icon(self, name): + return QtGui.QIcon(os.path.join(self.basedir, name)) - for text, tooltip_text, image_file, callback in self.toolitems: - if text == None: - self.layout.addSpacing( 8 ) - continue + def _init_toolbar(self): + self.basedir = os.path.join(matplotlib.rcParams[ 'datapath' ],'images') - fname = os.path.join( basedir, image_file ) - image = QtGui.QPixmap() - image.load( fname ) + a = self.addAction(self._icon('home.svg'), 'Home', self.home) + a.setToolTip('Reset original view') + a = self.addAction(self._icon('back.svg'), 'Back', self.back) + a.setToolTip('Back to previous view') + a = self.addAction(self._icon('forward.svg'), 'Forward', self.forward) + a.setToolTip('Forward to next view') + self.addSeparator() + a = self.addAction(self._icon('move.svg'), 'Pan', self.pan) + a.setToolTip('Pan axes with left mouse, zoom with right') + a = self.addAction(self._icon('zoom_to_rect.svg'), 'Zoom', self.zoom) + a.setToolTip('Zoom to rectangle') + self.addSeparator() + a = self.addAction(self._icon('subplots.png'), 'Subplots', + self.configure_subplots) + a.setToolTip('Configure subplots') + a = self.addAction(self._icon('filesave.svg'), 'Save', + self.save_figure) + a.setToolTip('Save the figure') - button = QtGui.QPushButton( QtGui.QIcon( image ), "", self ) - button.setToolTip(tooltip_text) - self.buttons[ text ] = button + self.buttons = {} - # The automatic layout doesn't look that good - it's too close - # to the images so add a margin around it. - margin = self.margin - button.setFixedSize( image.width()+margin, image.height()+margin ) - - QtCore.QObject.connect( button, QtCore.SIGNAL( 'clicked()' ), - getattr( self, callback ) ) - self.layout.addWidget( button ) - # Add the x,y location widget at the right side of the toolbar # The stretch factor is 1 which means any resizing of the toolbar # will resize this label instead of the buttons. - self.locLabel = QtGui.QLabel( "", self ) - self.locLabel.setAlignment( QtCore.Qt.AlignRight | QtCore.Qt.AlignTop ) - self.locLabel.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Ignored, - QtGui.QSizePolicy.Ignored)) - self.layout.addWidget( self.locLabel, 1 ) + if self.coordinates: + self.locLabel = QtGui.QLabel( "", self ) + self.locLabel.setAlignment( + QtCore.Qt.AlignRight | QtCore.Qt.AlignTop ) + self.locLabel.setSizePolicy( + QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, + QtGui.QSizePolicy.Ignored)) + labelAction = self.addWidget(self.locLabel) + labelAction.setVisible(True) # reference holder for subplots_adjust window self.adj_window = None @@ -351,7 +315,9 @@ self.canvas.draw() def set_message( self, s ): - self.locLabel.setText( s.replace(', ', '\n') ) + self.emit(QtCore.SIGNAL("message"), s) + if self.coordinates: + self.locLabel.setText(s.replace(', ', '\n')) def set_cursor( self, cursor ): if DEBUG: print 'Set cursor' , cursor @@ -384,28 +350,6 @@ win.show() -# self.adj_window = QtGui.QDialog() -# win = self.adj_window -# win.setAttribute(QtCore.Qt.WA_DeleteOnClose) -# win.setWindowTitle("Subplot Configuration Tool") -# image = os.path.join( matplotlib.rcParams['datapath'],'images','matplotlib.png' ) -# win.setWindowIcon(QtGui.QIcon( image )) -# -# toolfig = Figure(figsize=(6,3)) -# toolfig.subplots_adjust(top=0.9) -# canvas = self._get_canvas(toolfig) -# tool = SubplotTool(self.canvas.figure, toolfig) -# -# canvas.setParent(win) -# w = int (toolfig.bbox.width()) -# h = int (toolfig.bbox.height()) -# -# win.resize(w, h) -# canvas.setFocus() -# -# canvas.show() -# win.show() - def _get_canvas(self, fig): return FigureCanvasQT(fig) @@ -425,7 +369,7 @@ selectedFilter = filter filters.append(filter) filters = ';;'.join(filters) - + fname = QtGui.QFileDialog.getSaveFileName( self, "Choose a filename to save to", start, filters, selectedFilter) if fname: Modified: trunk/matplotlib/lib/matplotlib/backends/backend_qt4agg.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backends/backend_qt4agg.py 2008-01-26 13:22:33 UTC (rev 4900) +++ trunk/matplotlib/lib/matplotlib/backends/backend_qt4agg.py 2008-01-27 15:17:28 UTC (rev 4901) @@ -35,7 +35,7 @@ # must be inited after the window, drawingArea and figure # attrs are set if matplotlib.rcParams['toolbar']=='classic': - print "Classic toolbar is not yet supported" + print "Classic toolbar is not supported" elif matplotlib.rcParams['toolbar']=='toolbar2': toolbar = NavigationToolbar2QTAgg(canvas, parent) else: This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |