Audacity 3.2.0
ProjectSnap.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 ProjectSnap.cpp
7
8 Dmitry Vedenko
9
10**********************************************************************/
11#include "ProjectSnap.h"
12#include "Project.h"
13#include "XMLWriter.h"
15
16#include "ProjectRate.h"
18
21 {
22 auto result = std::make_shared<ProjectSnap>(project);
23 return result;
24 }
25};
26
28{
29 return project.AttachedObjects::Get<ProjectSnap>(sKey);
30}
31
33{
34 return Get(const_cast<AudacityProject&>(project));
35}
36
38 : mProject(project)
39{}
40
42{
43 if (mSnapMode != mode)
44 {
45 mSnapMode = mode;
46
47 SnapModeSetting.WriteEnum(mSnapMode);
48 gPrefs->Flush();
49
51 }
52}
53
55{
56 return mSnapMode;
57}
58
60{
61 if (mSnapTo != snap)
62 {
63 mSnapTo = snap;
64
66 gPrefs->Flush();
67
69 }
70}
71
73{
74 return mSnapTo;
75}
76
78{
80 return { time, false };
81
83}
84
85SnapResult ProjectSnap::SingleStep(double time, bool upwards) const
86{
88 return { time, false };
89
91}
92
94 [](const AudacityProject& project, XMLWriter& xmlFile)
95 {
96 // Keep this attr for compatibility
97 auto& snapSettings = ProjectSnap::Get(project);
98 xmlFile.WriteAttr(
99 wxT("snapto"), snapSettings.GetSnapMode() != SnapMode::SNAP_OFF ?
100 wxT("on") :
101 wxT("off"));
102 }
103};
104
106 // Just a pointer to function, but needing overload resolution as non-const:
108 {
109 // PRL: The following has persisted as a per-project setting for long.
110 // Maybe that should be abandoned. Enough to save changes in the user
111 // preference file.
112 // DV: Keep the old behavior here: project only stored *off* and *nearest*
113 { "snapto",
114 [](auto& snapSettings, auto value)
115 {
116 snapSettings.SetSnapMode(
117 value.ToWString() == wxT("on") ? SnapMode::SNAP_NEAREST :
119 } },
120 }
121};
wxT("CloseDown"))
audacity::BasicSettings * gPrefs
Definition: Prefs.cpp:68
an object holding per-project preferred sample rate
static const AudacityProject::AttachedObjects::RegisteredFactory sKey
Definition: ProjectSnap.cpp:19
static ProjectFileIORegistry::AttributeWriterEntry entry
Definition: ProjectSnap.cpp:93
static ProjectFileIORegistry::AttributeReaderEntries entries
StringSetting SnapToSetting
Definition: SnapUtils.cpp:29
EnumSetting< SnapMode > SnapModeSetting
Definition: SnapUtils.cpp:31
SnapMode
Definition: SnapUtils.h:21
const auto project
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
Client code makes static instance from a factory of attachments; passes it to Get or Find as a retrie...
Definition: ClientData.h:275
An explicitly nonlocalized string, not meant for the user to see.
Definition: Identifier.h:22
const wxString & GET() const
Explicit conversion to wxString, meant to be ugly-looking and demanding of a comment why it's correct...
Definition: Identifier.h:66
CallbackReturn Publish(const SnapChangedMessage &message)
Send a message to connected callbacks.
Definition: Observer.h:207
Project snapping settings.
Definition: ProjectSnap.h:29
void SetSnapMode(SnapMode mode)
Definition: ProjectSnap.cpp:41
const AudacityProject & mProject
Definition: ProjectSnap.h:48
Identifier mSnapTo
Definition: ProjectSnap.h:51
SnapResult SnapTime(double time) const
Definition: ProjectSnap.cpp:77
ProjectSnap(const AudacityProject &project)
Definition: ProjectSnap.cpp:37
SnapResult SingleStep(double time, bool upwards) const
Definition: ProjectSnap.cpp:85
void SetSnapTo(Identifier snap)
Definition: ProjectSnap.cpp:59
SnapMode mSnapMode
Definition: ProjectSnap.h:50
static ProjectSnap & Get(AudacityProject &project)
Definition: ProjectSnap.cpp:27
Identifier GetSnapTo() const
Definition: ProjectSnap.cpp:72
SnapMode GetSnapMode() const
Definition: ProjectSnap.cpp:54
bool Write(const T &value)
Write value to config and return true if successful.
Definition: Prefs.h:259
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:25
virtual bool Flush() noexcept=0
static SnapResult Snap(const Identifier &id, const AudacityProject &project, double time, bool nearest)
Definition: SnapUtils.cpp:146
static SnapResult SingleStep(const Identifier &id, const AudacityProject &project, double time, bool upwards)
Definition: SnapUtils.cpp:158
Typically statically constructed.