refactor: type improvements to landing.py and panel.py (#960)

* landing.py

* panel.py and a method's name change

* remove # noqa

* remove set_title method and pass title to the PanelModal constructor

Co-Authored-By: Jann Stute <46534683+Computerdores@users.noreply.github.com>

* send keyPressEvent only if it's not used

* pass window_title to constructor

use `title` if `window_title` is `None`

Co-Authored-By: Jann Stute <46534683+Computerdores@users.noreply.github.com>

---------

Co-authored-by: Jann Stute <46534683+Computerdores@users.noreply.github.com>
This commit is contained in:
VasigaranAndAngel
2025-07-03 23:40:20 +05:30
committed by GitHub
parent 001ff14799
commit 29154babf8
11 changed files with 29 additions and 42 deletions

View File

@@ -168,9 +168,7 @@ class BuildTagPanel(PanelWidget):
tsp = TagSearchPanel(self.lib, exclude_ids)
tsp.tag_chosen.connect(lambda x: self.add_parent_tag_callback(x))
self.add_tag_modal = PanelModal(tsp)
self.add_tag_modal.setTitle(Translations["tag.parent_tags.add"])
self.add_tag_modal.setWindowTitle(Translations["tag.parent_tags.add"])
self.add_tag_modal = PanelModal(tsp, Translations["tag.parent_tags.add"])
self.parent_tags_add_button.clicked.connect(self.add_tag_modal.show)
# Color ----------------------------------------------------------------

View File

@@ -279,10 +279,10 @@ class SettingsPanel(PanelWidget):
modal = PanelModal(
widget=settings_panel,
window_title=Translations["settings.title"],
done_callback=lambda: settings_panel.update_settings(driver),
has_save=True,
)
modal.title_widget.setVisible(False)
modal.setWindowTitle(Translations["settings.title"])
return modal

View File

@@ -176,7 +176,6 @@ class TagColorManager(QWidget):
self.create_namespace_modal = PanelModal(
build_namespace_panel,
Translations["namespace.create.title"],
Translations["namespace.create.title"],
has_save=True,
)

View File

@@ -35,10 +35,9 @@ class TagDatabasePanel(TagSearchPanel):
panel = BuildTagPanel(self.lib)
self.modal = PanelModal(
panel,
Translations["tag.new"],
has_save=True,
)
self.modal.setTitle(Translations["tag.new"])
self.modal.setWindowTitle(Translations["tag.new"])
if name.strip():
panel.name_field.setText(name)

View File

@@ -178,9 +178,9 @@ class TagSearchPanel(PanelWidget):
from tagstudio.qt.modals.build_tag import BuildTagPanel # here due to circular imports
self.build_tag_modal: BuildTagPanel = BuildTagPanel(self.lib)
self.add_tag_modal: PanelModal = PanelModal(self.build_tag_modal, has_save=True)
self.add_tag_modal.setTitle(Translations["tag.new"])
self.add_tag_modal.setWindowTitle(Translations["tag.add"])
self.add_tag_modal: PanelModal = PanelModal(
self.build_tag_modal, Translations["tag.new"], Translations["tag.add"], has_save=True
)
self.build_tag_modal.name_field.setText(name)
self.add_tag_modal.saved.connect(on_tag_modal_saved)
@@ -365,10 +365,10 @@ class TagSearchPanel(PanelWidget):
self.edit_modal = PanelModal(
build_tag_panel,
self.lib.tag_display_name(tag.id),
Translations["tag.edit"],
done_callback=(self.update_tags(self.search_field.text())),
has_save=True,
)
self.edit_modal.setWindowTitle(Translations["tag.edit"])
self.edit_modal.saved.connect(lambda: callback(build_tag_panel))
self.edit_modal.show()

View File

@@ -363,13 +363,12 @@ class QtDriver(DriverMixin, QObject):
# Initialize the Tag Manager panel
self.tag_manager_panel = PanelModal(
widget=TagDatabasePanel(self, self.lib),
title=Translations["tag_manager.title"],
done_callback=lambda: self.main_window.preview_panel.update_widgets(
update_preview=False
),
has_save=False,
)
self.tag_manager_panel.setTitle(Translations["tag_manager.title"])
self.tag_manager_panel.setWindowTitle(Translations["tag_manager.title"])
# Initialize the Color Group Manager panel
self.color_manager_panel = TagColorManager(self)
@@ -380,7 +379,6 @@ class QtDriver(DriverMixin, QObject):
self.add_tag_modal = PanelModal(
widget=self.tag_search_panel,
title=Translations["tag.add.plural"],
window_title=Translations["tag.add.plural"],
)
self.tag_search_panel.tag_chosen.connect(
lambda t: (
@@ -662,10 +660,9 @@ class QtDriver(DriverMixin, QObject):
panel = FileExtensionModal(self.lib)
self.file_extension_panel = PanelModal(
panel,
Translations["ignore_list.title"],
has_save=True,
)
self.file_extension_panel.setTitle(Translations["ignore_list.title"])
self.file_extension_panel.setWindowTitle(Translations["ignore_list.title"])
self.file_extension_panel.saved.connect(
lambda: (panel.save(), self.update_browsing_state())
)
@@ -785,10 +782,10 @@ class QtDriver(DriverMixin, QObject):
panel = BuildTagPanel(self.lib)
self.modal = PanelModal(
panel,
Translations["tag.new"],
Translations["tag.add"],
has_save=True,
)
self.modal.setTitle(Translations["tag.new"])
self.modal.setWindowTitle(Translations["tag.add"])
self.modal.saved.connect(
lambda: (

View File

@@ -134,7 +134,6 @@ class ColorBoxWidget(FieldWidget):
self.edit_modal = PanelModal(
build_color_panel,
"Edit Color",
"Edit Color",
has_save=True,
)

View File

@@ -84,13 +84,12 @@ class LandingWidget(QWidget):
self.landing_layout.addWidget(self.open_button, alignment=Qt.AlignmentFlag.AlignCenter)
self.landing_layout.addWidget(self.status_label, alignment=Qt.AlignmentFlag.AlignCenter)
def update_logo_color(self, style: str = "mono"):
def update_logo_color(self, style: typing.Literal["mono", "gradient"] = "mono"):
"""Update the color of the TagStudio logo.
Args:
style (str): = The style of the logo. Either "mono" or "gradient".
"""
logo_im: Image.Image = None
if style == "mono":
logo_im = theme_fg_overlay(self.logo_raw)
elif style == "gradient":

View File

@@ -25,9 +25,9 @@ class PanelModal(QWidget):
self,
widget: "PanelWidget",
title: str = "",
window_title: str = "",
done_callback: Callable | None = None,
save_callback: Callable | None = None,
window_title: str | None = None,
done_callback: Callable[[], None] | None = None,
save_callback: Callable[[str], None] | None = None,
has_save: bool = False,
):
# [Done]
@@ -35,7 +35,7 @@ class PanelModal(QWidget):
# [Cancel] [Save]
super().__init__()
self.widget = widget
self.setWindowTitle(window_title)
self.setWindowTitle(title if window_title is None else window_title)
self.setWindowModality(Qt.WindowModality.ApplicationModal)
self.root_layout = QVBoxLayout(self)
self.root_layout.setContentsMargins(6, 0, 6, 6)
@@ -44,7 +44,7 @@ class PanelModal(QWidget):
self.title_widget.setObjectName("fieldTitle")
self.title_widget.setWordWrap(True)
self.title_widget.setStyleSheet("font-weight:bold;font-size:14px;padding-top: 6px")
self.setTitle(title)
self.title_widget.setText(title)
self.title_widget.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.button_container = QWidget()
@@ -89,7 +89,8 @@ class PanelModal(QWidget):
# trigger save button actions when pressing enter in the widget
self.widget.add_callback(lambda: self.save_button.click())
widget.done.connect(lambda: save_callback(widget.get_content()))
if save_callback is not None:
widget.done.connect(lambda: save_callback(widget.get_content()))
self.root_layout.addWidget(self.title_widget)
self.root_layout.addWidget(widget)
@@ -98,22 +99,20 @@ class PanelModal(QWidget):
self.root_layout.addWidget(self.button_container)
widget.parent_post_init()
def closeEvent(self, event): # noqa: N802
@override
def closeEvent(self, event: QtGui.QCloseEvent) -> None:
if self.cancel_button:
self.cancel_button.click()
elif self.done_button:
self.done_button.click()
event.accept()
def setTitle(self, title: str): # noqa: N802
self.title_widget.setText(title)
class PanelWidget(QWidget):
"""Used for widgets that go in a modal panel, ex. for editing or searching."""
done = Signal()
parent_modal: PanelModal = None
parent_modal: PanelModal | None = None
panel_save_button: QPushButton | None = None
panel_cancel_button: QPushButton | None = None
panel_done_button: QPushButton | None = None
@@ -122,19 +121,19 @@ class PanelWidget(QWidget):
super().__init__()
def get_content(self) -> str:
return ""
def reset(self) -> None:
pass
def reset(self):
def parent_post_init(self) -> None:
pass
def parent_post_init(self):
pass
def add_callback(self, callback: Callable, event: str = "returnPressed"):
def add_callback(self, callback: Callable[[], None], event: str = "returnPressed"):
logger.warning(f"[PanelModal] add_callback not implemented for {self.__class__.__name__}")
@override
def keyPressEvent(self, event: QtGui.QKeyEvent) -> None: # noqa N802
def keyPressEvent(self, event: QtGui.QKeyEvent) -> None:
if event.key() == QtCore.Qt.Key.Key_Escape:
if self.panel_cancel_button:
self.panel_cancel_button.click()
@@ -146,5 +145,4 @@ class PanelWidget(QWidget):
elif self.panel_done_button:
self.panel_done_button.click()
else: # Other key presses
pass
return super().keyPressEvent(event)
super().keyPressEvent(event)

View File

@@ -408,7 +408,6 @@ class FieldContainers(QWidget):
modal = PanelModal(
DatetimePicker(self.driver, field.value or dt.now()),
title=f"Edit {field.type.name}",
window_title=f"Edit {field.type.name}",
save_callback=(
lambda content: (
self.update_field(field, content), # type: ignore

View File

@@ -71,7 +71,6 @@ class PreviewPanel(QWidget):
self.tag_search_panel = TagSearchPanel(self.driver.lib, is_tag_chooser=True)
self.add_tag_modal = PanelModal(self.tag_search_panel, Translations["tag.add.plural"])
self.add_tag_modal.setWindowTitle(Translations["tag.add.plural"])
self.add_field_modal = AddFieldModal(self.lib)