KiCad PCB EDA Suite
KIGFX::ACCELERATING_ZOOM_CONTROLLER Class Reference

Class that zooms faster if scroll events happen very close together. More...

#include <zoom_controller.h>

Inheritance diagram for KIGFX::ACCELERATING_ZOOM_CONTROLLER:
KIGFX::ZOOM_CONTROLLER

Classes

class  TIMESTAMP_PROVIDER
 

Public Types

using TIMEOUT = std::chrono::milliseconds
 The type of the acceleration timeout. More...
 
using CLOCK = std::chrono::steady_clock
 The clock used for the timestamp (guaranteed to be monotonic) More...
 
using TIME_PT = std::chrono::time_point< CLOCK >
 The type of the time stamps. More...
 

Public Member Functions

 ACCELERATING_ZOOM_CONTROLLER (const TIMEOUT &aAccTimeout=DEFAULT_TIMEOUT, TIMESTAMP_PROVIDER *aTimestampProv=nullptr)
 
double GetScaleForRotation (int aRotation) override
 Gets the scale factor produced by a given mousewheel rotation. More...
 
TIMEOUT GetTimeout () const
 
void SetTimeout (const TIMEOUT &aNewTimeout)
 

Static Public Attributes

static constexpr TIMEOUT DEFAULT_TIMEOUT = std::chrono::milliseconds( 500 )
 The default timeout, after which a another scroll will not be accelerated. More...
 

Private Attributes

TIMESTAMP_PROVIDERm_timestampProv
 The timestamp provider to use (might be provided externally) More...
 
std::unique_ptr< TIMESTAMP_PROVIDERm_ownTimestampProv
 Any provider owned by this class (the default one, if used) More...
 
TIME_PT m_lastTimestamp
 The timestamp of the last event. More...
 
TIMEOUT m_accTimeout
 The timeout value. More...
 

Detailed Description

Class that zooms faster if scroll events happen very close together.

Definition at line 60 of file zoom_controller.h.

Member Typedef Documentation

using KIGFX::ACCELERATING_ZOOM_CONTROLLER::CLOCK = std::chrono::steady_clock

The clock used for the timestamp (guaranteed to be monotonic)

Definition at line 67 of file zoom_controller.h.

using KIGFX::ACCELERATING_ZOOM_CONTROLLER::TIME_PT = std::chrono::time_point<CLOCK>

The type of the time stamps.

Definition at line 70 of file zoom_controller.h.

using KIGFX::ACCELERATING_ZOOM_CONTROLLER::TIMEOUT = std::chrono::milliseconds

The type of the acceleration timeout.

Definition at line 64 of file zoom_controller.h.

Constructor & Destructor Documentation

ACCELERATING_ZOOM_CONTROLLER::ACCELERATING_ZOOM_CONTROLLER ( const TIMEOUT aAccTimeout = DEFAULT_TIMEOUT,
TIMESTAMP_PROVIDER aTimestampProv = nullptr 
)
Parameters
aAccTimeoutthe timeout - if a scroll happens within this timeframe, the zoom will be faster
aTimestampProva provider for timestamps. If null, a default will be provided, which is the main steady_clock (this is probably what you want for real usage)

Definition at line 55 of file zoom_controller.cpp.

References KIGFX::ACCELERATING_ZOOM_CONTROLLER::TIMESTAMP_PROVIDER::GetTimestamp(), m_lastTimestamp, m_ownTimestampProv, and m_timestampProv.

57  : m_accTimeout( aAccTimeout )
58 {
59  if( aTimestampProv )
60  {
61  m_timestampProv = aTimestampProv;
62  }
63  else
64  {
65  m_ownTimestampProv = std::make_unique<SIMPLE_TIMESTAMPER>();
67  }
68 
69  m_lastTimestamp = m_timestampProv->GetTimestamp();
70 }
TIME_PT m_lastTimestamp
The timestamp of the last event.
TIMESTAMP_PROVIDER * m_timestampProv
The timestamp provider to use (might be provided externally)
TIMEOUT m_accTimeout
The timeout value.
std::unique_ptr< TIMESTAMP_PROVIDER > m_ownTimestampProv
Any provider owned by this class (the default one, if used)

Member Function Documentation

double ACCELERATING_ZOOM_CONTROLLER::GetScaleForRotation ( int  aRotation)
overridevirtual

Gets the scale factor produced by a given mousewheel rotation.

Parameters
aRotationrotation of the mouse wheel (this comes from wxMouseEvent::GetWheelRotation())
Returns
the scale factor to scroll by

