UBGraphicsScene.h 16.1 KB
Newer Older
1
/*
2
 * Copyright (C) 2015-2018 Département de l'Instruction Publique (DIP-SEM)
Craig Watson's avatar
Craig Watson committed
3
 *
Claudio Valerio's avatar
Claudio Valerio committed
4
 * Copyright (C) 2013 Open Education Foundation
5
 *
Claudio Valerio's avatar
Claudio Valerio committed
6 7
 * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
 * l'Education Numérique en Afrique (GIP ENA)
8
 *
Claudio Valerio's avatar
Claudio Valerio committed
9 10 11
 * This file is part of OpenBoard.
 *
 * OpenBoard is free software: you can redistribute it and/or modify
Claudio Valerio's avatar
Claudio Valerio committed
12 13
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License,
14 15 16 17
 * with a specific linking exception for the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the
 * same license as the "OpenSSL" library).
 *
Claudio Valerio's avatar
Claudio Valerio committed
18
 * OpenBoard is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Claudio Valerio's avatar
Claudio Valerio committed
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Claudio Valerio's avatar
Claudio Valerio committed
21
 * GNU General Public License for more details.
22
 *
Claudio Valerio's avatar
Claudio Valerio committed
23
 * You should have received a copy of the GNU General Public License
Claudio Valerio's avatar
Claudio Valerio committed
24
 * along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
25 26
 */

Claudio Valerio's avatar
Claudio Valerio committed
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#ifndef UBGRAPHICSSCENE_H_
#define UBGRAPHICSSCENE_H_

#include <QtGui>

#include "frameworks/UBCoreGraphicsScene.h"

#include "core/UB.h"

#include "UBItem.h"
#include "tools/UBGraphicsCurtainItem.h"

class UBGraphicsPixmapItem;
class UBGraphicsProxyWidget;
class UBGraphicsSvgItem;
class UBGraphicsPolygonItem;
class UBGraphicsMediaItem;
class UBGraphicsWidgetItem;
class UBGraphicsW3CWidgetItem;
class UBGraphicsAppleWidgetItem;
Yimgo's avatar
Yimgo committed
48
class UBToolWidget;
49 50 51 52 53 54 55 56 57 58 59 60
class UBGraphicsPDFItem;
class UBGraphicsTextItem;
class UBGraphicsRuler;
class UBGraphicsProtractor;
class UBGraphicsCompass;
class UBDocumentProxy;
class UBGraphicsCurtainItem;
class UBGraphicsStroke;
class UBMagnifierParams;
class UBMagnifier;
class UBGraphicsCache;
class UBGraphicsGroupContainerItem;
Ilia Ryabokon's avatar
Ilia Ryabokon committed
61
class UBSelectionFrame;
62
class UBBoardView;
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

const double PI = 4.0 * atan(1.0);

class UBZLayerController : public QObject
{
    Q_OBJECT

public:
    struct ItemLayerTypeData {
        ItemLayerTypeData() : bottomLimit(0), topLimit(0), curValue(0), incStep(1) {;}
        ItemLayerTypeData(qreal bot, qreal top, qreal increment = 1) : bottomLimit(bot), topLimit(top), curValue(bot), incStep(increment) {;}
        qreal bottomLimit; //bottom bound of the layer
        qreal topLimit;//top bound of the layer
        qreal curValue;//current value of variable
        qreal incStep;//incremental step
    };

    enum moveDestination {
        up
        , down
        , top
        , bottom
    };

    typedef QMap<itemLayerType::Enum, ItemLayerTypeData> ScopeMap;

    UBZLayerController(QGraphicsScene *scene);

    qreal getBottomLimit(itemLayerType::Enum key) const {return scopeMap.value(key).bottomLimit;}
    qreal getTopLimit(itemLayerType::Enum key) const {return scopeMap.value(key).topLimit;}
    bool validLayerType(itemLayerType::Enum key) const {return scopeMap.contains(key);}

    static qreal errorNum() {return errorNumber;}

    qreal generateZLevel(itemLayerType::Enum key);
    qreal generateZLevel(QGraphicsItem *item);

