Esempio n. 1
0
    def basic_hydrometer_state_selector(self):
        """ Button 51: Hydrometer selector """
        # Create the dialog and signals
        self.dlg_hydro_state = Multirow_selector()
        self.load_settings(self.dlg_hydro_state)
        self.dlg_hydro_state.btn_ok.clicked.connect(
            partial(self.close_dialog, self.dlg_hydro_state))
        self.dlg_hydro_state.rejected.connect(
            partial(self.close_dialog, self.dlg_hydro_state))
        self.dlg_hydro_state.txt_name.setVisible(False)
        self.dlg_hydro_state.setWindowTitle("Hydrometer selector")
        utils_giswater.setWidgetText(
            self.dlg_hydro_state, self.dlg_hydro_state.lbl_unselected,
            self.controller.tr('Unselected hydrometers',
                               context_name='labels'))
        utils_giswater.setWidgetText(
            self.dlg_hydro_state, self.dlg_hydro_state.lbl_selected,
            self.controller.tr('Selected hydrometers', context_name='labels'))
        tableleft = "ext_rtc_hydrometer_state"
        tableright = "selector_hydrometer"
        field_id_left = "id"
        field_id_right = "state_id"
        self.multi_row_selector(self.dlg_hydro_state, tableleft, tableright,
                                field_id_left, field_id_right)

        # Open dialog
        self.open_dialog(self.dlg_hydro_state, maximize_button=False)
Esempio n. 2
0
    def master_psector_selector(self):
        """ Button 47: Psector selector """

        # Create the dialog and signals
        self.dlg_psector_selector = Multirow_selector()
        self.load_settings(self.dlg_psector_selector)
        self.dlg_psector_selector.btn_ok.clicked.connect(
            partial(self.close_dialog, self.dlg_psector_selector))
        self.dlg_psector_selector.setWindowTitle("Psector selector")
        utils_giswater.setWidgetText(
            self.dlg_psector_selector, self.dlg_psector_selector.lbl_filter,
            self.controller.tr('Filter by: Psector name',
                               context_name='labels'))
        utils_giswater.setWidgetText(
            self.dlg_psector_selector,
            self.dlg_psector_selector.lbl_unselected,
            self.controller.tr('Unselected psectors', context_name='labels'))
        utils_giswater.setWidgetText(
            self.dlg_psector_selector, self.dlg_psector_selector.lbl_selected,
            self.controller.tr('Selected psectors', context_name='labels'))

        tableleft = "plan_psector"
        tableright = "selector_psector"
        field_id_left = "psector_id"
        field_id_right = "psector_id"
        self.multi_row_selector(self.dlg_psector_selector, tableleft,
                                tableright, field_id_left, field_id_right)
        self.open_dialog(self.dlg_psector_selector,
                         dlg_name="multirow_selector",
                         maximize_button=False)
Esempio n. 3
0
    def basic_exploitation_selector(self):
        """ Button 41: Explotation selector """

        self.dlg_expoitation = Multirow_selector()
        self.load_settings(self.dlg_expoitation)

        self.dlg_expoitation.btn_ok.clicked.connect(
            partial(self.close_dialog, self.dlg_expoitation))
        self.dlg_expoitation.rejected.connect(
            partial(self.close_dialog, self.dlg_expoitation))
        self.dlg_expoitation.setWindowTitle("Explotation selector")
        utils_giswater.setWidgetText(
            self.dlg_expoitation, self.dlg_expoitation.lbl_filter,
            self.controller.tr('Filter by: Exploitation name',
                               context_name='labels'))
        utils_giswater.setWidgetText(
            self.dlg_expoitation, self.dlg_expoitation.lbl_unselected,
            self.controller.tr('Unselected exploitations',
                               context_name='labels'))
        utils_giswater.setWidgetText(
            self.dlg_expoitation, self.dlg_expoitation.lbl_selected,
            self.controller.tr('Selected exploitations',
                               context_name='labels'))

        tableleft = "exploitation"
        tableright = "selector_expl"
        field_id_left = "expl_id"
        field_id_right = "expl_id"
        self.multi_row_selector(self.dlg_expoitation, tableleft, tableright,
                                field_id_left, field_id_right)

        # Open dialog
        self.open_dialog(self.dlg_expoitation, maximize_button=False)
