Skip to content

Layout Items

Custom CPLUS layout items.

BasicScenarioDetailsItem

BasicScenarioDetailsItem(*args, **kwargs)

Bases: QgsLayoutItemGroup

Contains elements showing the basic details of a scenario such as a title, description, map and legend.

Source code in src/cplus_plugin/lib/reports/layout_items.py
def __init__(self, *args, **kwargs):
    self._result: ScenarioResult = kwargs.pop("scenario_result", None)
    self._project = kwargs.pop("project", None)
    super().__init__(*args, **kwargs)

    self._add_scenario_layout_items()

    self._update_scenario_details()

attemptResize

attemptResize(*args, **kwargs)

Override to set the correct position of the legend item.

Source code in src/cplus_plugin/lib/reports/layout_items.py
def attemptResize(self, *args, **kwargs):
    """Override to set the correct position of the legend item."""
    super().attemptResize(*args, **kwargs)

    group_height = self.sizeWithUnits().height()
    group_width = self.sizeWithUnits().width()

    reference_point = self.pagePositionWithUnits()
    reference_point_x = reference_point.x()
    reference_point_y = reference_point.y()

    # Set position of the legend
    legend_height = self._legend.sizeWithUnits().height() / 2
    new_reference_y = reference_point_y + (group_height - legend_height)
    legend_ref_point = QgsLayoutPoint(
        reference_point_x, new_reference_y, self.layout().units()
    )
    self._legend.attemptMove(legend_ref_point, True, False, self.page())
    self._legend.attemptResize(
        QgsLayoutSize(group_width, legend_height, self.layout().units())
    )

    # Set height for the map
    map_height = group_height - (
        legend_height
        + self._title_label.sizeWithUnits().height()
        + self._description_label.sizeWithUnits().height()
    )
    self._scenario_map.attemptResize(
        QgsLayoutSize(
            self._scenario_map.sizeWithUnits().width(),
            map_height,
            self.layout().units(),
        )
    )

set_label_font classmethod

set_label_font(label, size, bold=False, italic=False, color=None)

Set font properties of the given layout label item.

Parameters:

Name Type Description Default
label QgsLayoutItemLabel

Label item whose font properties will be updated.

required
size float

Point size of the font.

required
bold bool

True if font is to be bold, else False (default).

False
italic bool

True if font is to be in italics, else False (default).

False
color QColor

Color for the text or None for the default color.

None
Source code in src/cplus_plugin/lib/reports/layout_items.py
@classmethod
def set_label_font(
    cls,
    label: QgsLayoutItemLabel,
    size: float,
    bold: bool = False,
    italic: bool = False,
    color: QtGui.QColor = None,
):
    """Set font properties of the given layout label item.

    :param label: Label item whose font properties will
    be updated.
    :type label: QgsLayoutItemLabel

    :param size: Point size of the font.
    :type size: int

    :param bold: True if font is to be bold, else
    False (default).
    :type bold: bool

    :param italic: True if font is to be in italics, else
    False (default).
    :type italic: bool

    :param color: Color for the text or None for the default color.
    :type color: QtGui.QColor
    """
    font = get_report_font(size, bold, italic)
    version = Qgis.versionInt()

    # Text format size unit
    if version < 33000:
        unit_type = QgsUnitTypes.RenderUnit.RenderPoints
    else:
        unit_type = Qgis.RenderUnit.Points

    # Label font setting option
    if version < 32400:
        label.setFont(font)
        if color is not None:
            label.setFontColor(color)
    else:
        txt_format = QgsTextFormat()
        txt_format.setFont(font)
        txt_format.setSize(size)
        txt_format.setSizeUnit(unit_type)

        if color is not None:
            txt_format.setColor(color)

        label.setTextFormat(txt_format)

    label.refresh()

CplusMapRepeatItem

CplusMapRepeatItem(*args, **kwargs)

Bases: QgsLayoutItemShape

Defines an outline area within a layout where map items containing NCS pathway or activity will be drawn.

Source code in src/cplus_plugin/lib/reports/layout_items.py
def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.setShapeType(QgsLayoutItemShape.Shape.Rectangle)

    # We shall use a frame so that it can be turned off / on
    # using the item properties UI. The symbol is just a proxy.
    # Symbol properties
    symbol_props = {
        "color": "229,182,54,0",
        "style": "solid",
        "outline_style": "dash",
        "line_color": "132,192,68",
        "outline_width": "0",
        "joinstyle": "miter",
    }
    symbol = QgsFillSymbol.createSimple(symbol_props)
    self.setSymbol(symbol)

    self._model_component_type = kwargs.pop(
        "model_component_type", ModelComponentType.UNKNOWN
    )

model_component_type property writable

model_component_type

Gets the model component type associated with this map item i.e. NCS pathway or activity.

Returns:

Type Description
Enum

Type of the model component.

icon

icon()

Override for custom CPLUS map item.

Source code in src/cplus_plugin/lib/reports/layout_items.py
def icon(self) -> QtGui.QIcon:
    """Override for custom CPLUS map item."""
    return FileUtils.get_icon("mLayoutItemMap_cplus.svg")

readPropertiesFromElement

readPropertiesFromElement(element, document, context)

Override reading of item properties.

Source code in src/cplus_plugin/lib/reports/layout_items.py
def readPropertiesFromElement(self, element, document, context):
    """Override reading of item properties."""
    status = super().readPropertiesFromElement(element, document, context)
    if status:
        model_component_type = element.attribute("modelComponentType", "")
        self._model_component_type = ModelComponentType.from_string(
            model_component_type
        )

    return status

type

type()

Return item's unique type identifier.

Source code in src/cplus_plugin/lib/reports/layout_items.py
def type(self):
    """Return item's unique type identifier."""
    return CPLUS_MAP_REPEAT_ITEM_TYPE

visibleName

visibleName()

Override for visible name of the item.

Source code in src/cplus_plugin/lib/reports/layout_items.py
def visibleName(self) -> str:
    """Override for visible name of the item."""
    return tr("CPLUS Map Repeat Area Item")

visiblePluralName

visiblePluralName()

Override for plural name of the items.

Source code in src/cplus_plugin/lib/reports/layout_items.py
def visiblePluralName(self) -> str:
    """Override for plural name of the items."""
    return tr("CPLUS Map Repeat Area Items")

writePropertiesToElement

writePropertiesToElement(el, document, context)

Override saving of item properties.

Source code in src/cplus_plugin/lib/reports/layout_items.py
def writePropertiesToElement(self, el, document, context):
    """Override saving of item properties."""
    status = super().writePropertiesToElement(el, document, context)
    if status:
        el.setAttribute("modelComponentType", self._model_component_type.value)

    return status

CplusMapRepeatItemLayoutItemMetadata

CplusMapRepeatItemLayoutItemMetadata()

Bases: QgsLayoutItemAbstractMetadata

Metadata info of the cplus map repeat item.

Source code in src/cplus_plugin/lib/reports/layout_items.py
def __init__(self):
    super().__init__(CPLUS_MAP_REPEAT_ITEM_TYPE, tr("CPLUS Map Repeat Area Item"))

createItem

createItem(layout)

Factory method that return the cplus map item.

Source code in src/cplus_plugin/lib/reports/layout_items.py
def createItem(self, layout) -> CplusMapRepeatItem:
    """Factory method that return the cplus map item."""
    return CplusMapRepeatItem(layout)

Last update: October 2, 2024
Back to top