def enable_feature_type(self, dialog, widget_name='tbl_relation'):

        feature_type = qt_tools.getWidget(dialog, 'feature_type')
        widget_table = qt_tools.getWidget(dialog, widget_name)
        if feature_type is not None and widget_table is not None:
            if len(self.ids) > 0:
                feature_type.setEnabled(False)
            else:
                feature_type.setEnabled(True)
    def set_completer_object(self, dialog, table_object):
        """ Set autocomplete of widget @table_object + "_id" 
            getting id's from selected @table_object 
        """

        widget = qt_tools.getWidget(dialog, table_object + "_id")
        if not widget:
            return

        # Set SQL
        field_object_id = "id"
        if table_object == "element":
            field_object_id = table_object + "_id"
        sql = (f"SELECT DISTINCT({field_object_id})"
               f" FROM {table_object}")
        rows = self.controller.get_rows(sql)
        if rows is None:
            return

        for i in range(0, len(rows)):
            aux = rows[i]
            rows[i] = str(aux[0])

        # Set completer and model: add autocomplete in the widget
        self.completer = QCompleter()
        self.completer.setCaseSensitivity(Qt.CaseInsensitive)
        widget.setCompleter(self.completer)
        model = QStringListModel()
        model.setStringList(rows)
        self.completer.setModel(model)
    def tab_feature_changed(self, dialog, table_object, excluded_layers=[]):
        """ Set geom_type and layer depending selected tab
            @table_object = ['doc' | 'element' | 'cat_work']
        """

        self.get_values_from_form(dialog)
        tab_position = dialog.tab_feature.currentIndex()
        if tab_position == 0:
            self.geom_type = "arc"
        elif tab_position == 1:
            self.geom_type = "node"
        elif tab_position == 2:
            self.geom_type = "connec"
        elif tab_position == 3:
            self.geom_type = "element"
        elif tab_position == 4:
            self.geom_type = "gully"

        self.hide_generic_layers(excluded_layers=excluded_layers)
        widget_name = f"tbl_{table_object}_x_{self.geom_type}"
        viewname = f"v_edit_{self.geom_type}"
        self.widget = qt_tools.getWidget(dialog, widget_name)

        # Adding auto-completion to a QLineEdit
        self.set_completer_feature_id(dialog.feature_id, self.geom_type, viewname)

        self.iface.actionPan().trigger()
    def reset_model(self, dialog, table_object, geom_type):
        """ Reset model of the widget """

        table_relation = f"{table_object}_x_{geom_type}"
        widget_name = f"tbl_{table_relation}"
        widget = qt_tools.getWidget(dialog, widget_name)
        if widget:
            widget.setModel(None)
    def reload_qtable(self, dialog, geom_type):
        """ Reload QtableView """

        value = qt_tools.getWidgetText(dialog, dialog.psector_id)
        expr = f"psector_id = '{value}'"
        qtable = qt_tools.getWidget(dialog, f'tbl_psector_x_{geom_type}')
        self.fill_table_by_expr(qtable, f"plan_psector_x_{geom_type}", expr)
        self.set_table_columns(dialog, qtable, f"plan_psector_x_{geom_type}")
        self.refresh_map_canvas()
Ejemplo n.º 6
0
    def duplicate_psector(self):

        id_psector = qt_tools.get_item_data(
            self.dlg_duplicate_psector,
            self.dlg_duplicate_psector.duplicate_psector, 0)
        new_psector_name = qt_tools.getWidgetText(
            self.dlg_duplicate_psector,
            self.dlg_duplicate_psector.new_psector_name)

        # Create body
        feature = '"type":"PSECTOR"'
        extras = f'"psector_id":"{id_psector}", "new_psector_name":"{new_psector_name}"'
        body = create_body(feature=feature, extras=extras)
        body = body.replace('""', 'null')
        complet_result = self.controller.get_json('gw_fct_psector_duplicate',
                                                  body)
        if not complet_result:
            message = 'Function gw_fct_psector_duplicate executed with no result'
            self.controller.show_message(message, 3)
            return

        # Populate tab info
        change_tab = False
        data = complet_result['body']['data']
        for k, v in list(data.items()):
            if str(k) == "info":
                change_tab = populate_info_text(self.dlg_duplicate_psector,
                                                data)

        # Close dialog
        if not change_tab:
            close_dialog(self.dlg_duplicate_psector)
        else:
            qt_tools.getWidget(
                self.dlg_duplicate_psector,
                self.dlg_duplicate_psector.btn_accept).setEnabled(False)
            self.dlg_duplicate_psector.setWindowTitle(
                f'SUCCESS IN DUPLICATING PSECTOR')

        self.is_duplicated.emit()
    def set_table_model(self, dialog, table_object, geom_type, expr_filter):
        """ Sets a TableModel to @widget_name attached to
            @table_name and filter @expr_filter 
        """

        expr = None
        if expr_filter:
            # Check expression
            (is_valid, expr) = self.check_expression(expr_filter)  # @UnusedVariable
            if not is_valid:
                return expr

        # Set a model with selected filter expression
        table_name = f"v_edit_{geom_type}"
        if self.schema_name not in table_name:
            table_name = self.schema_name + "." + table_name

        # Set the model
        model = QSqlTableModel()
        model.setTable(table_name)
        model.setEditStrategy(QSqlTableModel.OnManualSubmit)
        model.select()
        if model.lastError().isValid():
            self.controller.show_warning(model.lastError().text())
            return expr

        # Attach model to selected widget
        if type(table_object) is str:
            widget = qt_tools.getWidget(dialog, table_object)
            if not widget:
                message = "Widget not found"
                self.controller.log_info(message, parameter=table_object)
                return expr
        elif type(table_object) is QTableView:
            # self.controller.log_debug(f"set_table_model: {table_object.objectName()}")
            widget = table_object
        else:
            msg = "Table_object is not a table name or QTableView"
            self.controller.log_info(msg)
            return expr

        if expr_filter:
            widget.setModel(model)
            widget.model().setFilter(expr_filter)
            widget.model().select()
        else:
            widget.setModel(None)

        return expr
