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.error import logException
|
||||
from novelwriter.constants import nwConst, nwUnicode
|
||||
from novelwriter.constants import nwConst, nwLabels, nwUnicode, trConst
|
||||
|
||||
if TYPE_CHECKING: # pragma: no cover
|
||||
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 ")
|
||||
|
||||
|
||||
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
|
||||
##
|
||||
|
||||
Reference in New Issue
Block a user