Exemple #1
0
 def set_from(self, project, output):
     self.project = project
     self.output = output
     self.report = Reports(project, output)
     self.cboTime.clear()
     if project and self.output:
         for time_index in range(0, self.output.num_periods):
             self.cboTime.addItem(self.output.get_time_string(time_index))
         self.rbnNodes.setChecked(True)
         self.rbnNodes_Clicked()
Exemple #2
0
    def set_from(self, project, output):
        self.project = project
        self.output = output
        self.report = Reports(project, output)
        self.cboTime.clear()
        if self.project and self.output:
            for time_index in range(0, self.output.num_periods):
                #if self.???:  TODO: give choice between elapsed hours and date
                self.cboTime.addItem(self.output.get_time_string(time_index))
                #else:
                #    self.cboTime.addItem(self.output.get_date_string(time_index))

            self.rbnNodes.setChecked(True)
            self.rbnNodes_Clicked()
    def set_from(self, project, output):
        self.project = project
        self.output = output
        self.report = Reports(project, output)
        self.cboTime.clear()
        if self.project and self.output:
            for time_index in range(0, self.output.numPeriods):
                #if self.???:
                    self.cboTime.addItem(self.output.get_time_string(time_index))
                #else:
                #    self.cboTime.addItem(self.output.get_date_string(time_index))

            self.rbnNodes.setChecked(True)
            self.rbnNodes_Clicked()
Exemple #4
0
class frmTable(QtGui.QMainWindow, Ui_frmTable):

    def __init__(self, main_form):
        QtGui.QMainWindow.__init__(self, main_form)
        self.help_topic = "epanet/src/src/Table_Op.htm"
        self.setupUi(self)
        self.tabWidget.setCurrentIndex(0)
        self.cboCompare.addItems(('Below', 'Equal To', 'Above'))
        self.cmdOK.clicked.connect(self.cmdOK_Clicked)
        self.cmdCancel.clicked.connect(self.cmdCancel_Clicked)
        self.cmdAdd.clicked.connect(self.cmdAdd_Clicked)
        self.cmdDelete.clicked.connect(self.cmdDelete_Clicked)
        self.rbnNodes.clicked.connect(self.rbnNodes_Clicked)
        self.rbnLinks.clicked.connect(self.rbnLinks_Clicked)
        self.rbnTimeseriesNode.clicked.connect(self.rbnTimeseriesNode_Clicked)
        self.rbnTimeseriesLink.clicked.connect(self.rbnTimeseriesLink_Clicked)
        self._main_form = main_form
        self.item_type = ENR_node_type
        self.forms = []

    def set_from(self, project, output):
        self.project = project
        self.output = output
        self.report = Reports(project, output)
        self.cboTime.clear()
        if self.project and self.output:
            for time_index in range(0, self.output.num_periods):
                #if self.???:  TODO: give choice between elapsed hours and date
                self.cboTime.addItem(self.output.get_time_string(time_index))
                #else:
                #    self.cboTime.addItem(self.output.get_date_string(time_index))

            self.rbnNodes.setChecked(True)
            self.rbnNodes_Clicked()

    def rbnNodes_Clicked(self):
        if self.rbnNodes.isChecked():
            self.item_type = ENR_node_type
            self.set_attributes()
            self.cbxSort.setEnabled(True)
            self.cbxSort.setText(transl8("frmTable", "Sort by Elevation", None))
            self.rbnLinks.setChecked(False)
            self.rbnTimeseriesNode.setChecked(False)
            self.rbnTimeseriesLink.setChecked(False)

    def rbnLinks_Clicked(self):
        if self.rbnLinks.isChecked():
            self.item_type = ENR_link_type
            self.set_attributes()
            self.cbxSort.setEnabled(True)
            self.cbxSort.setText(transl8("frmTable", "Sort by Length", None))
            self.rbnNodes.setChecked(False)
            self.rbnTimeseriesNode.setChecked(False)
            self.rbnTimeseriesLink.setChecked(False)

    def rbnTimeseriesNode_Clicked(self):
        if self.rbnTimeseriesNode.isChecked():
            self.item_type = ENR_node_type
            self.set_attributes()
            self.cbxSort.setEnabled(False)
            self.cbxSort.setText(transl8("frmTable", "Sort by", None))
            self.rbnNodes.setChecked(False)
            self.rbnLinks.setChecked(False)
            self.rbnTimeseriesLink.setChecked(False)

    def rbnTimeseriesLink_Clicked(self):
        if self.rbnTimeseriesLink.isChecked():
            self.item_type = ENR_link_type
            self.set_attributes()
            self.cbxSort.setEnabled(False)
            self.cbxSort.setText(transl8("frmTable", "Sort by", None))
            self.rbnNodes.setChecked(False)
            self.rbnLinks.setChecked(False)
            self.rbnTimeseriesNode.setChecked(False)

    def set_attributes(self):
        self.lstColumns.clear()
        attribute_names = [attribute.name for attribute in self.item_type.Attributes]
        self.lstColumns.addItems(attribute_names)
        self.lstColumns.selectAll()
        self.cboFilter.clear()
        self.cboFilter.addItems(attribute_names)

    def cmdOK_Clicked(self):
        row_headers = []
        column_headers = []
        attributes = []
        quality_name = ""
        if hasattr(self.item_type, "AttributeQuality"):
            quality_name = self.item_type.AttributeQuality.name

        for column_index in self.lstColumns.selectedIndexes():
            attribute_index = column_index.row()
            if attribute_index < len(self.item_type.Attributes):
                attribute = self.item_type.Attributes[attribute_index]
                attributes.append(attribute)
                column_header = attribute.name
                units = ""
                if attribute.name == quality_name:
                    if self.project.options.quality.quality.value == 2: # 'chemical'
                        column_header = "Chemical"
                        units = attribute.units(self.output.unit_system)
                    elif self.project.options.quality.quality.value == 3: # 'Age'
                        column_header = "Age"
                        units = "hours"
                    elif self.project.options.quality.quality.value == 4:  # 'Trace'
                        column_header = "Trace " + self.project.options.quality.trace_node
                        units = "percent"
                else:
                    units = attribute.units(self.output.unit_system)
                if units:
                    column_header += '\n(' + units + ')'
                column_headers.append(column_header)
            else:
                print ("Did not find attribute " + attribute_index)

        frm = frmGenericListOutput(self._main_form, "EPANET Table Output")
        if self.rbnNodes.isChecked() or self.rbnLinks.isChecked():
            items_in_order = list()
            if self.rbnNodes.isChecked():
                for item in self.report.all_nodes_set_category():
                    items_in_order.append(item)
                    row_headers.append(item.category + ' ' + item.name)
            else:
                for item in self.report.all_links_set_category():
                    items_in_order.append(item)
                    row_headers.append(item.category + ' ' + item.name)
            time_index = self.cboTime.currentIndex()
            title = "Network Table - " + self.item_type.TypeLabel + "s at "+ self.output.get_time_string(time_index)
            self.make_table(frm, title, time_index, items_in_order,
                            row_headers, attributes, column_headers)
        else:
            name = self.txtNodeLink.text()  # TODO: turn this textbox into combo box or list?
            if self.rbnTimeseriesNode.isChecked():
                item = self.output.nodes[name]
            else:
                item = self.output.links[name]
            title = "Time Series Table - " + self.item_type.TypeLabel + ' ' + name
            self.make_timeseries_table(frm, title, item, attributes, column_headers)
        frm.tblGeneric.resizeColumnsToContents()
        frm.show()
        frm.update()
        self.forms.append(frm)
        # self.close()

    def make_table(self, frm, title, time_index, items, row_headers, attributes, column_headers):
        frm.setWindowTitle(title)
        tbl = frm.tblGeneric

        tbl.setRowCount(len(row_headers))
        tbl.setColumnCount(len(column_headers))
        tbl.setHorizontalHeaderLabels(column_headers)
        tbl.setVerticalHeaderLabels(row_headers)

        row = 0
        for this_item in items:
            col = 0
            values = this_item.get_all_attributes_at_time(self.output, time_index)
            for attribute in attributes:
                val_str = attribute.str(values[attribute.index])
                table_cell_widget = QtGui.QTableWidgetItem(val_str)
                table_cell_widget.setFlags(QtCore.Qt.ItemIsSelectable)  # | QtCore.Qt.ItemIsEnabled)
                table_cell_widget.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignRight)
                tbl.setItem(row, col, table_cell_widget)
                col += 1
            row += 1
            if row == 20:
                tbl.resizeColumnsToContents()
                frm.show()
                frm.update()
                process_events()

    def make_timeseries_table(self, frm, title, item, attributes, column_headers):
        frm.setWindowTitle(title)
        row_headers = []
        for time_index in range(0, self.output.num_periods):
            row_headers.append(self.output.get_time_string(time_index))
        tbl = frm.tblGeneric
        tbl.setRowCount(len(row_headers))
        tbl.setColumnCount(len(column_headers))
        tbl.setHorizontalHeaderLabels(column_headers)
        tbl.setVerticalHeaderLabels(row_headers)

        if item:
            row = 0
            for time_index in range(0, self.output.num_periods):
                col = 0
                values = item.get_all_attributes_at_time(self.output, time_index)
                for attribute in attributes:
                    val_str = attribute.str(values[attribute.index])
                    table_cell_widget = QtGui.QTableWidgetItem(val_str)
                    table_cell_widget.setFlags(QtCore.Qt.ItemIsSelectable)  # | QtCore.Qt.ItemIsEnabled)
                    table_cell_widget.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignRight)
                    tbl.setItem(row, col, table_cell_widget)
                    col += 1
                row += 1

    def cmdCancel_Clicked(self):
        self.close()

    def cmdAdd_Clicked(self):
        qty = self.cboFilter.currentText()
        comp = self.cboCompare.currentText()
        val = self.txtValue.text()
        entry = qty + " " + comp + " " + val
        if self.lstFilter.count() == 0:
            self.lstFilter.addItem(entry)
        else:
            if not self.lstFilter.findItems(entry, QtCore.Qt.MatchCaseSensitive):
                self.lstFilter.addItem(entry)
        pass

    def cmdDelete_Clicked(self):
        if not self.lstFilter.selectedItems(): return
        for wi in self.lstFilter.selectedItems():
            self.lstFilter.takeItem(self.lstFilter.row(wi))
        pass
