File indexing completed on 2024-05-19 04:36:39

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2016 Dominik Haumann <dhaumann@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU Library General Public License as published
0007  * by the Free Software Foundation, either version 2 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, see
0017  * <http://www.gnu.org/licenses/>.
0018  */
0019 #ifndef TIKZUI_ZOOM_CONTROLLER_H
0020 #define TIKZUI_ZOOM_CONTROLLER_H
0021 
0022 #include "tikzui_export.h"
0023 
0024 #include <QObject>
0025 #include <QPointer>
0026 #include <QRegExp>
0027 #include <QVector>
0028 
0029 class QComboBox;
0030 class QPinchGesture;
0031 class QRegExpValidator;
0032 class QTimeLine;
0033 
0034 namespace tikz {
0035 namespace ui {
0036 
0037 /**
0038  * Helper class for zooming.
0039  *
0040  * This class implements all logic needed to control zooming. Predefined zoom
0041  * factors make sure that the set zooming levels are sane (and human radable).
0042  *
0043  * A combo box can be attached by calling attachToComboBox(). The attached
0044  * combo box is automatically filled with the defined zoom factors.
0045  */
0046 class TIKZKITUI_EXPORT ZoomController : public QObject
0047 {
0048     Q_OBJECT
0049 
0050 public:
0051     ZoomController(QObject *parent = nullptr);
0052     virtual ~ZoomController();
0053 
0054     void setZoom(qreal z);
0055     qreal currentZoom() const;
0056 
0057     void setZoomFactors(const QVector<qreal>& factors);
0058     void attachToComboBox(QComboBox *comboBox);
0059 
0060     bool canZoomIn() const;
0061     bool canZoomOut() const;
0062 
0063     /**
0064      * Changes the current zoom level depending on the given @p delta.
0065      * A high precision delta is supported, for fine-resolution mice.
0066      */
0067     void processWheelEvent(int delta);
0068 
0069 public Q_SLOTS:
0070     void zoomIn(bool animated = false);
0071     void zoomOut(bool animated = false);
0072     void resetZoom();
0073 
0074 private Q_SLOTS:
0075     void syncComboBox();
0076     void comboBoxActivated(int index);
0077     void comboBoxEdited();
0078     void setZoomAnimated(qreal value);
0079 
0080 Q_SIGNALS:
0081     void zoomChanged(qreal currentZoom);
0082 
0083 protected:
0084     bool eventFilter(QObject *watched, QEvent *event) override;
0085 
0086 private:
0087     void startZoomAnimation(qreal startZoom, qreal endZoom);
0088 
0089 private:
0090     QTimeLine *m_animationTimeLine = nullptr;
0091     qreal m_startZoom = 1.0;
0092     qreal m_endZoom = 1.0;
0093     qreal m_currentZoom = 1.0;
0094     qreal m_gestureStartScale = 0.0;
0095     QVector<qreal> m_zoomFactors;
0096     QPointer<QComboBox> m_comboBox;
0097     QRegExp m_inputRegExp;
0098     QRegExpValidator *m_inputValidator = nullptr;
0099 };
0100 
0101 }
0102 }
0103 
0104 #endif // TIKZUI_ZOOM_CONTROLLER_H