    qreal changeZLevelTo(QGraphicsItem *item, moveDestination dest);
    itemLayerType::Enum typeForData(QGraphicsItem *item) const;
102
    void setLayerType(QGraphicsItem *pItem, itemLayerType::Enum pNewType);
103
    void shiftStoredZValue(QGraphicsItem *item, qreal zValue);
104

105 106
    bool zLevelAvailable(qreal z);

107 108 109 110 111 112 113 114 115 116 117 118
private:
    ScopeMap scopeMap;
    static qreal errorNumber;
    QGraphicsScene *mScene;
};

class UBGraphicsScene: public UBCoreGraphicsScene, public UBItem
{
    Q_OBJECT

    public:

Ilia Ryabokon's avatar
Ilia Ryabokon committed
119 120 121 122 123 124 125
    enum clearCase {
        clearItemsAndAnnotations = 0
        , clearAnnotations
        , clearItems
        , clearBackground
    };

126
    //        tmp stub for divide addings scene objects from undo mechanism implementation
127 128 129
        void enableUndoRedoStack(){mUndoRedoStackEnabled = true;}
        void setURStackEnable(bool enable){mUndoRedoStackEnabled = enable;}
        bool isURStackIsEnabled(){return mUndoRedoStackEnabled;}
130

Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
131
        UBGraphicsScene(UBDocumentProxy *parent, bool enableUndoRedoStack = true);
132 133 134 135
        virtual ~UBGraphicsScene();

        virtual UBItem* deepCopy() const;

136 137
        virtual void copyItemParameters(UBItem *copy) const {Q_UNUSED(copy);}

138 139
        UBGraphicsScene* sceneDeepCopy() const;

Ilia Ryabokon's avatar
Ilia Ryabokon committed
140
        void clearContent(clearCase pCase = clearItemsAndAnnotations);
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

        bool inputDevicePress(const QPointF& scenePos, const qreal& pressure = 1.0);
        bool inputDeviceMove(const QPointF& scenePos, const qreal& pressure = 1.0);
        bool inputDeviceRelease();

        void leaveEvent (QEvent* event);

        void addItem(QGraphicsItem* item);
        void removeItem(QGraphicsItem* item);

        void addItems(const QSet<QGraphicsItem*>& item);
        void removeItems(const QSet<QGraphicsItem*>& item);

        UBGraphicsWidgetItem* addWidget(const QUrl& pWidgetUrl, const QPointF& pPos = QPointF(0, 0));
        UBGraphicsAppleWidgetItem* addAppleWidget(const QUrl& pWidgetUrl, const QPointF& pPos = QPointF(0, 0));
156
        UBGraphicsW3CWidgetItem* addW3CWidget(const QUrl& pWidgetUrl, const QPointF& pPos = QPointF(0, 0));
157 158
        void addGraphicsWidget(UBGraphicsWidgetItem* graphicsWidget, const QPointF& pPos = QPointF(0, 0));

159 160
        QPointF lastCenter();
        void setLastCenter(QPointF center);
Yimgo's avatar
Yimgo committed
161

162 163 164
        UBGraphicsMediaItem* addMedia(const QUrl& pMediaFileUrl, bool shouldPlayAsap, const QPointF& pPos = QPointF(0, 0));
        UBGraphicsMediaItem* addVideo(const QUrl& pVideoFileUrl, bool shouldPlayAsap, const QPointF& pPos = QPointF(0, 0));
        UBGraphicsMediaItem* addAudio(const QUrl& pAudioFileUrl, bool shouldPlayAsap, const QPointF& pPos = QPointF(0, 0));
165
        UBGraphicsSvgItem* addSvg(const QUrl& pSvgFileUrl, const QPointF& pPos = QPointF(0, 0), const QByteArray pData = QByteArray());
166 167 168 169 170 171 172 173 174 175 176 177
        UBGraphicsTextItem* addText(const QString& pString, const QPointF& pTopLeft = QPointF(0, 0));