Exemple #5
0
class frmGraph(QtGui.QMainWindow, Ui_frmGraph):
    def __init__(self, main_form):
        QtGui.QMainWindow.__init__(self, main_form)
        self.help_topic = "epanet/src/src/Graph_Se.htm"
        self.setupUi(self)
        self.cmdAdd.setVisible(False)
        self.cmdDelete.setVisible(False)
        self.cmdUp.setVisible(False)
        self.cmdDown.setVisible(False)
        QtCore.QObject.connect(self.cmdOK, QtCore.SIGNAL("clicked()"),
                               self.cmdOK_Clicked)
        QtCore.QObject.connect(self.cmdCancel, QtCore.SIGNAL("clicked()"),
                               self.cmdCancel_Clicked)
        QtCore.QObject.connect(self.rbnNodes, QtCore.SIGNAL("clicked()"),
                               self.rbnNodes_Clicked)
        QtCore.QObject.connect(self.rbnLinks, QtCore.SIGNAL("clicked()"),
                               self.rbnLinks_Clicked)

        QtCore.QObject.connect(self.rbnTime, QtCore.SIGNAL("clicked()"),
                               self.rbnTime_Clicked)
        QtCore.QObject.connect(self.rbnProfile, QtCore.SIGNAL("clicked()"),
                               self.rbnProfile_Clicked)
        QtCore.QObject.connect(self.rbnContour, QtCore.SIGNAL("clicked()"),
                               self.rbnContour_Clicked)
        QtCore.QObject.connect(self.rbnFrequency, QtCore.SIGNAL("clicked()"),
                               self.rbnFrequency_Clicked)
        QtCore.QObject.connect(self.rbnSystem, QtCore.SIGNAL("clicked()"),
                               self.rbnSystem_Clicked)

        self.cboTime.currentIndexChanged.connect(
            self.cboTime_currentIndexChanged)

        self.lstToGraph.setSelectionMode(
            QtGui.QAbstractItemView.ExtendedSelection)
        self._main_form = main_form
        self.time_linked_graphs = []

    def set_from(self, project, output):
        self.project = project
        self.output = output
        self.report = Reports(project, output)
        self.cboTime.clear()
        if project and self.output:
            for time_index in range(0, self.output.num_periods):
                self.cboTime.addItem(self.output.get_time_string(time_index))
            self.rbnNodes.setChecked(True)
            self.rbnNodes_Clicked()

    def rbnNodes_Clicked(self):
        if self.rbnNodes.isChecked():
            self.gbxToGraph.setTitle(
                transl8("frmGraph", "Nodes to Graph", None))
            self.cboParameter.clear()
            self.cboParameter.addItems(
                [att.name for att in ENR_node_type.Attributes])
            self.lstToGraph.clear()
            self.list_items = self.output.nodes
            self.lstToGraph.addItems(self.list_items.keys())

    def rbnLinks_Clicked(self):
        if self.rbnLinks.isChecked():
            self.gbxToGraph.setTitle(
                transl8("frmGraph", "Links to Graph", None))
            self.cboParameter.clear()
            self.cboParameter.addItems(
                [att.name for att in ENR_link_type.Attributes])
            self.lstToGraph.clear()
            self.list_items = self.output.links
            self.lstToGraph.addItems(self.list_items.keys())

    def rbnTime_Clicked(self):
        self.cboParameter.setEnabled(True)
        self.gbxParameter.setEnabled(True)
        self.cboTime.setEnabled(False)
        self.gbxTime.setEnabled(False)
        self.gbxObject.setEnabled(True)
        self.rbnNodes.setEnabled(True)
        self.rbnLinks.setEnabled(True)
        self.gbxToGraph.setEnabled(True)
        self.lstToGraph.setEnabled(True)

    def rbnProfile_Clicked(self):
        self.cboParameter.setEnabled(True)
        self.gbxParameter.setEnabled(True)
        self.cboTime.setEnabled(True)
        self.gbxTime.setEnabled(True)
        self.gbxObject.setEnabled(False)
        # self.rbnNodes.setChecked(True)
        # self.rbnNodes.setEnabled(False)
        # self.rbnLinks.setEnabled(False)
        self.gbxToGraph.setEnabled(True)
        self.lstToGraph.setEnabled(True)

    def rbnContour_Clicked(self):
        self.cboParameter.setEnabled(True)
        self.gbxParameter.setEnabled(True)
        self.cboTime.setEnabled(True)
        self.gbxTime.setEnabled(True)
        self.gbxObject.setEnabled(False)
        self.rbnNodes.setEnabled(False)
        self.rbnLinks.setEnabled(False)
        self.gbxToGraph.setEnabled(False)
        self.lstToGraph.setEnabled(False)

    def rbnFrequency_Clicked(self):
        self.cboParameter.setEnabled(True)
        self.gbxParameter.setEnabled(True)
        self.cboTime.setEnabled(True)
        self.gbxTime.setEnabled(True)
        self.gbxObject.setEnabled(True)
        self.rbnNodes.setEnabled(True)
        self.rbnLinks.setEnabled(True)
        self.gbxToGraph.setEnabled(False)
        self.lstToGraph.setEnabled(False)

    def rbnSystem_Clicked(self):
        self.cboParameter.setEnabled(False)
        self.gbxParameter.setEnabled(False)
        self.cboTime.setEnabled(False)
        self.gbxTime.setEnabled(False)
        self.gbxObject.setEnabled(False)
        self.rbnNodes.setEnabled(False)
        self.rbnLinks.setEnabled(False)
        self.gbxToGraph.setEnabled(False)
        self.lstToGraph.setEnabled(False)

    def cboTime_currentIndexChanged(self):
        time_index = self.cboTime.currentIndex()
        if time_index >= 0:
            for graph in self.time_linked_graphs:
                graph[-1] = time_index
                graph[0](*graph[1:])

    def cmdOK_Clicked(self):
        parameter_label = self.cboParameter.currentText()
        if self.rbnNodes.isChecked():
            attribute = ENR_node_type.get_attribute_by_name(parameter_label)
        else:
            attribute = ENR_link_type.get_attribute_by_name(parameter_label)

        time_index = self.cboTime.currentIndex()

        if self.rbnTime.isChecked(
        ):  # TODO: use get_series instead of get_value if it is more efficient
            graphEPANET.plot_time(self.output, attribute,
                                  self.selected_items())

        if self.rbnSystem.isChecked():
            graphEPANET.plot_system_flow(self.output)

        if time_index < 0 and (self.rbnProfile.isChecked()
                               or self.rbnFrequency.isChecked()):
            QMessageBox.information(
                None, self._main_form.model,
                "There is no time step currently selected.", QMessageBox.Ok)
        else:
            if self.rbnProfile.isChecked():
                items = self.selected_items()
                if len(
                        items
                ) < 2:  # if fewer than two items were selected, use all items
                    items = self.list_items.values()
                self.plot_profile(attribute, time_index, items)
            if self.rbnFrequency.isChecked():
                graphEPANET.plot_freq(self.output, attribute, time_index,
                                      self.list_items.values())

    def selected_items(self):
        names = selected_list_items(self.lstToGraph)
        items = []
        for name in names:
            items.append(self.list_items[name])
        return items

    def plot_profile(self, attribute, time_index, items):
        fig = plt.figure()
        if self.rbnNodes.isChecked():
            x_values = self.report.node_distances(items)
        else:
            x_values = range(0, len(items))
        self.time_linked_graphs.append([
            graphEPANET.update_profile, self.output, items, x_values,
            attribute, fig.number, time_index
        ])
        graphEPANET.update_profile(self.output, items, x_values, attribute,
                                   fig.number, time_index)

    def cmdCancel_Clicked(self):
        self.close()
