Krita Source Code Documentation
Loading...
Searching...
No Matches
KoZoomMode.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 SPDX-FileCopyrightText: 2005 Johannes Schaub <litb_devel@web.de>
3 SPDX-FileCopyrightText: 2011 Arjen Hiemstra <ahiemstra@heimr.nl>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "KoZoomMode.h"
9
10#include <QDebug>
11
12#include <klocalizedstring.h>
13#include <kconfiggroup.h>
14#include <ksharedconfig.h>
15
16#include <cmath>
17#include "kis_assert.h"
18
20{
21 switch (mode) {
23 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(0 && "constant zoom has no user-friendly representation", "XXX");
25 return i18n("Fit View");
27 return i18n("Fit Width");
29 return i18n("Fit Height");
30 }
31 return "";
32}
33
35{
36 KConfigGroup config = KSharedConfig::openConfig()->group("");
37 int steps = config.readEntry("zoomSteps", 2);
38 qreal k = steps / M_LN2;
39
40 int first = ceil(log(minZoom) * k);
41 int size = floor(log(maxZoom) * k) - first + 1;
42 QVector<qreal> zoomLevels(size);
43
44 // enforce zoom levels relating to thirds (33.33%, 66.67%, ...)
45 QVector<qreal> snap(steps);
46 if (steps > 1) {
47 qreal third = log(4./ 3.) * k;
48 int i = round(third);
49 snap[(i - first) % steps] = third - i;
50 }
51
52 k = 1./ k;
53 for (int i = 0; i < steps; i++) {
54 qreal f = exp((i + first + snap[i]) * k);
55 f = floor(f * 0x1p48 + 0.5) / 0x1p48; // round off inaccuracies
56 for (int j = i; j < size; j += steps, f *= 2.) {
57 zoomLevels[j] = f;
58 }
59 }
60 return zoomLevels;
61}
62
63qreal KoZoomMode::findNextZoom(qreal currentZoom, const QVector<qreal> &zoomLevels)
64{
65 const qreal eps = 1e-5;
66 int i = 0;
67 while (i < zoomLevels.size() - 1 && currentZoom > zoomLevels[i] - eps) {
68 i++;
69 }
70
71 return qMax(currentZoom, zoomLevels[i]);
72}
73
74qreal KoZoomMode::findPrevZoom(qreal currentZoom, const QVector<qreal> &zoomLevels)
75{
76 const qreal eps = 1e-5;
77 int i = zoomLevels.size() - 1;
78 while (i > 0 && currentZoom < zoomLevels[i] + eps) i--;
79
80 return qMin(currentZoom, zoomLevels[i]);
81}
82
83QDebug operator<<(QDebug dbg, const KoZoomMode::Mode &mode)
84{
85 dbg.nospace() << "KoZoomMode::Mode(";
86
87 switch (mode) {
89 dbg << "ZOOM_CONSTANT";
90 break;
92 dbg << "ZOOM_PAGE";
93 break;
95 dbg << "ZOOM_WIDTH";
96 break;
98 dbg << "ZOOM_HEIGHT";
99 break;
100 default:
101 dbg << "UNKNOWN";
102 break;
103 }
104
105 dbg << ")";
106 return dbg.space();
107}
QDebug operator<<(QDebug dbg, const KoZoomMode::Mode &mode)
@ ZOOM_CONSTANT
zoom x %
Definition KoZoomMode.h:24
@ ZOOM_PAGE
zoom page
Definition KoZoomMode.h:25
@ ZOOM_WIDTH
zoom pagewidth
Definition KoZoomMode.h:26
@ ZOOM_HEIGHT
zoom pageheight
Definition KoZoomMode.h:27
static qreal findNextZoom(qreal currentZoom, const QVector< qreal > &zoomLevels)
static QString toString(Mode mode)
static QVector< qreal > generateStandardZoomLevels(qreal minZoom, qreal maxZoom)
static qreal findPrevZoom(qreal currentZoom, const QVector< qreal > &zoomLevels)
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
Definition kis_assert.h:129
const qreal eps