Implemented QDrag Class (#626)

* Added QWheelEvent

* removed x y

* Added QNativeGestureEvent

* Changed wrong type of QNativeGestureEventWrap value

* Added QTabletEvent

* Fixing build error for QTabletEvent

* adding dropaction

* fix typos

* Added more functions to QPainterPath

* Added more functions to QPainterPath

* Fixed multiple typos

* Fixed multiple typos

* Got QPainterPath additions working.

* Modified QPainterPath to use qreal instead

* Added QPointF, added a few missing methods to QPoint

* Added QRectF

* implemented QRectF

* Added acceptDrops and setAcceptDrops to QWidget, will be useful for addon implementation of Drag and Drop

* Added more methods to QUrl

* Added QMimeData, additional methods to QUrl, and Dropping should now be supported

* refactored

* Fixed more merge conflicts

* Is this my final merge conflict??

* All merge conflicts resolved

* All merge conflicts resolved

* Adds guide for drag and drop

* lint fix

* added preliminary stuff, will need to tweak to make compilable..

* Compile passed, let's see if it works...

* QDrag added, working

* pause a bit, QDrag causes delayed error of 'QPaintDevice: Cannot destroy paint device that is being painted'

* Revert "Implemented QDrag class"

* Disabled problematic functions

* pulling from origin

Co-authored-by: Switt Kongdachalert <switt1995@yahoo.com>
Co-authored-by: Atul R <atulanand94@gmail.com>
This commit is contained in:
swittk 2020-07-05 00:58:40 +07:00 committed by GitHub
parent 107c4c4a76
commit 5c62cdcc8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 441 additions and 0 deletions

View File

@ -43,6 +43,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QColor/qcolor_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QApplication/qapplication_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QClipboard/qclipboard_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QDrag/qdrag_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QKeyEvent/qkeyevent_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QMouseEvent/qmouseevent_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QWheelEvent/qwheelevent_wrap.cpp"
@ -141,6 +142,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/core/FlexLayout/flexlayout.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtGui/QMovie/nmovie.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtGui/QApplication/napplication.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtGui/QDrag/ndrag.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QWidget/nwidget.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QDialog/ndialog.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QLabel/nlabel.hpp"

View File

@ -0,0 +1,25 @@
#pragma once
#include <QDrag>
#include "Extras/Export/export.h"
#include "QtCore/QObject/qobject_macro.h"
#include "core/Events/eventwidget.h"
#include "core/Events/eventwidget_macro.h"
class DLL_EXPORT NDrag : public QDrag, public EventWidget {
Q_OBJECT
EVENTWIDGET_IMPLEMENTATIONS(QDrag)
public:
using QDrag::QDrag;
void connectSignalsToEventEmitter() {
QOBJECT_SIGNALS
QObject::connect(this, &QDrag::actionChanged, [=](Qt::DropAction action) {
Napi::Env env = this->emitOnNode.Env();
Napi::HandleScope scope(env);
this->emitOnNode.Call(
{Napi::String::New(env, "actionChanged"),
Napi::Number::From(env, static_cast<int>(action))});
});
}
};

View File

@ -0,0 +1,53 @@
#pragma once
#include <napi.h>
#include <QDrag>
#include "Extras/Export/export.h"
#include "QtGui/QDrag/ndrag.hpp"
#include "core/Component/component_macro.h"
/*
- Note that setMimeData() assigns ownership of the QMimeData object to the QDrag
object.
- The QDrag must be constructed on the heap with a parent QObject to ensure that
Qt can clean up after the drag and drop operation has been completed
*/
class DLL_EXPORT QDragWrap : public Napi::ObjectWrap<QDragWrap> {
COMPONENT_WRAPPED_METHODS_DECLARATION
private:
// A guarded pointer, QPointer<T>, behaves like a normal C++ pointer T *,
// except that it is automatically cleared when the referenced object is
// destroyed (unlike normal C++ pointers, which become "dangling pointers" in
// such cases). T must be a subclass of QObject.
QPointer<NDrag> instance;
public:
static Napi::Object init(Napi::Env env, Napi::Object exports);
QDragWrap(const Napi::CallbackInfo& info);
~QDragWrap();
NDrag* getInternalInstance();
// class constructor
static Napi::FunctionReference constructor;
// wrapped methods
Napi::Value defaultAction(const Napi::CallbackInfo& info);
Napi::Value dragCursor(const Napi::CallbackInfo& info);
Napi::Value exec(const Napi::CallbackInfo& info);
Napi::Value hotSpot(const Napi::CallbackInfo& info);
Napi::Value pixmap(const Napi::CallbackInfo& info);
Napi::Value setDragCursor(const Napi::CallbackInfo& info);
Napi::Value setHotSpot(const Napi::CallbackInfo& info);
Napi::Value setPixmap(const Napi::CallbackInfo& info);
Napi::Value supportedActions(const Napi::CallbackInfo& info);
Napi::Value mimeData(const Napi::CallbackInfo& info);
Napi::Value setMimeData(const Napi::CallbackInfo& info);
Napi::Value source(const Napi::CallbackInfo& info);
Napi::Value target(const Napi::CallbackInfo& info);
};
namespace StaticQDragWrapMethods {
DLL_EXPORT Napi::Value cancel(const Napi::CallbackInfo& info);
}

View File

@ -0,0 +1,221 @@
#include "QtGui/QDrag/qdrag_wrap.h"
#include "Extras/Utils/nutils.h"
#include "QtCore/QMimeData/qmimedata_wrap.h"
#include "QtCore/QObject/qobject_wrap.h"
#include "QtCore/QPoint/qpoint_wrap.h"
#include "QtGui/QPixmap/qpixmap_wrap.h"
Napi::FunctionReference QDragWrap::constructor;
Napi::Object QDragWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "QDrag";
Napi::Function func = DefineClass(
env, CLASSNAME,
{InstanceMethod("defaultAction", &QDragWrap::defaultAction),
InstanceMethod("dragCursor", &QDragWrap::dragCursor),
InstanceMethod("exec", &QDragWrap::exec),
InstanceMethod("hotSpot", &QDragWrap::hotSpot),
InstanceMethod("pixmap", &QDragWrap::pixmap),
InstanceMethod("setDragCursor", &QDragWrap::setDragCursor),
InstanceMethod("setHotSpot", &QDragWrap::setHotSpot),
InstanceMethod("setPixmap", &QDragWrap::setPixmap),
InstanceMethod("supportedActions", &QDragWrap::supportedActions),
InstanceMethod("mimeData", &QDragWrap::mimeData),
InstanceMethod("setMimeData", &QDragWrap::setMimeData),
InstanceMethod("source", &QDragWrap::source),
InstanceMethod("target", &QDragWrap::target),
StaticMethod("cancel", &StaticQDragWrapMethods::cancel),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QDragWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
NDrag* QDragWrap::getInternalInstance() { return this->instance; }
QDragWrap::QDragWrap(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<QDragWrap>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() == 1) {
Napi::Object wrap0_0 = info[0].As<Napi::Object>();
QObjectWrap* wrap0_1 = Napi::ObjectWrap<QObjectWrap>::Unwrap(wrap0_0);
QObject* dragSource = wrap0_1->getInternalInstance();
this->instance = new NDrag(dragSource);
} else {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = extrautils::configureComponent(this->getInternalInstance());
}
QDragWrap::~QDragWrap() {
// Do not destroy instance here. It will be done by Qt Event loop.
extrautils::safeDelete(this->instance);
}
// Instance Methods Here
Napi::Value QDragWrap::defaultAction(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
return Napi::Number::New(env, this->instance->defaultAction());
}
Napi::Value QDragWrap::dragCursor(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Qt::DropAction input0 =
(Qt::DropAction)info[0].As<Napi::Number>().Int32Value();
QPixmap ret = this->instance->dragCursor(input0);
auto instance = QPixmapWrap::constructor.New(
{Napi::External<QPixmap>::New(env, new QPixmap(ret))});
return instance;
}
Napi::Value QDragWrap::exec(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() < 2) {
Qt::DropActions input0 =
(Qt::DropActions)info[0].As<Napi::Number>().Int32Value();
return Napi::Number::New(env, this->instance->exec(input0));
}
Qt::DropActions input0 =
(Qt::DropActions)info[0].As<Napi::Number>().Int32Value();
Qt::DropAction input1 =
(Qt::DropAction)info[1].As<Napi::Number>().Int32Value();
return Napi::Number::New(env, this->instance->exec(input0, input1));
}
Napi::Value QDragWrap::hotSpot(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
QPoint ret = this->instance->hotSpot();
auto instance = QPointWrap::constructor.New(
{Napi::External<QPoint>::New(env, new QPoint(ret))});
return instance;
}
Napi::Value QDragWrap::pixmap(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
QPixmap ret = this->instance->pixmap();
auto instance = QPixmapWrap::constructor.New(
{Napi::External<QPixmap>::New(env, new QPixmap(ret))});
return instance;
}
Napi::Value QDragWrap::setDragCursor(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Object wrap0_0 = info[0].As<Napi::Object>();
QPixmapWrap* wrap0_1 = Napi::ObjectWrap<QPixmapWrap>::Unwrap(wrap0_0);
QPixmap* input0 = wrap0_1->getInternalInstance();
Qt::DropAction input1 =
(Qt::DropAction)info[1].As<Napi::Number>().Int32Value();
this->instance->setDragCursor(*input0, input1);
return env.Null();
}
Napi::Value QDragWrap::setHotSpot(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Object wrap0_0 = info[0].As<Napi::Object>();
QPointWrap* wrap0_1 = Napi::ObjectWrap<QPointWrap>::Unwrap(wrap0_0);
QPoint* input0 = wrap0_1->getInternalInstance();
this->instance->setHotSpot(*input0);
return env.Null();
}
Napi::Value QDragWrap::setPixmap(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Object wrap0_0 = info[0].As<Napi::Object>();
QPixmapWrap* wrap0_1 = Napi::ObjectWrap<QPixmapWrap>::Unwrap(wrap0_0);
QPixmap* input0 = wrap0_1->getInternalInstance();
this->instance->setPixmap(*input0);
return env.Null();
}
Napi::Value QDragWrap::supportedActions(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
return Napi::Number::New(env, this->instance->supportedActions());
}
// Static Methods here
Napi::Value StaticQDragWrapMethods::cancel(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
QDrag::cancel();
return env.Null();
}
Napi::Value QDragWrap::mimeData(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
const QMimeData* ret = this->instance->mimeData();
QMimeData* clone = new QMimeData();
// QMimeData has no copy constructor so I do this
QMimeDataWrap::cloneFromMimeDataToData((QMimeData*)ret, clone);
auto instance = QMimeDataWrap::constructor.New(
{Napi::External<QMimeData>::New(env, clone)});
return instance;
}
Napi::Value QDragWrap::setMimeData(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Object wrap0_0 = info[0].As<Napi::Object>();
QMimeDataWrap* wrap0_1 = Napi::ObjectWrap<QMimeDataWrap>::Unwrap(wrap0_0);
QMimeData* input0 = wrap0_1->getInternalInstance();
QMimeData* clone = new QMimeData();
// QMimeData has no copy constructor so I do this
QMimeDataWrap::cloneFromMimeDataToData((QMimeData*)input0, clone);
// Ownership of the data (the clone) is transferred to the QDrag object
// While our original QMimeData should be garbage collected.. I guessss?
this->instance->setMimeData(clone);
return env.Null();
}
// This function crashes the application
Napi::Value QDragWrap::source(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
QObject* source = this->instance->source();
// We can likely call a wrap directly since QObjectWrap is smart and uses
// QPointer?
auto instance =
QObjectWrap::constructor.New({Napi::External<QObject>::New(env, source)});
return instance;
}
// This function crashes the application
Napi::Value QDragWrap::target(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
QObject* target = this->instance->target();
// We can likely call a wrap directly since QObjectWrap is smart and uses
// QPointer?
auto instance =
QObjectWrap::constructor.New({Napi::External<QObject>::New(env, target)});
return instance;
}

View File

@ -20,6 +20,7 @@
#include "QtGui/QClipboard/qclipboard_wrap.h"
#include "QtGui/QColor/qcolor_wrap.h"
#include "QtGui/QCursor/qcursor_wrap.h"
#include "QtGui/QDrag/qdrag_wrap.h"
#include "QtGui/QEvent/QDragLeaveEvent/qdragleaveevent_wrap.h"
#include "QtGui/QEvent/QDragMoveEvent/qdragmoveevent_wrap.h"
#include "QtGui/QEvent/QDropEvent/qdropevent_wrap.h"
@ -180,6 +181,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
QMouseEventWrap::init(env, exports);
QWheelEventWrap::init(env, exports);
QTabletEventWrap::init(env, exports);
QDragWrap::init(env, exports);
QDropEventWrap::init(env, exports);
QDragMoveEventWrap::init(env, exports);
QDragLeaveEventWrap::init(env, exports);

View File

@ -23,6 +23,7 @@ export { QMouseEvent } from './lib/QtGui/QEvent/QMouseEvent';
export { QWheelEvent } from './lib/QtGui/QEvent/QWheelEvent';
export { QNativeGestureEvent } from './lib/QtGui/QEvent/QNativeGestureEvent';
export { QTabletEvent } from './lib/QtGui/QEvent/QTabletEvent';
export { QDrag } from './lib/QtGui/QDrag';
export { QDropEvent } from './lib/QtGui/QEvent/QDropEvent';
export { QDragMoveEvent } from './lib/QtGui/QEvent/QDragMoveEvent';
export { QDragLeaveEvent } from './lib/QtGui/QEvent/QDragLeaveEvent';

137
src/lib/QtGui/QDrag.ts Normal file
View File

@ -0,0 +1,137 @@
import { NativeElement, Component } from '../core/Component';
import addon from '../utils/addon';
import { checkIfNativeElement } from '../utils/helpers';
import { QObject } from '../QtCore/QObject';
import { DropAction } from '../QtEnums';
import { QPixmap } from './QPixmap';
import { QPoint } from '../QtCore/QPoint';
import { QMimeData } from '../QtCore/QMimeData';
/**
* description
*/
export class QDrag extends Component {
native: NativeElement;
constructor(arg?: NativeElement | QObject) {
super();
if (!arg) {
this.native = new addon.QDrag();
} else {
const isNative = checkIfNativeElement(arg);
if (isNative) {
this.native = arg as NativeElement;
} else if (arg.native) {
this.native = new addon.QDrag(arg.native);
} else {
this.native = new addon.QDrag();
}
}
}
/**
Returns the default proposed drop action for this drag operation.
*/
defaultAction(): DropAction {
return this.native.defaultAction();
}
/**
Returns the drag cursor for the action.
*/
dragCursor(action: DropAction): QPixmap {
return new QPixmap(this.native.dragCursor(action));
}
/**
Starts the drag and drop operation and returns a value indicating the requested drop action when it is completed. The drop actions that the user can choose from are specified in supportedActions. The default proposed action will be selected among the allowed actions in the following order: Move, Copy and Link.
*/
exec(supportedActions: number = DropAction.MoveAction, defaultDropAction?: DropAction): DropAction {
if (defaultDropAction) {
return this.native.exec(supportedActions, defaultDropAction);
} else {
return this.native.exec(supportedActions);
}
}
/**
Returns the position of the hot spot relative to the top-left corner of the cursor.
*/
hotSpot(): QPoint {
return new QPoint(this.native.hotSpot());
}
/**
Returns the pixmap used to represent the data in a drag and drop operation.
*/
pixmap(): QPixmap {
return new QPixmap(this.native.pixmap());
}
/**
Sets the drag cursor for the action. This allows you to override the default native cursors. To revert to using the native cursor for action pass in a null QPixmap as cursor.
*/
setDragCursor(cursor: QPixmap, action: DropAction): void {
return this.native.setDragCursor(cursor.native, action);
}
/**
Sets the position of the hot spot relative to the top-left corner of the pixmap used to the point specified by hotspot.
*/
setHotSpot(hotspot: QPoint): void {
return this.native.setHotSpot(hotspot.native);
}
/**
Sets pixmap as the pixmap used to represent the data in a drag and drop operation. You can only set a pixmap before the drag is started.
*/
setPixmap(pixmap: QPixmap): void {
return this.native.setPixmap(pixmap.native);
}
/**
Returns the set of possible drop actions for this drag operation.
*/
supportedActions(): number {
return this.native.supportedActions();
}
/**
Cancels a drag operation initiated by Qt.
*/
static cancel(): void {
return addon.QDrag.cancel();
}
//Manaully implemented methods
/**
* Returns the MIME data that is encapsulated by the drag object.
*/
mimeData(): QMimeData {
return new QMimeData(this.native.mimeData());
}
/**
* Sets the data to be sent to the given MIME data.
* Ownership of the data is transferred to the QDrag object.
*/
setMimeData(data: QMimeData): void {
return this.native.setMimeData(data.native);
}
/*
* Returns the source of the drag object. This is the widget where the drag and drop operation originated.
THIS FUNCTION CRASHES THE APPLICATION, NEEDS REIMPLEMENTATION
*/
// source(): QObject {
// return new QObject(this.native.source());
// }
/*
* Returns the target of the drag and drop operation. This is the widget where the drag object was dropped.
THIS FUNCTION CRASHES THE APPLICATION, NEEDS REIMPLEMENTATION
*/
// target(): QObject {
// return new QObject(this.native.target());
// }
}