File indexing completed on 2024-05-19 04:36:12

0001 /*
0002  *  SPDX-FileCopyrightText: 2015 Boudhayan Gupta <bgupta@kde.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include <KLocalizedString>
0008 #include <QtMath>
0009 
0010 #include "SmartSpinBox.h"
0011 
0012 SmartSpinBox::SmartSpinBox(QWidget *parent)
0013     : QDoubleSpinBox(parent)
0014 {
0015     connect(this, qOverload<qreal>(&SmartSpinBox::valueChanged), this, &SmartSpinBox::suffixChangeHandler);
0016 }
0017 
0018 QString SmartSpinBox::textFromValue(double val) const
0019 {
0020     if ((qFloor(val) == val) && (qCeil(val) == val)) {
0021         return QWidget::locale().toString(qint64(val));
0022     }
0023     return QWidget::locale().toString(val, 'f', decimals());
0024 }
0025 
0026 void SmartSpinBox::suffixChangeHandler(double val)
0027 {
0028     int integerSeconds = static_cast<int>(val);
0029     if (val == integerSeconds) {
0030         setSuffix(i18ncp("Integer number of seconds", " second", " seconds", integerSeconds));
0031     } else {
0032         setSuffix(i18nc("Decimal number of seconds", " seconds"));
0033     }
0034 }
0035 
0036 #include "moc_SmartSpinBox.cpp"