        UBGraphicsTextItem*  addTextWithFont(const QString& pString, const QPointF& pTopLeft = QPointF(0, 0)
                , int pointSize = -1, const QString& fontFamily = "", bool bold = false, bool italic = false);
        UBGraphicsTextItem* addTextHtml(const QString &pString = QString(), const QPointF& pTopLeft = QPointF(0, 0));

        UBGraphicsW3CWidgetItem* addOEmbed(const QUrl& pContentUrl, const QPointF& pPos = QPointF(0, 0));

        UBGraphicsGroupContainerItem *createGroup(QList<QGraphicsItem*> items);
        void addGroup(UBGraphicsGroupContainerItem *groupItem);

        QGraphicsItem* setAsBackgroundObject(QGraphicsItem* item, bool pAdaptTransformation = false, bool expand = false);
178
        void unsetBackgroundObject();
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

        QGraphicsItem* backgroundObject() const
        {
            return mBackgroundObject;
        }

        bool isBackgroundObject(const QGraphicsItem* item) const
        {
            return item == mBackgroundObject;
        }

        QGraphicsItem* scaleToFitDocumentSize(QGraphicsItem* item, bool center = false, int margin = 0, bool expand = false);

        QRectF normalizedSceneRect(qreal ratio = -1.0);

194
        QGraphicsItem *itemForUuid(QUuid uuid);
195 196 197

        void moveTo(const QPointF& pPoint);
        void drawLineTo(const QPointF& pEndPoint, const qreal& pWidth, bool bLineStyle);
198
        void drawLineTo(const QPointF& pEndPoint, const qreal& pStartWidth, const qreal& endWidth, bool bLineStyle);
199 200
        void eraseLineTo(const QPointF& pEndPoint, const qreal& pWidth);
        void drawArcTo(const QPointF& pCenterPoint, qreal pSpanAngle);
201
        void drawCurve(const QList<QPair<QPointF, qreal> > &points);
202
        void drawCurve(const QList<QPointF>& points, qreal startWidth, qreal endWidth);
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

        bool isEmpty() const;

        void setDocument(UBDocumentProxy* pDocument);

        UBDocumentProxy* document() const
        {
            return mDocument;
        }

        bool isDarkBackground() const
        {
            return mDarkBackground;
        }

        bool isLightBackground() const
        {
            return !mDarkBackground;
        }

Craig Watson's avatar
Craig Watson committed
223
        UBPageBackground pageBackground() const
224
        {
Craig Watson's avatar
Craig Watson committed
225
            return mPageBackground;
226 227
        }

228 229 230 231 232
        int backgroundGridSize() const
        {
            return mBackgroundGridSize;
        }

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
        bool hasBackground()
        {
            return (mBackgroundObject != 0);
        }

        void addRuler(QPointF center);
        void addProtractor(QPointF center);
        void addCompass(QPointF center);
        void addTriangle(QPointF center);
        void addMagnifier(UBMagnifierParams params);

        void addMask(const QPointF &center = QPointF());
        void addCache();

        QList<QGraphicsItem*> getFastAccessItems()
        {
            return mFastAccessItems;
        }

        class SceneViewState
        {
            public:
                SceneViewState()
                {
                    zoomFactor = 1;
                    horizontalPosition = 0;
                    verticalPostition = 0;
260
                    mLastSceneCenter = QPointF();
261 262
                }

263
                SceneViewState(qreal pZoomFactor, int pHorizontalPosition, int pVerticalPostition, QPointF sceneCenter = QPointF())// 1595/1605
264 265 266 267
                {
                    zoomFactor = pZoomFactor;
                    horizontalPosition = pHorizontalPosition;
                    verticalPostition = pVerticalPostition;
268 269 270 271 272 273
                    mLastSceneCenter = sceneCenter;
                }

                QPointF lastSceneCenter() // Save Scene Center to replace the view when the scene becomes active
                {
                    return mLastSceneCenter;
274 275
                }

276 277 278 279 280 281 282
                void setLastSceneCenter(QPointF center)
                {
                    mLastSceneCenter = center;
                }