Implements KIGFX::ZOOM_CONTROLLER.

Definition at line 73 of file zoom_controller.cpp.

References Format(), KIGFX::ACCELERATING_ZOOM_CONTROLLER::TIMESTAMP_PROVIDER::GetTimestamp(), m_accTimeout, m_lastTimestamp, m_timestampProv, max, and traceZoomScroll.

74 {
75  // The minimal step value when changing the current zoom level
76  const double zoomLevelScale = 1.2;
77 
78  const auto timestamp = m_timestampProv->GetTimestamp();
79  auto timeDiff = std::chrono::duration_cast<TIMEOUT>( timestamp - m_lastTimestamp );
80 
81  m_lastTimestamp = timestamp;
82 
83  wxLogTrace( traceZoomScroll,
84  wxString::Format( "Rot %d, time diff: %ldms", aRotation, (long)timeDiff.count() ) );
85 
86  double zoomScale;
87 
88  // Set scaling speed depending on scroll wheel event interval
89  if( timeDiff < m_accTimeout )
90  {
91  zoomScale = 2.05 - timeDiff / m_accTimeout;
92 
93  // be sure zoomScale value is significant
94  zoomScale = std::max( zoomScale, zoomLevelScale );
95 
96  if( aRotation < 0 )
97  zoomScale = 1.0 / zoomScale;
98  }
99  else
100  {
101  zoomScale = ( aRotation > 0 ) ? zoomLevelScale : 1 / zoomLevelScale;
102  }
103 
104  wxLogTrace( traceZoomScroll, wxString::Format( " Zoom factor: %f", zoomScale ) );
105 
106  return zoomScale;
107 }
const wxChar *const traceZoomScroll
Flag to enable debug output of zoom-scrolling calculations in KIGFX::ZOOM_CONTROLLER and derivatives...
TIME_PT m_lastTimestamp
The timestamp of the last event.
TIMESTAMP_PROVIDER * m_timestampProv
The timestamp provider to use (might be provided externally)
std::chrono::milliseconds TIMEOUT
The type of the acceleration timeout.
TIMEOUT m_accTimeout
The timeout value.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
#define max(a, b)
Definition: auxiliary.h:86
TIMEOUT KIGFX::ACCELERATING_ZOOM_CONTROLLER::GetTimeout ( ) const
inline

Definition at line 101 of file zoom_controller.h.

102  {
103  return m_accTimeout;
104  }
TIMEOUT m_accTimeout
The timeout value.
void KIGFX::ACCELERATING_ZOOM_CONTROLLER::SetTimeout ( const TIMEOUT aNewTimeout)
inline

Definition at line 106 of file zoom_controller.h.

107  {
108  m_accTimeout = aNewTimeout;
109  }
TIMEOUT m_accTimeout
The timeout value.

Member Data Documentation

constexpr ACCELERATING_ZOOM_CONTROLLER::TIMEOUT ACCELERATING_ZOOM_CONTROLLER::DEFAULT_TIMEOUT = std::chrono::milliseconds( 500 )
static

The default timeout, after which a another scroll will not be accelerated.

Definition at line 73 of file zoom_controller.h.

Referenced by KIGFX::CONSTANT_ZOOM_CONTROLLER::GetScaleForRotation().

TIMEOUT KIGFX::ACCELERATING_ZOOM_CONTROLLER::m_accTimeout
private

The timeout value.

Definition at line 121 of file zoom_controller.h.

Referenced by GetScaleForRotation().

TIME_PT KIGFX::ACCELERATING_ZOOM_CONTROLLER::m_lastTimestamp
private

The timestamp of the last event.

Definition at line 119 of file zoom_controller.h.

Referenced by ACCELERATING_ZOOM_CONTROLLER(), and GetScaleForRotation().

std::unique_ptr<TIMESTAMP_PROVIDER> KIGFX::ACCELERATING_ZOOM_CONTROLLER::m_ownTimestampProv
private

Any provider owned by this class (the default one, if used)

Definition at line 116 of file zoom_controller.h.

Referenced by ACCELERATING_ZOOM_CONTROLLER().

TIMESTAMP_PROVIDER* KIGFX::ACCELERATING_ZOOM_CONTROLLER::m_timestampProv
private

The timestamp provider to use (might be provided externally)

Definition at line 113 of file zoom_controller.h.

Referenced by ACCELERATING_ZOOM_CONTROLLER(), and GetScaleForRotation().


The documentation for this class was generated from the following files: