Add Input Method classes and support

This commit is contained in:
Simon Edwards 2023-03-04 10:28:14 +01:00
parent 2561fd92f9
commit 47d98500cf
13 changed files with 301 additions and 6 deletions

View File

@ -62,6 +62,8 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QDragMoveEvent/qdragmoveevent_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QDragMoveEvent/qdragmoveevent_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QDragLeaveEvent/qdragleaveevent_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QDragLeaveEvent/qdragleaveevent_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QResizeEvent/qresizeevent_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QResizeEvent/qresizeevent_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QInputMethodEvent/qinputmethodevent_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QInputMethodQueryEvent/qinputmethodqueryevent_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QFontDatabase/qfontdatabase_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QFontDatabase/qfontdatabase_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QFontMetrics/qfontmetrics_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QFontMetrics/qfontmetrics_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QFontMetricsF/qfontmetricsf_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QFontMetricsF/qfontmetricsf_wrap.cpp"

View File

@ -0,0 +1,32 @@
#pragma once
#include <napi.h>
#include <QInputMethodEvent>
#include "Extras/Export/export.h"
#include "QtGui/QEvent/QEvent/qevent_macro.h"
#include "core/Component/component_macro.h"
class DLL_EXPORT QInputMethodEventWrap
: public Napi::ObjectWrap<QInputMethodEventWrap> {
COMPONENT_WRAPPED_METHODS_DECLARATION
QEVENT_WRAPPED_METHODS_DECLARATION
private:
QInputMethodEvent* instance;
public:
static Napi::Object init(Napi::Env env, Napi::Object exports);
QInputMethodEventWrap(const Napi::CallbackInfo& info);
~QInputMethodEventWrap();
QInputMethodEvent* getInternalInstance();
// class constructor
static Napi::FunctionReference constructor;
// wrapped methods
Napi::Value commitString(const Napi::CallbackInfo& info);
Napi::Value preeditString(const Napi::CallbackInfo& info);
Napi::Value replacementLength(const Napi::CallbackInfo& info);
Napi::Value replacementStart(const Napi::CallbackInfo& info);
Napi::Value setCommitString(const Napi::CallbackInfo& info);
};

View File

@ -0,0 +1,30 @@
#pragma once
#include <napi.h>
#include <QInputMethodQueryEvent>
#include "Extras/Export/export.h"
#include "QtGui/QEvent/QEvent/qevent_macro.h"
#include "core/Component/component_macro.h"
class DLL_EXPORT QInputMethodQueryEventWrap
: public Napi::ObjectWrap<QInputMethodQueryEventWrap> {
COMPONENT_WRAPPED_METHODS_DECLARATION
QEVENT_WRAPPED_METHODS_DECLARATION
private:
QInputMethodQueryEvent* instance;
public:
static Napi::Object init(Napi::Env env, Napi::Object exports);
QInputMethodQueryEventWrap(const Napi::CallbackInfo& info);
~QInputMethodQueryEventWrap();
QInputMethodQueryEvent* getInternalInstance();
// class constructor
static Napi::FunctionReference constructor;
// wrapped methods
Napi::Value queries(const Napi::CallbackInfo& info);
Napi::Value setValue(const Napi::CallbackInfo& info);
Napi::Value value(const Napi::CallbackInfo& info);
};

View File

