File: ProjectSelectionManager.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 (161 lines) | stat: -rw-r--r-- 5,069 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
/**********************************************************************

Audacity: A Digital Audio Editor

ProjectSelectionManager.cpp

Paul Licameli split from ProjectManager.cpp

**********************************************************************/
#include "ProjectSelectionManager.h"

#include "Project.h"
#include "ProjectHistory.h"
#include "ProjectNumericFormats.h"
#include "ProjectRate.h"
#include "ProjectSnap.h"
#include "ProjectTimeSignature.h"
#include "Snap.h"
#include "ViewInfo.h"

static AudacityProject::AttachedObjects::RegisteredFactory
sProjectSelectionManagerKey {
   []( AudacityProject &project ) {
      return std::make_shared< ProjectSelectionManager >( project );
   }
};

ProjectSelectionManager &ProjectSelectionManager::Get(
   AudacityProject &project )
{
   return project.AttachedObjects::Get< ProjectSelectionManager >(
      sProjectSelectionManagerKey );
}

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

ProjectSelectionManager::ProjectSelectionManager(AudacityProject& project)
    : mProject { project }
    , mSnappingChangedSubscription { ProjectSnap::Get(project).Subscribe(
         [this](auto&)
         {
            SnapSelection();
         })}
    , mTimeSignatureChangedSubscription {
       ProjectTimeSignature::Get(project).Subscribe([this](auto&)
                                                    { SnapSelection(); }) }
    , mProjectRateChangedSubscription { ProjectRate::Get(project).Subscribe(
         [this](auto&) { SnapSelection(); }) } 

{
   // Be consistent with ProjectNumericFormats
   auto &formats = ProjectNumericFormats::Get(mProject);
   SetSelectionFormat(formats.GetSelectionFormat());
   SetAudioTimeFormat(formats.GetAudioTimeFormat());
   SetFrequencySelectionFormatName(formats.GetFrequencySelectionFormatName());
   SetBandwidthSelectionFormatName(formats.GetBandwidthSelectionFormatName());

   // And stay consistent
   mFormatsSubscription = ProjectNumericFormats::Get(project)
      .Subscribe(*this, &ProjectSelectionManager::OnFormatsChanged);
}

ProjectSelectionManager::~ProjectSelectionManager() = default;

void ProjectSelectionManager::SnapSelection()
{   
   auto& project = mProject;
   auto& projectSnap = ProjectSnap::Get(mProject);

   if (projectSnap.GetSnapMode() == SnapMode::SNAP_OFF)
      return;

   auto& viewInfo = ViewInfo::Get(project);
   auto& selectedRegion = viewInfo.selectedRegion;

   const double oldt0 = selectedRegion.t0();
   const double oldt1 = selectedRegion.t1();

   const double t0 = projectSnap.SnapTime(oldt0).time;
   const double t1 = projectSnap.SnapTime(oldt1).time;

   if (t0 != oldt0 || t1 != oldt1)
      selectedRegion.setTimes(t0, t1);
}

void ProjectSelectionManager::OnFormatsChanged(ProjectNumericFormatsEvent evt)
{
   auto &formats = ProjectNumericFormats::Get(mProject);
   switch (evt.type) {
   case ProjectNumericFormatsEvent::ChangedSelectionFormat:
      return SetSelectionFormat(formats.GetSelectionFormat());
   case ProjectNumericFormatsEvent::ChangedAudioTimeFormat:
      return SetAudioTimeFormat(formats.GetAudioTimeFormat());
   case ProjectNumericFormatsEvent::ChangedFrequencyFormat:
      return SetFrequencySelectionFormatName(
         formats.GetFrequencySelectionFormatName());
   case ProjectNumericFormatsEvent::ChangedBandwidthFormat:
      return SetBandwidthSelectionFormatName(
         formats.GetBandwidthSelectionFormatName());
   default:
      break;
   }
}

void ProjectSelectionManager::SetSelectionFormat(const NumericFormatID & format)
{
   gPrefs->Write(wxT("/SelectionFormat"), format);
   gPrefs->Flush();
}

void ProjectSelectionManager::SetAudioTimeFormat(const NumericFormatID & format)
{
   gPrefs->Write(wxT("/AudioTimeFormat"), format.GET());
   gPrefs->Flush();
}

void ProjectSelectionManager::ModifySelection(
   double &start, double &end, bool done)
{
   auto &project = mProject;
   auto &history = ProjectHistory::Get( project );
   auto &viewInfo = ViewInfo::Get( project );
   viewInfo.selectedRegion.setTimes(start, end);
   if (done)
      history.ModifyState(false);
}

void ProjectSelectionManager::SetFrequencySelectionFormatName(
   const NumericFormatID &formatName)
{
   gPrefs->Write(wxT("/FrequencySelectionFormatName"),
                 formatName.GET());
   gPrefs->Flush();
}

void ProjectSelectionManager::SetBandwidthSelectionFormatName(
   const NumericFormatID & formatName)
{
   gPrefs->Write(wxT("/BandwidthSelectionFormatName"), formatName.GET());
   gPrefs->Flush();
}

void ProjectSelectionManager::ModifySpectralSelection(double nyquist,
   double &bottom, double &top, bool done)
{
   auto &project = mProject;
   auto &history = ProjectHistory::Get(project);
   auto &viewInfo = ViewInfo::Get(project);
   if (bottom >= 0.0)
      bottom = std::min(nyquist, bottom);
   if (top >= 0.0)
      top = std::min(nyquist, top);
   viewInfo.selectedRegion.setFrequencies(bottom, top);
   if (done)
      history.ModifyState(false);
}