Add a formatting function for file extension filters for file dialogs
This commit is contained in:
+14
-1
@@ -41,7 +41,7 @@ from PyQt5.QtCore import QCoreApplication, QUrl
|
|||||||
|
|
||||||
from novelwriter.enum import nwItemClass, nwItemType, nwItemLayout
|
from novelwriter.enum import nwItemClass, nwItemType, nwItemLayout
|
||||||
from novelwriter.error import logException
|
from novelwriter.error import logException
|
||||||
from novelwriter.constants import nwConst, nwUnicode
|
from novelwriter.constants import nwConst, nwLabels, nwUnicode, trConst
|
||||||
|
|
||||||
if TYPE_CHECKING: # pragma: no cover
|
if TYPE_CHECKING: # pragma: no cover
|
||||||
from typing import TypeGuard # Requires Python 3.10
|
from typing import TypeGuard # Requires Python 3.10
|
||||||
@@ -248,6 +248,19 @@ def formatVersion(value: str) -> str:
|
|||||||
return value.lower().replace("a", " Alpha ").replace("b", " Beta ").replace("rc", " RC ")
|
return value.lower().replace("a", " Alpha ").replace("b", " Beta ").replace("rc", " RC ")
|
||||||
|
|
||||||
|
|
||||||
|
def formatFileFilter(extensions: list[str | tuple[str, str]]) -> str:
|
||||||
|
"""Format a list of extensions, or extension + label pairs into a
|
||||||
|
QFileDialog extensions filter.
|
||||||
|
"""
|
||||||
|
result = []
|
||||||
|
for ext in extensions:
|
||||||
|
if isinstance(ext, str):
|
||||||
|
result.append(f"{trConst(nwLabels.FILE_FILTERS.get(ext))} ({ext})")
|
||||||
|
elif isinstance(ext, tuple) and len(ext) == 2:
|
||||||
|
result.append(f"{ext[0]} ({ext[1]})")
|
||||||
|
return ";;".join(result)
|
||||||
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# String Functions
|
# String Functions
|
||||||
##
|
##
|
||||||
|
|||||||
@@ -270,6 +270,12 @@ class nwLabels:
|
|||||||
nwBuildFmt.J_HTML: ".json",
|
nwBuildFmt.J_HTML: ".json",
|
||||||
nwBuildFmt.J_NWD: ".json",
|
nwBuildFmt.J_NWD: ".json",
|
||||||
}
|
}
|
||||||
|
FILE_FILTERS = {
|
||||||
|
"*.txt": QT_TRANSLATE_NOOP("Constant", "Text files"),
|
||||||
|
"*.md": QT_TRANSLATE_NOOP("Constant", "Markdown files"),
|
||||||
|
"*.nwd": QT_TRANSLATE_NOOP("Constant", "novelWriter files"),
|
||||||
|
"*": QT_TRANSLATE_NOOP("Constant", "All files"),
|
||||||
|
}
|
||||||
UNIT_NAME = {
|
UNIT_NAME = {
|
||||||
"mm": QT_TRANSLATE_NOOP("Constant", "Millimetres"),
|
"mm": QT_TRANSLATE_NOOP("Constant", "Millimetres"),
|
||||||
"cm": QT_TRANSLATE_NOOP("Constant", "Centimetres"),
|
"cm": QT_TRANSLATE_NOOP("Constant", "Centimetres"),
|
||||||
|
|||||||
@@ -34,11 +34,11 @@ from PyQt5.QtCore import QUrl
|
|||||||
|
|
||||||
from novelwriter.common import (
|
from novelwriter.common import (
|
||||||
checkBool, checkFloat, checkInt, checkIntTuple, checkPath, checkString,
|
checkBool, checkFloat, checkInt, checkIntTuple, checkPath, checkString,
|
||||||
checkStringNone, checkUuid, formatInt, formatTime, formatTimeStamp,
|
checkStringNone, checkUuid, formatFileFilter, formatInt, formatTime,
|
||||||
formatVersion, fuzzyTime, getFileSize, hexToInt, isHandle, isItemClass,
|
formatTimeStamp, formatVersion, fuzzyTime, getFileSize, hexToInt, isHandle,
|
||||||
isItemLayout, isItemType, isTitleTag, jsonEncode, makeFileNameSafe, minmax,
|
isItemClass, isItemLayout, isItemType, isTitleTag, jsonEncode,
|
||||||
numberToRoman, NWConfigParser, openExternalPath, readTextFile, simplified,
|
makeFileNameSafe, minmax, numberToRoman, NWConfigParser, openExternalPath,
|
||||||
transferCase, xmlIndent, yesNo
|
readTextFile, simplified, transferCase, xmlIndent, yesNo
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -346,6 +346,18 @@ def testBaseCommon_formatVersion():
|
|||||||
# END Test testBaseCommon_formatVersion
|
# END Test testBaseCommon_formatVersion
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.base
|
||||||
|
def testBaseCommon_formatFileFilter():
|
||||||
|
"""Test the formatFileFilter function."""
|
||||||
|
assert formatFileFilter(["*.txt"]) == "Text files (*.txt)"
|
||||||
|
assert formatFileFilter(["*.txt", "*"]) == "Text files (*.txt);;All files (*)"
|
||||||
|
assert formatFileFilter([("Stuff", "*.stuff"), "*.txt", "*"]) == (
|
||||||
|
"Stuff (*.stuff);;Text files (*.txt);;All files (*)"
|
||||||
|
)
|
||||||
|
|
||||||
|
# END Test testBaseCommon_formatFileFilter
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.base
|
@pytest.mark.base
|
||||||
def testBaseCommon_simplified():
|
def testBaseCommon_simplified():
|
||||||
"""Test the simplified function."""
|
"""Test the simplified function."""
|
||||||
|
|||||||
Reference in New Issue
Block a user