File indexing completed on 2024-05-12 16:37:09

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2002-2006 David Faure <faure@kde.org>
0003  * Copyright (C) 2005-2006 Thomas Zander <zander@kde.org>
0004  * Copyright (C) 2009 Inge Wallin <inge@lysator.liu.se>
0005  * Copyright (C) 2010-2011 Boudewijn Rempt <boud@kogmbh.com>
0006  *
0007  * This library is free software; you can redistribute it and/or
0008  * modify it under the terms of the GNU Library General Public
0009  * License as published by the Free Software Foundation; either
0010  * version 2 of the License, or (at your option) any later version.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Library General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Library General Public License
0018  * along with this library; see the file COPYING.LIB.  If not, write to
0019  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020  * Boston, MA 02110-1301, USA.
0021  */
0022 #ifndef KWCANVASBASE_H
0023 #define KWCANVASBASE_H
0024 
0025 #include <QCache>
0026 #include <QDebug>
0027 
0028 #include "KWDocument.h"
0029 #include "words_export.h"
0030 #include "KWViewMode.h"
0031 #include "KWPage.h"
0032 
0033 #include <KoCanvasBase.h>
0034 
0035 #include <QRectF>
0036 #include <QImage>
0037 #include <QQueue>
0038 
0039 class QRect;
0040 class QPainter;
0041 
0042 class KoToolProxy;
0043 class KoShape;
0044 class KoViewConverter;
0045 class KWPageCacheManager;
0046 
0047 class WORDS_EXPORT KWCanvasBase : public KoCanvasBase
0048 {
0049 public:
0050     static const qreal AnnotationAreaWidth;
0051 
0052     explicit KWCanvasBase(KWDocument *document, QObject *parent = 0);
0053     ~KWCanvasBase() override;
0054 
0055 public: // KoCanvasBase interface methods.
0056 
0057     /// reimplemented method from superclass
0058     void gridSize(qreal *horizontal, qreal *vertical) const override;
0059 
0060     /// reimplemented method from superclass
0061     void addCommand(KUndo2Command *command) override;
0062 
0063     /// reimplemented method from superclass
0064     KoShapeManager *shapeManager() const override;
0065 
0066     /// reimplemented method from superclass
0067     void updateCanvas(const QRectF &rc) override;
0068 
0069     /// reimplemented method from superclass
0070     KoUnit unit() const override;
0071 
0072     /// reimplemented method from superclass
0073     KoToolProxy *toolProxy() const override;
0074 
0075     /// reimplemented method from superclass
0076     void clipToDocument(const KoShape *shape, QPointF &move) const override;
0077 
0078     /// reimplemented method from superclass
0079     KoGuidesData *guidesData() override;
0080 
0081     /// reimplemented method from superclass
0082     KoViewConverter *viewConverter() const override;
0083 
0084     /// return the document that this canvas works on
0085     KWDocument *document() const;
0086 
0087     /// return the viewMode currently associated with this canvas
0088     KWViewMode *viewMode() const;
0089 
0090     /// reimplemented method from superclass
0091     void ensureVisible(const QRectF &rect) override;
0092 
0093     /**
0094      * Enable or disable the page cache. The cache stores the rendered pages. It is
0095      * emptied when the zoomlevel changes.
0096      *
0097      * @param enabled: if true, we cache the contents of the document for this canvas,
0098      *  for the current zoomlevel
0099      * @param cacheSize: the maximum size for the cache. The cache will throw away
0100      *  pages once this size is reached. Depending on Qt's implementation of QCache, the
0101      *  unit is pages.
0102      * @param maxZoom above this zoomlevel we'll paint a scaled version of the cache, instead
0103      *  of creating a new cache
0104      */
0105     virtual void setCacheEnabled(bool enabled, int cacheSize = 50, qreal maxZoom = 2.0);
0106 
0107     /**
0108      * return whether annotations are shown in the canvas.
0109      */
0110     bool showAnnotations() const;
0111     void setShowAnnotations(bool doShow);
0112 
0113     /// @return the offset of the document in this canvas
0114     QPoint documentOffset() const;
0115 
0116 protected:
0117 
0118     void paint(QPainter &painter, const QRectF &paintRect);
0119 
0120     void paintBackgrounds(QPainter &painter, KWViewMode::ViewMap &viewMap);
0121     void paintPageDecorations(QPainter &painter, KWViewMode::ViewMap &viewMap);
0122     void paintBorder(QPainter &painter, KWViewMode::ViewMap &viewMap);
0123     void doPaintBorder(QPainter &painter, const KoBorder &border, const QRectF &borderRect) const;
0124 
0125     void paintGrid(QPainter &painter, KWViewMode::ViewMap &viewMap);
0126 
0127     /**
0128      * paint one border along one of the 4 sides.
0129      * @param painter the Qt painter
0130      * @param borderData the border data
0131      * @param lineStart the line start
0132      * @param lineEnd the line end
0133      * @param zoom the zoom quotient
0134      * @param inwardsX is the horizontal vector (with value -1, 0 or 1) for the vector
0135      * pointing inwards for the border part nearest the center of the page.
0136      * @param inwardsY is the vertical vector (with value -1, 0 or 1) for the vector
0137      * pointing inwards for the border part nearest the center of the page.
0138      */
0139     void paintBorderSide(QPainter &painter, const KoBorder::BorderData &borderData,
0140                          const QPointF &lineStart, const QPointF &lineEnd, qreal zoom,
0141                          int inwardsX, int inwardsY) const;
0142 
0143     virtual void updateCanvasInternal(const QRectF &clip) = 0;
0144 
0145 protected:
0146 
0147     KWDocument *m_document;
0148     KoShapeManager *m_shapeManager;
0149     KoToolProxy *m_toolProxy;
0150     KWViewMode *m_viewMode;
0151     QPoint m_documentOffset;
0152     KoViewConverter *m_viewConverter;
0153     bool  m_showAnnotations;   //< true if annotations should be shown in the canvas
0154 
0155     bool m_cacheEnabled;
0156     qreal m_currentZoom;
0157     qreal m_maxZoom; //< above this zoomlevel we scale the cached image, instead of recreating the cache.
0158     KWPageCacheManager *m_pageCacheManager;
0159     int m_cacheSize;
0160 
0161 };
0162 
0163 #endif // KWCANVASBASE_H