diff --git a/src/worksheetview.cpp b/src/worksheetview.cpp index 4cfe67a5..706d823b 100644 --- a/src/worksheetview.cpp +++ b/src/worksheetview.cpp @@ -1,282 +1,278 @@ /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. --- Copyright (C) 2012 Martin Kuettler */ #include "worksheetview.h" #include "worksheet.h" #include #include #include WorksheetView::WorksheetView(Worksheet* scene, QWidget* parent) : QGraphicsView(scene, parent), m_scale(1), m_animation(nullptr), m_hAnimation(nullptr), m_vAnimation(nullptr), m_worksheet(scene) { connect(scene, SIGNAL(sceneRectChanged(const QRectF&)), this, SLOT(sceneRectChanged(const QRectF&))); setAlignment(Qt::AlignLeft | Qt::AlignTop); //setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); } -WorksheetView::~WorksheetView() -{ -} - void WorksheetView::makeVisible(const QRectF& sceneRect) { const qreal w = viewport()->width(); const qreal h = viewport()->height(); QRectF rect(m_scale*sceneRect.topLeft(), m_scale*sceneRect.size()); qreal x,y; if (m_animation) { x = m_hAnimation->endValue().toReal(); y = m_vAnimation->endValue().toReal(); if (QRectF(x,y,w,h).contains(rect)) return; } if (horizontalScrollBar()) x = horizontalScrollBar()->value(); else x = 0; if (verticalScrollBar()) y = verticalScrollBar()->value(); else y = 0; if (!m_animation && QRectF(x,y,w,h).contains(rect)) return; qreal nx, ny; if (y > rect.y() || rect.height() > h) ny = rect.y(); else ny = rect.y() + rect.height() - h; if (rect.x() + rect.width() <= w || x > rect.x()) nx = 0; else nx = rect.x() + rect.width() - w; if (!m_worksheet->animationsEnabled()) { if (horizontalScrollBar()) horizontalScrollBar()->setValue(nx); if (verticalScrollBar()) verticalScrollBar()->setValue(ny); return; } if (!m_animation) m_animation = new QParallelAnimationGroup(this); if (horizontalScrollBar()) { if (!m_hAnimation) { m_hAnimation = new QPropertyAnimation(horizontalScrollBar(), "value", this); m_hAnimation->setStartValue(horizontalScrollBar()->value()); nx = qBound(qreal(0.0), nx, qreal(0.0+horizontalScrollBar()->maximum())); m_hAnimation->setEndValue(nx); m_hAnimation->setDuration(100); m_animation->addAnimation(m_hAnimation); } else { qreal progress = static_cast(m_hAnimation->currentTime()) / m_hAnimation->totalDuration(); QEasingCurve curve = m_hAnimation->easingCurve(); qreal value = curve.valueForProgress(progress); qreal sx = 1/(1-value)*(m_hAnimation->currentValue().toReal() - value * nx); m_hAnimation->setStartValue(sx); m_hAnimation->setEndValue(nx); } } else { m_hAnimation = nullptr; } if (verticalScrollBar()) { if (!m_vAnimation) { m_vAnimation = new QPropertyAnimation(verticalScrollBar(), "value", this); m_vAnimation->setStartValue(verticalScrollBar()->value()); ny = qBound(qreal(0.0), ny, qreal(0.0+verticalScrollBar()->maximum())); m_vAnimation->setEndValue(ny); m_vAnimation->setDuration(100); m_animation->addAnimation(m_vAnimation); } else { qreal progress = static_cast(m_vAnimation->currentTime()) / m_vAnimation->totalDuration(); QEasingCurve curve = m_vAnimation->easingCurve(); qreal value = curve.valueForProgress(progress); qreal sy = 1/(1-value)*(m_vAnimation->currentValue().toReal() - value * ny); m_vAnimation->setStartValue(sy); m_vAnimation->setEndValue(ny); } } else { m_vAnimation = nullptr; } connect(m_animation, &QParallelAnimationGroup::finished, this, &WorksheetView::endAnimation); m_animation->start(); } -bool WorksheetView::isVisible(const QRectF& sceneRect) +bool WorksheetView::isVisible(const QRectF& sceneRect) const { const qreal w = viewport()->width(); const qreal h = viewport()->height(); QRectF rect(m_scale*sceneRect.topLeft(), m_scale*sceneRect.size()); qreal x,y; if (m_animation) { x = m_hAnimation->endValue().toReal(); y = m_vAnimation->endValue().toReal(); } else { if (horizontalScrollBar()) x = horizontalScrollBar()->value(); else x = 0; if (verticalScrollBar()) y = verticalScrollBar()->value(); else y = 0; } return QRectF(x,y,w,h).contains(rect); } -bool WorksheetView::isAtEnd() +bool WorksheetView::isAtEnd() const { bool atEnd = true; if (verticalScrollBar()) atEnd &= (verticalScrollBar()->value()==verticalScrollBar()->maximum()); return atEnd; } -void WorksheetView::scrollToEnd() +void WorksheetView::scrollToEnd() const { if (verticalScrollBar()) verticalScrollBar()->setValue(verticalScrollBar()->maximum()); } void WorksheetView::scrollBy(int dy) { if (!verticalScrollBar()) return; int ny = verticalScrollBar()->value() + dy; if (ny < 0) ny = 0; else if (ny > verticalScrollBar()->maximum()) ny = verticalScrollBar()->maximum(); int x; if (horizontalScrollBar()) x = horizontalScrollBar()->value(); else x = 0; const qreal w = viewport()->width() / m_scale; const qreal h = viewport()->height() / m_scale; makeVisible(QRectF(x, ny, w, h)); } void WorksheetView::endAnimation() { if (!m_animation) return; m_animation->deleteLater(); m_hAnimation = nullptr; m_vAnimation = nullptr; m_animation = nullptr; } -QPoint WorksheetView::viewCursorPos() +QPoint WorksheetView::viewCursorPos() const { return viewport()->mapFromGlobal(QCursor::pos()); } -QPointF WorksheetView::sceneCursorPos() +QPointF WorksheetView::sceneCursorPos() const { return mapToScene(viewCursorPos()); } -QRectF WorksheetView::viewRect() +QRectF WorksheetView::viewRect() const { const qreal w = viewport()->width() / m_scale; const qreal h = viewport()->height() / m_scale; qreal y = verticalScrollBar()->value(); qreal x = horizontalScrollBar() ? horizontalScrollBar()->value() : 0; return QRectF(x, y, w, h); } -void WorksheetView::resizeEvent(QResizeEvent * event) +void WorksheetView::resizeEvent(QResizeEvent* event) { QGraphicsView::resizeEvent(event); updateSceneSize(); } -qreal WorksheetView::scaleFactor() +qreal WorksheetView::scaleFactor() const { return m_scale; } void WorksheetView::updateSceneSize() { QSize s = viewport()->size(); m_worksheet->setViewSize(s.width()/m_scale, s.height()/m_scale, m_scale); sendViewRectChange(); } -void WorksheetView::sceneRectChanged(const QRectF& sceneRect) +void WorksheetView::sceneRectChanged(const QRectF& sceneRect) const { Q_UNUSED(sceneRect); if (verticalScrollBar()) connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(sendViewRectChange()), Qt::UniqueConnection); if (horizontalScrollBar()) connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(sendViewRectChange()), Qt::UniqueConnection); } -void WorksheetView::sendViewRectChange() +void WorksheetView::sendViewRectChange() const { emit viewRectChanged(viewRect()); } void WorksheetView::zoomIn() { m_scale *= 1.1; scale(1.1, 1.1); updateSceneSize(); } void WorksheetView::zoomOut() { m_scale /= 1.1; scale(1/1.1, 1/1.1); updateSceneSize(); } diff --git a/src/worksheetview.h b/src/worksheetview.h index db60af31..677a361e 100644 --- a/src/worksheetview.h +++ b/src/worksheetview.h @@ -1,73 +1,69 @@ /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. --- Copyright (C) 2012 Martin Kuettler */ #ifndef WORKSHEETVIEW_H #define WORKSHEETVIEW_H #include class QParallelAnimationGroup; class QPropertyAnimation; class Worksheet; class WorksheetView : public QGraphicsView { Q_OBJECT - public: +public: WorksheetView(Worksheet* scene, QWidget* parent); - ~WorksheetView() override; void makeVisible(const QRectF& sceneRect); - bool isVisible(const QRectF& sceneRect); - bool isAtEnd(); - void scrollToEnd(); + bool isVisible(const QRectF& sceneRect) const; + bool isAtEnd() const; + void scrollToEnd() const; void scrollBy(int dy); - QPoint viewCursorPos(); - QPointF sceneCursorPos(); - - QRectF viewRect(); - - void resizeEvent(QResizeEvent* event) Q_DECL_OVERRIDE; - - qreal scaleFactor(); - + QPoint viewCursorPos() const; + QPointF sceneCursorPos() const; + QRectF viewRect() const; + qreal scaleFactor() const; void updateSceneSize(); - Q_SIGNALS: - void viewRectChanged(QRectF rect); +Q_SIGNALS: + void viewRectChanged(QRectF rect) const; - public Q_SLOTS: +public Q_SLOTS: void zoomIn(); void zoomOut(); void endAnimation(); - void sceneRectChanged(const QRectF& sceneRect); - void sendViewRectChange(); + void sceneRectChanged(const QRectF& sceneRect) const; + void sendViewRectChange() const; + +private: + void resizeEvent(QResizeEvent*) override; - private: qreal m_scale; QParallelAnimationGroup* m_animation; QPropertyAnimation* m_hAnimation; QPropertyAnimation* m_vAnimation; Worksheet* m_worksheet; }; #endif //WORKSHEETVIEW_H