Adds text measuring support for button and label
This commit is contained in:
@@ -1,8 +1,26 @@
|
||||
#include "utils.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <QWidget>
|
||||
#include "deps/spdlog/spdlog.h"
|
||||
|
||||
void extrautils::noop(){}
|
||||
|
||||
void extrautils::throwTypeError(Napi::Env env, std::string errorMessage){
|
||||
Napi::TypeError::New(env, errorMessage.c_str()).ThrowAsJavaScriptException();
|
||||
}
|
||||
|
||||
YGSize extrautils::measureQtWidget (YGNodeRef node, float width, YGMeasureMode widthMode, float height, YGMeasureMode heightMode){
|
||||
FlexLayout::NodeContext *ctx = FlexLayout::getNodeContext(node);
|
||||
if(ctx){
|
||||
QLayoutItem* childLayoutItem = ctx->item;
|
||||
QWidget* widget = childLayoutItem->widget();
|
||||
if(widget){
|
||||
QSize size = widget->sizeHint();
|
||||
return YGSize{
|
||||
.height = static_cast<float>(size.height()),
|
||||
.width = static_cast<float>(size.width())
|
||||
};
|
||||
}
|
||||
}
|
||||
return YGSize{ .height = 0, .width = 0 };
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
#ifndef EXTRAUTILS_WRAP_H
|
||||
#define EXTRAUTILS_WRAP_H
|
||||
#pragma once
|
||||
|
||||
#include <napi.h>
|
||||
#include "src/cpp/core/FlexLayout/flexlayout.h"
|
||||
|
||||
namespace extrautils {
|
||||
void noop();
|
||||
void throwTypeError(Napi::Env env, std::string errorMessage);
|
||||
YGSize measureQtWidget (YGNodeRef node, float width, YGMeasureMode widthMode, float height, YGMeasureMode heightMode);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -39,6 +39,8 @@ QLabelWrap::QLabelWrap(const Napi::CallbackInfo& info): Napi::ObjectWrap<QLabelW
|
||||
}else {
|
||||
extrautils::throwTypeError(env, "Wrong number of arguments");
|
||||
}
|
||||
// Adds measure function on yoga node so that widget size is calculated based on its text also.
|
||||
YGNodeSetMeasureFunc(this->instance->getFlexNode(), &extrautils::measureQtWidget);
|
||||
}
|
||||
|
||||
QLabelWrap::~QLabelWrap() {
|
||||
|
||||
@@ -21,7 +21,7 @@ class QLabelWrap : public Napi::ObjectWrap<QLabelWrap>{
|
||||
Napi::Value text(const Napi::CallbackInfo &info);
|
||||
|
||||
QWIDGET_WRAPPED_METHODS_DECLARATION
|
||||
YOGAWIDGET_WRAPPED_METHODS_DECLARATION
|
||||
YOGAWIDGET_WRAPPED_METHODS_DECLARATION
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -35,6 +35,8 @@ QPushButtonWrap::QPushButtonWrap(const Napi::CallbackInfo& info): Napi::ObjectWr
|
||||
}else {
|
||||
extrautils::throwTypeError(env, "Wrong number of arguments");
|
||||
}
|
||||
// Adds measure function on yoga node so that widget size is calculated based on its text also.
|
||||
YGNodeSetMeasureFunc(this->instance->getFlexNode(), &extrautils::measureQtWidget);
|
||||
}
|
||||
|
||||
QPushButtonWrap::~QPushButtonWrap() {
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "npushbutton.h"
|
||||
#include "src/cpp/QtGui/QWidget/qwidget_macro.h"
|
||||
#include "src/cpp/core/YogaWidget/yogawidget_macro.h"
|
||||
#include "src/cpp/Extras/Utils/utils.h"
|
||||
|
||||
class QPushButtonWrap : public Napi::ObjectWrap<QPushButtonWrap> {
|
||||
private:
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <QWidget>
|
||||
#include "spdlog/spdlog.h"
|
||||
|
||||
FlexLayout::NodeContext *FlexLayout::getNodeContext(YGNodeRef node) const
|
||||
FlexLayout::NodeContext *FlexLayout::getNodeContext(YGNodeRef node)
|
||||
{
|
||||
if(!node){
|
||||
return nullptr;
|
||||
@@ -122,6 +122,7 @@ void FlexLayout::setGeometry(const QRect &rect)
|
||||
QWidget* childWidget = childLayoutItem->widget();
|
||||
if(childWidget){
|
||||
childWidget->setGeometry(childRect);
|
||||
spdlog::info("Object: {}, rect: w:{}, h:{}, l:{}, t:{}",childWidget->metaObject()->className(),width,height,left,top);
|
||||
}else {
|
||||
childLayoutItem->setGeometry(childRect);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#ifndef FLEXLAYOUT_H
|
||||
#define FLEXLAYOUT_H
|
||||
#pragma once
|
||||
|
||||
#include "deps/yoga/YGNode.h"
|
||||
#include <QLayout>
|
||||
@@ -25,6 +24,7 @@ class FlexLayout: public QLayout
|
||||
{
|
||||
private:
|
||||
YGNodeRef node;
|
||||
public:
|
||||
struct NodeContext
|
||||
{
|
||||
NodeContext(QLayoutItem *i) {
|
||||
@@ -32,9 +32,6 @@ private:
|
||||
}
|
||||
QLayoutItem *item;
|
||||
};
|
||||
NodeContext* getNodeContext(YGNodeRef node) const;
|
||||
|
||||
public:
|
||||
FlexLayout(QWidget* parentWidget=nullptr, YGNodeRef parentNode=nullptr);
|
||||
~FlexLayout() override;
|
||||
QSize sizeHint() const override;
|
||||
@@ -45,6 +42,6 @@ public:
|
||||
void addWidget(QWidget* childWidget, YGNodeRef childNode);
|
||||
void setGeometry(const QRect &rect) override;
|
||||
void setFlexNode(YGNodeRef parentNode);
|
||||
static NodeContext* getNodeContext(YGNodeRef node);
|
||||
};
|
||||
|
||||
#endif // FLEXLAYOUT_H
|
||||
|
||||
Reference in New Issue
Block a user