UBGraphicsWidgetItem.h 8.54 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
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
Claudio Valerio's avatar
Claudio Valerio committed
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,
Claudio Valerio's avatar
Claudio Valerio committed
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.
Claudio Valerio's avatar
Claudio Valerio committed
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/>.
Claudio Valerio's avatar
Claudio Valerio committed
25
 */
26 27


Claudio Valerio's avatar
Claudio Valerio committed
28

Claudio Valerio's avatar
Claudio Valerio committed
29

Claudio Valerio's avatar
Claudio Valerio committed
30 31 32 33 34
#ifndef UBGRAPHICSWIDGETITEM_H
#define UBGRAPHICSWIDGETITEM_H

#include <QtGui>
#include <QtWebKit>
35
#include <QDomElement>
36
#include <QGraphicsWebView>
Claudio Valerio's avatar
Claudio Valerio committed
37

38
#include "core/UB.h"
Claudio Valerio's avatar
Claudio Valerio committed
39

40 41
#include "UBItem.h"
#include "UBResizableGraphicsItem.h"
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
42

Claudio Valerio's avatar
Claudio Valerio committed
43 44 45 46
class UBWidgetUniboardAPI;
class UBGraphicsScene;
class UBW3CWidgetAPI;
class UBW3CWidgetWebStorageAPI;
47 48
class UBGraphiscItem;
class UBGraphiscItemDelegate;
Claudio Valerio's avatar
Claudio Valerio committed
49

50 51 52 53 54 55 56 57
struct UBWidgetType
{
    enum Enum
    {
        W3C = 0, Apple, Other
    };
};

Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
58
class UBGraphicsWidgetItem : public QGraphicsWebView, public UBItem, public UBResizableGraphicsItem, public UBGraphicsItem
Claudio Valerio's avatar
Claudio Valerio committed
59
{
60
    Q_OBJECT
Claudio Valerio's avatar
Claudio Valerio committed
61 62

    public:
Yimgo's avatar
Yimgo committed
63
        UBGraphicsWidgetItem(const QUrl &pWidgetUrl = QUrl(), QGraphicsItem *parent = 0);
Claudio Valerio's avatar
Claudio Valerio committed
64 65
        ~UBGraphicsWidgetItem();

66 67 68 69
        enum { Type = UBGraphicsItemType::GraphicsWidgetItemType };

        virtual int type() const { return Type; }

Claudio Valerio's avatar
Claudio Valerio committed
70 71
        virtual void initialize();

Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
72 73 74 75
        virtual void resize(qreal w, qreal h);
        virtual void resize(const QSizeF & size);
        virtual QSizeF size() const;

76 77 78
        QUrl mainHtml();
        void loadMainHtml();
        QUrl widgetUrl();
79
        void widgetUrl(QUrl url) { mWidgetUrl = url; }
80
        QString mainHtmlFileName();
Claudio Valerio's avatar
Claudio Valerio committed
81

82 83
        bool canBeContent();
        bool canBeTool();
Claudio Valerio's avatar
Claudio Valerio committed
84 85

        QString preference(const QString& key) const;
86
        void setPreference(const QString& key, QString value);
Claudio Valerio's avatar
Claudio Valerio committed
87 88 89 90 91
        QMap<QString, QString> preferences() const;
        void removePreference(const QString& key);
        void removeAllPreferences();

        QString datastoreEntry(const QString& key) const;
92
        void setDatastoreEntry(const QString& key, QString value);
Claudio Valerio's avatar
Claudio Valerio committed
93 94 95
        QMap<QString, QString> datastoreEntries() const;
        void removeDatastoreEntry(const QString& key);
        void removeAllDatastoreEntries();
96

97
        void removeScript();
98

99
        void processDropEvent(QGraphicsSceneDragDropEvent *event);
Ivan Ilin's avatar
Ivan Ilin committed
100
        bool isDropableData(const QMimeData *data) const;
101

102 103 104 105
        virtual QUrl getOwnFolder() const;
        virtual void setOwnFolder(const QUrl &newFolder);
        virtual void setSnapshotPath(const QUrl &newFilePath);
        virtual QUrl getSnapshotPath();
106

107
        virtual void clearSource();
108

root's avatar
root committed
109
        virtual void setUuid(const QUuid &pUuid);
110

111
        QSize nominalSize() const;
112

113
        bool hasLoadedSuccessfully() const;
114

115 116 117
        bool freezable();
        bool resizable();
        bool isFrozen();
118

119 120 121
        QPixmap snapshot();
        void setSnapshot(const QPixmap& pix);
        QPixmap takeSnapshot();
122

123 124
        virtual UBItem* deepCopy() const = 0;
        virtual UBGraphicsScene* scene();
125 126

        static int widgetType(const QUrl& pUrl);
127
        static QString widgetName(const QUrl& pUrl);
128
        static QString iconFilePath(const QUrl& pUrl);
129 130 131 132

    public slots:
        void freeze();
        void unFreeze();
133

Claudio Valerio's avatar
Claudio Valerio committed
134
    protected:
135 136 137 138 139 140
        enum OSType
        {
            type_NONE = 0, // 0000
            type_WIN  = 1, // 0001
            type_MAC  = 2, // 0010
            type_UNIX = 4, // 0100
141
            type_ALL  = 7 // 0111
142 143
        };

144
        bool mFirstReleaseAfterMove;
145 146 147 148 149 150 151 152 153
        bool mInitialLoadDone;
        bool mIsFreezable;
        bool mIsResizable;
        bool mLoadIsErronous;
        bool mMouseIsPressed;
        int mCanBeContent;
        int mCanBeTool;
        QSize mNominalSize;
        QString mMainHtmlFileName;
154
        QUrl mMainHtmlUrl;
155 156 157
        QUrl mWidgetUrl;
        QMap<QString, QString> mDatastore;
        QMap<QString, QString> mPreferences;
Claudio Valerio's avatar
Claudio Valerio committed
158

Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
159

160 161
        virtual bool event(QEvent *event);
        virtual void dropEvent(QGraphicsSceneDragDropEvent *event);
Claudio Valerio's avatar
Claudio Valerio committed
162 163
        virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
        virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
164 165 166 167
        virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
        virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
        virtual void sendJSEnterEvent();
        virtual void sendJSLeaveEvent();
168
        virtual void injectInlineJavaScript();
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
169 170
        virtual void wheelEvent(QGraphicsSceneWheelEvent *event);
        virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value);
