spacr.qt.widgets.picture_export¶
Right-click a picture and save it, as a PNG or as a PDF.
A picture on screen is often the thing somebody wants in a slide an hour later, and the route to it was a screenshot of a window, at the window’s resolution, with the surrounding chrome in it. This saves THE PICTURE, at the resolution it was rendered at rather than the size it happens to be shown at, into a file of the user’s choosing.
TWO FORMATS, AND THE PDF IS NOT A PNG WITH A DIFFERENT EXTENSION. A raster
dropped into a PDF satisfies the file name and nothing else; a figure panel
is a page, and a page has a size in millimetres. save_picture() gives
the PDF a page the image’s own shape and tells the writer the resolution
Preferences carries, so the picture lands at the size it was measured at and
prints at that resolution.
The resolution and the default format are the ones the Figures preferences
already own – spacr.qt.preferences.get_figure_png_dpi() and
spacr.qt.preferences.get_figure_format() – so a user who has already
said “300 DPI, PDF” is not asked a second time in different words.
Functions¶
|
Whatever was handed over, as a |
|
Ask for a path. |
|
The right-click menu: save as PNG, save as PDF. |
|
Pop the menu at |
|
Give |
|
The resolution a saved picture is written at. |
|
|
|
Ask for a path and write |
|
Write |
|
A file name for |
Module Contents¶
- spacr.qt.widgets.picture_export.as_image(picture) PySide6.QtGui.QImage | None[source]¶
Whatever was handed over, as a
QImage, orNone.A caller may hold a
QPixmap(what a view shows), aQImage(what a renderer produced) or nothing at all yet.- Parameters:
picture – a
QPixmap, aQImageorNone; a null image or any other type also givesNone.
- spacr.qt.widgets.picture_export.ask_where_to_save(parent, stem: str) str[source]¶
Ask for a path.
""when the user walked away.THE SUFFIX IS PUT BACK IF THE USER DROPS IT. A name typed without one would otherwise be written as a PNG whatever filter was selected, which is how “save as PDF” quietly produces a raster.
- Parameters:
parent – the widget the save dialog is parented to.
stem – the file name offered, without a suffix; cleaned by
suggested_name().
The right-click menu: save as PNG, save as PDF.
Each action carries its suffix as its
data, so the caller reads the choice off the action rather than comparing it against two references it had to keep.- Parameters:
parent – the widget that owns the menu.
enabled –
Falsewhen there is no picture yet. The actions are SHOWN AND GREYED rather than left out, and a line underneath says why: a menu that is empty on one field and full on the next reads as a bug, not as a state.
- spacr.qt.widgets.picture_export.choose_format(view, point, enabled: bool) str[source]¶
Pop the menu at
pointand return the chosen suffix, or"".THE MODAL CALL IS ALONE IN HERE so everything around it can be driven by a test.
QMenu.execspins an event loop of its own, and a test that reached it would hang rather than fail; substituting this one function is how the rest of the gesture is checked at all.- Parameters:
view – the widget the menu belongs to;
pointis mapped to global coordinates through it.point – where the right-click happened, in
viewcoordinates.enabled –
Falsegreys out the save actions, as inbuild_menu().
- spacr.qt.widgets.picture_export.install_picture_save(view, picture: Callable[[], object], stem: str = 'picture', unless: Callable[[], bool] | None = None) bool[source]¶
Give
viewa right-click menu that saves what it is showing.- Parameters:
view – the widget the user right-clicks.
picture – called when the menu is used; returns the
QPixmaporQImageto write. Called AT SAVE TIME rather than now, so the menu always writes the picture currently on screen.stem – the file name offered, without a suffix. A callable is asked at save time, so a view whose contents change – Overlay one moment, Flows the next – offers the name of what is on it now.
unless – asked first;
Truemeans the right button belongs to something else at this moment and no menu is shown. The live preview’s ruler is cleared with a right-click while it is active, and a menu appearing over that would take a tool away to add a file dialog nobody asked for.
- Returns:
whether the menu was installed.
- spacr.qt.widgets.picture_export.picture_dpi() int[source]¶
The resolution a saved picture is written at.
- Returns:
the Figures preference, or
FALLBACK_DPI.
- spacr.qt.widgets.picture_export.preferred_suffix() str[source]¶
".pdf"or".png", following the Figures format preference.
- spacr.qt.widgets.picture_export.save_as(view, picture, stem: str, want: str) str[source]¶
Ask for a path and write
picturethere.""if nothing was.- Parameters:
view – the widget the save dialog and any warning are parented to.
picture – the
QPixmaporQImageto write.stem – the file name offered, without a suffix.
want – the suffix the user picked in the menu. It WINS over what the file dialog came back with, so “save as PDF” followed by a name ending in
.pngstill writes a PDF rather than quietly changing format because of a typed extension.
- Returns:
the path written, or
"".
- spacr.qt.widgets.picture_export.save_picture(picture, path, dpi: int | None = None) bool[source]¶
Write
picturetopath, as a PNG or a PDF by its suffix.- Parameters:
picture – a
QPixmaporQImage.path – where to write.
.pdfwrites a page; anything else writes a PNG.dpi – resolution;
picture_dpi()when omitted.
- Returns:
whether a file was written.
Nested helpers¶
Offer the save menu at
point; the path written, or"".- Parameters:
point – where the right-click landed, in
viewcoordinates.
spacr/qt/widgets/picture_export.py:338