~ubuntu-branches/ubuntu/gutsy/matplotlib/gutsy

« back to all changes in this revision

Viewing changes to lib/matplotlib/backends/backend_qt4.py

  • Committer: Bazaar Package Importer
  • Author(s): Michael Bienia
  • Date: 2007-07-31 23:04:56 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070731230456-lfmr0h69yh1i37w1
Tags: 0.90.1-2ubuntu1
* Merge from Debian unstable. Remaining changes:
  + debian/rules:
    - Check only the files for the python version which just got installed
      (fixes FTBFS).
    - Modify Maintainer value to match DebianMaintainerField spec.

Show diffs side-by-side

added added

removed removed

Lines of Context:
50
50
        qApp = QtGui.QApplication( [" "] )
51
51
        QtCore.QObject.connect( qApp, QtCore.SIGNAL( "lastWindowClosed()" ),
52
52
                            qApp, QtCore.SLOT( "quit()" ) )
 
53
    else:
 
54
      # someone else aready created the qApp and
 
55
      # we let them handle the event-loop control.
 
56
      show._needmain = False
53
57
 
54
 
def show( mainloop=True ):
 
58
def show():
55
59
    """
56
60
    Show all the figures and enter the qt main loop
57
61
    This should be the last line of your script
64
68
    figManager =  Gcf.get_active()
65
69
    if figManager != None:
66
70
        figManager.canvas.draw()
67
 
        #if ( createQApp ):
68
 
        #   qtapplication.setMainWidget( figManager.canvas )
69
 
 
70
 
    if mainloop:
71
 
        qApp.exec_()
 
71
 
 
72
    if ( show._needmain ):
 
73
      qApp.exec_()
 
74
      show._needmain = False
 
75
 
 
76
show._needmain = True
72
77
 
73
78
 
74
79
def new_figure_manager( num, *args, **kwargs ):
143
148
 
144
149
    def _get_key( self, event ):
145
150
        if event.key() < 256:
146
 
            key = event.text().latin1()
 
151
            key = str(event.text())
147
152
        elif self.keyvald.has_key( event.key() ):
148
153
            key = self.keyvald[ event.key() ]
149
154
        else:
169
174
        self.window.setAttribute(QtCore.Qt.WA_DeleteOnClose)
170
175
        
171
176
        self.window.setWindowTitle("Figure %d" % num)
172
 
        image = os.path.join( matplotlib.rcParams['datapath'],'matplotlib.png' )
 
177
        image = os.path.join( matplotlib.rcParams['datapath'],'images','matplotlib.png' )
173
178
        self.window.setWindowIcon(QtGui.QIcon( image ))
174
179
 
175
180
        centralWidget = QtGui.QWidget( self.window )
197
202
 
198
203
        # Reset the window height so the canvas will be the right
199
204
        # size.  This ALMOST works right.  The first issue is that the
200
 
        # height w/ a toolbar seems to be off by just a little bit (so
201
 
        # we add 4 pixels).  The second is that the total width/height
 
205
        # reported toolbar height does not include the margin (so
 
206
        # we add the margin).  The second is that the total width/height
202
207
        # is slightly smaller that we actually want.  It seems like
203
208
        # the border of the window is being included in the size but
204
209
        # AFAIK there is no way to get that size.  
205
210
        w = self.canvas.width()
206
211
        h = self.canvas.height()
207
212
        if self.toolbar:
208
 
           h += self.toolbar.height() + 4
 
213
           h += self.toolbar.height() + NavigationToolbar2QT.margin
209
214
        self.window.resize( w, h )
210
215
        
211
216
        if matplotlib.is_interactive():
240
245
        if self.window._destroying: return
241
246
        self.window._destroying = True
242
247
        if DEBUG: print "destroy figure manager"
243
 
        self.window.close(True)
 
248
        self.window.close()
244
249
 
245
250
class NavigationToolbar2QT( NavigationToolbar2, QtGui.QWidget ):
246
251
    # list of toolitems to add to the toolbar, format is:
256
261
        ('Subplots', 'Configure subplots','subplots.png', 'configure_subplots'),
257
262
        ('Save', 'Save the figure','filesave.ppm', 'save_figure'),
258
263
        )
259
 
        
 
264
    
 
265
    margin = 12 # extra margin for the toolbar
 
266
    
260
267
    def __init__(self, canvas, parent):
261
268
        self.canvas = canvas
262
269
        QtGui.QWidget.__init__( self, parent )
269
276
        NavigationToolbar2.__init__( self, canvas )
270
277
        
271
278
    def _init_toolbar( self ):
272
 
        basedir = matplotlib.rcParams[ 'datapath' ]
 
279
        basedir = os.path.join(matplotlib.rcParams[ 'datapath' ],'images')
273
280
        
274
281
        for text, tooltip_text, image_file, callback in self.toolitems:
275
282
            if text == None:
285
292
 
286
293
            # The automatic layout doesn't look that good - it's too close
287
294
            # to the images so add a margin around it.
288
 
            margin = 4
 
295
            margin = self.margin
289
296
            button.setFixedSize( image.width()+margin, image.height()+margin )
290
297
 
291
298
            QtCore.QObject.connect( button, QtCore.SIGNAL( 'clicked()' ),
296
303
        # The stretch factor is 1 which means any resizing of the toolbar
297
304
        # will resize this label instead of the buttons.
298
305
        self.locLabel = QtGui.QLabel( "", self )
299
 
        self.locLabel.setAlignment( QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter )
 
306
        self.locLabel.setAlignment( QtCore.Qt.AlignRight | QtCore.Qt.AlignTop )
300
307
        self.locLabel.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Ignored,
301
308
                                                      QtGui.QSizePolicy.Ignored))
302
309
        self.layout.addWidget( self.locLabel, 1 )
327
334
        self.canvas.drawRectangle( rect )
328
335
        
329
336
    def configure_subplots(self):
330
 
        self.adj_window = QtGui.QDialog()
 
337
        self.adj_window = QtGui.QMainWindow()
331
338
        win = self.adj_window
332
339
        win.setAttribute(QtCore.Qt.WA_DeleteOnClose)
 
340
        
333
341
        win.setWindowTitle("Subplot Configuration Tool")
334
 
        image = os.path.join( matplotlib.rcParams['datapath'],'matplotlib.png' )
 
342
        image = os.path.join( matplotlib.rcParams['datapath'],'images','matplotlib.png' )
335
343
        win.setWindowIcon(QtGui.QIcon( image ))
336
 
 
337
 
        toolfig = Figure(figsize=(6,3))
338
 
        toolfig.subplots_adjust(top=0.9)
339
 
        canvas = self._get_canvas(toolfig)
340
 
        tool = SubplotTool(self.canvas.figure, toolfig)
341
 
 
342
 
        canvas.setParent(win)
343
 
        w = int (toolfig.bbox.width())
344
 
        h = int (toolfig.bbox.height())
345
 
 
346
 
        win.resize(w, h)
347
 
        canvas.setFocus()
 
344
        
 
345
        tool = SubplotToolQt(self.canvas.figure, win)
 
346
        win.setCentralWidget(tool)
 
347
        win.setSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred)
348
348
        
349
349
        win.show()
 
350
        
 
351
#        self.adj_window = QtGui.QDialog()
 
352
#        win = self.adj_window
 
353
#        win.setAttribute(QtCore.Qt.WA_DeleteOnClose)
 
354
#        win.setWindowTitle("Subplot Configuration Tool")
 
355
#        image = os.path.join( matplotlib.rcParams['datapath'],'images','matplotlib.png' )
 
356
#        win.setWindowIcon(QtGui.QIcon( image ))
 
357
#
 
358
#        toolfig = Figure(figsize=(6,3))
 
359
#        toolfig.subplots_adjust(top=0.9)
 
360
#        canvas = self._get_canvas(toolfig)
 
361
#        tool = SubplotTool(self.canvas.figure, toolfig)
 
362
#
 
363
#        canvas.setParent(win)
 
364
#        w = int (toolfig.bbox.width())
 
365
#        h = int (toolfig.bbox.height())
 
366
#
 
367
#        win.resize(w, h)
 
368
#        canvas.setFocus()
 
369
#        
 
370
#        canvas.show()
 
371
#        win.show()
350
372
    
351
373
    def _get_canvas(self, fig):
352
374
        return FigureCanvasQT(fig)
356
378
        if fname:
357
379
            self.canvas.print_figure( str(fname.toLatin1()) )
358
380
 
 
381
 
 
382
class SubplotToolQt( SubplotTool, QtGui.QWidget ):
 
383
    def __init__(self, targetfig, parent):
 
384
        QtGui.QWidget.__init__(self, None)
 
385
        
 
386
        self.targetfig = targetfig
 
387
        self.parent = parent
 
388
        
 
389
        self.sliderleft = QtGui.QSlider(QtCore.Qt.Horizontal)
 
390
        self.sliderbottom = QtGui.QSlider(QtCore.Qt.Vertical)
 
391
        self.sliderright = QtGui.QSlider(QtCore.Qt.Horizontal)
 
392
        self.slidertop = QtGui.QSlider(QtCore.Qt.Vertical)
 
393
        self.sliderwspace = QtGui.QSlider(QtCore.Qt.Horizontal)
 
394
        self.sliderhspace = QtGui.QSlider(QtCore.Qt.Vertical)
 
395
        
 
396
        # constraints
 
397
        QtCore.QObject.connect( self.sliderleft, QtCore.SIGNAL( "valueChanged(int)" ),
 
398
                                self.sliderright.setMinimum )
 
399
        QtCore.QObject.connect( self.sliderright, QtCore.SIGNAL( "valueChanged(int)" ),
 
400
                                self.sliderleft.setMaximum )
 
401
        QtCore.QObject.connect( self.sliderbottom, QtCore.SIGNAL( "valueChanged(int)" ),
 
402
                                self.slidertop.setMinimum )
 
403
        QtCore.QObject.connect( self.slidertop, QtCore.SIGNAL( "valueChanged(int)" ),
 
404
                                self.sliderbottom.setMaximum )
 
405
        
 
406
        sliders = (self.sliderleft, self.sliderbottom, self.sliderright,
 
407
                   self.slidertop, self.sliderwspace, self.sliderhspace, )
 
408
        adjustments = ('left:', 'bottom:', 'right:', 'top:', 'wspace:', 'hspace:')
 
409
        
 
410
        for slider, adjustment in zip(sliders, adjustments):
 
411
            slider.setMinimum(0)
 
412
            slider.setMaximum(1000)
 
413
            slider.setSingleStep(5)
 
414
 
 
415
        layout = QtGui.QGridLayout()
 
416
        
 
417
        leftlabel = QtGui.QLabel('left')
 
418
        layout.addWidget(leftlabel, 2, 0)
 
419
        layout.addWidget(self.sliderleft, 2, 1)
 
420
        
 
421
        toplabel = QtGui.QLabel('top')
 
422
        layout.addWidget(toplabel, 0, 2)
 
423
        layout.addWidget(self.slidertop, 1, 2)
 
424
        layout.setAlignment(self.slidertop, QtCore.Qt.AlignHCenter)
 
425
        
 
426
        bottomlabel = QtGui.QLabel('bottom')
 
427
        layout.addWidget(QtGui.QLabel('bottom'), 4, 2)
 
428
        layout.addWidget(self.sliderbottom, 3, 2)
 
429
        layout.setAlignment(self.sliderbottom, QtCore.Qt.AlignHCenter)
 
430
        
 
431
        rightlabel = QtGui.QLabel('right')
 
432
        layout.addWidget(rightlabel, 2, 4)
 
433
        layout.addWidget(self.sliderright, 2, 3)
 
434
        
 
435
        hspacelabel = QtGui.QLabel('hspace')
 
436
        layout.addWidget(hspacelabel, 0, 6)
 
437
        layout.setAlignment(hspacelabel, QtCore.Qt.AlignHCenter)
 
438
        layout.addWidget(self.sliderhspace, 1, 6)
 
439
        layout.setAlignment(self.sliderhspace, QtCore.Qt.AlignHCenter)
 
440
        
 
441
        wspacelabel = QtGui.QLabel('wspace')
 
442
        layout.addWidget(wspacelabel, 4, 6)
 
443
        layout.setAlignment(wspacelabel, QtCore.Qt.AlignHCenter)
 
444
        layout.addWidget(self.sliderwspace, 3, 6)
 
445
        layout.setAlignment(self.sliderwspace, QtCore.Qt.AlignBottom)
 
446
        
 
447
        layout.setRowStretch(1,1)
 
448
        layout.setRowStretch(3,1)
 
449
        layout.setColumnStretch(1,1)
 
450
        layout.setColumnStretch(3,1)
 
451
        layout.setColumnStretch(6,1)
 
452
        
 
453
        self.setLayout(layout)
 
454
        
 
455
        self.sliderleft.setSliderPosition(int(targetfig.subplotpars.left*1000))
 
456
        self.sliderbottom.setSliderPosition(int(targetfig.subplotpars.bottom*1000))
 
457
        self.sliderright.setSliderPosition(int(targetfig.subplotpars.right*1000))
 
458
        self.slidertop.setSliderPosition(int(targetfig.subplotpars.top*1000))
 
459
        self.sliderwspace.setSliderPosition(int(targetfig.subplotpars.wspace*1000))
 
460
        self.sliderhspace.setSliderPosition(int(targetfig.subplotpars.hspace*1000))
 
461
        
 
462
        QtCore.QObject.connect( self.sliderleft, QtCore.SIGNAL( "valueChanged(int)" ),
 
463
                                self.funcleft )
 
464
        QtCore.QObject.connect( self.sliderbottom, QtCore.SIGNAL( "valueChanged(int)" ),
 
465
                                self.funcbottom )
 
466
        QtCore.QObject.connect( self.sliderright, QtCore.SIGNAL( "valueChanged(int)" ),
 
467
                                self.funcright )
 
468
        QtCore.QObject.connect( self.slidertop, QtCore.SIGNAL( "valueChanged(int)" ),
 
469
                                self.functop )
 
470
        QtCore.QObject.connect( self.sliderwspace, QtCore.SIGNAL( "valueChanged(int)" ),
 
471
                                self.funcwspace )
 
472
        QtCore.QObject.connect( self.sliderhspace, QtCore.SIGNAL( "valueChanged(int)" ),
 
473
                                self.funchspace )
 
474
 
 
475
    def funcleft(self, val):
 
476
        self.targetfig.subplots_adjust(left=val/1000.)
 
477
        if self.drawon: self.targetfig.canvas.draw()
 
478
 
 
479
    def funcright(self, val):
 
480
        self.targetfig.subplots_adjust(right=val/1000.)
 
481
        if self.drawon: self.targetfig.canvas.draw()
 
482
 
 
483
    def funcbottom(self, val):
 
484
        self.targetfig.subplots_adjust(bottom=val/1000.)
 
485
        if self.drawon: self.targetfig.canvas.draw()
 
486
 
 
487
    def functop(self, val):
 
488
        self.targetfig.subplots_adjust(top=val/1000.)
 
489
        if self.drawon: self.targetfig.canvas.draw()
 
490
 
 
491
    def funcwspace(self, val):
 
492
        self.targetfig.subplots_adjust(wspace=val/1000.)
 
493
        if self.drawon: self.targetfig.canvas.draw()
 
494
 
 
495
    def funchspace(self, val):
 
496
        self.targetfig.subplots_adjust(hspace=val/1000.)
 
497
        if self.drawon: self.targetfig.canvas.draw()
 
498
        
 
499
 
359
500
def error_msg_qt( msg, parent=None ):
360
501
    if not is_string_like( msg ):
361
502
        msg = ','.join( map( str,msg ) )