Added QSvgWidget (#710)

* feat: added QSvgWidget

* docs: fix sidebars syntax
This commit is contained in:
Ruslan Garifullin 2020-10-18 17:37:52 +03:00 committed by GitHub
parent f1725b991e
commit 0b5ee00b58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 1399 additions and 3 deletions

View File

@ -137,6 +137,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QTextEdit/qtextedit_wrap.cpp"
"${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"
# 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"
@ -198,6 +199,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QStandardItemModel/nstandarditemmodel.hpp"
"${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"
)
AddCommonConfig(${CORE_WIDGETS_ADDON})
@ -229,5 +231,6 @@ target_link_libraries(${CORE_WIDGETS_ADDON} PRIVATE
Qt5::Widgets
Qt5::Core
Qt5::Gui
Qt5::Svg
)

View File

@ -20,6 +20,6 @@ macro(AddQtSupport addonName)
message(STATUS "Using QT installation for ${addonName} QT_CMAKE_HOME_DIR:${QT_CMAKE_HOME_DIR}")
set(Qt5_DIR ${QT_CMAKE_HOME_DIR})
find_package(Qt5 COMPONENTS Widgets Gui Core REQUIRED)
find_package(Qt5 COMPONENTS Widgets Gui Core Svg REQUIRED)
endmacro(AddQtSupport addonName)

View File

