Skip to content

Commit

Permalink
Add the file filter function where needed
Browse files Browse the repository at this point in the history
  • Loading branch information
vkbo committed Feb 11, 2024
1 parent 642b5e3 commit 701fe29
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 24 deletions.
8 changes: 3 additions & 5 deletions novelwriter/dialogs/wordlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
)

from novelwriter import CONFIG, SHARED
from novelwriter.common import formatFileFilter
from novelwriter.core.spellcheck import UserDictionary
from novelwriter.extensions.configlayout import NColourLabel

Expand Down Expand Up @@ -184,12 +185,9 @@ def _importWords(self) -> None:
SHARED.info(self.tr(
"Note: The import file must be a plain text file with UTF-8 or ASCII encoding."
))
extFilter = [
"{0} (*.txt)".format(self.tr("Text files")),
"{0} (*)".format(self.tr("All files")),
]
ffilter = formatFileFilter(["*.txt", "*"])
path, _ = QFileDialog.getOpenFileName(
self, self.tr("Import File"), str(Path.home()), filter=";;".join(extFilter)
self, self.tr("Import File"), str(Path.home()), filter=ffilter
)
if path:
try:
Expand Down
11 changes: 3 additions & 8 deletions novelwriter/guimain.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
from novelwriter.enum import (
nwDocAction, nwDocInsert, nwDocMode, nwItemType, nwWidget, nwView
)
from novelwriter.common import hexToInt
from novelwriter.common import formatFileFilter, hexToInt

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -664,14 +664,9 @@ def importDocument(self) -> bool:
return False

lastPath = CONFIG.lastPath()
extFilter = [
"{0} (*.txt)".format(self.tr("Text files")),
"{0} (*.md)".format(self.tr("Markdown files")),
"{0} (*.nwd)".format(self.tr("novelWriter files")),
"{0} (*)".format(self.tr("All files")),
]
ffilter = formatFileFilter(["*.txt", "*.md", "*.nwd", "*"])
loadFile, _ = QFileDialog.getOpenFileName(
self, self.tr("Import File"), str(lastPath), filter=";;".join(extFilter)
self, self.tr("Import File"), str(lastPath), filter=ffilter
)
if not loadFile:
return False
Expand Down
8 changes: 4 additions & 4 deletions novelwriter/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

from PyQt5.QtCore import QObject, QRunnable, QThreadPool, pyqtSignal
from PyQt5.QtWidgets import QFileDialog, QMessageBox, QWidget
from novelwriter.common import formatFileFilter

from novelwriter.constants import nwFiles
from novelwriter.core.spellcheck import NWSpellEnchant
Expand Down Expand Up @@ -221,13 +222,12 @@ def runInThreadPool(self, runnable: QRunnable, priority: int = 0) -> None:
def getProjectPath(self, parent: QWidget, path: str | Path | None = None,
allowZip: bool = False) -> Path | None:
"""Open the file dialog and select a novelWriter project file."""
label = (self.tr("novelWriter Project File or Zip")
label = (self.tr("novelWriter Project File or Zip File")
if allowZip else self.tr("novelWriter Project File"))
ext = f"{nwFiles.PROJ_FILE} *.zip" if allowZip else nwFiles.PROJ_FILE
ffilter = formatFileFilter([(label, ext), "*"])
selected, _ = QFileDialog.getOpenFileName(
parent, self.tr("Open Project"), str(path or ""), filter=";;".join(
[f"{label} ({ext})", "{0} (*)".format(self.tr("All Files"))]
)
parent, self.tr("Open Project"), str(path or ""), filter=ffilter
)
return Path(selected) if selected else None

Expand Down
11 changes: 5 additions & 6 deletions novelwriter/tools/dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

from novelwriter import CONFIG, SHARED
from novelwriter.error import formatException
from novelwriter.common import openExternalPath, formatInt, getFileSize
from novelwriter.common import formatFileFilter, openExternalPath, formatInt, getFileSize

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -180,12 +180,11 @@ def closeEvent(self, event: QCloseEvent) -> None:
@pyqtSlot()
def _doBrowseHunspell(self):
"""Browse for a Free/Libre Office dictionary."""
extFilter = [
self.tr("Free or Libre Office extension ({0})").format("*.sox *.oxt"),
self.tr("All files ({0})").format("*"),
]
ffilter = formatFileFilter([
(self.tr("Free or Libre Office extension"), "*.sox *.oxt"), "*"
])
soxFile, _ = QFileDialog.getOpenFileName(
self, self.tr("Browse Files"), "", filter=";;".join(extFilter)
self, self.tr("Browse Files"), "", filter=ffilter
)
if soxFile:
path = Path(soxFile).absolute()
Expand Down
2 changes: 1 addition & 1 deletion novelwriter/tools/writingstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def _saveData(self, dataFmt: int) -> bool:
# Generate the file name
savePath = CONFIG.lastPath() / f"sessionStats.{fileExt}"
savePath, _ = QFileDialog.getSaveFileName(
self, self.tr("Save Data As"), str(savePath), "%s (*.%s)" % (textFmt, fileExt)
self, self.tr("Save Data As"), str(savePath), f"{textFmt} (*.{fileExt})"
)
if not savePath:
return False
Expand Down

0 comments on commit 701fe29

Please sign in to comment.