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

The Toolbox class provides some useful general purpose methods. More...

#include <toolbox.h>

+ Collaboration diagram for Toolbox:

Public Member Functions

 Toolbox ()=delete
 
 Toolbox (const Toolbox &other)=delete
 
 ~Toolbox ()=delete
 
Toolboxoperator= (const Toolbox &rhs)=delete
 

Static Public Member Functions

template<typename T >
static QSet< T > toSet (const QList< T > &list) noexcept
 Helper method to convert a QList<T> to a QSet<T>
 
template<typename T >
static QVector< T > toVector (const QSet< T > &set) noexcept
 Helper method to convert a QSet<T> to a QVector<T>
 
template<typename T >
static QList< T > toList (const QSet< T > &set) noexcept
 Helper method to convert a QSet<T> to a QList<T>
 
template<typename T >
static QList< T > sortedQSet (const QSet< T > &set) noexcept
 
template<typename T , typename Compare >
static QList< T > sortedQSet (const QSet< T > &set, const Compare &cmp) noexcept
 
template<typename T >
static T sorted (const T &container) noexcept
 
template<typename T , typename Compare >
static void sortNumeric (T &container, Compare compare, Qt::CaseSensitivity caseSensitivity=Qt::CaseInsensitive, bool ignorePunctuation=false) noexcept
 Sort a container of arbitrary objects using QCollators numeric mode.
 
template<typename T >
static void sortNumeric (T &container, Qt::CaseSensitivity caseSensitivity=Qt::CaseInsensitive, bool ignorePunctuation=false) noexcept
 Sort a container of strings using QCollators numeric mode.
 
static bool isTextUpsideDown (const Angle &rotation) noexcept
 Check if a text with a given rotation is considered as upside down.
 
static QRectF boundingRectFromRadius (qreal radius) noexcept
 
static QRectF boundingRectFromRadius (qreal rx, qreal ry) noexcept
 
static QRectF adjustedBoundingRect (const QRectF &rect, qreal offset) noexcept
 
static QPainterPath shapeFromPath (const QPainterPath &path, const QPen &pen, const QBrush &brush, const UnsignedLength &minWidth=UnsignedLength(0)) noexcept
 
static std::optional< LengtharcRadius (const Point &p1, const Point &p2, const Angle &angle) noexcept
 
static std::optional< PointarcCenter (const Point &p1, const Point &p2, const Angle &angle) noexcept
 
static Angle arcAngle (const Point &p1, const Point &p2, const Point &center=Point(0, 0)) noexcept
 Calculate the angle between two given points.
 
static Angle arcAngleFrom3Points (const Point &start, const Point &mid, const Point &end) noexcept
 Calculate the angle of an arc given by 3 points.
 
static Angle angleBetweenPoints (const Point &p1, const Point &p2) noexcept
 Calculate the angle between two points.
 
static Point nearestPointOnLine (const Point &p, const Point &l1, const Point &l2) noexcept
 Calculate the point on a given line which is nearest to a given point.
 
static UnsignedLength shortestDistanceBetweenPointAndLine (const Point &p, const Point &l1, const Point &l2, Point *nearest=nullptr) noexcept
 Calculate the shortest distance between a given point and a given line.
 
static QString incrementNumberInString (QString string) noexcept
 Copy a string while incrementing its contained number.
 
static QStringList expandRangesInString (const QString &string) noexcept
 Expand ranges like "1..5" in a string to all its values.
 
static QString cleanUserInputString (const QString &input, const QRegularExpression &removeRegex, bool trim=true, bool toLower=false, bool toUpper=false, const QString &spaceReplacement=" ", int maxLength=-1) noexcept
 Clean a user input string.
 
static QString prettyPrintLocale (const QString &code) noexcept
 Pretty print the name of a QLocale.
 
template<typename T >
static QString floatToString (T value, int decimals, const QLocale &locale) noexcept
 Convert a float or double to a localized string.
 
