class GEMPlotterDialog(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        # Set up the user interface from Designer.
        self.ui = Ui_GEMPlotter()
        self.ui.setupUi(self)
        self.modelfile = None
        self.ff = {}  # fragility functions dictionary
        self.fig = Figure()
        self.axes = self.fig.add_subplot(111)
        self.axes.grid(True)
        self.canvas = FigureCanvasQTAgg(self.fig)
        self.canvas.setParent(self)
        self.ui.plotLayout.addWidget(self.canvas)

    @QtCore.pyqtSlot()
    def on_cancelButton_clicked(self):
        self.close()

    @QtCore.pyqtSlot()
    def on_saveButton_clicked(self):
        choices = 'PNG (*.png)|*.png'
        path = unicode(QtGui.QFileDialog.getSaveFileName(
            self, 'Save plot', '', choices))
        self.canvas.print_figure(path)

    @QtCore.pyqtSlot(int)
    def plot_ff(self, taxonomy_idx):
        if taxonomy_idx <= 0:
            return
        self.axes.clear()
        taxonomy = str(self.ui.taxonomyCombo.itemText(taxonomy_idx))
        iml, ys = self.ff[taxonomy]
        for state, y in zip(self.states, ys):
            self.axes.plot(iml['imls'], y, label=state)
        self.axes.legend(loc='upper left')
        self.canvas.draw()
        self.ui.saveButton.setEnabled(True)

    @QtCore.pyqtSlot()
    def on_chooseButton_clicked(self):
        self.modelfile = unicode(QtGui.QFileDialog.getOpenFileName(
            self, 'Select Fragility Model file',
            QtCore.QDir.homePath(),
            'Model files (*.xml)'))
        # TODO: what to do if modelfile is empty?
        # what to do if the file is incorrect?
        self._fillCombo()
        self.ui.taxonomyCombo.currentIndexChanged.connect(self.plot_ff)

    def _fillCombo(self):
        p = iter(FragilityModelParser(self.modelfile))
        kind, self.states = next(p)
        self.ff = dict((taxonomy, (iml, y))
                       for taxonomy, iml, y, no_damage_limit in p)
        self.ui.taxonomyCombo.clear()
        self.ui.taxonomyCombo.addItems(['Taxonomy'] + self.ff.keys())
        self.ui.taxonomyCombo.setEnabled(True)
 def __init__(self):
     QtGui.QDialog.__init__(self)
     # Set up the user interface from Designer.
     self.ui = Ui_GEMPlotter()
     self.ui.setupUi(self)
     self.modelfile = None
     self.ff = {}  # fragility functions dictionary
     self.fig = Figure()
     self.axes = self.fig.add_subplot(111)
     self.axes.grid(True)
     self.canvas = FigureCanvasQTAgg(self.fig)
     self.canvas.setParent(self)
     self.ui.plotLayout.addWidget(self.canvas)