Krita Source Code Documentation
Loading...
Searching...
No Matches
KoUnit Class Reference

#include <KoUnit.h>

+ Inheritance diagram for KoUnit:

Public Types

enum  ListOption { ListAll = 0 , HidePixel = 1 , HideMask = HidePixel }
 Used to control the scope of the unit types listed in the UI. More...
 
enum  Type {
  Millimeter = 0 , Point , Inch , Centimeter ,
  Decimeter , Pica , Cicero , Pixel ,
  TypeCount
}
 

Public Member Functions

void adjustByPixelTransform (const QTransform &t)
 
qreal fromUserValue (const QString &value, bool *ok=0) const
 
qreal fromUserValue (qreal value) const
 
int indexInListForUi (ListOptions listOptions=ListAll) const
 
 KoUnit (Type unit=Point, qreal factor=1.0)
 
KoUnitoperator= (Type unit)
 
bool operator== (const KoUnit &other) const
 
void setFactor (qreal factor)
 
QString symbol () const
 Get the symbol string of the unit.
 
QString toString () const
 
QString toUserStringValue (qreal ptValue) const
 
qreal toUserValue (qreal ptValue, bool rounding=true) const
 
qreal toUserValuePrecise (const qreal ptValue) const
 
qreal toUserValueRounded (const qreal value) const
 
KoUnit::Type type () const
 

Static Public Member Functions

static qreal approxTransformScale (const QTransform &t)
 
static qreal convertFromUnitToUnit (const qreal value, const KoUnit &fromUnit, const KoUnit &toUnit, qreal factor=1.0)
 
static KoUnit fromListForUi (int index, ListOptions listOptions=ListAll, qreal factor=1.0)
 
static KoUnit fromSymbol (const QString &symbol, bool *ok=0)
 
static QStringList listOfUnitNameForUi (ListOptions listOptions=ListAll)
 Returns the list of unit types for the UI, controlled with the given listOptions.
 
static qreal parseAngle (const QString &value, qreal defaultVal=0.0)
 parse an angle to its value in degrees
 
static qreal parseValue (const QString &value, qreal defaultVal=0.0)
 parse common Krita and Odf values, like "10cm", "5mm" to pt
 
static QString unitDescription (KoUnit::Type type)
 Get the description string of the given unit.
 

Private Attributes

qreal m_pixelConversion
 
Type m_type
 

Detailed Description

Krita stores everything in pt (using "qreal") internally. When displaying a value to the user, the value is converted to the user's unit of choice, and rounded to a reasonable precision to avoid 0.999999

For implementing the selection of a unit type in the UI use the *ForUi() methods. They ensure the same order of the unit types in all places, with the order not bound to the order in the enum (so ABI-compatible extension is possible) and with the order and scope of listed types controlled by the ListOptions parameter.

Definition at line 70 of file KoUnit.h.

Member Enumeration Documentation

◆ ListOption

Used to control the scope of the unit types listed in the UI.

Enumerator
ListAll 
HidePixel 
HideMask 

Definition at line 87 of file KoUnit.h.

87 {
88 ListAll = 0,
89 HidePixel = 1,
91 };
@ HideMask
Definition KoUnit.h:90
@ HidePixel
Definition KoUnit.h:89
@ ListAll
Definition KoUnit.h:88

◆ Type

Length units supported by Krita.

Enumerator
Millimeter 
Point 

Postscript point, 1/72th of an Inco.

Inch 
Centimeter 
Decimeter 
Pica 
Cicero 
Pixel 
TypeCount 

Definition at line 74 of file KoUnit.h.

74 {
75 Millimeter = 0,
76 Point,
77 Inch,
80 Pica,
81 Cicero,
82 Pixel,
84 };
@ Point
Postscript point, 1/72th of an Inco.
Definition KoUnit.h:76
@ Centimeter
Definition KoUnit.h:78
@ Decimeter
Definition KoUnit.h:79
@ Pica
Definition KoUnit.h:80
@ Millimeter
Definition KoUnit.h:75
@ Inch
Definition KoUnit.h:77
@ Pixel
Definition KoUnit.h:82
@ Cicero
Definition KoUnit.h:81
@ TypeCount
Definition KoUnit.h:83