Exemple #6
0
class frmGraph(QMainWindow, Ui_frmGraph):
    def __init__(self, main_form):
        QMainWindow.__init__(self, main_form)
        self.help_topic = "epanet/src/src/Graph_Se.htm"
        self.setupUi(self)
        self.cmdAdd.setVisible(True)
        self.cmdDelete.setVisible(True)
        self.cmdUp.setVisible(True)
        self.cmdDown.setVisible(True)
        self.cmdOK.clicked.connect(self.cmdOK_Clicked)
        self.cmdCancel.clicked.connect(self.cmdCancel_Clicked)
        self.rbnNodes.clicked.connect(self.rbnNodes_Clicked)
        self.rbnLinks.clicked.connect(self.rbnLinks_Clicked)
        self.rbnTime.clicked.connect(self.rbnTime_Clicked)
        self.rbnProfile.clicked.connect(self.rbnProfile_Clicked)
        self.rbnContour.clicked.connect(self.rbnContour_Clicked)
        self.rbnFrequency.clicked.connect(self.rbnFrequency_Clicked)
        self.rbnSystem.clicked.connect(self.rbnSystem_Clicked)

        self.cboTime.currentIndexChanged.connect(
            self.cboTime_currentIndexChanged)

        self.cmdAdd.clicked.connect(self.add_element)
        self.cmdDelete.clicked.connect(self.delete_element)
        self.cmdUp.clicked.connect(self.moveup_element)
        self.cmdDown.clicked.connect(self.movedown_element)

        self.lstToGraph.setSelectionMode(QAbstractItemView.ExtendedSelection)
        self.lstToGraph.itemSelectionChanged.connect(self.select_on_map)
        self._main_form = main_form
        self.onObjectSelected = self._main_form.objectsSelected
        self.onObjectSelected.connect(self.set_selected_object)
        self.time_linked_graphs = []

    def set_from(self, project, output):
        self.project = project
        self.output = output
        self.report = Reports(project, output)
        self.cboTime.clear()
        if project and self.output:
            for time_index in range(0, self.output.num_periods):
                self.cboTime.addItem(self.output.get_time_string(time_index))
            self.rbnTime.setChecked(True)
            self.rbnNodes.setChecked(True)
            self.rbnTime_Clicked()
            self.rbnNodes_Clicked()
            self.add_element()

    def rbnNodes_Clicked(self):
        if self.rbnNodes.isChecked():
            self.gbxToGraph.setTitle(
                transl8("frmGraph", "Nodes to Graph", None))
            self.cboParameter.clear()
            attr_list = []
            for att in ENR_node_type.Attributes:
                if att.name == "Quality":
                    if self.project.options.quality.quality.value == 2:  # 'chemical'
                        attr_list.append("Chemical")
                        # units = attribute.units(self.output.unit_system)
                    elif self.project.options.quality.quality.value == 3:  # 'Age'
                        attr_list.append("Age")
                        # units = "hours"
                    elif self.project.options.quality.quality.value == 4:  # 'Trace'
                        attr_list.append(
                            "Trace " + self.project.options.quality.trace_node)
                        # units = "percent"
                else:
                    attr_list.append(att.name)

            self.cboParameter.addItems(attr_list)
            self.lstToGraph.clear()
            self.list_items = self.output.nodes
            self.add_element()
            # self.lstToGraph.addItems(self.list_items.keys())

    def rbnLinks_Clicked(self):
        if self.rbnLinks.isChecked():
            self.gbxToGraph.setTitle(
                transl8("frmGraph", "Links to Graph", None))
            self.cboParameter.clear()

            attr_list = []
            for att in ENR_link_type.Attributes:
                if att.name == "Quality":
                    if self.project.options.quality.quality.value == 2:  # 'chemical'
                        attr_list.append("Chemical")
                        # units = attribute.units(self.output.unit_system)
                    elif self.project.options.quality.quality.value == 3:  # 'Age'
                        attr_list.append("Age")
                        # units = "hours"
                    elif self.project.options.quality.quality.value == 4:  # 'Trace'
                        attr_list.append(
                            "Trace " + self.project.options.quality.trace_node)
                        # units = "percent"
                else:
                    attr_list.append(att.name)

            self.cboParameter.addItems(attr_list)
            self.lstToGraph.clear()
            self.list_items = self.output.links
            self.add_element()
            # self.lstToGraph.addItems(self.list_items.keys())

    def rbnTime_Clicked(self):
        self.cboParameter.setEnabled(True)
        self.gbxParameter.setEnabled(True)
        self.cboTime.setEnabled(False)
        self.gbxTime.setEnabled(False)
        self.gbxObject.setEnabled(True)
        self.rbnNodes.setEnabled(True)
        self.rbnLinks.setEnabled(True)
        self.gbxToGraph.setEnabled(True)
        self.lstToGraph.setEnabled(True)
        self.cboParameter.setVisible(True)
        self.cboTime.setVisible(False)
        self.lstToGraph.setVisible(True)

    def rbnProfile_Clicked(self):
        self.cboParameter.setEnabled(True)
        self.gbxParameter.setEnabled(True)
        self.cboTime.setEnabled(True)
        self.gbxTime.setEnabled(True)
        self.gbxObject.setEnabled(False)
        # self.rbnNodes.setChecked(True)
        # self.rbnNodes.setEnabled(False)
        # self.rbnLinks.setEnabled(False)
        self.gbxToGraph.setEnabled(True)
        self.lstToGraph.setEnabled(True)
        self.cboParameter.setVisible(True)
        self.cboTime.setVisible(True)
        self.lstToGraph.setVisible(True)
        if self.rbnProfile.isChecked():
            self.rbnNodes.setChecked(True)
            self.rbnNodes_Clicked()

    def rbnContour_Clicked(self):
        self.cboParameter.setEnabled(True)
        self.gbxParameter.setEnabled(True)
        self.cboTime.setEnabled(True)
        self.gbxTime.setEnabled(True)
        self.gbxObject.setEnabled(False)
        self.rbnNodes.setEnabled(False)
        self.rbnLinks.setEnabled(False)
        self.gbxToGraph.setEnabled(False)
        self.lstToGraph.setEnabled(False)
        self.cboParameter.setVisible(True)
        self.cboTime.setVisible(True)
        self.lstToGraph.setVisible(False)

    def rbnFrequency_Clicked(self):
        self.cboParameter.setEnabled(True)
        self.gbxParameter.setEnabled(True)
        self.cboTime.setEnabled(True)
        self.gbxTime.setEnabled(True)
        self.gbxObject.setEnabled(True)
        self.rbnNodes.setEnabled(True)
        self.rbnLinks.setEnabled(True)
        self.gbxToGraph.setEnabled(False)
        self.lstToGraph.setEnabled(False)
        self.cboParameter.setVisible(True)
        self.cboTime.setVisible(True)
        self.lstToGraph.setVisible(False)

    def rbnSystem_Clicked(self):
        self.cboParameter.setEnabled(False)
        self.gbxParameter.setEnabled(False)
        self.cboTime.setEnabled(False)
        self.gbxTime.setEnabled(False)
        self.gbxObject.setEnabled(False)
        self.rbnNodes.setEnabled(False)
        self.rbnLinks.setEnabled(False)
        self.gbxToGraph.setEnabled(False)
        self.lstToGraph.setEnabled(False)
        self.cboParameter.setVisible(False)
        self.cboTime.setVisible(False)
        self.lstToGraph.setVisible(False)

    def cboTime_currentIndexChanged(self):
        time_index = self.cboTime.currentIndex()
        if time_index >= 0:
            for graph in self.time_linked_graphs:
                graph[-1] = time_index
                graph[0](*graph[1:])

    def select_on_map(self):
        obj_lyr_group = None
        selected_items = []
        if self.rbnNodes.isChecked():
            obj_lyr_group = self._main_form.model_layers.nodes_layers
        elif self.rbnLinks.isChecked():
            obj_lyr_group = self._main_form.model_layers.links_layers
        if obj_lyr_group:
            del selected_items[:]
            for sitm in self.lstToGraph.selectedItems():
                selected_items.append(sitm.text())
            # for lyr in obj_lyr_group:
            #     self._main_form.select_named_items(lyr, selected_items)

    def set_selected_object(self, layer_name, object_ids):
        if layer_name and object_ids:
            can_select = False
            if (layer_name.lower().startswith("junction") or \
                  layer_name.lower().startswith("reserv") or \
                  layer_name.lower().startswith("tank") or \
                  layer_name.lower().startswith("source")) and \
                  self.rbnNodes.isChecked():
                can_select = True
            elif (layer_name.lower().startswith("pump") or \
                  layer_name.lower().startswith("valve") or \
                  layer_name.lower().startswith("pipe")) and \
                  self.rbnLinks.isChecked():
                can_select = True

            if can_select:
                for id in object_ids:
                    itms = self.lstToGraph.findItems(id,
                                                     QtCore.Qt.MatchExactly)
                    if itms and len(itms) > 0:
                        for itm in itms:
                            itm.setSelected(True)

    def add_element(self):
        layers = None
        if self.rbnNodes.isChecked():
            layers = self._main_form.model_layers.nodes_layers
        elif self.rbnLinks.isChecked():
            layers = self._main_form.model_layers.links_layers
        if layers is None:
            return
        selected_prev = []
        for ind in range(0, self.lstToGraph.count()):
            selected_prev.append(self.lstToGraph.item(ind).text())
        selected_new = []
        for layer in layers:
            # from qgis.core import QgsVectorLayer()
            # layer = QgsVectorLayer()
            for f in layer.getSelectedFeatures():
                if not f['name'] in selected_new:
                    selected_new.append(f['name'])

        for n in selected_new:
            if not n in selected_prev:
                selected_prev.append(n)

        if len(selected_prev) > 0:
            self.lstToGraph.clear()
            self.lstToGraph.addItems(selected_prev)

    def delete_element(self):
        all = []
        selected = []
        for ind in range(0, self.lstToGraph.count()):
            itm = self.lstToGraph.item(ind)
            all.append(itm.text())
            if itm.isSelected():
                selected.append(itm.text())

        if len(selected) > 0:
            for s in selected:
                all.remove(s)
            self.lstToGraph.clear()
            self.lstToGraph.addItems(all)

    def moveup_element(self):
        current_row = self.lstToGraph.currentRow()
        if current_row - 1 >= 0:
            current_itm = self.lstToGraph.takeItem(current_row)
            self.lstToGraph.insertItem(current_row - 1, current_itm)
            self.lstToGraph.setCurrentRow(current_row - 1)
            current_itm.setSelected(True)

    def movedown_element(self):
        current_row = self.lstToGraph.currentRow()
        if current_row + 1 < self.lstToGraph.count():
            current_itm = self.lstToGraph.takeItem(current_row)
            self.lstToGraph.insertItem(current_row + 1, current_itm)
            self.lstToGraph.setCurrentRow(current_row + 1)
            current_itm.setSelected(True)

    def cmdOK_Clicked(self):
        if self.rbnTime.isChecked() or self.rbnProfile.isChecked():
            if self.lstToGraph.count() == 0 or len(self.selected_items()) == 0:
                QMessageBox.information(
                    None, self._main_form.model,
                    "Need to select model elements for graphing.",
                    QMessageBox.Ok)
                return
        parameter_label = self.cboParameter.currentText()
        lqual_name = ""
        lqual_unit = ""
        if parameter_label.startswith("Chemical") or \
            parameter_label.startswith("Age") or \
            parameter_label.startswith("Trace"):
            lqual_name = parameter_label
            if lqual_name.startswith("Age"):
                lqual_unit = "hours"
            elif lqual_name.startswith("Trace"):
                lqual_unit = "percent"
            parameter_label = "Quality"

        if self.rbnNodes.isChecked():
            attribute = ENR_node_type.get_attribute_by_name(parameter_label)
        else:
            attribute = ENR_link_type.get_attribute_by_name(parameter_label)

        time_index = self.cboTime.currentIndex()

        if self.rbnTime.isChecked(
        ):  # TODO: use get_series instead of get_value if it is more efficient
            graphEPANET.plot_time(self.output, attribute,
                                  self.selected_items(), lqual_name,
                                  lqual_unit)

        if self.rbnSystem.isChecked():
            ljuncs = []
            lreserv = []
            for node_j in self.project.junctions.value:
                ljuncs.append(node_j.name)
            for node_res in self.project.reservoirs.value:
                lreserv.append(node_res.name)
            graphEPANET.plot_system_flow(self.output, ljuncs, lreserv)

        if time_index < 0 and (self.rbnProfile.isChecked()
                               or self.rbnFrequency.isChecked()):
            QMessageBox.information(
                None, self._main_form.model,
                "There is no time step currently selected.", QMessageBox.Ok)
        else:
            if self.rbnProfile.isChecked():
                items = self.selected_items()
                if len(
                        items
                ) < 2:  # if fewer than two items were selected, use all items
                    items = self.list_items.values()
                self.plot_profile(attribute, time_index, items, lqual_name,
                                  lqual_unit)
            if self.rbnFrequency.isChecked():
                graphEPANET.plot_freq(self.output, attribute, time_index,
                                      self.list_items.values(), lqual_name,
                                      lqual_unit)

    def selected_items(self):
        names = selected_list_items(self.lstToGraph)
        items = []
        for name in names:
            items.append(self.list_items[name])
        return items

    def plot_profile(self, attribute, time_index, items, aname="", aunit=""):
        fig = plt.figure()
        if self.rbnNodes.isChecked():
            x_values = self.report.node_distances(items)
        else:
            x_values = range(0, len(items))
        self.time_linked_graphs.append([
            graphEPANET.update_profile, self.output, items, x_values,
            attribute, fig.number, time_index
        ])
        graphEPANET.update_profile(self.output, items, x_values, attribute,
                                   fig.number, time_index, aname, aunit)

    def cmdCancel_Clicked(self):
        self.close()
