LibrePCB Developers Documentation
Loading...
Searching...
No Matches
ImageHelpers Class Referencefinal

Various editor helper functions for working with librepcb::Image. More...

#include <imagehelpers.h>

+ Collaboration diagram for ImageHelpers:

Public Types

enum class  Target
 

Public Member Functions

 ImageHelpers ()=delete
 
 ImageHelpers (const ImageHelpers &other)=delete
 
 ~ImageHelpers ()=delete
 
ImageHelpersoperator= (const ImageHelpers &rhs)=delete
 

Static Public Member Functions

static std::optional< FileProofNamefindExistingFile (const TransactionalDirectory &dir, const QByteArray &data)
 Find a file with specific content in a directory.
 
static FileProofName getUnusedFileName (const TransactionalDirectory &dir, QString nameUserInput, const QString &extension)
 Build a valid filename for an image file to be created.
 
static bool execImageChooserDialog (QByteArray &data, QString &format, QString &basename, const QString &settingsKey)
 Execute "open file" dialog to choose an image.
 
static std::optional< FileProofNamefindExistingOrAskForNewImageFileName (const TransactionalDirectory &dir, Target target, const QByteArray &data, const QString &format, const QString &basename, bool &exists)
 Determine the filename of an image to be added to a directory.
 
static bool isImageInClipboard () noexcept
 Check if the clipboard contains any kind of image.
 
static bool getImageFromClipboard (QByteArray &data, QString &format, QString &basename) noexcept
 Try to get the image from the clipboard.
 

Detailed Description

Various editor helper functions for working with librepcb::Image.

Intended to share code between various editors resp. their state machines.

Member Enumeration Documentation

◆ Target

enum class Target
strong
Enumerator
Symbol 
Project 

Constructor & Destructor Documentation

◆ ImageHelpers() [1/2]

ImageHelpers ( )
delete

◆ ImageHelpers() [2/2]

ImageHelpers ( const ImageHelpers other)
delete

◆ ~ImageHelpers()

~ImageHelpers ( )
delete

Member Function Documentation

◆ operator=()

ImageHelpers & operator= ( const ImageHelpers rhs)
delete

◆ findExistingFile()

std::optional< FileProofName > findExistingFile ( const TransactionalDirectory dir,
const QByteArray &  data 
)
static

Find a file with specific content in a directory.

Intended to reuse image files already existing in a directory.

Parameters
dirThe directory to search for files.
dataThe content of the file to search for.
Returns
The filename if a file was found, std::nullopt otherwise.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getUnusedFileName()

FileProofName getUnusedFileName ( const TransactionalDirectory dir,
QString  nameUserInput,
const QString &  extension 
)
static

Build a valid filename for an image file to be created.

This method builds a filename that is a valid librepcb::FileProofName for a file which doesn't exist yet in a given directory by guarantee. If there are filename conflicts, a number will be appended to make it unique. Intended to be used to determine a valid filename before adding a new file to a directory.

Parameters
dirThe directory where a new file shall be created.
nameUserInputThe desired file basename (untrusted input accepted). If empty or completely invalid, a fallback name will be used instead.
extensionThe image file extension. Must be one of librepcb::Image::getSupportedExtensions() (case sensitive), otherwise an exception is thrown!
Returns
A guaranteed valid filename of a non-existent file.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ execImageChooserDialog()

bool execImageChooserDialog ( QByteArray &  data,
QString &  format,
QString &  basename,
const QString &  settingsKey 
)
static

Execute "open file" dialog to choose an image.

Note
If neccessary, this method does convert the selected image file to a supported image format. However, if the file extension is one of the supported formats, this method does not open or validate the file. Always call librepcb::Image::tryLoad() afterwards to do so.
Parameters
dataContent of the chosen image (may have been converted into a supported image format).
formatImage format of data (e.g. "png").
basenameBasename of the chosen file (untrusted user input).
settingsKeyThe dialog saves the chosen file under this QSettings key and preselects the file when opening the next time. Set to something like "schematic_editor/add_image/file".
Return values
trueAn image has been chosen (and converted if neccessary).
falseThe user canceled the file dialog.
Exceptions
ExceptionIf a format conversion was needed and it failed.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ findExistingOrAskForNewImageFileName()

std::optional< FileProofName > findExistingOrAskForNewImageFileName ( const TransactionalDirectory dir,
Target  target,
const QByteArray &  data,
const QString &  format,
const QString &  basename,
bool &  exists 
)
static

Determine the filename of an image to be added to a directory.

This may show a blocking input dialog where the user can enter a file name.

Parameters
dirDirectory where the file is asked to be added (this method does not actually add it).
targetThe target type that the directory represents. Only used for a UI translation string.
dataThe image data asked to be added.
formatThe image format of data.
basenameThe desired basename of the image (untrusted user input).
existsOutput variable whether the file already existed in dir or not.
Returns
The filename of the new file to be created, or std::nullopt if the user aborted the input dialog.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isImageInClipboard()

bool isImageInClipboard ( )
staticnoexcept

Check if the clipboard contains any kind of image.

If the clipboard contains a filepath to an image file (rather than the image itself), it is also taken into account (i.e. true returned) since getImageFromClipboard() is able to load it.

Call this method for a lightweight check if theres is any data getImageFromClipboard() is able to load.

Return values
trueThe clipboard contains an image.
falseThere's no image in the clipboard.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getImageFromClipboard()

bool getImageFromClipboard ( QByteArray &  data,
QString &  format,
QString &  basename 
)
staticnoexcept

Try to get the image from the clipboard.

If the clipboard contains a filepath to an image file (rather than the image itself), this method tries to load the image from that file.

Note
If neccessary, this method does convert the image to a supported image format. However, if the clipboard already contains one of the supported formats, this method does not validate the file. Always call librepcb::Image::tryLoad() afterwards to do so.
Parameters
dataContent of the image (may have been converted into a supported image format).
formatImage format of data (e.g. "png").
basenameBasename of the image (untrusted user input).
Return values
trueSuccessfully fetched the image.
falseNo image in the clipboard, or failed to fetch it.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

The documentation for this class was generated from the following files: