File: zoomcontroller.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (167 lines) | stat: -rw-r--r-- 4,178 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * Copyright 2016 Igor Kushnir <igorkuo@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License or (at your option) version 3 or any later version
 * accepted by the membership of KDE e.V. (or its successor approved
 * by the membership of KDE e.V.), which shall act as a proxy
 * defined in Section 14 of version 3 of the license.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "zoomcontroller.h"

#include <KConfigGroup>

#include <QPoint>
#include <QKeyEvent>
#include <QWheelEvent>

#include <cmath>

namespace {

constexpr const char* factorConfigEntryKey() { return "Zoom Factor"; }

constexpr double defaultFactor{1};
constexpr double defaultMultiplier{1.1};

double multiplier(double scale)
{
    // Allow finer-grained control over zoom factor compared to defaultMultiplier
    constexpr double smallMultiplier{1.05};
    return std::pow(smallMultiplier, scale);
}

} // namespace

using KDevelop::ZoomControllerPrivate;
using KDevelop::ZoomController;

class KDevelop::ZoomControllerPrivate
{
public:
    explicit ZoomControllerPrivate(const KConfigGroup& configGroup);
    void writeConfig();

    KConfigGroup m_configGroup;
    double m_factor{defaultFactor};
};

ZoomControllerPrivate::ZoomControllerPrivate(const KConfigGroup& configGroup)
    : m_configGroup{configGroup}
{
    m_factor = m_configGroup.readEntry(factorConfigEntryKey(), defaultFactor);
}

void ZoomControllerPrivate::writeConfig()
{
    m_configGroup.writeEntry(factorConfigEntryKey(), m_factor);
    m_configGroup.sync();
}

ZoomController::ZoomController(const KConfigGroup& configGroup, QObject* parent)
    : QObject(parent)
    , d_ptr{new ZoomControllerPrivate(configGroup)}
{
}

ZoomController::~ZoomController() = default;

double ZoomController::factor() const
{
    Q_D(const ZoomController);

    return d->m_factor;
}

void ZoomController::setFactor(double factor)
{
    Q_D(ZoomController);

    factor = qBound(0.1, factor, 10.0);
    if (d->m_factor == factor) {
        return;
    }
    d->m_factor = factor;
    d->writeConfig();
    emit factorChanged(d->m_factor);
}

void ZoomController::zoomBy(double scale)
{
    Q_D(ZoomController);

    setFactor(d->m_factor * multiplier(scale));
}

bool ZoomController::handleKeyPressEvent(QKeyEvent* event)
{
    Q_ASSERT(event);

    const auto requiredModifiers = Qt::ControlModifier;
    if (!(event->modifiers() & requiredModifiers)) {
        return false;
    }
    // Qt::ShiftModifier is required for the 0 key in some keyboard layouts
    // (such as Programmer Dvorak). Allow Qt::KeypadModifier to support numpad keys.
    // Don't allow other modifiers, such as Alt and Meta, to minimize shortcut conflicts.
    const auto allowedModifiers = Qt::ControlModifier | Qt::ShiftModifier | Qt::KeypadModifier;
    if (event->modifiers() & ~allowedModifiers) {
        return false;
    }

    if (event->key() == Qt::Key_0) {
        resetZoom();
        event->accept();
        return true;
    }

    return false;
}

bool ZoomController::handleWheelEvent(QWheelEvent* event)
{
    Q_ASSERT(event);

    if (!(event->modifiers() & Qt::ControlModifier)) {
        return false;
    }

    constexpr double minStandardDelta{120};
    const QPoint delta = event->angleDelta();
    const double scale{(delta.x() + delta.y()) / minStandardDelta};

    zoomBy(scale);
    event->accept();
    return true;
}

void ZoomController::zoomIn()
{
    Q_D(ZoomController);

    setFactor(d->m_factor * defaultMultiplier);
}

void ZoomController::zoomOut()
{
    Q_D(ZoomController);

    setFactor(d->m_factor / defaultMultiplier);
}

void ZoomController::resetZoom()
{
    setFactor(defaultFactor);
}