summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/wasm/qwasmdom.cpp
blob: 6b2b3d09336abb83d5155f69629b1d9d8ec37058 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "qwasmdom.h"

#include <QtCore/qdir.h>
#include <QtCore/qfile.h>
#include <QtCore/qpoint.h>
#include <QtCore/qrect.h>
#include <QtGui/qimage.h>
#include <private/qstdweb_p.h>
#include <QtCore/qurl.h>

#include <utility>
#include <emscripten/wire.h>

QT_BEGIN_NAMESPACE

namespace dom {
namespace {
std::string dropActionToDropEffect(Qt::DropAction action)
{
    switch (action) {
    case Qt::DropAction::CopyAction:
        return "copy";
    case Qt::DropAction::IgnoreAction:
        return "none";
    case Qt::DropAction::LinkAction:
        return "link";
    case Qt::DropAction::MoveAction:
    case Qt::DropAction::TargetMoveAction:
        return "move";
    case Qt::DropAction::ActionMask:
        Q_ASSERT(false);
        return "";
    }
}
} // namespace

DataTransfer::DataTransfer(emscripten::val webDataTransfer)
    : webDataTransfer(webDataTransfer) {
}

DataTransfer::~DataTransfer() = default;

DataTransfer::DataTransfer(const DataTransfer &other) = default;

DataTransfer::DataTransfer(DataTransfer &&other) = default;

DataTransfer &DataTransfer::operator=(const DataTransfer &other) = default;

DataTransfer &DataTransfer::operator=(DataTransfer &&other) = default;

void DataTransfer::setDragImage(emscripten::val element, const QPoint &hotspot)
{
    webDataTransfer.call<void>("setDragImage", element, emscripten::val(hotspot.x()),
                               emscripten::val(hotspot.y()));
}

void DataTransfer::setData(std::string format, std::string data)
{
    webDataTransfer.call<void>("setData", emscripten::val(std::move(format)),
                               emscripten::val(std::move(data)));
}

void DataTransfer::setDropAction(Qt::DropAction action)
{
    webDataTransfer.set("dropEffect", emscripten::val(dropActionToDropEffect(action)));
}

void DataTransfer::setDataFromMimeData(const QMimeData &mimeData)
{
    for (const auto &format : mimeData.formats()) {
        auto data = mimeData.data(format);

        auto encoded = format.startsWith("text/")
                ? QString::fromLocal8Bit(data).toStdString()
                : "QB64" + QString::fromLocal8Bit(data.toBase64()).toStdString();

        setData(format.toStdString(), std::move(encoded));
    }
}

// Converts a DataTransfer instance to a QMimeData instance. Invokes the
// given callback when the conversion is complete. The callback takes ownership
// of the QMimeData.
void DataTransfer::toMimeDataWithFile(std::function<void(QMimeData *)> callback)
{
    enum class ItemKind {
        File,
        String,
    };

    class MimeContext {

    public:
        MimeContext(int itemCount, std::function<void(QMimeData *)> callback)
        :m_remainingItemCount(itemCount), m_callback(callback)
        {

        }

        void deref() {
            if (--m_remainingItemCount > 0)
                return;

            mimeData->setUrls(fileUrls);

            m_callback(mimeData);

            // Delete files; we expect that the user callback reads/copies
            // file content before returning.
            // Fixme: tie file lifetime to lifetime of the QMimeData?
            for (QUrl fileUrl: fileUrls)
                QFile(fileUrl.toLocalFile()).remove();

            delete this;
        }

        QMimeData *mimeData = new QMimeData();
        QList<QUrl> fileUrls;

    private:
        int m_remainingItemCount;
        std::function<void(QMimeData *)> m_callback;
    };

    const auto items = webDataTransfer["items"];
    const int itemCount = items["length"].as<int>();
    const int fileCount = webDataTransfer["files"]["length"].as<int>();
    MimeContext *mimeContext = new MimeContext(itemCount, callback);

    for (int i = 0; i < itemCount; ++i) {
        const auto item = items[i];
        const auto itemKind =
                item["kind"].as<std::string>() == "string" ? ItemKind::String : ItemKind::File;
        const auto itemMimeType = QString::fromStdString(item["type"].as<std::string>());

        switch (itemKind) {
        case ItemKind::File: {
            qstdweb::File webfile(item.call<emscripten::val>("getAsFile"));

            if (webfile.size() > 1e+9) { // limit file size to 1 GB
                qWarning() << "File is too large (> 1GB) and will be skipped. File size is" << webfile.size();
                mimeContext->deref();
                continue;
            }

            QString mimeFormat = QString::fromStdString(webfile.type());
            QString fileName = QString::fromStdString(webfile.name());

            // there's a file, now read it
            QByteArray fileContent(webfile.size(), Qt::Uninitialized);
            webfile.stream(fileContent.data(), [=]() {
                
                // If we get a single file, and that file is an image, then
                // try to decode the image data. This handles the case where
                // image data (i.e. not an image file) is pasted. The browsers
                // will then create a fake "image.png" file which has the image
                // data. As a side effect Qt will also decode the image for 
                // single-image-file drops, since there is no way to differentiate
                // the fake "image.png" from a real one.
                if (fileCount == 1 && mimeFormat.contains("image/")) {
                    QImage image;
                    if (image.loadFromData(fileContent))
                        mimeContext->mimeData->setImageData(image);
                }

                QDir qtTmpDir("/qt/tmp/"); // "tmp": indicate that these files won't stay around
                qtTmpDir.mkpath(qtTmpDir.path());

                QUrl fileUrl = QUrl::fromLocalFile(qtTmpDir.filePath(QString::fromStdString(webfile.name())));
                mimeContext->fileUrls.append(fileUrl);

                QFile file(fileUrl.toLocalFile());
                if (!file.open(QFile::WriteOnly)) {
                    qWarning() << "File was not opened";
                    mimeContext->deref();
                    return;
                }
                if (file.write(fileContent) < 0) {
                    qWarning() << "Write failed";
                    file.close();
                }
                mimeContext->deref();
            });
            break;
        }
        case ItemKind::String:
            if (itemMimeType.contains("STRING", Qt::CaseSensitive)
                || itemMimeType.contains("TEXT", Qt::CaseSensitive)) {
                mimeContext->deref();
                break;
            }
            QString a;
            QString data = QString::fromEcmaString(webDataTransfer.call<emscripten::val>(
                    "getData", emscripten::val(itemMimeType.toStdString())));

            if (!data.isEmpty()) {
                if (itemMimeType == "text/html")
                    mimeContext->mimeData->setHtml(data);
                else if (itemMimeType.isEmpty() || itemMimeType == "text/plain")
                    mimeContext->mimeData->setText(data); // the type can be empty
                else {
                    // TODO improve encoding
                    if (data.startsWith("QB64")) {
                        data.remove(0, 4);
                        mimeContext->mimeData->setData(itemMimeType,
                                                  QByteArray::fromBase64(QByteArray::fromStdString(
                                                          data.toStdString())));
                    } else {
                        mimeContext->mimeData->setData(itemMimeType, data.toLocal8Bit());
                    }
                }
            }
            mimeContext->deref();
            break;
        }
    } // for items
}

QMimeData *DataTransfer::toMimeDataPreview()
{
    auto data = new QMimeData();

    QList<QUrl> uriList;
    for (int i = 0; i < webDataTransfer["items"]["length"].as<int>(); ++i) {
        const auto item = webDataTransfer["items"][i];
        if (item["kind"].as<std::string>() == "file") {
            uriList.append(QUrl("blob://placeholder"));
        } else {
            const auto itemMimeType = QString::fromStdString(item["type"].as<std::string>());
            data->setData(itemMimeType, QByteArray());
        }
    }
    data->setUrls(uriList);
    return data;
}

void syncCSSClassWith(emscripten::val element, std::string cssClassName, bool flag)
{
    if (flag) {
        element["classList"].call<void>("add", emscripten::val(std::move(cssClassName)));
        return;
    }

    element["classList"].call<void>("remove", emscripten::val(std::move(cssClassName)));
}

QPointF mapPoint(emscripten::val source, emscripten::val target, const QPointF &point)
{
    const auto sourceBoundingRect =
            QRectF::fromDOMRect(source.call<emscripten::val>("getBoundingClientRect"));
    const auto targetBoundingRect =
            QRectF::fromDOMRect(target.call<emscripten::val>("getBoundingClientRect"));

    const auto offset = sourceBoundingRect.topLeft() - targetBoundingRect.topLeft();
    return point + offset;
}

void drawImageToWebImageDataArray(const QImage &sourceImage, emscripten::val destinationImageData,
                                  const QRect &sourceRect)
{
    Q_ASSERT_X(destinationImageData["constructor"]["name"].as<std::string>() == "ImageData",
               Q_FUNC_INFO, "The destination should be an ImageData instance");

    constexpr int BytesPerColor = 4;
    if (sourceRect.width() == sourceImage.width()) {
        // Copy a contiguous chunk of memory
        // ...............
        // OOOOOOOOOOOOOOO
        // OOOOOOOOOOOOOOO -> image data
        // OOOOOOOOOOOOOOO
        // ...............
        auto imageMemory = emscripten::typed_memory_view(sourceRect.width() * sourceRect.height()
                                                                 * BytesPerColor,
                                                         sourceImage.constScanLine(sourceRect.y()));
        destinationImageData["data"].call<void>(
                "set", imageMemory, sourceRect.y() * sourceImage.width() * BytesPerColor);
    } else {
        // Go through the scanlines manually to set the individual lines in bulk. This is
        // marginally less performant than the above.
        // ...............
        // ...OOOOOOOOO... r = 0  -> image data
        // ...OOOOOOOOO... r = 1  -> image data
        // ...OOOOOOOOO... r = 2  -> image data
        // ...............
        for (int row = 0; row < sourceRect.height(); ++row) {
            auto scanlineMemory =
                    emscripten::typed_memory_view(sourceRect.width() * BytesPerColor,
                                                  sourceImage.constScanLine(row + sourceRect.y())
                                                          + BytesPerColor * sourceRect.x());
            destinationImageData["data"].call<void>("set", scanlineMemory,
                                                    (sourceRect.y() + row) * sourceImage.width()
                                                                    * BytesPerColor
                                                            + sourceRect.x() * BytesPerColor);
        }
    }
}

} // namespace dom

QT_END_NAMESPACE