Audacity 3.2.0
NumericConverter.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 NumericConverter.h
6
7 Dominic Mazzoni
8
9 See NumericConverter.cpp for documentation on how to use the
10 format string to specify how a NumericTextCtrl's fields are
11 laid out.
12
13 Paul Licameli split from NumericTextCtrl.h
14
15**********************************************************************/
16#ifndef __AUDACITY_NUMERIC_CONVERTER__
17#define __AUDACITY_NUMERIC_CONVERTER__
18
19#include <memory>
20#include <numeric>
21
25
27#include "TranslatableString.h"
28
29struct FormatChangedToFitValueMessage final { double value; };
30
31class NUMERIC_FORMATS_API NumericConverter /* not final */ :
32 public Observer::Publisher<FormatChangedToFitValueMessage>
33{
34public:
36 const NumericFormatID & formatName = {},
37 double value = 0.0f);
38
39 virtual ~NumericConverter();
40
41 // ValueToControls() formats a raw value (either provided as
42 // argument, or mValue, depending on the version of the function
43 // called). The result is stored to mValueString.
44 virtual void ValueToControls();
45 virtual void ValueToControls(double rawValue, bool nearest = true);
46
47 // Converts the stored formatted string (mValueString) back to a
48 // raw value (mValue).
49 virtual void ControlsToValue();
50
51private:
52 bool ParseFormatString(const TranslatableString & untranslatedFormat);
53
54public:
55 // returns true if the format type really changed:
56 bool SetTypeAndFormatName(const NumericConverterType& type, const NumericFormatID& formatName);
57 // returns true if the format name really changed:
58 bool SetFormatName(const NumericFormatID &formatName);
59 // Could be empty if custom format is used
60 NumericFormatID GetFormatName() const;
61
62 bool SetCustomFormat(const TranslatableString& customFormat);
63
64 void SetValue(double newValue);
65 void SetMinValue(double minValue);
66 void ResetMinValue();
67 void SetMaxValue(double maxValue);
68 void ResetMaxValue();
69
70 double GetValue();
71 wxString GetString();
72
73 void UpdateFormatToFit(double value);
74
75 // Adjust the value by the number "steps" in the active format.
76 // Increment if "dir" is 1, decrement if "dir" is -1.
77 void Adjust(int steps, int dir, int focusedDigit);
78
79 void Increment(int focusedDigit = -1);
80 void Decrement(int focusedDigit = -1);
81
82protected:
83 bool UpdateFormatter();
84 virtual void OnFormatUpdated(bool resetFocus);
85
87
89
90 double mMinValue { 0.0 };
91 double mMaxValue { std::numeric_limits<double>::max() };
92 double mInvalidValue { -1 };
93
94 double mValue { mInvalidValue };
95
96 std::unique_ptr<NumericConverterFormatter>
98
101
102 // Formatted mValue, by ValueToControls().
103 wxString mValueString;
104 std::vector<wxString> mFieldValueStrings;
105
107
108private:
109 int GetSafeFocusedDigit(int focusedDigit) const noexcept;
110};
111#endif // __AUDACITY_NUMERIC_CONVERTER__
A context in which formatter operates.
An explicitly nonlocalized string, not meant for the user to see.
Definition: Identifier.h:22
NumericConverter provides the advanced formatting control used in the selection bar of Audacity.
NumericConverterType mType
std::vector< wxString > mFieldValueStrings
FormatterContext mContext
std::unique_ptr< NumericConverterFormatter > mFormatter
TranslatableString mCustomFormat
Observer::Subscription mFormatUpdatedSubscription
NumericFormatID mFormatID
An object that sends messages to an open-ended list of subscribed callbacks.
Definition: Observer.h:108
A move-only handle representing a connection to a Publisher.
Definition: Observer.h:70
Holds a msgid for the translation catalog; may also bind format arguments.