Fix inconsistend expand/collapse when tree items are moved

This commit is contained in:
Veronica Berglyd Olsen
2022-06-11 12:33:37 +02:00
parent ad438e128a
commit 77746f712f
2 changed files with 16 additions and 20 deletions
+15 -18
View File
@@ -35,9 +35,9 @@ from time import time
from PyQt5.QtCore import Qt, QSize, pyqtSignal, pyqtSlot
from PyQt5.QtGui import QIcon, QPalette
from PyQt5.QtWidgets import (
qApp, QTreeWidget, QTreeWidgetItem, QAbstractItemView, QMenu, QAction,
QFrame, QDialog, QHeaderView, QWidget, QVBoxLayout, QLabel, QToolButton,
QSizePolicy, QInputDialog, QHBoxLayout, QShortcut
QAbstractItemView, QAction, QDialog, QFrame, QHBoxLayout, QHeaderView,
QInputDialog, QLabel, QMenu, QShortcut, QSizePolicy, QToolButton,
QTreeWidget, QTreeWidgetItem, QVBoxLayout, QWidget
)
from novelwriter.core import NWDoc
@@ -143,15 +143,6 @@ class GuiProjectView(QWidget):
"""
return self.projTree.hasFocus()
def anyFocus(self):
"""Check if any widget or child widget has focus.
"""
if self.hasFocus():
return True
if self.isAncestorOf(qApp.focusWidget()):
return True
return False
##
# Public Slots
##
@@ -536,34 +527,40 @@ class GuiProjectTree(QTreeWidget):
"""Move an item up or down in the tree.
"""
tHandle = self.getSelectedHandle()
tItem = self._getTreeItem(tHandle)
if tItem is None:
trItem = self._getTreeItem(tHandle)
if trItem is None:
logger.verbose("No item selected")
return False
pItem = tItem.parent()
pItem = trItem.parent()
isExp = trItem.isExpanded()
if pItem is None:
tIndex = self.indexOfTopLevelItem(tItem)
tIndex = self.indexOfTopLevelItem(trItem)
nChild = self.topLevelItemCount()
nIndex = tIndex + nStep
if nIndex < 0 or nIndex >= nChild:
return False
cItem = self.takeTopLevelItem(tIndex)
self.insertTopLevelItem(nIndex, cItem)
else:
tIndex = pItem.indexOfChild(tItem)
tIndex = pItem.indexOfChild(trItem)
nChild = pItem.childCount()
nIndex = tIndex + nStep
if nIndex < 0 or nIndex >= nChild:
return False
cItem = pItem.takeChild(tIndex)
pItem.insertChild(nIndex, cItem)
self._recordLastMove(cItem, pItem, tIndex)
self._alertTreeChange(tHandle=tHandle, flush=True)
self.clearSelection()
cItem.setSelected(True)
trItem.setSelected(True)
trItem.setExpanded(isExp)
return True
+1 -2
View File
@@ -26,7 +26,7 @@ from tools import buildTestProject
from PyQt5.QtWidgets import QMessageBox, QInputDialog
from novelwriter.gui.projtree import GuiProjectView, GuiProjectTree
from novelwriter.gui.projtree import GuiProjectTree
from novelwriter.enum import nwItemType, nwItemClass
@@ -163,7 +163,6 @@ def testGuiProjTree_MoveItems(qtbot, monkeypatch, nwGUI, fncDir, mockRnd):
monkeypatch.setattr(QMessageBox, "question", lambda *a: QMessageBox.Yes)
monkeypatch.setattr(QMessageBox, "information", lambda *a: QMessageBox.Yes)
monkeypatch.setattr(QInputDialog, "getText", lambda *a, text: (text, True))
monkeypatch.setattr(GuiProjectView, "anyFocus", lambda *a: True)
nwTree = nwGUI.treeView