File indexing completed on 2024-06-23 05:48:51
0001 /* 0002 This file is part of the Okteta Kasten module, made within the KDE community. 0003 0004 SPDX-FileCopyrightText: 2009 Friedrich W. H. Kossebau <kossebau@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0007 */ 0008 0009 #ifndef KASTEN_UINTSPINBOX_HPP 0010 #define KASTEN_UINTSPINBOX_HPP 0011 0012 // Qt 0013 #include <QAbstractSpinBox> 0014 // C++ 0015 #include <limits> 0016 0017 class UIntSpinBox : public QAbstractSpinBox 0018 { 0019 Q_OBJECT 0020 Q_PROPERTY(quint64 value READ value WRITE setValue USER true) 0021 0022 public: 0023 explicit UIntSpinBox(QWidget* parent = nullptr, int base = 10); 0024 0025 ~UIntSpinBox() override; 0026 0027 public: 0028 quint64 value() const; 0029 0030 quint64 maximum() const; 0031 0032 public: 0033 void setValue(quint64 value); 0034 0035 void setMaximum(quint64 max); 0036 void setBase(int base); 0037 0038 static UIntSpinBox* createUInt64Spinbox(QWidget* parent = nullptr); 0039 static UIntSpinBox* createUInt32Spinbox(QWidget* parent = nullptr); 0040 static UIntSpinBox* createUInt16Spinbox(QWidget* parent = nullptr); 0041 static UIntSpinBox* createUInt8Spinbox(QWidget* parent = nullptr); 0042 0043 protected: // QAbstractSpinBox API 0044 QValidator::State validate(QString& input, int& pos) const override; 0045 void stepBy(int steps) override; 0046 void fixup(QString& input) const override; 0047 StepEnabled stepEnabled() const override; 0048 0049 private: 0050 void updateEditLine() const; 0051 0052 private: 0053 mutable quint64 mValue = 0; 0054 0055 quint64 mMaximum = std::numeric_limits<quint64>::max(); 0056 // TODO minimum 0057 int mBase = 0; 0058 0059 QString mPrefix; 0060 }; 0061 0062 inline UIntSpinBox::UIntSpinBox(QWidget* parent, int base) 0063 : QAbstractSpinBox(parent) 0064 { 0065 setBase(base); 0066 } 0067 0068 inline UIntSpinBox::~UIntSpinBox() = default; 0069 0070 inline quint64 UIntSpinBox::value() const { return mValue; } 0071 inline quint64 UIntSpinBox::maximum() const { return mMaximum; } 0072 0073 inline void UIntSpinBox::setMaximum(quint64 maximum) 0074 { 0075 if (mMaximum == maximum) { 0076 return; 0077 } 0078 0079 mMaximum = maximum; 0080 0081 if (mValue > mMaximum) { 0082 mValue = mMaximum; 0083 0084 updateEditLine(); 0085 } 0086 } 0087 0088 inline void UIntSpinBox::setValue(quint64 value) 0089 { 0090 if (value > mMaximum) { 0091 value = mMaximum; 0092 } 0093 0094 if (mValue == value) { 0095 return; 0096 } 0097 0098 mValue = value; 0099 0100 updateEditLine(); 0101 } 0102 0103 inline void UIntSpinBox::setBase(int base) 0104 { 0105 base = qBound(2, base, 36); 0106 0107 if (mBase == base) { 0108 return; 0109 } 0110 0111 mBase = base; 0112 mPrefix = QString::fromLatin1( 0113 (base == 16) ? "0x" : 0114 (base == 8) ? "0o" : 0115 (base == 2) ? "0b" : 0116 /* else */ nullptr); 0117 } 0118 0119 inline UIntSpinBox* UIntSpinBox::createUInt64Spinbox(QWidget* parent) 0120 { 0121 return new UIntSpinBox(parent); 0122 } 0123 0124 inline UIntSpinBox* UIntSpinBox::createUInt32Spinbox(QWidget* parent) 0125 { 0126 auto* ret = new UIntSpinBox(parent); 0127 ret->setMaximum(std::numeric_limits<quint32>::max()); 0128 return ret; 0129 } 0130 0131 inline UIntSpinBox* UIntSpinBox::createUInt16Spinbox(QWidget* parent) 0132 { 0133 auto* ret = new UIntSpinBox(parent); 0134 ret->setMaximum(std::numeric_limits<quint16>::max()); 0135 return ret; 0136 } 0137 0138 inline UIntSpinBox* UIntSpinBox::createUInt8Spinbox(QWidget* parent) 0139 { 0140 auto* ret = new UIntSpinBox(parent); 0141 ret->setMaximum(std::numeric_limits<quint8>::max()); 0142 return ret; 0143 } 0144 0145 #endif