File indexing completed on 2024-05-19 04:24:46

0001 /* This file is part of the KDE project
0002 
0003    SPDX-FileCopyrightText: 2006, 2010 Boudewijn Rempt <boud@valdyas.org>
0004    SPDX-FileCopyrightText: 2006, 2010 Thomas Zander <zander@kde.org>
0005    SPDX-FileCopyrightText: 2006 Thorsten Zachmann <zachmann@kde.org>
0006 
0007    SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #ifndef KOCANVASBASE_H
0011 #define KOCANVASBASE_H
0012 
0013 #include <QPoint>
0014 
0015 #include "kritaflake_export.h"
0016 
0017 class KUndo2Command;
0018 
0019 class KoUnit;
0020 class KoCanvasResourceProvider;
0021 class KoShapeManager;
0022 class KoToolProxy;
0023 class KoViewConverter;
0024 class KoShapeController;
0025 class KoShapeControllerBase;
0026 class KoCanvasController;
0027 class KoShape;
0028 class KoSnapGuide;
0029 class KoSelectedShapesProxy;
0030 
0031 class QWidget;
0032 class QCursor;
0033 class QPointF;
0034 class QRectF;
0035 class QSizeF;
0036 
0037 #include <QObject>
0038 
0039 /**
0040  * KoCanvasBase is the interface actual application canvas classes
0041  * should implement. Flake tools know about the canvas, so they can
0042  * do things like scroll, redraw, set a cursor etc.
0043  */
0044 class KRITAFLAKE_EXPORT KoCanvasBase : public QObject
0045 {
0046     Q_OBJECT
0047 public:
0048 
0049     /**
0050      * The constructor.
0051      * @param shapeController the implementation of the shapeController that the
0052      *   application provides to allow shapes to be added in multiple views.
0053      */
0054     explicit KoCanvasBase(KoShapeControllerBase *shapeController, KoCanvasResourceProvider *sharedResourceManager = 0);
0055     ~KoCanvasBase() override;
0056 
0057 public:
0058 
0059     /**
0060      * @return true if opengl can be used directly on the canvas
0061      */
0062     virtual bool canvasIsOpenGL() const { return false; }
0063 
0064     /**
0065      * retrieve the grid size setting.
0066      * The grid spacing will be provided in pt.
0067      * @param horizontal a pointer to a qreal that will be filled with the horizontal grid-spacing
0068      * @param vertical a pointer to a qreal that will be filled with the vertical grid-spacing
0069      */
0070     virtual void gridSize(QPointF *offset, QSizeF *spacing) const = 0;
0071 
0072     /**
0073      * return if snap to grid is enabled.
0074      * @return if snap to grid is enabled.
0075      */
0076     virtual bool snapToGrid() const = 0;
0077 
0078 
0079     /**
0080      * set the specified cursor on this canvas
0081      *
0082      * @param cursor the new cursor
0083      * @return the old cursor
0084      */
0085     virtual void setCursor(const QCursor &cursor) = 0;
0086 
0087     /**
0088      * Adds a command to the history. Call this for each @p command you create.
0089      * This will also execute the command.
0090      * This means, most of the application's code will look like
0091      *    MyCommand * cmd = new MyCommand( parameters );
0092      *    canvas.addCommand( cmd );
0093      *
0094      * Note that the command history takes ownership of the command, it will delete
0095      * it when the undo limit is reached, or when deleting the command history itself.
0096      * @param command the command to add
0097      */
0098     virtual void addCommand(KUndo2Command *command) = 0;
0099 
0100     /**
0101      * Return the current shapeManager. WARNING: the shape manager can switch
0102      * in time, e.g. when a layer is changed. Please don't keep any persistent
0103      * connections to it. Instead please use selectedShapesProxy(),
0104      * which is guaranteed to be the same throughout the life of the canvas.
0105      *
0106      * @return the current shapeManager
0107      */
0108     virtual KoShapeManager *shapeManager() const = 0;
0109 
0110     /**
0111      * @brief selectedShapesProxy() is a special interface for keeping a persistent connections
0112      * to selectionChanged() and selectionContentChanged() signals. While shapeManager() can change
0113      * throughout the life time of the canvas, selectedShapesProxy() is guaranteed to stay the same.
0114      * @return persistent KoSelectedShapesProxy object
0115      */
0116     virtual KoSelectedShapesProxy *selectedShapesProxy() const = 0;
0117 
0118     /**
0119      * Tell the canvas to repaint the specified rectangle. The coordinates
0120      * are document coordinates, not view coordinates.
0121      */
0122     virtual void updateCanvas(const QRectF &rc) = 0;
0123 
0124     /**
0125      * Return the proxy to the active tool (determining which tool
0126      * is really, really active is hard when tablets are involved,
0127      * so leave that to others.
0128      */
0129     virtual KoToolProxy *toolProxy() const = 0;
0130 
0131     /**
0132      * Return the viewConverter for this view.
0133      * @return the viewConverter for this view.
0134      */
0135     virtual const KoViewConverter *viewConverter() const = 0;
0136     virtual KoViewConverter *viewConverter() = 0;
0137 
0138     /**
0139      * Convert a coordinate in pixels to pt.
0140      * @param viewPoint the point in the coordinate system of the widget, or window.
0141      */
0142     virtual QPointF viewToDocument(const QPointF &viewPoint) const;
0143 
0144     /**
0145      * Return the widget that will be added to the scrollArea.
0146      */
0147     virtual QWidget *canvasWidget() = 0;
0148 
0149     /**
0150      * Return the widget that will be added to the scrollArea.
0151      */
0152     virtual const QWidget *canvasWidget() const = 0;
0153 
0154     /**
0155      * Return the unit of the current document for initialization of the widgets created
0156      * by the flake framework.
0157      * @see KoDocument::unit()
0158      */
0159     virtual KoUnit unit() const = 0;
0160 
0161     /**
0162      * Called when the user tries to move the argument shape to allow the application to limit the
0163      * users movement to stay within the document bounds.
0164      * An implementation can alter the parameter move to make sure that if the distance moved
0165      * is applied to the shape it will not become unreachable for the user.
0166      * The default implementation does not restrict movement.
0167      * @param shape the shape that will be moved soon.
0168      * @param move the distance the caller intends to move the shape.
0169      */
0170     virtual void clipToDocument(const KoShape *shape, QPointF &move) const;
0171 
0172     /**
0173      * Return the position of the document origin inside the canvas widget, in pixels.
0174      * By default the origin of the canvas widget and the position of the
0175      * document origin are coincident, thus an empty point is returned.
0176      */
0177     virtual QPoint documentOrigin() const {
0178         return QPoint(0, 0);
0179     }
0180 
0181     /**
0182      * disconnect the given QObject completely and utterly from any and all
0183      * connections it has to any QObject owned by the canvas. Do this in
0184      * the setCanvas of every KoCanvasObserver.
0185      */
0186     virtual void disconnectCanvasObserver(QObject *object);
0187 
0188     /**
0189      * Return a pointer to the resource manager associated with this
0190      * canvas. The resource manager contains per-canvas settings such
0191      * as current foreground and background color.
0192      * If instead of per-canvas resources you need per-document resources
0193      * you can by going via the shapeController instead;
0194      * @code
0195      *   canvasBase->shapeController()->resourceManager();
0196      * @endcode
0197      * @see KoShapeController::resourceManager()
0198      */
0199     KoCanvasResourceProvider *resourceManager() const;
0200 
0201     /**
0202      * Return the shape controller for this canvas.
0203      * A shape controller is used to create or delete shapes and show the relevant dialogs to the user.
0204      */
0205     KoShapeController *shapeController() const;
0206 
0207     /**
0208      * Return the canvas controller for this canvas.
0209      */
0210     KoCanvasController *canvasController() const;
0211 
0212     /**
0213      * @brief Scrolls the content of the canvas so that the given rect is visible.
0214      *
0215      * The rect is to be specified in document coordinates.
0216      *
0217      * @param rect the rectangle to make visible
0218      */
0219     virtual void ensureVisible(const QRectF &rect);
0220 
0221     /**
0222      * Returns the snap guide of the canvas
0223      */
0224     KoSnapGuide *snapGuide() const;
0225 
0226     /// called by KoCanvasController to set the controller that handles this canvas.
0227     void setCanvasController(KoCanvasController *controller);
0228 
0229 private:
0230     // we need a KoShapeControllerBase so that it can work
0231     KoCanvasBase();
0232 
0233     class Private;
0234     Private * const d;
0235 };
0236 
0237 #endif // KOCANVASBASE_H