Ejemplo n.º 8
0
def set_table_columns(dialog, widget, table_name, sort_order=0, isQStandardItemModel=False, schema_name=None):
    """ Configuration of tables. Set visibility and width of columns """

    widget = qt_tools.getWidget(dialog, widget)
    if not widget:
        return

    if schema_name is not None:
        config_table = f"{schema_name}.config_form_tableview"
    else:
        config_table = f"config_form_tableview"

    # Set width and alias of visible columns
    columns_to_delete = []
    sql = (f"SELECT columnindex, width, alias, status"
           f" FROM {config_table}"
           f" WHERE tablename = '{table_name}'"
           f" ORDER BY columnindex")
    rows = global_vars.controller.get_rows(sql, log_info=False)
    if not rows:
        return

    for row in rows:
        if not row['status']:
            columns_to_delete.append(row['columnindex'] - 1)
        else:
            width = row['width']
            if width is None:
                width = 100
            widget.setColumnWidth(row['columnindex'] - 1, width)
            widget.model().setHeaderData(row['columnindex'] - 1, Qt.Horizontal, row['alias'])

    # Set order
    if isQStandardItemModel:
        widget.model().sort(sort_order, Qt.AscendingOrder)
    else:
        widget.model().setSort(sort_order, Qt.AscendingOrder)
        widget.model().select()
    # Delete columns
    for column in columns_to_delete:
        widget.hideColumn(column)

    return widget
    def reload_table(self, dialog, table_object, geom_type, expr_filter):
        """ Reload @widget with contents of @tablename applying selected @expr_filter """

        if type(table_object) is str:
            widget_name = f"tbl_{table_object}_x_{geom_type}"
            widget = qt_tools.getWidget(dialog, widget_name)
            if not widget:
                message = "Widget not found"
                self.controller.log_info(message, parameter=widget_name)
                return None
        elif type(table_object) is QTableView:
            widget = table_object
        else:
            msg = "Table_object is not a table name or QTableView"
            self.controller.log_info(msg)
            return None

        expr = self.set_table_model(dialog, widget, geom_type, expr_filter)
        return expr
    def delete_records(self, dialog, table_object, query=False):
        """ Delete selected elements of the table """

        self.disconnect_signal_selection_changed()

        if type(table_object) is str:
            widget_name = f"tbl_{table_object}_x_{self.geom_type}"
            widget = qt_tools.getWidget(dialog, widget_name)
            if not widget:
                message = "Widget not found"
                self.controller.show_warning(message, parameter=widget_name)
                return
        elif type(table_object) is QTableView:
            widget = table_object
        else:
            msg = "Table_object is not a table name or QTableView"
            self.controller.log_info(msg)
            return

        # Control when QTableView is void or has no model
        try:
            # Get selected rows
            selected_list = widget.selectionModel().selectedRows()
        except AttributeError as e:
            selected_list = []

        if len(selected_list) == 0:
            message = "Any record selected"
            self.controller.show_info_box(message)
            return

        if query:
            full_list = widget.model()
            for x in range(0, full_list.rowCount()):
                self.ids.append(widget.model().record(x).value(f"{self.geom_type}_id"))
        else:
            self.ids = self.list_ids[self.geom_type]

        field_id = self.geom_type + "_id"

        del_id = []
        inf_text = ""
        list_id = ""
        for i in range(0, len(selected_list)):
            row = selected_list[i].row()
            id_feature = widget.model().record(row).value(field_id)
            inf_text += f"{id_feature}, "
            list_id += f"'{id_feature}', "
            del_id.append(id_feature)
        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:
            for el in del_id:
                self.ids.remove(el)
        else:
            return

        expr_filter = None
        expr = None
        if len(self.ids) > 0:

            # Set expression filter with features in the list
            expr_filter = f'"{field_id}" IN ('
            for i in range(len(self.ids)):
                expr_filter += f"'{self.ids[i]}', "
            expr_filter = expr_filter[:-2] + ")"

            # Check expression
            (is_valid, expr) = self.check_expression(expr_filter)  # @UnusedVariable
            if not is_valid:
                return

        # Update model of the widget with selected expr_filter
        if query:
            self.delete_feature_at_plan(dialog, self.geom_type, list_id)
            self.reload_qtable(dialog, self.geom_type)
        else:
            self.reload_table(dialog, table_object, self.geom_type, expr_filter)
            self.apply_lazy_init(table_object)

        # Select features with previous filter
        # Build a list of feature id's and select them
        self.select_features_by_ids(self.geom_type, expr)

        if query:
            self.remove_selection()

        # Update list
        self.list_ids[self.geom_type] = self.ids
        self.enable_feature_type(dialog, table_object)
        self.connect_signal_selection_changed(dialog, table_object)