Constructor & Destructor Documentation

◆ KoUnit()

KoUnit::KoUnit ( Type unit = Point,
qreal factor = 1.0 )
inlineexplicit

Construction requires initialization. The factor is for variable factor units like pixel

Definition at line 103 of file KoUnit.h.

103 {
104 m_type = unit;
105 m_pixelConversion = factor;
106 }
qreal m_pixelConversion
Definition KoUnit.h:213
Type m_type
Definition KoUnit.h:212

Member Function Documentation

◆ adjustByPixelTransform()

void KoUnit::adjustByPixelTransform ( const QTransform & t)

Adjust the unit by pixel transformation applied to the describing object. It multiplies the pixel coefficient by the average scale of the matrix.

Definition at line 392 of file KoUnit.cpp.

393{
395}
static qreal approxTransformScale(const QTransform &t)
Definition KoUnit.cpp:387

References approxTransformScale(), and m_pixelConversion.

◆ approxTransformScale()

qreal KoUnit::approxTransformScale ( const QTransform & t)
static

Get an approximate scale of a unit vector that was converted by the transformation.

Please note that exact values are guaranteed only for combinations of Translate, Rotation and Uniform Scale matrices. For combinations having shears and perspective the value will be average for the point near CS origin.

Definition at line 387 of file KoUnit.cpp.

388{
389 return std::sqrt(qAbs(t.determinant()));
390}

◆ convertFromUnitToUnit()

qreal KoUnit::convertFromUnitToUnit ( const qreal value,
const KoUnit & fromUnit,
const KoUnit & toUnit,
qreal factor = 1.0 )
static

convert the given value directly from one unit to another

Definition at line 295 of file KoUnit.cpp.

296{
297 qreal pt;
298 switch (fromUnit.type()) {
299 case Millimeter:
300 pt = MM_TO_POINT(value);
301 break;
302 case Centimeter:
303 pt = CM_TO_POINT(value);
304 break;
305 case Decimeter:
306 pt = DM_TO_POINT(value);
307 break;
308 case Inch:
309 pt = INCH_TO_POINT(value);
310 break;
311 case Pica:
312 pt = PI_TO_POINT(value);
313 break;
314 case Cicero:
315 pt = CC_TO_POINT(value);
316 break;
317 case Pixel:
318 pt = value / factor;
319 break;
320 case Point:
321 default:
322 pt = value;
323 }
324
325 switch (toUnit.type()) {
326 case Millimeter:
327 return POINT_TO_MM(pt);
328 case Centimeter:
329 return POINT_TO_CM(pt);
330 case Decimeter:
331 return POINT_TO_DM(pt);
332 case Inch:
333 return POINT_TO_INCH(pt);
334 case Pica:
335 return POINT_TO_PI(pt);
336 case Cicero:
337 return POINT_TO_CC(pt);
338 case Pixel:
339 return pt * factor;
340 case Point:
341 default:
342 return pt;
343 }
344
345}
float value(const T *src, size_t ch)
constexpr qreal POINT_TO_CM(qreal px)
Definition KoUnit.h:33
constexpr qreal POINT_TO_DM(qreal px)
Definition KoUnit.h:35
constexpr qreal CC_TO_POINT(qreal cc)
Definition KoUnit.h:44
constexpr qreal POINT_TO_MM(qreal px)
Definition KoUnit.h:31
constexpr qreal POINT_TO_INCH(qreal px)
Definition KoUnit.h:37
constexpr qreal CM_TO_POINT(qreal cm)
Definition KoUnit.h:34
constexpr qreal INCH_TO_POINT(qreal inch)
Definition KoUnit.h:38
constexpr qreal POINT_TO_PI(qreal px)
Definition KoUnit.h:41
constexpr qreal PI_TO_POINT(qreal pi)
Definition KoUnit.h:43
constexpr qreal POINT_TO_CC(qreal px)
Definition KoUnit.h:42
constexpr qreal MM_TO_POINT(qreal mm)
Definition KoUnit.h:32
constexpr qreal DM_TO_POINT(qreal dm)
Definition KoUnit.h:36
KoUnit::Type type() const
Definition KoUnit.h:118

