From 0f4f0c5567d11b0721786651716d8ed498a13c5a Mon Sep 17 00:00:00 2001 From: Atul R Date: Mon, 27 Jan 2020 20:43:52 +0100 Subject: [PATCH] fixes qmovie breaking on error. (#370) Adds default error handler for all event listeners --- .../include/nodegui/QtGui/QMovie/qmovie_wrap.h | 4 ++++ src/cpp/lib/QtGui/QMovie/qmovie_wrap.cpp | 15 +++++++++++---- src/lib/QtGui/QMovie.ts | 3 +++ src/lib/core/EventWidget.ts | 6 ++++++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/cpp/include/nodegui/QtGui/QMovie/qmovie_wrap.h b/src/cpp/include/nodegui/QtGui/QMovie/qmovie_wrap.h index 32786eeda..9a8f5558d 100644 --- a/src/cpp/include/nodegui/QtGui/QMovie/qmovie_wrap.h +++ b/src/cpp/include/nodegui/QtGui/QMovie/qmovie_wrap.h @@ -2,7 +2,9 @@ #include +#include #include +#include #include "Extras/Utils/nutils.h" #include "QtCore/QObject/qobject_macro.h" @@ -13,6 +15,7 @@ class DLL_EXPORT QMovieWrap : public Napi::ObjectWrap { private: QPointer instance; + QSharedPointer bufferDevice; public: static Napi::Object init(Napi::Env env, Napi::Object exports); @@ -36,4 +39,5 @@ class DLL_EXPORT QMovieWrap : public Napi::ObjectWrap { Napi::Value currentFrameNumber(const Napi::CallbackInfo& info); Napi::Value currentPixmap(const Napi::CallbackInfo& info); Napi::Value loadFromData(const Napi::CallbackInfo& info); + Napi::Value frameCount(const Napi::CallbackInfo& info); }; diff --git a/src/cpp/lib/QtGui/QMovie/qmovie_wrap.cpp b/src/cpp/lib/QtGui/QMovie/qmovie_wrap.cpp index 0cb3b2110..28d6b79ff 100644 --- a/src/cpp/lib/QtGui/QMovie/qmovie_wrap.cpp +++ b/src/cpp/lib/QtGui/QMovie/qmovie_wrap.cpp @@ -28,6 +28,7 @@ Napi::Object QMovieWrap::init(Napi::Env env, Napi::Object exports) { InstanceMethod("currentFrameNumber", &QMovieWrap::currentFrameNumber), InstanceMethod("currentPixmap", &QMovieWrap::currentPixmap), InstanceMethod("loadFromData", &QMovieWrap::loadFromData), + InstanceMethod("frameCount", &QMovieWrap::frameCount), QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE(QMovieWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); @@ -57,6 +58,7 @@ QMovieWrap::QMovieWrap(const Napi::CallbackInfo& info) Napi::TypeError::New(env, "Wrong number of arguments") .ThrowAsJavaScriptException(); } + this->bufferDevice = QSharedPointer(new QBuffer); this->rawData = extrautils::configureQObject(this->getInternalInstance()); } @@ -167,8 +169,13 @@ Napi::Value QMovieWrap::loadFromData(const Napi::CallbackInfo& info) { Napi::HandleScope scope(env); Napi::Buffer buffer = info[0].As>(); QByteArray byteArray = QByteArray(buffer.Data(), buffer.Length()); - QBuffer* bufferDevice = new QBuffer(); - bufferDevice->setData(byteArray); - this->instance->setDevice(bufferDevice); + this->bufferDevice->setData(byteArray); + this->instance->setDevice(bufferDevice.data()); return env.Null(); -} \ No newline at end of file +} +Napi::Value QMovieWrap::frameCount(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + int frameCount = this->instance->frameCount(); + return Napi::Value::From(env, frameCount); +} diff --git a/src/lib/QtGui/QMovie.ts b/src/lib/QtGui/QMovie.ts index 89f763da5..f9c25871b 100644 --- a/src/lib/QtGui/QMovie.ts +++ b/src/lib/QtGui/QMovie.ts @@ -74,6 +74,9 @@ export class QMovie extends NodeObject { currentPixmap(): QPixmap { return new QPixmap(this.native.currentPixmap()); } + frameCount(): number { + return this.native.frameCount(); + } } export enum CacheMode { diff --git a/src/lib/core/EventWidget.ts b/src/lib/core/EventWidget.ts index 67e8309a5..3260eaf45 100644 --- a/src/lib/core/EventWidget.ts +++ b/src/lib/core/EventWidget.ts @@ -1,6 +1,11 @@ import { EventEmitter } from 'events'; import { NativeElement, Component, NativeRawPointer } from './Component'; +function addDefaultErrorHandler(native: NativeElement, emitter: EventEmitter): void { + native.subscribeToQtEvent('error'); + emitter.addListener('error', () => null); +} + /** > Abstract class that adds event handling support to all widgets. @@ -37,6 +42,7 @@ export abstract class EventWidget extends Component { } else { throw new Error('initNodeEventEmitter not implemented on native side'); } + addDefaultErrorHandler(native, this.emitter); } /**