Exemple #7
0
class frmGraph(QtGui.QMainWindow, Ui_frmGraph):

    def __init__(self, main_form):
        QtGui.QMainWindow.__init__(self, main_form)
        self.help_topic = "epanet/src/src/Graph_Se.htm"
        self.setupUi(self)
        self.cmdAdd.setVisible(False)
        self.cmdDelete.setVisible(False)
        self.cmdUp.setVisible(False)
        self.cmdDown.setVisible(False)
        QtCore.QObject.connect(self.cmdOK, QtCore.SIGNAL("clicked()"), self.cmdOK_Clicked)
        QtCore.QObject.connect(self.cmdCancel, QtCore.SIGNAL("clicked()"), self.cmdCancel_Clicked)
        QtCore.QObject.connect(self.rbnNodes, QtCore.SIGNAL("clicked()"), self.rbnNodes_Clicked)
        QtCore.QObject.connect(self.rbnLinks, QtCore.SIGNAL("clicked()"), self.rbnLinks_Clicked)

        QtCore.QObject.connect(self.rbnTime, QtCore.SIGNAL("clicked()"), self.rbnTime_Clicked)
        QtCore.QObject.connect(self.rbnProfile, QtCore.SIGNAL("clicked()"), self.rbnProfile_Clicked)
        QtCore.QObject.connect(self.rbnContour, QtCore.SIGNAL("clicked()"), self.rbnContour_Clicked)
        QtCore.QObject.connect(self.rbnFrequency, QtCore.SIGNAL("clicked()"), self.rbnFrequency_Clicked)
        QtCore.QObject.connect(self.rbnSystem, QtCore.SIGNAL("clicked()"), self.rbnSystem_Clicked)

        self.cboTime.currentIndexChanged.connect(self.cboTime_currentIndexChanged)

        self.lstToGraph.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
        self._main_form = main_form
        self.time_linked_graphs = []

    def set_from(self, project, output):
        self.project = project
        self.output = output
        self.report = Reports(project, output)
        self.cboTime.clear()
        if project and self.output:
            for time_index in range(0, self.output.numPeriods):
                self.cboTime.addItem(self.output.get_time_string(time_index))
            self.rbnNodes.setChecked(True)
            self.rbnNodes_Clicked()

    def rbnNodes_Clicked(self):
        if self.rbnNodes.isChecked():
            self.gbxToGraph.setTitle(transl8("frmGraph", "Nodes to Graph", None))
            self.cboParameter.clear()
            self.cboParameter.addItems(ENR_NodeAttributeNames)
            self.lstToGraph.clear()
            # for index in range(0, self.output.nodeCount - 1):
            #     self.lstToGraph.addItem(str(self.output.get_NodeID(index)))
            for node_id in self.report.all_node_ids():
                self.lstToGraph.addItem(node_id)

    def rbnLinks_Clicked(self):
        if self.rbnLinks.isChecked():
            self.gbxToGraph.setTitle(transl8("frmGraph", "Links to Graph", None))
            self.cboParameter.clear()
            self.cboParameter.addItems(ENR_LinkAttributeNames)
            self.lstToGraph.clear()
            self.lstToGraph.addItems(self.report.all_link_ids())

    def rbnTime_Clicked(self):
        self.cboParameter.setEnabled(True)
        self.gbxParameter.setEnabled(True)
        self.cboTime.setEnabled(False)
        self.gbxTime.setEnabled(False)
        self.gbxObject.setEnabled(True)
        self.rbnNodes.setEnabled(True)
        self.rbnLinks.setEnabled(True)
        self.gbxToGraph.setEnabled(True)
        self.lstToGraph.setEnabled(True)

    def rbnProfile_Clicked(self):
        self.cboParameter.setEnabled(True)
        self.gbxParameter.setEnabled(True)
        self.cboTime.setEnabled(True)
        self.gbxTime.setEnabled(True)
        self.gbxObject.setEnabled(False)
        # self.rbnNodes.setChecked(True)
        # self.rbnNodes.setEnabled(False)
        # self.rbnLinks.setEnabled(False)
        self.gbxToGraph.setEnabled(True)
        self.lstToGraph.setEnabled(True)

    def rbnContour_Clicked(self):
        self.cboParameter.setEnabled(True)
        self.gbxParameter.setEnabled(True)
        self.cboTime.setEnabled(True)
        self.gbxTime.setEnabled(True)
        self.gbxObject.setEnabled(False)
        self.rbnNodes.setEnabled(False)
        self.rbnLinks.setEnabled(False)
        self.gbxToGraph.setEnabled(False)
        self.lstToGraph.setEnabled(False)

    def rbnFrequency_Clicked(self):
        self.cboParameter.setEnabled(True)
        self.gbxParameter.setEnabled(True)
        self.cboTime.setEnabled(True)
        self.gbxTime.setEnabled(True)
        self.gbxObject.setEnabled(True)
        self.rbnNodes.setEnabled(True)
        self.rbnLinks.setEnabled(True)
        self.gbxToGraph.setEnabled(False)
        self.lstToGraph.setEnabled(False)

    def rbnSystem_Clicked(self):
        self.cboParameter.setEnabled(False)
        self.gbxParameter.setEnabled(False)
        self.cboTime.setEnabled(False)
        self.gbxTime.setEnabled(False)
        self.gbxObject.setEnabled(False)
        self.rbnNodes.setEnabled(False)
        self.rbnLinks.setEnabled(False)
        self.gbxToGraph.setEnabled(False)
        self.lstToGraph.setEnabled(False)

    def cboTime_currentIndexChanged(self):
        time_index = self.cboTime.currentIndex()
        if time_index >= 0:
            for graph in self.time_linked_graphs:
                graph[-1] = time_index
                graph[0](*graph[1:])

    def cmdOK_Clicked(self):
        attribute_index = self.cboParameter.currentIndex()
        if self.rbnNodes.isChecked():
            get_index = self.output.get_NodeIndex
            get_value = self.output.get_NodeValue
            get_series = self.output.get_NodeSeries
            parameter_code = ENR_NodeAttributes[attribute_index]
            units = ENR_NodeAttributeUnits[attribute_index][self.output.unit_system]

        else:
            get_index = self.output.get_LinkIndex
            get_value = self.output.get_LinkValue
            get_series = self.output.get_LinkSeries
            parameter_code = ENR_LinkAttributes[attribute_index]
            units = ENR_LinkAttributeUnits[attribute_index][self.output.unit_system]

        parameter_label = self.cboParameter.currentText()
        if units:
            parameter_label += ' (' + units + ')'
        time_index = self.cboTime.currentIndex()

        if self.rbnTime.isChecked():  # TODO: use get_series instead of get_value if it is more efficient
            graphEPANET.plot_time(self.output, get_index, get_value, parameter_code, parameter_label, selected_list_items(self.lstToGraph))

        if self.rbnSystem.isChecked():
            graphEPANET.plot_system_flow(self.output)

        if time_index < 0 and (self.rbnProfile.isChecked() or self.rbnFrequency.isChecked()):
            QMessageBox.information(None, self._main_form.model,
                                    "There is no time step currently selected.",
                                    QMessageBox.Ok)
        else:
            if self.rbnProfile.isChecked():
                graph_ids = selected_list_items(self.lstToGraph)
                if not graph_ids:
                    graph_ids = self.report.all_node_ids()
                self.plot_profile(get_index, get_value, parameter_code, parameter_label, time_index, graph_ids)
            if self.rbnFrequency.isChecked():
                graphEPANET.plot_freq(self.output, get_index, get_value, parameter_code, parameter_label,
                                      time_index, all_list_items(self.lstToGraph))

    def plot_profile(self, get_index, get_value, parameter_code, parameter_label, time_index, ids):
        fig = plt.figure()
        if self.rbnNodes.isChecked():
            x_values = self.report.node_distances(ids)
        else:
            x_values = range(0, len(ids))
        self.time_linked_graphs.append([graphEPANET.update_profile, self.output, ids, x_values,
             get_index, get_value, parameter_code, parameter_label, fig.number, time_index])
        graphEPANET.update_profile(self.output, ids, x_values,
                                   get_index, get_value, parameter_code, parameter_label, fig.number, time_index)

    def cmdCancel_Clicked(self):
        self.close()
