Audacity 3.2.0
NumericConverterFormatter.cpp
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/**********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 @file NumericConverterFormatter.cpp
7
8 Dmitry Vedenko
9
10 **********************************************************************/
12
13#include <cmath>
14
15namespace
16{
17size_t CalculateDigits(size_t rangeEnd)
18{
19 if (rangeEnd == 0)
20 return 0;
21
22 --rangeEnd;
23
24 size_t digitsCount = 0;
25
26 while (rangeEnd > 0)
27 {
28 rangeEnd /= 10;
29 ++digitsCount;
30 }
31
32 return digitsCount;
33}
34}
35
36NumericField::NumericField(size_t _digits, bool zeropad)
37 : digits { _digits }
38{
39 if (zeropad && digits > 1)
40 formatStr.Printf(wxT("%%0%zud"), digits); // ex. "%03d" if digits is 3
41 else
42 formatStr = "%d";
43}
44
45NumericField NumericField::ForRange(size_t range, bool zeropad, size_t minDigits)
46{
47 // Previously, Audacity used 5 digits by default (why?)
48 return NumericField(
49 range > 1 ? std::max(minDigits, CalculateDigits(range)) : 5, zeropad);
50}
51
52NumericField NumericField::WithDigits(size_t digits, bool zeropad)
53{
54 return NumericField(digits, zeropad);
55}
56
58{
59}
60
62{
63}
64
66{
67 return mPrefix;
68}
69
71{
72 return mFields;
73}
74
76{
77 return mDigits;
78}
wxT("CloseDown"))
std::vector< NumericField > NumericFields
std::vector< DigitInfo > DigitInfos
const NumericFields & GetFields() const
const wxString & GetPrefix() const
const DigitInfos & GetDigitInfos() const
virtual void UpdateFormatForValue(double value, bool canShrink)
Potentially updates the format so it can fit the value. Default implementation is empty.
static NumericField WithDigits(size_t digits, bool zeropad=true)
static NumericField ForRange(size_t range, bool zeropad=true, size_t minDigits=0)
NumericField(size_t digits, bool zeropad)