From e6eccd06a9963fa25ebd954d2b32c9eaffa50430 Mon Sep 17 00:00:00 2001 From: Atul R Date: Sat, 3 Aug 2019 23:35:11 +0200 Subject: [PATCH] Added Image rendering support via qlabel and qpixmap --- config/application.gypi | 1 + docs/README.md | 3 +- docs/api/QLabel.md | 6 ++ docs/api/QPixmap.md | 43 ++++++++++++++ package-lock.json | 2 +- .../QtGui/QApplication/qapplication_wrap.h | 4 +- src/cpp/QtGui/QPixmap/qpixmap_wrap.cpp | 57 +++++++++++++++++++ src/cpp/QtGui/QPixmap/qpixmap_wrap.h | 18 ++++++ src/cpp/QtWidgets/QLabel/qlabel_wrap.cpp | 14 ++++- src/cpp/QtWidgets/QLabel/qlabel_wrap.h | 1 + src/cpp/main.cpp | 2 + src/lib/QtGui/QPixmap/index.ts | 17 ++++++ src/lib/QtWidgets/QLabel/index.ts | 4 ++ src/lib/index.ts | 1 + 14 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 docs/api/QPixmap.md create mode 100644 src/cpp/QtGui/QPixmap/qpixmap_wrap.cpp create mode 100644 src/cpp/QtGui/QPixmap/qpixmap_wrap.h create mode 100644 src/lib/QtGui/QPixmap/index.ts diff --git a/config/application.gypi b/config/application.gypi index bc81fc510..6f4f6fe17 100644 --- a/config/application.gypi +++ b/config/application.gypi @@ -14,6 +14,7 @@ "../src/cpp/QtGui/QApplication/qapplication_wrap.cpp", "../src/cpp/QtGui/QWidget/qwidget_wrap.cpp", "../src/cpp/QtGui/QEvent/QKeyEvent/qkeyevent_wrap.cpp", + "../src/cpp/QtGui/QPixmap/qpixmap_wrap.cpp", '../src/cpp/core/FlexLayout/flexlayout_wrap.cpp', "../src/cpp/QtWidgets/QGridLayout/qgridlayout_wrap.cpp", "../src/cpp/QtWidgets/QLabel/qlabel_wrap.cpp", diff --git a/docs/README.md b/docs/README.md index 3732697e4..6eb7de217 100644 --- a/docs/README.md +++ b/docs/README.md @@ -37,13 +37,14 @@ - [QMainWindow (Window)](api/QMainWindow.md) - [QWidget (View)](api/QWidget.md) -- [QLabel (Text)](api/QLabel.md) +- [QLabel (Text/Image)](api/QLabel.md) - [QPushButton (Button)](api/QPushButton.md) - [QRadioButton (RadioButton)](api/QRadioButton.md) - [QCheckBox (CheckBox)](api/QCheckBox.md) - [QLineEdit (LineEdit)](api/QLineEdit.md) - [QProgressBar (ProgressBar)](api/QProgressBar.md) - [FlexLayout](api/FlexLayout.md) +- [QPixmap](api/QPixmap.md) ### Internal Modules diff --git a/docs/api/QLabel.md b/docs/api/QLabel.md index fa1ebdd17..66bdb7aaa 100644 --- a/docs/api/QLabel.md +++ b/docs/api/QLabel.md @@ -48,3 +48,9 @@ returns the current text from the label. #### `label.setWordWrap(on)` - `on` boolean - If true it sets wordwrap on the label + +#### `label.setPixmap(pixMap)` + +Images in the form of a pixmap can be set as the label content + +- `pixMap` [QPixmap](api/QPixmap.md) - Allows to set image content in the form of a QPixmap on the label diff --git a/docs/api/QPixmap.md b/docs/api/QPixmap.md new file mode 100644 index 000000000..f095fb6c4 --- /dev/null +++ b/docs/api/QPixmap.md @@ -0,0 +1,43 @@ +## Class: QPixmap + +> The QPixmap class helps hold an image in the form of off-screen image representation. + +**This class is a JS wrapper around Qt's [QPixmap class](https://doc.qt.io/qt-5/qpixmap.html)** + +A `QPixmap` provides ability to store an image in the memory. + +**QPixmap inherits from [Component](api/Component.md)** + +### Example + +```javascript +const { QPixmap } = require("@nodegui/nodegui"); + +const imageUrl = "path/to/png"; +const pixMap = new QPixmap(imageUrl); +``` + +### `new QPixmap(imageUrl?)` + +- `imageUrl` string (_optional_). Absolute path of the image that needs to be loaded in the memory. + +### Static Methods + +QPixmap can access all the static methods defined in [Component](api/Component.md) + +### Instance Properties + +QPixmap can access all the instance properties defined in [Component](api/Component.md) + +### Instance Methods + +QPixmap can access all the instance methods defined in [Component](api/Component.md) + +Additionally it also has the following instance methods: + +#### `pixMap.load(imageUrl)` + +loads an image from the url into memory as a Pixmap. +returns true if load was successful otherwise returns false. + +- `imageUrl` string (_optional_). Absolute path of the image that needs to be loaded in the memory. diff --git a/package-lock.json b/package-lock.json index f996e1714..e48e728d9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@nodegui/nodegui", - "version": "0.0.6-alpha", + "version": "0.0.7-alpha", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/cpp/QtGui/QApplication/qapplication_wrap.h b/src/cpp/QtGui/QApplication/qapplication_wrap.h index 635789547..ad291a76b 100644 --- a/src/cpp/QtGui/QApplication/qapplication_wrap.h +++ b/src/cpp/QtGui/QApplication/qapplication_wrap.h @@ -1,5 +1,4 @@ -#ifndef QAPPLICATION_WRAP_H -#define QAPPLICATION_WRAP_H +#pragma once #include #include @@ -22,4 +21,3 @@ public: Napi::Value exec(const Napi::CallbackInfo& info); }; -#endif \ No newline at end of file diff --git a/src/cpp/QtGui/QPixmap/qpixmap_wrap.cpp b/src/cpp/QtGui/QPixmap/qpixmap_wrap.cpp new file mode 100644 index 000000000..8f6864c91 --- /dev/null +++ b/src/cpp/QtGui/QPixmap/qpixmap_wrap.cpp @@ -0,0 +1,57 @@ +#include "qpixmap_wrap.h" +#include "src/cpp/Extras/Utils/nutils.h" + +Napi::FunctionReference QPixmapWrap::constructor; + +Napi::Object QPixmapWrap::init(Napi::Env env, Napi::Object exports) +{ + Napi::HandleScope scope(env); + char CLASSNAME[] = "QPixmap"; + Napi::Function func = DefineClass(env, CLASSNAME,{ + InstanceMethod("load", &QPixmapWrap::load), + }); + constructor = Napi::Persistent(func); + exports.Set(CLASSNAME, func); + return exports; +} + +QPixmapWrap::QPixmapWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) +{ + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + if(info.Length() == 1) { + Napi::String url = info[0].As(); + QString imageUrl = QString::fromUtf8(url.Utf8Value().c_str()); + this->instance = new QPixmap(imageUrl); + }else if (info.Length() == 0){ + this->instance = new QPixmap(); + }else { + extrautils::throwTypeError(env, "Wrong number of arguments"); + } +} + +QPixmapWrap::~QPixmapWrap() +{ + delete this->instance; +} + +QPixmap* QPixmapWrap::getInternalInstance() +{ + return this->instance; +} + +Napi::Value QPixmapWrap::load(const Napi::CallbackInfo& info) +{ + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + bool loadSuccess = false; + if(info.Length() == 1) { + Napi::String url = info[0].As(); + QString imageUrl = QString::fromUtf8(url.Utf8Value().c_str()); + loadSuccess = this->instance->load(imageUrl); + }else { + extrautils::throwTypeError(env, "Wrong number of arguments"); + } + return Napi::Boolean::New(env, loadSuccess); +} + diff --git a/src/cpp/QtGui/QPixmap/qpixmap_wrap.h b/src/cpp/QtGui/QPixmap/qpixmap_wrap.h new file mode 100644 index 000000000..ed0c7bdb3 --- /dev/null +++ b/src/cpp/QtGui/QPixmap/qpixmap_wrap.h @@ -0,0 +1,18 @@ +#pragma once + +#include +#include + +class QPixmapWrap : public Napi::ObjectWrap { +private: + QPixmap* instance; +public: + static Napi::FunctionReference constructor; + static Napi::Object init(Napi::Env env, Napi::Object exports); + QPixmapWrap(const Napi::CallbackInfo& info); + ~QPixmapWrap(); + QPixmap* getInternalInstance(); + // Wrapped methods + Napi::Value load(const Napi::CallbackInfo& info); +}; + diff --git a/src/cpp/QtWidgets/QLabel/qlabel_wrap.cpp b/src/cpp/QtWidgets/QLabel/qlabel_wrap.cpp index e7c957095..8a2f74a1a 100644 --- a/src/cpp/QtWidgets/QLabel/qlabel_wrap.cpp +++ b/src/cpp/QtWidgets/QLabel/qlabel_wrap.cpp @@ -1,5 +1,6 @@ #include "qlabel_wrap.h" #include "src/cpp/QtGui/QWidget/qwidget_wrap.h" +#include "src/cpp/QtGui/QPixmap/qpixmap_wrap.h" #include "src/cpp/Extras/Utils/nutils.h" #include @@ -12,7 +13,7 @@ Napi::Object QLabelWrap::init(Napi::Env env, Napi::Object exports) { InstanceMethod("setWordWrap", &QLabelWrap::setWordWrap), InstanceMethod("setText", &QLabelWrap::setText), InstanceMethod("text", &QLabelWrap::text), - InstanceMethod("getFlexNode", &QLabelWrap::getFlexNode), + InstanceMethod("setPixmap", &QLabelWrap::setPixmap), QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QLabelWrap) }); constructor = Napi::Persistent(func); @@ -74,3 +75,14 @@ Napi::Value QLabelWrap::text(const Napi::CallbackInfo &info) std::string labelText = this->instance->text().toStdString(); return Napi::String::New(env, labelText); } + + Napi::Value QLabelWrap::setPixmap(const Napi::CallbackInfo &info) + { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + Napi::Object pixmapObject = info[0].As(); + QPixmapWrap* pixmapWrap = Napi::ObjectWrap::Unwrap(pixmapObject); + this->instance->setPixmap(*pixmapWrap->getInternalInstance()); + return env.Null(); + } diff --git a/src/cpp/QtWidgets/QLabel/qlabel_wrap.h b/src/cpp/QtWidgets/QLabel/qlabel_wrap.h index 5c8d40589..24c04ebe9 100644 --- a/src/cpp/QtWidgets/QLabel/qlabel_wrap.h +++ b/src/cpp/QtWidgets/QLabel/qlabel_wrap.h @@ -19,6 +19,7 @@ class QLabelWrap : public Napi::ObjectWrap{ Napi::Value setWordWrap(const Napi::CallbackInfo& info); Napi::Value setText(const Napi::CallbackInfo& info); Napi::Value text(const Napi::CallbackInfo &info); + Napi::Value setPixmap(const Napi::CallbackInfo &info); QWIDGET_WRAPPED_METHODS_DECLARATION diff --git a/src/cpp/main.cpp b/src/cpp/main.cpp index e667fbacc..e067c2709 100644 --- a/src/cpp/main.cpp +++ b/src/cpp/main.cpp @@ -1,5 +1,6 @@ #include "src/cpp/QtGui/QApplication/qapplication_wrap.h" #include "src/cpp/QtGui/QWidget/qwidget_wrap.h" +#include "src/cpp/QtGui/QPixmap/qpixmap_wrap.h" #include "src/cpp/QtWidgets/QGridLayout/qgridlayout_wrap.h" #include "src/cpp/QtWidgets/QLayout/qlayout_wrap.h" #include "src/cpp/QtWidgets/QLabel/qlabel_wrap.h" @@ -21,6 +22,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) { InitPrivateHelpers(env); QApplicationWrap::init(env, exports); QWidgetWrap::init(env, exports); + QPixmapWrap::init(env, exports); QGridLayoutWrap::init(env, exports); FlexLayoutWrap::init(env, exports); QMainWindowWrap::init(env,exports); diff --git a/src/lib/QtGui/QPixmap/index.ts b/src/lib/QtGui/QPixmap/index.ts new file mode 100644 index 000000000..1990c01b0 --- /dev/null +++ b/src/lib/QtGui/QPixmap/index.ts @@ -0,0 +1,17 @@ +import addon from "../../core/addon"; +import { Component, NativeElement } from "../../core/Component"; + +export class QPixmap extends Component { + native: NativeElement; + constructor(imageUrl?: string) { + super(); + if (imageUrl) { + this.native = new addon.QPixmap(imageUrl); + } else { + this.native = new addon.QPixmap(); + } + } + load = (imageUrl: string) => { + return this.native.load(imageUrl); + }; +} diff --git a/src/lib/QtWidgets/QLabel/index.ts b/src/lib/QtWidgets/QLabel/index.ts index 206445369..1fc4eb726 100644 --- a/src/lib/QtWidgets/QLabel/index.ts +++ b/src/lib/QtWidgets/QLabel/index.ts @@ -2,6 +2,7 @@ import addon from "../../core/addon"; import { NodeWidget } from "../../QtGui/QWidget"; import { BaseWidgetEvents } from "../../core/EventWidget"; import { NativeElement } from "../../core/Component"; +import { QPixmap } from "../../QtGui/QPixmap"; export const QLabelEvents = Object.freeze({ ...BaseWidgetEvents @@ -32,4 +33,7 @@ export class QLabel extends NodeWidget { text() { return this.native.text(); } + setPixmap(pixMap: QPixmap) { + this.native.setPixmap(pixMap.native); + } } diff --git a/src/lib/index.ts b/src/lib/index.ts index bbb018fc7..827e1abaa 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1,5 +1,6 @@ export { QApplication } from "./QtGui/QApplication"; export { QWidget, QWidgetEvents } from "./QtGui/QWidget"; +export { QPixmap } from "./QtGui/QPixmap"; // Abstract: export { NodeWidget } from "./QtGui/QWidget"; export { NodeLayout } from "./QtWidgets/QLayout";