from PySide import QtCore, QtGui, QtDeclarative

class OtherWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(OtherWindow, self).__init__(parent)

        self.setWindowTitle('other window')

        self.menuFile = self.menuBar().addMenu('&File')
        self.menuFile.addAction('&graph').triggered.connect(self.make_graph)
        self.menuFile.addAction('&label').triggered.connect(self.make_label)
        self.menuFile.addAction('E&xit').triggered.connect(self.close)

        w = QtGui.QWidget()
        l = QtGui.QVBoxLayout(w)
        self.layout = l

        self.setCentralWidget(w)

    def clear(self):
        while self.layout.count() > 0:
            w = self.layout.takeAt(0)
            w.widget().hide()

    def make_label(self):
        self.clear()
        self.layout.addWidget(QtGui.QLabel('something or other'))

    def make_graph(self):
        self.clear()

        # enthought stuff auto-creates a QApplication
        # we import it here so that it uses ours
        from enable.api import Window
        from chaco.api import ArrayPlotData, Plot

        self.plotdata = ArrayPlotData()
        self.plot = Plot(self.plotdata, padding=50, border_visible=True)
        self.chacoWindow = Window(self, -1, component=self.plot)
        self.plotWidget = self.chacoWindow.control

        self.layout.addWidget(self.plotWidget)


class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.declview = QtDeclarative.QDeclarativeView()
        self.setCentralWidget(self.declview)
        self.context = self.declview.rootContext()
        self.context.setContextProperty('controller', self)

        self.menuFile = self.menuBar().addMenu('&File')
        self.menuFile.addAction('&Launch').triggered.connect(self.other)
        self.menuFile.addAction('E&xit').triggered.connect(self.close)

    def other(self):
        w2 = OtherWindow()
        w2.show()
        self.others = w2

    def closeEvent(self, event):
        # This next line clearing the assumed reference loop allows the program
        # to close cleanly.
        #self.context.setContextProperty('controller', None)
        super(MainWindow, self).closeEvent(event)

def thing():
    w = MainWindow()
    w.show()

app = QtGui.QApplication([])
thing()
app.exec_()
