def dropMimeData( self, mime_data: "QMimeData", action: "Qt.DropAction", row: int, column: int, parent: "QModelIndex", ): """ Decodes and inserts the MIME data (layers) from a drop operation onto the table. """ if action == Qt.IgnoreAction: return True if not mime_data.hasFormat(Dial.KerasLayerListMIME.value): return False # Get the row number where the layers will be inserted if row != -1: begin_row = row else: begin_row = self.rowCount() LOGGER.debug("Drop action type: %s", action) LOGGER.debug("Adding a new row at index %s...", begin_row) # Get the serilalized data from the MIME data and prepare for decoding encoded_data: QByteArray = mime_data.data( Dial.KerasLayerListMIME.value) stream = QDataStream(encoded_data, QIODevice.ReadOnly) # Unserialize binary data layers = [] while not stream.atEnd(): layer = stream.readQVariant() layers.append(layer) LOGGER.debug("Values to insert: %s", len(layers)) LOGGER.debug(layers) # When adding new layers we must ensure that the names are all uniques if action == Qt.CopyAction: self.__set_unique_layer_names(layers) # Insert the decoded layers on the model self.insertRows(begin_row, len(layers), self.createIndex(begin_row, 0, layers)) return True
def dropMimeData(self, data, action, row, column, parentIndex): """ Handles the data supplied by a drag and drop operation that ended with the given action. """ if action == Qt.IgnoreAction: return True if not data.hasFormat("application/vnd.text.list"): return False encodedData = data.data("application/vnd.text.list") stream = QDataStream(encodedData, QIODevice.ReadOnly) parent = self.nodeFromIndex(parentIndex) destRow = row if row != -1 else parent.childCount() numPlacedBeforeDestination = 0 # MAYA-66630: We want all of this to be one undo transaction while not stream.atEnd(): typeId = stream.readInt32() uuid = stream.readInt64() oldRow = stream.readInt32() child = None if typeId == TYPE_ID_LIGHT_ITEM: child = self.lights[uuid] elif typeId == TYPE_ID_GROUP_ITEM: child = self.groups[uuid] else: print("Error: Unstream of unknown node type " + str(typeId)) continue oldParent = child.parent() oldParent.removeChild(child) # We are inserting into range [row, row+numRows]. But we also remove. If we remove an item from our parent before this range, # we must reduce indices that we insert at after that if oldParent == parent and oldRow < row: numPlacedBeforeDestination += 1 parent.insertChild(child, destRow - numPlacedBeforeDestination) destRow += 1 return True