Skip to content

NCS pathway editor

Dialog for creating or editing an NCS pathway entry.

NcsPathwayEditorDialog

NcsPathwayEditorDialog(parent=None, ncs_pathway=None, excluded_names=None)

Bases: QDialog, WidgetUi

Dialog for creating or editing an NCS pathway entry.

Source code in src/cplus_plugin/gui/ncs_pathway_editor_dialog.py
def __init__(self, parent=None, ncs_pathway=None, excluded_names=None):
    super().__init__(parent)
    self.setupUi(self)

    QgsGui.enableAutoGeometryRestore(self)

    self._message_bar = QgsMessageBar()
    self.vl_notification.addWidget(self._message_bar)

    self._carbon_model = CarbonLayerModel(self)
    self.lst_carbon_layers.setModel(self._carbon_model)
    self.lst_carbon_layers.selectionModel().selectionChanged.connect(
        self._on_selection_changed
    )

    self.txt_description.textChanged.connect(self.description_changed)

    self.buttonBox.accepted.connect(self._on_accepted)
    self.btn_add_layer.clicked.connect(self._on_select_file)

    icon_pixmap = QtGui.QPixmap(ICON_PATH)
    self.icon_la.setPixmap(icon_pixmap)

    help_icon = FileUtils.get_icon("mActionHelpContents_green.svg")
    self.btn_help.setIcon(help_icon)
    self.btn_help.clicked.connect(self.open_help)

    add_icon = FileUtils.get_icon("symbologyAdd.svg")
    self.btn_add_carbon.setIcon(add_icon)
    self.btn_add_carbon.clicked.connect(self._on_add_carbon_layer)

    remove_icon = FileUtils.get_icon("symbologyRemove.svg")
    self.btn_delete_carbon.setIcon(remove_icon)
    self.btn_delete_carbon.setEnabled(False)
    self.btn_delete_carbon.clicked.connect(self._on_remove_carbon_layer)

    edit_icon = FileUtils.get_icon("mActionToggleEditing.svg")
    self.btn_edit_carbon.setIcon(edit_icon)
    self.btn_edit_carbon.setEnabled(False)
    self.btn_edit_carbon.clicked.connect(self._on_edit_carbon_layer)

    pathways = settings_manager.get_default_layers("ncs_pathway")
    self.cbo_default_layer.addItem("")
    self.cbo_default_layer.addItems([p["name"] for p in pathways])
    self.cbo_default_layer.setCurrentIndex(0)
    self.cbo_default_layer.currentIndexChanged.connect(
        self._on_default_layer_selection_changed
    )

    add_default_icon = FileUtils.get_icon("mActionAddDefaultCarbonLayer.svg")
    self.btn_add_default_carbon.setIcon(add_default_icon)
    self.btn_add_default_carbon.setMenu(QtWidgets.QMenu(self))
    items = settings_manager.get_default_layers("ncs_carbon")
    for item in items:
        action = QtWidgets.QAction(item["name"], self)
        action.triggered.connect(
            lambda checked, item=item: self._on_default_carbon_layer_selected(item)
        )
        self.btn_add_default_carbon.menu().addAction(action)

    self._excluded_names = excluded_names
    if excluded_names is None:
        self._excluded_names = []

    self._edit_mode = False
    self._layer = None
    self._ncs_pathway = ncs_pathway
    if self._ncs_pathway is not None:
        self._edit_mode = True
        self._layer = self._ncs_pathway.to_map_layer()
        self._update_controls()

edit_mode property

edit_mode

Returns the state of the editor.

Returns:

Type Description
bool

True if the editor is editing an existing NcsPathway object, else False if its creating a new object.

layer property

layer

Returns the raster layer specified by the user, either existing layers in the map canvas or from the selected file.

Returns:

Type Description
QgsRasterLayer

The raster layer specified by the user or None if not set.

ncs_pathway property

ncs_pathway

Returns a reference to the NcsPathway object.

Returns:

Type Description
NcsPathway

Reference to the NcsPathway object.

description_changed

description_changed()

Slot to handle description text changes, it currently limits the number of characters to only be 300 characters per description

Source code in src/cplus_plugin/gui/ncs_pathway_editor_dialog.py
def description_changed(self):
    """Slot to handle description text changes, it currently
    limits the number of characters to only be 300 characters
    per description
    """

    description = self.txt_description.toPlainText()
    if len(description) > 300:
        self.txt_description.setPlainText(description[:300])

open_help

open_help(activated)

Opens the user documentation for the plugin in a browser.

Source code in src/cplus_plugin/gui/ncs_pathway_editor_dialog.py
def open_help(self, activated: bool):
    """Opens the user documentation for the plugin in a browser."""
    open_documentation(USER_DOCUMENTATION_SITE)

selected_carbon_items

selected_carbon_items()

Returns the selected carbon items in the list view.

Returns:

Type Description
list

A collection of the selected carbon items.

Source code in src/cplus_plugin/gui/ncs_pathway_editor_dialog.py
def selected_carbon_items(self) -> typing.List[CarbonLayerItem]:
    """Returns the selected carbon items in the list view.

    :returns: A collection of the selected carbon items.
    :rtype: list
    """
    selection_model = self.lst_carbon_layers.selectionModel()
    idxs = selection_model.selectedRows()

    return [self._carbon_model.item(idx.row()) for idx in idxs]

validate

validate()

Validates if name and layer have been specified.

Returns:

Type Description
True

True if user input (i.e. name and layer) have been set.

Source code in src/cplus_plugin/gui/ncs_pathway_editor_dialog.py
def validate(self) -> bool:
    """Validates if name and layer have been specified.

    :returns: True if user input (i.e. name and layer) have been set.
    :rtype: True
    """
    status = True

    self._message_bar.clearWidgets()

    name = self.txt_name.text()
    if not name:
        msg = tr("NCS pathway name cannot be empty.")
        self._show_warning_message(msg)
        status = False

    if name.lower() in self._excluded_names:
        msg = tr("name has already been used.")
        self._show_warning_message(f"'{name}' {msg}")
        status = False

    if not self.txt_description.toPlainText():
        msg = tr("Description cannot be empty.")
        self._show_warning_message(msg)
        status = False

    layer = self._get_selected_map_layer()
    default_layer = self._get_selected_default_layer()
    if layer is None and default_layer is None:
        msg = tr("Map layer not specified.")
        self._show_warning_message(msg)
        status = False

    if layer and not layer.isValid():
        msg = tr("Map layer is not valid.")
        self._show_warning_message(msg)
        status = False

    return status

Last update: November 25, 2024
Back to top