Exemplo n.º 1
0
class PersonData(Data):
    name = data_attribute()
    hobby = data_attribute(HobbyData)
    special_hobby = data_attribute(
        HobbyData,
        subtypes=True,
        serializer=lambda _, **k: k["super"]()["__state__"],
        deserializer=_deserializer,
    )
    generic_hobby = data_attribute(
        HobbyData,
        subtypes=True,
    )
Exemplo n.º 2
0
class OQHistoryWidgetDefaultHeader(ListModelHeader):
    """
    Default header for :class:`objettoqt.widgets.OQHistoryWidget`.

    Inherits from:
      - :class:`objettoqt.models.ListModelHeader`
    """

    title = data_attribute(string_types, subtypes=True, default="name")
    """
    Title.

    :type: str
    """

    fallback = data_attribute(string_types, subtypes=True, default="---")
    """
    Fallback value.

    :type: str
    """
    def data(self, obj, row, role=QtCore.Qt.DisplayRole):
        """
        Dim/brighten text depending on the history's index.

        :param obj: History changes list.
        :type obj: objetto.objects.ListObject

        :param row: Row.
        :type row: int

        :param role: Role.
        :type role: QtCore.Qt.ItemDataRole

        :return: Data.
        :rtype: str or objetto.bases.BaseObject
        """

        # Dim/brighten text.
        history = obj._parent
        if isinstance(history, HistoryObject):
            if role == QtCore.Qt.ForegroundRole:
                if row > history.index:  # TODO: use system colors
                    return QtGui.QBrush(QtGui.QColor(128, 128, 138, 100))
                elif history.index == row:
                    return QtGui.QBrush(QtGui.QColor(255, 255, 255, 255))

        return super(OQHistoryWidgetDefaultHeader, self).data(obj,
                                                              row,
                                                              role=role)
Exemplo n.º 3
0
class HobbyData(Data):
    description = data_attribute()
Exemplo n.º 4
0
class ListModelHeader(AbstractListModelHeader):
    """
    To be used with :class:`objettoqt.models.OQListModel`.
    Carries information on how the value should be retrieved for a column.

    For the :attr:`QtCore.Qt.DisplayRole`, this implementation will use the
    :attr:`objettoqt.models.AbstractListModelHeader.title` as the attribute name that
    will be queried from the source object at the row.

    The fallback value will be returned if a title is provided and the object at
    the row does not have an attribute with the same name as the title.

    In the case an empty title is provided (default behavior), a string representation
    of the object at the row will be returned.

    The source object at the row can be accesed through the :attr:`QtCore.Qt.UserRole`.

    Inherits from:
      - :class:`objettoqt.models.AbstractListModelHeader`
    """

    fallback = data_attribute(string_types, subtypes=True, default="")
    """
    Fallback value.

    :type: str
    """

    default_flags = data_attribute(
        default=QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
    )
    """
    Default flags.

    :type: QtCore.Qt.ItemFlag
    """

    def flags(self, obj, row):
        """
        Retrieve flags for an item at a specific row.

        :param obj: List object.
        :type obj: objetto.objects.ListObject

        :param row: Row.
        :type row: int

        :return: Flags.
        :rtype: QtCore.Qt.ItemFlag
        """
        return self.default_flags

    def data(self, obj, row, role=QtCore.Qt.DisplayRole):
        """
        Retrieve data for an item at a specific row.

        :param obj: List object.
        :type obj: objetto.objects.ListObject

        :param row: Row.
        :type row: int

        :param role: Role.
        :type role: QtCore.Qt.ItemDataRole

        :return: Data.
        :rtype: str or objetto.bases.BaseObject
        """
        with obj.app.read_context():
            if role == QtCore.Qt.DisplayRole:
                sub_obj = obj[row]
                title = self.title
                if title:
                    return str(getattr(sub_obj, title, self.fallback))
                else:
                    return str(sub_obj)
            elif role == QtCore.Qt.UserRole:
                return obj[row]
Exemplo n.º 5
0
class AbstractListModelHeader(InteractiveData):
    """
    **(abstract class)**

    To be used with :class:`objettoqt.models.OQListModel`.
    Carries information on how the value should be retrieved for a column.

    Inherits from:
      - :class:`objetto.InteractiveData`
    """

    title = data_attribute(string_types, subtypes=True, default="")
    """
    Title.

    :type: str
    """

    metadata = data_attribute(default=None)
    """
    Metadata.

    :type: str
    """

    def flags(self, obj, row):
        """
        **virtual method**

        Retrieve flags for an item at a specific row.

        :param obj: List object.
        :type obj: objetto.objects.ListObject

        :param row: Row.
        :type row: int

        :return: Flags.
        :rtype: QtCore.Qt.ItemFlag
        """
        if False and self and obj and row:  # for PyCharm
            pass
        return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable

    @abstractmethod
    def data(self, obj, row, role=QtCore.Qt.DisplayRole):
        """
        **abstract method**

        Retrieve data for an item at a specific row.

        :param obj: List object.
        :type obj: objetto.objects.ListObject

        :param row: Row.
        :type row: int

        :param role: Role.
        :type role: QtCore.Qt.ItemDataRole

        :return: Data.
        """
        raise NotImplementedError()