Пример #1
0
def list_selection_roots(cls):
    d = {}
    for field in fields(cls):
        if field.delegate is not None:
            if field.delegate.list_selection_root:
                d[field.name] = field.delegate.list_selection_root
    return d
Пример #2
0
                    def slot(
                        datum,
                        item=item,
                        field_name=field_name,
                        editable=editable,
                    ):
                        node = item.data(epyqlib.utils.qt.UserRoles.node)
                        model = node.find_root().model
                        field_metadata = getattr(
                            fields(node),
                            field_name,
                        )
                        data_display = field_metadata.data_display

                        field_metadata.updating = True
                        try:
                            display_datum = datum
                            if data_display is not None:
                                display_datum = data_display(
                                    node,
                                    value=display_datum,
                                    model=model,
                                )
                            elif field_metadata.converter == two_state_checkbox:
                                display_datum = ""

                            if display_datum is None:
                                # TODO: CAMPid 0794305784527546542452654254679680
                                # The display role is supposed to be '-' for None
                                # but they can't be different
                                #
                                # http://doc.qt.io/qt-5/qstandarditem.html#data
                                #   The default implementation treats Qt::EditRole
                                #   and Qt::DisplayRole as referring to the same
                                #   data
                                display_text = ""
                                # edit_text = ''
                                if editable:
                                    decoration = QtGui.QColor("green")
                                    decoration.setAlphaF(0.4)
                                else:
                                    decoration = None
                            else:
                                display_text = str(display_datum)
                                # edit_text = display_text
                                decoration = None

                            item.setData(display_text, PyQt5.QtCore.Qt.DisplayRole)
                            # item.setData(edit_text, PyQt5.QtCore.Qt.EditRole)
                            item.setData(decoration, PyQt5.QtCore.Qt.DecorationRole)
                            item.setData(datum, epyqlib.utils.qt.UserRoles.raw)
                            if item.isCheckable():
                                item.setCheckState(
                                    PyQt5.QtCore.Qt.Checked
                                    if datum
                                    else PyQt5.QtCore.Qt.Unchecked,
                                )
                        finally:
                            field_metadata.updating = False
Пример #3
0
    def _name(column):
        cls, field_name = column

        field = getattr(fields(cls), field_name)
        name = field.human_name

        if name is None:
            name = field_name.replace("_", " ").title()

        return name
Пример #4
0
    def select(self, index):
        index = to_source_model(index)
        model = index.model()
        item = model.itemFromIndex(index)
        node = item.data(epyqlib.utils.qt.UserRoles.node)
        field_name = item.data(epyqlib.utils.qt.UserRoles.field_name)
        model = item.data(epyqlib.utils.qt.UserRoles.attrs_model)

        metadata = getattr(fields(type(node)), field_name)
        
        if metadata.delegate is not None:
            delegate = metadata.delegate.get_delegate(model, self.parent)
        else:
            delegate = self.regular

        return delegate