@ -21,6 +21,11 @@ function getMiniQtConfig() {
link: `https://download.qt.io/online/qtsdkrepository/mac_x64/desktop/qt5_5141/qt.qt5.5141.clang_64/5.14.1-0-202001241000qtbase-MacOS-MacOS_10_13-Clang-MacOS-MacOS_10_13-X86_64.7z`,
skipSetup: checkIfExists(path.resolve(qtHome, 'plugins', 'platforms', 'libqcocoa.dylib')),
},
{
name: 'Qt Svg',
link: `https://download.qt.io/online/qtsdkrepository/mac_x64/desktop/qt5_5141/qt.qt5.5141.clang_64/5.14.1-0-202001241000qtsvg-MacOS-MacOS_10_13-Clang-MacOS-MacOS_10_13-X86_64.7z`,
skipSetup: checkIfExists(path.resolve(qtHome, 'lib', 'QtSvg.framework', 'QtSvg')),
},
{
name: 'Qt Tools',
link: `https://download.qt.io/online/qtsdkrepository/mac_x64/desktop/qt5_5141/qt.qt5.5141.clang_64/5.14.1-0-202001241000qttools-MacOS-MacOS_10_13-Clang-MacOS-MacOS_10_13-X86_64.7z`,
@ -39,6 +44,11 @@ function getMiniQtConfig() {
link: `https://download.qt.io/online/qtsdkrepository/windows_x86/desktop/qt5_5141/qt.qt5.5141.win64_msvc2017_64/5.14.1-0-202001240957qtbase-Windows-Windows_10-MSVC2017-Windows-Windows_10-X86_64.7z`,
skipSetup: checkIfExists(path.resolve(qtHome, 'bin', 'Qt5Core.dll')),
},
{
name: 'Qt SVG',
link: `https://download.qt.io/online/qtsdkrepository/windows_x86/desktop/qt5_5141/qt.qt5.5141.win64_msvc2017_64/5.14.1-0-202001240957qtsvg-Windows-Windows_10-MSVC2017-Windows-Windows_10-X86_64.7z`,
skipSetup: checkIfExists(path.resolve(qtHome, 'bin', 'Qt5Svg.dll')),
},
{
name: 'Qt Tools',
link: `https://download.qt.io/online/qtsdkrepository/windows_x86/desktop/qt5_5141/qt.qt5.5141.win64_msvc2017_64/5.14.1-0-202001240957qttools-Windows-Windows_10-MSVC2017-Windows-Windows_10-X86_64.7z`,
@ -57,6 +67,11 @@ function getMiniQtConfig() {
link: `https://download.qt.io/online/qtsdkrepository/linux_x64/desktop/qt5_5141/qt.qt5.5141.gcc_64/5.14.1-0-202001240953qtbase-Linux-RHEL_7_6-GCC-Linux-RHEL_7_6-X86_64.7z`,
skipSetup: checkIfExists(path.resolve(qtHome, 'bin', 'moc')),
},
{
name: 'Qt SVG',
link: `https://download.qt.io/online/qtsdkrepository/linux_x64/desktop/qt5_5141/qt.qt5.5141.gcc_64/5.14.1-0-202001240953qtsvg-Linux-RHEL_7_6-GCC-Linux-RHEL_7_6-X86_64.7z`,
skipSetup: checkIfExists(path.resolve(qtHome, 'lib', 'libQt5Svg.so')),
},
{
name: 'Qt ICU',
link: `https://download.qt.io/online/qtsdkrepository/linux_x64/desktop/qt5_5141/qt.qt5.5141.gcc_64/5.14.1-0-202001240953icu-linux-Rhel7.2-x64.7z`,

View File

@ -0,0 +1,17 @@
#pragma once
#include <QSvgWidget>
#include "Extras/Export/export.h"
#include "QtWidgets/QWidget/qwidget_macro.h"
#include "core/NodeWidget/nodewidget.h"
#include "napi.h"
class DLL_EXPORT NSvgWidget : public QSvgWidget, public NodeWidget {
Q_OBJECT
NODEWIDGET_IMPLEMENTATIONS(QSvgWidget)
public:
using QSvgWidget::QSvgWidget; // inherit all constructors of QSvgWidget
void connectSignalsToEventEmitter() { QWIDGET_SIGNALS }
};

View File

@ -0,0 +1,25 @@
#pragma once
#include <napi.h>
#include <QPointer>
#include "Extras/Export/export.h"
#include "QtWidgets/QWidget/qwidget_macro.h"
#include "nsvgwidget.hpp"
class DLL_EXPORT QSvgWidgetWrap : public Napi::ObjectWrap<QSvgWidgetWrap> {
QWIDGET_WRAPPED_METHODS_DECLARATION
private:
QPointer<NSvgWidget> instance;
public:
static Napi::Object init(Napi::Env env, Napi::Object exports);
QSvgWidgetWrap(const Napi::CallbackInfo& info);
~QSvgWidgetWrap();
NSvgWidget* getInternalInstance();
// class constructor
static Napi::FunctionReference constructor;
// wrapped methods
Napi::Value load(const Napi::CallbackInfo& info);
};

View File

@ -0,0 +1,63 @@
#include "QtWidgets/QSvgWidget/qsvgwidget_wrap.h"
#include <QWidget>
#include "Extras/Utils/nutils.h"
#include "QtWidgets/QWidget/qwidget_wrap.h"
Napi::FunctionReference QSvgWidgetWrap::constructor;
Napi::Object QSvgWidgetWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "QSvgWidget";
Napi::Function func =
DefineClass(env, CLASSNAME,
{InstanceMethod("load", &QSvgWidgetWrap::load),
QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QSvgWidgetWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
NSvgWidget* QSvgWidgetWrap::getInternalInstance() { return this->instance; }
QSvgWidgetWrap::~QSvgWidgetWrap() { extrautils::safeDelete(this->instance); }
QSvgWidgetWrap::QSvgWidgetWrap(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<QSvgWidgetWrap>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() == 1) {
Napi::Object parentObject = info[0].As<Napi::Object>();
NodeWidgetWrap* parentWidgetWrap =
Napi::ObjectWrap<NodeWidgetWrap>::Unwrap(parentObject);
this->instance = new NSvgWidget(parentWidgetWrap->getInternalInstance());
} else if (info.Length() == 0) {
this->instance = new NSvgWidget();
} else {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = extrautils::configureQWidget(
this->getInternalInstance(), this->getInternalInstance()->getFlexNode(),
true);
}
Napi::Value QSvgWidgetWrap::load(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Value value = info[0];
std::string napis = value.As<Napi::String>().Utf8Value();
QString s = QString::fromUtf8(napis.c_str());
QString svgPrefix = "data:image/svg+xml;base64,";
if (s.startsWith(svgPrefix)) {
QString base64 = s.replace(svgPrefix, QString(""));
QByteArray svg =
QByteArray::fromBase64(s.toUtf8(), QByteArray::Base64Encoding);
this->instance->load(svg);
} else {
this->instance->load(s);
}
return env.Null();
}

View File

@ -86,6 +86,7 @@
#include "QtWidgets/QStandardItem/qstandarditem_wrap.h"
#include "QtWidgets/QStandardItemModel/qstandarditemmodel_wrap.h"
#include "QtWidgets/QStatusBar/qstatusbar_wrap.h"
#include "QtWidgets/QSvgWidget/qsvgwidget_wrap.h"
#include "QtWidgets/QSystemTrayIcon/qsystemtrayicon_wrap.h"
#include "QtWidgets/QTabBar/qtabbar_wrap.h"
#include "QtWidgets/QTabWidget/qtabwidget_wrap.h"
@ -208,6 +209,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
QSettingsWrap::init(env, exports);
QStandardItemModelWrap::init(env, exports);
QStandardItemWrap::init(env, exports);
QSvgWidgetWrap::init(env, exports);
return exports;
}

View File

@ -99,6 +99,7 @@ export { QPainterPath } from './lib/QtWidgets/QPainterPath';
export { QDialog, QDialogSignals } from './lib/QtWidgets/QDialog';
export { QMessageBox, QMessageBoxSignals, QMessageBoxIcon, ButtonRole } from './lib/QtWidgets/QMessageBox';
export { QInputDialog, QInputDialogSignals, InputDialogOptions, InputMode } from './lib/QtWidgets/QInputDialog';
export { QSvgWidget } from './lib/QtWidgets/QSvgWidget';
export { QButtonGroup, QButtonGroupSignals } from './lib/QtWidgets/QButtonGroup';

View File

@ -0,0 +1,51 @@
import addon from '../utils/addon';
import { NodeWidget, QWidgetSignals } from './QWidget';
import { NativeElement } from '../core/Component';
/**
> Display SVG files in a widget.
* **This class is a JS wrapper around Qt's [QSvgWidget class](https://doc.qt.io/qt-5/qsvgwidget.html)**
The `QSvgWidget` class provides a widget that is used to display the contents of Scalable Vector Graphics (SVG) files.
### Example
```javascript
const fs = require("fs");
const { QSvgWidget } = require("@nodegui/nodegui");
const svg = new QSvgWidget();
svg.load("icon.svg");
// or
fs.readFile("icon.svg", (err, buffer) => {
if (!err) svg.load(buffer);
})
```
*/
export class QSvgWidget extends NodeWidget<QWidgetSignals> {
native: NativeElement;
constructor();
constructor(parent: NodeWidget<any>);
constructor(parent?: NodeWidget<any>) {
let native;
if (parent) {
native = new addon.QSvgWidget(parent.native);
} else {
native = new addon.QSvgWidget();
}
super(native);
this.native = native;
parent && this.setNodeParent(parent);
}
load(file: string | Buffer): void {
if (file instanceof Buffer) {
this.native.load('data:image/svg+xml;base64,' + file.toString('base64'));
} else {
this.native.load(file);
}
}
}

View File

@ -8,7 +8,7 @@ sidebar_label: "NodeWidget"
*This class implements all methods, properties of Qt's [QWidget class](https://doc.qt.io/qt-5/qwidget.html) so that it can be inherited by all widgets**
`NodeWidget` is an abstract class and hence no instances of the same should be created. It exists so that we can add similar functionalities to all widget's easily. Additionally it helps in type checking process. If you wish to create a `div` like widget use [QWidget](qwidget.md) instead.
`NodeWidget` is an abstract class and hence no instances of the same should be created. It exists so that we can add similar functionalities to all widget's easily. Additionally it helps in type checking process. If you wish to create a `div` like widget use [QWidget](QWidget.md) instead.
*NodeWidget is the base class for all widgets.**
@ -78,6 +78,8 @@ Implement all native QWidget methods here so that all widgets get access to thos
↳ [QTabWidget](qtabwidget.md)
↳ [QSvgWidget](qsvgwidget.md)
↳ [QGroupBox](qgroupbox.md)
## Index

View File

@ -8,7 +8,7 @@ sidebar_label: "QAbstractScrollArea"
*This class implements all methods, properties of Qt's [QAbstractScrollArea class](https://doc.qt.io/qt-5/qabstractscrollarea.html) so that it can be inherited by all scroll based widgets**
`QAbstractScrollArea` is an abstract class and hence no instances of the same should be created. It exists so that we can add similar functionalities to all scrollable widget's easily. If you wish to create a scrollarea use [QScrollArea](qscrollarea.md) instead.
`QAbstractScrollArea` is an abstract class and hence no instances of the same should be created. It exists so that we can add similar functionalities to all scrollable widget's easily. If you wish to create a scrollarea use [QScrollArea](QScrollArea.md) instead.
*QAbstractScrollArea is the base class for all widgets.**

File diff suppressed because it is too large Load Diff

View File

@ -265,6 +265,7 @@ sidebar_label: "Globals"
* [QStandardItemModel](classes/qstandarditemmodel.md)
* [QStatusBar](classes/qstatusbar.md)
* [QStyle](classes/qstyle.md)
* [QSvgWidget](classes/qsvgwidget.md)
* [QSystemTrayIcon](classes/qsystemtrayicon.md)
* [QTabBar](classes/qtabbar.md)
* [QTabWidget](classes/qtabwidget.md)

View File

@ -99,6 +99,7 @@ module.exports = {
'api/generated/classes/qstandarditemmodel',
'api/generated/classes/qstatusbar',
'api/generated/classes/qstyle',
'api/generated/classes/qsvgwidget',
'api/generated/classes/qsystemtrayicon',
'api/generated/classes/qtabbar',
'api/generated/classes/qtabwidget',