From 8b6abd75f8f6fb7659567fc8cbf7f525fb0ca6bc Mon Sep 17 00:00:00 2001 From: Ranieri Date: Tue, 24 Nov 2020 16:39:24 -0300 Subject: [PATCH] Add QDesktopWidget (#738) * Add QDesktopWidget * Add docs --- CMakeLists.txt | 2 + .../QDesktopWidget/nqdesktopwidget.hpp | 13 ++++ .../QDesktopWidget/qdesktopwidget_wrap.h | 23 ++++++ .../QDesktopWidget/qdesktopwidget_wrap.cpp | 70 +++++++++++++++++++ src/cpp/main.cpp | 2 + src/demo.ts | 17 +++++ src/lib/QtGui/QApplication.ts | 4 ++ src/lib/QtWidgets/QDesktopWidget.ts | 50 +++++++++++++ 8 files changed, 181 insertions(+) create mode 100644 src/cpp/include/nodegui/QtWidgets/QDesktopWidget/nqdesktopwidget.hpp create mode 100644 src/cpp/include/nodegui/QtWidgets/QDesktopWidget/qdesktopwidget_wrap.h create mode 100644 src/cpp/lib/QtWidgets/QDesktopWidget/qdesktopwidget_wrap.cpp create mode 100644 src/lib/QtWidgets/QDesktopWidget.ts diff --git a/CMakeLists.txt b/CMakeLists.txt index f4dfd7a06..1970b6480 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -138,6 +138,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QStandardItemModel/qstandarditemmodel_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QStandardItem/qstandarditem_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QSvgWidget/qsvgwidget_wrap.cpp" + "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QDesktopWidget/qdesktopwidget_wrap.cpp" # Custom widgets (include them for automoc since they contain Q_OBJECT) "${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtCore/QObject/nobject.hpp" "${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/core/FlexLayout/flexlayout.hpp" @@ -200,6 +201,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED "${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QTextBrowser/ntextbrowser.hpp" "${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QTextEdit/ntextedit.hpp" "${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QSvgWidget/nsvgwidget.hpp" + "${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QDesktopWidget/nqdesktopwidget.hpp" ) AddCommonConfig(${CORE_WIDGETS_ADDON}) diff --git a/src/cpp/include/nodegui/QtWidgets/QDesktopWidget/nqdesktopwidget.hpp b/src/cpp/include/nodegui/QtWidgets/QDesktopWidget/nqdesktopwidget.hpp new file mode 100644 index 000000000..7b4ce8c55 --- /dev/null +++ b/src/cpp/include/nodegui/QtWidgets/QDesktopWidget/nqdesktopwidget.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "core/NodeWidget/nodewidget.h" +#include +#include + +class NQDesktopWidget : public QDesktopWidget, public NodeWidget { + public: + Q_OBJECT + NODEWIDGET_IMPLEMENTATIONS(QDesktopWidget) + public: + using QDesktopWidget::QDesktopWidget; // inherit all constructors of QStatusBar +}; diff --git a/src/cpp/include/nodegui/QtWidgets/QDesktopWidget/qdesktopwidget_wrap.h b/src/cpp/include/nodegui/QtWidgets/QDesktopWidget/qdesktopwidget_wrap.h new file mode 100644 index 000000000..5ff2bfee4 --- /dev/null +++ b/src/cpp/include/nodegui/QtWidgets/QDesktopWidget/qdesktopwidget_wrap.h @@ -0,0 +1,23 @@ +#pragma once +#include "napi.h" +#include "QtWidgets/QWidget/qwidget_macro.h" +#include +#include "nqdesktopwidget.hpp" + +class QDesktopWidgetWrap : public Napi::ObjectWrap { + private: + QPointer instance; + + public: + static Napi::Object init(Napi::Env env, Napi::Object exports); + QDesktopWidgetWrap(const Napi::CallbackInfo &info); + ~QDesktopWidgetWrap(); + NQDesktopWidget *getInternalInstance(); + static Napi::FunctionReference constructor; + // wrapped methods + Napi::Value availableGeometry(const Napi::CallbackInfo &info); + Napi::Value screenGeometry(const Napi::CallbackInfo &info); + Napi::Value screenNumber(const Napi::CallbackInfo &info); + + QWIDGET_WRAPPED_METHODS_DECLARATION +}; diff --git a/src/cpp/lib/QtWidgets/QDesktopWidget/qdesktopwidget_wrap.cpp b/src/cpp/lib/QtWidgets/QDesktopWidget/qdesktopwidget_wrap.cpp new file mode 100644 index 000000000..e45e4b7eb --- /dev/null +++ b/src/cpp/lib/QtWidgets/QDesktopWidget/qdesktopwidget_wrap.cpp @@ -0,0 +1,70 @@ +#include "QtWidgets/QDesktopWidget/qdesktopwidget_wrap.h" + +#include "Extras/Utils/nutils.h" +#include "QtWidgets/QWidget/qwidget_wrap.h" +#include "QtCore/QRect/qrect_wrap.h" + +#include + +Napi::FunctionReference QDesktopWidgetWrap::constructor; + +Napi::Object QDesktopWidgetWrap::init(Napi::Env env, Napi::Object exports) { + Napi::HandleScope scope(env); + char CLASSNAME[] = "QDesktopWidget"; + Napi::Function func = + DefineClass(env, CLASSNAME, + {InstanceMethod("screenGeometry", &QDesktopWidgetWrap::screenGeometry), + InstanceMethod("availableGeometry", &QDesktopWidgetWrap::availableGeometry), + InstanceMethod("screenNumber", &QDesktopWidgetWrap::screenNumber), + QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QDesktopWidgetWrap)}); + constructor = Napi::Persistent(func); + exports.Set(CLASSNAME, func); + return exports; +} + +NQDesktopWidget *QDesktopWidgetWrap::getInternalInstance() { return this->instance; } + +QDesktopWidgetWrap::QDesktopWidgetWrap(const Napi::CallbackInfo &info) + : Napi::ObjectWrap(info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + if (info.Length() == 0) { + this->instance = new NQDesktopWidget(); + } else { + Napi::TypeError::New(env, "Wrong number of arguments") + .ThrowAsJavaScriptException(); + } + this->rawData = extrautils::configureQWidget( + this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), + true); +} + +QDesktopWidgetWrap::~QDesktopWidgetWrap() { extrautils::safeDelete(this->instance); } + +Napi::Value QDesktopWidgetWrap::screenGeometry(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + Napi::Number screen = info[0].As(); + QRect rect = this->instance->screenGeometry(screen); + auto instance = QRectWrap::constructor.New({Napi::External::New(env, new QRect(rect))}); + return instance; +} + +Napi::Value QDesktopWidgetWrap::availableGeometry(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + Napi::Number screen = info[0].As(); + QRect rect = this->instance->availableGeometry(screen); + auto instance = QRectWrap::constructor.New({Napi::External::New(env, new QRect(rect))}); + return instance; +} + +Napi::Value QDesktopWidgetWrap::screenNumber(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + int value = this->instance->screenNumber(); + return Napi::Value::From(env, value); +} diff --git a/src/cpp/main.cpp b/src/cpp/main.cpp index 4f5fba718..c3a6d545e 100644 --- a/src/cpp/main.cpp +++ b/src/cpp/main.cpp @@ -99,6 +99,7 @@ #include "QtWidgets/QToolButton/qtoolbutton_wrap.h" #include "QtWidgets/QTreeWidget/qtreewidget_wrap.h" #include "QtWidgets/QTreeWidgetItem/qtreewidgetitem_wrap.h" +#include "QtWidgets/QDesktopWidget/qdesktopwidget_wrap.h" #include "QtWidgets/QWidget/qwidget_wrap.h" #include "core/FlexLayout/flexlayout_wrap.h" #include "core/Integration/integration.h" @@ -210,6 +211,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) { QStandardItemModelWrap::init(env, exports); QStandardItemWrap::init(env, exports); QSvgWidgetWrap::init(env, exports); + QDesktopWidgetWrap::init(env, exports); return exports; } diff --git a/src/demo.ts b/src/demo.ts index f5446ca97..0adc1514b 100644 --- a/src/demo.ts +++ b/src/demo.ts @@ -4,10 +4,27 @@ import { QLabel } from './lib/QtWidgets/QLabel'; import { QTreeWidget } from './lib/QtWidgets/QTreeWidget'; import { QTreeWidgetItem } from './lib/QtWidgets/QTreeWidgetItem'; import { QIcon } from './lib/QtGui/QIcon'; +import { QDesktopWidget } from './lib/QtWidgets/QDesktopWidget'; +import { QApplication } from './lib/QtGui/QApplication'; const win = new QMainWindow(); win.resize(500, 500); +// ex 1 +const desktop = new QDesktopWidget(); +const availableGeometry = desktop.availableGeometry(); +const screenGeometry = desktop.screenGeometry(); +console.log(availableGeometry.width() + 'x' + availableGeometry.height()); +console.log(screenGeometry.width() + 'x' + screenGeometry.height()); +console.log(desktop.screenNumber()); +// ex 2 +const qApp = QApplication.desktop() +const availableGeometry2 = qApp.availableGeometry(); +const screenGeometry2 = qApp.screenGeometry(); +console.log(availableGeometry2.width() + 'x' + availableGeometry2.height()); +console.log(screenGeometry2.width() + 'x' + screenGeometry2.height()); +console.log(qApp.screenNumber()); + const outer = new QWidget(); const outerLayout = new QGridLayout(); outer.setLayout(outerLayout); diff --git a/src/lib/QtGui/QApplication.ts b/src/lib/QtGui/QApplication.ts index ba7323e95..34e74bb3b 100644 --- a/src/lib/QtGui/QApplication.ts +++ b/src/lib/QtGui/QApplication.ts @@ -4,6 +4,7 @@ import { checkIfNativeElement } from '../utils/helpers'; import { QClipboard } from './QClipboard'; import { QStyle } from './QStyle'; import { QObjectSignals, NodeObject } from '../QtCore/QObject'; +import { QDesktopWidget } from '../QtWidgets/QDesktopWidget'; /** @@ -64,6 +65,9 @@ export class QApplication extends NodeObject { static style(): QStyle { return new QStyle(addon.QApplication.style()); } + static desktop(): QDesktopWidget { + return new QDesktopWidget(); + } } export type QApplicationSignals = QObjectSignals; diff --git a/src/lib/QtWidgets/QDesktopWidget.ts b/src/lib/QtWidgets/QDesktopWidget.ts new file mode 100644 index 000000000..6cba77dbd --- /dev/null +++ b/src/lib/QtWidgets/QDesktopWidget.ts @@ -0,0 +1,50 @@ +import { QRect } from '../QtCore/QRect'; +import { NodeWidget, QWidgetSignals } from './QWidget'; +import { NativeElement } from '../core/Component'; +import addon from '../utils/addon'; + +/** + +> QDesktopWidget is a class that provides access to screen information on multi-head systems.. + +* **This class is a JS wrapper around Qt's [QDesktopWidget Class](https://doc.qt.io/qt-5/qdesktopwidget.html)** + +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. + +### Example + +```js +const { QDesktopWidget } = require("@nodegui/nodegui"); + +const desktop = new QDesktopWidget(); +const availableGeometry = desktop.availableGeometry(); +const screenGeometry = desktop.screenGeometry(); +console.log(availableGeometry.width() + 'x' + availableGeometry.height()); +console.log(screenGeometry.width() + 'x' + screenGeometry.height()); +console.log(desktop.screenNumber()); +``` + */ +export type QDesktopWidgetSignals = QWidgetSignals; +export class QDesktopWidget extends NodeWidget { + native: NativeElement; + constructor(parent?: NodeWidget) { + let native; + if (parent) { + native = new addon.QDesktopWidget(parent.native); + } else { + native = new addon.QDesktopWidget(); + } + super(native); + this.native = native; + this.nodeParent = parent; + } + availableGeometry(screen = -1): QRect { + return new QRect(this.native.availableGeometry(screen)); + } + screenGeometry(screen = -1): QRect { + return new QRect(this.native.screenGeometry(screen)); + } + screenNumber(): number { + return this.native.screenNumber(); + } +}