You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.1 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2012 Torsten Hueter, torstenhtr <at> gmx.de
  5. * Copyright (C) 2013-2015 CERN
  6. * Copyright (C) 2012-2019 KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
  9. * @author Maciej Suminski <maciej.suminski@cern.ch>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, you may find one here:
  23. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  24. * or you may search the http://www.gnu.org website for the version 2 license,
  25. * or you may write to the Free Software Foundation, Inc.,
  26. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  27. */
  28. #include <view/zoom_controller.h>
  29. #include <trace_helpers.h>
  30. #include <wx/log.h>
  31. #include <algorithm>
  32. using namespace KIGFX;
  33. /**
  34. * A very simple timestamper that uses the #KIGFX::ACCELERATING_ZOOM_CONTROLLER::CLOCK
  35. * to provide a timestamp. Since that's a steady_clock, it's monotonic.
  36. */
  37. class SIMPLE_TIMESTAMPER : public ACCELERATING_ZOOM_CONTROLLER::TIMESTAMP_PROVIDER
  38. {
  39. public:
  40. ACCELERATING_ZOOM_CONTROLLER::TIME_PT GetTimestamp() override
  41. {
  42. return ACCELERATING_ZOOM_CONTROLLER::CLOCK::now();
  43. }
  44. };
  45. ACCELERATING_ZOOM_CONTROLLER::ACCELERATING_ZOOM_CONTROLLER(
  46. double aScale, const TIMEOUT& aAccTimeout, TIMESTAMP_PROVIDER* aTimestampProv ) :
  47. m_accTimeout( aAccTimeout ),
  48. m_scale( aScale )
  49. {
  50. if( aTimestampProv )
  51. {
  52. m_timestampProv = aTimestampProv;
  53. }
  54. else
  55. {
  56. m_ownTimestampProv = std::make_unique<SIMPLE_TIMESTAMPER>();
  57. m_timestampProv = m_ownTimestampProv.get();
  58. }
  59. m_lastTimestamp = m_timestampProv->GetTimestamp();
  60. }
  61. double ACCELERATING_ZOOM_CONTROLLER::GetScaleForRotation( int aRotation )
  62. {
  63. // The minimal step value when changing the current zoom level
  64. const double minStep = 1.05;
  65. const auto timestamp = m_timestampProv->GetTimestamp();
  66. auto timeDiff = std::chrono::duration_cast<TIMEOUT>( timestamp - m_lastTimestamp );
  67. m_lastTimestamp = timestamp;
  68. wxLogTrace( traceZoomScroll,
  69. wxString::Format( "Rot %d, time diff: %ldms", aRotation, (long)timeDiff.count() ) );
  70. double zoomScale;
  71. // Set scaling speed depending on scroll wheel event interval
  72. if( timeDiff < m_accTimeout )
  73. {
  74. zoomScale = ( 2.05 * m_scale / 5.0 ) - timeDiff / m_accTimeout;
  75. // be sure zoomScale value is significant
  76. zoomScale = std::max( zoomScale, minStep );
  77. if( aRotation < 0 )
  78. zoomScale = 1.0 / zoomScale;
  79. }
  80. else
  81. {
  82. zoomScale = ( aRotation > 0 ) ? minStep : 1 / minStep;
  83. }
  84. wxLogTrace( traceZoomScroll, wxString::Format( " Zoom factor: %f", zoomScale ) );
  85. return zoomScale;
  86. }
  87. CONSTANT_ZOOM_CONTROLLER::CONSTANT_ZOOM_CONTROLLER( double aScale ) : m_scale( aScale )
  88. {
  89. }
  90. double CONSTANT_ZOOM_CONTROLLER::GetScaleForRotation( int aRotation )
  91. {
  92. wxLogTrace( traceZoomScroll, wxString::Format( "Rot %d", aRotation ) );
  93. aRotation = ( aRotation > 0 ) ? std::min( aRotation, 100 ) : std::max( aRotation, -100 );
  94. double dscale = aRotation * m_scale;
  95. double zoom_scale = ( aRotation > 0 ) ? ( 1 + dscale ) : 1 / ( 1 - dscale );
  96. wxLogTrace( traceZoomScroll, wxString::Format( " Zoom factor: %f", zoom_scale ) );
  97. return zoom_scale;
  98. }
  99. // need these until C++17
  100. constexpr ACCELERATING_ZOOM_CONTROLLER::TIMEOUT ACCELERATING_ZOOM_CONTROLLER::DEFAULT_TIMEOUT;
  101. constexpr double CONSTANT_ZOOM_CONTROLLER::MAC_SCALE;
  102. constexpr double CONSTANT_ZOOM_CONTROLLER::GTK3_SCALE;
  103. constexpr double CONSTANT_ZOOM_CONTROLLER::MSW_SCALE;