Disable dragging and dropping onto root level, and allow free moving of notes between root folders.

This commit is contained in:
Veronica K. B. Olsen
2019-11-02 15:12:52 +01:00
parent a39387d405
commit 9dbe39347c
+14 -6
View File
@@ -18,7 +18,7 @@ from PyQt5.QtGui import QIcon, QFont, QColor
from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem, QAbstractItemView, QApplication from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem, QAbstractItemView, QApplication
from nw.project.item import NWItem from nw.project.item import NWItem
from nw.enum import nwItemType, nwItemClass, nwAlert from nw.enum import nwItemType, nwItemClass, nwItemLayout, nwAlert
from nw.constants import nwLabels from nw.constants import nwLabels
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -62,6 +62,10 @@ class GuiDocTree(QTreeWidget):
self.setDragEnabled(True) self.setDragEnabled(True)
self.setDragDropMode(QAbstractItemView.InternalMove) self.setDragDropMode(QAbstractItemView.InternalMove)
# But don't allow drop on root level
trRoot = self.invisibleRootItem()
trRoot.setFlags(trRoot.flags() ^ Qt.ItemIsDropEnabled)
# Set Multiple Selection by CTRL # Set Multiple Selection by CTRL
self.setSelectionMode(QAbstractItemView.ExtendedSelection) self.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.setSelectionBehavior(QAbstractItemView.SelectRows) self.setSelectionBehavior(QAbstractItemView.SelectRows)
@@ -183,7 +187,7 @@ class GuiDocTree(QTreeWidget):
nIndex = tIndex + nStep nIndex = tIndex + nStep
if nIndex < 0 or nIndex >= nChild: if nIndex < 0 or nIndex >= nChild:
return False return False
cItem = self.takeTopLevelItem(tIndex) cItem = self.takeTopLevelItem(tIndex)
self.insertTopLevelItem(nIndex, cItem) self.insertTopLevelItem(nIndex, cItem)
else: else:
tIndex = pItem.indexOfChild(tItem) tIndex = pItem.indexOfChild(tItem)
@@ -191,7 +195,7 @@ class GuiDocTree(QTreeWidget):
nIndex = tIndex + nStep nIndex = tIndex + nStep
if nIndex < 0 or nIndex >= nChild: if nIndex < 0 or nIndex >= nChild:
return False return False
cItem = pItem.takeChild(tIndex) cItem = pItem.takeChild(tIndex)
pItem.insertChild(nIndex, cItem) pItem.insertChild(nIndex, cItem)
self.clearSelection() self.clearSelection()
cItem.setSelected(True) cItem.setSelected(True)
@@ -451,12 +455,12 @@ class GuiDocTree(QTreeWidget):
trItemP = trItemS.parent() trItemP = trItemS.parent()
if trItemP is None: if trItemP is None:
logger.error("Failed to find new parent item of %s" % tHandle) logger.error("Failed to find new parent item of %s" % tHandle)
return return False
pHandle = trItemP.text(self.C_HANDLE) pHandle = trItemP.text(self.C_HANDLE)
nwItemS.setParent(pHandle) nwItemS.setParent(pHandle)
self.setTreeItemValues(tHandle) self.setTreeItemValues(tHandle)
self.theProject.setProjectChanged(True) self.theProject.setProjectChanged(True)
return return True
def _moveOrphanedItem(self, tHandle, dHandle): def _moveOrphanedItem(self, tHandle, dHandle):
trItemS = self._getTreeItem(tHandle) trItemS = self._getTreeItem(tHandle)
@@ -505,13 +509,14 @@ class GuiDocTree(QTreeWidget):
dnItem = self.theProject.getItem(dHandle) dnItem = self.theProject.getItem(dHandle)
isSame = snItem.itemClass == dnItem.itemClass isSame = snItem.itemClass == dnItem.itemClass
isNone = snItem.itemClass == nwItemClass.NO_CLASS isNone = snItem.itemClass == nwItemClass.NO_CLASS
isNote = snItem.itemLayout == nwItemLayout.NOTE
onFile = dnItem.itemType == nwItemType.FILE onFile = dnItem.itemType == nwItemType.FILE
isRoot = snItem.itemType == nwItemType.ROOT isRoot = snItem.itemType == nwItemType.ROOT
onRoot = dnItem.itemType == nwItemType.ROOT onRoot = dnItem.itemType == nwItemType.ROOT
isOnTop = self.dropIndicatorPosition() == QAbstractItemView.OnItem isOnTop = self.dropIndicatorPosition() == QAbstractItemView.OnItem
isAbove = self.dropIndicatorPosition() == QAbstractItemView.AboveItem isAbove = self.dropIndicatorPosition() == QAbstractItemView.AboveItem
isBelow = self.dropIndicatorPosition() == QAbstractItemView.BelowItem isBelow = self.dropIndicatorPosition() == QAbstractItemView.BelowItem
if (isSame or isNone) and not (onFile and isOnTop) and not isRoot: if (isSame or isNone or isNote) and not (onFile and isOnTop) and not isRoot:
logger.verbose("Drag'n'drop of item %s accepted" % sHandle) logger.verbose("Drag'n'drop of item %s accepted" % sHandle)
QTreeWidget.dropEvent(self, theEvent) QTreeWidget.dropEvent(self, theEvent)
if isNone: if isNone:
@@ -519,11 +524,14 @@ class GuiDocTree(QTreeWidget):
self._cleanOrphanedRoot() self._cleanOrphanedRoot()
else: else:
self._updateItemParent(sHandle) self._updateItemParent(sHandle)
if not isSame:
snItem.setClass(dnItem.itemClass)
elif isRoot and (isAbove or isBelow) and onRoot: elif isRoot and (isAbove or isBelow) and onRoot:
logger.verbose("Drag'n'drop of item %s accepted" % sHandle) logger.verbose("Drag'n'drop of item %s accepted" % sHandle)
QTreeWidget.dropEvent(self, theEvent) QTreeWidget.dropEvent(self, theEvent)
else: else:
logger.verbose("Drag'n'drop of item %s not accepted" % sHandle) logger.verbose("Drag'n'drop of item %s not accepted" % sHandle)
self.makeAlert("The item cannot be moved to that location.", nwAlert.ERROR)
return return