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 clock used for the timestamp (guaranteed to be monotonic). More...
 
using CLOCK = std::chrono::steady_clock
 The type of the time stamps. More...
 
using TIME_PT = std::chrono::time_point< CLOCK >
 The default timeout, after which a another scroll will not be accelerated. More...
 

Public Member Functions

 ACCELERATING_ZOOM_CONTROLLER (double aScale=DEFAULT_ACCELERATION_SCALE, const TIMEOUT &aAccTimeout=DEFAULT_TIMEOUT, TIMESTAMP_PROVIDER *aTimestampProv=nullptr)
 
double GetScaleForRotation (int aRotation) override
 Get 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 minimum step factor for accelerating controller. More...
 
static constexpr double DEFAULT_ACCELERATION_SCALE = 5.0
 

Private Attributes

TIMESTAMP_PROVIDERm_timestampProv
 < The timestamp provider to use (might be provided externally). More...
 
std::unique_ptr< TIMESTAMP_PROVIDERm_ownTimestampProv
 The timestamp of the last event. More...
 
TIME_PT m_lastTimestamp
 The timeout value. More...
 
TIMEOUT m_accTimeout
 
double m_scale
 A multiplier for the minimum zoom step size. More...
 

Detailed Description

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

Definition at line 61 of file zoom_controller.h.

Member Typedef Documentation

◆ CLOCK

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

The type of the time stamps.

Definition at line 68 of file zoom_controller.h.

◆ TIME_PT

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

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

Definition at line 71 of file zoom_controller.h.

◆ TIMEOUT

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

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

Definition at line 65 of file zoom_controller.h.

Constructor & Destructor Documentation

◆ ACCELERATING_ZOOM_CONTROLLER()

ACCELERATING_ZOOM_CONTROLLER::ACCELERATING_ZOOM_CONTROLLER ( double  aScale = DEFAULT_ACCELERATION_SCALE,
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 54 of file zoom_controller.cpp.

55 :
56 m_accTimeout( aAccTimeout ),
57 m_scale( aScale )
58{
59 if( aTimestampProv )
60 {
61 m_timestampProv = aTimestampProv;
62 }
63 else
64 {
65 m_ownTimestampProv = std::make_unique<SIMPLE_TIMESTAMPER>();
67 }
68
70}
TIME_PT m_lastTimestamp
The timeout value.
double m_scale
A multiplier for the minimum zoom step size.
TIMESTAMP_PROVIDER * m_timestampProv
< The timestamp provider to use (might be provided externally).
std::unique_ptr< TIMESTAMP_PROVIDER > m_ownTimestampProv
The timestamp of the last event.

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

Member Function Documentation

◆ GetScaleForRotation()

double ACCELERATING_ZOOM_CONTROLLER::GetScaleForRotation ( int  aRotation)
overridevirtual

Get 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.

74{
75 // The minimal step value when changing the current zoom level
76 const double minStep = 1.05;
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 * m_scale / 5.0 ) - timeDiff / m_accTimeout;
92
93 // be sure zoomScale value is significant
94 zoomScale = std::max( zoomScale, minStep );
95
96 if( aRotation < 0 )
97 zoomScale = 1.0 / zoomScale;
98 }
99 else
100 {
101 zoomScale = ( aRotation > 0 ) ? minStep : 1 / minStep;
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.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200

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

Referenced by BOOST_AUTO_TEST_CASE().

◆ GetTimeout()

TIMEOUT KIGFX::ACCELERATING_ZOOM_CONTROLLER::GetTimeout ( ) const
inline

Definition at line 106 of file zoom_controller.h.

107 {
108 return m_accTimeout;
109 }

References m_accTimeout.

◆ SetTimeout()

void KIGFX::ACCELERATING_ZOOM_CONTROLLER::SetTimeout ( const TIMEOUT aNewTimeout)
inline

Definition at line 111 of file zoom_controller.h.

112 {
113 m_accTimeout = aNewTimeout;
114 }

References m_accTimeout.

Member Data Documentation

◆ DEFAULT_ACCELERATION_SCALE

constexpr double KIGFX::ACCELERATING_ZOOM_CONTROLLER::DEFAULT_ACCELERATION_SCALE = 5.0
staticconstexpr

Definition at line 77 of file zoom_controller.h.

◆ DEFAULT_TIMEOUT

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

The default minimum step factor for accelerating controller.

Definition at line 74 of file zoom_controller.h.

◆ m_accTimeout

TIMEOUT KIGFX::ACCELERATING_ZOOM_CONTROLLER::m_accTimeout
private

Definition at line 127 of file zoom_controller.h.

Referenced by GetScaleForRotation(), GetTimeout(), and SetTimeout().

◆ m_lastTimestamp

TIME_PT KIGFX::ACCELERATING_ZOOM_CONTROLLER::m_lastTimestamp
private

The timeout value.

Definition at line 124 of file zoom_controller.h.

Referenced by ACCELERATING_ZOOM_CONTROLLER(), and GetScaleForRotation().

◆ m_ownTimestampProv

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

The timestamp of the last event.

Definition at line 121 of file zoom_controller.h.

Referenced by ACCELERATING_ZOOM_CONTROLLER().

◆ m_scale

double KIGFX::ACCELERATING_ZOOM_CONTROLLER::m_scale
private

A multiplier for the minimum zoom step size.

Definition at line 130 of file zoom_controller.h.

Referenced by GetScaleForRotation().

◆ m_timestampProv

TIMESTAMP_PROVIDER* KIGFX::ACCELERATING_ZOOM_CONTROLLER::m_timestampProv
private

< The timestamp provider to use (might be provided externally).

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

Definition at line 118 of file zoom_controller.h.

Referenced by ACCELERATING_ZOOM_CONTROLLER(), and GetScaleForRotation().


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