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

#include <KoUnitDoubleSpinBox.h>

+ Inheritance diagram for KoUnitDoubleSpinBox:

Signals

void valueChangedPt (qreal)
 emitted like valueChanged in the parent, but this one emits the point value
 

Public Member Functions

virtual void changeValue (double newValue)
 
 KoUnitDoubleSpinBox (QWidget *parent=0)
 
 Private (double low, double up, double step)
 
void setLineStep (double step)
 Set step size in the current unit.
 
void setLineStepPt (double step)
 Set step size in points.
 
void setMaximum (double max)
 Set maximum value in points.
 
void setMinimum (double min)
 Set minimum value in points.
 
void setMinMaxStep (double min, double max, double step)
 Set minimum, maximum value and the step size (all in points)
 
virtual void setUnit (const KoUnit &)
 
QString textFromValue (double value) const override
 
QValidator::State validate (QString &input, int &pos) const override
 reimplemented from superclass, will forward to KoUnitDoubleValidator
 
double value () const
 
double valueFromText (const QString &str) const override
 
 ~KoUnitDoubleSpinBox () override
 

Public Attributes

double lowerInPoints
 lowest value in points
 
double stepInPoints
 step in points
 
KoUnit unit
 
double upperInPoints
 highest value in points
 

Private Slots

void privateValueChanged ()
 

Private Attributes

Private *const d
 
- Private Attributes inherited from Private
KisCanvas2canvas
 
int displayedFrame
 
int intendedFrame
 

Additional Inherited Members

- Private Member Functions inherited from Private
 Private (KisCanvas2 *c)
 

Detailed Description

Spin box for double precision numbers with unit display. Use this widget for any value that represents a real measurable value for consistency throughout Krita. This widget shows the value in the user-selected units (inch, millimeters, etc) but keeps the Krita-widget default measurement unit internally. This has the advantage that just setting and getting a value will not change the value due to conversions. The KoDocument class has a unit() method for consistent (document wide) configuration of the used unit. It is advised to use a QDoubleSpinBox in QtDesigner and then use the context-menu item: 'Promote to Custom Widget' and use the values: 'classname=KoUnitDoubleSpinBox', 'headerfile=KoUnitDoubleSpinBox.h' This will generate code that uses this spinbox in the correct manner.

This class need to be replaced as much as possible with

See also
KisDoubleParseUnitSpinBox to add math parsing ability.

Definition at line 23 of file KoUnitDoubleSpinBox.cpp.

Constructor & Destructor Documentation

◆ KoUnitDoubleSpinBox()

KoUnitDoubleSpinBox::KoUnitDoubleSpinBox ( QWidget * parent = 0)
explicit

Constructor Create a new spinBox with very broad range predefined. This spinbox will have min and max borders of 10000 points and use the default unit of points.

Parameters
parentthe parent widget

Definition at line 40 of file KoUnitDoubleSpinBox.cpp.

41 : QDoubleSpinBox( parent ),
42 d( new Private(-9999, 9999, 1))
43{
44 QDoubleSpinBox::setDecimals( 2 );
45 //setAcceptLocalizedNumbers( true );
47 setAlignment( Qt::AlignRight );
48
49 connect(this, SIGNAL(valueChanged(double)), SLOT(privateValueChanged()));
50}
virtual void setUnit(const KoUnit &)
@ Point
Postscript point, 1/72th of an Inco.
Definition KoUnit.h:76

References KoUnit::Point, privateValueChanged(), and setUnit().

◆ ~KoUnitDoubleSpinBox()

KoUnitDoubleSpinBox::~KoUnitDoubleSpinBox ( )
override

Definition at line 52 of file KoUnitDoubleSpinBox.cpp.

53{
54 delete d;
55}

References d.

Member Function Documentation

◆ changeValue()

void KoUnitDoubleSpinBox::changeValue ( double newValue)
virtual

Set the new value in points which will then be converted to the current unit for display

Parameters
newValuethe new value
See also
value()

Definition at line 113 of file KoUnitDoubleSpinBox.cpp.

114{
115 QDoubleSpinBox::setValue( d->unit.toUserValue( val ) );
116 // TODO: Q_EMIT valueChanged ONLY if the value was out-of-bounds
117 // This will allow the 'user' dialog to set a dirty bool and ensure
118 // a proper value is getting saved.
119}

References d.

◆ Private()

KoUnitDoubleSpinBox::Private ( double low,
double up,
double step )
inline

Definition at line 26 of file KoUnitDoubleSpinBox.cpp.

27 : lowerInPoints(low),
28 upperInPoints(up),
29 stepInPoints(step),
31 {
32 }
double stepInPoints
step in points
double lowerInPoints
lowest value in points
double upperInPoints
highest value in points

◆ privateValueChanged

void KoUnitDoubleSpinBox::privateValueChanged ( )
privateslot

Definition at line 121 of file KoUnitDoubleSpinBox.cpp.

121 {
122 Q_EMIT valueChangedPt( value () );
123}
void valueChangedPt(qreal)
emitted like valueChanged in the parent, but this one emits the point value

References value(), and valueChangedPt().

◆ setLineStep()

void KoUnitDoubleSpinBox::setLineStep ( double step)

Set step size in the current unit.

Definition at line 163 of file KoUnitDoubleSpinBox.cpp.

164{
165 d->stepInPoints = KoUnit(KoUnit::Point).toUserValue(step);
166 QDoubleSpinBox::setSingleStep( step );
167}
qreal toUserValue(qreal ptValue, bool rounding=true) const
Definition KoUnit.cpp:186

References d, KoUnit::Point, and KoUnit::toUserValue().

◆ setLineStepPt()

void KoUnitDoubleSpinBox::setLineStepPt ( double step)

Set step size in points.

Definition at line 169 of file KoUnitDoubleSpinBox.cpp.

170{
171 d->stepInPoints = step;
172 QDoubleSpinBox::setSingleStep( d->unit.toUserValue( step ) );
173}

References d.

◆ setMaximum()

void KoUnitDoubleSpinBox::setMaximum ( double max)

Set maximum value in points.

Definition at line 157 of file KoUnitDoubleSpinBox.cpp.

158{
159 d->upperInPoints = max;
160 QDoubleSpinBox::setMaximum( d->unit.toUserValue( max ) );
161}
constexpr std::enable_if< sizeof...(values)==0, size_t >::type max()

References d.

◆ setMinimum()

void KoUnitDoubleSpinBox::setMinimum ( double min)

Set minimum value in points.

Definition at line 151 of file KoUnitDoubleSpinBox.cpp.

152{
153 d->lowerInPoints = min;
154 QDoubleSpinBox::setMinimum( d->unit.toUserValue( min ) );
155}
T min(T a, T b, T c)

References d.

◆ setMinMaxStep()

void KoUnitDoubleSpinBox::setMinMaxStep ( double min,
double max,
double step )

Set minimum, maximum value and the step size (all in points)

Definition at line 175 of file KoUnitDoubleSpinBox.cpp.

176{
177 setMinimum( min );
178 setMaximum( max );
179 setLineStepPt( step );
180}
void setMaximum(double max)
Set maximum value in points.
void setLineStepPt(double step)
Set step size in points.
void setMinimum(double min)
Set minimum value in points.

References setLineStepPt(), setMaximum(), and setMinimum().

◆ setUnit()

void KoUnitDoubleSpinBox::setUnit ( const KoUnit & unit)
virtual

This spinbox shows the internal value after a conversion to the unit set here.

Definition at line 125 of file KoUnitDoubleSpinBox.cpp.

126{
127 if (unit == d->unit) return;
128
129 double oldvalue = d->unit.fromUserValue( QDoubleSpinBox::value() );
130 QDoubleSpinBox::setMinimum( unit.toUserValue( d->lowerInPoints ) );
131 QDoubleSpinBox::setMaximum( unit.toUserValue( d->upperInPoints ) );
132
133 qreal step = unit.toUserValue( d->stepInPoints );
134
135 if (unit.type() == KoUnit::Pixel) {
136 // limit the pixel step by 1.0
137 step = qMax(qreal(1.0), step);
138 }
139
140 QDoubleSpinBox::setSingleStep( step );
141 d->unit = unit;
142 QDoubleSpinBox::setValue(unit.toUserValuePrecise(oldvalue));
143 setSuffix(unit.symbol().prepend(QLatin1Char(' ')));
144}
KoUnit::Type type() const
Definition KoUnit.h:118
qreal toUserValuePrecise(const qreal ptValue) const
Definition KoUnit.cpp:161
QString symbol() const
Get the symbol string of the unit.
Definition KoUnit.cpp:347
@ Pixel
Definition KoUnit.h:82

References d, KoUnit::Pixel, KoUnit::symbol(), KoUnit::toUserValue(), KoUnit::toUserValuePrecise(), KoUnit::type(), and unit.

◆ textFromValue()

QString KoUnitDoubleSpinBox::textFromValue ( double value) const
override

Transform the double in a nice text, using locale symbols

Parameters
valuethe number as double
Returns
the resulting string

Definition at line 182 of file KoUnitDoubleSpinBox.cpp.

183{
184 //debugWidgets <<"textFromValue:" << QString::number( value, 'f', 12 ) <<" =>" << num;
185 //const QString num(QString("%1%2").arg(QLocale().toString(value, d->precision ), m_unit.symbol()));
186 //const QString num ( QString( "%1").arg( QLocale().toString( value, d->precision )) );
187 return QLocale().toString( value, 'f', decimals() );
188}

References value().

◆ validate()

QValidator::State KoUnitDoubleSpinBox::validate ( QString & input,
int & pos ) const
override

reimplemented from superclass, will forward to KoUnitDoubleValidator

Definition at line 57 of file KoUnitDoubleSpinBox.cpp.

58{
59#ifdef DEBUG_VALIDATOR
60 debugWidgets <<"KoUnitDoubleSpinBox::validate :" << input <<" at" << pos;
61#else
62 Q_UNUSED(pos);
63#endif
64
65 QRegularExpression regexp ("([ a-zA-Z]+)$"); // Letters or spaces at end
66 QRegularExpressionMatch match;
67 const int res = input.indexOf(regexp, 0, &match);
68
69 if ( res == -1 )
70 {
71 // Nothing like an unit? The user is probably editing the unit
72#ifdef DEBUG_VALIDATOR
73 debugWidgets <<"Intermediate (no unit)";
74#endif
75 return QValidator::Intermediate;
76 }
77
78 // ### TODO: are all the QString::trimmed really necessary?
79 const QString number ( input.left( res ).trimmed() );
80 const QString unitName ( match.captured( 1 ).trimmed().toLower() );
81
82#ifdef DEBUG_VALIDATOR
83 debugWidgets <<"Split:" << number <<":" << unitName <<":";
84#endif
85
86 const double value = valueFromText( number );
87 double newVal = 0.0;
88 if (!qIsNaN(value)) {
89 bool ok;
90 const KoUnit unit = KoUnit::fromSymbol(unitName, &ok);
91 if ( ok )
92 newVal = unit.fromUserValue( value );
93 else
94 {
95 // Probably the user is trying to edit the unit
96#ifdef DEBUG_VALIDATOR
97 debugWidgets <<"Intermediate (unknown unit)";
98#endif
99 return QValidator::Intermediate;
100 }
101 }
102 else
103 {
104 warnWidgets << "Not a number: " << number;
105 return QValidator::Invalid;
106 }
107 newVal = d->unit.toUserValuePrecise(newVal);
108 //input = textFromValue( newVal ); // don't overwrite for now; the effect is not exactly what I expect...
109
110 return QValidator::Acceptable;
111}
#define warnWidgets
#define debugWidgets
double valueFromText(const QString &str) const override
qreal fromUserValue(qreal value) const
Definition KoUnit.cpp:201
static KoUnit fromSymbol(const QString &symbol, bool *ok=0)
Definition KoUnit.cpp:271

References d, debugWidgets, KoUnit::fromSymbol(), KoUnit::fromUserValue(), unit, value(), valueFromText(), and warnWidgets.

◆ value()

double KoUnitDoubleSpinBox::value ( ) const
Returns
the current value, converted in points

Definition at line 146 of file KoUnitDoubleSpinBox.cpp.

147{
148 return d->unit.fromUserValue( QDoubleSpinBox::value() );
149}

References d.

◆ valueChangedPt

void KoUnitDoubleSpinBox::valueChangedPt ( qreal )
signal

emitted like valueChanged in the parent, but this one emits the point value

◆ valueFromText()

double KoUnitDoubleSpinBox::valueFromText ( const QString & str) const
override

Transform a string into a double, while taking care of locale specific symbols.

Parameters
strthe string to transform into a number
Returns
the value as double

Definition at line 190 of file KoUnitDoubleSpinBox.cpp.

191{
192 QString str2( str );
193 str2.remove(d->unit.symbol());
194 return QLocale().toDouble(str2);
195// QString str2( str );
196// /* KLocale::readNumber wants the thousand separator exactly at 1000.
197// But when editing, it might be anywhere. So we need to remove it. */
198// const QString sep( KGlobal::locale()->thousandsSeparator() );
199// if ( !sep.isEmpty() )
200// str2.remove( sep );
201// str2.remove(d->unit.symbol());
202// bool ok;
203// const double dbl = KGlobal::locale()->readNumber( str2, &ok );
204//#ifdef DEBUG_VALUEFROMTEXT
205// if ( ok )
206// debugWidgets <<"valueFromText:" << str <<": => :" << str2 <<": =>" << QString::number( dbl, 'f', 12 );
207// else
208// warnWidgets << "valueFromText error:" << str << ": => :" << str2 << ":";
209//#endif
210// return dbl;
211}

References d.

Member Data Documentation

◆ d

Private* const KoUnitDoubleSpinBox::d
private

Definition at line 98 of file KoUnitDoubleSpinBox.h.

◆ lowerInPoints

double KoUnitDoubleSpinBox::lowerInPoints

lowest value in points

Definition at line 34 of file KoUnitDoubleSpinBox.cpp.

◆ stepInPoints

double KoUnitDoubleSpinBox::stepInPoints

step in points

Definition at line 36 of file KoUnitDoubleSpinBox.cpp.

◆ unit

KoUnit KoUnitDoubleSpinBox::unit

Definition at line 37 of file KoUnitDoubleSpinBox.cpp.

◆ upperInPoints

double KoUnitDoubleSpinBox::upperInPoints

highest value in points

Definition at line 35 of file KoUnitDoubleSpinBox.cpp.


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