Esempio n. 4
0
    def sector_selection(self, tableleft, tableright, field_id_left, field_id_right):
        """ Load the tables in the selection form """

        dlg_psector_sel = Multirow_selector()
        self.load_settings(dlg_psector_sel)
        dlg_psector_sel.btn_ok.clicked.connect(dlg_psector_sel.close)
        # dlg_psector_sel.setWindowTitle(" Dscenario selector")
        if tableleft == 'sector':
            dlg_psector_sel.setWindowTitle(" Sector selector")
            utils_giswater.setWidgetText(dlg_psector_sel, dlg_psector_sel.lbl_filter, self.controller.tr('Filter by: Sector name', context_name='labels'))
            utils_giswater.setWidgetText(dlg_psector_sel, dlg_psector_sel.lbl_unselected, self.controller.tr('Unselected sectors', context_name='labels'))
            utils_giswater.setWidgetText(dlg_psector_sel, dlg_psector_sel.lbl_selected, self.controller.tr('Selected sectors', context_name='labels'))
        if tableleft == 'cat_dscenario':
            dlg_psector_sel.setWindowTitle(" Dscenario selector")
            utils_giswater.setWidgetText(dlg_psector_sel, dlg_psector_sel.lbl_filter, self.controller.tr('Filter by: Dscenario name', context_name='labels'))
            utils_giswater.setWidgetText(dlg_psector_sel, dlg_psector_sel.lbl_unselected, self.controller.tr('Unselected dscenarios', context_name='labels'))
            utils_giswater.setWidgetText(dlg_psector_sel, dlg_psector_sel.lbl_selected, self.controller.tr('Selected dscenarios', context_name='labels'))
        self.multi_row_selector(dlg_psector_sel, tableleft, tableright, field_id_left, field_id_right)
        dlg_psector_sel.setWindowFlags(Qt.WindowStaysOnTopHint)
        dlg_psector_sel.exec_()
    def mincut_selector(self):
        self.dlg_mincut_sel = Multirow_selector()
        self.load_settings(self.dlg_mincut_sel)

        self.dlg_mincut_sel.btn_ok.clicked.connect(partial(self.close_dialog, self.dlg_mincut_sel))
        self.dlg_mincut_sel.rejected.connect(partial(self.close_dialog, self.dlg_mincut_sel))
        self.dlg_mincut_sel.setWindowTitle("Mincut selector")
        utils_giswater.setWidgetText(self.dlg_mincut_sel, 'lbl_filter', self.controller.tr('Filter by: Mincut id', context_name='labels'))
        utils_giswater.setWidgetText(self.dlg_mincut_sel, 'lbl_unselected', self.controller.tr('Unselected mincut', context_name='labels'))
        utils_giswater.setWidgetText(self.dlg_mincut_sel, 'lbl_selected', self.controller.tr('Selected mincut', context_name='labels'))

        tableleft = "v_ui_anl_mincut_result_cat"
        tableright = "anl_mincut_result_selector"
        field_id_left = "id"
        field_id_right = "result_id"
        index = [0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
        self.multi_row_selector(self.dlg_mincut_sel, tableleft, tableright, field_id_left, field_id_right, index=index)
        self.dlg_mincut_sel.btn_select.clicked.connect(partial(self.mincut.set_visible_mincut_layers))
        # Open dialog
        self.open_dialog(self.dlg_mincut_sel, maximize_button=False)
Esempio n. 6
0
class Basic(ParentAction):
    def __init__(self, iface, settings, controller, plugin_dir):
        """ Class to control toolbar 'basic' """
        self.minor_version = "3.0"
        self.search_plus = None
        ParentAction.__init__(self, iface, settings, controller, plugin_dir)
        self.logged = False
        self.login_file = os.path.join(self.plugin_dir, 'config', 'login.auth')

    def set_giswater(self, giswater):
        self.giswater = giswater

    def set_project_type(self, project_type):
        self.project_type = project_type

    def basic_exploitation_selector(self):
        """ Button 41: Explotation selector """

        self.dlg_expoitation = Multirow_selector()
        self.load_settings(self.dlg_expoitation)

        self.dlg_expoitation.btn_ok.clicked.connect(
            partial(self.close_dialog, self.dlg_expoitation))
        self.dlg_expoitation.rejected.connect(
            partial(self.close_dialog, self.dlg_expoitation))
        self.dlg_expoitation.setWindowTitle("Explotation selector")
        utils_giswater.setWidgetText(
            self.dlg_expoitation, self.dlg_expoitation.lbl_filter,
            self.controller.tr('Filter by: Exploitation name',
                               context_name='labels'))
        utils_giswater.setWidgetText(
            self.dlg_expoitation, self.dlg_expoitation.lbl_unselected,
            self.controller.tr('Unselected exploitations',
                               context_name='labels'))
        utils_giswater.setWidgetText(
            self.dlg_expoitation, self.dlg_expoitation.lbl_selected,
            self.controller.tr('Selected exploitations',
                               context_name='labels'))

        tableleft = "exploitation"
        tableright = "selector_expl"
        field_id_left = "expl_id"
        field_id_right = "expl_id"
        self.multi_row_selector(self.dlg_expoitation, tableleft, tableright,
                                field_id_left, field_id_right)

        # Open dialog
        self.open_dialog(self.dlg_expoitation, maximize_button=False)

    def basic_state_selector(self):
        """ Button 48: State selector """

        # Create the dialog and signals
        self.dlg_state = Multirow_selector()
        self.load_settings(self.dlg_state)
        self.dlg_state.btn_ok.clicked.connect(
            partial(self.close_dialog, self.dlg_state))
        self.dlg_state.rejected.connect(
            partial(self.close_dialog, self.dlg_state))
        self.dlg_state.txt_name.setVisible(False)
        self.dlg_state.setWindowTitle("State selector")
        utils_giswater.setWidgetText(
            self.dlg_state, self.dlg_state.lbl_unselected,
            self.controller.tr('Unselected states', context_name='labels'))
        utils_giswater.setWidgetText(
            self.dlg_state, self.dlg_state.lbl_selected,
            self.controller.tr('Selected states', context_name='labels'))
        tableleft = "value_state"
        tableright = "selector_state"
        field_id_left = "id"
        field_id_right = "state_id"
        self.multi_row_selector(self.dlg_state, tableleft, tableright,
                                field_id_left, field_id_right)

        # Open dialog
        self.open_dialog(self.dlg_state, maximize_button=False)

    def basic_hydrometer_state_selector(self):
        """ Button 51: Hydrometer selector """
        # Create the dialog and signals
        self.dlg_hydro_state = Multirow_selector()
        self.load_settings(self.dlg_hydro_state)
        self.dlg_hydro_state.btn_ok.clicked.connect(
            partial(self.close_dialog, self.dlg_hydro_state))
        self.dlg_hydro_state.rejected.connect(
            partial(self.close_dialog, self.dlg_hydro_state))
        self.dlg_hydro_state.txt_name.setVisible(False)
        self.dlg_hydro_state.setWindowTitle("Hydrometer selector")
        utils_giswater.setWidgetText(
            self.dlg_hydro_state, self.dlg_hydro_state.lbl_unselected,
            self.controller.tr('Unselected hydrometers',
                               context_name='labels'))
        utils_giswater.setWidgetText(
            self.dlg_hydro_state, self.dlg_hydro_state.lbl_selected,
            self.controller.tr('Selected hydrometers', context_name='labels'))
        tableleft = "ext_rtc_hydrometer_state"
        tableright = "selector_hydrometer"
        field_id_left = "id"
        field_id_right = "state_id"
        self.multi_row_selector(self.dlg_hydro_state, tableleft, tableright,
                                field_id_left, field_id_right)

        # Open dialog
        self.open_dialog(self.dlg_hydro_state, maximize_button=False)

    def basic_search_plus(self):
        """ Button 32: Open search plus dialog """

        try:
            if self.search_plus is not None:
                if self.search_plus.dlg_search.tab_main.count() > 0:
                    # TODO pending translation
                    # Manage 'i18n' of the form and make it visible
                    # self.controller.translate_form(self.search_plus.dlg_search, 'search_plus')
                    self.search_plus.dock_dialog()
                else:
                    message = "Search Plus: Any layer has been found. Check parameters in table 'config_param_system'"
                    self.controller.show_warning(message, duration=20)
        except RuntimeError:
            pass

    def close_dialog(self, dlg):
        ParentAction.close_dialog(self, dlg)
        try:
            self.search_plus.refresh_data()
        except:
            pass
Esempio n. 7
0
class Master(ParentAction):
    def __init__(self, iface, settings, controller, plugin_dir):
        """ Class to control toolbar 'master' """
        self.config_dict = {}
        ParentAction.__init__(self, iface, settings, controller, plugin_dir)
        self.manage_new_psector = ManageNewPsector(iface, settings, controller,
                                                   plugin_dir)

    def set_project_type(self, project_type):
        self.project_type = project_type

    def master_new_psector(self, psector_id=None):
        """ Button 45: New psector """
        self.manage_new_psector.new_psector(psector_id, 'plan')

    def master_psector_mangement(self):
        """ Button 46: Psector management """

        # Create the dialog and signals
        self.dlg_psector_mng = Psector_management()

        self.load_settings(self.dlg_psector_mng)
        table_name = "plan_psector"
        column_id = "psector_id"

        # Tables
        qtbl_psm = self.dlg_psector_mng.findChild(QTableView, "tbl_psm")
        qtbl_psm.setSelectionBehavior(QAbstractItemView.SelectRows)

        # Set signals
        self.dlg_psector_mng.btn_cancel.clicked.connect(
            partial(self.close_dialog, self.dlg_psector_mng))
        self.dlg_psector_mng.rejected.connect(
            partial(self.close_dialog, self.dlg_psector_mng))
        self.dlg_psector_mng.btn_delete.clicked.connect(
            partial(self.multi_rows_delete, self.dlg_psector_mng, qtbl_psm,
                    table_name, column_id))
        self.dlg_psector_mng.btn_update_psector.clicked.connect(
            partial(self.update_current_psector, self.dlg_psector_mng,
                    qtbl_psm))
        self.dlg_psector_mng.txt_name.textChanged.connect(
            partial(self.filter_by_text, self.dlg_psector_mng, qtbl_psm,
                    self.dlg_psector_mng.txt_name, "plan_psector"))
        self.dlg_psector_mng.tbl_psm.doubleClicked.connect(
            partial(self.charge_psector, qtbl_psm))
        self.fill_table_psector(qtbl_psm, table_name)
        self.set_table_columns(self.dlg_psector_mng, qtbl_psm, table_name)
        self.set_label_current_psector(self.dlg_psector_mng)

        # Open form
        self.dlg_psector_mng.setWindowFlags(Qt.WindowStaysOnTopHint)
        self.open_dialog(self.dlg_psector_mng, dlg_name="psector_management")

    def update_current_psector(self, dialog, qtbl_psm):

        selected_list = qtbl_psm.selectionModel().selectedRows()
        if len(selected_list) == 0:
            message = "Any record selected"
            self.controller.show_warning(message)
            return
        row = selected_list[0].row()
        psector_id = qtbl_psm.model().record(row).value("psector_id")
        aux_widget = QLineEdit()
        aux_widget.setText(str(psector_id))
        self.upsert_config_param_user(dialog, aux_widget, "psector_vdefault")

        message = "Values has been updated"
        self.controller.show_info(message)

        self.fill_table(qtbl_psm, "plan_psector")
        self.set_table_columns(dialog, qtbl_psm, "plan_psector")
        self.set_label_current_psector(dialog)
        self.open_dialog(dialog)

    def upsert_config_param_user(self, dialog, widget, parameter):
        """ Insert or update values in tables with current_user control """

        tablename = "config_param_user"
        sql = ("SELECT * FROM " + self.schema_name + "." + tablename + ""
               " WHERE cur_user = current_user")
        rows = self.controller.get_rows(sql)
        exist_param = False
        if type(widget) != QDateEdit:
            if utils_giswater.getWidgetText(dialog, widget) != "":
                for row in rows:
                    if row[1] == parameter:
                        exist_param = True
                if exist_param:
                    sql = "UPDATE " + self.schema_name + "." + tablename + " SET value = "
                    if widget.objectName() != 'state_vdefault':
                        sql += (
                            "'" +
                            utils_giswater.getWidgetText(dialog, widget) + "'"
                            " WHERE cur_user = current_user AND parameter = '"
                            + parameter + "'")
                    else:
                        sql += (
                            "(SELECT id FROM " + self.schema_name +
                            ".value_state"
                            " WHERE name = '" +
                            utils_giswater.getWidgetText(dialog, widget) + "')"
                            " WHERE cur_user = current_user AND parameter = 'state_vdefault'"
                        )
                else:
                    sql = 'INSERT INTO ' + self.schema_name + '.' + tablename + '(parameter, value, cur_user)'
                    if widget.objectName() != 'state_vdefault':
                        sql += " VALUES ('" + parameter + "', '" + utils_giswater.getWidgetText(
                            dialog, widget) + "', current_user)"
                    else:
                        sql += (" VALUES ('" + parameter + "',"
                                " (SELECT id FROM " + self.schema_name +
                                ".value_state"
                                " WHERE name = '" +
                                utils_giswater.getWidgetText(dialog, widget) +
                                "'), current_user)")
        else:
            for row in rows:
                if row[1] == parameter:
                    exist_param = True
            _date = widget.dateTime().toString('yyyy-MM-dd')
            if exist_param:
                sql = ("UPDATE " + self.schema_name + "." + tablename + ""
                       " SET value = '" + str(_date) + "'"
                       " WHERE cur_user = current_user AND parameter = '" +
                       parameter + "'")
            else:
                sql = ("INSERT INTO " + self.schema_name + "." + tablename +
                       "(parameter, value, cur_user)"
                       " VALUES ('" + parameter + "', '" + _date +
                       "', current_user);")
        self.controller.execute_sql(sql)

    def filter_by_text(self, dialog, table, widget_txt, tablename):

        result_select = utils_giswater.getWidgetText(dialog, widget_txt)
        if result_select != 'null':
            expr = " name ILIKE '%" + result_select + "%'"
            # Refresh model with selected filter
            table.model().setFilter(expr)
            table.model().select()
        else:
            self.fill_table(table, tablename)

    def charge_psector(self, qtbl_psm):

        selected_list = qtbl_psm.selectionModel().selectedRows()
        if len(selected_list) == 0:
            message = "Any record selected"
            self.controller.show_warning(message)
            return
        row = selected_list[0].row()
        psector_id = qtbl_psm.model().record(row).value("psector_id")
        self.close_dialog(self.dlg_psector_mng)
        self.master_new_psector(psector_id)

    def multi_rows_delete(self, dialog, widget, table_name, column_id):
        """ Delete selected elements of the table
        :param QTableView widget: origin
        :param table_name: table origin
        :param column_id: Refers to the id of the source table
        """

        # Get selected rows
        selected_list = widget.selectionModel().selectedRows()
        if len(selected_list) == 0:
            message = "Any record selected"
            self.controller.show_warning(message)
            return

        inf_text = ""
        list_id = ""
        for i in range(0, len(selected_list)):
            row = selected_list[i].row()
            id_ = widget.model().record(row).value(str(column_id))
            inf_text += str(id_) + ", "
            list_id = list_id + "'" + str(id_) + "', "
        inf_text = inf_text[:-2]
        list_id = list_id[:-2]
        message = "Are you sure you want to delete these records?"
        answer = self.controller.ask_question(message, "Delete records",
                                              inf_text)

        if answer:
            sql = ("DELETE FROM " + self.schema_name + "." + table_name + ""
                   " WHERE " + column_id + " IN (" + list_id + ")")
            self.controller.execute_sql(sql)
            widget.model().select()
            sql = (
                "SELECT value FROM " + self.schema_name + ".config_param_user "
                " WHERE parameter = 'psector_vdefault' AND cur_user = current_user"
                " AND value IN (" + list_id + ")")
            row = self.controller.get_row(sql)
            if row is not None:
                sql = (
                    "DELETE FROM " + self.schema_name + ".config_param_user "
                    " WHERE parameter = 'psector_vdefault' AND cur_user = current_user"
                    " AND value = '" + row[0] + "'")
                self.controller.execute_sql(sql)
                utils_giswater.setWidgetText(dialog, 'lbl_vdefault_psector',
                                             '')

    def master_psector_selector(self):
        """ Button 47: Psector selector """

        # Create the dialog and signals
        self.dlg_psector_selector = Multirow_selector()
        self.load_settings(self.dlg_psector_selector)
        self.dlg_psector_selector.btn_ok.clicked.connect(
            partial(self.close_dialog, self.dlg_psector_selector))
        self.dlg_psector_selector.setWindowTitle("Psector selector")
        utils_giswater.setWidgetText(
            self.dlg_psector_selector, self.dlg_psector_selector.lbl_filter,
            self.controller.tr('Filter by: Psector name',
                               context_name='labels'))
        utils_giswater.setWidgetText(
            self.dlg_psector_selector,
            self.dlg_psector_selector.lbl_unselected,
            self.controller.tr('Unselected psectors', context_name='labels'))
        utils_giswater.setWidgetText(
            self.dlg_psector_selector, self.dlg_psector_selector.lbl_selected,
            self.controller.tr('Selected psectors', context_name='labels'))

        tableleft = "plan_psector"
        tableright = "selector_psector"
        field_id_left = "psector_id"
        field_id_right = "psector_id"
        self.multi_row_selector(self.dlg_psector_selector, tableleft,
                                tableright, field_id_left, field_id_right)
        self.open_dialog(self.dlg_psector_selector,
                         dlg_name="multirow_selector",
                         maximize_button=False)

    def master_estimate_result_new(self,
                                   tablename=None,
                                   result_id=None,
                                   index=0):
        """ Button 38: New estimate result """

        # Create dialog
        dlg_estimate_result_new = EstimateResultNew()
        self.load_settings(dlg_estimate_result_new)

        # Set signals
        dlg_estimate_result_new.btn_close.clicked.connect(
            partial(self.close_dialog, dlg_estimate_result_new))
        dlg_estimate_result_new.prices_coefficient.setValidator(
            QDoubleValidator())

        self.populate_cmb_result_type(dlg_estimate_result_new.cmb_result_type,
                                      'plan_result_type', False)

        if result_id != 0 and result_id:
            sql = ("SELECT * FROM " + self.schema_name + "." + tablename + " "
                   " WHERE result_id = '" + str(result_id) +
                   "' AND current_user = cur_user")
            row = self.controller.get_row(sql)
            if row is None:
                message = "Any record found for current user in table"
                self.controller.show_warning(message,
                                             parameter='plan_result_cat')
                return

            utils_giswater.setWidgetText(dlg_estimate_result_new,
                                         dlg_estimate_result_new.result_name,
                                         row['result_id'])
            dlg_estimate_result_new.cmb_result_type.setCurrentIndex(index)
            utils_giswater.setWidgetText(
                dlg_estimate_result_new,
                dlg_estimate_result_new.prices_coefficient,
                row['network_price_coeff'])
            utils_giswater.setWidgetText(dlg_estimate_result_new,
                                         dlg_estimate_result_new.observ,
                                         row['descript'])
            dlg_estimate_result_new.result_name.setEnabled(False)
            dlg_estimate_result_new.cmb_result_type.setEnabled(False)
            dlg_estimate_result_new.prices_coefficient.setEnabled(False)
            dlg_estimate_result_new.observ.setEnabled(False)
            dlg_estimate_result_new.btn_calculate.setText("Close")
            dlg_estimate_result_new.btn_calculate.clicked.connect(
                partial(self.close_dialog))
        else:
            dlg_estimate_result_new.btn_calculate.clicked.connect(
                partial(self.master_estimate_result_new_calculate,
                        dlg_estimate_result_new))
        # TODO pending translation
        # Manage i18n of the form and open it
        # self.controller.translate_form(dlg_estimate_result_new, 'estimate_result_new')

        self.open_dialog(dlg_estimate_result_new,
                         dlg_name="plan_estimate_result_new",
                         maximize_button=False)

    def populate_cmb_result_type(self, combo, table_name, allow_nulls=True):

        sql = ("SELECT id, name"
               " FROM " + self.schema_name + "." + table_name + ""
               " ORDER BY name")
        rows = self.controller.get_rows(sql)
        if not rows:
            return

        combo.blockSignals(True)
        combo.clear()
        if allow_nulls:
            combo.addItem("", "")
        records_sorted = sorted(rows, key=operator.itemgetter(1))
        for record in records_sorted:
            combo.addItem(record[1], record)
        combo.blockSignals(False)

    def master_estimate_result_new_calculate(self, dialog):
        """ Execute function 'gw_fct_plan_estimate_result' """

        # Get values from form
        result_name = utils_giswater.getWidgetText(dialog, "result_name")
        combo = utils_giswater.getWidget(dialog, "cmb_result_type")
        elem = combo.itemData(combo.currentIndex())
        result_type = str(elem[0])
        coefficient = utils_giswater.getWidgetText(dialog,
                                                   "prices_coefficient")
        observ = utils_giswater.getWidgetText(dialog, "observ")

        if result_name == 'null':
            message = "Please, introduce a result name"
            self.controller.show_warning(message)
            return
        if coefficient == 'null':
            message = "Please, introduce a coefficient value"
            self.controller.show_warning(message)
            return

        # Check data executing function 'gw_fct_epa_audit_check_data'
        sql = "SELECT " + self.schema_name + ".gw_fct_plan_audit_check_data(" + str(
            result_type) + ");"
        row = self.controller.get_row(sql, log_sql=True)
        if not row:
            return

        if row[0] > 0:
            msg = ("It is not possible to execute the economic result."
                   "There are errors on your project. Review it!")
            if result_type == 1:
                fprocesscat_id = 15
            else:
                fprocesscat_id = 16
            sql_details = ("SELECT table_id, column_id, error_message"
                           " FROM audit_check_data"
                           " WHERE fprocesscat_id = " + str(fprocesscat_id) +
                           " AND enabled is false")
            inf_text = "For more details execute query:\n" + sql_details
            title = "Execute epa model"
            self.controller.show_info_box(msg,
                                          title,
                                          inf_text,
                                          parameter=row[0])
            return

        # Execute function 'gw_fct_plan_result'
        sql = ("SELECT " + self.schema_name + ".gw_fct_plan_result('" +
               result_name + "', " + result_type + ", '" + coefficient +
               "', '" + observ + "');")
        status = self.controller.execute_sql(sql)
        if status:
            message = "Values has been updated"
            self.controller.show_info(message)

        # Refresh canvas and close dialog
        self.iface.mapCanvas().refreshAllLayers()
        self.close_dialog(dialog)

    def master_estimate_result_selector(self):
        """ Button 49: Estimate result selector """

        # Create dialog
        self.dlg_estimate_result_selector = EstimateResultSelector()
        self.load_settings(self.dlg_estimate_result_selector)

        # Populate combo
        self.populate_combo(
            self.dlg_estimate_result_selector.rpt_selector_result_id,
            'plan_result_selector')

        # Set current value
        table_name = "om_result_cat"
        sql = (
            "SELECT name FROM " + self.schema_name + "." + table_name + " "
            " WHERE cur_user = current_user AND result_type = 1 AND result_id = (SELECT result_id FROM "
            + self.schema_name + ".plan_result_selector)")
        row = self.controller.get_row(sql)
        if row:
            utils_giswater.setWidgetText(
                self.dlg_estimate_result_selector,
                self.dlg_estimate_result_selector.rpt_selector_result_id,
                str(row[0]))

        # Set signals
        self.dlg_estimate_result_selector.btn_accept.clicked.connect(
            partial(self.master_estimate_result_selector_accept))
        self.dlg_estimate_result_selector.btn_cancel.clicked.connect(
            partial(self.close_dialog, self.dlg_estimate_result_selector))
        # TODO pending translation
        # Manage i18n of the form and open it
        # self.controller.translate_form(self.dlg_estimate_result_selector, 'estimate_result_selector')
        self.open_dialog(self.dlg_estimate_result_selector,
                         dlg_name="plan_estimate_result_selector",
                         maximize_button=False)

    def populate_combo(self, combo, table_result):

        table_name = "om_result_cat"
        sql = ("SELECT name, result_id"
               " FROM " + self.schema_name + "." + table_name + " "
               " WHERE cur_user = current_user"
               " AND result_type = 1"
               " ORDER BY name")
        rows = self.controller.get_rows(sql)
        if not rows:
            return

        combo.blockSignals(True)
        combo.clear()
        records_sorted = sorted(rows, key=operator.itemgetter(1))
        for record in records_sorted:
            combo.addItem(record[0], record)
        combo.blockSignals(False)

        # Check if table exists
        if not self.controller.check_table(table_result):
            message = "Table not found"
            self.controller.show_warning(message, parameter=table_result)
            return

        sql = ("SELECT result_id FROM " + self.schema_name + "." +
               table_result + " "
               " WHERE cur_user = current_user")
        row = self.controller.get_row(sql)
        if row:
            utils_giswater.setSelectedItem(self.dlg_estimate_result_selector,
                                           combo, str(row[0]))

    def upsert(self, combo, tablename):

        # Check if table exists
        if not self.controller.check_table(tablename):
            message = "Table not found"
            self.controller.show_warning(message, parameter=tablename)
            return

        result_id = utils_giswater.get_item_data(
            self.dlg_estimate_result_selector, combo, 1)
        sql = ("DELETE FROM " + self.schema_name + "." + tablename +
               " WHERE current_user = cur_user;"
               "\nINSERT INTO " + self.schema_name + "." + tablename +
               " (result_id, cur_user)"
               " VALUES(" + str(result_id) + ", current_user);")
        status = self.controller.execute_sql(sql)
        if status:
            message = "Values has been updated"
            self.controller.show_info(message)

        # Refresh canvas
        self.iface.mapCanvas().refreshAllLayers()
        self.close_dialog(self.dlg_estimate_result_selector)

    def master_estimate_result_selector_accept(self):
        """ Update value of table 'plan_result_selector' """

        self.upsert(self.dlg_estimate_result_selector.rpt_selector_result_id,
                    'plan_result_selector')

    def master_estimate_result_manager(self):
        """ Button 50: Plan estimate result manager """

        # Create the dialog and signals
        self.dlg_merm = EstimateResultManager()
        self.load_settings(self.dlg_merm)

        #TODO activar este boton cuando sea necesario
        self.dlg_merm.btn_delete.setVisible(False)

        # Tables
        tablename = 'om_result_cat'
        self.tbl_om_result_cat = self.dlg_merm.findChild(
            QTableView, "tbl_om_result_cat")
        self.tbl_om_result_cat.setSelectionBehavior(
            QAbstractItemView.SelectRows)

        # Set signals
        self.dlg_merm.tbl_om_result_cat.doubleClicked.connect(
            partial(self.charge_plan_estimate_result, self.dlg_merm))
        self.dlg_merm.btn_cancel.clicked.connect(
            partial(self.close_dialog, self.dlg_merm))
        self.dlg_merm.rejected.connect(
            partial(self.close_dialog, self.dlg_merm))
        self.dlg_merm.btn_delete.clicked.connect(
            partial(self.delete_merm, self.dlg_merm))
        self.dlg_merm.txt_name.textChanged.connect(
            partial(self.filter_merm, self.dlg_merm, tablename))

        set_edit_strategy = QSqlTableModel.OnManualSubmit
        self.fill_table(self.tbl_om_result_cat, tablename, set_edit_strategy)
        #self.set_table_columns(self.tbl_om_result_cat, tablename)

        # Open form
        self.dlg_merm.setWindowFlags(Qt.WindowStaysOnTopHint)
        self.open_dialog(self.dlg_merm,
                         dlg_name="plan_estimate_result_manager")

    def charge_plan_estimate_result(self, dialog):
        """ Send selected plan to 'plan_estimate_result_new.ui' """

        selected_list = dialog.tbl_om_result_cat.selectionModel().selectedRows(
        )
        if len(selected_list) == 0:
            message = "Any record selected"
            self.controller.show_warning(message)
            return

        row = selected_list[0].row()
        result_id = dialog.tbl_om_result_cat.model().record(row).value(
            "result_id")
        self.close_dialog(dialog)
        self.master_estimate_result_new('om_result_cat', result_id, 0)

    def delete_merm(self, dialog):
        """ Delete selected row from 'master_estimate_result_manager' dialog from selected tab """

        self.multi_rows_delete(dialog, dialog.tbl_om_result_cat,
                               'plan_result_cat', 'result_id')

    def filter_merm(self, dialog, tablename):
        """ Filter rows from 'master_estimate_result_manager' dialog from selected tab """

        self.filter_by_text(dialog, dialog.tbl_om_result_cat, dialog.txt_name,
                            tablename)
class MincutConfig(ParentAction):
    
    def __init__(self, mincut):
        """ Class constructor """
        self.mincut = mincut
        self.canvas = mincut.canvas
        self.plugin_dir = mincut.plugin_dir
        self.controller = self.mincut.controller
        self.schema_name = self.controller.schema_name
        

    def config(self):
        """ B5-99: Config """
        
        # Dialog multi_selector
        self.dlg_multi = Multi_selector()

        self.tbl_config = self.dlg_multi.findChild(QTableView, "tbl")
        self.btn_insert = self.dlg_multi.findChild(QPushButton, "btn_insert")
        self.btn_delete = self.dlg_multi.findChild(QPushButton, "btn_delete")
        
        table = "anl_mincut_selector_valve"
        self.menu_valve = QMenu()
        self.fill_insert_menu(table)

        self.dlg_multi.btn_insert.clicked.connect(partial(self.fill_insert_menu, table))
        btn_close = self.dlg_multi.findChild(QPushButton, "btn_close")
        btn_close.clicked.connect(partial(self.close_dialog, self.dlg_multi))


        self.dlg_multi.btn_insert.setMenu(self.menu_valve)
        self.dlg_multi.btn_delete.clicked.connect(partial(self.delete_records_config, self.tbl_config, table))

        self.fill_table_config(self.tbl_config, self.schema_name + "." + table)
        
        # Open form
        self.dlg_multi.setWindowFlags(Qt.WindowStaysOnTopHint)
        self.dlg_multi.open()


    def fill_insert_menu(self, table):
        """ Insert menu on QPushButton->QMenu """
        
        self.menu_valve.clear()
        node_type = "VALVE"
        sql = ("SELECT id FROM " + self.schema_name + ".node_type"
               " WHERE type = '" + node_type + "' ORDER BY id")
        rows = self.controller.get_rows(sql)
        if not rows:
            return
        
        # Fill menu
        for row in rows:
            elem = row[0]
            # If not exist in table _selector_state insert to menu
            # Check if we already have data with selected id
            sql = "SELECT id FROM " + self.schema_name + "." + table + " WHERE id = '" + elem + "'"
            rows = self.controller.get_rows(sql)
            if not rows:
                self.menu_valve.addAction(elem, partial(self.insert, elem, table))



    def insert(self, id_action, table):
        """ On action(select value from menu) execute SQL """

        # Insert value into database
        sql = "INSERT INTO " + self.schema_name + "." + table + " (id) VALUES ('" + id_action + "')"
        self.controller.execute_sql(sql)
        self.fill_table_config(self.tbl_config, self.schema_name+"."+table)
        self.fill_insert_menu(table)
        self.dlg_multi.btn_insert.setMenu(self.menu_valve)

    def fill_table_config(self, widget, table_name):
        """ Set a model with selected filter. Attach that model to selected table """

        # Set model
        model = QSqlTableModel()
        model.setTable(table_name)
        model.setEditStrategy(QSqlTableModel.OnManualSubmit)
        model.setSort(0, 0)
        model.select()

        # Check for errors
        if model.lastError().isValid():
            self.controller.show_warning(model.lastError().text())

        # Attach model to table view
        widget.setModel(model) 
        
        
    def delete_records_config(self, widget, table_name):
        """ Delete selected elements of the table """

        # Get selected rows
        selected_list = widget.selectionModel().selectedRows()
        if len(selected_list) == 0:
            message = "Any record selected"
            self.controller.show_warning(message)
            return

        inf_text = ""
        list_id = ""
        for i in range(0, len(selected_list)):
            row = selected_list[i].row()
            id_ = widget.model().record(row).value("id")
            inf_text += str(id_) + ", "
            list_id = list_id + "'" + str(id_) + "', "
        inf_text = inf_text[:-2]
        list_id = list_id[:-2]
        message = "Are you sure you want to delete these records?"
        title = "Delete records"
        answer = self.controller.ask_question(message, title, inf_text)
        if answer:
            sql = ("DELETE FROM " + self.schema_name + "." + table_name + ""
                   " WHERE id IN (" + list_id + ")")
            self.controller.execute_sql(sql)
            widget.model().select()
        self.fill_insert_menu('anl_mincut_selector_valve')

    def mg_mincut_management(self):
        """ Button 27: Mincut management """

        self.action = "mg_mincut_management"

        # Create the dialog and signals
        self.dlg_min_edit = Mincut_edit()
        self.load_settings(self.dlg_min_edit)
        self.set_icon(self.dlg_min_edit.btn_selector_mincut, "191")

        self.tbl_mincut_edit = self.dlg_min_edit.findChild(QTableView, "tbl_mincut_edit")
        self.txt_mincut_id = self.dlg_min_edit.findChild(QLineEdit, "txt_mincut_id")
        self.tbl_mincut_edit.setSelectionBehavior(QAbstractItemView.SelectRows)        
        
        # Adding auto-completion to a QLineEdit
        self.completer = QCompleter()
        self.txt_mincut_id.setCompleter(self.completer)
        model = QStringListModel()

        sql = "SELECT DISTINCT(id) FROM " + self.schema_name + ".v_ui_anl_mincut_result_cat "
        rows = self.controller.get_rows(sql)
        values = []
        for row in rows:
            values.append(str(row[0]))

        model.setStringList(values)
        self.completer.setModel(model)
        self.txt_mincut_id.textChanged.connect(partial(self.filter_by_id, self.tbl_mincut_edit, self.txt_mincut_id, "v_ui_anl_mincut_result_cat"))

        self.dlg_min_edit.tbl_mincut_edit.doubleClicked.connect(self.open_mincut)
        self.dlg_min_edit.btn_cancel.clicked.connect(partial(self.close_dialog, self.dlg_min_edit))
        self.dlg_min_edit.rejected.connect(partial(self.close_dialog, self.dlg_min_edit))
        self.dlg_min_edit.btn_delete.clicked.connect(partial(self.delete_mincut_management, self.tbl_mincut_edit, "v_ui_anl_mincut_result_cat", "id"))
        self.dlg_min_edit.btn_selector_mincut.clicked.connect(partial(self.mincut_selector))

        # Fill ComboBox state
        sql = ("SELECT name"
               " FROM " + self.schema_name + ".anl_mincut_cat_state"
               " ORDER BY name")
        rows = self.controller.get_rows(sql)
        utils_giswater.fillComboBox(self.dlg_min_edit, "state_edit", rows)
        self.dlg_min_edit.state_edit.activated.connect(partial(self.filter_by_state, self.tbl_mincut_edit, self.dlg_min_edit.state_edit, "v_ui_anl_mincut_result_cat"))

        # Set a model with selected filter. Attach that model to selected table
        self.fill_table_mincut_management(self.tbl_mincut_edit, self.schema_name + ".v_ui_anl_mincut_result_cat")
        self.set_table_columns(self.dlg_min_edit, self.tbl_mincut_edit, "v_ui_anl_mincut_result_cat")

        #self.mincut.set_table_columns(self.tbl_mincut_edit, "v_ui_anl_mincut_result_cat")

        # Open the dialog
        self.dlg_min_edit.setWindowFlags(Qt.WindowStaysOnTopHint)
        self.dlg_min_edit.show()


    def mincut_selector(self):
        self.dlg_mincut_sel = Multirow_selector()
        self.load_settings(self.dlg_mincut_sel)

        self.dlg_mincut_sel.btn_ok.clicked.connect(partial(self.close_dialog, self.dlg_mincut_sel))
        self.dlg_mincut_sel.rejected.connect(partial(self.close_dialog, self.dlg_mincut_sel))
        self.dlg_mincut_sel.setWindowTitle("Mincut selector")
        utils_giswater.setWidgetText(self.dlg_mincut_sel, 'lbl_filter', self.controller.tr('Filter by: Mincut id', context_name='labels'))
        utils_giswater.setWidgetText(self.dlg_mincut_sel, 'lbl_unselected', self.controller.tr('Unselected mincut', context_name='labels'))
        utils_giswater.setWidgetText(self.dlg_mincut_sel, 'lbl_selected', self.controller.tr('Selected mincut', context_name='labels'))

        tableleft = "v_ui_anl_mincut_result_cat"
        tableright = "anl_mincut_result_selector"
        field_id_left = "id"
        field_id_right = "result_id"
        index = [0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
        self.multi_row_selector(self.dlg_mincut_sel, tableleft, tableright, field_id_left, field_id_right, index=index)
        self.dlg_mincut_sel.btn_select.clicked.connect(partial(self.mincut.set_visible_mincut_layers))
        # Open dialog
        self.open_dialog(self.dlg_mincut_sel, maximize_button=False)


    def open_mincut(self):
        """ Open mincut form with selected record of the table """

        selected_list = self.tbl_mincut_edit.selectionModel().selectedRows()
        if len(selected_list) == 0:
            message = "Any record selected"
            self.controller.show_warning(message)
            return
        
        row = selected_list[0].row()

        # Get mincut_id from selected row
        result_mincut_id = self.tbl_mincut_edit.model().record(row).value("id")

        # Close this dialog and open selected mincut
        self.close_dialog(self.dlg_min_edit)
        self.mincut.init_mincut_form()
        self.mincut.load_mincut(result_mincut_id)


    def filter_by_id(self, table, widget_txt, tablename):

        id_ = utils_giswater.getWidgetText(self.dlg_min_edit, widget_txt)
        if id_ != 'null':
            expr = " id = '" + id_ + "'"
            # Refresh model with selected filter
            table.model().setFilter(expr)
            table.model().select()
        else:
            self.fill_table_mincut_management(self.tbl_mincut_edit, self.schema_name + "." + tablename)


    def filter_by_state(self, table, widget, tablename):
        
        state = utils_giswater.getWidgetText(self.dlg_min_edit, widget)
        if state != 'null':
            expr_filter = " state = '" + str(state) + "'"
            # Refresh model with selected expr_filter
            table.model().setFilter(expr_filter)
            table.model().select()
        else:
            self.fill_table_mincut_management(self.tbl_mincut_edit, self.schema_name + "." + tablename)


    def fill_table_mincut_management(self, widget, table_name):
        """ Set a model with selected filter. Attach that model to selected table """

        # Set model
        model = QSqlTableModel();
        model.setTable(table_name)
        model.setEditStrategy(QSqlTableModel.OnManualSubmit)
        model.sort(0, 1)
        model.select()

        # Check for errors
        if model.lastError().isValid():
            self.controller.show_warning(model.lastError().text())

        # Attach model to table view
        widget.setModel(model)


    def delete_mincut_management(self, widget, table_name, column_id):
        """ Delete selected elements of the table (by id) """
        
        # Get selected rows
        selected_list = widget.selectionModel().selectedRows()
        if len(selected_list) == 0:
            message = "Any record selected"
            self.controller.show_warning(message)
            return

        inf_text = ""
        list_id = ""
        for i in range(0, len(selected_list)):
            row = selected_list[i].row()
            id_ = widget.model().record(row).value(str(column_id))
            inf_text+= str(id_) + ", "
            list_id = list_id + "'" + str(id_) + "', "
        inf_text = inf_text[:-2]
        list_id = list_id[:-2]
        message = "Are you sure you want to delete these records?"
        title = "Delete records"
        answer = self.controller.ask_question(message, title, inf_text)
        if answer:
            sql = ("DELETE FROM " + self.schema_name + "." + table_name + ""
                   " WHERE " + column_id + " IN (" + list_id + ")")
            self.controller.execute_sql(sql)
            widget.model().select()
            layer = self.controller.get_layer_by_tablename('v_anl_mincut_result_node')
            if layer is not None:
                layer.triggerRepaint()
            layer = self.controller.get_layer_by_tablename('v_anl_mincut_result_connec')
            if layer is not None:
                layer.triggerRepaint()
            layer = self.controller.get_layer_by_tablename('v_anl_mincut_result_arc')
            if layer is not None:
                layer.triggerRepaint()
            layer = self.controller.get_layer_by_tablename('v_anl_mincut_result_valve')
            if layer is not None:
                layer.triggerRepaint()
            layer = self.controller.get_layer_by_tablename('v_anl_mincut_result_cat')
            if layer is not None:
                layer.triggerRepaint()
            layer = self.controller.get_layer_by_tablename('v_anl_mincut_result_hydrometer')
            if layer is not None:
                layer.triggerRepaint()