File indexing completed on 2024-05-12 15:56:38
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 cavas, 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 KoViewConverter *viewConverter() const = 0; 0136 0137 /** 0138 * Convert a coordinate in pixels to pt. 0139 * @param viewPoint the point in the coordinate system of the widget, or window. 0140 */ 0141 virtual QPointF viewToDocument(const QPointF &viewPoint) const; 0142 0143 /** 0144 * Return the widget that will be added to the scrollArea. 0145 */ 0146 virtual QWidget *canvasWidget() = 0; 0147 0148 /** 0149 * Return the widget that will be added to the scrollArea. 0150 */ 0151 virtual const QWidget *canvasWidget() const = 0; 0152 0153 /** 0154 * Return the unit of the current document for initialization of the widgets created 0155 * by the flake framework. 0156 * @see KoDocument::unit() 0157 */ 0158 virtual KoUnit unit() const = 0; 0159 0160 /** 0161 * Called when the user tries to move the argument shape to allow the application to limit the 0162 * users movement to stay within the document bounds. 0163 * An implementation can alter the parameter move to make sure that if the distance moved 0164 * is applied to the shape it will not become unreachable for the user. 0165 * The default implementation does not restrict movement. 0166 * @param shape the shape that will be moved soon. 0167 * @param move the distance the caller intends to move the shape. 0168 */ 0169 virtual void clipToDocument(const KoShape *shape, QPointF &move) const; 0170 0171 /** 0172 * Return the position of the document origin inside the canvas widget, in pixels. 0173 * By default the origin of the canvas widget and the position of the 0174 * document origin are coincident, thus an empty point is returned. 0175 */ 0176 virtual QPoint documentOrigin() const { 0177 return QPoint(0, 0); 0178 } 0179 0180 /** 0181 * This method should somehow call QWidget::updateMicroFocus() on the canvas widget. 0182 */ 0183 virtual void updateInputMethodInfo() = 0; 0184 0185 /** 0186 * disconnect the given QObject completely and utterly from any and all 0187 * connections it has to any QObject owned by the canvas. Do this in 0188 * the setCanvas of every KoCanvasObserver. 0189 */ 0190 virtual void disconnectCanvasObserver(QObject *object); 0191 0192 /** 0193 * Return a pointer to the resource manager associated with this 0194 * canvas. The resource manager contains per-canvas settings such 0195 * as current foreground and background color. 0196 * If instead of per-canvas resources you need per-document resources 0197 * you can by going via the shapeController instead; 0198 * @code 0199 * canvasBase->shapeController()->resourceManager(); 0200 * @endcode 0201 * @see KoShapeController::resourceManager() 0202 */ 0203 KoCanvasResourceProvider *resourceManager() const; 0204 0205 /** 0206 * Return the shape controller for this canvas. 0207 * A shape controller is used to create or delete shapes and show the relevant dialogs to the user. 0208 */ 0209 KoShapeController *shapeController() const; 0210 0211 /** 0212 * Return the canvas controller for this canvas. 0213 */ 0214 KoCanvasController *canvasController() const; 0215 0216 /** 0217 * @brief Scrolls the content of the canvas so that the given rect is visible. 0218 * 0219 * The rect is to be specified in document coordinates. 0220 * 0221 * @param rect the rectangle to make visible 0222 */ 0223 virtual void ensureVisible(const QRectF &rect); 0224 0225 /** 0226 * Returns the snap guide of the canvas 0227 */ 0228 KoSnapGuide *snapGuide() const; 0229 0230 /// called by KoCanvasController to set the controller that handles this canvas. 0231 void setCanvasController(KoCanvasController *controller); 0232 0233 private: 0234 // we need a KoShapeControllerBase so that it can work 0235 KoCanvasBase(); 0236 0237 class Private; 0238 Private * const d; 0239 }; 0240 0241 #endif // KOCANVASBASE_H