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

0001 /*
0002     SPDX-FileCopyrightText: 2007 Carsten Niehaus <cniehaus@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "isotopeguideview.h"
0008 
0009 #include "isotopescene.h"
0010 #include <QScrollBar>
0011 
0012 IsotopeGuideView::IsotopeGuideView(QWidget *parent)
0013     : QGraphicsView(parent)
0014 {
0015     m_guidedView = nullptr;
0016     m_scale = 1.0;
0017 
0018     setCursor(Qt::OpenHandCursor);
0019 }
0020 
0021 void IsotopeGuideView::setGuidedView(IsotopeView *guidedView)
0022 {
0023     m_guidedView = guidedView;
0024     connect(m_guidedView, &IsotopeView::zoomLevelChanged, this, &IsotopeGuideView::setZoomLevel);
0025     connect(m_guidedView, &IsotopeView::visibleSceneRectChanged, this, &IsotopeGuideView::setVisibleSceneRect);
0026 
0027     updateScene();
0028 }
0029 
0030 void IsotopeGuideView::setVisibleSceneRect(const QPolygonF &sceneRect)
0031 {
0032     m_visibleSceneRect = sceneRect;
0033     viewport()->update();
0034 }
0035 
0036 void IsotopeGuideView::drawForeground(QPainter *painter, const QRectF &rect)
0037 {
0038     if (m_guidedView && m_visibleSceneRect.boundingRect().intersects(rect)) {
0039         QRectF drawRect = m_visibleSceneRect.boundingRect().adjusted(0, 0, -1, -1);
0040         painter->setPen(QPen(Qt::yellow, 1.0 / m_scale));
0041         painter->fillRect(drawRect, QBrush(QColor(0, 0, 0, 100)));
0042         painter->drawRect(drawRect);
0043     }
0044 }
0045 
0046 void IsotopeGuideView::resizeEvent(QResizeEvent *event)
0047 {
0048     Q_UNUSED(event)
0049     m_scale = qMin(qreal(viewport()->width()) / sceneRect().width(), qreal(viewport()->height()) / sceneRect().height());
0050     setTransform(QTransform::fromScale(m_scale, m_scale));
0051 }
0052 
0053 void IsotopeGuideView::mousePressEvent(QMouseEvent *event)
0054 {
0055     m_dragEvent = mapFromScene(m_visibleSceneRect).boundingRect().contains(event->pos());
0056     if (m_dragEvent && event->buttons() & Qt::LeftButton) {
0057         m_lastMousePos = event->pos();
0058         setCursor(Qt::ClosedHandCursor);
0059     }
0060 }
0061 
0062 void IsotopeGuideView::mouseReleaseEvent(QMouseEvent *event)
0063 {
0064     Q_UNUSED(event)
0065     m_dragEvent = false;
0066     setCursor(Qt::OpenHandCursor);
0067 }
0068 
0069 void IsotopeGuideView::mouseMoveEvent(QMouseEvent *event)
0070 {
0071     if (m_dragEvent && event->buttons() & Qt::LeftButton) {
0072         const QPoint p1(m_guidedView->mapFromScene(mapToScene(m_lastMousePos)));
0073         const QPoint p2(m_guidedView->mapFromScene(mapToScene(event->pos())));
0074         m_guidedView->horizontalScrollBar()->setValue(m_guidedView->horizontalScrollBar()->value() + p2.x() - p1.x());
0075         m_guidedView->verticalScrollBar()->setValue(m_guidedView->verticalScrollBar()->value() + p2.y() - p1.y());
0076         m_lastMousePos = event->pos();
0077 
0078         m_visibleSceneRect = m_guidedView->visibleSceneRect();
0079         viewport()->update();
0080     }
0081 }
0082 
0083 void IsotopeGuideView::setZoomLevel(double zoomLevel)
0084 {
0085     m_zoomLevel = zoomLevel;
0086 }
0087 
0088 void IsotopeGuideView::updateScene()
0089 {
0090     m_zoomLevel = m_guidedView->zoomLevel();
0091     setScene(m_guidedView->scene());
0092     setSceneRect(scene()->itemsBoundingRect());
0093     ensureVisible(scene()->sceneRect());
0094     resizeEvent(nullptr);
0095 }