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