Skip to content
Snippets Groups Projects
Verified Commit cb9c9aa8 authored by Burak Hançerli's avatar Burak Hançerli :headphones:
Browse files

chore: add changes missing in rebase

parent 186f4d3a
No related branches found
No related tags found
1 merge request!1QDS-9900 Update the design viewer app to work with Qt 6.5 / emsdk 3.1.25
Pipeline #61331 canceled
......@@ -26,64 +26,23 @@
#include "dv_wasm.h"
#if defined(Q_OS_WASM)
#include <emscripten/val.h>
#include <emscripten.h>
#include <functional>
#include <QFile>
std::function<void(char *, size_t, char *)> g_setFileDataCallback;
extern "C" EMSCRIPTEN_KEEPALIVE void qt_callSetFileData(char *content,
size_t contentSize,
char *fileName)
{
if (g_setFileDataCallback == nullptr)
return;
g_setFileDataCallback(content, contentSize, fileName);
g_setFileDataCallback = nullptr;
}
#include <QTimer>
void DvWasm::fetchProject(QByteArray *data, QString *fileName)
{
// Call qt_callSetFileData to make sure the emscripten linker does not
// optimize it away, which may happen if the function is called from JavaScript
// only. Set g_setFileDataCallback to null to make it a no-op.
::g_setFileDataCallback = nullptr;
::qt_callSetFileData(nullptr, 0, nullptr);
auto setFileDataCallback =
[data, fileName](char *content, size_t contentSize, char *projectFilename) {
data->setRawData(content, contentSize);
*fileName = QString::fromUtf8(projectFilename);
};
g_setFileDataCallback = setFileDataCallback;
EM_ASM_({
// Copy the file file content to the C++ heap.
// Note: this could be simplified by passing the content as an
// "array" type to ccall and then let it copy to C++ memory.
// However, this built-in solution does not handle files larger
// than ~15M (Chrome). Instead, allocate memory manually and
// pass a pointer to the C++ side (which will free() it when done).
// Access global variables set by index.html
emscripten::val jsData = emscripten::val::global("contentArray");
emscripten::val jsFileName = emscripten::val::global("projectfileName");
// TODO: consider slice()ing the file to read it picewise and
// then assembling it in a QByteArray on the C++ side.
const contentSize = contentArray.byteLength;
const heapPointer = _malloc(contentSize);
const heapBytes = new Uint8Array(Module.HEAPU8.buffer, heapPointer, contentSize);
heapBytes.set(contentArray);
// Null out the first data copy to enable GC
reader = null;
contentArray = null;
// Call the C++ file data ready callback
ccall("qt_callSetFileData",
null,
["number", "number", "string"],
[heapPointer, contentSize, projectfileName]);
});
// Copy project data to the C++ heap and convert string
*data = QByteArray::fromEcmaUint8Array(jsData);
*fileName = QString::fromStdString(jsFileName.as<std::string>());
}
void DvWasm::printLog(const QString &message)
......@@ -96,22 +55,41 @@ void DvWasm::printWarn(const QString &message)
QString escaped = message;
escaped.replace("'", "\'");
escaped.replace("\n", "\\n");
emscripten_run_script("alert('" + escaped.toUtf8() + "');");
emscripten::val::global("window").call<void>("alert", escaped.toStdString());
}
void DvWasm::printError(const QString &message, const QString &fileName, int line)
{
printWarn(message);
emscripten_run_script("location.hash = ''; location.reload();");
emscripten::val location = emscripten::val::global("window")["location"];
location.set("hash", std::string());
location.call<void>("reload");
}
void DvWasm::showAppWindow()
{
printLog("Resizing the QML app window");
const QSize size;
const QString command
= QString::fromLatin1("setScreenSize(%1, %2);").arg(size.width()).arg(size.height());
emscripten_run_script(command.toUtf8());
emscripten::val qtContainer = emscripten::val::global("container"); // global from index.html
emscripten::val qtContainerStyle = qtContainer["style"];
if (size.isEmpty()) {
qtContainerStyle.set("width", std::string("100%"));
qtContainerStyle.set("height",
std::string(
"100%")); // ### FIXME: 100% height gives 0px height for some reason
} else {
qtContainerStyle.set("width", QString("%1px").arg(size.width()).toStdString());
qtContainerStyle.set("height", QString("%1px").arg(size.height()).toStdString());
}
// Make Qt pick up the new container size by calling the resizeCanvasElement()
// qtloader API. This needs to be done on delay after initial setup to make
// sure qtloader is initialized.
QTimer::singleShot(0, [qtContainer]() {
emscripten::val qtLoader = emscripten::val::global("qtLoader");
qtLoader.call<void>("resizeCanvasElement", qtContainer);
});
printLog("Showing the QML app window");
m_quickWindow->show();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment