From 7428b10630988030f35e275be8faf5a814d71800 Mon Sep 17 00:00:00 2001 From: kagenihisomi Date: Sun, 28 Jan 2024 11:56:05 +1100 Subject: [PATCH] fontformat: show custom fonts only --- ui/configpanel.py | 9 +++++++++ ui/fontformatpanel.py | 31 +++++++++++++++++++++++++++++-- utils/config.py | 1 + 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/ui/configpanel.py b/ui/configpanel.py index 85b39006..1e0091b6 100644 --- a/ui/configpanel.py +++ b/ui/configpanel.py @@ -458,6 +458,12 @@ def __init__(self, *args, **kwargs) -> None: global_fntfmt_layout.addItem(QSpacerItem(0, 0, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding), 0, 2) + # self.let_show_only_custom_fonts = QCheckBox(parent=self) + self.let_show_only_custom_fonts, sublock = checkbox_with_label(self.tr("Font selection"), discription=self.tr("Show only custom fonts")) + global_fntfmt_layout.addWidget(sublock, 4, 0) + self.let_show_only_custom_fonts.stateChanged.connect(self.on_show_only_custom_fonts) + + self.let_autolayout_checker, sublock = generalConfigPanel.addCheckBox(self.tr('Auto layout'), discription=self.tr('Split translation into multi-lines according to the extracted balloon region.')) self.let_autolayout_adaptive_fntsize_checker, _ = generalConfigPanel.addCheckBox(None, self.tr('Adjust font size adaptively if it is set to \"decide by program.\"'), target_block=sublock) @@ -586,6 +592,9 @@ def on_family_flag_changed(self): def on_effect_flag_changed(self): pcfg.let_fnteffect_flag = self.let_effect_combox.currentIndex() + def on_show_only_custom_fonts(self): + pcfg.let_show_only_custom_fonts_flag = self.let_show_only_custom_fonts.isChecked() + def focusOnTranslator(self): idx0, idx1 = self.trans_sub_block.idx0, self.trans_sub_block.idx1 self.configTable.setCurrentItem(idx0, idx1) diff --git a/ui/fontformatpanel.py b/ui/fontformatpanel.py index de272a0f..2e511a62 100644 --- a/ui/fontformatpanel.py +++ b/ui/fontformatpanel.py @@ -4,8 +4,7 @@ from qtpy.QtWidgets import QComboBox, QMenu, QMessageBox, QStackedLayout, QGraphicsDropShadowEffect, QLineEdit, QScrollArea, QSizePolicy, QHBoxLayout, QVBoxLayout, QFrame, QFontComboBox, QApplication, QPushButton, QCheckBox, QLabel from qtpy.QtCore import Signal, Qt, QRectF -from qtpy.QtGui import QDoubleValidator, QFocusEvent, QMouseEvent, QTextCursor, QFontMetrics, QIcon, QColor, QPixmap, QPainter, QContextMenuEvent, QKeyEvent - +from qtpy.QtGui import QDoubleValidator, QFocusEvent, QMouseEvent, QTextCursor, QFontMetrics, QIcon, QColor, QPixmap, QPainter, QContextMenuEvent, QKeyEvent, QFontDatabase from utils.fontformat import FontFormat from utils import shared @@ -330,6 +329,21 @@ def __init__(self, emit_if_focused=True, *args, **kwargs) -> None: self.emit_if_focused = emit_if_focused self._current_font = self.currentFont().family() + # Find all custom(application) fonts + application_fonts=[] + i=0 + while True: + # Returns ['my_font_name'] + font_=QFontDatabase.applicationFontFamilies(i) + if font_: + print(font_) + application_fonts.append(font_[0]) + else: + break + i+=1 + self.application_fonts=application_fonts + + def apply_fontfamily(self): ffamily = self.currentFont().family() if ffamily in shared.FONT_FAMILIES: @@ -348,6 +362,19 @@ def on_fontfamily_changed(self): self.apply_fontfamily() + def showPopup(self): + if pcfg.let_show_only_custom_fonts_flag: + current_font = self.currentFont().family() + + # Override and set custom textlist of fonts + self.clear() + self.addItems(self.application_fonts) + + # Prevent the current font from changing to first in list + self.setCurrentText(current_font) + self.on_fontfamily_changed() + super().showPopup() + CHEVRON_SIZE = 20 def chevron_down(): return QIcon(r'icons/chevron-down.svg').pixmap(CHEVRON_SIZE, CHEVRON_SIZE, mode=QIcon.Mode.Normal) diff --git a/utils/config.py b/utils/config.py index d42511d3..0992039c 100644 --- a/utils/config.py +++ b/utils/config.py @@ -83,6 +83,7 @@ class ProgramConfig(Config): let_autolayout_flag: bool = True let_autolayout_adaptive_fntsz: bool = True let_uppercase_flag: bool = True + let_show_only_custom_fonts_flag:bool = False text_styles_path: str = osp.join(shared.DEFAULT_TEXTSTYLE_DIR, 'default.json') expand_tstyle_panel: bool = True fsearch_case: bool = False