Krita Source Code Documentation
Loading...
Searching...
No Matches
KisSpinBoxI18nHelper.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2021-2023 Alvin Wong <alvin@alvinhc.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
8
9#include <QDebug>
10#include <QSpinBox>
11
13{
14 namespace
15 {
16 const char *const HANDLER_PROPERTY_NAME = "_kis_KisSpinBoxI18nHelper_handler";
17
18 struct HandlerWrapper
19 {
20 std::function<void(int)> m_handler;
21
22 HandlerWrapper() {}
23
24 explicit HandlerWrapper(std::function<void(int)> handler)
25 : m_handler(handler)
26 {}
27 };
28
29 } /* namespace */
30
31} /* namespace KisSpinBoxI18nHelper */
32
33Q_DECLARE_METATYPE(KisSpinBoxI18nHelper::HandlerWrapper)
34
36{
37 void install(QSpinBox *spinBox, std::function<QString(int)> messageFn)
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 }
47
48 bool update(QSpinBox *spinBox)
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 }
65
66 template<typename TSpinBox>
67 static void setTextGeneric(TSpinBox *spinBox, const QStringView textTemplate)
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 }
79
80 void setText(QSpinBox *spinBox, const QStringView textTemplate)
81 {
82 setTextGeneric(spinBox, textTemplate);
83 }
84
85 void setText(QDoubleSpinBox *spinBox, const QStringView textTemplate)
86 {
87 setTextGeneric(spinBox, textTemplate);
88 }
89
90} /* namespace KisSpinBoxI18nHelper */
float value(const T *src, size_t ch)
std::function< void(int)> m_handler
typedef void(QOPENGLF_APIENTRYP PFNGLINVALIDATEBUFFERDATAPROC)(GLuint buffer)
Q_DECLARE_METATYPE(KisPaintopLodLimitations)
void setText(QSpinBox *spinBox, const QStringView textTemplate)
void install(QSpinBox *spinBox, std::function< QString(int)> messageFn)
static void setTextGeneric(TSpinBox *spinBox, const QStringView textTemplate)
bool update(QSpinBox *spinBox)