Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix for recent library re-creating a library at the last library location. #238

Merged
merged 4 commits into from
Jun 3, 2024

Conversation

Loran425
Copy link
Collaborator

@Loran425 Loran425 commented Jun 3, 2024

If the latest opened library no longer contains a TS_FOLDER_NAME (currently .TagStudio) folder clear the recent library and revert to default behavior for opening TagStudio.

NOTE: if the library moves to a different architecture (e.g. living alongside preference instead of alongside the files this check will need to be removed)

Fixes #212

… been deleted.

If the library no longer has a `.TagStudio` folder clear the Last_Library value
@Loran425
Copy link
Collaborator Author

Loran425 commented Jun 3, 2024

Another check should probably be done when populating the recent libraries in the preview panel so that broken libraries either don't appear or appear in a different color (an unavailable network share doesn't get deleted but gets crossed out and becomes unclickable or clicking does a check for availability)

@CyanVoxel
Copy link
Member

Another check should probably be done when populating the recent libraries in the preview panel so that broken libraries either don't appear or appear in a different color (an unavailable network share doesn't get deleted but gets crossed out and becomes unclickable or clicking does a check for availability)

The following changes to def _fill_libs_widget() on line 264 preview_panel.py will check if the locations are valid, then disable the buttons if they aren't:

def _fill_libs_widget(
        self, libraries: list[tuple[str, tuple[str, str]]], layout: QVBoxLayout
    ):
        def clear_layout(layout_item: QVBoxLayout):
            for i in reversed(range(layout_item.count())):
                child = layout_item.itemAt(i)
                if child.widget() is not None:
                    child.widget().deleteLater()
                elif child.layout() is not None:
                    clear_layout(child.layout())  # type: ignore

        # remove any potential previous items
        clear_layout(layout)

        label = QLabel("Recent Libraries")
        label.setAlignment(Qt.AlignCenter)  # type: ignore

        row_layout = QHBoxLayout()
        row_layout.addWidget(label)
        layout.addLayout(row_layout)

        def set_button_style(
            btn: QPushButton, extras: list[str] | None = None, enabled: bool = True
        ):
            base_style = [
                f"background-color:{Theme.COLOR_BG.value};",
                "border-radius:6px;",
                "text-align: left;",
                "padding-top: 3px;",
                "padding-left: 6px;",
                "padding-bottom: 4px;",
            ]

            full_style_rows = base_style + (extras or [])

            btn.setStyleSheet(
                (
                    "QPushButton{"
                    f"{''.join(full_style_rows)}"
                    "}"
                    f"QPushButton::hover{{background-color:{Theme.COLOR_HOVER.value};}}"
                    f"QPushButton::pressed{{background-color:{Theme.COLOR_PRESSED.value};}}"
                )
            )
            btn.setCursor(Qt.CursorShape.PointingHandCursor)

        for item_key, (full_val, cut_val) in libraries:
            button = QPushButton(text=cut_val)
            button.setObjectName(f"path{item_key}")
            disabled_style: list[str] = [
                f"color:{get_tag_color(ColorType.LIGHT_ACCENT, "Red")};",
                f"background-color:{get_tag_color(ColorType.DARK_ACCENT, "Red")};",
            ]

            if Path(full_val).exists():

                def open_library_button_clicked(path):
                    return lambda: self.driver.open_library(Path(path))

                button.clicked.connect(open_library_button_clicked(full_val))
                set_button_style(button)
            else:
                button.setEnabled(False)
                button.setToolTip("Location no longer exists.")
                set_button_style(button, disabled_style)

            button_remove = QPushButton("➖")
            button_remove.setCursor(Qt.CursorShape.PointingHandCursor)
            button_remove.setFixedWidth(30)
            set_button_style(button_remove)

            def remove_recent_library_clicked(key: str):
                return lambda: (
                    self.driver.remove_recent_library(key),
                    self.fill_libs_widget(self.libs_layout),
                )

            button_remove.clicked.connect(remove_recent_library_clicked(item_key))

            row_layout = QHBoxLayout()
            row_layout.addWidget(button)
            row_layout.addWidget(button_remove)

            layout.addLayout(row_layout)

image

Comment on lines 528 to 535
# TODO: Remove this check if the library is no longer saved with files
if not (Path(lib) / TS_FOLDER_NAME).exists():
logging.error(
f"[QT DRIVER] {TS_FOLDER_NAME} folder in {lib} does not exist."
)
self.settings.setValue(SettingItems.LAST_LIBRARY, "")
lib = None

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If LAST_LIBRARY is absent from the config while START_LOAD_LAST remains (a situation I encountered while testing) then this block soft crashes if it doesn't verify that lib received a value from the config.

Suggested change
# TODO: Remove this check if the library is no longer saved with files
if not (Path(lib) / TS_FOLDER_NAME).exists():
logging.error(
f"[QT DRIVER] {TS_FOLDER_NAME} folder in {lib} does not exist."
)
self.settings.setValue(SettingItems.LAST_LIBRARY, "")
lib = None
if lib:
# TODO: Remove this check if the library is no longer saved with files
if not (Path(lib) / TS_FOLDER_NAME).exists():
logging.error(
f"[QT DRIVER] {TS_FOLDER_NAME} folder in {lib} does not exist."
)
self.settings.setValue(SettingItems.LAST_LIBRARY, "")
lib = None

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh yeah I didn't think about that case. change implemented.

@CyanVoxel CyanVoxel added the Type: Bug Something isn't working as intended label Jun 3, 2024
@Loran425
Copy link
Collaborator Author

Loran425 commented Jun 3, 2024

Went a slightly different direction on the stylesheet just giving those buttons a disabled style and adding the color to the theme instead of importing the get_tag_color function.

@Loran425
Copy link
Collaborator Author

Loran425 commented Jun 3, 2024

but now I've got an issue where Path("") is evaluating to Path(".") which is obviously not intended behavior.

@CyanVoxel CyanVoxel merged commit 10b90dc into TagStudioDev:main Jun 3, 2024
3 checks passed
@Loran425 Loran425 deleted the bugfix/deleted-recent-library branch June 3, 2024 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Something isn't working as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TagStudio opens recent library even if it has been deleted
2 participants