From 942c96e54648bdee3d8448db2fa11a087ad134c4 Mon Sep 17 00:00:00 2001 From: Atul R Date: Sun, 4 Aug 2019 11:52:02 +0200 Subject: [PATCH] Adds scalable image via aspect ratio setting --- src/cpp/QtGui/QPixmap/qpixmap_wrap.cpp | 14 ++++++++++---- src/cpp/QtGui/QWidget/qwidget_macro.h | 11 +++++++++++ src/lib/QtEnums/index.ts | 5 +++++ src/lib/QtGui/QPixmap/index.ts | 11 ++++++++--- src/lib/QtGui/QWidget/index.ts | 3 +++ src/lib/index.ts | 2 ++ 6 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 src/lib/QtEnums/index.ts diff --git a/src/cpp/QtGui/QPixmap/qpixmap_wrap.cpp b/src/cpp/QtGui/QPixmap/qpixmap_wrap.cpp index 1354c21a2..06069a796 100644 --- a/src/cpp/QtGui/QPixmap/qpixmap_wrap.cpp +++ b/src/cpp/QtGui/QPixmap/qpixmap_wrap.cpp @@ -66,10 +66,16 @@ Napi::Value QPixmapWrap::scaled(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); - Napi::Number width = info[0].As(); - Napi::Number height = info[1].As(); - - QPixmap* updatedPixMap = new QPixmap(this->instance->scaled(width, height)); + Napi::Number widthValue = info[0].As(); + Napi::Number heightValue = info[1].As(); + int width = widthValue.Int32Value(); + int height = heightValue.Int32Value(); + Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio; + if(info.Length() > 2){ + int aspectRatioModeInt = info[2].As().Int32Value(); + aspectRatioMode = static_cast(aspectRatioModeInt); + } + QPixmap* updatedPixMap = new QPixmap(this->instance->scaled(width, height, aspectRatioMode)); auto instance = QPixmapWrap::constructor.New({ Napi::External::New(env, updatedPixMap) }); return instance; } diff --git a/src/cpp/QtGui/QWidget/qwidget_macro.h b/src/cpp/QtGui/QWidget/qwidget_macro.h index e009dbd65..6f0756643 100644 --- a/src/cpp/QtGui/QWidget/qwidget_macro.h +++ b/src/cpp/QtGui/QWidget/qwidget_macro.h @@ -4,6 +4,7 @@ #include "src/cpp/core/YogaWidget/yogawidget_macro.h" #include "src/cpp/core/Events/eventwidget_macro.h" #include "src/cpp/core/Component/component_macro.h" +#include /* This macro adds common QWidgets exported methods @@ -121,6 +122,15 @@ Napi::Value update(const Napi::CallbackInfo& info){ \ this->instance->update(); \ return env.Null(); \ } \ +Napi::Value size(const Napi::CallbackInfo& info){ \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + QSize size = this->instance->size(); \ + Napi::Object sizeObj = Napi::Object::New(env); \ + sizeObj.Set("width", size.width()); \ + sizeObj.Set("height", size.height()); \ + return sizeObj; \ +} \ #endif //QWIDGET_WRAPPED_METHODS_DECLARATION @@ -144,6 +154,7 @@ Napi::Value update(const Napi::CallbackInfo& info){ \ InstanceMethod("setMinimumSize",&WidgetWrapName::setMinimumSize), \ InstanceMethod("repaint",&WidgetWrapName::repaint), \ InstanceMethod("update",&WidgetWrapName::update), \ + InstanceMethod("size",&WidgetWrapName::size), \ #endif // QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE diff --git a/src/lib/QtEnums/index.ts b/src/lib/QtEnums/index.ts new file mode 100644 index 000000000..7c6193ee6 --- /dev/null +++ b/src/lib/QtEnums/index.ts @@ -0,0 +1,5 @@ +export enum AspectRatioMode { + "IgnoreAspectRatio", + "KeepAspectRatio", + "KeepAspectRatioByExpanding" +} diff --git a/src/lib/QtGui/QPixmap/index.ts b/src/lib/QtGui/QPixmap/index.ts index a89432669..8a6119728 100644 --- a/src/lib/QtGui/QPixmap/index.ts +++ b/src/lib/QtGui/QPixmap/index.ts @@ -1,7 +1,8 @@ import addon from "../../core/addon"; import { Component, NativeElement } from "../../core/Component"; - +import { AspectRatioMode } from "../../QtEnums"; type arg = string | NativeElement; + export class QPixmap extends Component { native: NativeElement; constructor(arg?: arg) { @@ -18,8 +19,12 @@ export class QPixmap extends Component { load = (imageUrl: string) => { return this.native.load(imageUrl); }; - scaled = (width: number, height: number): QPixmap => { - const nativePixmap = this.native.scaled(width, height); + scaled = ( + width: number, + height: number, + aspectRatioMode?: AspectRatioMode + ): QPixmap => { + const nativePixmap = this.native.scaled(width, height, aspectRatioMode); return new QPixmap(nativePixmap); }; } diff --git a/src/lib/QtGui/QWidget/index.ts b/src/lib/QtGui/QWidget/index.ts index f5a1bb8e4..38ed53d56 100644 --- a/src/lib/QtGui/QWidget/index.ts +++ b/src/lib/QtGui/QWidget/index.ts @@ -57,6 +57,9 @@ export abstract class NodeWidget extends EventWidget { update = () => { this.native.update(); }; + size = (): { width: number; height: number } => { + return this.native.size(); + }; } export class QWidget extends NodeWidget { diff --git a/src/lib/index.ts b/src/lib/index.ts index 827e1abaa..7e2d1e052 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1,3 +1,5 @@ +// enums +export { AspectRatioMode } from "./QtEnums"; export { QApplication } from "./QtGui/QApplication"; export { QWidget, QWidgetEvents } from "./QtGui/QWidget"; export { QPixmap } from "./QtGui/QPixmap";