spinbox.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Licensed under GPLv2 or any later version
  2. // Refer to the license.txt file included.
  3. // Copyright 2014 Tony Wasserka
  4. // All rights reserved.
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above copyright
  12. // notice, this list of conditions and the following disclaimer in the
  13. // documentation and/or other materials provided with the distribution.
  14. // * Neither the name of the owner nor the names of its contributors may
  15. // be used to endorse or promote products derived from this software
  16. // without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. #pragma once
  30. #include <QAbstractSpinBox>
  31. #include <QtGlobal>
  32. class QVariant;
  33. /**
  34. * A custom spin box widget with enhanced functionality over Qt's QSpinBox
  35. */
  36. class CSpinBox : public QAbstractSpinBox {
  37. Q_OBJECT
  38. public:
  39. CSpinBox(QWidget* parent = nullptr);
  40. void stepBy(int steps) override;
  41. StepEnabled stepEnabled() const override;
  42. void SetValue(qint64 val);
  43. void SetRange(qint64 min, qint64 max);
  44. void SetBase(int base);
  45. void SetPrefix(const QString& prefix);
  46. void SetSuffix(const QString& suffix);
  47. void SetNumDigits(int num_digits);
  48. QValidator::State validate(QString& input, int& pos) const override;
  49. signals:
  50. void ValueChanged(qint64 val);
  51. private slots:
  52. void OnEditingFinished();
  53. private:
  54. void UpdateText();
  55. bool HasSign() const;
  56. QString TextFromValue();
  57. qint64 ValueFromText();
  58. qint64 min_value, max_value;
  59. qint64 value;
  60. QString prefix, suffix;
  61. int base;
  62. int num_digits;
  63. };