                QPointF mLastSceneCenter;

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
                qreal zoomFactor;
                int horizontalPosition;
                int verticalPostition;
        };

        SceneViewState viewState() const
        {
            return mViewState;
        }

        void setViewState(const SceneViewState& pViewState)
        {
            mViewState = pViewState;
        }

        virtual void setRenderingQuality(UBItem::RenderingQuality pRenderingQuality);

        QList<QUrl> relativeDependencies() const;

        QSize nominalSize();

304 305
        QSize sceneSize();

306 307 308 309
        void setNominalSize(const QSize& pSize);

        void setNominalSize(int pWidth, int pHeight);

310
        qreal changeZLevelTo(QGraphicsItem *item, UBZLayerController::moveDestination dest, bool addUndo=false);
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

        enum RenderingContext
        {
            Screen = 0, NonScreen, PdfExport, Podcast
        };

        void setRenderingContext(RenderingContext pRenderingContext)
        {
            mRenderingContext = pRenderingContext;
        }

        RenderingContext renderingContext() const
        {
            return mRenderingContext;
        }

        QSet<QGraphicsItem*> tools(){ return mTools;}

        void registerTool(QGraphicsItem* item)
        {
            mTools << item;
        }

        const QPointF& previousPoint()
        {
            return mPreviousPoint;
        }

        void setSelectedZLevel(QGraphicsItem *item);
        void setOwnZlevel(QGraphicsItem *item);

        static QUuid getPersonalUuid(QGraphicsItem *item);

344
        UBGraphicsPolygonItem* polygonToPolygonItem(const QPolygonF pPolygon);
Ilia Ryabokon's avatar
Ilia Ryabokon committed
345
        void clearSelectionFrame();
346
        UBBoardView *controlView();
347
        void notifyZChanged(QGraphicsItem *item, qreal zValue);
Nicolas Nenon's avatar
Nicolas Nenon committed
348
        void deselectAllItemsExcept(QGraphicsItem* graphicsItem);
349

350 351
        QRectF annotationsBoundingRect() const;

352
public slots:
353
        void updateSelectionFrame();
354
        void updateSelectionFrameWrapper(int);
Claudio Valerio's avatar
Claudio Valerio committed
355
        void initStroke();
356
        void hideTool();
357

Craig Watson's avatar
Craig Watson committed
358
        void setBackground(bool pIsDark, UBPageBackground pBackground);
359
        void setBackgroundZoomFactor(qreal zoom);
360
        void setBackgroundGridSize(int pSize);
361 362 363
        void setDrawingMode(bool bModeDesktop);
        void deselectAllItems();

364
        UBGraphicsPixmapItem* addPixmap(const QPixmap& pPixmap,
365
            QGraphicsItem* replaceFor,
366 367
            const QPointF& pPos = QPointF(0,0),
            qreal scaleFactor = 1.0,
368 369
            bool pUseAnimation = false,
            bool useProxyForDocumentPath = false);
370 371 372 373 374 375

        void textUndoCommandAdded(UBGraphicsTextItem *textItem);

        void setToolCursor(int tool);

        void selectionChangedProcessing();
376 377
        void moveMagnifier();
        void moveMagnifier(QPoint newPos, bool forceGrab = false);
378 379 380
        void closeMagnifier();
        void zoomInMagnifier();
        void zoomOutMagnifier();
381
        void changeMagnifierMode(int mode);
382 383 384 385 386
        void resizedMagnifier(qreal newPercent);

    protected:

        UBGraphicsPolygonItem* lineToPolygonItem(const QLineF& pLine, const qreal& pWidth);
387 388
        UBGraphicsPolygonItem* lineToPolygonItem(const QLineF &pLine, const qreal &pStartWidth, const qreal &pEndWidth);

389
        UBGraphicsPolygonItem* arcToPolygonItem(const QLineF& pStartRadius, qreal pSpanAngle, qreal pWidth);
390 391
        UBGraphicsPolygonItem* curveToPolygonItem(const QList<QPair<QPointF, qreal> > &points);
        UBGraphicsPolygonItem* curveToPolygonItem(const QList<QPointF> &points, qreal startWidth, qreal endWidth);
392
        void addPolygonItemToCurrentStroke(UBGraphicsPolygonItem* polygonItem);
393 394 395

