Skip to content

Commit 3c40c8a

Browse files
committed
add button for removing recent lib
1 parent e04a93e commit 3c40c8a

File tree

2 files changed

+62
-20
lines changed

2 files changed

+62
-20
lines changed

tagstudio/src/qt/ts_qt.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,12 @@ def filter_items(self, query=""):
13291329

13301330
# self.update_thumbs()
13311331

1332+
def remove_recent_library(self, item_key: str):
1333+
self.settings.beginGroup(SettingItems.LIBS_LIST)
1334+
self.settings.remove(item_key)
1335+
self.settings.endGroup()
1336+
self.settings.sync()
1337+
13321338
def update_libs_list(self, path: str | Path):
13331339
"""add library to list in SettingItems.LIBS_LIST"""
13341340
ITEMS_LIMIT = 5

tagstudio/src/qt/widgets/preview_panel.py

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -248,44 +248,80 @@ def fill_libs_widget(self, layout: QVBoxLayout):
248248
libs_sorted = sorted(lib_items.items(), key=lambda item: item[0], reverse=True)
249249

250250
self.render_libs = new_keys
251-
return self._fill_libs_widget(libs_sorted, layout)
251+
self._fill_libs_widget(libs_sorted, layout)
252252

253253
def _fill_libs_widget(
254254
self, libraries: list[tuple[str, tuple[str, str]]], layout: QVBoxLayout
255255
):
256+
def clear_layout(layout_item: QVBoxLayout):
257+
for i in reversed(range(layout_item.count())):
258+
child = layout_item.itemAt(i)
259+
if child.widget() is not None:
260+
child.widget().deleteLater()
261+
elif child.layout() is not None:
262+
clear_layout(child.layout())
263+
256264
# remove any potential previous items
257265
for idx in reversed(range(layout.count())):
258-
widget = layout.itemAt(idx).widget()
259-
layout.removeWidget(widget)
260-
# remove from GUI
261-
widget.setParent(None)
266+
row_layout = layout.itemAt(idx)
267+
clear_layout(row_layout)
262268

263269
label = QLabel("Recent Libraries")
264270
label.setAlignment(Qt.AlignCenter)
265-
layout.addWidget(label)
266271

267-
for tstamp, (full_val, cut_val) in libraries:
268-
button = QPushButton(text=cut_val)
269-
button.setObjectName(f"path{tstamp}")
272+
row_layout = QHBoxLayout()
273+
row_layout.addWidget(label)
274+
layout.addLayout(row_layout)
270275

271-
def open_library_button_clicked(path):
272-
return lambda: self.driver.open_library(path)
276+
def get_button_style(extras: list[str] | None = None):
277+
base_style = [
278+
f"background-color:{Theme.COLOR_BG.value};",
279+
"border-radius:6px;",
280+
"text-align: left;",
281+
"padding-top: 3px;",
282+
"padding-left: 6px;",
283+
"padding-bottom: 4px;",
284+
]
273285

274-
button.clicked.connect(open_library_button_clicked(full_val))
275-
button.setStyleSheet(
286+
full_style = base_style + (extras or [])
287+
288+
return (
276289
"QPushButton{"
277-
f"background-color:{Theme.COLOR_BG.value};"
278-
"border-radius:6px;"
279-
"text-align: left;"
280-
"padding-top: 3px;"
281-
"padding-left: 6px;"
282-
"padding-bottom: 4px;"
290+
f"{''.join(full_style)}"
283291
"}"
284292
f"QPushButton::hover{{background-color:{Theme.COLOR_HOVER.value};}}"
285293
f"QPushButton::pressed{{background-color:{Theme.COLOR_PRESSED.value};}}"
286294
)
295+
296+
for item_key, (full_val, cut_val) in libraries:
297+
button = QPushButton(text=cut_val)
298+
button.setObjectName(f"path{item_key}")
299+
300+
def open_library_button_clicked(path):
301+
return lambda: self.driver.open_library(path)
302+
303+
button.clicked.connect(open_library_button_clicked(full_val))
304+
button.setStyleSheet(get_button_style())
287305
button.setCursor(Qt.CursorShape.PointingHandCursor)
288-
layout.addWidget(button)
306+
307+
button_remove = QPushButton("➖")
308+
button_remove.setStyleSheet(get_button_style())
309+
button_remove.setCursor(Qt.CursorShape.PointingHandCursor)
310+
button_remove.setFixedWidth(30)
311+
312+
def remove_recent_library_clicked(key: str):
313+
return lambda: (
314+
self.driver.remove_recent_library(key),
315+
self.fill_libs_widget(self.libs_layout),
316+
)
317+
318+
button_remove.clicked.connect(remove_recent_library_clicked(item_key))
319+
320+
row_layout = QHBoxLayout()
321+
row_layout.addWidget(button)
322+
row_layout.addWidget(button_remove)
323+
324+
layout.addLayout(row_layout)
289325

290326
def resizeEvent(self, event: QResizeEvent) -> None:
291327
self.update_image_size(

0 commit comments

Comments
 (0)