Skip to content

Commit

Permalink
fix: resouce leak in translate_with_setter by deleting the offendin…
Browse files Browse the repository at this point in the history
…g code (#817)

* feat: implement first class for translated widget

* feat: add further translated widget types

* refactor: replace usages of translate_qobject with appropriate classes

* refactor: remove wrapper classes

* refactor: remove unnecessary used of Translations.formatted

* refactor: remove further unnecessary format calls

* refactor: replace calls for Translations.formatted with calls to str.format and remove some unused code

* refactor: remove TranslatedString class

* refactor: replace translate_with_setter with direct calls to the setter
  • Loading branch information
Computerdores authored Mar 3, 2025
1 parent 152c2b2 commit 2a78759
Show file tree
Hide file tree
Showing 37 changed files with 287 additions and 550 deletions.
10 changes: 3 additions & 7 deletions tagstudio/src/core/library/alchemy/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,9 @@ def open_sqlite_library(self, library_dir: Path, is_new: bool) -> LibraryStatus:
db_version = db_result.value # type: ignore

if db_version < 6: # NOTE: DB_VERSION 6 is the first supported SQL DB version.
mismatch_text = Translations.translate_formatted(
"status.library_version_mismatch"
)
found_text = Translations.translate_formatted("status.library_version_found")
expected_text = Translations.translate_formatted(
"status.library_version_expected"
)
mismatch_text = Translations["status.library_version_mismatch"]
found_text = Translations["status.library_version_found"]
expected_text = Translations["status.library_version_expected"]
return LibraryStatus(
success=False,
message=(
Expand Down
7 changes: 3 additions & 4 deletions tagstudio/src/qt/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def setupUi(self, MainWindow):
# Thumbnail Size placeholder
self.thumb_size_combobox = QComboBox(self.centralwidget)
self.thumb_size_combobox.setObjectName(u"thumbSizeComboBox")
Translations.translate_with_setter(self.thumb_size_combobox.setPlaceholderText, "home.thumbnail_size")
self.thumb_size_combobox.setPlaceholderText(Translations["home.thumbnail_size"])
self.thumb_size_combobox.setCurrentText("")
sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
Expand Down Expand Up @@ -142,7 +142,7 @@ def setupUi(self, MainWindow):
self.horizontalLayout_2.addWidget(self.forwardButton)

self.searchField = QLineEdit(self.centralwidget)
Translations.translate_with_setter(self.searchField.setPlaceholderText, "home.search_entries")
self.searchField.setPlaceholderText(Translations["home.search_entries"])
self.searchField.setObjectName(u"searchField")
self.searchField.setMinimumSize(QSize(0, 32))

Expand All @@ -152,8 +152,7 @@ def setupUi(self, MainWindow):
self.searchField.setCompleter(self.searchFieldCompleter)
self.horizontalLayout_2.addWidget(self.searchField)

self.searchButton = QPushButton(self.centralwidget)
Translations.translate_qobject(self.searchButton, "home.search")
self.searchButton = QPushButton(Translations["home.search"], self.centralwidget)
self.searchButton.setObjectName(u"searchButton")
self.searchButton.setMinimumSize(QSize(0, 32))

Expand Down
36 changes: 14 additions & 22 deletions tagstudio/src/qt/modals/about.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@
from PIL import ImageQt
from PySide6.QtCore import Qt
from PySide6.QtGui import QPixmap
from PySide6.QtWidgets import (
QHBoxLayout,
QLabel,
QPushButton,
QVBoxLayout,
QWidget,
)
from PySide6.QtWidgets import QHBoxLayout, QLabel, QPushButton, QVBoxLayout, QWidget
from src.core.constants import VERSION, VERSION_BRANCH
from src.qt.modals.ffmpeg_checker import FfmpegChecker
from src.qt.resource_manager import ResourceManager
Expand All @@ -22,7 +16,7 @@
class AboutModal(QWidget):
def __init__(self, config_path):
super().__init__()
Translations.translate_with_setter(self.setWindowTitle, "about.title")
self.setWindowTitle(Translations["about.title"])

self.fc: FfmpegChecker = FfmpegChecker()
self.rm: ResourceManager = ResourceManager()
Expand All @@ -42,34 +36,32 @@ def __init__(self, config_path):
self.logo_widget.setAlignment(Qt.AlignmentFlag.AlignHCenter)
self.logo_widget.setContentsMargins(0, 0, 0, 20)

self.content_widget = QLabel()
self.content_widget.setObjectName("contentLabel")
self.content_widget.setWordWrap(True)
self.content_widget.setOpenExternalLinks(True)
ff_version = self.fc.version()
ffmpeg = '<span style="color:red">Missing</span>'
if ff_version["ffmpeg"] is not None:
ffmpeg = '<span style="color:green">Found</span> (' + ff_version["ffmpeg"] + ")"
ffprobe = '<span style="color:red">Missing</span>'
if ff_version["ffprobe"] is not None:
ffprobe = '<span style="color:green">Found</span> (' + ff_version["ffprobe"] + ")"
Translations.translate_qobject(
self.content_widget,
"about.content",
version=VERSION,
branch=VERSION_BRANCH,
config_path=config_path,
ffmpeg=ffmpeg,
ffprobe=ffprobe,
self.content_widget = QLabel(
Translations["about.content"].format(
version=VERSION,
branch=VERSION_BRANCH,
config_path=config_path,
ffmpeg=ffmpeg,
ffprobe=ffprobe,
)
)
self.content_widget.setObjectName("contentLabel")
self.content_widget.setWordWrap(True)
self.content_widget.setOpenExternalLinks(True)
self.content_widget.setAlignment(Qt.AlignmentFlag.AlignHCenter)

self.button_widget = QWidget()
self.button_layout = QHBoxLayout(self.button_widget)
self.button_layout.addStretch(1)

self.close_button = QPushButton()
Translations.translate_qobject(self.close_button, "generic.close")
self.close_button = QPushButton(Translations["generic.close"])
self.close_button.clicked.connect(lambda: self.close())
self.close_button.setMaximumWidth(80)

Expand Down
11 changes: 4 additions & 7 deletions tagstudio/src/qt/modals/add_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,16 @@ def __init__(self, library: Library):
# [Cancel] [Save]
super().__init__()
self.lib = library
Translations.translate_with_setter(self.setWindowTitle, "library.field.add")
self.setWindowTitle(Translations["library.field.add"])
self.setWindowModality(Qt.WindowModality.ApplicationModal)
self.setMinimumSize(400, 300)
self.root_layout = QVBoxLayout(self)
self.root_layout.setContentsMargins(6, 6, 6, 6)

self.title_widget = QLabel()
self.title_widget = QLabel(Translations["library.field.add"])
self.title_widget.setObjectName("fieldTitle")
self.title_widget.setWordWrap(True)
self.title_widget.setStyleSheet("font-weight:bold;" "font-size:14px;" "padding-top: 6px;")
Translations.translate_qobject(self.title_widget, "library.field.add")
self.title_widget.setAlignment(Qt.AlignmentFlag.AlignCenter)

self.list_widget = QListWidget()
Expand All @@ -51,13 +50,11 @@ def __init__(self, library: Library):
self.button_layout.setContentsMargins(6, 6, 6, 6)
self.button_layout.addStretch(1)

self.cancel_button = QPushButton()
Translations.translate_qobject(self.cancel_button, "generic.cancel")
self.cancel_button = QPushButton(Translations["generic.cancel"])
self.cancel_button.clicked.connect(self.hide)
self.button_layout.addWidget(self.cancel_button)

self.save_button = QPushButton()
Translations.translate_qobject(self.save_button, "generic.add")
self.save_button = QPushButton(Translations["generic.add"])
self.save_button.setDefault(True)
self.save_button.clicked.connect(self.hide)
self.save_button.clicked.connect(
Expand Down
26 changes: 8 additions & 18 deletions tagstudio/src/qt/modals/build_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,23 @@ def __init__(self, library: Library, color_group: TagColorGroup):
self.preview_layout.addWidget(self.preview_button)

# Name -----------------------------------------------------------------
self.name_title = QLabel()
Translations.translate_qobject(self.name_title, "library_object.name")
self.name_title = QLabel(Translations["library_object.name"])
self.name_field = QLineEdit()
self.name_field.setFixedHeight(24)
self.name_field.textChanged.connect(self.on_text_changed)
Translations.translate_with_setter(
self.name_field.setPlaceholderText, "library_object.name_required"
)
self.name_field.setPlaceholderText(Translations["library_object.name_required"])
self.form_layout.addRow(self.name_title, self.name_field)

# Slug -----------------------------------------------------------------
self.slug_title = QLabel()
Translations.translate_qobject(self.slug_title, "library_object.slug")
self.slug_title = QLabel(Translations["library_object.slug"])
self.slug_field = QLineEdit()
self.slug_field.setEnabled(False)
self.slug_field.setFixedHeight(24)
Translations.translate_with_setter(
self.slug_field.setPlaceholderText, "library_object.slug_required"
)
self.slug_field.setPlaceholderText(Translations["library_object.slug_required"])
self.form_layout.addRow(self.slug_title, self.slug_field)

# Primary --------------------------------------------------------------
self.primary_title = QLabel()
Translations.translate_qobject(self.primary_title, "color.primary")
self.primary_title = QLabel(Translations["color.primary"])
self.primary_button = QPushButton()
self.primary_button.setMinimumSize(44, 22)
self.primary_button.setMaximumHeight(22)
Expand All @@ -108,17 +101,15 @@ def __init__(self, library: Library, color_group: TagColorGroup):
self.secondary_layout = QHBoxLayout(self.secondary_widget)
self.secondary_layout.setContentsMargins(0, 0, 0, 0)
self.secondary_layout.setSpacing(6)
self.secondary_title = QLabel()
Translations.translate_qobject(self.secondary_title, "color.secondary")
self.secondary_title = QLabel(Translations["color.secondary"])
self.secondary_button = QPushButton()
self.secondary_button.setMinimumSize(44, 22)
self.secondary_button.setMaximumHeight(22)
self.edit_secondary_modal = QColorDialog()
self.secondary_button.clicked.connect(self.secondary_color_callback)
self.secondary_layout.addWidget(self.secondary_button)

self.secondary_reset_button = QPushButton()
Translations.translate_qobject(self.secondary_reset_button, "generic.reset")
self.secondary_reset_button = QPushButton(Translations["generic.reset"])
self.secondary_reset_button.clicked.connect(self.update_secondary)
self.secondary_layout.addWidget(self.secondary_reset_button)
self.secondary_layout.setStretch(0, 3)
Expand All @@ -143,8 +134,7 @@ def __init__(self, library: Library, color_group: TagColorGroup):
)
)
self.border_layout.addWidget(self.border_checkbox)
self.border_label = QLabel()
Translations.translate_qobject(self.border_label, "color.color_border")
self.border_label = QLabel(Translations["color.color_border"])
self.border_layout.addWidget(self.border_label)

primary_color = QColor(get_tag_color(ColorType.PRIMARY, TagColorEnum.DEFAULT))
Expand Down
22 changes: 6 additions & 16 deletions tagstudio/src/qt/modals/build_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,12 @@ def __init__(self, library: Library, namespace: Namespace | None = None):
self.name_layout.setContentsMargins(0, 0, 0, 0)
self.name_layout.setSpacing(0)
self.name_layout.setAlignment(Qt.AlignmentFlag.AlignLeft)
self.name_title = QLabel()
Translations.translate_qobject(self.name_title, "library_object.name")
self.name_title = QLabel(Translations["library_object.name"])
self.name_layout.addWidget(self.name_title)
self.name_field = QLineEdit()
self.name_field.setFixedHeight(24)
self.name_field.textChanged.connect(self.on_text_changed)
Translations.translate_with_setter(
self.name_field.setPlaceholderText, "library_object.name_required"
)
self.name_field.setPlaceholderText(Translations["library_object.name_required"])
self.name_layout.addWidget(self.name_field)

# Slug -----------------------------------------------------------------
Expand All @@ -66,26 +63,19 @@ def __init__(self, library: Library, namespace: Namespace | None = None):
self.slug_layout.setContentsMargins(0, 0, 0, 0)
self.slug_layout.setSpacing(0)
self.slug_layout.setAlignment(Qt.AlignmentFlag.AlignLeft)
self.slug_title = QLabel()
Translations.translate_qobject(self.slug_title, "library_object.slug")
self.slug_title = QLabel(Translations["library_object.slug"])
self.slug_layout.addWidget(self.slug_title)
self.slug_field = QLineEdit()
self.slug_field.setFixedHeight(24)
self.slug_field.setEnabled(False)
Translations.translate_with_setter(
self.slug_field.setPlaceholderText, "library_object.slug_required"
)
self.slug_field.setPlaceholderText(Translations["library_object.slug_required"])
self.slug_layout.addWidget(self.slug_field)

# Description ----------------------------------------------------------
self.desc_label = QLabel()
self.desc_label = QLabel(Translations["namespace.create.description"])
self.desc_label.setWordWrap(True)
Translations.translate_with_setter(self.desc_label.setText, "namespace.create.description")
self.desc_color_label = QLabel()
self.desc_color_label = QLabel(Translations["namespace.create.description_color"])
self.desc_color_label.setWordWrap(True)
Translations.translate_with_setter(
self.desc_color_label.setText, "namespace.create.description_color"
)

# Add Widgets to Layout ================================================
self.root_layout.addWidget(self.name_widget)
Expand Down
30 changes: 11 additions & 19 deletions tagstudio/src/qt/modals/build_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,12 @@ def __init__(self, library: Library, tag: Tag | None = None):
self.name_layout.setContentsMargins(0, 0, 0, 0)
self.name_layout.setSpacing(0)
self.name_layout.setAlignment(Qt.AlignmentFlag.AlignLeft)
self.name_title = QLabel()
Translations.translate_qobject(self.name_title, "tag.name")
self.name_title = QLabel(Translations["tag.name"])
self.name_layout.addWidget(self.name_title)
self.name_field = QLineEdit()
self.name_field.setFixedHeight(24)
self.name_field.textChanged.connect(self.on_name_changed)
Translations.translate_with_setter(
self.name_field.setPlaceholderText, "tag.tag_name_required"
)
self.name_field.setPlaceholderText(Translations["tag.tag_name_required"])
self.name_layout.addWidget(self.name_field)

# Shorthand ------------------------------------------------------------
Expand All @@ -104,8 +101,7 @@ def __init__(self, library: Library, tag: Tag | None = None):
self.shorthand_layout.setContentsMargins(0, 0, 0, 0)
self.shorthand_layout.setSpacing(0)
self.shorthand_layout.setAlignment(Qt.AlignmentFlag.AlignLeft)
self.shorthand_title = QLabel()
Translations.translate_qobject(self.shorthand_title, "tag.shorthand")
self.shorthand_title = QLabel(Translations["tag.shorthand"])
self.shorthand_layout.addWidget(self.shorthand_title)
self.shorthand_field = QLineEdit()
self.shorthand_layout.addWidget(self.shorthand_field)
Expand All @@ -117,8 +113,7 @@ def __init__(self, library: Library, tag: Tag | None = None):
self.aliases_layout.setContentsMargins(0, 0, 0, 0)
self.aliases_layout.setSpacing(0)
self.aliases_layout.setAlignment(Qt.AlignmentFlag.AlignLeft)
self.aliases_title = QLabel()
Translations.translate_qobject(self.aliases_title, "tag.aliases")
self.aliases_title = QLabel(Translations["tag.aliases"])
self.aliases_layout.addWidget(self.aliases_title)

self.aliases_table = QTableWidget(0, 2)
Expand All @@ -144,8 +139,7 @@ def __init__(self, library: Library, tag: Tag | None = None):
self.disam_button_group = QButtonGroup(self)
self.disam_button_group.setExclusive(False)

self.parent_tags_title = QLabel()
Translations.translate_qobject(self.parent_tags_title, "tag.parent_tags")
self.parent_tags_title = QLabel(Translations["tag.parent_tags"])
self.parent_tags_layout.addWidget(self.parent_tags_title)

self.scroll_contents = QWidget()
Expand Down Expand Up @@ -173,8 +167,8 @@ def __init__(self, library: Library, tag: Tag | None = None):
tsp = TagSearchPanel(self.lib, exclude_ids)
tsp.tag_chosen.connect(lambda x: self.add_parent_tag_callback(x))
self.add_tag_modal = PanelModal(tsp)
Translations.translate_with_setter(self.add_tag_modal.setTitle, "tag.parent_tags.add")
Translations.translate_with_setter(self.add_tag_modal.setWindowTitle, "tag.parent_tags.add")
self.add_tag_modal.setTitle(Translations["tag.parent_tags.add"])
self.add_tag_modal.setWindowTitle(Translations["tag.parent_tags.add"])
self.parent_tags_add_button.clicked.connect(self.add_tag_modal.show)

# Color ----------------------------------------------------------------
Expand All @@ -184,8 +178,7 @@ def __init__(self, library: Library, tag: Tag | None = None):
self.color_layout.setContentsMargins(0, 0, 0, 6)
self.color_layout.setSpacing(6)
self.color_layout.setAlignment(Qt.AlignmentFlag.AlignLeft)
self.color_title = QLabel()
Translations.translate_qobject(self.color_title, "tag.color")
self.color_title = QLabel(Translations["tag.color"])
self.color_layout.addWidget(self.color_title)
self.color_button: TagColorPreview
try:
Expand All @@ -195,7 +188,7 @@ def __init__(self, library: Library, tag: Tag | None = None):
logger.error("[BuildTag] Could not access Tag member attributes", error=e)
self.color_button = TagColorPreview(self.lib, None)
self.tag_color_selection = TagColorSelection(self.lib)
chose_tag_color_title = Translations.translate_formatted("tag.choose_color")
chose_tag_color_title = Translations["tag.choose_color"]
self.choose_color_modal = PanelModal(
self.tag_color_selection,
chose_tag_color_title,
Expand All @@ -214,8 +207,7 @@ def __init__(self, library: Library, tag: Tag | None = None):
self.cat_layout.setContentsMargins(0, 0, 0, 0)
self.cat_layout.setSpacing(6)
self.cat_layout.setAlignment(Qt.AlignmentFlag.AlignLeft)
self.cat_title = QLabel()
Translations.translate_qobject(self.cat_title, "tag.is_category")
self.cat_title = QLabel(Translations["tag.is_category"])
self.cat_checkbox = QCheckBox()
self.cat_checkbox.setFixedSize(22, 22)

Expand Down Expand Up @@ -404,7 +396,7 @@ def __build_row_item_widget(self, tag: Tag, parent_id: int, is_disambiguation: b
disam_button = QRadioButton()
disam_button.setObjectName(f"disambiguationButton.{parent_id}")
disam_button.setFixedSize(22, 22)
disam_button.setToolTip(Translations.translate_formatted("tag.disambiguation.tooltip"))
disam_button.setToolTip(Translations["tag.disambiguation.tooltip"])
disam_button.setStyleSheet(
f"QRadioButton{{"
f"background: rgba{primary_color.toTuple()};"
Expand Down
Loading

0 comments on commit 2a78759

Please sign in to comment.