References CC_TO_POINT(), Centimeter, Cicero, CM_TO_POINT(), Decimeter, DM_TO_POINT(), Inch, INCH_TO_POINT(), Millimeter, MM_TO_POINT(), PI_TO_POINT(), Pica, Pixel, Point, POINT_TO_CC(), POINT_TO_CM(), POINT_TO_DM(), POINT_TO_INCH(), POINT_TO_MM(), POINT_TO_PI(), type(), and value().

◆ fromListForUi()

KoUnit KoUnit::fromListForUi ( int index,
ListOptions listOptions = ListAll,
qreal factor = 1.0 )
static

Returns a KoUnit instance with the type at the index of the UI list with the given listOptions.

Definition at line 80 of file KoUnit.cpp.

81{
83
84 if ((0 <= index) && (index < KoUnit::TypeCount)) {
85 // iterate through all enums and skip the Pixel enum if needed
86 for (int i = 0; i < KoUnit::TypeCount; ++i) {
87 if ((listOptions&HidePixel) && (typesInUi[i] == Pixel)) {
88 ++index;
89 continue;
90 }
91 if (i == index) {
92 type = typesInUi[i];
93 break;
94 }
95 }
96 }
97
98 return KoUnit(type, factor);
99}
static const KoUnit::Type typesInUi[KoUnit::TypeCount]
Definition KoUnit.cpp:57
KoUnit(Type unit=Point, qreal factor=1.0)
Definition KoUnit.h:103

References HidePixel, KoUnit(), Pixel, Point, type(), TypeCount, and typesInUi.

◆ fromSymbol()

KoUnit KoUnit::fromSymbol ( const QString & symbol,
bool * ok = 0 )
static

Convert a unit symbol string into a KoUnit

Parameters
symbolsymbol to convert
okif set, it will be true if the unit was known, false if unknown

Definition at line 271 of file KoUnit.cpp.

272{
273 Type result = Point;
274
275 if (symbol == QLatin1String("inch") /*compat*/) {
276 result = Inch;
277 if (ok)
278 *ok = true;
279 } else {
280 if (ok)
281 *ok = false;
282
283 for (int i = 0; i < TypeCount; ++i) {
284 if (symbol == QLatin1String(unitNameList[i])) {
285 result = static_cast<Type>(i);
286 if (ok)
287 *ok = true;
288 }
289 }
290 }
291
292 return KoUnit(result);
293}
static const char *const unitNameList[KoUnit::TypeCount]
Definition KoUnit.cpp:20
QString symbol() const
Get the symbol string of the unit.
Definition KoUnit.cpp:347

References Inch, KoUnit(), Point, symbol(), TypeCount, and unitNameList.

◆ fromUserValue() [1/2]

qreal KoUnit::fromUserValue ( const QString & value,
bool * ok = 0 ) const

This method is the one to use to read a value from a dialog

Parameters
valuevalue entered by the user
okif set, the pointed bool is set to true if the value could be converted to a qreal, and to false otherwise.
Returns
the value converted to points for internal use

Definition at line 224 of file KoUnit.cpp.

225{
226 return fromUserValue(QLocale().toDouble(value, ok));
227}
qreal fromUserValue(qreal value) const
Definition KoUnit.cpp:201
double toDouble(const quint8 *data, int channelpos)

References fromUserValue(), toDouble(), and value().

◆ fromUserValue() [2/2]

qreal KoUnit::fromUserValue ( qreal value) const

This method is the one to use to read a value from a dialog

Returns
the value converted to points for internal use

Definition at line 201 of file KoUnit.cpp.

