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}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
virtual void setUnit(const KoUnit &)
@ Point
Postscript point, 1/72th of an Inco.
Definition KoUnit.h:76

References connect(), 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 112 of file KoUnitDoubleSpinBox.cpp.

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

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 120 of file KoUnitDoubleSpinBox.cpp.

120 {
121 Q_EMIT valueChangedPt( value () );
122}
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 162 of file KoUnitDoubleSpinBox.cpp.

163{
164 d->stepInPoints = KoUnit(KoUnit::Point).toUserValue(step);
165 QDoubleSpinBox::setSingleStep( step );
166}
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 168 of file KoUnitDoubleSpinBox.cpp.

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

References d.

◆ setMaximum()

void KoUnitDoubleSpinBox::setMaximum ( double max)

Set maximum value in points.

Definition at line 156 of file KoUnitDoubleSpinBox.cpp.

157{
158 d->upperInPoints = max;
159 QDoubleSpinBox::setMaximum( d->unit.toUserValue( max ) );
160}
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 150 of file KoUnitDoubleSpinBox.cpp.

151{
152 d->lowerInPoints = min;
153 QDoubleSpinBox::setMinimum( d->unit.toUserValue( min ) );
154}
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 174 of file KoUnitDoubleSpinBox.cpp.

175{
176 setMinimum( min );
177 setMaximum( max );
178 setLineStepPt( step );
179}
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 124 of file KoUnitDoubleSpinBox.cpp.

125{
126 if (unit == d->unit) return;
127
128 double oldvalue = d->unit.fromUserValue( QDoubleSpinBox::value() );
129 QDoubleSpinBox::setMinimum( unit.toUserValue( d->lowerInPoints ) );
130 QDoubleSpinBox::setMaximum( unit.toUserValue( d->upperInPoints ) );
131
132 qreal step = unit.toUserValue( d->stepInPoints );
133
134 if (unit.type() == KoUnit::Pixel) {
135 // limit the pixel step by 1.0
136 step = qMax(qreal(1.0), step);
137 }
138
139 QDoubleSpinBox::setSingleStep( step );
140 d->unit = unit;
141 QDoubleSpinBox::setValue(unit.toUserValuePrecise(oldvalue));
142 setSuffix(unit.symbol().prepend(QLatin1Char(' ')));
143}
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 181 of file KoUnitDoubleSpinBox.cpp.

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

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 QRegExp regexp ("([ a-zA-Z]+)$"); // Letters or spaces at end
66 const int res = regexp.indexIn(input);
67
68 if ( res == -1 )
69 {
70 // Nothing like an unit? The user is probably editing the unit
71#ifdef DEBUG_VALIDATOR
72 debugWidgets <<"Intermediate (no unit)";
73#endif
74 return QValidator::Intermediate;
75 }
76
77 // ### TODO: are all the QString::trimmed really necessary?
78 const QString number ( input.left( res ).trimmed() );
79 const QString unitName ( regexp.cap( 1 ).trimmed().toLower() );
80
81#ifdef DEBUG_VALIDATOR
82 debugWidgets <<"Split:" << number <<":" << unitName <<":";
83#endif
84
85 const double value = valueFromText( number );
86 double newVal = 0.0;
87 if (!qIsNaN(value)) {
88 bool ok;
89 const KoUnit unit = KoUnit::fromSymbol(unitName, &ok);
90 if ( ok )
91 newVal = unit.fromUserValue( value );
92 else
93 {
94 // Probably the user is trying to edit the unit
95#ifdef DEBUG_VALIDATOR
96 debugWidgets <<"Intermediate (unknown unit)";
97#endif
98 return QValidator::Intermediate;
99 }
100 }
101 else
102 {
103 warnWidgets << "Not a number: " << number;
104 return QValidator::Invalid;
105 }
106 newVal = d->unit.toUserValuePrecise(newVal);
107 //input = textFromValue( newVal ); // don't overwrite for now; the effect is not exactly what I expect...
108
109 return QValidator::Acceptable;
110}
#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 145 of file KoUnitDoubleSpinBox.cpp.

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

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 189 of file KoUnitDoubleSpinBox.cpp.

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

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: