QPainter (#279)
* QPainter * QPainter * QPoint drawConvexPolygon for QPainter * QPoint drawConvexPolygon for QPainter * Added in QColor and expanded on the demo. * Added some functions to QPainter and expanded on the demo. * Expanded on the demo. * Update package.json * Update qcolor_wrap.h
This commit is contained in:
parent
6babd83c3b
commit
43d4ad7218
@ -51,6 +51,8 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
|
||||
"${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/QRect/qrect_wrap.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtCore/QPoint/qpoint_wrap.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtCore/QColor/qcolor_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/QBoxLayout/qboxlayout_wrap.cpp"
|
||||
@ -58,6 +60,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QFileDialog/qfiledialog_wrap.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QTableWidget/qtablewidget_wrap.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QTableWidgetItem/qtablewidgetitem_wrap.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QPainter/qpainter_wrap.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QGridLayout/qgridlayout_wrap.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QDial/qdial_wrap.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QLabel/qlabel_wrap.cpp"
|
||||
|
||||
26
src/cpp/include/nodegui/QtCore/QColor/qcolor_wrap.h
Normal file
26
src/cpp/include/nodegui/QtCore/QColor/qcolor_wrap.h
Normal file
@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <napi.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <QColor>
|
||||
|
||||
#include "core/Component/component_macro.h"
|
||||
|
||||
class QColorWrap : public Napi::ObjectWrap<QColorWrap> {
|
||||
private:
|
||||
std::unique_ptr<QColor> instance;
|
||||
|
||||
public:
|
||||
static Napi::FunctionReference constructor;
|
||||
static Napi::Object init(Napi::Env env, Napi::Object exports);
|
||||
QColorWrap(const Napi::CallbackInfo& info);
|
||||
~QColorWrap();
|
||||
QColor* getInternalInstance();
|
||||
// Wrapped methods
|
||||
|
||||
COMPONENT_WRAPPED_METHODS_DECLARATION
|
||||
};
|
||||
|
||||
namespace StaticQColorWrapMethods {
|
||||
} // namespace StaticQColorWrapMethods
|
||||
27
src/cpp/include/nodegui/QtCore/QPoint/qpoint_wrap.h
Normal file
27
src/cpp/include/nodegui/QtCore/QPoint/qpoint_wrap.h
Normal file
@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <napi.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <QPoint>
|
||||
|
||||
#include "core/Component/component_macro.h"
|
||||
|
||||
class QPointWrap : public Napi::ObjectWrap<QPointWrap> {
|
||||
private:
|
||||
std::unique_ptr<QPoint> instance;
|
||||
|
||||
public:
|
||||
static Napi::FunctionReference constructor;
|
||||
static Napi::Object init(Napi::Env env, Napi::Object exports);
|
||||
QPointWrap(const Napi::CallbackInfo& info);
|
||||
~QPointWrap();
|
||||
QPoint* getInternalInstance();
|
||||
// Wrapped methods
|
||||
|
||||
COMPONENT_WRAPPED_METHODS_DECLARATION
|
||||
};
|
||||
|
||||
namespace StaticQPointWrapMethods {
|
||||
Napi::Value fromQVariant(const Napi::CallbackInfo& info);
|
||||
} // namespace StaticQPointWrapMethods
|
||||
37
src/cpp/include/nodegui/QtWidgets/QPainter/qpainter_wrap.h
Normal file
37
src/cpp/include/nodegui/QtWidgets/QPainter/qpainter_wrap.h
Normal file
@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <napi.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <QPainter>
|
||||
|
||||
#include "Extras/Utils/nutils.h"
|
||||
#include "core/Component/component_wrap.h"
|
||||
|
||||
class QPainterWrap : public Napi::ObjectWrap<QPainterWrap> {
|
||||
private:
|
||||
QPainter* instance;
|
||||
|
||||
public:
|
||||
static Napi::Object init(Napi::Env env, Napi::Object exports);
|
||||
QPainterWrap(const Napi::CallbackInfo& info);
|
||||
~QPainterWrap();
|
||||
QPainter* getInternalInstance();
|
||||
// class constructor
|
||||
static Napi::FunctionReference constructor;
|
||||
// wrapped methods
|
||||
Napi::Value drawText(const Napi::CallbackInfo& info);
|
||||
Napi::Value begin(const Napi::CallbackInfo& info);
|
||||
Napi::Value end(const Napi::CallbackInfo& info);
|
||||
Napi::Value rotate(const Napi::CallbackInfo& info);
|
||||
Napi::Value setPen(const Napi::CallbackInfo& info);
|
||||
Napi::Value setRenderHint(const Napi::CallbackInfo& info);
|
||||
Napi::Value setBrush(const Napi::CallbackInfo& info);
|
||||
Napi::Value drawLine(const Napi::CallbackInfo& info);
|
||||
Napi::Value scale(const Napi::CallbackInfo& info);
|
||||
Napi::Value translate(const Napi::CallbackInfo& info);
|
||||
Napi::Value drawConvexPolygon(const Napi::CallbackInfo& info);
|
||||
Napi::Value save(const Napi::CallbackInfo& info);
|
||||
Napi::Value restore(const Napi::CallbackInfo& info);
|
||||
COMPONENT_WRAPPED_METHODS_DECLARATION
|
||||
};
|
||||
58
src/cpp/lib/QtCore/QColor/qcolor_wrap.cpp
Normal file
58
src/cpp/lib/QtCore/QColor/qcolor_wrap.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
#include "QtCore/QColor/qcolor_wrap.h"
|
||||
|
||||
#include "Extras/Utils/nutils.h"
|
||||
#include "QtCore/QVariant/qvariant_wrap.h"
|
||||
|
||||
Napi::FunctionReference QColorWrap::constructor;
|
||||
|
||||
Napi::Object QColorWrap::init(Napi::Env env, Napi::Object exports) {
|
||||
Napi::HandleScope scope(env);
|
||||
char CLASSNAME[] = "QColor";
|
||||
Napi::Function func =
|
||||
DefineClass(env, CLASSNAME, {COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE});
|
||||
constructor = Napi::Persistent(func);
|
||||
exports.Set(CLASSNAME, func);
|
||||
return exports;
|
||||
}
|
||||
|
||||
QColorWrap::QColorWrap(const Napi::CallbackInfo& info)
|
||||
: Napi::ObjectWrap<QColorWrap>(info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
if (info.Length() == 4) {
|
||||
int r = info[0].As<Napi::Number>().Int32Value();
|
||||
int g = info[1].As<Napi::Number>().Int32Value();
|
||||
int b = info[2].As<Napi::Number>().Int32Value();
|
||||
int a = info[3].As<Napi::Number>().Int32Value();
|
||||
this->instance = std::make_unique<QColor>(r, g, b, a);
|
||||
} else if (info.Length() == 3) {
|
||||
int r = info[0].As<Napi::Number>().Int32Value();
|
||||
int g = info[1].As<Napi::Number>().Int32Value();
|
||||
int b = info[2].As<Napi::Number>().Int32Value();
|
||||
this->instance = std::make_unique<QColor>(r, g, b);
|
||||
} else if (info.Length() == 1) {
|
||||
if (info[0].IsString()) {
|
||||
QString color =
|
||||
QString::fromUtf8(info[0].As<Napi::String>().Utf8Value().c_str());
|
||||
this->instance = std::make_unique<QColor>(color);
|
||||
} else if (info[0].IsNumber()) {
|
||||
Qt::GlobalColor color =
|
||||
(Qt::GlobalColor)info[0].As<Napi::Number>().Int32Value();
|
||||
this->instance = std::make_unique<QColor>(color);
|
||||
} else {
|
||||
this->instance =
|
||||
std::unique_ptr<QColor>(info[0].As<Napi::External<QColor>>().Data());
|
||||
}
|
||||
} else if (info.Length() == 0) {
|
||||
this->instance = std::make_unique<QColor>();
|
||||
} else {
|
||||
Napi::TypeError::New(env, "Wrong number of arguments")
|
||||
.ThrowAsJavaScriptException();
|
||||
}
|
||||
this->rawData = extrautils::configureComponent(this->getInternalInstance());
|
||||
}
|
||||
|
||||
QColorWrap::~QColorWrap() { this->instance.reset(); }
|
||||
|
||||
QColor* QColorWrap::getInternalInstance() { return this->instance.get(); }
|
||||
41
src/cpp/lib/QtCore/QPoint/qpoint_wrap.cpp
Normal file
41
src/cpp/lib/QtCore/QPoint/qpoint_wrap.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
#include "QtCore/QPoint/qpoint_wrap.h"
|
||||
|
||||
#include "Extras/Utils/nutils.h"
|
||||
#include "QtCore/QVariant/qvariant_wrap.h"
|
||||
|
||||
Napi::FunctionReference QPointWrap::constructor;
|
||||
|
||||
Napi::Object QPointWrap::init(Napi::Env env, Napi::Object exports) {
|
||||
Napi::HandleScope scope(env);
|
||||
char CLASSNAME[] = "QPoint";
|
||||
Napi::Function func =
|
||||
DefineClass(env, CLASSNAME, {COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE});
|
||||
constructor = Napi::Persistent(func);
|
||||
exports.Set(CLASSNAME, func);
|
||||
return exports;
|
||||
}
|
||||
|
||||
QPointWrap::QPointWrap(const Napi::CallbackInfo& info)
|
||||
: Napi::ObjectWrap<QPointWrap>(info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
if (info.Length() == 2) {
|
||||
int xpos = info[0].As<Napi::Number>().Int32Value();
|
||||
int ypos = info[1].As<Napi::Number>().Int32Value();
|
||||
this->instance = std::make_unique<QPoint>(xpos, ypos);
|
||||
} else if (info.Length() == 1) {
|
||||
this->instance =
|
||||
std::unique_ptr<QPoint>(info[0].As<Napi::External<QPoint>>().Data());
|
||||
} else if (info.Length() == 0) {
|
||||
this->instance = std::make_unique<QPoint>();
|
||||
} else {
|
||||
Napi::TypeError::New(env, "Wrong number of arguments")
|
||||
.ThrowAsJavaScriptException();
|
||||
}
|
||||
this->rawData = extrautils::configureComponent(this->getInternalInstance());
|
||||
}
|
||||
|
||||
QPointWrap::~QPointWrap() { this->instance.reset(); }
|
||||
|
||||
QPoint* QPointWrap::getInternalInstance() { return this->instance.get(); }
|
||||
177
src/cpp/lib/QtWidgets/QPainter/qpainter_wrap.cpp
Normal file
177
src/cpp/lib/QtWidgets/QPainter/qpainter_wrap.cpp
Normal file
@ -0,0 +1,177 @@
|
||||
#include "QtWidgets/QPainter/qpainter_wrap.h"
|
||||
|
||||
#include "Extras/Utils/nutils.h"
|
||||
#include "QtCore/QColor/qcolor_wrap.h"
|
||||
#include "QtCore/QPoint/qpoint_wrap.h"
|
||||
#include "QtWidgets/QWidget/qwidget_wrap.h"
|
||||
#include "core/Component/component_wrap.h"
|
||||
|
||||
Napi::FunctionReference QPainterWrap::constructor;
|
||||
|
||||
Napi::Object QPainterWrap::init(Napi::Env env, Napi::Object exports) {
|
||||
Napi::HandleScope scope(env);
|
||||
char CLASSNAME[] = "QPainter";
|
||||
Napi::Function func = DefineClass(
|
||||
env, CLASSNAME,
|
||||
{InstanceMethod("drawText", &QPainterWrap::drawText),
|
||||
InstanceMethod("begin", &QPainterWrap::begin),
|
||||
InstanceMethod("end", &QPainterWrap::end),
|
||||
InstanceMethod("rotate", &QPainterWrap::rotate),
|
||||
InstanceMethod("setPen", &QPainterWrap::setPen),
|
||||
InstanceMethod("setBrush", &QPainterWrap::setBrush),
|
||||
InstanceMethod("drawLine", &QPainterWrap::drawLine),
|
||||
InstanceMethod("scale", &QPainterWrap::scale),
|
||||
InstanceMethod("translate", &QPainterWrap::translate),
|
||||
InstanceMethod("setRenderHint", &QPainterWrap::setRenderHint),
|
||||
InstanceMethod("drawConvexPolygon", &QPainterWrap::drawConvexPolygon),
|
||||
InstanceMethod("save", &QPainterWrap::save),
|
||||
InstanceMethod("restore", &QPainterWrap::restore),
|
||||
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE});
|
||||
constructor = Napi::Persistent(func);
|
||||
exports.Set(CLASSNAME, func);
|
||||
return exports;
|
||||
}
|
||||
|
||||
QPainter* QPainterWrap::getInternalInstance() { return this->instance; }
|
||||
QPainterWrap::~QPainterWrap() { delete this->instance; }
|
||||
|
||||
QPainterWrap::QPainterWrap(const Napi::CallbackInfo& info)
|
||||
: Napi::ObjectWrap<QPainterWrap>(info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
if (info.Length() == 1) {
|
||||
Napi::Object deviceObject = info[0].As<Napi::Object>();
|
||||
QWidgetWrap* deviceWrap =
|
||||
Napi::ObjectWrap<QWidgetWrap>::Unwrap(deviceObject);
|
||||
this->instance = new QPainter(deviceWrap->getInternalInstance());
|
||||
} else if (info.Length() == 0) {
|
||||
this->instance = new QPainter();
|
||||
} else {
|
||||
Napi::TypeError::New(env, "Wrong number of arguments")
|
||||
.ThrowAsJavaScriptException();
|
||||
}
|
||||
this->rawData = extrautils::configureComponent(this->getInternalInstance());
|
||||
}
|
||||
Napi::Value QPainterWrap::drawText(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
int x = info[0].As<Napi::Number>().Int32Value();
|
||||
int y = info[1].As<Napi::Number>().Int32Value();
|
||||
Napi::String napiText = info[2].As<Napi::String>();
|
||||
std::string text = napiText.Utf8Value();
|
||||
this->instance->drawText(x, y, QString::fromUtf8(text.c_str()));
|
||||
return env.Null();
|
||||
}
|
||||
Napi::Value QPainterWrap::begin(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
Napi::Object deviceObject = info[0].As<Napi::Object>();
|
||||
QWidgetWrap* deviceWrap = Napi::ObjectWrap<QWidgetWrap>::Unwrap(deviceObject);
|
||||
QWidget* device = deviceWrap->getInternalInstance();
|
||||
bool ret = this->instance->begin(device);
|
||||
return Napi::Value::From(env, ret);
|
||||
}
|
||||
Napi::Value QPainterWrap::end(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
bool ret = this->instance->end();
|
||||
return Napi::Value::From(env, ret);
|
||||
}
|
||||
Napi::Value QPainterWrap::rotate(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
int angle = info[0].As<Napi::Number>().Int32Value();
|
||||
this->instance->rotate(angle);
|
||||
return env.Null();
|
||||
}
|
||||
Napi::Value QPainterWrap::setPen(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
Napi::String napiType = info[1].As<Napi::String>();
|
||||
std::string type = napiType.Utf8Value();
|
||||
|
||||
if (type == "color") {
|
||||
Napi::Object colorObject = info[0].As<Napi::Object>();
|
||||
QColorWrap* colorWrap = Napi::ObjectWrap<QColorWrap>::Unwrap(colorObject);
|
||||
QColor* color = colorWrap->getInternalInstance();
|
||||
this->instance->setPen(*color);
|
||||
} else if (type == "style") {
|
||||
Qt::PenStyle style = (Qt::PenStyle)info[0].As<Napi::Number>().Int32Value();
|
||||
this->instance->setPen(style);
|
||||
}
|
||||
return env.Null();
|
||||
}
|
||||
Napi::Value QPainterWrap::drawLine(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
int x1 = info[0].As<Napi::Number>().Int32Value();
|
||||
int y1 = info[1].As<Napi::Number>().Int32Value();
|
||||
int x2 = info[2].As<Napi::Number>().Int32Value();
|
||||
int y2 = info[3].As<Napi::Number>().Int32Value();
|
||||
this->instance->drawLine(x1, y1, x2, y2);
|
||||
return env.Null();
|
||||
}
|
||||
Napi::Value QPainterWrap::scale(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
int sx = info[0].As<Napi::Number>().Int32Value();
|
||||
int sy = info[1].As<Napi::Number>().Int32Value();
|
||||
this->instance->scale(sx, sy);
|
||||
return env.Null();
|
||||
}
|
||||
Napi::Value QPainterWrap::translate(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
int dx = info[0].As<Napi::Number>().Int32Value();
|
||||
int dy = info[1].As<Napi::Number>().Int32Value();
|
||||
this->instance->translate(dx, dy);
|
||||
return env.Null();
|
||||
}
|
||||
Napi::Value QPainterWrap::drawConvexPolygon(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
Napi::Array pointsNapi = info[0].As<Napi::Array>();
|
||||
QPolygon polygon;
|
||||
for (int i = 0; i < pointsNapi.Length(); i++) {
|
||||
Napi::Object pointObject = pointsNapi.Get(i).As<Napi::Object>();
|
||||
QPointWrap* pointWrap = Napi::ObjectWrap<QPointWrap>::Unwrap(pointObject);
|
||||
QPoint* point = pointWrap->getInternalInstance();
|
||||
polygon << *point;
|
||||
}
|
||||
this->instance->drawConvexPolygon(polygon);
|
||||
return env.Null();
|
||||
}
|
||||
Napi::Value QPainterWrap::save(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
this->instance->save();
|
||||
return env.Null();
|
||||
}
|
||||
Napi::Value QPainterWrap::restore(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
this->instance->restore();
|
||||
return env.Null();
|
||||
}
|
||||
Napi::Value QPainterWrap::setBrush(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
Napi::Object colorObject = info[0].As<Napi::Object>();
|
||||
QColorWrap* colorWrap = Napi::ObjectWrap<QColorWrap>::Unwrap(colorObject);
|
||||
QColor* color = colorWrap->getInternalInstance();
|
||||
QBrush* brush = new QBrush(*color);
|
||||
this->instance->setBrush(*brush);
|
||||
return env.Null();
|
||||
}
|
||||
Napi::Value QPainterWrap::setRenderHint(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
QPainter::RenderHint hint =
|
||||
(QPainter::RenderHint)info[0].As<Napi::Number>().Int32Value();
|
||||
|
||||
this->instance->setRenderHint(hint, true);
|
||||
return env.Null();
|
||||
}
|
||||
@ -1,6 +1,8 @@
|
||||
#include <napi.h>
|
||||
|
||||
#include "QtCore/QColor/qcolor_wrap.h"
|
||||
#include "QtCore/QObject/qobject_wrap.h"
|
||||
#include "QtCore/QPoint/qpoint_wrap.h"
|
||||
#include "QtCore/QRect/qrect_wrap.h"
|
||||
#include "QtCore/QSize/qsize_wrap.h"
|
||||
#include "QtCore/QUrl/qurl_wrap.h"
|
||||
@ -30,6 +32,7 @@
|
||||
#include "QtWidgets/QMainWindow/qmainwindow_wrap.h"
|
||||
#include "QtWidgets/QMenu/qmenu_wrap.h"
|
||||
#include "QtWidgets/QMenuBar/qmenubar_wrap.h"
|
||||
#include "QtWidgets/QPainter/qpainter_wrap.h"
|
||||
#include "QtWidgets/QPlainTextEdit/qplaintextedit_wrap.h"
|
||||
#include "QtWidgets/QProgressBar/qprogressbar_wrap.h"
|
||||
#include "QtWidgets/QPushButton/qpushbutton_wrap.h"
|
||||
@ -60,6 +63,8 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
|
||||
QVariantWrap::init(env, exports);
|
||||
QSizeWrap::init(env, exports);
|
||||
QRectWrap::init(env, exports);
|
||||
QPointWrap::init(env, exports);
|
||||
QColorWrap::init(env, exports);
|
||||
QUrlWrap::init(env, exports);
|
||||
QClipboardWrap::init(env, exports);
|
||||
QWidgetWrap::init(env, exports);
|
||||
@ -75,6 +80,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
|
||||
QFileDialogWrap::init(env, exports);
|
||||
QTableWidgetWrap::init(env, exports);
|
||||
QTableWidgetItemWrap::init(env, exports);
|
||||
QPainterWrap::init(env, exports);
|
||||
QTreeWidgetWrap::init(env, exports);
|
||||
QTreeWidgetItemWrap::init(env, exports);
|
||||
QGridLayoutWrap::init(env, exports);
|
||||
|
||||
103
src/demo.ts
103
src/demo.ts
@ -1,38 +1,83 @@
|
||||
import { QMainWindow, QTreeWidget, QTreeWidgetItem, QSystemTrayIcon, QIcon, QWidget, FlexLayout } from './index';
|
||||
import {
|
||||
FlexLayout,
|
||||
PenStyle,
|
||||
QColor,
|
||||
QMainWindow,
|
||||
QMainWindowEvents,
|
||||
QPainter,
|
||||
QPoint,
|
||||
QWidget,
|
||||
RenderHint,
|
||||
} from './index';
|
||||
|
||||
const win = new QMainWindow();
|
||||
const center = new QWidget();
|
||||
const layout = new FlexLayout();
|
||||
const hourHand = [new QPoint(7, 8), new QPoint(-7, 8), new QPoint(0, -40)];
|
||||
const minuteHand = [new QPoint(7, 8), new QPoint(-7, 8), new QPoint(0, -70)];
|
||||
const secondHand = [new QPoint(4, 8), new QPoint(-4, 8), new QPoint(0, -70)];
|
||||
const hourColor = new QColor(127, 0, 127);
|
||||
const minuteColor = new QColor(0, 127, 127, 191);
|
||||
const secondColor = new QColor(0, 0, 0);
|
||||
|
||||
const tree = new QTreeWidget();
|
||||
center.setLayout(layout);
|
||||
win.setWindowTitle('Analog Clock');
|
||||
win.resize(200, 200);
|
||||
const side = Math.min(win.geometry().width, win.geometry().height);
|
||||
|
||||
const item1 = new QTreeWidgetItem(tree, ['Hello item1!']);
|
||||
const item2 = new QTreeWidgetItem(item1, ['Hello item2!']);
|
||||
const item3 = new QTreeWidgetItem(['Hello item3!!']);
|
||||
tree.addTopLevelItem(item3);
|
||||
tree.addEventListener('itemSelectionChanged', () => {
|
||||
const items = tree.selectedItems();
|
||||
items.forEach(function(item) {
|
||||
const parent = item.parent();
|
||||
if (parent) {
|
||||
const child = parent.child(0);
|
||||
if (child) {
|
||||
console.log(child.text(0));
|
||||
}
|
||||
function repaint(): void {
|
||||
win.repaint();
|
||||
setTimeout(repaint, 1000);
|
||||
}
|
||||
|
||||
setTimeout(repaint, 1000);
|
||||
win.addEventListener(QMainWindowEvents.Paint, () => {
|
||||
const time = new Date();
|
||||
|
||||
const painter = new QPainter(win);
|
||||
painter.setRenderHint(RenderHint.Antialiasing);
|
||||
painter.translate(win.geometry().width / 2, win.geometry().height / 2);
|
||||
painter.scale(side / 200.0, side / 200.0);
|
||||
|
||||
painter.setPen(PenStyle.NoPen);
|
||||
painter.setBrush(hourColor);
|
||||
|
||||
painter.save();
|
||||
painter.rotate(30.0 * (time.getHours() + time.getMinutes() / 60.0));
|
||||
painter.drawConvexPolygon(hourHand);
|
||||
painter.restore();
|
||||
|
||||
painter.setPen(hourColor);
|
||||
|
||||
for (let i = 0; i < 12; ++i) {
|
||||
painter.drawLine(88, 0, 96, 0);
|
||||
painter.rotate(30.0);
|
||||
}
|
||||
|
||||
painter.setPen(PenStyle.NoPen);
|
||||
painter.setBrush(minuteColor);
|
||||
|
||||
painter.save();
|
||||
painter.rotate(6.0 * (time.getMinutes() + time.getSeconds() / 60.0));
|
||||
painter.drawConvexPolygon(minuteHand);
|
||||
painter.restore();
|
||||
|
||||
painter.setBrush(secondColor);
|
||||
painter.setPen(PenStyle.NoPen);
|
||||
|
||||
painter.save();
|
||||
painter.rotate(360 * (time.getSeconds() / 60.0));
|
||||
painter.drawConvexPolygon(secondHand);
|
||||
painter.restore();
|
||||
|
||||
painter.setPen(minuteColor);
|
||||
for (let j = 0; j < 60; ++j) {
|
||||
if (j % 5 != 0) {
|
||||
painter.drawLine(92, 0, 96, 0);
|
||||
}
|
||||
console.log(item.childCount());
|
||||
(global as any).gc();
|
||||
});
|
||||
painter.rotate(6.0);
|
||||
}
|
||||
painter.end();
|
||||
});
|
||||
center.setLayout(new FlexLayout());
|
||||
center.layout?.addWidget(tree);
|
||||
|
||||
win.setCentralWidget(center);
|
||||
win.show();
|
||||
|
||||
const systray = new QSystemTrayIcon();
|
||||
systray.setIcon(new QIcon());
|
||||
systray.show();
|
||||
systray.showMessage('Hello', 'world');
|
||||
|
||||
(global as any).win = win;
|
||||
(global as any).systray = systray;
|
||||
|
||||
@ -47,6 +47,7 @@ export { QPlainTextEdit, QPlainTextEditEvents, LineWrapMode } from './lib/QtWidg
|
||||
export { QScrollArea, QScrollAreaEvents } from './lib/QtWidgets/QScrollArea';
|
||||
export { QTreeWidget, QTreeWidgetEvents } from './lib/QtWidgets/QTreeWidget';
|
||||
export { QTreeWidgetItem } from './lib/QtWidgets/QTreeWidgetItem';
|
||||
export { QPainter, RenderHint } from './lib/QtWidgets/QPainter';
|
||||
|
||||
export {
|
||||
QSystemTrayIcon,
|
||||
@ -61,6 +62,8 @@ export { QObject, NodeObject } from './lib/QtCore/QObject';
|
||||
export { QVariant } from './lib/QtCore/QVariant';
|
||||
export { QSize } from './lib/QtCore/QSize';
|
||||
export { QRect } from './lib/QtCore/QRect';
|
||||
export { QPoint } from './lib/QtCore/QPoint';
|
||||
export { QColor } from './lib/QtCore/QColor';
|
||||
export { QUrl, ParsingMode } from './lib/QtCore/QUrl';
|
||||
// Layouts:
|
||||
export { QBoxLayout } from './lib/QtWidgets/QBoxLayout';
|
||||
|
||||
25
src/lib/QtCore/QColor.ts
Normal file
25
src/lib/QtCore/QColor.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { Component, NativeElement } from '../core/Component';
|
||||
import addon from '../utils/addon';
|
||||
import { checkIfNativeElement } from '../utils/helpers';
|
||||
|
||||
export class QColor extends Component {
|
||||
native: NativeElement;
|
||||
|
||||
//eslint-disable-next-line @typescript-eslint/no-inferrable-types
|
||||
constructor(arg?: NativeElement | number | string, g: number = 0, b: number = 0, a: number = 0) {
|
||||
super();
|
||||
const count = arguments.length;
|
||||
if (count == 4) {
|
||||
this.native = new addon.QColor(arg, g, b, a);
|
||||
} else if (count == 3) {
|
||||
this.native = new addon.QColor(arg, g, b);
|
||||
} else if (count == 1 && checkIfNativeElement(arg)) {
|
||||
this.native = arg as NativeElement;
|
||||
} else if (count == 1) {
|
||||
console.log(typeof arg);
|
||||
this.native = new addon.QColor(arg);
|
||||
} else {
|
||||
this.native = new addon.QColor();
|
||||
}
|
||||
}
|
||||
}
|
||||
20
src/lib/QtCore/QPoint.ts
Normal file
20
src/lib/QtCore/QPoint.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { NativeElement, Component } from '../core/Component';
|
||||
import addon from '../utils/addon';
|
||||
import { checkIfNativeElement } from '../utils/helpers';
|
||||
import { QVariant } from './QVariant';
|
||||
|
||||
export class QPoint extends Component {
|
||||
native: NativeElement;
|
||||
//eslint-disable-next-line @typescript-eslint/no-inferrable-types
|
||||
constructor(arg?: NativeElement | number, y: number = 0) {
|
||||
super();
|
||||
const count = arguments.length;
|
||||
if (count > 1) {
|
||||
this.native = new addon.QPoint(arg, y);
|
||||
} else if (count == 1 && checkIfNativeElement(arg)) {
|
||||
this.native = arg as NativeElement;
|
||||
} else {
|
||||
this.native = new addon.QPoint();
|
||||
}
|
||||
}
|
||||
}
|
||||
9
src/lib/QtEnums/PenStyle/index.ts
Normal file
9
src/lib/QtEnums/PenStyle/index.ts
Normal file
@ -0,0 +1,9 @@
|
||||
export enum PenStyle {
|
||||
NoPen,
|
||||
SolidLine,
|
||||
DashLine,
|
||||
DotLine,
|
||||
DashDotLine,
|
||||
DashDotDotLine,
|
||||
CustomDashLine,
|
||||
}
|
||||
@ -87,3 +87,4 @@ export { WindowFrameSection } from './WindowFrameSection';
|
||||
export { WindowModality } from './WindowModality';
|
||||
export { WindowState } from './WindowState';
|
||||
export { WindowType } from './WindowType';
|
||||
export { PenStyle } from './PenStyle';
|
||||
|
||||
85
src/lib/QtWidgets/QPainter.ts
Normal file
85
src/lib/QtWidgets/QPainter.ts
Normal file
@ -0,0 +1,85 @@
|
||||
import addon from '../utils/addon';
|
||||
import { Component, NativeElement } from '../core/Component';
|
||||
import { PenStyle, QColor, QPoint } from '../..';
|
||||
|
||||
export enum RenderHint {
|
||||
Antialiasing = 0x01,
|
||||
TextAntialiasing = 0x02,
|
||||
SmoothPixmapTransform = 0x04,
|
||||
HighQualityAntialiasing = 0x08,
|
||||
NonCosmeticDefaultPen = 0x10,
|
||||
Qt4CompatiblePainting = 0x20,
|
||||
LosslessImageRendering = 0x40,
|
||||
}
|
||||
|
||||
export class QPainter extends Component {
|
||||
native: NativeElement;
|
||||
|
||||
constructor(device?: Component) {
|
||||
let native;
|
||||
if (device) {
|
||||
native = new addon.QPainter(device.native);
|
||||
} else {
|
||||
native = new addon.QPainter();
|
||||
}
|
||||
super();
|
||||
this.native = native;
|
||||
}
|
||||
|
||||
drawText(x: number, y: number, text: string): void {
|
||||
return this.native.drawText(x, y, text);
|
||||
}
|
||||
|
||||
begin(device: Component): boolean {
|
||||
return this.native.begin(device.native);
|
||||
}
|
||||
|
||||
end(): boolean {
|
||||
return this.native.end();
|
||||
}
|
||||
|
||||
rotate(angle: number): void {
|
||||
this.native.rotate(angle);
|
||||
}
|
||||
|
||||
setPen(arg: PenStyle | QColor): void {
|
||||
if (typeof arg == 'number') {
|
||||
this.native.setPen(arg, 'style');
|
||||
} else if (arg instanceof QColor) {
|
||||
this.native.setPen(arg.native, 'color');
|
||||
}
|
||||
}
|
||||
|
||||
setRenderHint(hint: RenderHint, on = true) {
|
||||
this.native.setRenderHint(hint, on);
|
||||
}
|
||||
|
||||
drawLine(x1: number, y1: number, x2: number, y2: number): void {
|
||||
this.native.drawLine(x1, y1, x2, y2);
|
||||
}
|
||||
|
||||
scale(sx: number, sy: number): void {
|
||||
this.native.scale(sx, sy);
|
||||
}
|
||||
|
||||
translate(dx: number, dy: number): void {
|
||||
this.native.translate(dx, dy);
|
||||
}
|
||||
|
||||
drawConvexPolygon(points: QPoint[]): void {
|
||||
const nativePoints = points.map(point => point.native);
|
||||
this.native.drawConvexPolygon(nativePoints);
|
||||
}
|
||||
|
||||
save(): void {
|
||||
this.native.save();
|
||||
}
|
||||
|
||||
restore(): void {
|
||||
this.native.restore();
|
||||
}
|
||||
|
||||
setBrush(color: QColor) {
|
||||
this.native.setBrush(color.native);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user