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

Add an option to show image from editor in folder #3412

Merged
merged 3 commits into from
Sep 20, 2024
Merged

Conversation

user1823
Copy link
Contributor

@user1823 user1823 commented Sep 10, 2024

This feature is quite handy, especially when you want to see an enlarged view of the image.

For e.g., see https://forums.ankiweb.net/t/copying-images-out-of-cards-is-hard/20272/6

Credits: @abdnh's Reveal in File Manager add-on (https://github.com/abdnh/anki-misc/tree/master/reveal_in_file_manager)

@user1823

This comment was marked as outdated.

@abdnh
Copy link
Collaborator

abdnh commented Sep 10, 2024

It would be nice to open the file manager with the file selected. Here are relevant examples from add-ons:

https://github.com/ijgnd/anki21__OpenInExternalEditor_Rename_Duplicate_for_Image_Audio_Video/blob/master/src/showInFilemanager.py

https://github.com/abdnh/anki-misc/blob/master/reveal_in_file_manager/__init__.py

@user1823
Copy link
Contributor Author

It would be nice to open the file manager with the file selected.

What is the use case?

@abdnh
Copy link
Collaborator

abdnh commented Sep 10, 2024

It potentially allows for more operations on the file (e.g. copy, opening in some editor, etc) but probably just opening the image is easier to implement, as you don't need to handle different file managers on Linux AFAIK.

@user1823
Copy link
Contributor Author

user1823 commented Sep 10, 2024

It potentially allows for more operations on the file (e.g. copy, opening in some editor, etc)

Ability to copy the image has already been added in #3362

Adding the ability to open the image in external editors doesn't seem important enough to justify the complexity. Plus, if we open the file manager instead of the file itself, opening the image will require an additional click.

@glutanimate
Copy link
Contributor

glutanimate commented Sep 10, 2024

This looks like code extracted almost 1:1 from Image Occlusion Enhanced, including the lack of type annotations on open_image, which would be nice to improve here. Although this can be OK for more trivial snippets like this (opinions differ on what is trivial), it would still be nice to credit add-on authors in these cases. Even features enabled by seemingly trivial code can take a while to ideate, develop, and test, so leaving licensing and CLA requirements aside, I think it's only fair to maintain that continuity of credits going from add-on feature to native feature.

Also I'm not sure if the implementation needs to be as complex as it was here. I wrote this ≈7 years ago, and at that point the OS-specific calls might have been to work around Qt bugs which are no longer present today. In other places in Anki we just delegate picking the correct command to Qt's desktop services API, so I would suggest checking if that is sufficient here as well. If you find that the direct calls are still needed, then it could be advisable not to use subprocess directly as the add-on did, but rather use existing utilities in Anki like call. Similarly, it would probably be good to use the no_bundled_libs context for the QDesktopServices.openUrl call.

qt/aqt/editor.py Outdated Show resolved Hide resolved
@dae
Copy link
Member

dae commented Sep 10, 2024

When I think about experiences with other apps, I think "reveal in Finder/file browser" options seem to be more common than "open in default editor" - as it lets you also do operations on the file, open non-default editors, etc. It's an extra step compared to a separate option to open in the default editor, but it's more flexible if we can't have both.

Please obtain permission before using other people's code in your PRs - when you added your name to the CONTRIBUTORS file, you agreed to do that. The comment you've added gives credit, but now looks like the code has been appropriated, and may make other contributors think they can do the same thing. A better way to approach this would be to ask @glutanimate if he's happy to have that code contributed under the standard BSD-3 license of contributions, and then mention so in your commit message instead of in a code comment.

@user1823
Copy link
Contributor Author

Thank you all for your inputs. I will be more careful next time. I will try to fix the issues and accommodate the feedback in this PR tomorrow.

@user1823 user1823 marked this pull request as draft September 12, 2024 15:03
@user1823 user1823 changed the title Add an option to open image from editor in system viewer Add an option to show image from editor in folder Sep 12, 2024
@user1823 user1823 marked this pull request as ready for review September 12, 2024 16:18
@user1823
Copy link
Contributor Author

user1823 commented Sep 12, 2024

This is ready for a second review now.

It's an extra step compared to a separate option to open in the default editor, but it's more flexible if we can't have both.

@dae, does this mean that adding both options would be acceptable?

In other places in Anki, we just delegate picking the correct command to Qt's desktop services API, so I would suggest checking if that is sufficient here as well.

It seems that Qt doesn't provide any option for selecting the desired file in the file manager. So, I have used direct calls. As per your suggestion, I have used call rather than using subprocess directly.

@user1823

This comment was marked as off-topic.

else:
# Just open the file in any other platform
with no_bundled_libs():
QDesktopServices.openUrl(QUrl(f"file://{path}"))
Copy link
Member

Choose a reason for hiding this comment

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

Have you copied+pasted this code from somewhere? From a quick read of the Qt docs, it looks like openUrl() should do what we want on all platforms?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For local files, openUrl() opens the file in the default viewer if it can. But, we want it to highlight the file even when there is a viewer available.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I was under the mistaken impression that it would select the file, not open it directly.

a = menu.addAction(tr.editing_copy_image())
qconnect(a.triggered, self.on_copy_image)

if is_win or is_mac:
Copy link
Member

Choose a reason for hiding this comment

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

Any feature we add should work across all OSes - is there a reason why non-Mac/Windows has been excluded here?

Copy link
Contributor Author

@user1823 user1823 Sep 20, 2024

Choose a reason for hiding this comment

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

It would definitely be better to make a feature work on all platforms. But, I have no idea how to make it work with all the different file managers in Linux. Even if this doesn't work on Linux, it is still an improvement over what we currently have, especially when you consider that Windows + Mac users comprise most of the Anki's userbase.

Copy link
Member

Choose a reason for hiding this comment

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

"We can't do it on Linux easily" is a reasonable answer for now :-) Thanks for clarifying.

@dae dae merged commit 02d2566 into ankitects:main Sep 20, 2024
1 check passed
@user1823
Copy link
Contributor Author

It's an extra step compared to a separate option to open in the default editor, but it's more flexible if we can't have both.

@dae, does this mean that adding both options would be acceptable?

Would you accept another PR adding an "Open image" option?

@user1823 user1823 deleted the patch-1 branch September 20, 2024 11:50
@dae
Copy link
Member

dae commented Sep 20, 2024

Yes, if you feel it's necessary.

twwn added a commit to twwn/anki that referenced this pull request Sep 22, 2024
* master:
  Update translations
  Add an option to show image from editor in folder (ankitects#3412)
  Call the profile_did_open() hook earlier (ankitects#3421)
  Fix FSRS progress update issues (ankitects#3420)
  Update tooltip text (ankitects#3418)
  Fix pasting from the primary selection (ankitects#3413)
  Fix occlusion rounding bug (ankitects#3400)
  Add comment about the usage of the input field in the statistics page (ankitects#3394) (ankitects#3398)
  Possible to show “last” subdeck name in Browser? (ankitects#3387)
twwn pushed a commit to twwn/anki that referenced this pull request Sep 22, 2024
* Add "Show in folder" option to images in editor

Credits: @abdnh's Reveal in File Manager add-on (https://github.com/abdnh/anki-misc/tree/master/reveal_in_file_manager)

* Refactor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants