Krita Source Code Documentation
Loading...
Searching...
No Matches
KisSpinBoxI18nHelper Namespace Reference

Functions

void install (QSpinBox *spinBox, std::function< QString(int)> messageFn)
 
void setText (KisSelectionPropertySliderBase *spinBox, const QStringView textTemplate)=delete
 
void setText (QDoubleSpinBox *spinBox, const QStringView textTemplate)
 
void setText (QSpinBox *spinBox, const QStringView textTemplate)
 
template<typename TSpinBox >
static void setTextGeneric (TSpinBox *spinBox, const QStringView textTemplate)
 
bool update (QSpinBox *spinBox)
 

Function Documentation

◆ install()

KRITAWIDGETUTILS_EXPORT void KisSpinBoxI18nHelper::install ( QSpinBox * spinBox,
std::function< QString(int)> messageFn )

Handles pluralization of prefix/suffix of QSpinBox-like widgets using an i18n string in the form of prefix {n} suffix. This uses the valueChanged signal to automatically update the text.

In case the valueChanged signal wouldn't be emitted (i.e. signals are blocked), call KisSpinBoxI18nHelper::update to update the text.

Parameters
spinBoxThe QSpinBox to handle.
messageFnA function (usually a lambda expression) that receives the current value to be shown and returns a localized QString in the form of prefix {n} suffix. The prefix and suffix of spinBox will be set accordingly.

Definition at line 37 of file KisSpinBoxI18nHelper.cpp.

38 {
39 const auto changeHandler = [messageFn, spinBox](int value) {
40 setText(spinBox, messageFn(value));
41 };
42 // Apply prefix/suffix with existing value immediately.
43 changeHandler(spinBox->value());
44 QObject::connect(spinBox, QOverload<int>::of(&QSpinBox::valueChanged), changeHandler);
45 spinBox->setProperty(HANDLER_PROPERTY_NAME, QVariant::fromValue(HandlerWrapper(changeHandler)));
46 }
float value(const T *src, size_t ch)
void setText(QSpinBox *spinBox, const QStringView textTemplate)

References setText(), and value().

◆ setText() [1/3]

void KisSpinBoxI18nHelper::setText ( KisSelectionPropertySliderBase * spinBox,
const QStringView textTemplate )
delete

Deleted overload - KisSelectionPropertySlider contains special handling to switch its prefix/suffix internally. Do not use KisSpinBoxI18nHelper::setText to directly set the prefix/suffix. Use KisSelectionPropertySliderBase::setTextTemplates instead.

◆ setText() [2/3]

KRITAWIDGETUTILS_EXPORT void KisSpinBoxI18nHelper::setText ( QDoubleSpinBox * spinBox,
QStringView textTemplate )

Set the prefix/suffix of a QDoubleSpinbox-like widget using an i18n string in the form of prefix {n} suffix. This is only done once immediately.

Parameters
spinboxThe QDoubleSpinBox to set prefix/suffix on.
textTemplateThe text in the form of prefix{n}suffix, usually passed through i18n or i18nc.

Definition at line 85 of file KisSpinBoxI18nHelper.cpp.

86 {
87 setTextGeneric(spinBox, textTemplate);
88 }
static void setTextGeneric(TSpinBox *spinBox, const QStringView textTemplate)

References setTextGeneric().

◆ setText() [3/3]

KRITAWIDGETUTILS_EXPORT void KisSpinBoxI18nHelper::setText ( QSpinBox * spinBox,
QStringView textTemplate )

Set the prefix/suffix of a QSpinbox-like widget using an i18n string in the form of prefix {n} suffix. This is only done once immediately. If plural handling is required, use install instead.

Parameters
spinboxThe QSpinBox to set prefix/suffix on.
textTemplateThe text in the form of prefix{n}suffix, usually passed through i18n or i18nc.

Definition at line 80 of file KisSpinBoxI18nHelper.cpp.

81 {
82 setTextGeneric(spinBox, textTemplate);
83 }

References setTextGeneric().

◆ setTextGeneric()

template<typename TSpinBox >
static void KisSpinBoxI18nHelper::setTextGeneric ( TSpinBox * spinBox,
const QStringView textTemplate )
static

Definition at line 67 of file KisSpinBoxI18nHelper.cpp.

68 {
69 const QLatin1String placeholder{"{n}"};
70 const qsizetype idx = textTemplate.indexOf(placeholder);
71 if (idx >= 0) {
72 spinBox->setPrefix(textTemplate.left(idx).toString());
73 spinBox->setSuffix(textTemplate.mid(idx + placeholder.size()).toString());
74 } else {
75 spinBox->setPrefix(QString());
76 spinBox->setSuffix(textTemplate.toString());
77 }
78 }

◆ update()

KRITAWIDGETUTILS_EXPORT bool KisSpinBoxI18nHelper::update ( QSpinBox * spinBox)

Manually updates the prefix/suffix of a spinbox with its current value, in case signals are blocked for the spinbox while its value is being changed.

Parameters
spinBoxThe QSpinBox to update.

Definition at line 48 of file KisSpinBoxI18nHelper.cpp.

49 {
50 const QVariant handlerVariant = spinBox->property(HANDLER_PROPERTY_NAME);
51 if (!handlerVariant.isValid()) {
52 qWarning() << "KisSpinBoxI18nHelper::update called with" << spinBox
53 << "but it does not have the property" << HANDLER_PROPERTY_NAME;
54 return false;
55 }
56 if (!handlerVariant.canConvert<HandlerWrapper>()) {
57 qWarning() << "KisSpinBoxI18nHelper::update called with" << spinBox
58 << "but its property" << HANDLER_PROPERTY_NAME << "is invalid";
59 return false;
60 }
61 const HandlerWrapper handler = handlerVariant.value<HandlerWrapper>();
62 handler.m_handler(spinBox->value());
63 return true;
64 }