File indexing completed on 2024-05-19 04:29:53

0001 /*
0002  * SPDX-FileCopyrightText: 2021-2023 Alvin Wong <alvin@alvinhc.com>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #ifndef KIS_SPIN_BOX_I18N_HELPER_H
0008 #define KIS_SPIN_BOX_I18N_HELPER_H
0009 
0010 #include <functional>
0011 
0012 #include <QStringView>
0013 
0014 #include "kritawidgetutils_export.h"
0015 
0016 class QDoubleSpinBox;
0017 class QSpinBox;
0018 class QString;
0019 
0020 class KisSelectionPropertySliderBase;
0021 
0022 namespace KisSpinBoxI18nHelper
0023 {
0024     /**
0025      * Handles pluralization of prefix/suffix of `QSpinBox`-like widgets using
0026      * an i18n string in the form of `prefix {n} suffix`. This uses the
0027      * `valueChanged` signal to automatically update the text.
0028      *
0029      * In case the `valueChanged` signal wouldn't be emitted (i.e. signals
0030      * are blocked), call `KisSpinBoxI18nHelper::update` to update the text.
0031      *
0032      * @param spinBox The QSpinBox to handle.
0033      * @param messageFn A function (usually a lambda expression) that receives
0034      *                  the current value to be shown and returns a localized
0035      *                  `QString` in the form of `prefix {n} suffix`. The
0036      *                  prefix and suffix of `spinBox` will be set accordingly.
0037      */
0038     KRITAWIDGETUTILS_EXPORT void install(QSpinBox *spinBox, std::function<QString(int)> messageFn);
0039 
0040     /**
0041      * Manually updates the prefix/suffix of a spinbox with its current value,
0042      * in case signals are blocked for the spinbox while its value is being
0043      * changed.
0044      *
0045      * @param spinBox The QSpinBox to update.
0046      */
0047     KRITAWIDGETUTILS_EXPORT bool update(QSpinBox *spinBox);
0048 
0049     /**
0050      * Set the prefix/suffix of a `QSpinbox`-like widget using an i18n string
0051      * in the form of `prefix {n} suffix`. This is only done once immediately.
0052      * If plural handling is required, use `install` instead.
0053      *
0054      * @param spinbox The `QSpinBox` to set prefix/suffix on.
0055      * @param textTemplate The text in the form of `prefix{n}suffix`, usually
0056      *                     passed through `i18n` or `i18nc`.
0057      */
0058     KRITAWIDGETUTILS_EXPORT void setText(QSpinBox *spinBox, QStringView textTemplate);
0059 
0060     /**
0061      * Set the prefix/suffix of a `QDoubleSpinbox`-like widget using an i18n
0062      * string in the form of `prefix {n} suffix`. This is only done once
0063      * immediately.
0064      *
0065      * @param spinbox The `QDoubleSpinBox` to set prefix/suffix on.
0066      * @param textTemplate The text in the form of `prefix{n}suffix`, usually
0067      *                     passed through `i18n` or `i18nc`.
0068      */
0069     KRITAWIDGETUTILS_EXPORT void setText(QDoubleSpinBox *spinBox, QStringView textTemplate);
0070 
0071     /**
0072     * **Deleted overload** - KisSelectionPropertySlider contains special handling
0073     * to switch its prefix/suffix internally. Do not use `KisSpinBoxI18nHelper::setText`
0074     * to directly set the prefix/suffix. Use `KisSelectionPropertySliderBase::setTextTemplates`
0075     * instead.
0076     */
0077     void setText(KisSelectionPropertySliderBase *spinBox, const QStringView textTemplate) = delete;
0078 
0079 } /* namespace KisSpinBoxI18nHelper */
0080 
0081 #endif /* KIS_SPIN_BOX_I18N_HELPER_H */