Skip to content

Activity editor

Dialog for creating or editing an activity.

ActivityEditorDialog

ActivityEditorDialog(parent=None, activity=None, excluded_names=None)

Bases: QDialog, WidgetUi

Dialog for creating or editing an activity.

Source code in src/cplus_plugin/gui/activity_editor_dialog.py
def __init__(self, parent=None, activity=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.style_btn.setSymbolType(Qgis.SymbolType.Fill)

    self.btn_color_ramp.setShowNull(False)
    self.btn_color_ramp.setShowGradientOnly(True)
    self.btn_color_ramp.setColorRampDialogTitle(
        self.tr("Set Color Ramp for Output Activity")
    )
    # Default gradient colour which closely matches the color
    # for the activity in the scenario layer
    start_color = generate_random_color()
    stop_color = generate_random_color()
    self.btn_color_ramp.setColorRamp(QgsGradientColorRamp(start_color, stop_color))
    self.style_btn.setColor(start_color)

    self.buttonBox.accepted.connect(self._on_accepted)
    self.btn_select_file.clicked.connect(self._on_select_file)
    self.btn_help.clicked.connect(self.open_help)

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

    self.cbo_layer.setFilters(QgsMapLayerProxyModel.Filter.RasterLayer)

    self._edit_mode = False
    self._layer = None
    self._mask_layer = None

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

    self._activity = activity
    if self._activity is not None:
        self._edit_mode = True
        self._layer = self._activity.to_map_layer()
        self._update_controls()

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

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

    # Hide map layer handling
    self.layer_gb.setVisible(False)

    # Mask layers
    add_icon = FileUtils.get_icon("symbologyAdd.svg")
    self.btn_add_mask.setIcon(add_icon)
    self.btn_add_mask.clicked.connect(self._on_add_mask_layer)

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

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

    if self._activity is not None:
        mask_paths_list = self._activity.mask_paths

        for mask_path in mask_paths_list or []:
            if mask_path == "":
                continue
            item = QtWidgets.QListWidgetItem()
            item.setData(QtCore.Qt.DisplayRole, mask_path)
            self.lst_mask_layers.addItem(item)
        self.mask_layers_changed()

activity property

activity

Returns a reference to the activity object.

Returns:

Type Description
Activity

Reference to the activity object.

edit_mode property

edit_mode

Returns the state of the editor.

Returns:

Type Description
bool

True if the editor is editing an existing activity 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.

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/activity_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/activity_editor_dialog.py
def open_help(self, activated: bool):
    """Opens the user documentation for the plugin in a browser."""
    open_documentation(USER_DOCUMENTATION_SITE)

output_layer_color_ramp

output_layer_color_ramp()

Returns the selected color ramp.

Returns:

Type Description
QgsColorRamp

The color ramp selected by the user.

Source code in src/cplus_plugin/gui/activity_editor_dialog.py
def output_layer_color_ramp(self) -> QgsColorRamp:
    """Returns the selected color ramp.

    :returns: The color ramp selected by the user.
    :rtype: QgsColorRamp
    """
    color_ramp = self.btn_color_ramp.colorRamp()

    return color_ramp

scenario_fill_symbol_layer

scenario_fill_symbol_layer()

Gets the first fill symbol layer in the symbol as set in the button.

It checks to ensure that there is at least one fill symbol layer contained in the symbol.

Returns:

Type Description
QgsFillSymbolLayer

Fill symbol layer to be used in the activity.

Source code in src/cplus_plugin/gui/activity_editor_dialog.py
def scenario_fill_symbol_layer(self) -> QgsFillSymbolLayer:
    """Gets the first fill symbol layer in the symbol as
    set in the button.

    It checks to ensure that there is at least one fill symbol
    layer contained in the symbol.

    :returns: Fill symbol layer to be used in the activity.
    :rtype: QgsFillSymbolLayer
    """
    fill_symbol_layer = None
    btn_symbol = self.style_btn.symbol()

    for i in range(btn_symbol.symbolLayerCount()):
        symbol_layer = btn_symbol.symbolLayer(i)
        if isinstance(symbol_layer, QgsFillSymbolLayer):
            fill_symbol_layer = symbol_layer
            break

    return fill_symbol_layer

validate

validate()

Validates if name has been specified.

Returns:

Type Description
True

True if the name have been set.

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

    :returns: True if the name have been set.
    :rtype: True
    """
    status = True

    self._message_bar.clearWidgets()

    name = self.txt_name.text()
    if not name:
        msg = tr("Activity 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()
    if layer and not layer.isValid():
        msg = tr("Map layer is not valid.")
        self._show_warning_message(msg)
        status = False

    fill_symbol_layer = self.scenario_fill_symbol_layer()
    if fill_symbol_layer is None:
        msg = tr("No fill symbol defined for the scenario layer.")
        self._show_warning_message(msg)
        status = False

    if self.btn_color_ramp.colorRamp() is None or self.btn_color_ramp.isNull():
        msg = tr("No color ramp defined for the output activity layer.")
        self._show_warning_message(msg)
        status = False

    return status

Last update: November 25, 2024
Back to top