Skip to content

Carbon Layer Model

MVC model for carbon layer paths.

CarbonLayerItem

CarbonLayerItem(layer_path)

Bases: QStandardItem

Represents a single carbon layer path.

Source code in src/cplus_plugin/gui/carbon_item_model.py
def __init__(self, layer_path: str):
    super().__init__()

    self._layer_path = layer_path
    self._is_valid = True
    self.update(self._layer_path)

is_valid property

is_valid

Returns the validity status of the carbon layer path.

The path could be invalid if it does not exist or if the corresponding map layer is invalid.

Returns:

Type Description
bool

True if valid, else False.

layer_path property

layer_path

Returns the path to the carbon layer.

Returns:

Type Description
str

Path to the carbon layer.

type

type()

Returns the type of the standard item.

Returns:

Type Description
int

Type identifier of the carbob item.

Source code in src/cplus_plugin/gui/carbon_item_model.py
def type(self) -> int:
    """Returns the type of the standard item.

    :returns: Type identifier of the carbob item.
    :rtype: int
    """
    return QtGui.QStandardItem.UserType + 5

update

update(layer_path)

Update the UI properties.

Source code in src/cplus_plugin/gui/carbon_item_model.py
def update(self, layer_path: str):
    """Update the UI properties."""
    if self._layer_path.startswith("cplus://"):
        paths = self._layer_path.split("/")
        self.setText(f"Online Default: {paths[-1]}")
        self.setToolTip(f"Online Default: {paths[-1]}")
        self._is_valid = True
        self.setIcon(QtGui.QIcon())
        return
    self._layer_path = str(os.path.normpath(layer_path))
    p = Path(self._layer_path)
    self.setText(p.name)
    self.setToolTip(self._layer_path)

    # Check validity
    if p.exists():
        layer = QgsRasterLayer(layer_path)
        if layer.isValid():
            self._is_valid = True
            self.setIcon(QtGui.QIcon())
        else:
            self._is_valid = False
            error_icon = FileUtils.get_icon("mIndicatorLayerError.svg")
            self.setIcon(error_icon)
            self.setToolTip(tr("Carbon layer is not invalid."))
    else:
        self._is_valid = False
        error_icon = FileUtils.get_icon("mIndicatorLayerError.svg")
        self.setIcon(error_icon)
        self.setToolTip(tr("File path is invalid."))

CarbonLayerModel

CarbonLayerModel(parent=None, carbon_paths=None)

Bases: QStandardItemModel

View model for carbon layers.

Source code in src/cplus_plugin/gui/carbon_item_model.py
def __init__(
    self, parent=None, carbon_paths: typing.Union[typing.List[str], None] = None
):
    super().__init__(parent)
    self.setColumnCount(1)

    if carbon_paths is not None:
        for cp in carbon_paths:
            self.add_carbon_layer(cp)

add_carbon_layer

add_carbon_layer(layer_path)

Adds a carbon layer to the model.

Parameters:

Name Type Description Default
layer_path str

Carbon layer path.

required

Returns:

Type Description
bool

True if the carbon layer was successfully added, else False if there is an existing item with the same path.

Source code in src/cplus_plugin/gui/carbon_item_model.py
def add_carbon_layer(self, layer_path: str) -> bool:
    """Adds a carbon layer to the model.

    :param layer_path: Carbon layer path.
    :type layer_path: str

    :returns: True if the carbon layer was successfully added,
    else False if there is an existing item with the same path.
    """
    if self.contains_layer_path(layer_path):
        return False

    carbon_item = CarbonLayerItem(layer_path)
    self.appendRow(carbon_item)

    return True

carbon_layer_index

carbon_layer_index(layer_path)

Get the model index for the given layer path.

Parameters:

Name Type Description Default
layer_path str

Carbon layer path.

required

Returns:

Type Description
QtCore.QModelIndex

The index corresponding to the given layer path else an invalid index if not found.

Source code in src/cplus_plugin/gui/carbon_item_model.py
def carbon_layer_index(self, layer_path: str) -> QtCore.QModelIndex:
    """Get the model index for the given layer path.

    :param layer_path: Carbon layer path.
    :type layer_path: str

    :returns: The index corresponding to the given layer path else
    an invalid index if not found.
    :rtype: QtCore.QModelIndex
    """
    if not layer_path.startswith("cplus://"):
        norm_path = str(os.path.normpath(layer_path))
    else:
        norm_path = layer_path
    matching_index = None
    for r in range(self.rowCount()):
        index = self.index(r, 0)
        if not index.isValid():
            continue
        item = self.itemFromIndex(index)
        if item.layer_path == norm_path:
            matching_index = index
            break

    if matching_index is None:
        return QtCore.QModelIndex()

    return matching_index

carbon_paths

carbon_paths(valid_only=False)

Gets all the carbon paths in the model.

Parameters:

Name Type Description Default
valid_only bool

Only return the carbon paths that are valid.

False

Returns:

Type Description
list

A collection of carbon paths in the model.

Source code in src/cplus_plugin/gui/carbon_item_model.py
def carbon_paths(self, valid_only: bool = False) -> list:
    """Gets all the carbon paths in the model.

    :param valid_only: Only return the carbon paths that are valid.
    :type valid_only: bool

    :returns: A collection of carbon paths in the model.
    :rtype: list
    """
    carbon_paths = []
    for r in range(self.rowCount()):
        index = self.index(r, 0)
        item = self.itemFromIndex(index)
        if valid_only:
            if item.is_valid:
                carbon_paths.append(item.layer_path)
        else:
            carbon_paths.append(item.layer_path)

    return carbon_paths

contains_layer_path

contains_layer_path(layer_path)

Checks if the specified layer path exists in the model.

Parameters:

Name Type Description Default
layer_path str

Carbon layer path.

required

Returns:

Type Description
bool

True if the path exists, else False.

Source code in src/cplus_plugin/gui/carbon_item_model.py
def contains_layer_path(self, layer_path: str) -> bool:
    """Checks if the specified layer path exists in the model.

    :param layer_path: Carbon layer path.
    :type layer_path: str

    :returns: True if the path exists, else False.
    :rtype: bool
    """
    carbon_idx = self.carbon_layer_index(layer_path)
    if carbon_idx.isValid():
        return True

    return False

update_carbon_path

update_carbon_path(index, layer_path)

Update the carbon path at the given position specified by the index.

Parameters:

Name Type Description Default
index QModelIndex

Location to modify the carbon path.

required
layer_path str

Carbon layer path.

required

Returns:

Type Description
bool

True if the path was successfully updated else False if there is no carbon item at the given location.

Source code in src/cplus_plugin/gui/carbon_item_model.py
def update_carbon_path(self, index: QtCore.QModelIndex, layer_path: str) -> bool:
    """Update the carbon path at the given position specified by the index.

    :param index: Location to modify the carbon path.
    :type index: QtCore.QModelIndex

    :param layer_path: Carbon layer path.
    :type layer_path: str

    :returns: True if the path was successfully updated else False
    if there is no carbon item at the given location.
    :rtype: bool
    """
    if not index.isValid():
        return False

    item = self.itemFromIndex(index)
    if item is None:
        return False

    item.update(layer_path)

    return True

Last update: October 2, 2024
Back to top