Make some general import/export functionality improvements

This commit is contained in:
Veronica Berglyd Olsen
2024-12-31 00:14:25 +01:00
parent 33aea82e6e
commit 136939d391
3 changed files with 22 additions and 16 deletions
+7 -1
View File
@@ -29,7 +29,8 @@ import uuid
from typing import TYPE_CHECKING, Any
from novelwriter.common import (
checkBool, checkInt, checkStringNone, checkUuid, isHandle, simplified
checkBool, checkInt, checkStringNone, checkUuid, isHandle,
makeFileNameSafe, simplified
)
from novelwriter.core.status import NWStatus
@@ -101,6 +102,11 @@ class NWProjectData:
"""Return the project name."""
return self._name
@property
def fileSafeName(self) -> str:
"""Return the project name in a file name safe format."""
return makeFileNameSafe(self._name)
@property
def author(self) -> str:
"""Return the project author."""
+10 -9
View File
@@ -96,9 +96,11 @@ class GuiWordList(NDialog):
self.newEntry = QLineEdit(self)
self.addButton = NIconToolButton(self, iSz, "add")
self.addButton.setToolTip(self.tr("Add Word"))
self.addButton.clicked.connect(self._doAdd)
self.delButton = NIconToolButton(self, iSz, "remove")
self.delButton.setToolTip(self.tr("Remove Word"))
self.delButton.clicked.connect(self._doDelete)
self.editBox = QHBoxLayout()
@@ -184,11 +186,10 @@ class GuiWordList(NDialog):
SHARED.info(self.tr(
"Note: The import file must be a plain text file with UTF-8 or ASCII encoding."
))
ffilter = formatFileFilter(["*.txt", "*"])
path, _ = QFileDialog.getOpenFileName(
self, self.tr("Import File"), str(CONFIG.homePath()), filter=ffilter
)
if path:
if path := QFileDialog.getOpenFileName(
self, self.tr("Import File"), str(CONFIG.homePath()),
filter=formatFileFilter(["*.txt", "*"]),
)[0]:
try:
with open(path, mode="r", encoding="utf-8") as fo:
words = set(w.strip() for w in fo.read().split())
@@ -202,10 +203,10 @@ class GuiWordList(NDialog):
@pyqtSlot()
def _exportWords(self) -> None:
"""Export words to file."""
path, _ = QFileDialog.getSaveFileName(
self, self.tr("Export File"), str(CONFIG.homePath())
)
if path:
name = f"{SHARED.project.data.fileSafeName} - {self.windowTitle()}.txt"
if path := QFileDialog.getSaveFileName(
self, self.tr("Export File"), str(CONFIG.homePath() / name),
)[0]:
try:
path = Path(path).with_suffix(".txt")
with open(path, mode="w", encoding="utf-8") as fo:
+5 -6
View File
@@ -41,7 +41,7 @@ from PyQt5.QtWidgets import (
)
from novelwriter import CONFIG, SHARED
from novelwriter.common import checkInt, formatFileFilter, makeFileNameSafe
from novelwriter.common import checkInt, formatFileFilter
from novelwriter.constants import nwKeyWords, nwLabels, nwStats, nwStyles, trConst
from novelwriter.enum import nwChange, nwDocMode, nwItemClass, nwItemLayout, nwItemType, nwOutline
from novelwriter.error import logException
@@ -541,11 +541,10 @@ class GuiOutlineTree(QTreeWidget):
@pyqtSlot()
def exportOutline(self) -> None:
"""Export the outline as a CSV file."""
path = CONFIG.lastPath("outline") / f"{makeFileNameSafe(SHARED.project.data.name)}.csv"
path, _ = QFileDialog.getSaveFileName(
self, self.tr("Save Outline As"), str(path), formatFileFilter(["*.csv", "*"])
)
if path:
name = CONFIG.lastPath("outline") / f"{SHARED.project.data.fileSafeName}.csv"
if path := QFileDialog.getSaveFileName(
self, self.tr("Save Outline As"), str(name), formatFileFilter(["*.csv", "*"])
)[0]:
CONFIG.setLastPath("outline", path)
logger.info("Writing CSV file: %s", path)
cols = [col for col in self._treeOrder if not self._colHidden[col]]