Skip to content

Commit 8b6abd7

Browse files
authored
Add QDesktopWidget (#738)
* Add QDesktopWidget * Add docs
1 parent 4c8610a commit 8b6abd7

File tree

8 files changed

+181
-0
lines changed

8 files changed

+181
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
138138
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QStandardItemModel/qstandarditemmodel_wrap.cpp"
139139
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QStandardItem/qstandarditem_wrap.cpp"
140140
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QSvgWidget/qsvgwidget_wrap.cpp"
141+
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QDesktopWidget/qdesktopwidget_wrap.cpp"
141142
# Custom widgets (include them for automoc since they contain Q_OBJECT)
142143
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtCore/QObject/nobject.hpp"
143144
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/core/FlexLayout/flexlayout.hpp"
@@ -200,6 +201,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
200201
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QTextBrowser/ntextbrowser.hpp"
201202
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QTextEdit/ntextedit.hpp"
202203
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QSvgWidget/nsvgwidget.hpp"
204+
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QDesktopWidget/nqdesktopwidget.hpp"
203205
)
204206

205207
AddCommonConfig(${CORE_WIDGETS_ADDON})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include "core/NodeWidget/nodewidget.h"
4+
#include <QDesktopWidget>
5+
#include <QApplication>
6+
7+
class NQDesktopWidget : public QDesktopWidget, public NodeWidget {
8+
public:
9+
Q_OBJECT
10+
NODEWIDGET_IMPLEMENTATIONS(QDesktopWidget)
11+
public:
12+
using QDesktopWidget::QDesktopWidget; // inherit all constructors of QStatusBar
13+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
#include "napi.h"
3+
#include "QtWidgets/QWidget/qwidget_macro.h"
4+
#include <QPointer>
5+
#include "nqdesktopwidget.hpp"
6+
7+
class QDesktopWidgetWrap : public Napi::ObjectWrap<QDesktopWidgetWrap> {
8+
private:
9+
QPointer<NQDesktopWidget> instance;
10+
11+
public:
12+
static Napi::Object init(Napi::Env env, Napi::Object exports);
13+
QDesktopWidgetWrap(const Napi::CallbackInfo &info);
14+
~QDesktopWidgetWrap();
15+
NQDesktopWidget *getInternalInstance();
16+
static Napi::FunctionReference constructor;
17+
// wrapped methods
18+
Napi::Value availableGeometry(const Napi::CallbackInfo &info);
19+
Napi::Value screenGeometry(const Napi::CallbackInfo &info);
20+
Napi::Value screenNumber(const Napi::CallbackInfo &info);
21+
22+
QWIDGET_WRAPPED_METHODS_DECLARATION
23+
};
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "QtWidgets/QDesktopWidget/qdesktopwidget_wrap.h"
2+
3+
#include "Extras/Utils/nutils.h"
4+
#include "QtWidgets/QWidget/qwidget_wrap.h"
5+
#include "QtCore/QRect/qrect_wrap.h"
6+
7+
#include <QWidget>
8+
9+
Napi::FunctionReference QDesktopWidgetWrap::constructor;
10+
11+
Napi::Object QDesktopWidgetWrap::init(Napi::Env env, Napi::Object exports) {
12+
Napi::HandleScope scope(env);
13+
char CLASSNAME[] = "QDesktopWidget";
14+
Napi::Function func =
15+
DefineClass(env, CLASSNAME,
16+
{InstanceMethod("screenGeometry", &QDesktopWidgetWrap::screenGeometry),
17+
InstanceMethod("availableGeometry", &QDesktopWidgetWrap::availableGeometry),
18+
InstanceMethod("screenNumber", &QDesktopWidgetWrap::screenNumber),
19+
QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QDesktopWidgetWrap)});
20+
constructor = Napi::Persistent(func);
21+
exports.Set(CLASSNAME, func);
22+
return exports;
23+
}
24+
25+
NQDesktopWidget *QDesktopWidgetWrap::getInternalInstance() { return this->instance; }
26+
27+
QDesktopWidgetWrap::QDesktopWidgetWrap(const Napi::CallbackInfo &info)
28+
: Napi::ObjectWrap<QDesktopWidgetWrap>(info) {
29+
Napi::Env env = info.Env();
30+
Napi::HandleScope scope(env);
31+
32+
if (info.Length() == 0) {
33+
this->instance = new NQDesktopWidget();
34+
} else {
35+
Napi::TypeError::New(env, "Wrong number of arguments")
36+
.ThrowAsJavaScriptException();
37+
}
38+
this->rawData = extrautils::configureQWidget(
39+
this->getInternalInstance(), this->getInternalInstance()->getFlexNode(),
40+
true);
41+
}
42+
43+
QDesktopWidgetWrap::~QDesktopWidgetWrap() { extrautils::safeDelete(this->instance); }
44+
45+
Napi::Value QDesktopWidgetWrap::screenGeometry(const Napi::CallbackInfo &info) {
46+
Napi::Env env = info.Env();
47+
Napi::HandleScope scope(env);
48+
49+
Napi::Number screen = info[0].As<Napi::Number>();
50+
QRect rect = this->instance->screenGeometry(screen);
51+
auto instance = QRectWrap::constructor.New({Napi::External<QRect>::New(env, new QRect(rect))});
52+
return instance;
53+
}
54+
55+
Napi::Value QDesktopWidgetWrap::availableGeometry(const Napi::CallbackInfo &info) {
56+
Napi::Env env = info.Env();
57+
Napi::HandleScope scope(env);
58+
59+
Napi::Number screen = info[0].As<Napi::Number>();
60+
QRect rect = this->instance->availableGeometry(screen);
61+
auto instance = QRectWrap::constructor.New({Napi::External<QRect>::New(env, new QRect(rect))});
62+
return instance;
63+
}
64+
65+
Napi::Value QDesktopWidgetWrap::screenNumber(const Napi::CallbackInfo &info) {
66+
Napi::Env env = info.Env();
67+
Napi::HandleScope scope(env);
68+
int value = this->instance->screenNumber();
69+
return Napi::Value::From(env, value);
70+
}

