From edd23e263465c8145dea4933d533135e2a5061b4 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Sat, 27 Oct 2018 19:47:48 -0400 Subject: [PATCH] Move AudacityProject::GetZoomOf* --- src/Menus.h | 1 + src/Project.cpp | 103 +--------------------------------- src/Project.h | 5 -- src/ViewInfo.cpp | 6 +- src/ViewInfo.h | 8 ++- src/menus/ViewMenus.cpp | 121 ++++++++++++++++++++++++++++++++++++++-- 6 files changed, 127 insertions(+), 117 deletions(-) diff --git a/src/Menus.h b/src/Menus.h index 6b4c592ea..9782fac77 100644 --- a/src/Menus.h +++ b/src/Menus.h @@ -118,6 +118,7 @@ void DoSelectSomething( AudacityProject &project ); } namespace ViewActions { +double GetZoomOfToFit( const AudacityProject &project ); void DoZoomFit( AudacityProject &project ); void DoZoomFitV( AudacityProject &project ); } diff --git a/src/Project.cpp b/src/Project.cpp index 90de2b4c9..bb9a74fb9 100644 --- a/src/Project.cpp +++ b/src/Project.cpp @@ -2951,7 +2951,7 @@ AudacityProject *AudacityProject::OpenProject( pProject->OpenFile( fileNameArg, addtohistory ); pNewProject = nullptr; if( pProject && pProject->mIsRecovered ) - pProject->Zoom( pProject->GetZoomOfToFit() ); + pProject->Zoom( ViewActions::GetZoomOfToFit( *pProject ) ); return pProject; } @@ -5679,107 +5679,6 @@ int AudacityProject::GetEstimatedRecordingMinsLeftOnDisk(long lCaptureChannels) } -double AudacityProject::GetZoomOfToFit(){ - const double end = mTracks->GetEndTime(); - const double start = mViewInfo.bScrollBeyondZero - ? std::min(mTracks->GetStartTime(), 0.0) - : 0; - const double len = end - start; - - if (len <= 0.0) - return mViewInfo.GetZoom(); - - int w; - mTrackPanel->GetTracksUsableArea(&w, NULL); - w -= 10; - return w/len; -} - -double AudacityProject::GetZoomOfSelection(){ - const double lowerBound = - std::max(mViewInfo.selectedRegion.t0(), ScrollingLowerBoundTime()); - const double denom = - mViewInfo.selectedRegion.t1() - lowerBound; - if (denom <= 0.0) - return mViewInfo.GetZoom(); - - // LL: The "-1" is just a hack to get around an issue where zooming to - // selection doesn't actually get the entire selected region within the - // visible area. This causes a problem with scrolling at end of playback - // where the selected region may be scrolled off the left of the screen. - // I know this isn't right, but until the real rounding or 1-off issue is - // found, this will have to work. - // PRL: Did I fix this? I am not sure, so I leave the hack in place. - // Fixes might have resulted from commits - // 1b8f44d0537d987c59653b11ed75a842b48896ea and - // e7c7bb84a966c3b3cc4b3a9717d5f247f25e7296 - int width; - mTrackPanel->GetTracksUsableArea(&width, NULL); - return (width - 1) / denom; -} - -double AudacityProject::GetZoomOfPreset( int preset ){ - - // Sets a limit on how far we will zoom out as a factor over zoom to fit. - const double maxZoomOutFactor = 4.0; - // Sets how many pixels we allow for one uint, such as seconds. - const double pixelsPerUnit = 5.0; - - double result = 1.0; - double zoomToFit = GetZoomOfToFit(); - switch( preset ){ - default: - case WaveTrack::kZoomDefault: - result = ZoomInfo::GetDefaultZoom(); - break; - case WaveTrack::kZoomToFit: - result = zoomToFit; - break; - case WaveTrack::kZoomToSelection: - result = GetZoomOfSelection(); - break; - case WaveTrack::kZoomMinutes: - result = pixelsPerUnit * 1.0/60; - break; - case WaveTrack::kZoomSeconds: - result = pixelsPerUnit * 1.0; - break; - case WaveTrack::kZoom5ths: - result = pixelsPerUnit * 5.0; - break; - case WaveTrack::kZoom10ths: - result = pixelsPerUnit * 10.0; - break; - case WaveTrack::kZoom20ths: - result = pixelsPerUnit * 20.0; - break; - case WaveTrack::kZoom50ths: - result = pixelsPerUnit * 50.0; - break; - case WaveTrack::kZoom100ths: - result = pixelsPerUnit * 100.0; - break; - case WaveTrack::kZoom500ths: - result = pixelsPerUnit * 500.0; - break; - case WaveTrack::kZoomMilliSeconds: - result = pixelsPerUnit * 1000.0; - break; - case WaveTrack::kZoomSamples: - result = 44100.0; - break; - case WaveTrack::kZoom4To1: - result = 44100.0 * 4; - break; - case WaveTrack::kMaxZoom: - result = ZoomInfo::GetMaxZoom(); - break; - }; - if( result < (zoomToFit/maxZoomOutFactor) ) - result = zoomToFit / maxZoomOutFactor; - return result; -} - AudacityProject::PlaybackScroller::PlaybackScroller(AudacityProject *project) : mProject(project) { diff --git a/src/Project.h b/src/Project.h index 63d957260..d62ab16ed 100644 --- a/src/Project.h +++ b/src/Project.h @@ -597,11 +597,6 @@ public: void DeleteCurrentAutoSaveFile(); public: - double GetZoomOfToFit(); - double GetZoomOfSelection(); - - double GetZoomOfPreset(int preset ); - bool IsSoloSimple() const { return mSoloPref == wxT("Simple"); } bool IsSoloNone() const { return mSoloPref == wxT("None"); } diff --git a/src/ViewInfo.cpp b/src/ViewInfo.cpp index 68745c697..98d1f0cce 100644 --- a/src/ViewInfo.cpp +++ b/src/ViewInfo.cpp @@ -87,9 +87,9 @@ bool ZoomInfo::ZoomOutAvailable() const return zoom > gMinZoom; } -double ZoomInfo::GetZoom( ){ return zoom;}; -double ZoomInfo::GetMaxZoom( ){ return gMaxZoom;}; -double ZoomInfo::GetMinZoom( ){ return gMinZoom;}; +double ZoomInfo::GetZoom( ) const { return zoom;}; +double ZoomInfo::GetMaxZoom( ) { return gMaxZoom;}; +double ZoomInfo::GetMinZoom( ) { return gMinZoom;}; void ZoomInfo::SetZoom(double pixelsPerSecond) { diff --git a/src/ViewInfo.h b/src/ViewInfo.h index d9d1d020c..49f40f412 100644 --- a/src/ViewInfo.h +++ b/src/ViewInfo.h @@ -85,12 +85,14 @@ public: static double GetDefaultZoom() { return 44100.0 / 512.0; } - // There is NO GetZoom()! - // Use TimeToPosition and PositionToTime and OffsetTimeByPixels! // Limits zoom to certain bounds void SetZoom(double pixelsPerSecond); - double GetZoom(); + + // This function should not be used to convert positions to times and back + // Use TimeToPosition and PositionToTime and OffsetTimeByPixels instead + double GetZoom() const; + static double GetMaxZoom( ); static double GetMinZoom( ); diff --git a/src/menus/ViewMenus.cpp b/src/menus/ViewMenus.cpp index b2bce37f9..8b7ffe1e5 100644 --- a/src/menus/ViewMenus.cpp +++ b/src/menus/ViewMenus.cpp @@ -14,12 +14,125 @@ // private helper classes and functions namespace { + +double GetZoomOfSelection( const AudacityProject &project ) +{ + const auto &viewInfo = project.GetViewInfo(); + const auto &trackPanel = *project.GetTrackPanel(); + + const double lowerBound = + std::max(viewInfo.selectedRegion.t0(), + project.ScrollingLowerBoundTime()); + const double denom = + viewInfo.selectedRegion.t1() - lowerBound; + if (denom <= 0.0) + return viewInfo.GetZoom(); + + // LL: The "-1" is just a hack to get around an issue where zooming to + // selection doesn't actually get the entire selected region within the + // visible area. This causes a problem with scrolling at end of playback + // where the selected region may be scrolled off the left of the screen. + // I know this isn't right, but until the real rounding or 1-off issue is + // found, this will have to work. + // PRL: Did I fix this? I am not sure, so I leave the hack in place. + // Fixes might have resulted from commits + // 1b8f44d0537d987c59653b11ed75a842b48896ea and + // e7c7bb84a966c3b3cc4b3a9717d5f247f25e7296 + int width; + trackPanel.GetTracksUsableArea(&width, NULL); + return (width - 1) / denom; +} + +double GetZoomOfPreset( const AudacityProject &project, int preset ) +{ + + // Sets a limit on how far we will zoom out as a factor over zoom to fit. + const double maxZoomOutFactor = 4.0; + // Sets how many pixels we allow for one uint, such as seconds. + const double pixelsPerUnit = 5.0; + + double result = 1.0; + double zoomToFit = ViewActions::GetZoomOfToFit( project ); + switch( preset ){ + default: + case WaveTrack::kZoomDefault: + result = ZoomInfo::GetDefaultZoom(); + break; + case WaveTrack::kZoomToFit: + result = zoomToFit; + break; + case WaveTrack::kZoomToSelection: + result = GetZoomOfSelection( project ); + break; + case WaveTrack::kZoomMinutes: + result = pixelsPerUnit * 1.0/60; + break; + case WaveTrack::kZoomSeconds: + result = pixelsPerUnit * 1.0; + break; + case WaveTrack::kZoom5ths: + result = pixelsPerUnit * 5.0; + break; + case WaveTrack::kZoom10ths: + result = pixelsPerUnit * 10.0; + break; + case WaveTrack::kZoom20ths: + result = pixelsPerUnit * 20.0; + break; + case WaveTrack::kZoom50ths: + result = pixelsPerUnit * 50.0; + break; + case WaveTrack::kZoom100ths: + result = pixelsPerUnit * 100.0; + break; + case WaveTrack::kZoom500ths: + result = pixelsPerUnit * 500.0; + break; + case WaveTrack::kZoomMilliSeconds: + result = pixelsPerUnit * 1000.0; + break; + case WaveTrack::kZoomSamples: + result = 44100.0; + break; + case WaveTrack::kZoom4To1: + result = 44100.0 * 4; + break; + case WaveTrack::kMaxZoom: + result = ZoomInfo::GetMaxZoom(); + break; + }; + if( result < (zoomToFit/maxZoomOutFactor) ) + result = zoomToFit / maxZoomOutFactor; + return result; +} + } namespace ViewActions { // exported helper functions +double GetZoomOfToFit( const AudacityProject &project ) +{ + const auto &tracks = *project.GetTracks(); + const auto &viewInfo = project.GetViewInfo(); + const auto &trackPanel = *project.GetTrackPanel(); + + const double end = tracks.GetEndTime(); + const double start = viewInfo.bScrollBeyondZero + ? std::min( tracks.GetStartTime(), 0.0) + : 0; + const double len = end - start; + + if (len <= 0.0) + return viewInfo.GetZoom(); + + int w; + trackPanel.GetTracksUsableArea(&w, NULL); + w -= 10; + return w/len; +} + void DoZoomFit(AudacityProject &project) { auto &viewInfo = project.GetViewInfo(); @@ -29,7 +142,7 @@ void DoZoomFit(AudacityProject &project) ? std::min(tracks->GetStartTime(), 0.0) : 0; - project.Zoom( project.GetZoomOfToFit() ); + project.Zoom( GetZoomOfToFit( project ) ); project.TP_ScrollWindow(start); } @@ -91,7 +204,7 @@ void OnZoomSel(const CommandContext &context) auto &project = context.project; auto &selectedRegion = project.GetViewInfo().selectedRegion; - project.Zoom( project.GetZoomOfSelection() ); + project.Zoom( GetZoomOfSelection( project ) ); project.TP_ScrollWindow(selectedRegion.t0()); } @@ -105,8 +218,8 @@ void OnZoomToggle(const CommandContext &context) // const double origWidth = GetScreenEndTime() - origLeft; // Choose the zoom that is most different to the current zoom. - double Zoom1 = project.GetZoomOfPreset( TracksPrefs::Zoom1Choice() ); - double Zoom2 = project.GetZoomOfPreset( TracksPrefs::Zoom2Choice() ); + double Zoom1 = GetZoomOfPreset( project, TracksPrefs::Zoom1Choice() ); + double Zoom2 = GetZoomOfPreset( project, TracksPrefs::Zoom2Choice() ); double Z = viewInfo.GetZoom();// Current Zoom. double ChosenZoom = fabs(log(Zoom1 / Z)) > fabs(log( Z / Zoom2)) ? Zoom1:Zoom2;