@ -0,0 +1,90 @@
#include "QtGui/QEvent/QInputMethodEvent/qinputmethodevent_wrap.h"
#include <QString>
#include "Extras/Utils/nutils.h"
Napi::FunctionReference QInputMethodEventWrap::constructor;
Napi::Object QInputMethodEventWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "QInputMethodEvent";
Napi::Function func = DefineClass(
env, CLASSNAME,
{InstanceMethod("commitString", &QInputMethodEventWrap::commitString),
InstanceMethod("preeditString", &QInputMethodEventWrap::preeditString),
InstanceMethod("replacementLength",
&QInputMethodEventWrap::replacementLength),
InstanceMethod("replacementStart",
&QInputMethodEventWrap::replacementStart),
InstanceMethod("setCommitString",
&QInputMethodEventWrap::setCommitString),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QInputMethodEventWrap)
QEVENT_WRAPPED_METHODS_EXPORT_DEFINE(QInputMethodEventWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
QInputMethodEvent* QInputMethodEventWrap::getInternalInstance() {
return this->instance;
}
QInputMethodEventWrap::QInputMethodEventWrap(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<QInputMethodEventWrap>(info) {
Napi::Env env = info.Env();
if (info.Length() == 1) {
Napi::External<QInputMethodEvent> eventObject =
info[0].As<Napi::External<QInputMethodEvent>>();
this->instance = static_cast<QInputMethodEvent*>(eventObject.Data());
} else {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = extrautils::configureComponent(this->getInternalInstance());
}
QInputMethodEventWrap::~QInputMethodEventWrap() {
// Do not destroy instance here. It will be done by Qt Event loop.
}
Napi::Value QInputMethodEventWrap::setCommitString(
const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::String setCommitString = info[0].As<Napi::String>();
int replaceFrom = info[1].As<Napi::Number>().Int32Value();
int replaceLength = info[2].As<Napi::Number>().Int32Value();
this->instance->setCommitString(
QString::fromStdString(setCommitString.Utf8Value()), replaceFrom,
replaceLength);
return env.Null();
}
Napi::Value QInputMethodEventWrap::commitString(
const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
QString commitString = this->instance->commitString();
return Napi::Value::From(env, commitString.toStdString());
}
Napi::Value QInputMethodEventWrap::preeditString(
const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
QString preeditString = this->instance->preeditString();
return Napi::Value::From(env, preeditString.toStdString());
}
Napi::Value QInputMethodEventWrap::replacementLength(
const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
int x = this->instance->replacementLength();
return Napi::Number::From(env, x);
}
Napi::Value QInputMethodEventWrap::replacementStart(
const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
int x = this->instance->replacementStart();
return Napi::Number::From(env, x);
}

View File

@ -0,0 +1,79 @@
#include "QtGui/QEvent/QInputMethodQueryEvent/qinputmethodqueryevent_wrap.h"
#include <QString>
#include "Extras/Utils/nutils.h"
#include "QtCore/QVariant/qvariant_wrap.h"
Napi::FunctionReference QInputMethodQueryEventWrap::constructor;
Napi::Object QInputMethodQueryEventWrap::init(Napi::Env env,
Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "QInputMethodQueryEvent";
Napi::Function func = DefineClass(
env, CLASSNAME,
{InstanceMethod("queries", &QInputMethodQueryEventWrap::queries),
InstanceMethod("setValue", &QInputMethodQueryEventWrap::setValue),
InstanceMethod("value", &QInputMethodQueryEventWrap::value),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QInputMethodQueryEventWrap)
QEVENT_WRAPPED_METHODS_EXPORT_DEFINE(QInputMethodQueryEventWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
QInputMethodQueryEvent* QInputMethodQueryEventWrap::getInternalInstance() {
return this->instance;
}
QInputMethodQueryEventWrap::QInputMethodQueryEventWrap(
const Napi::CallbackInfo& info)
: Napi::ObjectWrap<QInputMethodQueryEventWrap>(info) {
Napi::Env env = info.Env();
if (info.Length() == 1) {
Napi::External<QInputMethodQueryEvent> eventObject =
info[0].As<Napi::External<QInputMethodQueryEvent>>();
this->instance = static_cast<QInputMethodQueryEvent*>(eventObject.Data());
} else {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = extrautils::configureComponent(this->getInternalInstance());
}
QInputMethodQueryEventWrap::~QInputMethodQueryEventWrap() {
// Do not destroy instance here. It will be done by Qt Event loop.
}
Napi::Value QInputMethodQueryEventWrap::queries(
const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
int queries = this->instance->queries();
return Napi::Number::From(env, queries);
}
Napi::Value QInputMethodQueryEventWrap::setValue(
const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Qt::InputMethodQuery query = static_cast<Qt::InputMethodQuery>(
info[0].As<Napi::Number>().Int32Value());
Napi::Value value = info[1];
QVariant* valueVariant = extrautils::convertToQVariant(env, value);
this->instance->setValue(query, *valueVariant);
delete valueVariant;
return env.Null();
}
Napi::Value QInputMethodQueryEventWrap::value(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Qt::InputMethodQuery query = static_cast<Qt::InputMethodQuery>(
info[0].As<Napi::Number>().Int32Value());
QVariant value = this->instance->value(query);
auto instance = QVariantWrap::constructor.New(
{Napi::External<QVariant>::New(env, new QVariant(value))});
return instance;
}

View File

@ -84,8 +84,8 @@ Napi::Object QTableWidgetWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("visualRow", &QTableWidgetWrap::visualRow), InstanceMethod("visualRow", &QTableWidgetWrap::visualRow),
InstanceMethod("clearSelection", &QTableWidgetWrap::clearSelection), InstanceMethod("clearSelection", &QTableWidgetWrap::clearSelection),
InstanceMethod("selectAll", &QTableWidgetWrap::selectAll), InstanceMethod("selectAll", &QTableWidgetWrap::selectAll),
InstanceMethod("scrollToBottom", &QTableWidgetWrap::scrollToBottom), InstanceMethod("scrollToBottom", &QTableWidgetWrap::scrollToBottom),
InstanceMethod("scrollToTop", &QTableWidgetWrap::scrollToTop), InstanceMethod("scrollToTop", &QTableWidgetWrap::scrollToTop),
QABSTRACTSCROLLAREA_WRAPPED_METHODS_EXPORT_DEFINE(QTableWidgetWrap)}); QABSTRACTSCROLLAREA_WRAPPED_METHODS_EXPORT_DEFINE(QTableWidgetWrap)});
constructor = Napi::Persistent(func); constructor = Napi::Persistent(func);

View File

@ -27,6 +27,8 @@
#include "QtGui/QEvent/QDragLeaveEvent/qdragleaveevent_wrap.h" #include "QtGui/QEvent/QDragLeaveEvent/qdragleaveevent_wrap.h"
#include "QtGui/QEvent/QDragMoveEvent/qdragmoveevent_wrap.h" #include "QtGui/QEvent/QDragMoveEvent/qdragmoveevent_wrap.h"
#include "QtGui/QEvent/QDropEvent/qdropevent_wrap.h" #include "QtGui/QEvent/QDropEvent/qdropevent_wrap.h"
#include "QtGui/QEvent/QInputMethodEvent/qinputmethodevent_wrap.h"
#include "QtGui/QEvent/QInputMethodQueryEvent/qinputmethodqueryevent_wrap.h"
#include "QtGui/QEvent/QKeyEvent/qkeyevent_wrap.h" #include "QtGui/QEvent/QKeyEvent/qkeyevent_wrap.h"
#include "QtGui/QEvent/QMouseEvent/qmouseevent_wrap.h" #include "QtGui/QEvent/QMouseEvent/qmouseevent_wrap.h"
#include "QtGui/QEvent/QMoveEvent/qmoveevent_wrap.h" #include "QtGui/QEvent/QMoveEvent/qmoveevent_wrap.h"
@ -246,6 +248,8 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
QResizeEventWrap::init(env, exports); QResizeEventWrap::init(env, exports);
QTimerEventWrap::init(env, exports); QTimerEventWrap::init(env, exports);
QAbstractItemDelegateWrap::init(env, exports); QAbstractItemDelegateWrap::init(env, exports);
QInputMethodEventWrap::init(env, exports);
QInputMethodQueryEventWrap::init(env, exports);
// Test // Test
CacheTestQObjectWrap::init(env, exports); CacheTestQObjectWrap::init(env, exports);

View File

@ -22,6 +22,8 @@ export { QFontDatabase, SystemFont, WritingSystem } from './lib/QtGui/QFontDatab
export { QFontMetrics } from './lib/QtGui/QFontMetrics'; export { QFontMetrics } from './lib/QtGui/QFontMetrics';
export { QFontMetricsF } from './lib/QtGui/QFontMetricsF'; export { QFontMetricsF } from './lib/QtGui/QFontMetricsF';
// Events: Maybe a separate module ? // Events: Maybe a separate module ?
export { QInputMethodEvent } from './lib/QtGui/QEvent/QInputMethodEvent';
export { QInputMethodQueryEvent } from './lib/QtGui/QEvent/QInputMethodQueryEvent';
export { QKeyEvent } from './lib/QtGui/QEvent/QKeyEvent'; export { QKeyEvent } from './lib/QtGui/QEvent/QKeyEvent';
export { QMouseEvent } from './lib/QtGui/QEvent/QMouseEvent'; export { QMouseEvent } from './lib/QtGui/QEvent/QMouseEvent';
export { QMoveEvent } from './lib/QtGui/QEvent/QMoveEvent'; export { QMoveEvent } from './lib/QtGui/QEvent/QMoveEvent';

View File

@ -1,8 +1,9 @@
import { NativeElement, Component } from '../core/Component'; import { NativeElement, Component } from '../core/Component';
import addon from '../utils/addon'; import addon from '../utils/addon';
import { checkIfNativeElement } from '../utils/helpers'; import { checkIfNativeElement } from '../utils/helpers';
import { QRect } from './QRect';
export type QVariantType = NativeElement | string | string[] | number | boolean; export type QVariantType = NativeElement | string | string[] | number | boolean | QRect;
export class QVariant extends Component { export class QVariant extends Component {
constructor(); constructor();

View File

@ -1,6 +1,5 @@
export enum InputMethodQuery { export enum InputMethodQuery {
ImEnabled = 0x1, ImEnabled = 0x1,
ImMicroFocus = 0x2,
ImCursorRectangle = 0x2, ImCursorRectangle = 0x2,
ImFont = 0x4, ImFont = 0x4,
ImCursorPosition = 0x8, ImCursorPosition = 0x8,
@ -17,6 +16,7 @@ export enum InputMethodQuery {
ImEnterKeyType = 0x2000, ImEnterKeyType = 0x2000,
ImAnchorRectangle = 0x4000, ImAnchorRectangle = 0x4000,
ImInputItemClipRectangle = 0x8000, ImInputItemClipRectangle = 0x8000,
ImReadOnly = 0x10000,
//Masks: //Masks:
ImQueryAll = 0xffffffff, ImQueryAll = 0xffffffff,
} }

View File

@ -0,0 +1,24 @@
import addon from '../../utils/addon';
import { NativeRawPointer } from '../../core/Component';
import { QEvent } from './QEvent';
export class QInputMethodEvent extends QEvent {
constructor(event: NativeRawPointer<'QEvent'>) {
super(new addon.QInputMethodEvent(event));
}
commitString(): string {
return this.native.commitString();
}
preeditString(): string {
return this.native.preeditString();
}
replacementLength(): number {
return this.native.replacementLength();
}
replacementStart(): number {
return this.native.replacementStart();
}
setCommitString(commitString: string, replaceFrom = 0, replaceLength = 0): void {
this.native.setCommitString(commitString, replaceFrom, replaceLength);
}
}

View File

@ -0,0 +1,27 @@
import addon from '../../utils/addon';
import { NativeRawPointer } from '../../core/Component';
import { QVariant, QVariantType } from '../../QtCore/QVariant';
import { QEvent } from './QEvent';
import { QRect } from '../../QtCore/QRect';
export class QInputMethodQueryEvent extends QEvent {
constructor(event: NativeRawPointer<'QEvent'>) {
super(new addon.QInputMethodQueryEvent(event));
}
queries(): number /* InputMethodQueries */ {
return this.native.queries();
}
setValue(query: number /* InputMethodQuery */, value: QVariantType): void {
if (value instanceof QRect) {
this.native.setValue(query, value.native);
} else {
this.native.setValue(query, value);
}
}
value(query: number /* InputMethodQuery */): QVariant {
return new QVariant(this.native.value(query));
}
}

View File

@ -193,7 +193,9 @@ export class QWidget<Signals extends QWidgetSignals = QWidgetSignals> extends Yo
heightForWidth(w: number): number { heightForWidth(w: number): number {
return this.native.heightForWidth(w); return this.native.heightForWidth(w);
} }
// TODO: Qt::InputMethodHints inputMethodHints() const inputMethodHints(): number {
return this.property('inputMethodHints').toInt();
}
// TODO: virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const // TODO: virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const
// TODO: void insertAction(QAction *before, QAction *action) // TODO: void insertAction(QAction *before, QAction *action)
// TODO: void insertActions(QAction *before, QList<QAction *> actions) // TODO: void insertActions(QAction *before, QList<QAction *> actions)
@ -396,7 +398,9 @@ export class QWidget<Signals extends QWidgetSignals = QWidgetSignals> extends Yo
setGraphicsEffect(effect: QGraphicsEffect<any>): void { setGraphicsEffect(effect: QGraphicsEffect<any>): void {
this.native.setGraphicsEffect(effect.native); this.native.setGraphicsEffect(effect.native);
} }
// TODO: void setInputMethodHints(Qt::InputMethodHints hints) setInputMethodHints(hints: number): void {
this.setProperty('inputMethodHints', hints);
}
setInlineStyle(style: string, postprocess = true): void { setInlineStyle(style: string, postprocess = true): void {
if (postprocess) { if (postprocess) {
this._rawInlineStyle = style; this._rawInlineStyle = style;