171
        virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
Claudio Valerio's avatar
Claudio Valerio committed
172 173 174 175

    protected slots:
        void geometryChangeRequested(const QRect& geom);
        virtual void javaScriptWindowObjectCleared();
176
        void mainFrameLoadFinished(bool ok);
Claudio Valerio's avatar
Claudio Valerio committed
177

shibakaneki's avatar
shibakaneki committed
178
    private slots:
179
        void onLinkClicked(const QUrl& url);
180
        void initialLayoutCompleted();
shibakaneki's avatar
shibakaneki committed
181

Claudio Valerio's avatar
Claudio Valerio committed
182
    private:
183 184
        bool mIsFrozen;
        bool mIsTakingSnapshot;
185
        bool mShouldMoveWidget;
Claudio Valerio's avatar
Claudio Valerio committed
186
        UBWidgetUniboardAPI* mUniboardAPI;
187 188
        QPixmap mSnapshot;
        QPointF mLastMousePos;
189
        QUrl ownFolder;
190
        QUrl SnapshotFile;
191 192

        static bool sInlineJavaScriptLoaded;
193
        static QStringList sInlineJavaScripts;
Claudio Valerio's avatar
Claudio Valerio committed
194 195 196 197
};

class UBGraphicsAppleWidgetItem : public UBGraphicsWidgetItem
{
198
    Q_OBJECT
Claudio Valerio's avatar
Claudio Valerio committed
199 200 201

