Exemple #1
0
    def updateActions(self, index: QModelIndex):
        # update paste action
        self.pasteAct.setEnabled(self.isPasteOk(index))

        # update copy/cut/delete actions
        if index.isValid():
            self.copyAct.setEnabled(True)
            self.cutAct.setEnabled(True)
            self.delClearAct.setEnabled(True)
        else:
            self.copyAct.setEnabled(False)
            self.cutAct.setEnabled(False)
            self.delClearAct.setEnabled(False)

        # update add action
        obj = index.data(OBJECT_ROLE)
        objType = type(obj)
        attrName = index.data(NAME_ROLE)

        if ClassesInfo.addActText(objType):
            addActText = ClassesInfo.addActText(objType)
            enabled = True
        elif attrName in Package.addableAttrs():
            addActText = ClassesInfo.addActText(Package, attrName)
            enabled = True
        elif isIterable(obj):
            addActText = f"Add {attrName} element"
            enabled = True
        else:
            addActText = "Add"
            enabled = False
        self.addAct.setEnabled(enabled)
        self.addAct.setText(addActText)
Exemple #2
0
    def onAddAct(self, objVal=None, parent: QModelIndex = None):
        parent = parent if parent else self.currentIndex()
        name = parent.data(NAME_ROLE)
        parentObj = parent.data(OBJECT_ROLE)

        if objVal:
            kwargs = {"parent": parent, "rmDefParams": False, "objVal": objVal}
        else:
            kwargs = {"parent": parent, "rmDefParams": True}

        try:
            if not parent.isValid():
                self.newPackWithDialog()
            elif name in Package.addableAttrs():
                self.addItemWithDialog(objType=ClassesInfo.addType(
                    Package, name),
                                       **kwargs)
            elif ClassesInfo.addType(type(parentObj)):
                self.addItemWithDialog(objType=ClassesInfo.addType(
                    type(parentObj)),
                                       **kwargs)
            else:
                raise TypeError("Parent type is not extendable:",
                                type(parent.data(OBJECT_ROLE)))
        except Exception as e:
            print(e)
            QMessageBox.critical(self, "Error", str(e))
Exemple #3
0
    def isPasteOk(self, index: QModelIndex) -> bool:
        if not self.treeObjClipboard or not index.isValid():
            return False

        if super(PackTreeView, self).isPasteOk(index):
            return True

        obj2paste = self.treeObjClipboard[0]
        currObj = index.data(OBJECT_ROLE)

        if ClassesInfo.addType(type(currObj)) and isinstance(
                obj2paste, ClassesInfo.addType(type(currObj))):
            return True
        return False
Exemple #4
0
    def addItem(self,
                obj: Union[Package, 'SubmodelElement', Iterable],
                parent: QModelIndex = QModelIndex()):
        parent = parent.siblingAtColumn(0)
        parentItem = self.objByIndex(parent)
        parentObj = parentItem.data(OBJECT_ROLE)
        parentObjCls = type(parentObj)
        parentName = parent.data(NAME_ROLE)

        kwargs = {
            "obj": obj,
            "parent": parentItem,
        }
        if isinstance(obj, Package):
            kwargs["parent"] = self._rootItem
            kwargs["new"] = False
            itemTyp = PackTreeViewItem
        elif parentName in Package.addableAttrs():
            package: Package = parent.data(PACKAGE_ROLE)
            package.add(obj)
            itemTyp = PackTreeViewItem
        elif ClassesInfo.changedParentObject(parentObjCls):
            parentObj = getattr(parentObj,
                                ClassesInfo.changedParentObject(parentObjCls))
            parentObj.add(obj)
            itemTyp = PackTreeViewItem
        elif isinstance(parentObj, AbstractSet):
            parentObj.add(obj)
            itemTyp = DetailedInfoItem
        elif isinstance(parentObj, list):
            parentObj.append(obj)
            itemTyp = DetailedInfoItem
        elif isinstance(parentObj, dict):
            parentObj[obj.key] = obj.value
            itemTyp = DetailedInfoItem
        else:
            raise AttributeError(
                f"Object couldn't be added: parent obj type is not appendable: {type(parentObj)}"
            )
        self.beginInsertRows(parent, self.rowCount(parent),
                             self.rowCount(parent))
        item = itemTyp(**kwargs)
        self.endInsertRows()
        itemIndex = self.index(item.row(), 0, parent)
        self.undo.append(
            SetDataItem(index=QPersistentModelIndex(itemIndex),
                        value=NOT_GIVEN,
                        role=CLEAR_ROW_ROLE))
        self.redo.clear()
        return itemIndex