202{
203 switch (m_type) {
204 case Millimeter:
205 return MM_TO_POINT(value);
206 case Centimeter:
207 return CM_TO_POINT(value);
208 case Decimeter:
209 return DM_TO_POINT(value);
210 case Inch:
211 return INCH_TO_POINT(value);
212 case Pica:
213 return PI_TO_POINT(value);
214 case Cicero:
215 return CC_TO_POINT(value);
216 case Pixel:
217 return value / m_pixelConversion;
218 case Point:
219 default:
220 return value;
221 }
222}

References CC_TO_POINT(), Centimeter, Cicero, CM_TO_POINT(), Decimeter, DM_TO_POINT(), Inch, INCH_TO_POINT(), m_pixelConversion, m_type, Millimeter, MM_TO_POINT(), PI_TO_POINT(), Pica, Pixel, Point, and value().

◆ indexInListForUi()

int KoUnit::indexInListForUi ( ListOptions listOptions = ListAll) const

Get the index of this unit in the list of unit types for the UI, if it is controlled with the given listOptions.

Definition at line 101 of file KoUnit.cpp.

102{
103 if ((listOptions&HidePixel) && (m_type == Pixel)) {
104 return -1;
105 }
106
107 int result = -1;
108
109 int skipped = 0;
110 for (int i = 0; i < KoUnit::TypeCount; ++i) {
111 if ((listOptions&HidePixel) && (typesInUi[i] == Pixel)) {
112 ++skipped;
113 continue;
114 }
115 if (typesInUi[i] == m_type) {
116 result = i - skipped;
117 break;
118 }
119 }
120
121 return result;
122}

References HidePixel, m_type, Pixel, TypeCount, and typesInUi.

◆ listOfUnitNameForUi()

QStringList KoUnit::listOfUnitNameForUi ( ListOptions listOptions = ListAll)
static

Returns the list of unit types for the UI, controlled with the given listOptions.

Definition at line 69 of file KoUnit.cpp.

70{
71 QStringList lst;
72 for (int i = 0; i < KoUnit::TypeCount; ++i) {
73 const Type type = typesInUi[i];
74 if ((type != Pixel) || ((listOptions & HideMask) == ListAll))
75 lst.append(unitDescription(type));
76 }
77 return lst;
78}
static QString unitDescription(KoUnit::Type type)
Get the description string of the given unit.
Definition KoUnit.cpp:32

References HideMask, ListAll, Pixel, type(), TypeCount, typesInUi, and unitDescription().

◆ operator=()

KoUnit & KoUnit::operator= ( Type unit)
inline

Definition at line 108 of file KoUnit.h.

108 {
109 m_type = unit; m_pixelConversion = 1.0; return *this;
110 }

◆ operator==()

bool KoUnit::operator== ( const KoUnit & other) const
inline

Definition at line 112 of file KoUnit.h.

