Skip to content

Commit

Permalink
Merge pull request #2382 from tacaswell/new_stlye_qt
Browse files Browse the repository at this point in the history
New stlye qt calls
  • Loading branch information
mdboom committed Sep 10, 2013
2 parents da6c6b5 + df3d882 commit b4c9ced
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 89 deletions.
13 changes: 6 additions & 7 deletions examples/user_interfaces/embedding_in_qt4.py
Expand Up @@ -10,7 +10,9 @@
# may be distributed without limitation.

from __future__ import unicode_literals
import sys, os, random
import sys
import os
import random
from matplotlib.backends import qt4_compat
use_pyside = qt4_compat.QT_API == qt4_compat.QT_API_PYSIDE
if use_pyside:
Expand Down Expand Up @@ -62,18 +64,15 @@ class MyDynamicMplCanvas(MyMplCanvas):
def __init__(self, *args, **kwargs):
MyMplCanvas.__init__(self, *args, **kwargs)
timer = QtCore.QTimer(self)
if use_pyside:
timer.timeout.connect(self.update_figure)
else:
QtCore.QObject.connect(timer, QtCore.SIGNAL("timeout()"), self.update_figure)
timer.timeout.connect(self.update_figure)
timer.start(1000)

def compute_initial_figure(self):
self.axes.plot([0, 1, 2, 3], [1, 2, 0, 4], 'r')
self.axes.plot([0, 1, 2, 3], [1, 2, 0, 4], 'r')

def update_figure(self):
# Build a list of 4 random integers between 0 and 10 (both inclusive)
l = [ random.randint(0, 10) for i in range(4) ]
l = [random.randint(0, 10) for i in range(4)]

self.axes.plot([0, 1, 2, 3], l, 'r')
self.draw()
Expand Down
74 changes: 26 additions & 48 deletions lib/matplotlib/backends/backend_qt4.py
Expand Up @@ -82,9 +82,8 @@ def _create_qApp():
if display is None or not re.search(':\d', display):
raise RuntimeError('Invalid DISPLAY variable')

qApp = QtGui.QApplication([" "])
QtCore.QObject.connect(qApp, QtCore.SIGNAL("lastWindowClosed()"),
qApp, QtCore.SLOT("quit()"))
qApp = QtGui.QApplication([str(" ")])
qApp.lastWindowClosed.connect(qApp.quit)
else:
qApp = app

Expand Down Expand Up @@ -134,17 +133,15 @@ def __init__(self, *args, **kwargs):
# Create a new timer and connect the timeout() signal to the
# _on_timer method.
self._timer = QtCore.QTimer()
QtCore.QObject.connect(self._timer, QtCore.SIGNAL('timeout()'),
self._on_timer)
self._timer.timeout.connect(self._on_timer)
self._timer_set_interval()

def __del__(self):
# Probably not necessary in practice, but is good behavior to
# disconnect
try:
TimerBase.__del__(self)
QtCore.QObject.disconnect(self._timer,
QtCore.SIGNAL('timeout()'), self._on_timer)
self._timer.timeout.disconnect(self._on_timer)
except RuntimeError:
# Timer C++ object already deleted
pass
Expand Down Expand Up @@ -423,8 +420,10 @@ def idle_draw(*args):


class MainWindow(QtGui.QMainWindow):
closing = QtCore.Signal()

def closeEvent(self, event):
self.emit(QtCore.SIGNAL('closing()'))
self.closing.emit()
QtGui.QMainWindow.closeEvent(self, event)


Expand All @@ -444,10 +443,8 @@ def __init__(self, canvas, num):
FigureManagerBase.__init__(self, canvas, num)
self.canvas = canvas
self.window = MainWindow()
self.window.connect(self.window, QtCore.SIGNAL('closing()'),
canvas.close_event)
self.window.connect(self.window, QtCore.SIGNAL('closing()'),
self._widgetclosed)
self.window.closing.connect(canvas.close_event)
self.window.closing.connect(self._widgetclosed)