    public:
        UBGraphicsAppleWidgetItem(const QUrl& pWidgetUrl, QGraphicsItem *parent = 0);
202
        ~UBGraphicsAppleWidgetItem();
203 204

        virtual void copyItemParameters(UBItem *copy) const;
root's avatar
root committed
205
        virtual void setUuid(const QUuid &pUuid);
206
        virtual UBItem* deepCopy() const;
Claudio Valerio's avatar
Claudio Valerio committed
207 208 209 210
};

class UBGraphicsW3CWidgetItem : public UBGraphicsWidgetItem
{
211
    Q_OBJECT
Claudio Valerio's avatar
Claudio Valerio committed
212 213

    public:
Guillaume Burel's avatar
Guillaume Burel committed
214 215 216
        class PreferenceValue
        {
            public:
Claudio Valerio's avatar
Claudio Valerio committed
217

Guillaume Burel's avatar
Guillaume Burel committed
218 219
                PreferenceValue()
                {
220
                    /* NOOP */
Guillaume Burel's avatar
Guillaume Burel committed
221
                }
222

Claudio Valerio's avatar
Claudio Valerio committed
223

Guillaume Burel's avatar
Guillaume Burel committed
224 225 226 227 228
                PreferenceValue(const QString& pValue, bool pReadonly)
                {
                    value = pValue;
                    readonly = pReadonly;
                }
Claudio Valerio's avatar
Claudio Valerio committed
229

Guillaume Burel's avatar
Guillaume Burel committed
230
                bool readonly;
231
                QString value;
Guillaume Burel's avatar
Guillaume Burel committed
232
         };
Claudio Valerio's avatar
Claudio Valerio committed
233

234 235 236 237 238 239 240 241 242 243 244 245
        class Metadata
        {
            public:
                QString id;
                QString name;
                QString description;
                QString author;
                QString authorEmail;
                QString authorHref;
                QString version;
        };

246
        UBGraphicsW3CWidgetItem(const QUrl& pWidgetUrl, QGraphicsItem *parent = 0);
247
        ~UBGraphicsW3CWidgetItem();
248 249 250

        virtual void setUuid(const QUuid &pUuid);
        virtual UBItem* deepCopy() const;
251
        virtual void copyItemParameters(UBItem *copy) const;
252
        QMap<QString, PreferenceValue> preferences();
253
        Metadata metadatas() const;
254

255
        static QString freezedWidgetFilePath();
256 257 258 259 260
        static QString createNPAPIWrapper(const QString& url, const QString& pMimeType = QString(), const QSize& sizeHint = QSize(300, 150), const QString& pName = QString());
        static QString createNPAPIWrapperInDir(const QString& url, const QDir& pDir, const QString& pMimeType = QString(), const QSize& sizeHint = QSize(300, 150), const QString& pName = QString());
        static QString createHtmlWrapperInDir(const QString& html, const QDir& pDir, const QSize& sizeHint,  const QString& pName);
        static QString freezedWidgetPage();
        static bool hasNPAPIWrapper(const QString& pMimeType);
Claudio Valerio's avatar
Claudio Valerio committed
261

262
        Metadata mMetadatas;
Claudio Valerio's avatar
Claudio Valerio committed
263 264 265 266 267

    private slots:
        virtual void javaScriptWindowObjectCleared();

    private:
268 269
        static void loadNPAPIWrappersTemplates();
        static QString textForSubElementByLocale(QDomElement rootElement, QString subTagName, QLocale locale);
Claudio Valerio's avatar
Claudio Valerio committed
270 271

        UBW3CWidgetAPI* mW3CWidgetAPI;
272
        QMap<QString, PreferenceValue> mPreferences;
Claudio Valerio's avatar
Claudio Valerio committed
273

274 275
        static bool sTemplateLoaded;
        static QString sNPAPIWrappperConfigTemplate;
276
        static QMap<QString, QString> sNPAPIWrapperTemplates;
Claudio Valerio's avatar
Claudio Valerio committed
277 278 279
};

#endif // UBGRAPHICSWIDGETITEM_H