diff --git a/Pdf4QtLib/sources/pdfitemmodels.cpp b/Pdf4QtLib/sources/pdfitemmodels.cpp index f205277..855ee30 100644 --- a/Pdf4QtLib/sources/pdfitemmodels.cpp +++ b/Pdf4QtLib/sources/pdfitemmodels.cpp @@ -574,8 +574,11 @@ QMimeData* PDFOutlineTreeItemModel::mimeData(const QModelIndexList& indexes) con QByteArray ba; { + QModelIndex index = indexes.front(); + const PDFOutlineTreeItem* item = static_cast(index.internalPointer()); + m_dragDropItem = item->getOutlineItem()->clone(); QDataStream stream(&ba, QDataStream::WriteOnly); - stream << indexes.front().internalId(); + stream << reinterpret_cast(m_dragDropItem.data()); } mimeData->setData(mimeTypes().front(), ba); @@ -605,9 +608,29 @@ bool PDFOutlineTreeItemModel::dropMimeData(const QMimeData* data, Qt::DropAction quintptr pointer = 0; stream >> pointer; - PDFOutlineTreeItem* item = reinterpret_cast(pointer); - QModelIndex sourceIndex = createIndex(item->getRow(), column, item); - return moveRow(sourceIndex.parent(), sourceIndex.row(), parent, row); + PDFOutlineItem* item = reinterpret_cast(pointer); + if (item == m_dragDropItem.data()) + { + if (row == -1) + { + row = rowCount(parent); + } + + if (column == -1) + { + column = 0; + } + + if (insertRow(row, parent)) + { + QModelIndex targetIndex = this->index(row, column, parent); + PDFOutlineTreeItem* targetTreeItem = static_cast(targetIndex.internalPointer()); + *targetTreeItem->getOutlineItem() = *item; + return true; + } + } + + return false; } bool PDFOutlineTreeItemModel::insertRows(int row, int count, const QModelIndex& parent) @@ -695,25 +718,20 @@ bool PDFOutlineTreeItemModel::moveRows(const QModelIndex& sourceParent, int sour destinationChild = 0; } - // Signalizace začátku přesunu řádků - if (!beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1, destinationParent, destinationChild)) - { - return false; - } - + beginRemoveRows(sourceParent, sourceRow, sourceRow + count - 1); QList nodesToMove; for (int i = 0; i < count; ++i) { nodesToMove.append(static_cast(sourceNode->takeChild(sourceRow))); } + endRemoveRows(); + beginInsertRows(destinationParent, destinationChild, destinationChild + count - 1); for (PDFOutlineTreeItem* node : nodesToMove) { destNode->insertCreatedChild(destinationChild++, node); } - - // Signalizace konce přesunu řádků - endMoveRows(); + endInsertRows(); return true; } diff --git a/Pdf4QtLib/sources/pdfitemmodels.h b/Pdf4QtLib/sources/pdfitemmodels.h index 05344e4..b4b0fce 100644 --- a/Pdf4QtLib/sources/pdfitemmodels.h +++ b/Pdf4QtLib/sources/pdfitemmodels.h @@ -214,6 +214,7 @@ public: private: QIcon m_icon; bool m_editable; + mutable QSharedPointer m_dragDropItem; }; class PDF4QTLIBSHARED_EXPORT PDFSelectableOutlineTreeItemModel : public PDFOutlineTreeItemModel diff --git a/Pdf4QtLib/sources/pdfoutline.cpp b/Pdf4QtLib/sources/pdfoutline.cpp index fb54187..1496ca6 100644 --- a/Pdf4QtLib/sources/pdfoutline.cpp +++ b/Pdf4QtLib/sources/pdfoutline.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2019-2022 Jakub Melka +// Copyright (C) 2019-2023 Jakub Melka // // This file is part of PDF4QT. //