class frmTable(QtGui.QMainWindow, Ui_frmTable):

    def __init__(self, main_form):
        QtGui.QMainWindow.__init__(self, main_form)
        self.help_topic = "epanet/src/src/Table_Op.htm"
        self.setupUi(self)
        self.tabWidget.setCurrentIndex(0)
        self.cboFilter.addItems(('Below', 'Equal To', 'Above'))
        self.cmdOK.clicked.connect(self.cmdOK_Clicked)
        self.cmdCancel.clicked.connect(self.cmdCancel_Clicked)
        self.rbnNodes.clicked.connect(self.rbnNodes_Clicked)
        self.rbnLinks.clicked.connect(self.rbnLinks_Clicked)
        self.rbnTimeseriesNode.clicked.connect(self.rbnTimeseriesNode_Clicked)
        self._main_form = main_form
        self.forms = []

    def set_from(self, project, output):
        self.project = project
        self.output = output
        self.report = Reports(project, output)
        self.cboTime.clear()
        if self.project and self.output:
            for time_index in range(0, self.output.numPeriods):
                #if self.???:
                    self.cboTime.addItem(self.output.get_time_string(time_index))
                #else:
                #    self.cboTime.addItem(self.output.get_date_string(time_index))

            self.rbnNodes.setChecked(True)
            self.rbnNodes_Clicked()

    def rbnNodes_Clicked(self):
        if self.rbnNodes.isChecked():
            self.cbxSort.setEnabled(True)
            self.cbxSort.setText(transl8("frmTable", "Sort by Elevation", None))
            self.lstColumns.clear()
            self.lstColumns.addItems(ENR_NodeAttributeNames)
            self.lstColumns.selectAll()
            self.cboFilter.clear()
            self.cboFilter.addItems(ENR_NodeAttributeNames)
            self.rbnLinks.setChecked(False)
            self.rbnTimeseriesNode.setChecked(False)
            self.rbnTimeseriesLink.setChecked(False)

    def rbnLinks_Clicked(self):
        if self.rbnLinks.isChecked():
            self.cbxSort.setEnabled(True)
            self.cbxSort.setText(transl8("frmTable", "Sort by Length", None))
            self.lstColumns.clear()
            self.lstColumns.addItems(ENR_LinkAttributeNames)
            self.lstColumns.selectAll()
            self.cboFilter.clear()
            self.cboFilter.addItems(ENR_LinkAttributeNames)
            self.rbnNodes.setChecked(False)
            self.rbnTimeseriesNode.setChecked(False)
            self.rbnTimeseriesLink.setChecked(False)

    def rbnTimeseriesNode_Clicked(self):
        if self.rbnTimeseriesNode.isChecked():
            self.cbxSort.setEnabled(False)
            self.cbxSort.setText(transl8("frmTable", "Sort by", None))
            self.lstColumns.clear()
            self.lstColumns.addItems(ENR_NodeAttributeNames)
            self.lstColumns.selectAll()
            self.cboFilter.clear()
            self.cboFilter.addItems(ENR_NodeAttributeNames)
            self.rbnNodes.setChecked(False)
            self.rbnLinks.setChecked(False)
            self.rbnTimeseriesLink.setChecked(False)

    def rbnTimeseriesLink_Clicked(self):
        if self.rbnTimeseriesLink.isChecked():
            self.cbxSort.setEnabled(False)
            self.cbxSort.setText(transl8("frmTable", "Sort by", None))
            self.lstColumns.clear()
            self.lstColumns.addItems(ENR_LinkAttributeNames)
            self.lstColumns.selectAll()
            self.cboFilter.clear()
            self.cboFilter.addItems(ENR_LinkAttributeNames)
            self.rbnNodes.setChecked(False)
            self.rbnLinks.setChecked(False)
            self.rbnTimeseriesNode.setChecked(False)

    def cmdOK_Clicked(self):
        row_headers = []
        column_headers = []
        parameter_codes = []
        if self.rbnNodes.isChecked() or self.rbnTimeseriesNode.isChecked():
            title = "Node"
            get_index = self.output.get_NodeIndex
            get_value = self.output.get_NodeValue
            get_series = self.output.get_NodeSeries
            for column_item in self.lstColumns.selectedItems():
                attribute_name = str(column_item.text())
                attribute_index = ENR_NodeAttributeNames.index(attribute_name)
                parameter_codes.append(ENR_NodeAttributes[attribute_index])
                units = ENR_NodeAttributeUnits[attribute_index][self.output.unit_system]
                if units:
                    attribute_name += '\n(' + units + ')'
                column_headers.append(attribute_name)

        else:  # if self.rbnLinks.isChecked() or self.rbnTimeseriesLink.isChecked():
            title = "Link"
            get_index = self.output.get_LinkIndex
            get_value = self.output.get_LinkValue
            get_series = self.output.get_LinkSeries
            for column_item in self.lstColumns.selectedItems():
                attribute_name = str(column_item.text())
                attribute_index = ENR_LinkAttributeNames.index(attribute_name)
                parameter_codes.append(ENR_LinkAttributes[attribute_index])
                units = ENR_LinkAttributeUnits[attribute_index][self.output.unit_system]
                if units:
                    attribute_name += '\n(' + units + ')'
                column_headers.append(attribute_name)

        frm = frmGenericListOutput(self._main_form, "EPANET Table Output")
        if self.rbnNodes.isChecked() or self.rbnLinks.isChecked():
            if self.rbnNodes.isChecked():
                ids = self.report.all_node_ids()
                for node_type, node_id in zip(self.report.all_node_types(), ids):
                    row_headers.append(node_type + ' ' + node_id)
            else:
                ids = self.report.all_link_ids()
                for link_type, link_id in zip(self.report.all_link_types(), ids):
                    row_headers.append(link_type + ' ' + link_id)
            time_index = self.cboTime.currentIndex()
            title = "Network Table - " + title + "s at "+ self.output.get_time_string(time_index)
            self.make_table(frm, title, get_index, get_value, time_index,
                            ids, row_headers, parameter_codes, column_headers)
        else:
            id = self.txtNodeLink.text()  # TODO: turn this textbox into combo box or list?
            title = "Time Series Table - " + title + ' ' + id
            self.make_timeseries_table(frm, title, get_index, get_value, id, parameter_codes, column_headers)
        frm.tblGeneric.resizeColumnsToContents()
        frm.show()
        self.forms.append(frm)
        # self.close()

    def make_table(self, frm, title, get_index, get_value, time_index, ids, row_headers, parameter_codes, column_headers):
        frm.setWindowTitle(title)
        tbl = frm.tblGeneric

        tbl.setRowCount(len(row_headers))
        tbl.setColumnCount(len(column_headers))
        tbl.setHorizontalHeaderLabels(column_headers)
        tbl.setVerticalHeaderLabels(row_headers)

        row = 0
        for this_id in ids:
            col = 0
            for parameter_code in parameter_codes:
                output_index = get_index(this_id)
                if output_index >= 0:
                    val = get_value(output_index, time_index, parameter_code)
                    if parameter_code == ENR_status and val in [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]:
                        val_str = ('Closed', 'Closed', 'Closed', 'Open', 'Active', 'Open', 'Open', 'Open')[int(val)]
                    else:
                        val_str = '{:7.2f}'.format(val)
                    item = QtGui.QTableWidgetItem(val_str)
                    item.setFlags(QtCore.Qt.ItemIsSelectable)  # | QtCore.Qt.ItemIsEnabled)
                    item.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignRight)
                    tbl.setItem(row, col, item)
                col += 1
            row += 1

    def make_timeseries_table(self, frm, title, get_index, get_value, this_id, parameter_codes, column_headers):
        frm.setWindowTitle(title)
        row_headers = []
        for time_index in range(0, self.output.numPeriods):
            row_headers.append(self.output.get_time_string(time_index))
        tbl = frm.tblGeneric
        tbl.setRowCount(len(row_headers))
        tbl.setColumnCount(len(column_headers))
        tbl.setHorizontalHeaderLabels(column_headers)
        tbl.setVerticalHeaderLabels(row_headers)

        output_index = get_index(this_id)
        if output_index >= 0:
            row = 0
            for time_index in range(0, self.output.numPeriods):
                col = 0
                for parameter_code in parameter_codes:
                    val = get_value(output_index, time_index, parameter_code)
                    if parameter_code == ENR_status and val in [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]:
                        val_str = ('Closed', 'Closed', 'Closed', 'Open', 'Active', 'Open', 'Open', 'Open')[int(val)]
                    else:
                        val_str = '{:7.2f}'.format(val)
                    item = QtGui.QTableWidgetItem(val_str)
                    item.setFlags(QtCore.Qt.ItemIsSelectable)  # | QtCore.Qt.ItemIsEnabled)
                    item.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignRight)
                    tbl.setItem(row, col, item)
                    col += 1
                row += 1

    def cmdCancel_Clicked(self):
        self.close()
