Add QFrame and other improvements (#420)

This commit is contained in:
feng8848 2020-02-25 15:50:53 +08:00 committed by GitHub
parent 07aae3b04a
commit 9e1e8e576c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 277 additions and 39 deletions

View File

@ -69,6 +69,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QDateEdit/qdateedit_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QDateTimeEdit/qdatetimeedit_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QFileDialog/qfiledialog_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QFrame/qframe_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QListWidget/qlistwidget_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QListWidgetItem/qlistwidgetitem_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QTableView/qtableview_wrap.cpp"
@ -120,6 +121,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QDateTimeEdit/ndatetimeedit.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QDial/ndial.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QFileDialog/nfiledialog.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QFrame/nframe.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QListWidget/nlistwidget.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QTableView/ntableview.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QTableWidget/ntablewidget.hpp"

View File

@ -1,6 +1,6 @@
#pragma once
#include "QtWidgets/QWidget/qwidget_macro.h"
#include "QtWidgets/QFrame/qframe_macro.h"
#include "QtWidgets/QWidget/qwidget_wrap.h"
/*
@ -12,7 +12,9 @@
#ifndef QABSTRACTSCROLLAREA_WRAPPED_METHODS_DECLARATION
#define QABSTRACTSCROLLAREA_WRAPPED_METHODS_DECLARATION \
QWIDGET_WRAPPED_METHODS_DECLARATION \
\
QFRAME_WRAPPED_METHODS_DECLARATION \
\
Napi::Value setViewport(const Napi::CallbackInfo& info) { \
Napi::Env env = info.Env(); \
Napi::HandleScope scope(env); \
@ -38,13 +40,15 @@
#ifndef QABSTRACTSCROLLAREA_WRAPPED_METHODS_EXPORT_DEFINE
#define QABSTRACTSCROLLAREA_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \
QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \
\
QFRAME_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \
\
InstanceMethod("setViewport", &WidgetWrapName::setViewport), \
InstanceMethod("viewport", &WidgetWrapName::viewport),
#endif // QABSTRACTSCROLLAREA_WRAPPED_METHODS_EXPORT_DEFINE
#ifndef QABSTRACTSCROLLAREA_SIGNALS
#define QABSTRACTSCROLLAREA_SIGNALS QWIDGET_SIGNALS
#define QABSTRACTSCROLLAREA_SIGNALS QFRAME_SIGNALS
#endif

View File

@ -0,0 +1,16 @@
#pragma once
#include <QFrame>
#include "Extras/Utils/nutils.h"
#include "QtWidgets/QFrame/qframe_macro.h"
#include "core/NodeWidget/nodewidget.h"
class DLL_EXPORT NFrame : public QFrame, public NodeWidget {
Q_OBJECT
NODEWIDGET_IMPLEMENTATIONS(QFrame)
public:
using QFrame::QFrame;
void connectSignalsToEventEmitter() { QFRAME_SIGNALS }
};

View File

@ -0,0 +1,46 @@
#pragma once
#include "QtWidgets/QWidget/qwidget_macro.h"
#include "QtWidgets/QWidget/qwidget_wrap.h"
/*
This macro adds common QFrame exported methods
The exported methods are taken into this macro to avoid writing them in each
and every widget we export.
*/
#ifndef QFRAME_WRAPPED_METHODS_DECLARATION
#define QFRAME_WRAPPED_METHODS_DECLARATION \
\
QWIDGET_WRAPPED_METHODS_DECLARATION \
\
Napi::Value frameStyle(const Napi::CallbackInfo& info) { \
Napi::Env env = info.Env(); \
Napi::HandleScope scope(env); \
return Napi::Number::New(env, this->instance->frameStyle()); \
} \
Napi::Value setFrameStyle(const Napi::CallbackInfo& info) { \
Napi::Env env = info.Env(); \
Napi::HandleScope scope(env); \
int style = info[0].As<Napi::Number>().Int32Value(); \
this->instance->setFrameStyle(style); \
return env.Null(); \
}
#endif // QFRAME_WRAPPED_METHODS_DECLARATION
#ifndef QFRAME_WRAPPED_METHODS_EXPORT_DEFINE
#define QFRAME_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \
\
QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \
\
InstanceMethod("frameStyle", &WidgetWrapName::frameStyle), \
InstanceMethod("setFrameStyle", &WidgetWrapName::setFrameStyle),
#endif // QFRAME_WRAPPED_METHODS_EXPORT_DEFINE
#ifndef QFRAME_SIGNALS
#define QFRAME_SIGNALS QWIDGET_SIGNALS
#endif // QFRAME_SIGNALS

View File

@ -0,0 +1,24 @@
#pragma once
#include <napi.h>
#include <QPointer>
#include "Extras/Utils/nutils.h"
#include "QtWidgets/QFrame/nframe.hpp"
#include "QtWidgets/QFrame/qframe_macro.h"
class DLL_EXPORT QFrameWrap : public Napi::ObjectWrap<QFrameWrap> {
QFRAME_WRAPPED_METHODS_DECLARATION
private:
QPointer<NFrame> instance;
public:
static Napi::Object init(Napi::Env env, Napi::Object exports);
QFrameWrap(const Napi::CallbackInfo& info);
~QFrameWrap();
NFrame* getInternalInstance();
// class constructor
static Napi::FunctionReference constructor;
// wrapped methods
};

View File

@ -3,7 +3,7 @@
#include <QLabel>
#include "Extras/Utils/nutils.h"
#include "QtWidgets/QWidget/qwidget_macro.h"
#include "QtWidgets/QFrame/qframe_macro.h"
#include "core/NodeWidget/nodewidget.h"
class DLL_EXPORT NLabel : public QLabel, public NodeWidget {
@ -13,7 +13,7 @@ class DLL_EXPORT NLabel : public QLabel, public NodeWidget {
using QLabel::QLabel; // inherit all constructors of QLabel
void connectSignalsToEventEmitter() {
QWIDGET_SIGNALS
QFRAME_SIGNALS
QObject::connect(this, &QLabel::linkActivated, [=](const QString& link) {
Napi::Env env = this->emitOnNode.Env();
Napi::HandleScope scope(env);

View File

@ -5,11 +5,11 @@
#include <QPointer>
#include "Extras/Utils/nutils.h"
#include "QtWidgets/QWidget/qwidget_macro.h"
#include "QtWidgets/QFrame/qframe_macro.h"
#include "nlabel.hpp"
class DLL_EXPORT QLabelWrap : public Napi::ObjectWrap<QLabelWrap> {
QWIDGET_WRAPPED_METHODS_DECLARATION
QFRAME_WRAPPED_METHODS_DECLARATION
private:
QPointer<NLabel> instance;

View File

@ -1,8 +1,9 @@
#pragma once
#include <QStackedWidget>
#include "Extras/Utils/nutils.h"
#include "QtWidgets/QWidget/qwidget_macro.h"
#include "QtWidgets/QFrame/qframe_macro.h"
#include "core/NodeWidget/nodewidget.h"
#include "napi.h"
@ -14,7 +15,7 @@ class DLL_EXPORT NStackedWidget : public QStackedWidget, public NodeWidget {
// QStackedWidget
void connectSignalsToEventEmitter() {
QWIDGET_SIGNALS
QFRAME_SIGNALS
// Qt Connects: Implement all signal connects here
QObject::connect(this, &QStackedWidget::currentChanged, [=](int index) {
Napi::Env env = this->emitOnNode.Env();

View File

@ -5,12 +5,12 @@
#include <QPointer>
#include "Extras/Utils/nutils.h"
#include "QtWidgets/QFrame/qframe_macro.h"
#include "QtWidgets/QStackedWidget/nstackedwidget.hpp"
#include "QtWidgets/QWidget/qwidget_macro.h"
class DLL_EXPORT QStackedWidgetWrap
: public Napi::ObjectWrap<QStackedWidgetWrap> {
QWIDGET_WRAPPED_METHODS_DECLARATION
QFRAME_WRAPPED_METHODS_DECLARATION
private:
QPointer<NStackedWidget> instance;

View File

@ -0,0 +1,40 @@
#include "QtWidgets/QFrame/qframe_wrap.h"
#include "Extras/Utils/nutils.h"
Napi::FunctionReference QFrameWrap::constructor;
Napi::Object QFrameWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "QFrame";
Napi::Function func = DefineClass(
env, CLASSNAME, {QFRAME_WRAPPED_METHODS_EXPORT_DEFINE(QFrameWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
NFrame* QFrameWrap::getInternalInstance() { return this->instance; }
QFrameWrap::~QFrameWrap() { extrautils::safeDelete(this->instance); }
QFrameWrap::QFrameWrap(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<QFrameWrap>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() == 1) {
Napi::Object parentObject = info[0].As<Napi::Object>();
QWidgetWrap* parentWidgetWrap =
Napi::ObjectWrap<QWidgetWrap>::Unwrap(parentObject);
this->instance = new NFrame(parentWidgetWrap->getInternalInstance());
} else if (info.Length() == 0) {
this->instance = new NFrame();
} else {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = extrautils::configureQWidget(
this->getInternalInstance(), this->getInternalInstance()->getFlexNode(),
false);
}

View File

@ -24,7 +24,7 @@ Napi::Object QLabelWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("setNumInt", &QLabelWrap::setNumInt),
InstanceMethod("setPicture", &QLabelWrap::setPicture),
InstanceMethod("setPixmap", &QLabelWrap::setPixmap),
QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QLabelWrap)});
QFRAME_WRAPPED_METHODS_EXPORT_DEFINE(QLabelWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;

View File

@ -18,7 +18,7 @@ Napi::Object QStackedWidgetWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("currentIndex", &QStackedWidgetWrap::currentIndex),
InstanceMethod("setCurrentWidget",
&QStackedWidgetWrap::setCurrentWidget),
QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QStackedWidgetWrap)});
QFRAME_WRAPPED_METHODS_EXPORT_DEFINE(QStackedWidgetWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;

View File

@ -37,6 +37,7 @@
#include "QtWidgets/QDial/qdial_wrap.h"
#include "QtWidgets/QDialog/qdialog_wrap.h"
#include "QtWidgets/QFileDialog/qfiledialog_wrap.h"
#include "QtWidgets/QFrame/qframe_wrap.h"
#include "QtWidgets/QGridLayout/qgridlayout_wrap.h"
#include "QtWidgets/QGroupBox/qgroupbox_wrap.h"
#include "QtWidgets/QInputDialog/qinputdialog_wrap.h"
@ -111,6 +112,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
QDateEditWrap::init(env, exports);
QDateTimeEditWrap::init(env, exports);
QFileDialogWrap::init(env, exports);
QFrameWrap::init(env, exports);
QListWidgetWrap::init(env, exports);
QListWidgetItemWrap::init(env, exports);
QTableViewWrap::init(env, exports);

View File

@ -2,23 +2,15 @@ import { QTableView } from './lib/QtWidgets/QTableView';
import { QWidget } from './lib/QtWidgets/QWidget';
import { FlexLayout } from './lib/core/FlexLayout';
import { QTableWidget } from './lib/QtWidgets/QTableWidget';
// the code commented below have no problem
/*const view = new QTableView();
view.setStyleSheet(`
QTableView {
min-width:300px;
min-height:200px;
}
`);
view.show();
*/
import { QFrame, Shape } from './lib/QtWidgets/QFrame';
const widget = new QWidget();
const layout = new FlexLayout();
const view = new QTableView();
const table = new QTableWidget(5, 5);
const frame = new QFrame();
frame.setFrameShape(Shape.HLine);
view.setStyleSheet(`
QTableView {
@ -34,13 +26,21 @@ QTableView {
}
`);
frame.setStyleSheet(`
QFrame{
background: blue;
border: none;
height:15px;
}
`);
widget.setLayout(layout);
layout.addWidget(view); // app crash when add view to the layout
layout.addWidget(table); // app run ok when add table to the layout
layout.addWidget(view);
layout.addWidget(frame);
layout.addWidget(table);
widget.setInlineStyle('flex:1;');
widget.resize(600, 400);
widget.show();
//(global as any).main = view;
(global as any).main = widget;

View File

@ -41,6 +41,7 @@ export { QDateTimeEdit, NodeDateTimeEdit, QDateTimeEditSignals } from './lib/QtW
export { QLabel, QLabelSignals } from './lib/QtWidgets/QLabel';
export { QDial, QDialSignals } from './lib/QtWidgets/QDial';
export { QFileDialog, QFileDialogSignals } from './lib/QtWidgets/QFileDialog';
export { QFrame, QFrameSignals, Shadow, Shape } from './lib/QtWidgets/QFrame';
export { QLineEdit, QLineEditSignals, EchoMode } from './lib/QtWidgets/QLineEdit';
export { QMainWindow, QMainWindowSignals } from './lib/QtWidgets/QMainWindow';
export { QProgressBar, QProgressBarSignals, QProgressBarDirection } from './lib/QtWidgets/QProgressBar';

View File

@ -1,4 +1,5 @@
import { NodeWidget, QWidget, QWidgetSignals } from './QWidget';
import { NodeWidget, QWidget } from './QWidget';
import { NodeFrame, QFrameSignals } from './QFrame';
import { ScrollBarPolicy } from '../QtEnums/ScrollBarPolicy';
/**
@ -14,7 +15,7 @@ import { ScrollBarPolicy } from '../QtEnums/ScrollBarPolicy';
QAbstractScrollArea will list all methods and properties that are common to all scrollable widgets in the NodeGui world.
*/
export abstract class QAbstractScrollArea<Signals extends QAbstractScrollAreaSignals> extends NodeWidget<Signals> {
export abstract class QAbstractScrollArea<Signals extends QAbstractScrollAreaSignals> extends NodeFrame<Signals> {
viewportWidget?: NodeWidget<any>;
setViewport(widget: NodeWidget<any>): void {
this.viewportWidget = widget;
@ -34,4 +35,4 @@ export abstract class QAbstractScrollArea<Signals extends QAbstractScrollAreaSig
}
}
export type QAbstractScrollAreaSignals = QWidgetSignals;
export type QAbstractScrollAreaSignals = QFrameSignals;

View File

@ -0,0 +1,99 @@
import addon from '../utils/addon';
import { NodeWidget, QWidgetSignals } from './QWidget';
import { NativeElement } from '../core/Component';
import { checkIfNativeElement } from '../utils/helpers';
import { QRect } from '../QtCore/QRect';
export abstract class NodeFrame<Signals extends QFrameSignals> extends NodeWidget<Signals> {
setFrameRect(r: QRect): void {
this.setProperty('frameRect', r.native);
}
frameRect(): QRect {
return QRect.fromQVariant(this.property('frameRect'));
}
setFrameShadow(type: Shadow): void {
this.setProperty('frameShadow', type);
}
frameShadow(): Shadow {
return this.property('frameShadow').toInt();
}
setFrameShape(type: Shape): void {
this.setProperty('frameShape', type);
}
frameShape(): Shape {
return this.property('frameShape').toInt();
}
frameWidth(): number {
return this.property('frameWidth').toInt();
}
setLineWidth(width: number): void {
this.setProperty('lineWidth', width);
}
lineWidth(): number {
return this.property('lineWidth').toInt();
}
setMidLineWidth(width: number): void {
this.setProperty('midLineWidth', width);
}
midLineWidth(): number {
return this.property('midLineWidth').toInt();
}
setFrameStyle(style: number): void {
this.native.setFrameStyle(style);
}
frameStyle(): number {
return this.native.frameStyle();
}
}
export enum Shadow {
Plain = 0x0010,
Raised = 0x0020,
Sunken = 0x0030,
}
export enum Shape {
NoFrame = 0,
Box = 0x0001,
Panel = 0x0002,
WinPanel = 0x0003,
HLine = 0x0004,
VLine = 0x0005,
StyledPanel = 0x0006,
}
export type QFrameSignals = QWidgetSignals;
/**
> Create and control frame.
* **This class is a JS wrapper around Qt's [QFrame class](https://doc.qt.io/qt-5/qframe.html)**
The QFrame class is the base class of widgets that can have a frame. It can be used directly for creating simple placeholder frames without any contents.
### Example
```javascript
const { QFrame } = require("@nodegui/nodegui");
const frame = new QFrame();
```
*/
export class QFrame extends NodeFrame<QFrameSignals> {
native: NativeElement;
constructor(arg?: NodeWidget<QWidgetSignals> | NativeElement) {
let native;
let parent;
if (checkIfNativeElement(arg)) {
native = arg as NativeElement;
} else if (arg as NodeWidget<QWidgetSignals>) {
parent = arg as NodeWidget<QWidgetSignals>;
native = new addon.QFrame(parent.native);
} else {
native = new addon.QFrame();
}
super(native);
this.setNodeParent(parent);
this.native = native;
}
}

View File

@ -1,5 +1,6 @@
import addon from '../utils/addon';
import { NodeWidget, QWidgetSignals } from './QWidget';
import { NodeWidget } from './QWidget';
import { NodeFrame, QFrameSignals } from './QFrame';
import { NativeElement } from '../core/Component';
import { QPixmap } from '../QtGui/QPixmap';
import { QMovie } from '../QtGui/QMovie';
@ -26,7 +27,7 @@ label.setText("Hello");
```
*/
export class QLabel extends NodeWidget<QLabelSignals> {
export class QLabel extends NodeFrame<QLabelSignals> {
native: NativeElement;
private _picture?: QPicture;
private _pixmap?: QPixmap;
@ -153,7 +154,7 @@ export class QLabel extends NodeWidget<QLabelSignals> {
}
}
export interface QLabelSignals extends QWidgetSignals {
export interface QLabelSignals extends QFrameSignals {
linkActivated: (link: string) => void;
linkHovered: (link: string) => void;
}

View File

@ -1,5 +1,6 @@
import addon from '../utils/addon';
import { NodeWidget, QWidgetSignals } from './QWidget';
import { NodeWidget } from './QWidget';
import { NodeFrame, QFrameSignals } from './QFrame';
import { NativeElement } from '../core/Component';
/**
@ -43,7 +44,7 @@ win.show();
```
*/
export class QStackedWidget extends NodeWidget<QStackedWidgetSignals> {
export class QStackedWidget extends NodeFrame<QStackedWidgetSignals> {
native: NativeElement;
constructor();
constructor(parent: NodeWidget<any>);
@ -83,6 +84,6 @@ export class QStackedWidget extends NodeWidget<QStackedWidgetSignals> {
}
}
export interface QStackedWidgetSignals extends QWidgetSignals {
export interface QStackedWidgetSignals extends QFrameSignals {
currentChanged: (index: number) => void;
}