132 lines
4.5 KiB
Python
132 lines
4.5 KiB
Python
"""Utility Functions."""
|
|
from typing import NoReturn, Self
|
|
|
|
from PyQt6.QtCore import QCoreApplication, QDir, QStandardPaths, Qt
|
|
from PyQt6.QtGui import QColor, QFont, QFontDatabase, QTextCharFormat
|
|
from PyQt6.QtNetwork import QNetworkAccessManager, QNetworkDiskCache
|
|
from PyQt6.QtSql import QSqlQuery
|
|
|
|
translate = QCoreApplication.translate
|
|
|
|
|
|
def query_error(query: QSqlQuery) -> NoReturn:
|
|
"""Standarized query error reporter."""
|
|
print(
|
|
translate("MainWindow", "SQL Error:\n")
|
|
+ "{}\n{}\n{}:{}".format(
|
|
query.executedQuery(),
|
|
query.boundValues(),
|
|
query.lastError().type(),
|
|
query.lastError().text(),
|
|
)
|
|
)
|
|
raise Exception(translate("MainWindow", "SQL Error"))
|
|
|
|
|
|
class Resources:
|
|
_instance = None
|
|
nam = QNetworkAccessManager()
|
|
headerFont: QFont
|
|
labelFont: QFont
|
|
boldFont: QFont
|
|
textFont: QFont
|
|
italicFont: QFont
|
|
capsFont: QFont
|
|
smallCapsFont: QFont
|
|
phonicFont: QFont
|
|
|
|
baseColor: QColor
|
|
linkColor: QColor
|
|
subduedColor: QColor
|
|
|
|
subduedBackground: QColor
|
|
|
|
headerFormat = QTextCharFormat()
|
|
labelFormat = QTextCharFormat()
|
|
subduedFormat = QTextCharFormat()
|
|
subduedItalicFormat = QTextCharFormat()
|
|
sOnSFormat = QTextCharFormat()
|
|
subduedLabelFormat = QTextCharFormat()
|
|
phonticFormat = QTextCharFormat()
|
|
boldFormat = QTextCharFormat()
|
|
boldOnSFormat = QTextCharFormat()
|
|
italicFormat = QTextCharFormat()
|
|
textFormat = QTextCharFormat()
|
|
smallCapsFormat = QTextCharFormat()
|
|
|
|
def __new__(cls: type[Self]) -> Self:
|
|
if cls._instance:
|
|
return cls._instance
|
|
cls._instance = super(Resources, cls).__new__(cls)
|
|
#
|
|
# colors
|
|
#
|
|
cls.baseColor = QColor(Qt.GlobalColor.white)
|
|
cls.linkColor = QColor("#4a7d95")
|
|
cls.subduedColor = QColor(Qt.GlobalColor.gray)
|
|
cls.subduedBackground = QColor("#444")
|
|
#
|
|
# Formats
|
|
#
|
|
LARGE = 36
|
|
MEDIUM = 22
|
|
SMALL = 18
|
|
cls.headerFormat.setFontPointSize(LARGE)
|
|
cls.labelFormat.setFontPointSize(MEDIUM)
|
|
cls.sOnSFormat.setForeground(cls.subduedColor)
|
|
#cls.sOnSFormat.setBackground(cls.subduedBackground)
|
|
cls.sOnSFormat.setFontPointSize(SMALL)
|
|
cls.subduedFormat.setForeground(cls.subduedColor)
|
|
cls.subduedFormat.setFontPointSize(SMALL)
|
|
cls.subduedLabelFormat.setForeground(cls.subduedColor)
|
|
cls.subduedLabelFormat.setFontPointSize(SMALL)
|
|
cls.phonticFormat.setFont(QFontDatabase.font("Gentium", None,20))
|
|
cls.phonticFormat.setFontPointSize(SMALL)
|
|
cls.boldFormat.setFontWeight(QFont.Weight.Bold)
|
|
cls.boldFormat.setFontPointSize(SMALL)
|
|
cls.boldOnSFormat.setFontWeight(QFont.Weight.Bold)
|
|
cls.boldOnSFormat.setFontPointSize(SMALL)
|
|
cls.boldOnSFormat.setBackground(cls.subduedBackground)
|
|
cls.italicFormat.setFontItalic(True)
|
|
cls.italicFormat.setFontPointSize(SMALL)
|
|
cls.textFormat.setFontPointSize(SMALL)
|
|
cls.smallCapsFormat.setFontPointSize(SMALL)
|
|
cls.smallCapsFormat.setFontCapitalization(QFont.Capitalization.SmallCaps)
|
|
#
|
|
# Fonts
|
|
#
|
|
cls.headerFont = QFontDatabase.font("OpenDyslexic", None, 10)
|
|
cls.headerFont.setPixelSize(48)
|
|
cls.labelFont = QFont(cls.headerFont)
|
|
cls.labelFont.setPixelSize(30)
|
|
cls.boldFont = QFont(cls.headerFont)
|
|
cls.boldFont.setPixelSize(20)
|
|
cls.textFont = QFont(cls.boldFont)
|
|
cls.italicFont = QFont(cls.boldFont)
|
|
cls.capsFont = QFont(cls.boldFont)
|
|
cls.smallCapsFont = QFont(cls.boldFont)
|
|
|
|
cls.headerFont.setWeight(QFont.Weight.Bold)
|
|
cls.boldFont.setBold(True)
|
|
cls.italicFont.setItalic(True)
|
|
cls.capsFont.setCapitalization(QFont.Capitalization.AllUppercase)
|
|
cls.smallCapsFont.setCapitalization(QFont.Capitalization.SmallCaps)
|
|
|
|
cls.phonicFont = QFontDatabase.font("Gentium", None, 10)
|
|
cls.phonicFont.setPixelSize(20)
|
|
|
|
#
|
|
# Setup the Network Manager
|
|
#
|
|
cacheDir = QDir(
|
|
QStandardPaths.writableLocation(
|
|
QStandardPaths.StandardLocation.GenericCacheLocation
|
|
)
|
|
)
|
|
cacheDir.mkdir("Troglodite")
|
|
cacheDir = QDir(cacheDir.path() + QDir.separator() + "Troglodite")
|
|
netCache = QNetworkDiskCache()
|
|
netCache.setCacheDirectory(cacheDir.path())
|
|
cls.nam.setCache(netCache)
|
|
return cls._instance
|