class frmTable(QMainWindow, Ui_frmTable):

    def __init__(self, main_form):
        QMainWindow.__init__(self, main_form)
        self.help_topic = "epanet/src/src/Table_Op.htm"
        self.setupUi(self)
        self.tabWidget.setCurrentIndex(0)
        self.cboCompare.addItems(('Below', 'Equal To', 'Above'))
        self.cmdOK.clicked.connect(self.cmdOK_Clicked)
        self.cmdCancel.clicked.connect(self.cmdCancel_Clicked)
        self.cmdAdd.clicked.connect(self.cmdAdd_Clicked)
        self.cmdDelete.clicked.connect(self.cmdDelete_Clicked)
        self.rbnNodes.clicked.connect(self.rbnNodes_Clicked)
        self.rbnLinks.clicked.connect(self.rbnLinks_Clicked)
        self.rbnTimeseriesNode.clicked.connect(self.rbnTimeseriesNode_Clicked)
        self.rbnTimeseriesLink.clicked.connect(self.rbnTimeseriesLink_Clicked)
        self._main_form = main_form
        self.item_type = ENR_node_type
        self.forms = []
        self.cbxSort.setVisible(False)

    def set_from(self, project, output):
        self.project = project
        self.output = output
        self.report = Reports(project, output)
        self.cboTime.clear()
        if self.project and self.output:
            for time_index in range(0, self.output.num_periods):
                #if self.???:  TODO: give choice between elapsed hours and date
                self.cboTime.addItem(self.output.get_time_string(time_index))
                #else:
                #    self.cboTime.addItem(self.output.get_date_string(time_index))

            self.rbnNodes.setChecked(True)
            self.rbnNodes_Clicked()

    def rbnNodes_Clicked(self):
        if self.rbnNodes.isChecked():
            self.item_type = ENR_node_type
            self.set_attributes()
            self.cbxSort.setEnabled(True)
            self.cbxSort.setText(transl8("frmTable", "Sort by Elevation", None))
            self.rbnLinks.setChecked(False)
            self.rbnTimeseriesNode.setChecked(False)
            self.rbnTimeseriesLink.setChecked(False)

    def rbnLinks_Clicked(self):
        if self.rbnLinks.isChecked():
            self.item_type = ENR_link_type
            self.set_attributes()
            self.cbxSort.setEnabled(True)
            self.cbxSort.setText(transl8("frmTable", "Sort by Length", None))
            self.rbnNodes.setChecked(False)
            self.rbnTimeseriesNode.setChecked(False)
            self.rbnTimeseriesLink.setChecked(False)

    def rbnTimeseriesNode_Clicked(self):
        if self.rbnTimeseriesNode.isChecked():
            self.item_type = ENR_node_type
            self.set_attributes()
            self.cbxSort.setEnabled(False)
            self.cbxSort.setText(transl8("frmTable", "Sort by", None))
            self.rbnNodes.setChecked(False)
            self.rbnLinks.setChecked(False)
            self.rbnTimeseriesLink.setChecked(False)

    def rbnTimeseriesLink_Clicked(self):
        if self.rbnTimeseriesLink.isChecked():
            self.item_type = ENR_link_type
            self.set_attributes()
            self.cbxSort.setEnabled(False)
            self.cbxSort.setText(transl8("frmTable", "Sort by", None))
            self.rbnNodes.setChecked(False)
            self.rbnLinks.setChecked(False)
            self.rbnTimeseriesNode.setChecked(False)

    def set_attributes(self):
        self.lstColumns.clear()
        if self.item_type == ENR_node_type:
            self.property_attributes = ['Elevation', 'Base Demand', 'Initial Quality']
        elif self.item_type == ENR_link_type:
            self.property_attributes = ['Length', 'Diameter', 'Roughness', 'Bulk Coeff.', 'Wall Coeff.']
        self.lstColumns.addItems(self.property_attributes)
        attribute_names = [attribute.name for attribute in self.item_type.Attributes]
        self.lstColumns.addItems(attribute_names)
        self.lstColumns.selectAll()
        self.lstColumns.item(0).setSelected(False)
        self.lstColumns.item(1).setSelected(False)
        self.lstColumns.item(2).setSelected(False)
        if self.item_type == ENR_link_type:
            self.lstColumns.item(3).setSelected(False)
            self.lstColumns.item(4).setSelected(False)

        self.cboFilter.clear()
        if self.item_type == ENR_node_type:
            self.cboFilter.addItems(['Elevation', 'Base Demand', 'Initial Quality'])
        elif self.item_type == ENR_link_type:
            self.cboFilter.addItems(['Length', 'Diameter', 'Roughness', 'Bulk Coeff.', 'Wall Coeff.'])
        self.cboFilter.addItems(attribute_names)


    def cmdOK_Clicked(self):
        row_headers = []
        column_headers = []
        requested_output_attributes = []
        requested_property_attributes = []
        quality_name = ""
        if hasattr(self.item_type, "AttributeQuality"):
            quality_name = self.item_type.AttributeQuality.name

        for column_index in self.lstColumns.selectedIndexes():
            attribute_index = column_index.row() - len(self.property_attributes)
            if attribute_index < 0:
                # these are properties of the class
                attribute_index = attribute_index + len(self.property_attributes)
                column_header = self.property_attributes[attribute_index]
                if self.rbnNodes.isChecked():
                    meta_item = Junction.metadata.meta_item_of_label(column_header)
                else:
                    meta_item = Pipe.metadata.meta_item_of_label(column_header)
                attribute = meta_item.attribute
                if attribute:
                    units = ""
                    if self.output.unit_system == 0:
                        units = meta_item.units_english
                    elif self.output.unit_system == 1:
                        units = meta_item.units_metric
                    if len(units) == 0:
                        if attribute == 'base_demand_flow':
                            units = '(' + self.project.options.hydraulics.flow_units.name + ')'
                    if units:
                        column_header += '\n' + units
                    column_headers.append(column_header)
                    requested_property_attributes.append(self.property_attributes[attribute_index])
            elif attribute_index < len(self.item_type.Attributes):
                # these are output variables
                attribute = self.item_type.Attributes[attribute_index]
                requested_output_attributes.append(attribute)
                column_header = attribute.name
                units = ""
                if attribute.name == quality_name:
                    if self.project.options.quality.quality.value == 2: # 'chemical'
                        column_header = "Chemical"
                        units = attribute.units(self.output.unit_system)
                    elif self.project.options.quality.quality.value == 3: # 'Age'
                        column_header = "Age"
                        units = "hours"
                    elif self.project.options.quality.quality.value == 4:  # 'Trace'
                        column_header = "Trace " + self.project.options.quality.trace_node
                        units = "percent"
                else:
                    units = attribute.units(self.output.unit_system)
                if units:
                    column_header += '\n(' + units + ')'
                column_headers.append(column_header)
            else:
                print ("Did not find attribute " + str(attribute_index))

        frm = frmGenericListOutput(self._main_form, "EPANET Table Output")

        if self.rbnNodes.isChecked() or self.rbnLinks.isChecked():
            output_items = list()
            property_items = list()
            time_index = self.cboTime.currentIndex()

            # property items
            if self.rbnNodes.isChecked():
                for item in self.project.all_nodes():
                    property_items.append(item)
            else:
                for item in self.project.all_links():
                    property_items.append(item)

            # output items
            if self.rbnNodes.isChecked():
                for item in self.report.all_nodes_set_category():
                    output_items.append(item)
                    row_headers.append(item.category + ' ' + item.name)
            else:
                for item in self.report.all_links_set_category():
                    output_items.append(item)
                    row_headers.append(item.category + ' ' + item.name)

            title = "Network Table - " + self.item_type.TypeLabel + "s at "+ self.output.get_time_string(time_index)
            self.make_table(frm, title, time_index, row_headers, column_headers,
                            property_items, requested_property_attributes,
                            output_items, requested_output_attributes)

        else:
            name = self.txtNodeLink.text()  # TODO: turn this textbox into combo box or list?
            if self.rbnTimeseriesNode.isChecked():
                item = self.output.nodes[name]
            else:
                item = self.output.links[name]
            title = "Time Series Table - " + self.item_type.TypeLabel + ' ' + name
            self.make_timeseries_table(frm, title, item, requested_output_attributes, column_headers)
        frm.tblGeneric.resizeColumnsToContents()
        frm.show()
        frm.update()
        self.forms.append(frm)
        # self.close()

    def check_filters(self, values):
        ok_to_append = True
        for i in range(0, self.lstFilter.count()):
            word_list = self.lstFilter.item(i).text().split()
            qty = word_list[0]
            comp = word_list[1]
            val = word_list[2]
            pos = self.cboFilter.findText(qty) + 1
            if pos > 0:
                if comp == 'Below':
                    if values[pos] >= float(val):
                        ok_to_append = False
                elif comp == 'Equal To':
                    if values[pos] > float(val):
                        ok_to_append = False
                    if values[pos] < float(val):
                        ok_to_append = False
                elif comp == 'Above':
                    if values[pos] <= float(val):
                        ok_to_append = False
        return ok_to_append

    def make_table(self, frm, title, time_index, row_headers, column_headers,
                   property_items, requested_property_attributes,
                   output_items, requested_output_attributes):

        frm.setWindowTitle(title)
        tbl = frm.tblGeneric

        tbl.setRowCount(len(row_headers))
        tbl.setColumnCount(len(column_headers))
        tbl.setHorizontalHeaderLabels(column_headers)
        tbl.setVerticalHeaderLabels(row_headers)

        # first set from properties
        row = 0
        for this_item in property_items:
            col = 0
            for selected_property_attribute in requested_property_attributes:
                if self.rbnNodes.isChecked():
                    meta_item = Junction.metadata.meta_item_of_label(selected_property_attribute)
                else:
                    meta_item = Pipe.metadata.meta_item_of_label(selected_property_attribute)
                attribute = meta_item.attribute
                if attribute:  # Found an attribute
                    val_str = str(getattr(this_item, attribute, 0))
                    table_cell_widget = QTableWidgetItem(val_str)
                    table_cell_widget.setFlags(QtCore.Qt.ItemIsSelectable)  # | QtCore.Qt.ItemIsEnabled)
                    table_cell_widget.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignRight)
                    tbl.setItem(row, col, table_cell_widget)
                col += 1
            row += 1
            if row == 20:
                tbl.resizeColumnsToContents()
                frm.show()
                frm.update()
                process_events()

        # now set from output values
        row = 0
        for this_item in output_items:
            col = len(requested_property_attributes)
            values = this_item.get_all_attributes_at_time(self.output, time_index)
            for attribute in requested_output_attributes:
                val_str = attribute.str(values[attribute.index])
                table_cell_widget = QTableWidgetItem(val_str)
                table_cell_widget.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
                table_cell_widget.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignRight)
                tbl.setItem(row, col, table_cell_widget)
                col += 1
            row += 1
            if row == 20:
                tbl.resizeColumnsToContents()
                frm.show()
                frm.update()
                process_events()

        # now remove the rows that fail the filter check
        rows_to_remove = []
        for i in range(0, self.lstFilter.count()):
            word_list = self.lstFilter.item(i).text().split()
            val = word_list[len(word_list)-1]
            comp = word_list[len(word_list)-2]
            qty = ''
            for word_index in range(0, len(word_list)-2):
                qty += word_list[word_index]
                if word_index < len(word_list)-3:
                    qty += ' '
            pos = self.cboFilter.findText(qty)
            if pos > 0:
                # for each row of the grid
                for irow in range(0, tbl.rowCount()):
                    remove_me = False
                    value = float(tbl.item(irow, pos).text())
                    if comp == 'Below':
                        if value >= float(val):
                            remove_me = True
                    elif comp == 'Equal To':
                        if value != float(val):
                            remove_me = True
                    elif comp == 'Above':
                        if value <= float(val):
                            remove_me = True
                    if remove_me and not irow in rows_to_remove:
                        rows_to_remove.append(irow)
         # now go ahead and remove those rows
        removed_count = 0
        for row_number in rows_to_remove:
            tbl.removeRow(int(row_number)-removed_count)
            removed_count += 1
        for row in range(tbl.rowCount()):
            tbl.setRowHeight(row, 10)

        # do sort
        # tbl.sortByColumn(1, Qt.AscendingOrder)
        # sorting not yet enabled


    def make_timeseries_table(self, frm, title, item, attributes, column_headers):
        frm.setWindowTitle(title)
        row_headers = []
        for time_index in range(0, self.output.num_periods):
            row_headers.append(self.output.get_time_string(time_index))
        tbl = frm.tblGeneric
        tbl.setRowCount(len(row_headers))
        tbl.setColumnCount(len(column_headers))
        tbl.setHorizontalHeaderLabels(column_headers)
        tbl.setVerticalHeaderLabels(row_headers)

        if item:
            row = 0
            for time_index in range(0, self.output.num_periods):
                col = 0
                values = item.get_all_attributes_at_time(self.output, time_index)
                for attribute in attributes:
                    val_str = attribute.str(values[attribute.index])
                    table_cell_widget = QTableWidgetItem(val_str)
                    table_cell_widget.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
                    table_cell_widget.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignRight)
                    tbl.setItem(row, col, table_cell_widget)
                    col += 1
                row += 1
        for row in range(tbl.rowCount()):
            tbl.setRowHeight(row, 10)

    def cmdCancel_Clicked(self):
        self.close()

    def cmdAdd_Clicked(self):
        qty = self.cboFilter.currentText()
        comp = self.cboCompare.currentText()
        val = self.txtValue.text()
        entry = qty + " " + comp + " " + val
        if self.lstFilter.count() == 0:
            self.lstFilter.addItem(entry)
        else:
            if not self.lstFilter.findItems(entry, QtCore.Qt.MatchCaseSensitive):
                self.lstFilter.addItem(entry)
        pass

    def cmdDelete_Clicked(self):
        if not self.lstFilter.selectedItems(): return
        for wi in self.lstFilter.selectedItems():
            self.lstFilter.takeItem(self.lstFilter.row(wi))
        pass

    def copy(self):
        selected_range = self.tblGeneric.selectedRanges()[0]
        str = ""
        for i in range(selected_range.rowCount()):
            if i > 0:
                str += "\n"
            for j in range(selected_range.columnCount()):
                if j > 0:
                    str += "\t"
                str += self.tblGeneric.item(selected_range.topRow() + i, selected_range.leftColumn() + j).text()
        str += "\n"
        QApplication.clipboard().setText(str)

    def keyPressEvent(self, event):
        if event.matches(QtGui.QKeySequence.Copy):
            self.copy()