1 /*
2 This file is part of the Kasten Framework, made within the KDE community.
3
4 SPDX-FileCopyrightText: 2006-2008 Friedrich W. H. Kossebau <kossebau@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9 #include "zoomcontroller.hpp"
10
11 // Kasten gui
12 #include <Kasten/Zoomable>
13 // Kasten core
14 #include <Kasten/AbstractModel>
15 // KF
16 #include <KXMLGUIClient>
17 #include <KLocalizedString>
18 #include <KActionCollection>
19 #include <KStandardAction>
20 // Qt
21 #include <QAction>
22
23 namespace Kasten {
24
ZoomController(KXMLGUIClient * guiClient)25 ZoomController::ZoomController(KXMLGUIClient* guiClient)
26 {
27 mZoomInAction = KStandardAction::zoomIn( this, &ZoomController::zoomIn, this);
28 mZoomOutAction = KStandardAction::zoomOut(this, &ZoomController::zoomOut, this);
29
30 guiClient->actionCollection()->addActions({
31 mZoomInAction,
32 mZoomOutAction
33 });
34
35 #if 0
36 ZoomToAction = new KSelectAction(i18n("Zoom"), "viewmag", 0, ActionCollection, "zoomTo");
37 ZoomToAction->setEditable(true);
38 QList<double> mags = DisplayOptions::normalMagnificationValues();
39 QStringList translated;
40 int idx = 0;
41 int cur = 0;
42 for (QList<double>::iterator first = mags.begin(), last = mags.end();
43 first != last;
44 ++first) {
45 translated << i18nc("zoom-factor (percentage)", "%1%", *first * 100.0);
46 if (*first == 1.0) {
47 idx = cur;
48 }
49 ++cur;
50 }
51
52 ZoomToAction->setItems(translated);
53 ZoomToAction->setCurrentItem(idx);
54 connect(ZoomToAction, SIGNAL(triggered(QString)), SLOT(zoomTo(QString)));
55
56 // TODO: name size relative to object or view? name object(variable) or view?
57 // TODO: is this a sticking parameter?
58 FitToWidthAction = new KAction(i18n("&Fit to Width"), ActionCollection, "fit_to_width");
59 connect(FitWidthAction, SIGNAL(triggered(bool)), SLOT(fitToWidth()));
60 FitToHeightAction = new KAction(i18n("&Fit to Height"), ActionCollection, "fit_to_height");
61 connect(FitWidthAction, SIGNAL(triggered(bool)), SLOT(fitToHeight()));
62 FitToSizeAction = new KAction(i18n("&Fit to Size"), ActionCollection, "fit_to_size");
63 connect(FitToSizeAction, SIGNAL(triggered(bool)), SLOT(fitToSize()));
64 #endif
65 setTargetModel(nullptr);
66 }
67
setTargetModel(AbstractModel * model)68 void ZoomController::setTargetModel(AbstractModel* model)
69 {
70 if (mModel) {
71 mModel->disconnect(this);
72 }
73
74 mModel = model ? model->findBaseModelWithInterface<If::Zoomable*>() : nullptr;
75 mZoomControl = mModel ? qobject_cast<If::Zoomable*>(mModel) : nullptr;
76
77 if (mZoomControl) {
78 mZoomLevel = mZoomControl->zoomLevel();
79 connect(mModel, SIGNAL(zoomLevelChanged(double)), SLOT(onZoomLevelChange(double)));
80 }
81
82 const bool hasView = (mZoomControl != nullptr);
83 mZoomInAction->setEnabled(hasView);
84 mZoomOutAction->setEnabled(hasView);
85 }
86
zoomIn()87 void ZoomController::zoomIn()
88 {
89 mZoomControl->setZoomLevel(mZoomLevel * 1.10);
90 }
91
zoomOut()92 void ZoomController::zoomOut()
93 {
94 mZoomControl->setZoomLevel(mZoomLevel / 1.10);
95 }
96 #if 0
97 void ZoomController::zoomTo(const QString& nz)
98 {
99 QString z = nz;
100 double zoom;
101 z.remove(z.indexOf('%'), 1);
102 zoom = KLocale::global()->readNumber(z) / 100;
103 qCDebug(LOG_KASTEN_CONTROLLERS) << "ZOOM = " << nz << ", setting zoom = " << zoom << endl;
104
105 DisplayOptions options = miniWidget()->displayOptions();
106 options.setMagnification(zoom);
107 miniWidget()->setDisplayOptions(options);
108 miniWidget()->redisplay();
109 _mainWidget->setFocus();
110 updateZoomActions();
111 }
112
113 void ZoomController::fitToWidth()
114 {
115 if (pageView()->page()) {
116 miniWidget()->fitWidth(pageView()->viewport()->width() - 16);
117 }
118 // We subtract 16 pixels because of the page decoration.
119 updateZoomActions();
120 }
121
122 void ZoomController::fitToSize()
123 {
124 if (pageView()->page()) {
125 miniWidget()->fitWidthHeight(pageView()->viewport()->width() - 16,
126 pageView()->viewport()->height() - 16);
127 }
128 updateZoomActions();
129 }
130 #endif
onZoomLevelChange(double level)131 void ZoomController::onZoomLevelChange(double level)
132 {
133 mZoomLevel = level;
134 }
135
136 }
137