Methods and exports needed for Webview support (#217)

* Adds basic qurl support

* allows building with custom qt installation using the env variable

* cleans up

* cleanup
This commit is contained in:
Atul R 2019-11-24 23:05:53 +01:00 committed by GitHub
parent 15c2f1aca6
commit 22c7acd74e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 185 additions and 18 deletions

View File

@ -47,6 +47,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtCore/QObject/qobject_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtCore/QVariant/qvariant_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtCore/QSize/qsize_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtCore/QUrl/qurl_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QWidget/qwidget_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QGridLayout/qgridlayout_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QDial/qdial_wrap.cpp"

View File

@ -12,10 +12,17 @@ function(AddQtSupport addonName)
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE QT_HOME_DIR
)
if(DEFINED ENV{QT_INSTALL_DIR})
# Allows to use custom Qt installation via QT_INSTALL_DIR env variable
message(STATUS "Using Custom QT installation for ${addonName} QT_INSTALL_DIR:$ENV{QT_INSTALL_DIR}")
set(QT_HOME_DIR "$ENV{QT_INSTALL_DIR}")
endif()
string(REPLACE "\n" "" QT_HOME_DIR "${QT_HOME_DIR}")
string(REPLACE "\"" "" QT_HOME_DIR "${QT_HOME_DIR}")
if (APPLE)
# createQtMacSymlinks()
if(APPLE)
set(CUSTOM_QT_MOC_PATH "${QT_HOME_DIR}/bin/moc")
target_include_directories(${addonName} PRIVATE
@ -65,20 +72,8 @@ function(AddQtSupport addonName)
"${QT_HOME_DIR}/lib/libQt5Widgets.so"
)
endif()
# set custom moc executable location
set_target_properties(Qt5::moc PROPERTIES IMPORTED_LOCATION "${CUSTOM_QT_MOC_PATH}")
endfunction(AddQtSupport addonName)
# function(createQtMacSymlinks)
# message("Creating qt symlinks")
# execute_process(
# COMMAND 'mkdir -p ${QT_HOME_DIR}/include'
# COMMAND 'ln -sfn ${QT_HOME_DIR}/lib/QtCore.framework/Versions/5/Headers ${QT_HOME_DIR}/include/QtCore'
# COMMAND 'ln -sfn ${QT_HOME_DIR}/lib/QtGui.framework/Versions/5/Headers ${QT_HOME_DIR}/include/QtGui'
# COMMAND 'ln -sfn ${QT_HOME_DIR}/lib/QtWidgets.framework/Versions/5/Headers ${QT_HOME_DIR}/include/QtWidgets'
# WORKING_DIRECTORY ${QT_HOME_DIR}
# )
# endfunction()

View File

@ -0,0 +1,29 @@
#pragma once
#include <napi.h>
#include <stdlib.h>
#include <QUrl>
#include "core/Component/component_macro.h"
class QUrlWrap : public Napi::ObjectWrap<QUrlWrap> {
private:
std::unique_ptr<QUrl> instance;
public:
static Napi::FunctionReference constructor;
static Napi::Object init(Napi::Env env, Napi::Object exports);
QUrlWrap(const Napi::CallbackInfo& info);
~QUrlWrap();
QUrl* getInternalInstance();
// Wrapped methods
Napi::Value setUrl(const Napi::CallbackInfo& info);
Napi::Value toString(const Napi::CallbackInfo& info);
COMPONENT_WRAPPED_METHODS_DECLARATION
};
namespace StaticQUrlWrapMethods {
Napi::Value fromQVariant(const Napi::CallbackInfo& info);
} // namespace StaticQUrlWrapMethods

View File

