| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the examples of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #ifndef CONTEXT2D_H |
| 52 | #define CONTEXT2D_H |
| 53 | |
| 54 | #include "domimage.h" |
| 55 | |
| 56 | #include <QPainter> |
| 57 | #include <QPainterPath> |
| 58 | #include <QString> |
| 59 | #include <QStack> |
| 60 | #include <QMetaType> |
| 61 | #include <QTimerEvent> |
| 62 | |
| 63 | // [3] |
| 64 | class CanvasGradient |
| 65 | { |
| 66 | public: |
| 67 | CanvasGradient(const QGradient &v) |
| 68 | : value(v) {} |
| 69 | CanvasGradient() {} |
| 70 | |
| 71 | QGradient value; |
| 72 | }; |
| 73 | // [3] |
| 74 | |
| 75 | Q_DECLARE_METATYPE(CanvasGradient) |
| 76 | Q_DECLARE_METATYPE(CanvasGradient*) |
| 77 | |
| 78 | class ImageData { |
| 79 | }; |
| 80 | |
| 81 | class QContext2DCanvas; |
| 82 | |
| 83 | //! [0] |
| 84 | class Context2D : public QObject |
| 85 | { |
| 86 | Q_OBJECT |
| 87 | // compositing |
| 88 | Q_PROPERTY(qreal globalAlpha READ globalAlpha WRITE setGlobalAlpha) |
| 89 | Q_PROPERTY(QString globalCompositeOperation READ globalCompositeOperation WRITE setGlobalCompositeOperation) |
| 90 | Q_PROPERTY(QVariant strokeStyle READ strokeStyle WRITE setStrokeStyle) |
| 91 | Q_PROPERTY(QVariant fillStyle READ fillStyle WRITE setFillStyle) |
| 92 | // line caps/joins |
| 93 | Q_PROPERTY(qreal lineWidth READ lineWidth WRITE setLineWidth) |
| 94 | Q_PROPERTY(QString lineCap READ lineCap WRITE setLineCap) |
| 95 | Q_PROPERTY(QString lineJoin READ lineJoin WRITE setLineJoin) |
| 96 | Q_PROPERTY(qreal miterLimit READ miterLimit WRITE setMiterLimit) |
| 97 | // shadows |
| 98 | Q_PROPERTY(qreal shadowOffsetX READ shadowOffsetX WRITE setShadowOffsetX) |
| 99 | Q_PROPERTY(qreal shadowOffsetY READ shadowOffsetY WRITE setShadowOffsetY) |
| 100 | Q_PROPERTY(qreal shadowBlur READ shadowBlur WRITE setShadowBlur) |
| 101 | Q_PROPERTY(QString shadowColor READ shadowColor WRITE setShadowColor) |
| 102 | //! [0] |
| 103 | |
| 104 | public: |
| 105 | Context2D(QObject *parent = 0); |
| 106 | void setSize(int width, int height); |
| 107 | void setSize(const QSize &size); |
| 108 | QSize size() const; |
| 109 | |
| 110 | void clear(); |
| 111 | void reset(); |
| 112 | |
| 113 | // compositing |
| 114 | qreal globalAlpha() const; // (default 1.0) |
| 115 | QString globalCompositeOperation() const; // (default over) |
| 116 | QVariant strokeStyle() const; // (default black) |
| 117 | QVariant fillStyle() const; // (default black) |
| 118 | |
| 119 | void setGlobalAlpha(qreal alpha); |
| 120 | void setGlobalCompositeOperation(const QString &op); |
| 121 | void setStrokeStyle(const QVariant &style); |
| 122 | void setFillStyle(const QVariant &style); |
| 123 | |
| 124 | // line caps/joins |
| 125 | qreal lineWidth() const; // (default 1) |
| 126 | QString lineCap() const; // "butt", "round", "square" (default "butt") |
| 127 | QString lineJoin() const; // "round", "bevel", "miter" (default "miter") |
| 128 | qreal miterLimit() const; // (default 10) |
| 129 | |
| 130 | void setLineWidth(qreal w); |
| 131 | void setLineCap(const QString &s); |
| 132 | void setLineJoin(const QString &s); |
| 133 | void setMiterLimit(qreal m); |
| 134 | |
| 135 | // shadows |
| 136 | qreal shadowOffsetX() const; // (default 0) |
| 137 | qreal shadowOffsetY() const; // (default 0) |
| 138 | qreal shadowBlur() const; // (default 0) |
| 139 | QString shadowColor() const; // (default black) |
| 140 | |
| 141 | void setShadowOffsetX(qreal x); |
| 142 | void setShadowOffsetY(qreal y); |
| 143 | void setShadowBlur(qreal b); |
| 144 | void setShadowColor(const QString &str); |
| 145 | |
| 146 | //! [1] |
| 147 | public slots: |
| 148 | void save(); // push state on state stack |
| 149 | void restore(); // pop state stack and restore state |
| 150 | |
| 151 | void scale(qreal x, qreal y); |
| 152 | void rotate(qreal angle); |
| 153 | void translate(qreal x, qreal y); |
| 154 | void transform(qreal m11, qreal m12, qreal m21, qreal m22, |
| 155 | qreal dx, qreal dy); |
| 156 | void setTransform(qreal m11, qreal m12, qreal m21, qreal m22, |
| 157 | qreal dx, qreal dy); |
| 158 | |
| 159 | CanvasGradient createLinearGradient(qreal x0, qreal y0, |
| 160 | qreal x1, qreal y1); |
| 161 | CanvasGradient createRadialGradient(qreal x0, qreal y0, |
| 162 | qreal r0, qreal x1, |
| 163 | qreal y1, qreal r1); |
| 164 | |
| 165 | // rects |
| 166 | void clearRect(qreal x, qreal y, qreal w, qreal h); |
| 167 | void fillRect(qreal x, qreal y, qreal w, qreal h); |
| 168 | void strokeRect(qreal x, qreal y, qreal w, qreal h); |
| 169 | |
| 170 | // path API |
| 171 | void beginPath(); |
| 172 | void closePath(); |
| 173 | void moveTo(qreal x, qreal y); |
| 174 | void lineTo(qreal x, qreal y); |
| 175 | void quadraticCurveTo(qreal cpx, qreal cpy, qreal x, qreal y); |
| 176 | void bezierCurveTo(qreal cp1x, qreal cp1y, |
| 177 | qreal cp2x, qreal cp2y, qreal x, qreal y); |
| 178 | void arcTo(qreal x1, qreal y1, qreal x2, qreal y2, qreal radius); |
| 179 | void rect(qreal x, qreal y, qreal w, qreal h); |
| 180 | void arc(qreal x, qreal y, qreal radius, |
| 181 | qreal startAngle, qreal endAngle, |
| 182 | bool anticlockwise); |
| 183 | void fill(); |
| 184 | void stroke(); |
| 185 | void clip(); |
| 186 | bool isPointInPath(qreal x, qreal y) const; |
| 187 | //! [1] |
| 188 | |
| 189 | // drawing images |
| 190 | void drawImage(DomImage *image, qreal dx, qreal dy); |
| 191 | void drawImage(DomImage *image, qreal dx, qreal dy, |
| 192 | qreal dw, qreal dh); |
| 193 | void drawImage(DomImage *image, qreal sx, qreal sy, |
| 194 | qreal sw, qreal sh, qreal dx, qreal dy, |
| 195 | qreal dw, qreal dh); |
| 196 | |
| 197 | // pixel manipulation |
| 198 | ImageData getImageData(qreal sx, qreal sy, qreal sw, qreal sh); |
| 199 | void putImageData(ImageData image, qreal dx, qreal dy); |
| 200 | |
| 201 | //! [2] |
| 202 | signals: |
| 203 | void changed(const QImage &image); |
| 204 | //! [2] |
| 205 | |
| 206 | protected: |
| 207 | void timerEvent(QTimerEvent *e); |
| 208 | |
| 209 | private: |
| 210 | void beginPainting(); |
| 211 | const QImage &endPainting(); |
| 212 | void scheduleChange(); |
| 213 | |
| 214 | int m_changeTimerId; |
| 215 | QImage m_image; |
| 216 | QPainter m_painter; |
| 217 | QPainterPath m_path; |
| 218 | |
| 219 | enum DirtyFlag { |
| 220 | DirtyTransformationMatrix = 0x00001, |
| 221 | DirtyClippingRegion = 0x00002, |
| 222 | DirtyStrokeStyle = 0x00004, |
| 223 | DirtyFillStyle = 0x00008, |
| 224 | DirtyGlobalAlpha = 0x00010, |
| 225 | DirtyLineWidth = 0x00020, |
| 226 | DirtyLineCap = 0x00040, |
| 227 | DirtyLineJoin = 0x00080, |
| 228 | DirtyMiterLimit = 0x00100, |
| 229 | MDirtyPen = DirtyStrokeStyle |
| 230 | | DirtyLineWidth |
| 231 | | DirtyLineCap |
| 232 | | DirtyLineJoin |
| 233 | | DirtyMiterLimit, |
| 234 | DirtyShadowOffsetX = 0x00200, |
| 235 | DirtyShadowOffsetY = 0x00400, |
| 236 | DirtyShadowBlur = 0x00800, |
| 237 | DirtyShadowColor = 0x01000, |
| 238 | DirtyGlobalCompositeOperation = 0x2000, |
| 239 | DirtyFont = 0x04000, |
| 240 | DirtyTextAlign = 0x08000, |
| 241 | DirtyTextBaseline = 0x10000, |
| 242 | AllIsFullOfDirt = 0xfffff |
| 243 | }; |
| 244 | |
| 245 | struct State { |
| 246 | State() : flags(0) {} |
| 247 | QTransform matrix; |
| 248 | QPainterPath clipPath; |
| 249 | QBrush strokeStyle; |
| 250 | QBrush fillStyle; |
| 251 | qreal globalAlpha; |
| 252 | qreal lineWidth; |
| 253 | Qt::PenCapStyle lineCap; |
| 254 | Qt::PenJoinStyle lineJoin; |
| 255 | qreal miterLimit; |
| 256 | qreal shadowOffsetX; |
| 257 | qreal shadowOffsetY; |
| 258 | qreal shadowBlur; |
| 259 | QColor shadowColor; |
| 260 | QPainter::CompositionMode globalCompositeOperation; |
| 261 | QFont font; |
| 262 | int textAlign; |
| 263 | int textBaseline; |
| 264 | int flags; |
| 265 | }; |
| 266 | State m_state; |
| 267 | QStack<State> m_stateStack; |
| 268 | }; |
| 269 | |
| 270 | #endif |
| 271 | |