Пример #5
0
    def _pyqtify_connect(self, parent, child):
        connections = {}
        connection_id = get_connection_id(parent=parent, child=child)
        if connection_id in self.connected_signals:
            raise ConsistencyError(
                "already connected: {}".format((parent.uuid, child.uuid))
            )
        self.connected_signals[connection_id] = connections

        if child is self.root:
            root_item = self.model.invisibleRootItem()
            root_item.setData(child, epyqlib.utils.qt.UserRoles.node)
            root_item.setText("root")
        else:
            if parent is self.root:
                parent_item = self.model.invisibleRootItem()
            else:
                parent_item = self.item_from_node(parent)
            row = parent.row_of_child(child)

            items = []

            changed_signals = epyqlib.utils.qt.pyqtified(child).changed

            uneditable_highlight = QtGui.QColor("grey")
            uneditable_highlight.setAlphaF(0.4)
            droppable_highlight = QtGui.QColor("orange")
            droppable_highlight.setAlphaF(0.4)
            droppable_row = hasattr(row, "addable_types") or hasattr(
                row, "all_addable_types"
            )

            for i, column in enumerate(self.columns):
                field_name = column.fields.get(type(child))

                item = QtGui.QStandardItem()
                editable = False
                has_field = False
                field_for_column = column.fields.get(type(child))
                if field_for_column is not None:
                    metadata = getattr(fields(type(child)), field_for_column)
                    editable = metadata.editable
                    has_field = True

                item.setEditable(editable)
                if not editable and has_field:
                    if droppable_row:
                        item.setData(
                            uneditable_highlight,
                            QtCore.Qt.ItemDataRole.BackgroundRole,
                        )
                    else:
                        item.setData(
                            droppable_highlight,
                            QtCore.Qt.ItemDataRole.BackgroundRole,
                        )

                if i == 0:
                    self._all_items_dict[
                        (
                            item.data(epyqlib.utils.qt.UserRoles.node),
                            item.column(),
                        )
                    ] = item
                    self.node_to_item[child] = item
                item.setData(child, epyqlib.utils.qt.UserRoles.node)
                item.setData(i, epyqlib.utils.qt.UserRoles.column_index)
                item.setData(
                    field_name,
                    epyqlib.utils.qt.UserRoles.field_name,
                )
                item.setData(
                    self,
                    epyqlib.utils.qt.UserRoles.attrs_model,
                )

                if field_name is not None:
                    checkable = (
                        getattr(fields(type(child)), field_name).converter
                        == two_state_checkbox
                    )
                    item.setCheckable(checkable)

                    def slot(
                        datum,
                        item=item,
                        field_name=field_name,
                        editable=editable,
                    ):
                        node = item.data(epyqlib.utils.qt.UserRoles.node)
                        model = node.find_root().model
                        field_metadata = getattr(
                            fields(node),
                            field_name,
                        )
                        data_display = field_metadata.data_display

                        field_metadata.updating = True
                        try:
                            display_datum = datum
                            if data_display is not None:
                                display_datum = data_display(
                                    node,
                                    value=display_datum,
                                    model=model,
                                )
                            elif field_metadata.converter == two_state_checkbox:
                                display_datum = ""

                            if display_datum is None:
                                # TODO: CAMPid 0794305784527546542452654254679680
                                # The display role is supposed to be '-' for None
                                # but they can't be different
                                #
                                # http://doc.qt.io/qt-5/qstandarditem.html#data
                                #   The default implementation treats Qt::EditRole
                                #   and Qt::DisplayRole as referring to the same
                                #   data
                                display_text = ""
                                # edit_text = ''
                                if editable:
                                    decoration = QtGui.QColor("green")
                                    decoration.setAlphaF(0.4)
                                else:
                                    decoration = None
                            else:
                                display_text = str(display_datum)
                                # edit_text = display_text
                                decoration = None

                            item.setData(display_text, PyQt5.QtCore.Qt.DisplayRole)
                            # item.setData(edit_text, PyQt5.QtCore.Qt.EditRole)
                            item.setData(decoration, PyQt5.QtCore.Qt.DecorationRole)
                            item.setData(datum, epyqlib.utils.qt.UserRoles.raw)
                            if item.isCheckable():
                                item.setCheckState(
                                    PyQt5.QtCore.Qt.Checked
                                    if datum
                                    else PyQt5.QtCore.Qt.Unchecked,
                                )
                        finally:
                            field_metadata.updating = False

                    connections[
                        getattr(changed_signals, "_pyqtify_signal_" + field_name)
                    ] = slot

                    slot(getattr(child, field_name))

                items.append(item)

            parent_item.insertRow(row, items)

        connections[child.pyqt_signals.child_added] = self.child_added
        connections[child.pyqt_signals.child_removed] = self.deleted

        for signal, slot in connections.items():
            signal.connect(slot)