File indexing completed on 2024-06-23 05:49:19

0001 /*
0002     This file is part of the Kasten Framework, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2006-2008 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "zoomcontroller.hpp"
0010 
0011 // Kasten gui
0012 #include <Kasten/Zoomable>
0013 // Kasten core
0014 #include <Kasten/AbstractModel>
0015 // KF
0016 #include <KXMLGUIClient>
0017 #include <KLocalizedString>
0018 #include <KActionCollection>
0019 #include <KStandardAction>
0020 // Qt
0021 #include <QAction>
0022 
0023 namespace Kasten {
0024 
0025 ZoomController::ZoomController(KXMLGUIClient* guiClient)
0026 {
0027     mZoomInAction = KStandardAction::zoomIn(  this, &ZoomController::zoomIn,  this);
0028     mZoomOutAction = KStandardAction::zoomOut(this, &ZoomController::zoomOut, this);
0029 
0030     guiClient->actionCollection()->addActions({
0031         mZoomInAction,
0032         mZoomOutAction
0033     });
0034 
0035 #if 0
0036     ZoomToAction = new KSelectAction(i18n("Zoom"), "viewmag", 0, ActionCollection, "zoomTo");
0037     ZoomToAction->setEditable(true);
0038     QList<double> mags = DisplayOptions::normalMagnificationValues();
0039     QStringList translated;
0040     int idx = 0;
0041     int cur = 0;
0042     for (QList<double>::iterator first = mags.begin(), last = mags.end();
0043          first != last;
0044          ++first) {
0045         translated << i18nc("zoom-factor (percentage)", "%1%", *first * 100.0);
0046         if (*first == 1.0) {
0047             idx = cur;
0048         }
0049         ++cur;
0050     }
0051 
0052     ZoomToAction->setItems(translated);
0053     ZoomToAction->setCurrentItem(idx);
0054     connect(ZoomToAction, SIGNAL(triggered(QString)), SLOT(zoomTo(QString)));
0055 
0056     // TODO: name size relative to object or view? name object(variable) or view?
0057     // TODO: is this a sticking parameter?
0058     FitToWidthAction = new KAction(i18n("&Fit to Width"), ActionCollection, "fit_to_width");
0059     connect(FitWidthAction, SIGNAL(triggered(bool)), SLOT(fitToWidth()));
0060     FitToHeightAction = new KAction(i18n("&Fit to Height"), ActionCollection, "fit_to_height");
0061     connect(FitWidthAction, SIGNAL(triggered(bool)), SLOT(fitToHeight()));
0062     FitToSizeAction = new KAction(i18n("&Fit to Size"), ActionCollection, "fit_to_size");
0063     connect(FitToSizeAction, SIGNAL(triggered(bool)), SLOT(fitToSize()));
0064 #endif
0065     setTargetModel(nullptr);
0066 }
0067 
0068 void ZoomController::setTargetModel(AbstractModel* model)
0069 {
0070     if (mModel) {
0071         mModel->disconnect(this);
0072     }
0073 
0074     mModel = model ? model->findBaseModelWithInterface<If::Zoomable*>() : nullptr;
0075     mZoomControl = mModel ? qobject_cast<If::Zoomable*>(mModel) : nullptr;
0076 
0077     if (mZoomControl) {
0078         mZoomLevel = mZoomControl->zoomLevel();
0079         connect(mModel, SIGNAL(zoomLevelChanged(double)), SLOT(onZoomLevelChange(double)));
0080     }
0081 
0082     const bool hasView = (mZoomControl != nullptr);
0083     mZoomInAction->setEnabled(hasView);
0084     mZoomOutAction->setEnabled(hasView);
0085 }
0086 
0087 void ZoomController::zoomIn()
0088 {
0089     mZoomControl->setZoomLevel(mZoomLevel * 1.10);
0090 }
0091 
0092 void ZoomController::zoomOut()
0093 {
0094     mZoomControl->setZoomLevel(mZoomLevel / 1.10);
0095 }
0096 #if 0
0097 void ZoomController::zoomTo(const QString& nz)
0098 {
0099     QString z = nz;
0100     double zoom;
0101     z.remove(z.indexOf('%'), 1);
0102     zoom = KLocale::global()->readNumber(z) / 100;
0103     qCDebug(LOG_KASTEN_CONTROLLERS) << "ZOOM = "  << nz << ", setting zoom = " << zoom << endl;
0104 
0105     DisplayOptions options = miniWidget()->displayOptions();
0106     options.setMagnification(zoom);
0107     miniWidget()->setDisplayOptions(options);
0108     miniWidget()->redisplay();
0109     _mainWidget->setFocus();
0110     updateZoomActions();
0111 }
0112 
0113 void ZoomController::fitToWidth()
0114 {
0115     if (pageView()->page()) {
0116         miniWidget()->fitWidth(pageView()->viewport()->width() - 16);
0117     }
0118     // We subtract 16 pixels because of the page decoration.
0119     updateZoomActions();
0120 }
0121 
0122 void ZoomController::fitToSize()
0123 {
0124     if (pageView()->page()) {
0125         miniWidget()->fitWidthHeight(pageView()->viewport()->width() - 16,
0126                                      pageView()->viewport()->height() - 16);
0127     }
0128     updateZoomActions();
0129 }
0130 #endif
0131 void ZoomController::onZoomLevelChange(double level)
0132 {
0133     mZoomLevel = level;
0134 }
0135 
0136 }
0137 
0138 #include "moc_zoomcontroller.cpp"