Krita Source Code Documentation
Loading...
Searching...
No Matches
KoZoomActionState.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "KoZoomActionState.h"
8
9#include <QDebug>
10#include <QLocale>
11#include <klocalizedstring.h>
12
13namespace
14{
15QString zoomToString(qreal value)
16{
17 const qreal valueInPercent = value * 100;
18 const int precision = (value > 10.0) ? 0 : 1;
19
20 return i18n("%1%", QLocale().toString(valueInPercent, 'f', precision));
21}
22
23QVector<KoZoomActionState::ZoomItem> generateZoomGuiItems(const QVector<qreal> &standardZoomLevels)
24{
25 QVector<qreal> zoomLevels;
26
27 // we keep only sane zoom levels in the dropdown menu
28 std::copy_if(standardZoomLevels.begin(), standardZoomLevels.end(),
29 std::back_inserter(zoomLevels),
30 [] (qreal zoom) { return zoom >= 0.2 && zoom <= 10; });
31
33 result.reserve(zoomLevels.size() + 3);
34
38
39 Q_FOREACH (qreal value, zoomLevels) {
40 result.push_back(KoZoomActionState::ZoomItem{KoZoomMode::ZOOM_CONSTANT, value, zoomToString(value)});
41 }
42
43 return result;
44}
45
46} // namespace
47
52
54{
55 const bool forceInitialization = realGuiLevels.isEmpty();
56 bool needsUpdateRealLevels = forceInitialization;
57
58 if (forceInitialization ||
60 !qFuzzyCompare(newState.maxZoom, zoomState.maxZoom)) {
61
62 auto newStandardLevels = KoZoomMode::generateStandardZoomLevels(newState.minZoom, newState.maxZoom);
63 if (newStandardLevels != standardLevels) {
64 standardLevels = newStandardLevels;
65 auto newGuiLevels = generateZoomGuiItems(newStandardLevels);
66 if (newGuiLevels != guiLevels) {
67 guiLevels = newGuiLevels;
68 needsUpdateRealLevels = true;
69 }
70 }
71 }
72
73 int proposedCurrentIndex = currentRealLevelIndex;
74
75 if (needsUpdateRealLevels || !qFuzzyCompare(newState.zoom, zoomState.zoom) || newState.mode != zoomState.mode) {
77
78 const qreal eps = 1e-5;
79 auto fuzzyCompareLess = [eps] (const ZoomItem &lhs, qreal rhs) {
80 return std::get<qreal>(lhs) + eps < rhs;
81 };
82
83 auto it = std::lower_bound(realGuiLevels.begin(), realGuiLevels.end(),
84 newState.zoom, fuzzyCompareLess);
85
91 if (it == realGuiLevels.end() || !(qAbs(std::get<qreal>(*it) - newState.zoom) < eps)) {
92 it = realGuiLevels.insert(it, {KoZoomMode::ZOOM_CONSTANT, newState.zoom, zoomToString(newState.zoom)});
93 }
94
95 proposedCurrentIndex = std::distance(realGuiLevels.begin(), it);
96 }
97
98 auto textForFixedMode = [&] (const int index) {
99 ZoomItem zoomItem = realGuiLevels[index];
100 return i18nc("concatenate mode name (e.g. \"Fit Page\") and real zoom percent string", "%1 (%2)",
101 std::get<QString>(zoomItem),
102 zoomToString(newState.zoom));
103 };
104
105 switch (newState.mode) {
109 break;
113 break;
117 break;
119 default:
120 currentRealLevelIndex = proposedCurrentIndex;
122 break;
123 }
124
125 zoomState = newState;
126}
127
129{
130 const qreal eps = 1e-5;
131 auto fuzzyCompareLess = [eps](qreal lhs, qreal rhs) {
132 return lhs + eps < rhs;
133 };
134
135 auto it = std::lower_bound(standardLevels.begin(), standardLevels.end(),
136 zoom, fuzzyCompareLess);
137
138 return qMin(standardLevels.size() - 1, std::distance(standardLevels.begin(), it));
139}
140
145
146QDebug operator<<(QDebug dbg, const KoZoomActionState::ZoomItem &item)
147{
148 dbg.nospace() << "KoZoomActionState::ZoomItem("
149 << std::get<0>(item) << ", "
150 << std::get<1>(item) << ", "
151 << std::get<2>(item) << ")";
152
153 return dbg.space();
154}
155
float value(const T *src, size_t ch)
QDebug operator<<(QDebug dbg, const KoZoomActionState::ZoomItem &item)
QVector< ZoomItem > realGuiLevels
QVector< ZoomItem > guiLevels
QVector< qreal > standardLevels
int calcNearestStandardLevel() const
std::tuple< KoZoomMode::Mode, qreal, QString > ZoomItem
KoZoomActionState(const KoZoomState &state)
void setZoomState(const KoZoomState &state)
@ 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 QString toString(Mode mode)
static QVector< qreal > generateStandardZoomLevels(qreal minZoom, qreal maxZoom)
KoZoomMode::Mode mode
Definition KoZoomState.h:24
qreal maxZoom
Definition KoZoomState.h:27
qreal minZoom
Definition KoZoomState.h:26
static bool qFuzzyCompare(half p1, half p2)
const qreal eps
QString toString(const QString &value)