src/cpp/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
#include "QtWidgets/QToolButton/qtoolbutton_wrap.h"
100100
#include "QtWidgets/QTreeWidget/qtreewidget_wrap.h"
101101
#include "QtWidgets/QTreeWidgetItem/qtreewidgetitem_wrap.h"
102+
#include "QtWidgets/QDesktopWidget/qdesktopwidget_wrap.h"
102103
#include "QtWidgets/QWidget/qwidget_wrap.h"
103104
#include "core/FlexLayout/flexlayout_wrap.h"
104105
#include "core/Integration/integration.h"
@@ -210,6 +211,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
210211
QStandardItemModelWrap::init(env, exports);
211212
QStandardItemWrap::init(env, exports);
212213
QSvgWidgetWrap::init(env, exports);
214+
QDesktopWidgetWrap::init(env, exports);
213215
return exports;
214216
}
215217

src/demo.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,27 @@ import { QLabel } from './lib/QtWidgets/QLabel';
44
import { QTreeWidget } from './lib/QtWidgets/QTreeWidget';
55
import { QTreeWidgetItem } from './lib/QtWidgets/QTreeWidgetItem';
66
import { QIcon } from './lib/QtGui/QIcon';
7+
import { QDesktopWidget } from './lib/QtWidgets/QDesktopWidget';
8+
import { QApplication } from './lib/QtGui/QApplication';
79

810
const win = new QMainWindow();
911
win.resize(500, 500);
1012

13+
// ex 1
14+
const desktop = new QDesktopWidget();
15+
const availableGeometry = desktop.availableGeometry();
16+
const screenGeometry = desktop.screenGeometry();
17+
console.log(availableGeometry.width() + 'x' + availableGeometry.height());
18+
console.log(screenGeometry.width() + 'x' + screenGeometry.height());
19+
console.log(desktop.screenNumber());
20+
// ex 2
21+
const qApp = QApplication.desktop()
22+
const availableGeometry2 = qApp.availableGeometry();
23+
const screenGeometry2 = qApp.screenGeometry();
24+
console.log(availableGeometry2.width() + 'x' + availableGeometry2.height());
25+
console.log(screenGeometry2.width() + 'x' + screenGeometry2.height());
26+
console.log(qApp.screenNumber());
27+
1128
const outer = new QWidget();
1229
const outerLayout = new QGridLayout();
1330
outer.setLayout(outerLayout);

src/lib/QtGui/QApplication.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { checkIfNativeElement } from '../utils/helpers';
44
import { QClipboard } from './QClipboard';
55
import { QStyle } from './QStyle';
66
import { QObjectSignals, NodeObject } from '../QtCore/QObject';
7+
import { QDesktopWidget } from '../QtWidgets/QDesktopWidget';
78

89
/**
910
@@ -64,6 +65,9 @@ export class QApplication extends NodeObject<QApplicationSignals> {
6465
static style(): QStyle {
6566
return new QStyle(addon.QApplication.style());
6667
}
68+
static desktop(): QDesktopWidget {
69+
return new QDesktopWidget();
70+
}
6771
}
6872

6973
export type QApplicationSignals = QObjectSignals;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { QRect } from '../QtCore/QRect';
2+
import { NodeWidget, QWidgetSignals } from './QWidget';
3+
import { NativeElement } from '../core/Component';
4+
import addon from '../utils/addon';
5+
6+
/**
7+
8+
> QDesktopWidget is a class that provides access to screen information on multi-head systems..
9+
10+
* **This class is a JS wrapper around Qt's [QDesktopWidget Class](https://doc.qt.io/qt-5/qdesktopwidget.html)**
11+
12+
The QDesktopWidget class provides information about the user's desktop, such as its total size, number of screens, the geometry of each screen, and whether they are configured as separate desktops or a single virtual desktop.
13+
14+
### Example
15+
16+
```js
17+
const { QDesktopWidget } = require("@nodegui/nodegui");
18+
19+
const desktop = new QDesktopWidget();
20+
const availableGeometry = desktop.availableGeometry();
21+
const screenGeometry = desktop.screenGeometry();
22+
console.log(availableGeometry.width() + 'x' + availableGeometry.height());
23+
console.log(screenGeometry.width() + 'x' + screenGeometry.height());
24+
console.log(desktop.screenNumber());
25+
```
26+
*/
27+
export type QDesktopWidgetSignals = QWidgetSignals;
28+
export class QDesktopWidget extends NodeWidget<QDesktopWidgetSignals> {
29+
native: NativeElement;
30+
constructor(parent?: NodeWidget<any>) {
31+
let native;
32+
if (parent) {
33+
native = new addon.QDesktopWidget(parent.native);
34+
} else {
35+
native = new addon.QDesktopWidget();
36+
}
37+
super(native);
38+
this.native = native;
39+
this.nodeParent = parent;
40+
}
41+
availableGeometry(screen = -1): QRect {
42+
return new QRect(this.native.availableGeometry(screen));
43+
}
44+
screenGeometry(screen = -1): QRect {
45+
return new QRect(this.native.screenGeometry(screen));
46+
}
47+
screenNumber(): number {
48+
return this.native.screenNumber();
49+
}
50+
}

0 commit comments

Comments
 (0)