Add QColorDialog (#455)
* fix issue #439 * Add QTabBar * Add QColorDialog Co-authored-by: wuxiaofeng <wuxiaofeng@erayt.com>
This commit is contained in:
parent
38b8c9536d
commit
d80d27a10c
@ -67,6 +67,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QDialog/qdialog_wrap.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QBoxLayout/qboxlayout_wrap.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QCalendarWidget/qcalendarwidget_wrap.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QColorDialog/qcolordialog_wrap.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QComboBox/qcombobox_wrap.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QDateEdit/qdateedit_wrap.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QDateTimeEdit/qdatetimeedit_wrap.cpp"
|
||||
@ -121,6 +122,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QLabel/nlabel.hpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QCalendarWidget/ncalendarwidget.hpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QCheckBox/ncheckbox.hpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QColorDialog/ncolordialog.hpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QDateEdit/ndateedit.hpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QDateTimeEdit/ndatetimeedit.hpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QDial/ndial.hpp"
|
||||
|
||||
@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include <QColorDialog>
|
||||
|
||||
#include "Extras/Export/export.h"
|
||||
#include "QtGui/QColor/qcolor_wrap.h"
|
||||
#include "QtWidgets/QDialog/qdialog_macro.h"
|
||||
#include "core/NodeWidget/nodewidget.h"
|
||||
#include "napi.h"
|
||||
|
||||
class DLL_EXPORT NColorDialog : public QColorDialog, public NodeWidget {
|
||||
Q_OBJECT
|
||||
NODEWIDGET_IMPLEMENTATIONS(QColorDialog)
|
||||
public:
|
||||
using QColorDialog::QColorDialog;
|
||||
|
||||
void connectSignalsToEventEmitter() {
|
||||
QDIALOG_SIGNALS
|
||||
// Qt Connects: Implement all signal connects here
|
||||
QObject::connect(
|
||||
this, &QColorDialog::colorSelected, [=](const QColor& color) {
|
||||
Napi::Env env = this->emitOnNode.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
auto instance = QColorWrap::constructor.New(
|
||||
{Napi::External<QColor>::New(env, new QColor(color))});
|
||||
this->emitOnNode.Call(
|
||||
{Napi::String::New(env, "colorSelected"), instance});
|
||||
});
|
||||
|
||||
QObject::connect(
|
||||
this, &QColorDialog::currentColorChanged, [=](const QColor& color) {
|
||||
Napi::Env env = this->emitOnNode.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
auto instance = QColorWrap::constructor.New(
|
||||
{Napi::External<QColor>::New(env, new QColor(color))});
|
||||
this->emitOnNode.Call(
|
||||
{Napi::String::New(env, "currentColorChanged"), instance});
|
||||
});
|
||||
}
|
||||
};
|
||||
@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <napi.h>
|
||||
|
||||
#include <QPointer>
|
||||
|
||||
#include "Extras/Export/export.h"
|
||||
#include "QtWidgets/QColorDialog/ncolordialog.hpp"
|
||||
#include "QtWidgets/QDialog/qdialog_macro.h"
|
||||
|
||||
class DLL_EXPORT QColorDialogWrap : public Napi::ObjectWrap<QColorDialogWrap> {
|
||||
QDIALOG_WRAPPED_METHODS_DECLARATION
|
||||
private:
|
||||
QPointer<NColorDialog> instance;
|
||||
|
||||
public:
|
||||
static Napi::Object init(Napi::Env env, Napi::Object exports);
|
||||
QColorDialogWrap(const Napi::CallbackInfo& info);
|
||||
~QColorDialogWrap();
|
||||
NColorDialog* getInternalInstance();
|
||||
// class constructor
|
||||
static Napi::FunctionReference constructor;
|
||||
// wrapped methods
|
||||
Napi::Value selectedColor(const Napi::CallbackInfo& info);
|
||||
Napi::Value setOption(const Napi::CallbackInfo& info);
|
||||
Napi::Value testOption(const Napi::CallbackInfo& info);
|
||||
};
|
||||
|
||||
namespace StaticQColorDialogWrapMethods {
|
||||
DLL_EXPORT Napi::Value customColor(const Napi::CallbackInfo& info);
|
||||
DLL_EXPORT Napi::Value customCount(const Napi::CallbackInfo& info);
|
||||
DLL_EXPORT Napi::Value setCustomColor(const Napi::CallbackInfo& info);
|
||||
DLL_EXPORT Napi::Value setStandardColor(const Napi::CallbackInfo& info);
|
||||
DLL_EXPORT Napi::Value standardColor(const Napi::CallbackInfo& info);
|
||||
} // namespace StaticQColorDialogWrapMethods
|
||||
147
src/cpp/lib/QtWidgets/QColorDialog/qcolordialog_wrap.cpp
Normal file
147
src/cpp/lib/QtWidgets/QColorDialog/qcolordialog_wrap.cpp
Normal file
@ -0,0 +1,147 @@
|
||||
#include "QtWidgets/QColorDialog/qcolordialog_wrap.h"
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
#include "Extras/Utils/nutils.h"
|
||||
#include "QtGui/QColor/qcolor_wrap.h"
|
||||
#include "QtWidgets/QWidget/qwidget_wrap.h"
|
||||
|
||||
Napi::FunctionReference QColorDialogWrap::constructor;
|
||||
|
||||
Napi::Object QColorDialogWrap::init(Napi::Env env, Napi::Object exports) {
|
||||
Napi::HandleScope scope(env);
|
||||
char CLASSNAME[] = "QColorDialog";
|
||||
Napi::Function func = DefineClass(
|
||||
env, CLASSNAME,
|
||||
{InstanceMethod("selectedColor", &QColorDialogWrap::selectedColor),
|
||||
InstanceMethod("setOption", &QColorDialogWrap::setOption),
|
||||
InstanceMethod("testOption", &QColorDialogWrap::testOption),
|
||||
StaticMethod("customColor", &StaticQColorDialogWrapMethods::customColor),
|
||||
StaticMethod("customCount", &StaticQColorDialogWrapMethods::customCount),
|
||||
StaticMethod("setCustomColor",
|
||||
&StaticQColorDialogWrapMethods::setCustomColor),
|
||||
StaticMethod("setStandardColor",
|
||||
&StaticQColorDialogWrapMethods::setStandardColor),
|
||||
StaticMethod("standardColor",
|
||||
&StaticQColorDialogWrapMethods::standardColor),
|
||||
QDIALOG_WRAPPED_METHODS_EXPORT_DEFINE(QColorDialogWrap)});
|
||||
constructor = Napi::Persistent(func);
|
||||
exports.Set(CLASSNAME, func);
|
||||
return exports;
|
||||
}
|
||||
|
||||
NColorDialog* QColorDialogWrap::getInternalInstance() { return this->instance; }
|
||||
|
||||
QColorDialogWrap::~QColorDialogWrap() {
|
||||
extrautils::safeDelete(this->instance);
|
||||
}
|
||||
|
||||
QColorDialogWrap::QColorDialogWrap(const Napi::CallbackInfo& info)
|
||||
: Napi::ObjectWrap<QColorDialogWrap>(info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
if (info.Length() == 1) {
|
||||
Napi::Object parentObject = info[0].As<Napi::Object>();
|
||||
NodeWidgetWrap* parentWidgetWrap =
|
||||
Napi::ObjectWrap<NodeWidgetWrap>::Unwrap(parentObject);
|
||||
QWidget* parent = parentWidgetWrap->getInternalInstance();
|
||||
this->instance = new NColorDialog(parent);
|
||||
} else if (info.Length() == 0) {
|
||||
this->instance = new NColorDialog();
|
||||
} else {
|
||||
Napi::TypeError::New(env, "Wrong number of arguments")
|
||||
.ThrowAsJavaScriptException();
|
||||
}
|
||||
this->rawData = extrautils::configureQWidget(
|
||||
this->getInternalInstance(), this->getInternalInstance()->getFlexNode(),
|
||||
false);
|
||||
}
|
||||
|
||||
Napi::Value QColorDialogWrap::selectedColor(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
QColor color = this->instance->selectedColor();
|
||||
auto instance = QColorWrap::constructor.New(
|
||||
{Napi::External<QColor>::New(env, new QColor(color))});
|
||||
return instance;
|
||||
}
|
||||
|
||||
Napi::Value QColorDialogWrap::setOption(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
int option = info[0].As<Napi::Number>().Int32Value();
|
||||
bool on = info[1].As<Napi::Boolean>().Value();
|
||||
this->instance->setOption(
|
||||
static_cast<QColorDialog::ColorDialogOption>(option), on);
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
Napi::Value QColorDialogWrap::testOption(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
int option = info[0].As<Napi::Number>().Int32Value();
|
||||
bool on = this->instance->testOption(
|
||||
static_cast<QColorDialog::ColorDialogOption>(option));
|
||||
return Napi::Boolean::New(env, on);
|
||||
}
|
||||
|
||||
Napi::Value StaticQColorDialogWrapMethods::customColor(
|
||||
const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
int index = info[0].As<Napi::Number>().Int32Value();
|
||||
QColor color = QColorDialog::customColor(index);
|
||||
auto instance = QColorWrap::constructor.New(
|
||||
{Napi::External<QColor>::New(env, new QColor(color))});
|
||||
return instance;
|
||||
}
|
||||
|
||||
Napi::Value StaticQColorDialogWrapMethods::customCount(
|
||||
const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
int count = QColorDialog::customCount();
|
||||
return Napi::Number::New(env, count);
|
||||
}
|
||||
|
||||
Napi::Value StaticQColorDialogWrapMethods::setCustomColor(
|
||||
const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
int index = info[0].As<Napi::Number>().Int32Value();
|
||||
Napi::Object colorObject = info[1].As<Napi::Object>();
|
||||
QColorWrap* colorWrap = Napi::ObjectWrap<QColorWrap>::Unwrap(colorObject);
|
||||
QColorDialog::setCustomColor(index, *colorWrap->getInternalInstance());
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
Napi::Value StaticQColorDialogWrapMethods::setStandardColor(
|
||||
const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
int index = info[0].As<Napi::Number>().Int32Value();
|
||||
Napi::Object colorObject = info[1].As<Napi::Object>();
|
||||
QColorWrap* colorWrap = Napi::ObjectWrap<QColorWrap>::Unwrap(colorObject);
|
||||
QColorDialog::setStandardColor(index, *colorWrap->getInternalInstance());
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
Napi::Value StaticQColorDialogWrapMethods::standardColor(
|
||||
const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
int index = info[0].As<Napi::Number>().Int32Value();
|
||||
QColor color = QColorDialog::standardColor(index);
|
||||
auto instance = QColorWrap::constructor.New(
|
||||
{Napi::External<QColor>::New(env, new QColor(color))});
|
||||
return instance;
|
||||
}
|
||||
@ -31,6 +31,7 @@
|
||||
#include "QtWidgets/QButtonGroup/qbuttongroup_wrap.h"
|
||||
#include "QtWidgets/QCalendarWidget/qcalendarwidget_wrap.h"
|
||||
#include "QtWidgets/QCheckBox/qcheckbox_wrap.h"
|
||||
#include "QtWidgets/QColorDialog/qcolordialog_wrap.h"
|
||||
#include "QtWidgets/QComboBox/qcombobox_wrap.h"
|
||||
#include "QtWidgets/QDateEdit/qdateedit_wrap.h"
|
||||
#include "QtWidgets/QDateTimeEdit/qdatetimeedit_wrap.h"
|
||||
@ -109,6 +110,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
|
||||
QStyleWrap::init(env, exports);
|
||||
QCursorWrap::init(env, exports);
|
||||
QCalendarWidgetWrap::init(env, exports);
|
||||
QColorDialogWrap::init(env, exports);
|
||||
QComboBoxWrap::init(env, exports);
|
||||
QBoxLayoutWrap::init(env, exports);
|
||||
QDateEditWrap::init(env, exports);
|
||||
|
||||
14
src/demo.ts
14
src/demo.ts
@ -9,6 +9,8 @@ import {
|
||||
QLabel,
|
||||
QPixmap,
|
||||
} from './index';
|
||||
import { QColorDialog } from './lib/QtWidgets/QColorDialog';
|
||||
import { QColor } from './lib/QtGui/QColor';
|
||||
|
||||
const win = new QMainWindow();
|
||||
const center = new QWidget();
|
||||
@ -16,6 +18,7 @@ const label = new QLabel();
|
||||
const textInput = new QLineEdit();
|
||||
const getBtn = new QPushButton();
|
||||
const setBtn = new QPushButton();
|
||||
const colorBtn = new QPushButton();
|
||||
|
||||
//----------
|
||||
label.setText('Copy any image onto the clipboard and click `Get clipbard image button`');
|
||||
@ -37,10 +40,21 @@ setBtn.addEventListener('clicked', () => {
|
||||
label.setText(`Loaded image at ${textInput.text()} to global clipboard`);
|
||||
});
|
||||
|
||||
//--------------
|
||||
colorBtn.setText('Open color dialog');
|
||||
colorBtn.addEventListener('clicked', () => {
|
||||
const dialog = new QColorDialog();
|
||||
dialog.setCurrentColor(new QColor('white'));
|
||||
dialog.exec();
|
||||
const color = dialog.currentColor();
|
||||
console.log(color.red(), color.green(), color.blue());
|
||||
});
|
||||
|
||||
center.setLayout(new FlexLayout());
|
||||
center.layout?.addWidget(textInput);
|
||||
center.layout?.addWidget(setBtn);
|
||||
center.layout?.addWidget(getBtn);
|
||||
center.layout?.addWidget(colorBtn);
|
||||
center.layout?.addWidget(label);
|
||||
center.setInlineStyle(`width: 400; height: 400;`);
|
||||
win.setCentralWidget(center);
|
||||
|
||||
@ -36,6 +36,7 @@ export {
|
||||
// Widgets:
|
||||
export { QCalendarWidget, QCalendarWidgetSignals } from './lib/QtWidgets/QCalendarWidget';
|
||||
export { QCheckBox, QCheckBoxSignals } from './lib/QtWidgets/QCheckBox';
|
||||
export { QColorDialog, QColorDialogSignals } from './lib/QtWidgets/QColorDialog';
|
||||
export { QDateEdit } from './lib/QtWidgets/QDateEdit';
|
||||
export { QDateTimeEdit, NodeDateTimeEdit, QDateTimeEditSignals } from './lib/QtWidgets/QDateTimeEdit';
|
||||
export { QLabel, QLabelSignals } from './lib/QtWidgets/QLabel';
|
||||
|
||||
91
src/lib/QtWidgets/QColorDialog.ts
Normal file
91
src/lib/QtWidgets/QColorDialog.ts
Normal file
@ -0,0 +1,91 @@
|
||||
import addon from '../utils/addon';
|
||||
import { NodeWidget } from './QWidget';
|
||||
import { NativeElement } from '../core/Component';
|
||||
import { NodeDialog, QDialogSignals } from './QDialog';
|
||||
import { QColor } from '../QtGui/QColor';
|
||||
|
||||
/**
|
||||
|
||||
> Create and control color dialogs.
|
||||
|
||||
* **This class is a JS wrapper around Qt's [QColorDialog class](https://doc.qt.io/qt-5/qcolordialog.html)**
|
||||
|
||||
The `QColorDialog` class provides a dialog widget for specifying colors.
|
||||
|
||||
### Example
|
||||
|
||||
```javascript
|
||||
const { QColorDialog, QColor } = require("@nodegui/nodegui");
|
||||
|
||||
const colorDialog = new QColorDialog();
|
||||
colorDialog.setCurrentColor(new QColor('black'));
|
||||
colorDialog.exec();
|
||||
|
||||
const color = dialog.currentColor();
|
||||
console.log(color.red(), color.green(), color.blue());
|
||||
|
||||
```
|
||||
*/
|
||||
export class QColorDialog extends NodeDialog<QColorDialogSignals> {
|
||||
native: NativeElement;
|
||||
constructor();
|
||||
constructor(parent: NodeWidget<any>);
|
||||
constructor(parent?: NodeWidget<any>) {
|
||||
let native;
|
||||
if (parent) {
|
||||
native = new addon.QColorDialog(parent.native);
|
||||
} else {
|
||||
native = new addon.QColorDialog();
|
||||
}
|
||||
super(native);
|
||||
this.native = native;
|
||||
parent && this.setNodeParent(parent);
|
||||
}
|
||||
setCurrentColor(color: QColor): void {
|
||||
this.setProperty('currentColor', color.native);
|
||||
}
|
||||
currentColor(): QColor {
|
||||
return QColor.fromQVariant(this.property('currentColor'));
|
||||
}
|
||||
setOptions(options: ColorDialogOption): void {
|
||||
this.setProperty('options', options);
|
||||
}
|
||||
options(): ColorDialogOption {
|
||||
return this.property('options').toInt();
|
||||
}
|
||||
selectedColor(): QColor {
|
||||
return new QColor(this.native.selectedColor());
|
||||
}
|
||||
setOption(option: ColorDialogOption, on = true): void {
|
||||
this.native.setOption(option, on);
|
||||
}
|
||||
testOption(option: ColorDialogOption): boolean {
|
||||
return this.native.testOption(option);
|
||||
}
|
||||
static customColor(index: number): QColor {
|
||||
return new QColor(addon.QColorDialog.customColor(index));
|
||||
}
|
||||
static customCount(): number {
|
||||
return addon.QColorDialog.customCount();
|
||||
}
|
||||
static setCustomColor(index: number, color: QColor): void {
|
||||
addon.QColorDialog.setCustomColor(index, color.native);
|
||||
}
|
||||
static setStandardColor(index: number, color: QColor): void {
|
||||
addon.QColorDialog.setStandardColor(index, color.native);
|
||||
}
|
||||
static standardColor(index: number): QColor {
|
||||
return new QColor(addon.QColorDialog.standardColor(index));
|
||||
}
|
||||
}
|
||||
|
||||
export enum ColorDialogOption {
|
||||
ShowAlphaChannel = 0x00000001,
|
||||
NoButtons = 0x00000002,
|
||||
DontUseNativeDialog = 0x00000004,
|
||||
}
|
||||
|
||||
export interface QColorDialogSignals extends QDialogSignals {
|
||||
colorSelected: (color: QColor) => void;
|
||||
currentColorChanged: (color: QColor) => void;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user