        void initPolygonItem(UBGraphicsPolygonItem*);

396 397
        void drawEraser(const QPointF& pEndPoint, bool pressed = true);
        void redrawEraser(bool pressed);
398
        void hideEraser();
399
        void drawPointer(const QPointF& pEndPoint, bool isFirstDraw = false);
400
        void drawMarkerCircle(const QPointF& pEndPoint);
401
        void drawPenCircle(const QPointF& pEndPoint);
402
        void hideMarkerCircle();
403
        void hidePenCircle();
404 405 406 407 408 409 410
        void DisposeMagnifierQWidgets();


        virtual void keyReleaseEvent(QKeyEvent * keyEvent);

        void recolorAllItems();

411 412
        virtual void drawItems (QPainter * painter, int numItems,
                               QGraphicsItem * items[], const QStyleOptionGraphicsItem options[], QWidget * widget = 0);
413 414 415 416 417

        QGraphicsItem* rootItem(QGraphicsItem* item) const;

        virtual void drawBackground(QPainter *painter, const QRectF &rect);

418

419 420 421 422
    private:
        void setDocumentUpdated();
        void createEraiser();
        void createPointer();
423
        void createMarkerCircle();
424
        void createPenCircle();
425 426
        void updateEraserColor();
        void updateMarkerCircleColor();
427
        void updatePenCircleColor();
428
        bool hasTextItemWithFocus(UBGraphicsGroupContainerItem* item);
429
        void simplifyCurrentStroke();
430 431

        QGraphicsEllipseItem* mEraser;
432 433
        QGraphicsEllipseItem* mPointer; // "laser" pointer
        QGraphicsEllipseItem* mMarkerCircle; // dotted circle around marker
434
        QGraphicsEllipseItem* mPenCircle; // dotted circle around pen
435 436 437 438 439 440 441

        QSet<QGraphicsItem*> mAddedItems;
        QSet<QGraphicsItem*> mRemovedItems;

        UBDocumentProxy* mDocument;

        bool mDarkBackground;
Craig Watson's avatar
Craig Watson committed
442
        UBPageBackground mPageBackground;
443 444
        int mBackgroundGridSize;

445 446 447 448 449 450 451
        bool mIsDesktopMode;
        qreal mZoomFactor;

        QGraphicsItem* mBackgroundObject;

        QPointF mPreviousPoint;
        qreal mPreviousWidth;
452
        qreal mDistanceFromLastStrokePoint;
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476

        QList<UBGraphicsPolygonItem*> mPreviousPolygonItems;

        SceneViewState mViewState;

        bool mInputDeviceIsPressed;

        QSet<QGraphicsItem*> mTools;

        UBGraphicsPolygonItem *mArcPolygonItem;

        QSize mNominalSize;

        RenderingContext mRenderingContext;

        UBGraphicsStroke* mCurrentStroke;

        int mItemCount;

        QList<QGraphicsItem*> mFastAccessItems; // a local copy as QGraphicsScene::items() is very slow in Qt 4.6


        bool mHasCache;
        //        tmp stub for divide addings scene objects from undo mechanism implementation
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
477
        bool mUndoRedoStackEnabled;
478 479 480 481 482 483

        UBMagnifier *magniferControlViewWidget;
        UBMagnifier *magniferDisplayViewWidget;

        UBZLayerController *mZLayerController;
        UBGraphicsPolygonItem* mpLastPolygon;
484
        UBGraphicsPolygonItem* mTempPolygon;
485 486

        bool mDrawWithCompass;
487
        UBGraphicsPolygonItem *mCurrentPolygon;
Ilia Ryabokon's avatar
Ilia Ryabokon committed
488
        UBSelectionFrame *mSelectionFrame;
489 490 491 492 493
};



#endif /* UBGRAPHICSSCENE_H_ */