@ -0,0 +1,79 @@
#include "QtCore/QUrl/qurl_wrap.h"
#include "Extras/Utils/nutils.h"
#include "QtCore/QVariant/qvariant_wrap.h"
Napi::FunctionReference QUrlWrap::constructor;
Napi::Object QUrlWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "QUrl";
Napi::Function func = DefineClass(
env, CLASSNAME,
{InstanceMethod("setUrl", &QUrlWrap::setUrl),
InstanceMethod("toString", &QUrlWrap::toString),
StaticMethod("fromQVariant", &StaticQUrlWrapMethods::fromQVariant),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
QUrlWrap::QUrlWrap(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<QUrlWrap>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() == 2) {
std::string url = info[0].As<Napi::String>().Utf8Value();
int parseMode = info[1].As<Napi::Number>().Int32Value();
this->instance = std::make_unique<QUrl>(
QString::fromStdString(url), static_cast<QUrl::ParsingMode>(parseMode));
} else if (info.Length() == 1) {
if (info[0].IsExternal()) {
this->instance =
std::unique_ptr<QUrl>(info[0].As<Napi::External<QUrl>>().Data());
} else {
std::string url = info[0].As<Napi::String>().Utf8Value();
this->instance = std::make_unique<QUrl>(QString::fromStdString(url));
}
} else if (info.Length() == 0) {
this->instance = std::make_unique<QUrl>();
} else {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = extrautils::configureComponent(this->getInternalInstance());
}
QUrlWrap::~QUrlWrap() { this->instance.reset(); }
QUrl* QUrlWrap::getInternalInstance() { return this->instance.get(); }
Napi::Value QUrlWrap::setUrl(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
std::string url = info[0].As<Napi::String>().Utf8Value();
this->instance->setUrl(QString::fromStdString(url));
return env.Null();
}
Napi::Value QUrlWrap::toString(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
QString url = this->instance->toString();
return Napi::Value::From(env, url.toStdString());
}
Napi::Value StaticQUrlWrapMethods::fromQVariant(
const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Object variantObject = info[0].As<Napi::Object>();
QVariantWrap* variantWrap =
Napi::ObjectWrap<QVariantWrap>::Unwrap(variantObject);
QVariant* variant = variantWrap->getInternalInstance();
QUrl url = variant->value<QUrl>();
auto instance = QUrlWrap::constructor.New(
{Napi::External<QUrl>::New(env, new QUrl(url))});
return instance;
}

View File

@ -2,6 +2,7 @@
#include "QtCore/QObject/qobject_wrap.h"
#include "QtCore/QSize/qsize_wrap.h"
#include "QtCore/QUrl/qurl_wrap.h"
#include "QtCore/QVariant/qvariant_wrap.h"
#include "QtGui/QApplication/qapplication_wrap.h"
#include "QtGui/QClipboard/qclipboard_wrap.h"
@ -43,6 +44,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
QObjectWrap::init(env, exports);
QVariantWrap::init(env, exports);
QSizeWrap::init(env, exports);
QUrlWrap::init(env, exports);
QClipboardWrap::init(env, exports);
QWidgetWrap::init(env, exports);
QPixmapWrap::init(env, exports);

View File

@ -42,9 +42,11 @@ export { QShortcut, QShortcutEvents } from './lib/QtWidgets/QShortcut';
export { QObject, NodeObject } from './lib/QtCore/QObject';
export { QVariant } from './lib/QtCore/QVariant';
export { QSize } from './lib/QtCore/QSize';
export { QUrl, ParsingMode } from './lib/QtCore/QUrl';
// Layouts:
export { QGridLayout } from './lib/QtWidgets/QGridLayout';
export { FlexLayout } from './lib/core/FlexLayout';
// Others:
export { StyleSheet } from './lib/core/Style/StyleSheet';
export { NativeElement, Component } from './lib/core/Component';
export { checkIfNativeElement } from './lib/utils/helpers';

View File

@ -30,6 +30,6 @@ export class QSize extends Component {
return this.native.height();
}
static fromQVariant(variant: QVariant): QSize {
return addon.QSize.fromQVariant(variant.native);
return new QSize(addon.QSize.fromQVariant(variant.native));
}
}

35
src/lib/QtCore/QUrl.ts Normal file
View File

@ -0,0 +1,35 @@
import { NativeElement, Component } from '../core/Component';
import addon from '../utils/addon';
import { checkIfNativeElement } from '../utils/helpers';
import { QVariant } from './QVariant';
export enum ParsingMode {
TolerantMode,
StrictMode,
DecodedMode,
}
type argument = string | NativeElement;
export class QUrl extends Component {
native: NativeElement;
constructor(arg?: argument, parsingMode: ParsingMode = ParsingMode.TolerantMode) {
super();
if (!arg) {
this.native = new addon.QUrl();
} else if (checkIfNativeElement(arg)) {
this.native = arg as NativeElement;
} else {
this.native = new addon.QUrl(arg, parsingMode);
}
}
setUrl(url: string): void {
return this.native.setUrl(url);
}
toString(): string {
return this.native.toString();
}
static fromQVariant(variant: QVariant): QUrl {
return new QUrl(addon.QUrl.fromQVariant(variant.native));
}
}

View File

@ -0,0 +1,24 @@
import { QUrl } from '../QUrl';
import { QVariant } from '../QVariant';
describe('QUrl', () => {
it('initialize empty', () => {
const url = new QUrl();
expect(url).toBeTruthy();
});
it('initialize with url', () => {
const url = new QUrl('https://google.com');
expect(url).toBeTruthy();
});
it('setUrl', () => {
const url = new QUrl();
url.setUrl('https://yahoo.com');
expect(url.toString()).toEqual('https://yahoo.com');
});
it('initialize from QVariant', () => {
const url = new QUrl('https://google.com');
const variant = new QVariant(url);
expect(variant).toBeTruthy();
expect(QUrl.fromQVariant(variant).toString()).toBe('https://google.com');
});
});

View File

@ -50,6 +50,6 @@ export class QIcon extends Component {
return this.native.cacheKey();
}
static fromQVariant(variant: QVariant): QIcon {
return addon.QIcon.fromQVariant(variant.native);
return new QIcon(addon.QIcon.fromQVariant(variant.native));
}
}

View File

@ -43,6 +43,6 @@ export class QPixmap extends Component {
return this.native.width();
}
static fromQVariant(variant: QVariant): QPixmap {
return addon.QPixmap.fromQVariant(variant.native);
return new QPixmap(addon.QPixmap.fromQVariant(variant.native));
}
}