template<typename T >
static QString decimalFixedPointToString (T value, qint32 pointPos) noexcept
 Convert a fixed point decimal number from an integer to a QString.
 
template<typename T >
static T decimalFixedPointFromString (const QString &str, qint32 pointPos)
 Convert a fixed point decimal number from a QString to an integer.
 

Static Private Member Functions

static QStringList expandRangesInString (const QString &input, const QVector< std::tuple< int, int, QStringList > > &replacements) noexcept
 Internal helper function for expandRangesInString(const QString&)
 

Detailed Description

The Toolbox class provides some useful general purpose methods.

Constructor & Destructor Documentation

◆ Toolbox() [1/2]

Toolbox ( )
delete

◆ Toolbox() [2/2]

Toolbox ( const Toolbox other)
delete

◆ ~Toolbox()

~Toolbox ( )
delete

Member Function Documentation

◆ operator=()

Toolbox & operator= ( const Toolbox rhs)
delete

◆ toSet()

template<typename T >
static QSet< T > toSet ( const QList< T > &  list)
inlinestaticnoexcept

Helper method to convert a QList<T> to a QSet<T>

Until Qt 5.13, QList::toSet() was the way to do this conversion. But since Qt 5.14 this is deprecated, and a range constructor was added to QSet instead. This wrapper chooses the proper method depending on the Qt version to avoid raising any deprecation warnings.

Parameters
listThe QList to be converted.
Returns
The created QSet.
+ Here is the caller graph for this function:

◆ toVector()

template<typename T >
static QVector< T > toVector ( const QSet< T > &  set)
inlinestaticnoexcept

Helper method to convert a QSet<T> to a QVector<T>

Until Qt 5.13, QSet::toList() was the way to do this conversion. But since Qt 5.14 this is deprecated, and a range constructor was added to QVector instead. This wrapper chooses the proper method depending on the Qt version to avoid raising any deprecation warnings.

Parameters
setThe QSet to be converted.
Returns
The created QVector (in arbitrary order!).
+ Here is the caller graph for this function:

◆ toList()

template<typename T >
static QList< T > toList ( const QSet< T > &  set)
inlinestaticnoexcept

Helper method to convert a QSet<T> to a QList<T>

Until Qt 5.13, QSet::toList() was the way to do this conversion. But since Qt 5.14 this is deprecated, and a range constructor was added to QVector instead. This wrapper chooses the proper method depending on the Qt version to avoid raising any deprecation warnings.

Parameters
setThe QSet to be converted.
Returns
The created QVector (in arbitrary order!).
+ Here is the caller graph for this function:

◆ sortedQSet() [1/2]

template<typename T >
static QList< T > sortedQSet ( const QSet< T > &  set)
inlinestaticnoexcept
+ Here is the caller graph for this function:

◆ sortedQSet() [2/2]

template<typename T , typename Compare >
static QList< T > sortedQSet ( const QSet< T > &  set,
const Compare &  cmp 
)
inlinestaticnoexcept

◆ sorted()

template<typename T >
static T sorted ( const T &  container)
inlinestaticnoexcept
+ Here is the caller graph for this function:

◆ sortNumeric() [1/2]

template<typename T , typename Compare >
static void sortNumeric ( T &  container,
Compare  compare,
Qt::CaseSensitivity  caseSensitivity = Qt::CaseInsensitive,
bool  ignorePunctuation = false 
)
inlinestaticnoexcept

Sort a container of arbitrary objects using QCollators numeric mode.

Parameters
containerA string container as supported by QCollator.
compareCustom comparison function with the signature bool(const QCollator&, const V&, const V&) where V represents the container item type.
caseSensitivityCase sensitivity of comparison.
ignorePunctuationWhether punctuation is ignored or not.
+ Here is the caller graph for this function:

◆ sortNumeric() [2/2]

template<typename T >
static void sortNumeric ( T &  container,
Qt::CaseSensitivity  caseSensitivity = Qt::CaseInsensitive,
bool  ignorePunctuation = false 
)
inlinestaticnoexcept

Sort a container of strings using QCollators numeric mode.

Parameters
containerA string container as supported by QCollator.
caseSensitivityCase sensitivity of comparison.
ignorePunctuationWhether punctuation is ignored or not.
+ Here is the call graph for this function:

◆ isTextUpsideDown()

bool isTextUpsideDown ( const Angle rotation)
staticnoexcept

Check if a text with a given rotation is considered as upside down.

A text is considered as upside down if it is rotated counterclockwise by [-90..90°[, i.e. -90° is considered as upside down, but 90° is not considered as upside down.

Used to determine whether a text needs to be auto-rotated or not.

Parameters
rotationThe global (scene coordinates) rotation of the text.
Returns
Whether the text is upside down or not.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ boundingRectFromRadius() [1/2]

static QRectF boundingRectFromRadius ( qreal  radius)
inlinestaticnoexcept
+ Here is the caller graph for this function:

◆ boundingRectFromRadius() [2/2]

static QRectF boundingRectFromRadius ( qreal  rx,
qreal  ry 
)
inlinestaticnoexcept

◆ adjustedBoundingRect()

static QRectF adjustedBoundingRect ( const QRectF &  rect,
qreal  offset 
)
inlinestaticnoexcept
+ Here is the caller graph for this function:

◆ shapeFromPath()

QPainterPath shapeFromPath ( const QPainterPath &  path,
const QPen &  pen,
const QBrush &  brush,
const UnsignedLength minWidth = UnsignedLength(0) 
)
staticnoexcept
+ Here is the caller graph for this function:

◆ arcRadius()

std::optional< Length > arcRadius ( const Point p1,
const Point p2,
const Angle angle 
)
staticnoexcept
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ arcCenter()

std::optional< Point > arcCenter ( const Point p1,
const Point p2,
const Angle angle 
)
staticnoexcept
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ arcAngle()

Angle arcAngle ( const Point p1,
const Point p2,
const Point center = Point(0, 0) 
)
staticnoexcept

Calculate the angle between two given points.

Parameters
p1First point.
p2Second point.
centerCenter of the arc. Defaults to (0, 0).
Returns
Angle counter-clockwise from p1 to p2 (0..360°). Zero if it could not be determined.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ arcAngleFrom3Points()

Angle arcAngleFrom3Points ( const Point start,
const Point mid,
const Point end 
)
staticnoexcept

Calculate the angle of an arc given by 3 points.

Attention
This method might not be 100% accurate and thus should not be used for important things. It this is needed some day, unit tests should be changed to allow no (or very small) tolerance.
Parameters
startArc start point.
midArc middle point (50% of length).
endArc end point.
Returns
Angle counter-clockwise from start to end (0..360°). Zero if it could not be determined.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ angleBetweenPoints()

Angle angleBetweenPoints ( const Point p1,
const Point p2 
)
staticnoexcept

Calculate the angle between two points.

Parameters
p1First point.
p2Second point.
Returns
Angle counter-clockwise from p1 to p2 (0..360°). Zero if it could not be determined.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nearestPointOnLine()

Point nearestPointOnLine ( const Point p,
const Point l1,
const Point l2 
)
staticnoexcept

Calculate the point on a given line which is nearest to a given point.

Parameters
pAn arbitrary point
l1Start point of the line
l2End point of the line
Returns
Nearest point on the given line (either l1, l2, or a point between them)
Warning
This method works with floating point numbers and thus the result may not be perfectly precise.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ shortestDistanceBetweenPointAndLine()

UnsignedLength shortestDistanceBetweenPointAndLine ( const Point p,
const Point l1,
const Point l2,
Point nearest = nullptr 
)
staticnoexcept

Calculate the shortest distance between a given point and a given line.

Parameters
pAn arbitrary point
l1Start point of the line
l2End point of the line
nearestIf not nullptr, the nearest point is returned here
Returns
Shortest distance between the given point and the given line (>=0)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ incrementNumberInString()

QString incrementNumberInString ( QString  string)
staticnoexcept

Copy a string while incrementing its contained number.

  • If the string contains one or more numbers, the last one gets incremented
  • If it does not contain a number, a "1" is appended instead

This way, the returned number is guaranteed to be different from the input string. That's useful for example to generate unique, incrementing pin numbers like "X1", "X2", "X3" etc.

Parameters
stringThe input string
Returns
A new string with the incremented number
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ expandRangesInString() [1/2]

QStringList expandRangesInString ( const QString &  string)
staticnoexcept

Expand ranges like "1..5" in a string to all its values.

A range is either defined by two integers with ".." in between, or two ASCII letters with ".." in between. If multiple ranges are contained, all combinations of them will be created.

For example the string "X1..10_A..C" expands to the list ["X1_A", "X1_B", "X1_C", ..., "X10_C"].

Note
Minus ('-') and plus ('+') characters are not interpreted as the sign of a number because in EDA tools they often are considered as strings, not as number signs (e.g. the inputs of an OpAmp).
Parameters
stringThe input string (may or may not contain ranges).
Returns
A list with expanded ranges in all combinations. If the input string does not contain ranges, a list with one element (equal to the input) is returned.
+ Here is the caller graph for this function:

◆ cleanUserInputString()

QString cleanUserInputString ( const QString &  input,
const QRegularExpression &  removeRegex,
bool  trim = true,
bool  toLower = false,
bool  toUpper = false,
const QString &  spaceReplacement = " ",
int  maxLength = -1 
)
staticnoexcept

Clean a user input string.

Parameters
inputThe string typed by the user
removeRegexRegex for all patterns to remove from the string
trimIf true, leading and trailing spaces are removed
toLowerIf true, all characters are converted to lowercase
toUpperIf true, all characters are converted to uppercase
spaceReplacementAll spaces are replaced by this string
maxLengthIf >= 0, the string is truncated to this length
Returns
The cleaned string (may be empty)
+ Here is the caller graph for this function:

◆ prettyPrintLocale()

QString prettyPrintLocale ( const QString &  code)
staticnoexcept

Pretty print the name of a QLocale.

To show locale names in the UI. Examples:

  • "en_US" -> "American English (United States)"
  • "de" -> "Deutsch"
  • "eo" -> "Esperanto"
Parameters
codeThe code of the locale to print (e.g. "en_US").
Returns
String with the pretty printed locale name.
+ Here is the caller graph for this function:

◆ floatToString()

template<typename T >
static QString floatToString ( value,
int  decimals,
const QLocale &  locale 
)
inlinestaticnoexcept

Convert a float or double to a localized string.

Same as QLocale::toString<float/double>(), but with omitted trailing zeros and without group separators.

+ Here is the caller graph for this function:

◆ decimalFixedPointToString()

template<typename T >
static QString decimalFixedPointToString ( value,
qint32  pointPos 
)
inlinestaticnoexcept

Convert a fixed point decimal number from an integer to a QString.

Parameters
valueValue to convert
pointPosNumber of fixed point decimal positions

◆ decimalFixedPointFromString()

template<typename T >
static T decimalFixedPointFromString ( const QString &  str,
qint32  pointPos 
)
inlinestatic

Convert a fixed point decimal number from a QString to an integer.

Parameters
strA QString that represents the number
pointPosNumber of decimal positions. If the number has more decimal digits, this function will throw

◆ expandRangesInString() [2/2]

QStringList expandRangesInString ( const QString &  input,
const QVector< std::tuple< int, int, QStringList > > &  replacements 
)
staticprivatenoexcept

Internal helper function for expandRangesInString(const QString&)


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