Exemple #5
0
 def populate(self):
     kwargs = {
         "parent": self,
         "new": self.new,
     }
     if ClassesInfo.hasPackViewAttrs(type(self.obj)):
         for attr in ClassesInfo.packViewAttrs(type(self.obj)):
             # set package objStore as obj, so that delete works
             itemObj = getattr(self.obj, attr)
             packItem = PackTreeViewItem(itemObj, name=attr, **kwargs)
             if isinstance(itemObj, GeneratorType):
                 packItem.obj = self.obj.objStore
     elif isIterable(self.obj):
         if isinstance(self.obj, DictSupplementaryFileContainer):
             self._populateFileContainer(self.obj, **kwargs)
         else:
             self._populateIterable(self.obj, **kwargs)
Exemple #6
0
    def clearRows(self,
                  row: int,
                  count: int,
                  parent: QModelIndex = ...,
                  defaultVal=NOT_GIVEN) -> bool:
        """Delete rows if they are children of Iterable else set to Default"""
        parentItem = self.objByIndex(parent)
        parentObj = parentItem.data(OBJECT_ROLE)

        # if parentObj is Submodel and the parentObj is not rootItem
        # set submodel_element as parentObj
        if parent.isValid():
            try:
                parentAttr = ClassesInfo.changedParentObject(type(
                    parentObj))  #FIXME delete if Namespace.discard() works
                parentObj = getattr(parentObj, parentAttr)
            except AttributeError:
                pass

        for currRow in range(row + count - 1, row - 1, -1):
            child = parentItem.children()[currRow]
            if isinstance(parentObj, (list, dict, AbstractSet)):
                if isinstance(parentObj, list):
                    oldValue = parentObj.pop(currRow)
                elif isinstance(parentObj, dict):
                    value = parentObj.pop(child.objectName)
                    key = child.objectName
                    oldValue = DictItem(key, value)
                elif isinstance(parentObj, AbstractSet):
                    parentObj.discard(child.obj)
                    oldValue = child.obj
                self.removeRow(currRow, parent)
                self.undo.append(
                    SetDataItem(index=QPersistentModelIndex(parent),
                                value=oldValue,
                                role=ADD_ITEM_ROLE))
                self.redo.clear()
            else:
                if not defaultVal == NOT_GIVEN:
                    index = self.index(currRow, 0, parent)
                    self.setData(index, defaultVal, Qt.EditRole)
                elif isinstance(child.obj, Package):
                    # close package
                    oldValue = child.obj
                    self.removeRow(currRow, parent)
                    self.undo.append(
                        SetDataItem(index=QPersistentModelIndex(parent),
                                    value=oldValue,
                                    role=ADD_ITEM_ROLE))
                    self.redo.clear()
                else:
                    raise TypeError(
                        f"Unknown parent object type: "
                        f"object could not be deleted or set to default: "
                        f"{type(parentObj)}")
        return True
Exemple #7
0
    def getTypeHint(self):
        attrTypehint = None
        attrName = self.data(NAME_ROLE)

        try:
            attrTypehint = getAttrTypeHint(type(self.parentObj),
                                           attrName,
                                           delOptional=False)
            return attrTypehint
        except KeyError:
            print("Typehint could not be gotten")

        if isIterable(self.parentObj):
            attrTypehint = ClassesInfo.addType(type(self.parentObj))
            if not attrTypehint and self.parent().data(TYPE_HINT_ROLE):
                parentTypehint = self.parent().data(TYPE_HINT_ROLE)
                try:
                    attrTypehint = getIterItemTypeHint(parentTypehint)
                except KeyError:
                    print("Typehint could not be gotten")
        return attrTypehint