Skip to content

Commit 2a78759

Browse files
fix: resouce leak in translate_with_setter by deleting the offending 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
1 parent 152c2b2 commit 2a78759

37 files changed

+287
-550
lines changed

tagstudio/src/core/library/alchemy/library.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,9 @@ def open_sqlite_library(self, library_dir: Path, is_new: bool) -> LibraryStatus:
368368
db_version = db_result.value # type: ignore
369369

370370
if db_version < 6: # NOTE: DB_VERSION 6 is the first supported SQL DB version.
371-
mismatch_text = Translations.translate_formatted(
372-
"status.library_version_mismatch"
373-
)
374-
found_text = Translations.translate_formatted("status.library_version_found")
375-
expected_text = Translations.translate_formatted(
376-
"status.library_version_expected"
377-
)
371+
mismatch_text = Translations["status.library_version_mismatch"]
372+
found_text = Translations["status.library_version_found"]
373+
expected_text = Translations["status.library_version_expected"]
378374
return LibraryStatus(
379375
success=False,
380376
message=(

tagstudio/src/qt/main_window.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def setupUi(self, MainWindow):
7474
# Thumbnail Size placeholder
7575
self.thumb_size_combobox = QComboBox(self.centralwidget)
7676
self.thumb_size_combobox.setObjectName(u"thumbSizeComboBox")
77-
Translations.translate_with_setter(self.thumb_size_combobox.setPlaceholderText, "home.thumbnail_size")
77+
self.thumb_size_combobox.setPlaceholderText(Translations["home.thumbnail_size"])
7878
self.thumb_size_combobox.setCurrentText("")
7979
sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
8080
sizePolicy.setHorizontalStretch(0)
@@ -142,7 +142,7 @@ def setupUi(self, MainWindow):
142142
self.horizontalLayout_2.addWidget(self.forwardButton)
143143

144144
self.searchField = QLineEdit(self.centralwidget)
145-
Translations.translate_with_setter(self.searchField.setPlaceholderText, "home.search_entries")
145+
self.searchField.setPlaceholderText(Translations["home.search_entries"])
146146
self.searchField.setObjectName(u"searchField")
147147
self.searchField.setMinimumSize(QSize(0, 32))
148148

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

155-
self.searchButton = QPushButton(self.centralwidget)
156-
Translations.translate_qobject(self.searchButton, "home.search")
155+
self.searchButton = QPushButton(Translations["home.search"], self.centralwidget)
157156
self.searchButton.setObjectName(u"searchButton")
158157
self.searchButton.setMinimumSize(QSize(0, 32))
159158

tagstudio/src/qt/modals/about.py

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@
66
from PIL import ImageQt
77
from PySide6.QtCore import Qt
88
from PySide6.QtGui import QPixmap
9-
from PySide6.QtWidgets import (
10-
QHBoxLayout,
11-
QLabel,
12-
QPushButton,
13-
QVBoxLayout,
14-
QWidget,
15-
)
9+
from PySide6.QtWidgets import QHBoxLayout, QLabel, QPushButton, QVBoxLayout, QWidget
1610
from src.core.constants import VERSION, VERSION_BRANCH
1711
from src.qt.modals.ffmpeg_checker import FfmpegChecker
1812
from src.qt.resource_manager import ResourceManager
@@ -22,7 +16,7 @@
2216
class AboutModal(QWidget):
2317
def __init__(self, config_path):
2418
super().__init__()
25-
Translations.translate_with_setter(self.setWindowTitle, "about.title")
19+
self.setWindowTitle(Translations["about.title"])
2620

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

45-
self.content_widget = QLabel()
46-
self.content_widget.setObjectName("contentLabel")
47-
self.content_widget.setWordWrap(True)
48-
self.content_widget.setOpenExternalLinks(True)
4939
ff_version = self.fc.version()
5040
ffmpeg = '<span style="color:red">Missing</span>'
5141
if ff_version["ffmpeg"] is not None:
5242
ffmpeg = '<span style="color:green">Found</span> (' + ff_version["ffmpeg"] + ")"
5343
ffprobe = '<span style="color:red">Missing</span>'
5444
if ff_version["ffprobe"] is not None:
5545
ffprobe = '<span style="color:green">Found</span> (' + ff_version["ffprobe"] + ")"
56-
Translations.translate_qobject(
57-
self.content_widget,
58-
"about.content",
59-
version=VERSION,
60-
branch=VERSION_BRANCH,
61-
config_path=config_path,
62-
ffmpeg=ffmpeg,
63-
ffprobe=ffprobe,
46+
self.content_widget = QLabel(
47+
Translations["about.content"].format(
48+
version=VERSION,
49+
branch=VERSION_BRANCH,
50+
config_path=config_path,
51+
ffmpeg=ffmpeg,
52+
ffprobe=ffprobe,
53+
)
6454
)
55+
self.content_widget.setObjectName("contentLabel")
56+
self.content_widget.setWordWrap(True)
57+
self.content_widget.setOpenExternalLinks(True)
6558
self.content_widget.setAlignment(Qt.AlignmentFlag.AlignHCenter)
6659

6760
self.button_widget = QWidget()
6861
self.button_layout = QHBoxLayout(self.button_widget)
6962
self.button_layout.addStretch(1)
7063

71-
self.close_button = QPushButton()
72-
Translations.translate_qobject(self.close_button, "generic.close")
64+
self.close_button = QPushButton(Translations["generic.close"])
7365
self.close_button.clicked.connect(lambda: self.close())
7466
self.close_button.setMaximumWidth(80)
7567

tagstudio/src/qt/modals/add_field.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,16 @@ def __init__(self, library: Library):
3131
# [Cancel] [Save]
3232
super().__init__()
3333
self.lib = library
34-
Translations.translate_with_setter(self.setWindowTitle, "library.field.add")
34+
self.setWindowTitle(Translations["library.field.add"])
3535
self.setWindowModality(Qt.WindowModality.ApplicationModal)
3636
self.setMinimumSize(400, 300)
3737
self.root_layout = QVBoxLayout(self)
3838
self.root_layout.setContentsMargins(6, 6, 6, 6)
3939

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

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

54-
self.cancel_button = QPushButton()
55-
Translations.translate_qobject(self.cancel_button, "generic.cancel")
53+
self.cancel_button = QPushButton(Translations["generic.cancel"])
5654
self.cancel_button.clicked.connect(self.hide)
5755
self.button_layout.addWidget(self.cancel_button)
5856

59-
self.save_button = QPushButton()
60-
Translations.translate_qobject(self.save_button, "generic.add")
57+
self.save_button = QPushButton(Translations["generic.add"])
6158
self.save_button.setDefault(True)
6259
self.save_button.clicked.connect(self.hide)
6360
self.save_button.clicked.connect(

tagstudio/src/qt/modals/build_color.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,23 @@ def __init__(self, library: Library, color_group: TagColorGroup):
7272
self.preview_layout.addWidget(self.preview_button)
7373

7474
# Name -----------------------------------------------------------------
75-
self.name_title = QLabel()
76-
Translations.translate_qobject(self.name_title, "library_object.name")
75+
self.name_title = QLabel(Translations["library_object.name"])
7776
self.name_field = QLineEdit()
7877
self.name_field.setFixedHeight(24)
7978
self.name_field.textChanged.connect(self.on_text_changed)
80-
Translations.translate_with_setter(
81-
self.name_field.setPlaceholderText, "library_object.name_required"
82-
)
79+
self.name_field.setPlaceholderText(Translations["library_object.name_required"])
8380
self.form_layout.addRow(self.name_title, self.name_field)
8481

8582
# Slug -----------------------------------------------------------------
86-
self.slug_title = QLabel()
87-
Translations.translate_qobject(self.slug_title, "library_object.slug")
83+
self.slug_title = QLabel(Translations["library_object.slug"])
8884
self.slug_field = QLineEdit()
8985
self.slug_field.setEnabled(False)
9086
self.slug_field.setFixedHeight(24)
91-
Translations.translate_with_setter(
92-
self.slug_field.setPlaceholderText, "library_object.slug_required"
93-
)
87+
self.slug_field.setPlaceholderText(Translations["library_object.slug_required"])
9488
self.form_layout.addRow(self.slug_title, self.slug_field)
9589

9690
# Primary --------------------------------------------------------------
97-
self.primary_title = QLabel()
98-
Translations.translate_qobject(self.primary_title, "color.primary")
91+
self.primary_title = QLabel(Translations["color.primary"])
9992
self.primary_button = QPushButton()
10093
self.primary_button.setMinimumSize(44, 22)
10194
self.primary_button.setMaximumHeight(22)
@@ -108,17 +101,15 @@ def __init__(self, library: Library, color_group: TagColorGroup):
108101
self.secondary_layout = QHBoxLayout(self.secondary_widget)
109102
self.secondary_layout.setContentsMargins(0, 0, 0, 0)
110103
self.secondary_layout.setSpacing(6)
111-
self.secondary_title = QLabel()
112-
Translations.translate_qobject(self.secondary_title, "color.secondary")
104+
self.secondary_title = QLabel(Translations["color.secondary"])
113105
self.secondary_button = QPushButton()
114106
self.secondary_button.setMinimumSize(44, 22)
115107
self.secondary_button.setMaximumHeight(22)
116108
self.edit_secondary_modal = QColorDialog()
117109
self.secondary_button.clicked.connect(self.secondary_color_callback)
118110
self.secondary_layout.addWidget(self.secondary_button)
119111

120-
self.secondary_reset_button = QPushButton()
121-
Translations.translate_qobject(self.secondary_reset_button, "generic.reset")
112+
self.secondary_reset_button = QPushButton(Translations["generic.reset"])
122113
self.secondary_reset_button.clicked.connect(self.update_secondary)
123114
self.secondary_layout.addWidget(self.secondary_reset_button)
124115
self.secondary_layout.setStretch(0, 3)
@@ -143,8 +134,7 @@ def __init__(self, library: Library, color_group: TagColorGroup):
143134
)
144135
)
145136
self.border_layout.addWidget(self.border_checkbox)
146-
self.border_label = QLabel()
147-
Translations.translate_qobject(self.border_label, "color.color_border")
137+
self.border_label = QLabel(Translations["color.color_border"])
148138
self.border_layout.addWidget(self.border_label)
149139

150140
primary_color = QColor(get_tag_color(ColorType.PRIMARY, TagColorEnum.DEFAULT))

tagstudio/src/qt/modals/build_namespace.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,12 @@ def __init__(self, library: Library, namespace: Namespace | None = None):
4848
self.name_layout.setContentsMargins(0, 0, 0, 0)
4949
self.name_layout.setSpacing(0)
5050
self.name_layout.setAlignment(Qt.AlignmentFlag.AlignLeft)
51-
self.name_title = QLabel()
52-
Translations.translate_qobject(self.name_title, "library_object.name")
51+
self.name_title = QLabel(Translations["library_object.name"])
5352
self.name_layout.addWidget(self.name_title)
5453
self.name_field = QLineEdit()
5554
self.name_field.setFixedHeight(24)
5655
self.name_field.textChanged.connect(self.on_text_changed)
57-
Translations.translate_with_setter(
58-
self.name_field.setPlaceholderText, "library_object.name_required"
59-
)
56+
self.name_field.setPlaceholderText(Translations["library_object.name_required"])
6057
self.name_layout.addWidget(self.name_field)
6158

6259
# Slug -----------------------------------------------------------------
@@ -66,26 +63,19 @@ def __init__(self, library: Library, namespace: Namespace | None = None):
6663
self.slug_layout.setContentsMargins(0, 0, 0, 0)
6764
self.slug_layout.setSpacing(0)
6865
self.slug_layout.setAlignment(Qt.AlignmentFlag.AlignLeft)
69-
self.slug_title = QLabel()
70-
Translations.translate_qobject(self.slug_title, "library_object.slug")
66+
self.slug_title = QLabel(Translations["library_object.slug"])
7167
self.slug_layout.addWidget(self.slug_title)
7268
self.slug_field = QLineEdit()
7369
self.slug_field.setFixedHeight(24)
7470
self.slug_field.setEnabled(False)
75-
Translations.translate_with_setter(
76-
self.slug_field.setPlaceholderText, "library_object.slug_required"
77-
)
71+
self.slug_field.setPlaceholderText(Translations["library_object.slug_required"])
7872
self.slug_layout.addWidget(self.slug_field)
7973

8074
# Description ----------------------------------------------------------
81-
self.desc_label = QLabel()
75+
self.desc_label = QLabel(Translations["namespace.create.description"])
8276
self.desc_label.setWordWrap(True)
83-
Translations.translate_with_setter(self.desc_label.setText, "namespace.create.description")
84-
self.desc_color_label = QLabel()
77+
self.desc_color_label = QLabel(Translations["namespace.create.description_color"])
8578
self.desc_color_label.setWordWrap(True)
86-
Translations.translate_with_setter(
87-
self.desc_color_label.setText, "namespace.create.description_color"
88-
)
8979

9080
# Add Widgets to Layout ================================================
9181
self.root_layout.addWidget(self.name_widget)

tagstudio/src/qt/modals/build_tag.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,12 @@ def __init__(self, library: Library, tag: Tag | None = None):
8686
self.name_layout.setContentsMargins(0, 0, 0, 0)
8787
self.name_layout.setSpacing(0)
8888
self.name_layout.setAlignment(Qt.AlignmentFlag.AlignLeft)
89-
self.name_title = QLabel()
90-
Translations.translate_qobject(self.name_title, "tag.name")
89+
self.name_title = QLabel(Translations["tag.name"])
9190
self.name_layout.addWidget(self.name_title)
9291
self.name_field = QLineEdit()
9392
self.name_field.setFixedHeight(24)
9493
self.name_field.textChanged.connect(self.on_name_changed)
95-
Translations.translate_with_setter(
96-
self.name_field.setPlaceholderText, "tag.tag_name_required"
97-
)
94+
self.name_field.setPlaceholderText(Translations["tag.tag_name_required"])
9895
self.name_layout.addWidget(self.name_field)
9996

10097
# Shorthand ------------------------------------------------------------
@@ -104,8 +101,7 @@ def __init__(self, library: Library, tag: Tag | None = None):
104101
self.shorthand_layout.setContentsMargins(0, 0, 0, 0)
105102
self.shorthand_layout.setSpacing(0)
106103
self.shorthand_layout.setAlignment(Qt.AlignmentFlag.AlignLeft)
107-
self.shorthand_title = QLabel()
108-
Translations.translate_qobject(self.shorthand_title, "tag.shorthand")
104+
self.shorthand_title = QLabel(Translations["tag.shorthand"])
109105
self.shorthand_layout.addWidget(self.shorthand_title)
110106
self.shorthand_field = QLineEdit()
111107
self.shorthand_layout.addWidget(self.shorthand_field)
@@ -117,8 +113,7 @@ def __init__(self, library: Library, tag: Tag | None = None):
117113
self.aliases_layout.setContentsMargins(0, 0, 0, 0)
118114
self.aliases_layout.setSpacing(0)
119115
self.aliases_layout.setAlignment(Qt.AlignmentFlag.AlignLeft)
120-
self.aliases_title = QLabel()
121-
Translations.translate_qobject(self.aliases_title, "tag.aliases")
116+
self.aliases_title = QLabel(Translations["tag.aliases"])
122117
self.aliases_layout.addWidget(self.aliases_title)
123118

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

147-
self.parent_tags_title = QLabel()
148-
Translations.translate_qobject(self.parent_tags_title, "tag.parent_tags")
142+
self.parent_tags_title = QLabel(Translations["tag.parent_tags"])
149143
self.parent_tags_layout.addWidget(self.parent_tags_title)
150144

151145
self.scroll_contents = QWidget()
@@ -173,8 +167,8 @@ def __init__(self, library: Library, tag: Tag | None = None):
173167
tsp = TagSearchPanel(self.lib, exclude_ids)
174168
tsp.tag_chosen.connect(lambda x: self.add_parent_tag_callback(x))
175169
self.add_tag_modal = PanelModal(tsp)
176-
Translations.translate_with_setter(self.add_tag_modal.setTitle, "tag.parent_tags.add")
177-
Translations.translate_with_setter(self.add_tag_modal.setWindowTitle, "tag.parent_tags.add")
170+
self.add_tag_modal.setTitle(Translations["tag.parent_tags.add"])
171+
self.add_tag_modal.setWindowTitle(Translations["tag.parent_tags.add"])
178172
self.parent_tags_add_button.clicked.connect(self.add_tag_modal.show)
179173

180174
# Color ----------------------------------------------------------------
@@ -184,8 +178,7 @@ def __init__(self, library: Library, tag: Tag | None = None):
184178
self.color_layout.setContentsMargins(0, 0, 0, 6)
185179
self.color_layout.setSpacing(6)
186180
self.color_layout.setAlignment(Qt.AlignmentFlag.AlignLeft)
187-
self.color_title = QLabel()
188-
Translations.translate_qobject(self.color_title, "tag.color")
181+
self.color_title = QLabel(Translations["tag.color"])
189182
self.color_layout.addWidget(self.color_title)
190183
self.color_button: TagColorPreview
191184
try:
@@ -195,7 +188,7 @@ def __init__(self, library: Library, tag: Tag | None = None):
195188
logger.error("[BuildTag] Could not access Tag member attributes", error=e)
196189
self.color_button = TagColorPreview(self.lib, None)
197190
self.tag_color_selection = TagColorSelection(self.lib)
198-
chose_tag_color_title = Translations.translate_formatted("tag.choose_color")
191+
chose_tag_color_title = Translations["tag.choose_color"]
199192
self.choose_color_modal = PanelModal(
200193
self.tag_color_selection,
201194
chose_tag_color_title,
@@ -214,8 +207,7 @@ def __init__(self, library: Library, tag: Tag | None = None):
214207
self.cat_layout.setContentsMargins(0, 0, 0, 0)
215208
self.cat_layout.setSpacing(6)
216209
self.cat_layout.setAlignment(Qt.AlignmentFlag.AlignLeft)
217-
self.cat_title = QLabel()
218-
Translations.translate_qobject(self.cat_title, "tag.is_category")
210+
self.cat_title = QLabel(Translations["tag.is_category"])
219211
self.cat_checkbox = QCheckBox()
220212
self.cat_checkbox.setFixedSize(22, 22)
221213

@@ -404,7 +396,7 @@ def __build_row_item_widget(self, tag: Tag, parent_id: int, is_disambiguation: b
404396
disam_button = QRadioButton()
405397
disam_button.setObjectName(f"disambiguationButton.{parent_id}")
406398
disam_button.setFixedSize(22, 22)
407-
disam_button.setToolTip(Translations.translate_formatted("tag.disambiguation.tooltip"))
399+
disam_button.setToolTip(Translations["tag.disambiguation.tooltip"])
408400
disam_button.setStyleSheet(
409401
f"QRadioButton{{"
410402
f"background: rgba{primary_color.toTuple()};"

0 commit comments

Comments
 (0)