Reimplement item deletion

This commit is contained in:
Veronica Berglyd Olsen
2024-11-19 23:26:01 +01:00
parent 3532306833
commit 54685c8184
5 changed files with 69 additions and 353 deletions
+1 -3
View File
@@ -521,10 +521,8 @@ class ProjectBuilder:
f"%Short: {bfNote}\n\n"
)
# Also add the archive and trash folders
# Also add the archive folder
project.newRoot(nwItemClass.ARCHIVE)
project.trashFolder()
project.saveProject()
project.closeProject()
+16
View File
@@ -34,6 +34,7 @@ from PyQt5.QtGui import QIcon
from novelwriter import SHARED
from novelwriter.common import minmax
from novelwriter.core.item import NWItem
from novelwriter.enum import nwItemClass
from novelwriter.types import QtAlignRight
if TYPE_CHECKING: # pragma: no cover
@@ -214,6 +215,7 @@ class ProjectNode:
if self._parent:
child.item.setParent(self.item.itemHandle)
child.item.setRoot(self.item.itemRoot)
child.item.setClassDefaults(self.item.itemClass)
else:
child.item.setParent(None)
child.item.setRoot(child.item.itemHandle)
@@ -290,6 +292,10 @@ class ProjectModel(QAbstractItemModel):
return index.internalPointer()
return None
def nodes(self, indices: list[QModelIndex]) -> list[ProjectNode]:
"""Return the nodes for a list of model indices."""
return [i.internalPointer() for i in indices if i.isValid() and i.column() == 0]
def indexFromHandle(self, handle: str | None) -> QModelIndex:
"""Get the index representing a node in the model."""
if handle and (node := self._tree.nodes.get(handle)):
@@ -342,6 +348,16 @@ class ProjectModel(QAbstractItemModel):
self.endMoveRows()
return
def trashSelection(self, indices: list[QModelIndex]) -> bool:
"""Check if a selection of indices are all in trash or not."""
for index in indices:
if index.isValid():
node: ProjectNode = index.internalPointer()
print(node)
if node.item.itemClass != nwItemClass.TRASH:
return False
return True
def multiMove(self, indices: list[QModelIndex], target: QModelIndex) -> None:
"""Move multiple items to a new location."""
if target.isValid():
+5 -13
View File
@@ -154,7 +154,7 @@ class NWProject:
# Item Methods
##
def newRoot(self, itemClass: nwItemClass, pos: int = -1) -> str | None:
def newRoot(self, itemClass: nwItemClass, pos: int = -1) -> str:
"""Add a new root folder to the project. If label is not set,
use the class label.
"""
@@ -218,27 +218,19 @@ class NWProject:
project entry and a document file if it exists.
"""
if self._tree.checkType(tHandle, nwItemType.FILE):
delDoc = self._storage.getDocument(tHandle)
if not delDoc.deleteDocument():
doc = self._storage.getDocument(tHandle)
if not doc.deleteDocument():
SHARED.error(
self.tr("Could not delete document file."),
info=delDoc.getError()
info=doc.getError()
)
return False
self._index.deleteHandle(tHandle)
del self._tree[tHandle]
self._tree.remove(tHandle)
return True
def trashFolder(self) -> str:
"""Add the special trash root folder to the project."""
# trashHandle = self._tree.trashRoot
# if trashHandle is None:
# label = trConst(nwLabels.CLASS_NAME[nwItemClass.TRASH])
# return self._tree.create(label, None, nwItemType.ROOT, nwItemClass.TRASH)
return ""
##
# Project Methods
##
+24 -17
View File
@@ -28,7 +28,7 @@ import random
from collections.abc import Iterable, Iterator
from pathlib import Path
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Literal, overload
from PyQt5.QtCore import QModelIndex
@@ -141,6 +141,29 @@ class NWTree:
return False
return True
def remove(self, tHandle: str) -> bool:
"""Remove an item from the project tree."""
if (node := self._nodes.get(tHandle)) and tHandle in self._items:
index = self._model.indexFromNode(node)
if index.isValid() and self._model.removeChild(index.parent(), index.row()):
del self._nodes[tHandle]
del self._items[tHandle]
return True
@overload # pragma: no cover
def create(
self, label: str, parent: None, itemType: Literal[nwItemType.ROOT],
itemClass: nwItemClass, pos: int = -1
) -> str:
pass
@overload # pragma: no cover
def create(
self, label: str, parent: str | None, itemType: nwItemType,
itemClass: nwItemClass = nwItemClass.NO_CLASS, pos: int = -1
) -> str | None:
pass
def create(
self, label: str, parent: str | None, itemType: nwItemType,
itemClass: nwItemClass = nwItemClass.NO_CLASS, pos: int = -1,
@@ -431,22 +454,6 @@ class NWTree:
logger.error("No tree item with handle '%s'", str(tHandle))
return None
def __delitem__(self, tHandle: str) -> None:
"""Remove an item from the internal lists and dictionaries."""
# if tHandle in self._order and tHandle in self._tree:
# self._order.remove(tHandle)
# del self._tree[tHandle]
# else:
# logger.warning("Failed to delete item '%s': item not found", tHandle)
# return
# if tHandle in self._roots:
# del self._roots[tHandle]
# if tHandle == self._trash:
# self._trash = None
return
def __contains__(self, tHandle: str) -> bool:
"""Checks if a handle exists in the tree."""
return tHandle in self._items