From c2a37c814dab610fb291a72579b9aba5ec2265d1 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sat, 30 Jan 2021 19:43:00 +0100 Subject: [PATCH 1/3] Fix Open Project list sorting issue --- nw/gui/projload.py | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/nw/gui/projload.py b/nw/gui/projload.py index 348e26d7..b58f42f4 100644 --- a/nw/gui/projload.py +++ b/nw/gui/projload.py @@ -258,40 +258,31 @@ class GuiProjectLoad(QDialog): def _populateList(self): """Populate the list box with recent project data. """ - listOrder = [] - listData = {} - for projPath in self.mainConf.recentProj.keys(): + dataList = [] + for projPath in self.mainConf.recentProj: theEntry = self.mainConf.recentProj[projPath] - theTitle = "" - theTime = 0 - theWords = 0 - if "title" in theEntry.keys(): - theTitle = theEntry["title"] - if "time" in theEntry.keys(): - theTime = theEntry["time"] - if "words" in theEntry.keys(): - theWords = theEntry["words"] - if theTime > 0: - listOrder.append(theTime) - listData[theTime] = [theTitle, theWords, projPath] + theTitle = theEntry.get("title", "") + theTime = theEntry.get("time", 0) + theWords = theEntry.get("words", 0) + dataList.append([theTitle, theTime, theWords, projPath]) self.listBox.clear() - hasSelection = False - for timeStamp in sorted(listOrder, reverse=True): + sortList = sorted(dataList, key=lambda x: x[1], reverse=True) + for theTitle, theTime, theWords, projPath in sortList: newItem = QTreeWidgetItem([""]*4) newItem.setIcon(self.C_NAME, self.theParent.theTheme.getIcon("proj_nwx")) - newItem.setText(self.C_NAME, listData[timeStamp][0]) - newItem.setData(self.C_NAME, Qt.UserRole, listData[timeStamp][2]) - newItem.setText(self.C_COUNT, formatInt(listData[timeStamp][1])) - newItem.setText(self.C_TIME, datetime.fromtimestamp(timeStamp).strftime("%x %X")) + newItem.setText(self.C_NAME, theTitle) + newItem.setData(self.C_NAME, Qt.UserRole, projPath) + newItem.setText(self.C_COUNT, formatInt(theWords)) + newItem.setText(self.C_TIME, datetime.fromtimestamp(theTime).strftime("%x %X")) newItem.setTextAlignment(self.C_NAME, Qt.AlignLeft | Qt.AlignVCenter) newItem.setTextAlignment(self.C_COUNT, Qt.AlignRight | Qt.AlignVCenter) newItem.setTextAlignment(self.C_TIME, Qt.AlignRight | Qt.AlignVCenter) newItem.setFont(self.C_TIME, self.theTheme.guiFontFixed) self.listBox.addTopLevelItem(newItem) - if not hasSelection: - newItem.setSelected(True) - hasSelection = True + + if self.listBox.topLevelItemCount() > 0: + self.listBox.topLevelItem(0).setSelected(True) projColWidth = self.mainConf.getProjColWidths() if len(projColWidth) == 3: From f73cdac9e237c19fb26fe25b8b8382390e922815 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sun, 31 Jan 2021 11:53:38 +0100 Subject: [PATCH 2/3] Only allow the delete item command in the project tree if it has focus --- nw/gui/projtree.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/nw/gui/projtree.py b/nw/gui/projtree.py index 49ca0fcb..ea81c068 100644 --- a/nw/gui/projtree.py +++ b/nw/gui/projtree.py @@ -419,6 +419,9 @@ class GuiProjectTree(QTreeWidget): logger.error("No project open") return False + if not self.hasFocus(): + return False + if tHandle is None: tHandle = self.getSelectedHandle() From b02c4904a96c0d6530e18f3806d480adc07e6858 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sun, 31 Jan 2021 11:58:56 +0100 Subject: [PATCH 3/3] Fix tests --- tests/test_gui_doceditor.py | 2 ++ tests/test_gui_projtree.py | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/test_gui_doceditor.py b/tests/test_gui_doceditor.py index 9ef62951..de19fe9d 100644 --- a/tests/test_gui_doceditor.py +++ b/tests/test_gui_doceditor.py @@ -12,6 +12,7 @@ from PyQt5.QtCore import Qt from PyQt5.QtGui import QTextCursor from PyQt5.QtWidgets import QAction, QMessageBox +from nw.gui.projtree import GuiProjectTree from nw.constants import nwItemType, nwDocAction keyDelay = 2 @@ -24,6 +25,7 @@ def testGuiEditor_Main(qtbot, monkeypatch, nwGUI, fncDir, fncProj, refDir, outDi """ # Block message box monkeypatch.setattr(QMessageBox, "question", lambda *args: QMessageBox.Yes) + monkeypatch.setattr(GuiProjectTree, "hasFocus", lambda *args: True) # Create new, save, close project nwGUI.theProject.projTree.setSeed(42) diff --git a/tests/test_gui_projtree.py b/tests/test_gui_projtree.py index d49fa7a6..7d895deb 100644 --- a/tests/test_gui_projtree.py +++ b/tests/test_gui_projtree.py @@ -10,6 +10,8 @@ from tools import writeFile from PyQt5.QtCore import QItemSelectionModel from PyQt5.QtWidgets import QAction, QMessageBox +from nw.guimain import GuiMain +from nw.gui.projtree import GuiProjectTree from nw.constants import nwItemType, nwItemClass @pytest.mark.gui @@ -18,7 +20,8 @@ def testGuiProjTree_TreeItems(qtbot, caplog, monkeypatch, nwGUI, nwMinimal): """ # Block message box monkeypatch.setattr(QMessageBox, "question", lambda *args: QMessageBox.Yes) - monkeypatch.setattr("nw.guimain.GuiMain.editItem", lambda *args: None) + monkeypatch.setattr(GuiMain, "editItem", lambda *args: None) + monkeypatch.setattr(GuiProjectTree, "hasFocus", lambda *args: True) nwGUI.theProject.projTree.setSeed(42) nwTree = nwGUI.treeView