File indexing completed on 2022-11-29 12:50:42

0001 /*
0002     SPDX-FileCopyrightText: 2007 Carsten Niehaus <cniehaus@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "isotopeview.h"
0008 
0009 #include "isotopescene.h"
0010 #include "prefs.h"
0011 
0012 IsotopeView::IsotopeView(QWidget *parent)
0013     : QGraphicsView(parent)
0014 {
0015     m_scene = new IsotopeScene(this, Prefs::isotopeTableMode());
0016     m_zoomLevel = 1.0;
0017     initialize();
0018 }
0019 
0020 IsotopeView::IsotopeView(QWidget *parent, int mode)
0021     : QGraphicsView(parent)
0022 {
0023     m_scene = new IsotopeScene(this, mode);
0024     m_zoomLevel = 1.0;
0025     initialize();
0026 }
0027 
0028 void IsotopeView::initialize()
0029 {
0030     setScene(m_scene);
0031     setSceneRect(m_scene->itemsBoundingRect());
0032     // Zoom in a bit
0033     setZoom(0.3);
0034     // Makes sure that you always zoom to the mouse if you use the scroll wheel
0035     setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
0036     QPolygonF visibleSceneRect = mapToScene(viewport()->rect());
0037     Q_EMIT visibleSceneRectChanged(visibleSceneRect);
0038 }
0039 
0040 IsotopeView::~IsotopeView()
0041 {
0042     delete scene();
0043 }
0044 
0045 void IsotopeView::resizeEvent(QResizeEvent *event)
0046 {
0047     Q_UNUSED(event)
0048     if (!isInteractive()) {
0049         qreal scale = qMin(qreal(viewport()->width()) / sceneRect().width(), qreal(viewport()->height()) / sceneRect().height());
0050         setTransform(QTransform::fromScale(scale, scale));
0051     }
0052 }
0053 
0054 void IsotopeView::mouseMoveEvent(QMouseEvent *event)
0055 {
0056     QPolygonF visibleSceneRect = mapToScene(viewport()->rect());
0057     Q_EMIT visibleSceneRectChanged(visibleSceneRect);
0058 
0059     QGraphicsView::mouseMoveEvent(event);
0060 }
0061 
0062 void IsotopeView::wheelEvent(QWheelEvent *event)
0063 {
0064     if (!isInteractive()) {
0065         event->accept();
0066         return;
0067     }
0068 
0069     double oldZoomLevel = m_zoomLevel;
0070     double factor = event->angleDelta().y() / 1000.0;
0071     m_zoomLevel = oldZoomLevel + oldZoomLevel * factor;
0072 
0073     if (m_zoomLevel < 0.3) {
0074         m_zoomLevel = 0.3;
0075     } else if (m_zoomLevel > 10.0) {
0076         m_zoomLevel = 10.0;
0077     }
0078 
0079     if (oldZoomLevel != m_zoomLevel) {
0080         factor = m_zoomLevel / oldZoomLevel;
0081         scale(factor, factor);
0082         Q_EMIT zoomLevelChanged(m_zoomLevel);
0083     }
0084     QPolygonF visibleSceneRect = mapToScene(viewport()->rect());
0085     Q_EMIT visibleSceneRectChanged(visibleSceneRect);
0086 
0087     event->accept();
0088 }
0089 
0090 void IsotopeView::setZoom(double zoom)
0091 {
0092     double oldZoomLevel = m_zoomLevel;
0093     double factor;
0094 
0095     m_zoomLevel = zoom;
0096 
0097     if (m_zoomLevel < 0.3 && isInteractive()) {
0098         m_zoomLevel = 0.3;
0099     } else if (m_zoomLevel > 10.0 && isInteractive()) {
0100         m_zoomLevel = 10.0;
0101     }
0102 
0103     factor = m_zoomLevel / oldZoomLevel;
0104     scale(factor, factor);
0105     Q_EMIT zoomLevelChanged(m_zoomLevel);
0106     QPolygonF visibleSceneRect = mapToScene(viewport()->rect());
0107     Q_EMIT visibleSceneRectChanged(visibleSceneRect);
0108 }
0109 
0110 int IsotopeView::mode() const
0111 {
0112     return m_scene->mode();
0113 }
0114 
0115 void IsotopeView::setMode(int mode)
0116 {
0117     m_scene->setMode(mode);
0118     initialize();
0119 }