Exemplo n.º 1
0
def _create_resource_type_combo(
        current_resource_type: ResourceType, parent: QWidget,
        resource_database: ResourceDatabase) -> QComboBox:
    """

    :param current_resource_type:
    :param parent:
    :return:
    """
    resource_type_combo = ScrollProtectedComboBox(parent)

    for resource_type in iterate_enum(ResourceType):
        try:
            count_elements = len(resource_database.get_by_type(resource_type))
        except ValueError:
            count_elements = 0

        if count_elements == 0:
            continue

        resource_type_combo.addItem(resource_type.name, resource_type)
        if resource_type is current_resource_type:
            resource_type_combo.setCurrentIndex(resource_type_combo.count() -
                                                1)

    return resource_type_combo
Exemplo n.º 2
0
def _create_resource_name_combo(
    resource_database: ResourceDatabase,
    resource_type: ResourceType,
    current_resource: Optional[ResourceInfo],
    parent: QWidget,
) -> QComboBox:
    """

    :param resource_database:
    :param current_resource:
    :param parent:
    :return:
    """

    resource_name_combo = ScrollProtectedComboBox(parent)

    for resource in sorted(resource_database.get_by_type(resource_type),
                           key=lambda x: x.long_name):
        resource_name_combo.addItem(resource.long_name, resource)
        if resource is current_resource:
            resource_name_combo.setCurrentIndex(resource_name_combo.count() -
                                                1)

    return resource_name_combo
Exemplo n.º 3
0
class ResourceRequirementEditor:
    def __init__(
        self,
        parent: QWidget,
        layout: QHBoxLayout,
        resource_database: ResourceDatabase,
        item: ResourceRequirement,
    ):
        self.parent = parent
        self.layout = layout
        self.resource_database = resource_database

        self.resource_type_combo = _create_resource_type_combo(
            item.resource.resource_type, parent, resource_database)
        self.resource_type_combo.setMinimumWidth(75)
        self.resource_type_combo.setMaximumWidth(75)

        self.resource_name_combo = _create_resource_name_combo(
            self.resource_database, item.resource.resource_type, item.resource,
            self.parent)

        self.negate_combo = ScrollProtectedComboBox(parent)
        self.negate_combo.addItem("≥", False)
        self.negate_combo.addItem("<", True)
        self.negate_combo.setCurrentIndex(int(item.negate))
        self.negate_combo.setMinimumWidth(40)
        self.negate_combo.setMaximumWidth(40)

        self.negate_check = QtWidgets.QCheckBox(parent)
        self.negate_check.setChecked(item.negate)

        self.amount_edit = QLineEdit(parent)
        self.amount_edit.setValidator(QIntValidator(1, 10000))
        self.amount_edit.setText(str(item.amount))
        self.amount_edit.setMinimumWidth(45)
        self.amount_edit.setMaximumWidth(45)

        self.amount_combo = ScrollProtectedComboBox(parent)
        for trick_level in iterate_enum(LayoutTrickLevel):
            self.amount_combo.addItem(trick_level.long_name,
                                      userData=trick_level.as_number)
        self.amount_combo.setCurrentIndex(
            self.amount_combo.findData(item.amount))

        for widget in self._all_widgets:
            self.layout.addWidget(widget)

        self.resource_type_combo.currentIndexChanged.connect(self._update_type)
        self._update_visible_elements_by_type()

    @property
    def resource_type(self) -> ResourceType:
        return self.resource_type_combo.currentData()

    def _update_visible_elements_by_type(self):
        resource_type = self.resource_type

        if resource_type == ResourceType.DAMAGE:
            self.negate_combo.setCurrentIndex(0)

        self.negate_check.setText("Before" if resource_type ==
                                  ResourceType.EVENT else "Not")
        self.negate_check.setVisible(
            resource_type in
            {ResourceType.EVENT, ResourceType.VERSION, ResourceType.MISC})
        self.negate_combo.setVisible(
            resource_type in {ResourceType.ITEM, ResourceType.DAMAGE})
        self.negate_combo.setEnabled(resource_type == ResourceType.ITEM)
        self.amount_edit.setVisible(
            resource_type in {ResourceType.ITEM, ResourceType.DAMAGE})
        self.amount_combo.setVisible(resource_type == ResourceType.TRICK)

    def _update_type(self):
        old_combo = self.resource_name_combo

        self.resource_name_combo = _create_resource_name_combo(
            self.resource_database, self.resource_type_combo.currentData(),
            None, self.parent)

        self.layout.replaceWidget(old_combo, self.resource_name_combo)
        old_combo.deleteLater()
        self._update_visible_elements_by_type()

    def deleteLater(self):
        for widget in self._all_widgets:
            widget.deleteLater()

    @property
    def _all_widgets(self) -> typing.Iterable[QWidget]:
        yield self.resource_type_combo
        yield self.negate_check
        yield self.resource_name_combo
        yield self.negate_combo
        yield self.amount_edit
        yield self.amount_combo

    @property
    def current_requirement(self) -> ResourceRequirement:
        resource_type = self.resource_type

        # Quantity
        if resource_type == ResourceType.TRICK:
            quantity: int = self.amount_combo.currentData()
        elif resource_type == ResourceType.EVENT:
            quantity = 1
        else:
            quantity = int(self.amount_edit.text())

        # Negate flag
        if resource_type == ResourceType.ITEM:
            negate: bool = self.negate_combo.currentData()
        elif resource_type == ResourceType.EVENT:
            negate = self.negate_check.isChecked()
        else:
            negate = False

        return ResourceRequirement(self.resource_name_combo.currentData(),
                                   quantity, negate)