112 {
113 return m_type == other.m_type &&
114 (m_type != Pixel ||
116 }
static bool qFuzzyCompare(half p1, half p2)

References m_pixelConversion, m_type, and qFuzzyCompare().

◆ parseAngle()

qreal KoUnit::parseAngle ( const QString & value,
qreal defaultVal = 0.0 )
static

parse an angle to its value in degrees

Definition at line 352 of file KoUnit.cpp.

353{
354 if (_value.isEmpty())
355 return defaultVal;
356
357 QString value(_value.simplified());
358 value.remove(QLatin1Char(' '));
359
360 int firstLetter = -1;
361 for (int i = 0; i < value.length(); ++i) {
362 if (value.at(i).isLetter()) {
363 if (value.at(i) == QLatin1Char('e'))
364 continue;
365 firstLetter = i;
366 break;
367 }
368 }
369
370 if (firstLetter == -1)
371 return value.toDouble();
372
373 const QString type = value.mid(firstLetter);
374 value.truncate(firstLetter);
375 const qreal val = value.toDouble();
376
377 if (type == QLatin1String("deg"))
378 return val;
379 else if (type == QLatin1String("rad"))
380 return val * 180 / M_PI;
381 else if (type == QLatin1String("grad"))
382 return val * 0.9;
383
384 return defaultVal;
385}
#define M_PI
Definition kis_global.h:111

References M_PI, type(), and value().

◆ parseValue()

qreal KoUnit::parseValue ( const QString & value,
qreal defaultVal = 0.0 )
static

parse common Krita and Odf values, like "10cm", "5mm" to pt

Definition at line 229 of file KoUnit.cpp.

230{
231 if (_value.isEmpty())
232 return defaultVal;
233
234 QString value(_value.simplified());
235 value.remove(QLatin1Char(' '));
236
237 int firstLetter = -1;
238 for (int i = 0; i < value.length(); ++i) {
239 if (value.at(i).isLetter()) {
240 if (value.at(i) == QLatin1Char('e'))
241 continue;
242 firstLetter = i;
243 break;
244 }
245 }
246
247 if (firstLetter == -1)
248 return value.toDouble();
249
250 const QString symbol = value.mid(firstLetter);
251 value.truncate(firstLetter);
252 const qreal val = value.toDouble();
253
254 if (symbol == QLatin1String("pt"))
255 return val;
256
257 bool ok;
259 if (ok)
260 return u.fromUserValue(val);
261
262 if (symbol == QLatin1String("m"))
263 return DM_TO_POINT(val * 10.0);
264 else if (symbol == QLatin1String("km"))
265 return DM_TO_POINT(val * 10000.0);
266
267 // TODO : add support for mi/ft ?
268 return defaultVal;
269}
qreal u
static KoUnit fromSymbol(const QString &symbol, bool *ok=0)
Definition KoUnit.cpp:271

References DM_TO_POINT(), fromSymbol(), symbol(), u, and value().

◆ setFactor()

void KoUnit::setFactor ( qreal factor)
inline

Definition at line 122 of file KoUnit.h.

122 {
123 m_pixelConversion = factor;
124 }

◆ symbol()

QString KoUnit::symbol ( ) const

Get the symbol string of the unit.

Definition at line 347 of file KoUnit.cpp.

348{
349 return QLatin1String(unitNameList[m_type]);
350}

References m_type, and unitNameList.

◆ toString()

QString KoUnit::toString ( ) const
inline

Definition at line 189 of file KoUnit.h.

189 {
190 return symbol();
191 }

◆ toUserStringValue()

QString KoUnit::toUserStringValue ( qreal ptValue) const

This method is the one to use to display a value in a dialog

Returns
the value ptValue converted the unit and rounded, ready to be displayed

Definition at line 196 of file KoUnit.cpp.

197{
198 return QLocale().toString(toUserValue(ptValue));
199}
qreal toUserValue(qreal ptValue, bool rounding=true) const
Definition KoUnit.cpp:186

References toUserValue().

◆ toUserValue()

qreal KoUnit::toUserValue ( qreal ptValue,
bool rounding = true ) const

Convert the value ptValue with or with rounding as indicated by rounding. This method is a proxy to toUserValuePrecise and toUserValueRounded.

Returns
the value ptValue converted to unit

Definition at line 186 of file KoUnit.cpp.

187{
188 if (rounding) {
189 return toUserValueRounded(ptValue);
190 }
191 else {
192 return toUserValuePrecise(ptValue);
193 }
194}
qreal toUserValuePrecise(const qreal ptValue) const
Definition KoUnit.cpp:161
qreal toUserValueRounded(const qreal value) const
Definition KoUnit.cpp:126

References toUserValuePrecise(), and toUserValueRounded().

◆ toUserValuePrecise()

qreal KoUnit::toUserValuePrecise ( const qreal ptValue) const

Convert the value ptValue to the unit (without rounding) This method is meant to be used in complex calculations.

Returns
the converted value

Definition at line 161 of file KoUnit.cpp.

162{
163 switch (m_type) {
164 case Millimeter:
165 return POINT_TO_MM(ptValue);
166 case Centimeter:
167 return POINT_TO_CM(ptValue);
168 case Decimeter:
169 return POINT_TO_DM(ptValue);
170 case Inch:
171 return POINT_TO_INCH(ptValue);
172 case Pica:
173 return POINT_TO_PI(ptValue);
174 case Cicero:
175 return POINT_TO_CC(ptValue);
176 case Pixel:
177 return ptValue * m_pixelConversion;
178 case Point:
179 default:
180 return ptValue;
181 }
182}

References Centimeter, Cicero, Decimeter, Inch, m_pixelConversion, m_type, Millimeter, Pica, Pixel, Point, POINT_TO_CC(), POINT_TO_CM(), POINT_TO_DM(), POINT_TO_INCH(), POINT_TO_MM(), and POINT_TO_PI().

◆ toUserValueRounded()

qreal KoUnit::toUserValueRounded ( const qreal value) const

Convert the value ptValue to the unit and round it. This method is meant to be used to display a value in a dialog.

Returns
the converted and rounded value

Definition at line 126 of file KoUnit.cpp.

127{
128 qreal userValue = toUserValuePrecise(value);
129 qreal rounding = 1.0;
130
131 switch (m_type) {
132 case Pixel:
133 return userValue; // no rounding for Pixel value
134 case Millimeter:
135 rounding = MM_ROUNDING;
136 break;
137 case Centimeter:
138 rounding = CM_ROUNDING;
139 break;
140 case Decimeter:
141 rounding = DM_ROUNDING;
142 break;
143 case Inch:
144 rounding = IN_ROUNDING;
145 break;
146 case Pica:
147 rounding = PI_ROUNDING;
148 break;
149 case Cicero:
150 rounding = CC_ROUNDING;
151 break;
152 case Point:
153 default:
154 rounding = PT_ROUNDING;
155 }
156
157
158 return floor(userValue * rounding) / rounding;
159}
static const qreal IN_ROUNDING
Definition KoUnit.h:54
static const qreal PT_ROUNDING
Definition KoUnit.h:47
static const qreal MM_ROUNDING
Definition KoUnit.h:52
static const qreal CM_ROUNDING
Definition KoUnit.h:50
static const qreal DM_ROUNDING
Definition KoUnit.h:51
static const qreal CC_ROUNDING
Definition KoUnit.h:57
static const qreal PI_ROUNDING
Definition KoUnit.h:56

References CC_ROUNDING, Centimeter, Cicero, CM_ROUNDING, Decimeter, DM_ROUNDING, IN_ROUNDING, Inch, m_type, Millimeter, MM_ROUNDING, PI_ROUNDING, Pica, Pixel, Point, PT_ROUNDING, toUserValuePrecise(), and value().

◆ type()

KoUnit::Type KoUnit::type ( ) const
inline

Definition at line 118 of file KoUnit.h.

118 {
119 return m_type;
120 }

◆ unitDescription()

QString KoUnit::unitDescription ( KoUnit::Type type)
static

Get the description string of the given unit.

Definition at line 32 of file KoUnit.cpp.

33{
34 switch (type) {
36 return i18n("Millimeters (mm)");
38 return i18n("Centimeters (cm)");
40 return i18n("Decimeters (dm)");
41 case KoUnit::Inch:
42 return i18n("Inches (in)");
43 case KoUnit::Pica:
44 return i18n("Pica (pi)");
45 case KoUnit::Cicero:
46 return i18n("Cicero (cc)");
47 case KoUnit::Point:
48 return i18n("Points (pt)");
49 case KoUnit::Pixel:
50 return i18n("Pixels (px)");
51 default:
52 return i18n("Unsupported unit");
53 }
54}

References Centimeter, Cicero, Decimeter, Inch, Millimeter, Pica, Pixel, Point, and type().

Member Data Documentation

◆ m_pixelConversion

qreal KoUnit::m_pixelConversion
private

Definition at line 213 of file KoUnit.h.

◆ m_type

Type KoUnit::m_type
private

Definition at line 212 of file KoUnit.h.


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