KiCad PCB EDA Suite
Loading...
Searching...
No Matches
zoom_controller.cpp
Go to the documentation of this file.
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 The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * @author Tomasz Wlostowski <[email protected]>
9 * @author Maciej Suminski <[email protected]>
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, see <https://www.gnu.org/licenses/>.
23 */
24
26
27#include <trace_helpers.h>
28
29#include <wx/log.h>
30
31#include <algorithm>
32
33using namespace KIGFX;
34
35
41{
42public:
44 {
45 return ACCELERATING_ZOOM_CONTROLLER::CLOCK::now();
46 }
47};
48
49
51 double aScale, const TIMEOUT& aAccTimeout, TIMESTAMP_PROVIDER* aTimestampProv ) :
52 m_accTimeout( aAccTimeout ),
53 m_scale( aScale )
54{
55 if( aTimestampProv )
56 {
57 m_timestampProv = aTimestampProv;
58 }
59 else
60 {
61 m_ownTimestampProv = std::make_unique<SIMPLE_TIMESTAMPER>();
63 }
64
65 m_prevTimestamp = m_timestampProv->GetTimestamp();
66}
67
68
70{
71 // The minimal step value when changing the current zoom level
72 const double minStep = 1.05;
73
74 const auto timestamp = m_timestampProv->GetTimestamp();
75 auto timeDiff = std::chrono::duration_cast<TIMEOUT>( timestamp - m_prevTimestamp );
76
77 m_prevTimestamp = timestamp;
78
79 wxLogTrace( traceZoomScroll,
80 wxString::Format( "Rot %d, time diff: %ldms", aRotation, (long)timeDiff.count() ) );
81
82 double zoomScale;
83
84 // Set scaling speed depending on scroll wheel event interval
85 if( timeDiff < m_accTimeout
86 && ( (aRotation > 0) == m_prevRotationPositive ) )
87 {
88 zoomScale = ( 2.05 * m_scale / 5.0 ) - timeDiff / m_accTimeout;
89
90 // be sure zoomScale value is significant
91 zoomScale = std::max( zoomScale, minStep );
92
93 if( aRotation < 0 )
94 zoomScale = 1.0 / zoomScale;
95 }
96 else
97 {
98 zoomScale = ( aRotation > 0 ) ? minStep : 1 / minStep;
99 }
100 m_prevRotationPositive = aRotation > 0;
101
102 wxLogTrace( traceZoomScroll, wxString::Format( " Zoom factor: %f", zoomScale ) );
103
104 return zoomScale;
105}
106
107#ifdef __MINGW32__
108// For some reason, this is needed to avoid a link issue
109// (undefined reference to ACCELERATING_ZOOM_CONTROLLER::DEFAULT_TIMEOUT
110// and GAL_API CONSTANT_ZOOM_CONTROLLER::MSW_SCALE)
113#endif
114
115
117{
118}
119
120
122{
123 wxLogTrace( traceZoomScroll, wxString::Format( "Rot %d", aRotation ) );
124
125 aRotation = ( aRotation > 0 ) ? std::min( aRotation, 100 ) : std::max( aRotation, -100 );
126
127 double dscale = aRotation * m_scale;
128
129 double zoom_scale = ( aRotation > 0 ) ? ( 1 + dscale ) : 1 / ( 1 - dscale );
130
131 wxLogTrace( traceZoomScroll, wxString::Format( " Zoom factor: %f", zoom_scale ) );
132
133 return zoom_scale;
134}
std::chrono::time_point< CLOCK > TIME_PT
The type of the time stamps.
std::chrono::milliseconds TIMEOUT
The type of the acceleration timeout.
static constexpr TIMEOUT DEFAULT_TIMEOUT
The default timeout, after which a another scroll will not be accelerated.
TIME_PT m_prevTimestamp
The timestamp of the previous event.
TIMEOUT m_accTimeout
The timeout value.
bool m_prevRotationPositive
Previous rotation was positive.
double m_scale
A multiplier for the minimum zoom step size.
TIMESTAMP_PROVIDER * m_timestampProv
The timestamp provider to use (might be provided externally).
double GetScaleForRotation(int aRotation) override
Get the scale factor produced by a given mousewheel rotation.
ACCELERATING_ZOOM_CONTROLLER(double aScale=DEFAULT_ACCELERATION_SCALE, const TIMEOUT &aAccTimeout=DEFAULT_TIMEOUT, TIMESTAMP_PROVIDER *aTimestampProv=nullptr)
std::unique_ptr< TIMESTAMP_PROVIDER > m_ownTimestampProv
Any provider owned by this class (the default one, if used).
double GetScaleForRotation(int aRotation) override
Get the scale factor produced by a given mousewheel rotation.
double m_scale
The scale factor set by the constructor.
static constexpr double MSW_SCALE
A suitable (magic) scale factor for Windows systems.
A very simple timestamper that uses the KIGFX::ACCELERATING_ZOOM_CONTROLLER::CLOCK to provide a times...
ACCELERATING_ZOOM_CONTROLLER::TIME_PT GetTimestamp() override
#define GAL_API
Definition gal.h:27
const wxChar *const traceZoomScroll
Flag to enable debug output of zoom-scrolling calculations in KIGFX::ZOOM_CONTROLLER and derivatives.
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:29
wxLogTrace helper definitions.
ZOOM_CONTROLLER class definition.