CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/RecoLuminosity/LumiDB/python/lumiQTWidget.py

Go to the documentation of this file.
00001 import sys,os
00002 from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
00003 from PyQt4 import QtGui, QtCore
00004 
00005     
00006 class LumiCanvas(FigureCanvas):
00007     """this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
00008     def __init__(self, parent=None, fig=None):
00009         FigureCanvas.__init__(self, fig)
00010         self.setParent(parent)
00011         FigureCanvas.setSizePolicy(self,
00012                                    QtGui.QSizePolicy.Expanding,
00013                                    QtGui.QSizePolicy.Expanding)
00014         FigureCanvas.updateGeometry(self)
00015         
00016 class ApplicationWindow(QtGui.QMainWindow):
00017     '''
00018     main evt loop
00019     '''
00020     def __init__(self,fig=None):
00021         self.qApp=QtGui.QApplication(sys.argv)#every PyQt4 application must create an application object
00022         QtGui.QMainWindow.__init__(self)
00023         self.setWindowTitle('lumi plot')
00024         self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
00025         self.main_widget = QtGui.QWidget(self)
00026         l = QtGui.QVBoxLayout(self.main_widget)
00027         sc = LumiCanvas(self.main_widget,fig=fig)
00028         bn = QtGui.QPushButton("Save to File",self.main_widget)
00029         bn.clicked.connect(lambda:self.saveAs(fig))
00030         l.addWidget(sc)
00031         l.addWidget(bn)
00032         self.main_widget.setFocus()
00033         self.setCentralWidget(self.main_widget)
00034         
00035     def saveAs(self,fig):
00036         filename=QtGui.QFileDialog.getSaveFileName(self,"Save plot as file","","(*.png)")
00037         if filename == "": return
00038         fig.savefig(filename,format="PNG")                         
00039     def fileQuit(self):
00040         self.close()
00041     def closeEvent(self, ce):
00042         self.fileQuit()
00043     def destroy(self):
00044         sys.exit(self.qApp.exec_())
00045     
00046 if __name__ == "__main__":
00047     from numpy import arange, sin, pi
00048     from matplotlib.figure import Figure
00049     fig=Figure(figsize=(7.2,5.4),dpi=120)#create fig
00050     t = arange(0.0,3.0,0.01)
00051     s = sin(2*pi*t)
00052     ax=fig.add_subplot(111)
00053     ax.plot(t,s)
00054     aw=ApplicationWindow(fig=fig)
00055     aw.show()
00056     aw.destroy()