self.window.setWindowTitle("Figure %d" % num)
image = os.path.join(matplotlib.rcParams['datapath'],
Expand All @@ -469,8 +466,7 @@ def __init__(self, canvas, num):
self.toolbar = self._get_toolbar(self.canvas, self.window)
if self.toolbar is not None:
self.window.addToolBar(self.toolbar)
QtCore.QObject.connect(self.toolbar, QtCore.SIGNAL("message"),
self._show_message)
self.toolbar.message.connect(self._show_message)
tbs_height = self.toolbar.sizeHint().height()
else:
tbs_height = 0
Expand Down Expand Up @@ -540,8 +536,8 @@ def destroy(self, *args):
if self.window._destroying:
return
self.window._destroying = True
QtCore.QObject.disconnect(self.window, QtCore.SIGNAL('destroyed()'),
self._widgetclosed)
self.window.destroyed.connect(self._widgetclosed)

if self.toolbar:
self.toolbar.destroy()
if DEBUG:
Expand All @@ -556,6 +552,8 @@ def set_window_title(self, title):


class NavigationToolbar2QT(NavigationToolbar2, QtGui.QToolBar):
message = QtCore.Signal(str)

def __init__(self, canvas, parent, coordinates=True):
""" coordinates: should we show the coordinates on the right? """
self.canvas = canvas
Expand Down Expand Up @@ -656,7 +654,7 @@ def dynamic_update(self):
self.canvas.draw()

def set_message(self, s):
self.emit(QtCore.SIGNAL("message"), s)
self.message.emit(s)
if self.coordinates:
self.locLabel.setText(s.replace(', ', '\n'))

Expand Down Expand Up @@ -746,18 +744,10 @@ def __init__(self, targetfig, parent):
self.sliderhspace = QtGui.QSlider(QtCore.Qt.Vertical)

# constraints
QtCore.QObject.connect(self.sliderleft,
QtCore.SIGNAL("valueChanged(int)"),
self.sliderright.setMinimum)
QtCore.QObject.connect(self.sliderright,
QtCore.SIGNAL("valueChanged(int)"),
self.sliderleft.setMaximum)
QtCore.QObject.connect(self.sliderbottom,
QtCore.SIGNAL("valueChanged(int)"),
self.slidertop.setMinimum)
QtCore.QObject.connect(self.slidertop,
QtCore.SIGNAL("valueChanged(int)"),
self.sliderbottom.setMaximum)
self.sliderleft.valueChanged.connect(self.sliderright.setMinimum)
self.sliderright.valueChanged.connect(self.sliderleft.setMaximum)
self.sliderbottom.valueChanged.connect(self.slidertop.setMinimum)
self.slidertop.valueChanged.connect(self.sliderbottom.setMaximum)

sliders = (self.sliderleft, self.sliderbottom, self.sliderright,
self.slidertop, self.sliderwspace, self.sliderhspace,)
Expand All @@ -781,7 +771,7 @@ def __init__(self, targetfig, parent):
layout.setAlignment(self.slidertop, QtCore.Qt.AlignHCenter)

bottomlabel = QtGui.QLabel('bottom') # this might not ever be used
layout.addWidget(QtGui.QLabel('bottom'), 4, 2)
layout.addWidget(bottomlabel, 4, 2)
layout.addWidget(self.sliderbottom, 3, 2)
layout.setAlignment(self.sliderbottom, QtCore.Qt.AlignHCenter)

Expand Down Expand Up @@ -820,24 +810,12 @@ def __init__(self, targetfig, parent):
self.sliderhspace.setSliderPosition(
int(targetfig.subplotpars.hspace*1000))

QtCore.QObject.connect(self.sliderleft,
QtCore.SIGNAL("valueChanged(int)"),
self.funcleft)
QtCore.QObject.connect(self.sliderbottom,
QtCore.SIGNAL("valueChanged(int)"),
self.funcbottom)
QtCore.QObject.connect(self.sliderright,
QtCore.SIGNAL("valueChanged(int)"),
self.funcright)
QtCore.QObject.connect(self.slidertop,
QtCore.SIGNAL("valueChanged(int)"),
self.functop)
QtCore.QObject.connect(self.sliderwspace,
QtCore.SIGNAL("valueChanged(int)"),
self.funcwspace)
QtCore.QObject.connect(self.sliderhspace,
QtCore.SIGNAL("valueChanged(int)"),
self.funchspace)
self.sliderleft.valueChanged.connect(self.funcleft)
self.sliderbottom.valueChanged.connect(self.funcbottom)
self.sliderright.valueChanged.connect(self.funcright)
self.slidertop.valueChanged.connect(self.functop)
self.sliderwspace.valueChanged.connect(self.funcwspace)
self.sliderhspace.valueChanged.connect(self.funchspace)

def funcleft(self, val):
if val == self.sliderright.value():
Expand Down

0 comments on commit b4c9ced

Please sign in to comment.