Skip to content

Items selection dialog

Item selection dialog file

ItemsSelectionDialog

ItemsSelectionDialog(parent, parent_item=None, items=[], item_type=Activity)

Bases: QDialog, DialogUi

Dialog for handling items selection

Constructor

Source code in src/cplus_plugin/gui/items_selection_dialog.py
def __init__(self, parent, parent_item=None, items=[], item_type=Activity):
    """Constructor"""
    super().__init__()
    self.setupUi(self)
    self.parent = parent
    self.parent_item = parent_item

    self.item_type = item_type
    self.items = items

    select_all_btn = QtWidgets.QPushButton(tr("Select All"))
    select_all_btn.setToolTip(tr("Select the all listed items"))
    select_all_btn.clicked.connect(self.select_all_clicked)
    self.mButtonBox.addButton(select_all_btn, QtWidgets.QDialogButtonBox.ActionRole)

    clear_all_btn = QtWidgets.QPushButton(tr("Clear Selection"))
    clear_all_btn.setToolTip(tr("Clear the current selection"))
    clear_all_btn.clicked.connect(self.clear_all_clicked)
    self.mButtonBox.addButton(clear_all_btn, QtWidgets.QDialogButtonBox.ActionRole)

    toggle_selection_btn = QtWidgets.QPushButton(tr("Toggle Selection"))
    toggle_selection_btn.clicked.connect(self.toggle_selection_clicked)
    self.mButtonBox.addButton(
        toggle_selection_btn, QtWidgets.QDialogButtonBox.ActionRole
    )

    self.mButtonBox.accepted.connect(self.accept)

    self.set_items()

    for index in range(self.list_widget.count()):
        item = self.list_widget.item(index)
        item_uuid = item.data(QtCore.Qt.UserRole)

        if self.item_type is Activity:
            activity = settings_manager.get_activity(str(item_uuid))

            layer_model_uuids = [item.uuid for item in self.items]
            activity_layer_uuids = [
                layer.get("uuid")
                for layer in activity.priority_layers
                if layer is not None
            ]

            if (
                self.parent_item is not None
                and str(self.parent_item.get("uuid")) in activity_layer_uuids
            ) or (activity.uuid in layer_model_uuids):
                item.setCheckState(QtCore.Qt.Checked)
        else:
            layer = settings_manager.get_priority_layer(str(item_uuid))
            group_uuids = []

            for group in layer.get("groups"):
                group = settings_manager.find_group_by_name(group.get("name"))

                if group is not None:
                    group_uuids.append(str(group.get("uuid")))

            if self.parent_item.get("uuid") in group_uuids:
                item.setCheckState(QtCore.Qt.Checked)

accept

accept()

Saves the item selection

Source code in src/cplus_plugin/gui/items_selection_dialog.py
def accept(self):
    """Saves the item selection"""
    self.parent.set_selected_items(self.selected_items(), self.unselected_items())
    super().accept()

clear_all_clicked

clear_all_clicked()

Slot for handling clear selection for all items.

Source code in src/cplus_plugin/gui/items_selection_dialog.py
def clear_all_clicked(self):
    """Slot for handling clear selection for all items."""
    for item_index in range(self.list_widget.count()):
        item_item = self.list_widget.item(item_index)
        item_item.setCheckState(QtCore.Qt.Unchecked)

select_all_clicked

select_all_clicked()

Slot for handling selection for all items.

Source code in src/cplus_plugin/gui/items_selection_dialog.py
def select_all_clicked(self):
    """Slot for handling selection for all items."""
    for item_index in range(self.list_widget.count()):
        item_item = self.list_widget.item(item_index)
        item_item.setCheckState(QtCore.Qt.Checked)

selected_items

selected_items()

Returns the selected items from the dialog

Source code in src/cplus_plugin/gui/items_selection_dialog.py
def selected_items(self):
    """Returns the selected items from the dialog"""
    if self.item_type is Activity:
        items = settings_manager.get_all_activities()
    else:
        all_layers = settings_manager.get_priority_layers()
        items = []
        for layer in all_layers:
            model_layer = PriorityLayer(
                uuid=uuid.UUID(layer.get("uuid")),
                name=layer.get("name"),
                description=layer.get("description"),
                groups=layer.get("groups"),
            )
            items.append(model_layer)

    items_text = []
    for index in range(self.list_widget.count()):
        item = self.list_widget.item(index)
        if item.checkState() == QtCore.Qt.Checked:
            items_text.append(item.text())

    final_items = []

    for item in items:
        for item_name in items_text:
            if item.name == item_name:
                final_items.append(item)
                break

    return final_items

set_items

set_items()

Sets the item list in the dialog

Source code in src/cplus_plugin/gui/items_selection_dialog.py
def set_items(self):
    """Sets the item list in the dialog"""
    if self.item_type is Activity:
        items = settings_manager.get_all_activities()
    else:
        all_layers = settings_manager.get_priority_layers()
        items = []
        for layer in all_layers:
            model_layer = PriorityLayer(
                uuid=uuid.UUID(layer.get("uuid")),
                name=layer.get("name"),
                description=layer.get("description"),
                groups=layer.get("groups"),
            )
            items.append(model_layer)

    for item in items:
        list_widget_item = QtWidgets.QListWidgetItem(item.name)
        list_widget_item.setFlags(
            list_widget_item.flags() | QtCore.Qt.ItemIsUserCheckable
        )
        list_widget_item.setData(QtCore.Qt.UserRole, item.uuid)
        list_widget_item.setCheckState(QtCore.Qt.Unchecked)
        self.list_widget.addItem(list_widget_item)

toggle_selection_clicked

toggle_selection_clicked()

Toggles all the current items selection.

Source code in src/cplus_plugin/gui/items_selection_dialog.py
def toggle_selection_clicked(self):
    """Toggles all the current items selection."""
    for item_index in range(self.list_widget.count()):
        item_item = self.list_widget.item(item_index)
        state = item_item.checkState()
        if state == QtCore.Qt.Checked:
            item_item.setCheckState(QtCore.Qt.Unchecked)
        elif state == QtCore.Qt.Unchecked:
            item_item.setCheckState(QtCore.Qt.Checked)

unselected_items

unselected_items()

Returns unselected items from the dialog

Source code in src/cplus_plugin/gui/items_selection_dialog.py
def unselected_items(self):
    """Returns unselected items from the dialog"""
    if self.item_type is Activity:
        items = settings_manager.get_all_activities()
    else:
        all_layers = settings_manager.get_priority_layers()
        items = []
        for layer in all_layers:
            model_layer = PriorityLayer(
                uuid=uuid.UUID(layer.get("uuid")),
                name=layer.get("name"),
                description=layer.get("description"),
                groups=layer.get("groups"),
            )
            items.append(model_layer)

    items_text = []
    for index in range(self.list_widget.count()):
        item = self.list_widget.item(index)
        if item.checkState() == QtCore.Qt.Unchecked:
            items_text.append(item.text())
    item_names = ",".join(items_text)
    items = [item for item in items if item.name in item_names]
    return items

Last update: November 25, 2024
Back to top