File: ProjectNumericFormats.cpp

package info (click to toggle)
audacity 3.7.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 125,252 kB
  • sloc: cpp: 358,238; ansic: 75,458; lisp: 7,761; sh: 3,410; python: 1,503; xml: 1,385; perl: 854; makefile: 122
file content (163 lines) | stat: -rw-r--r-- 4,888 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**********************************************************************

 Audacity: A Digital Audio Editor

 @file ProjectNumericFormats.cpp

 Paul Licameli split from ProjectNumericFormats.cpp

 **********************************************************************/
#include "ProjectNumericFormats.h"
#include "Prefs.h"
#include "Project.h"

#include "NumericConverterFormats.h"
#include "NumericConverterFormatterContext.h"

#include "XMLAttributeValueView.h"
#include "XMLWriter.h"

static const AttachedProjectObjects::RegisteredFactory key
{
   [](AudacityProject &project)
   {
      return std::make_shared<ProjectNumericFormats>(project);
   }
};

ProjectNumericFormats &ProjectNumericFormats::Get(AudacityProject &project)
{
   return project.AttachedObjects::Get<ProjectNumericFormats&>(key);
}

const ProjectNumericFormats &ProjectNumericFormats::Get(
   const AudacityProject &project)
{
   return Get(const_cast<AudacityProject &>(project));
}

ProjectNumericFormats::ProjectNumericFormats(const AudacityProject& project)
   : mProject { project }
   , mSelectionFormat{
      gPrefs->Read(wxT("/SelectionFormat"), wxT(""))
   }
   , mFrequencySelectionFormatName{
      gPrefs->Read(wxT("/FrequencySelectionFormatName"), wxT(""))
   }
   , mBandwidthSelectionFormatName{
      gPrefs->Read(wxT("/BandwidthSelectionFormatName"), wxT(""))
   }
   , mAudioTimeFormat{
      gPrefs->Read(wxT("/AudioTimeFormat"), wxT("hh:mm:ss"))
   }
{}

ProjectNumericFormats::~ProjectNumericFormats() = default;

NumericFormatID
ProjectNumericFormats::GetFrequencySelectionFormatName() const
{
   return mFrequencySelectionFormatName;
}

void ProjectNumericFormats::SetFrequencySelectionFormatName(
   const NumericFormatID & formatName)
{
   if (mFrequencySelectionFormatName != formatName) {
      ProjectNumericFormatsEvent e{
         ProjectNumericFormatsEvent::ChangedFrequencyFormat,
         mFrequencySelectionFormatName, formatName
      };
      mFrequencySelectionFormatName = formatName;
      Publish(e);
   }
}

NumericFormatID
ProjectNumericFormats::GetBandwidthSelectionFormatName() const
{
   return mBandwidthSelectionFormatName;
}

NumericFormatSymbol ProjectNumericFormats::LookupFormat(
   const NumericConverterType& type, const wxString& identifier)
{
   return NumericConverterFormats::Lookup(
      FormatterContext::ProjectContext(mProject), type, identifier);
}

void ProjectNumericFormats::SetBandwidthSelectionFormatName(
   const NumericFormatID &formatName)
{
   if (mBandwidthSelectionFormatName != formatName) {
      ProjectNumericFormatsEvent e{
         ProjectNumericFormatsEvent::ChangedBandwidthFormat,
         mBandwidthSelectionFormatName, formatName
      };
      mBandwidthSelectionFormatName = formatName;
      Publish(e);
   }
}

void ProjectNumericFormats::SetSelectionFormat(const NumericFormatID &format)
{
   if (mSelectionFormat != format) {
      ProjectNumericFormatsEvent e{
         ProjectNumericFormatsEvent::ChangedSelectionFormat,
         mSelectionFormat, format
      };
      mSelectionFormat = format;
      Publish(e);
   }
}

NumericFormatID ProjectNumericFormats::GetSelectionFormat() const
{
   return mSelectionFormat;
}

void ProjectNumericFormats::SetAudioTimeFormat(const NumericFormatID &format)
{
   if (mAudioTimeFormat != format) {
      ProjectNumericFormatsEvent e{
         ProjectNumericFormatsEvent::ChangedAudioTimeFormat,
         mAudioTimeFormat, format
      };
      mAudioTimeFormat = format;
      Publish(e);
   }
}

NumericFormatID ProjectNumericFormats::GetAudioTimeFormat() const
{
   return mAudioTimeFormat;
}

static ProjectFileIORegistry::AttributeWriterEntry entry {
[](const AudacityProject &project, XMLWriter &xmlFile){
   auto &formats = ProjectNumericFormats::Get(project);
   xmlFile.WriteAttr(wxT("selectionformat"),
                     formats.GetSelectionFormat().GET());
   xmlFile.WriteAttr(wxT("frequencyformat"),
                     formats.GetFrequencySelectionFormatName().GET());
   xmlFile.WriteAttr(wxT("bandwidthformat"),
                     formats.GetBandwidthSelectionFormatName().GET());
}
};

static ProjectFileIORegistry::AttributeReaderEntries entries {
// Just a pointer to function, but needing overload resolution as non-const:
(ProjectNumericFormats& (*)(AudacityProject &)) &ProjectNumericFormats::Get, {
   // PRL:  The following have persisted as per-project settings for long.
   // Maybe that should be abandoned.  Enough to save changes in the user
   // preference file.
   { "selectionformat", [](auto &formats, auto value){
      formats.SetSelectionFormat(value.ToWString());
   } },
   { "frequencyformat", [](auto &formats, auto value){
      formats.SetFrequencySelectionFormatName(value.ToWString());
   } },
   { "bandwidthformat", [](auto &formats, auto value){
      formats.SetBandwidthSelectionFormatName(value.ToWString());
   } },
} };