File indexing completed on 2024-05-12 16:36:51

0001 /* This file is part of the KDE project
0002    Copyright (C) 2012 Paul Mendez <paulestebanms@gmail.com>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "KPrViewModePreviewShapeAnimations.h"
0021 
0022 //Stage Headers
0023 #include "KPrShapeManagerAnimationStrategy.h"
0024 #include "KPrPageSelectStrategyActive.h"
0025 #include "animations/KPrAnimationCache.h"
0026 #include "KPrView.h"
0027 //Calligra Headers
0028 #include <KoPACanvasBase.h>
0029 #include <KoShapeManager.h>
0030 #include <KoPAViewBase.h>
0031 #include <KoPAPageBase.h>
0032 #include <KoPageLayout.h>
0033 #include <KoCanvasController.h>
0034 #include <KoZoomController.h>
0035 #include <KoShapePaintingContext.h>
0036 #include <KPrViewModePresentation.h>
0037 //Qt Headers
0038 #include <QPainter>
0039 
0040 
0041 KPrViewModePreviewShapeAnimations::KPrViewModePreviewShapeAnimations(KoPAViewBase *view, KoPACanvasBase *canvas)
0042     : KoPAViewMode(view, canvas)
0043     , m_savedViewMode(0)
0044 {
0045     connect(&m_timeLine, SIGNAL(finished()), this, SLOT(activateSavedViewMode()));
0046     m_timeLine.setCurveShape(QTimeLine::LinearCurve);
0047     m_timeLine.setUpdateInterval(20);
0048 }
0049 
0050 KPrViewModePreviewShapeAnimations::~KPrViewModePreviewShapeAnimations()
0051 {
0052     delete m_animationCache;
0053 }
0054 
0055 void KPrViewModePreviewShapeAnimations::paint(KoPACanvasBase *canvas, QPainter &painter, const QRectF &paintRect)
0056 {
0057 #ifdef NDEBUG
0058     Q_UNUSED(canvas);
0059 #else
0060     Q_ASSERT(m_canvas == canvas);
0061 #endif
0062 
0063 
0064     painter.translate(-m_canvas->documentOffset());
0065     painter.setRenderHint(QPainter::Antialiasing);
0066     QRect clipRect = paintRect.translated(m_canvas->documentOffset()).toRect();
0067     painter.setClipRect(clipRect);
0068     painter.translate(m_canvas->documentOrigin().x(), m_canvas->documentOrigin().y());
0069     KoViewConverter *converter = m_view->viewConverter(m_canvas);
0070     KoShapePaintingContext context;
0071     view()->activePage()->paintBackground(painter, *converter, context);
0072     canvas->shapeManager()->paint(painter, *converter, true);
0073 }
0074 
0075 void KPrViewModePreviewShapeAnimations::tabletEvent(QTabletEvent *event, const QPointF &point)
0076 {
0077     Q_UNUSED(event);
0078     Q_UNUSED(point);
0079 }
0080 
0081 void KPrViewModePreviewShapeAnimations::mousePressEvent(QMouseEvent *event, const QPointF &point)
0082 {
0083     Q_UNUSED(event);
0084     Q_UNUSED(point);
0085 }
0086 
0087 void KPrViewModePreviewShapeAnimations::mouseDoubleClickEvent(QMouseEvent *event, const QPointF &point)
0088 {
0089     Q_UNUSED(event);
0090     Q_UNUSED(point);
0091 }
0092 
0093 void KPrViewModePreviewShapeAnimations::mouseMoveEvent(QMouseEvent *event, const QPointF &point)
0094 {
0095     Q_UNUSED(event);
0096     Q_UNUSED(point);
0097 }
0098 
0099 void KPrViewModePreviewShapeAnimations::mouseReleaseEvent(QMouseEvent *event, const QPointF &point)
0100 {
0101     Q_UNUSED(event);
0102     Q_UNUSED(point);
0103 }
0104 
0105 void KPrViewModePreviewShapeAnimations::shortcutOverrideEvent(QKeyEvent *event)
0106 {
0107     Q_UNUSED(event);
0108 }
0109 
0110 void KPrViewModePreviewShapeAnimations::keyPressEvent(QKeyEvent *event)
0111 {
0112     Q_UNUSED(event);
0113 }
0114 
0115 void KPrViewModePreviewShapeAnimations::keyReleaseEvent(QKeyEvent *event)
0116 {
0117     Q_UNUSED(event);
0118 }
0119 
0120 void KPrViewModePreviewShapeAnimations::wheelEvent(QWheelEvent *event, const QPointF &point)
0121 {
0122     Q_UNUSED(event);
0123     Q_UNUSED(point);
0124 }
0125 
0126 void KPrViewModePreviewShapeAnimations::activate(KoPAViewMode *previousViewMode)
0127 {
0128     m_savedViewMode = previousViewMode;               // store the previous view mode
0129     m_animationCache = new KPrAnimationCache();
0130     m_canvas->shapeManager()->setPaintingStrategy(new KPrShapeManagerAnimationStrategy(m_canvas->shapeManager(),
0131                                                                                        m_animationCache,
0132                                                        new KPrPageSelectStrategyActive(m_canvas)));
0133 
0134     // the update of the canvas is needed so that the old page gets drawn fully before the effect starts
0135 
0136     const KoPageLayout &layout = activePageLayout();
0137 
0138     QSizeF pageSize(layout.width, layout.height);
0139 
0140     //calculate size of union page + viewport
0141     QSizeF documentMinSize(view()->zoomController()->documentSize());
0142 
0143     // create a rect out of it with origin in tp left of page
0144     QRectF documentRect(QPointF((documentMinSize.width() - layout.width) * -0.5,
0145                                (documentMinSize.height() - layout.height) * -0.5),
0146                        documentMinSize);
0147 
0148     QPointF offset = -documentRect.topLeft();
0149     m_canvas->setDocumentOrigin(offset);
0150     m_view->zoomController()->setPageSize(pageSize);
0151 
0152     m_canvas->resourceManager()->setResource(KoCanvasResourceManager::PageSize, pageSize);
0153     m_canvas->repaint();
0154 
0155 
0156     m_timeLine.setDuration(m_shapeAnimation->duration());
0157     m_timeLine.setCurrentTime(0);
0158     m_animationCache->clear();
0159     m_animationCache->setPageSize(view()->zoomController()->pageSize());
0160     qreal zoom;
0161     view()->zoomHandler()->zoom(&zoom, &zoom);
0162     m_animationCache->setZoom(zoom);
0163     m_shapeAnimation->init(m_animationCache, 0);
0164     m_animationCache->startStep(0);
0165     m_timeLine.start();
0166     connect(&m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate()));
0167 }
0168 
0169 void KPrViewModePreviewShapeAnimations::deactivate()
0170 {
0171     if ( m_timeLine.state() == QTimeLine::Running ) { // there are still shape animations running
0172             m_timeLine.stop();
0173     }
0174     m_savedViewMode = 0;
0175     m_shapeAnimation->deactivate();
0176     m_canvas->shapeManager()->setPaintingStrategy(new KoShapeManagerPaintingStrategy(m_canvas->shapeManager()));
0177     delete (m_animationCache);
0178     m_animationCache = 0;
0179     disconnect(&m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate()));
0180 }
0181 
0182 void KPrViewModePreviewShapeAnimations::updateActivePage(KoPAPageBase *page)
0183 {
0184     m_view->setActivePage(page);
0185 }
0186 
0187 void KPrViewModePreviewShapeAnimations::setShapeAnimation(KPrShapeAnimation *shapeAnimation)
0188 {
0189     m_shapeAnimation = shapeAnimation;
0190     if (m_savedViewMode) {           //stop the previous playing
0191         activateSavedViewMode();
0192     }
0193 }
0194 
0195 void KPrViewModePreviewShapeAnimations::stopAnimation()
0196 {
0197     if (m_shapeAnimation) {
0198         m_timeLine.stop();
0199     }
0200 }
0201 
0202 void KPrViewModePreviewShapeAnimations::activateSavedViewMode()
0203 {
0204     if (KPrView *view = dynamic_cast<KPrView *>(m_view)) {
0205         if (m_savedViewMode == view->presentationMode()) {
0206             view->showNormal();
0207             return;
0208         }
0209     }
0210     m_view->setViewMode(m_savedViewMode);
0211 }
0212 
0213 void KPrViewModePreviewShapeAnimations::animate()
0214 {
0215     m_animationCache->next();
0216     m_shapeAnimation->setCurrentTime(m_timeLine.currentTime());
0217     canvas()->repaint();
0218 }