add setWidgetSizePolicy, createLabel put Image with !image:

This commit is contained in:
Syping 2020-05-20 22:48:23 +02:00
parent 7449286a20
commit e27dc5b7e7
2 changed files with 62 additions and 22 deletions

View file

@ -107,6 +107,7 @@ void LuaEngineGui::pushClass(lua_State *L_p)
pushFunction(L_p, "setWidgetParent", setWidgetParent);
pushFunction(L_p, "setWidgetReadOnly", setWidgetReadOnly);
pushFunction(L_p, "setWidgetSize", setWidgetSize);
pushFunction(L_p, "setWidgetSizePolicy", setWidgetSizePolicy);
pushFunction(L_p, "setWidgetValue", setWidgetValue);
pushFunction(L_p, "setWidgetVisible", setWidgetVisible);
pushFunction(L_p, "widgetAddText", widgetAddText);
@ -464,25 +465,7 @@ int LuaEngineGui::setObjectImage(lua_State *L_p)
if (getArgumentCount(L_p) >= 2) {
void *pointer = getPointer(L_p, 1);
if (pointer != NULL) {
QString imagePath = getVariant(L_p, 2).toString();
if (QFileInfo(imagePath).isRelative()) {
LuaEngine *engine = (LuaEngine*)engineRegistry->getEngine(L_p);
QString engineType = engine->property("EngineType").toString();
if (engineType == "Runtime") {
QString scriptPath = engine->property("ScriptPath").toString();
QString scriptDirPath = QFileInfo(scriptPath).canonicalPath();
QString newImagePath = QString("%1/%2").arg(scriptDirPath, imagePath);
if (QFile(newImagePath).exists()) {
imagePath = newImagePath;
}
}
else {
QString newImagePath = QString("%1/%2").arg(QApplication::applicationDirPath(), imagePath);
if (QFile(newImagePath).exists()) {
imagePath = newImagePath;
}
}
}
QString imagePath = pathForString(getVariant(L_p, 2).toString(), (LuaEngine*)engineRegistry->getEngine(L_p));
double pixelRatio = 1;
if (getArgumentCount(L_p) >= 3) {
bool pixelRatioOk;
@ -791,6 +774,20 @@ int LuaEngineGui::setWidgetSize(lua_State *L_p)
return 0;
}
int LuaEngineGui::setWidgetSizePolicy(lua_State *L_p)
{
if (getArgumentCount(L_p) >= 3) {
void *pointer = getPointer(L_p, 1);
if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) {
QSizePolicy::Policy horizontalPolicy = (QSizePolicy::Policy)getVariant(L_p, 2).toInt();
QSizePolicy::Policy verticalPolicy = (QSizePolicy::Policy)getVariant(L_p, 3).toInt();
((QWidget*)pointer)->setSizePolicy(horizontalPolicy, verticalPolicy);
return 0;
}
}
return 0;
}
int LuaEngineGui::setWidgetValue(lua_State *L_p)
{
if (getArgumentCount(L_p) >= 2) {
@ -982,16 +979,35 @@ int LuaEngineGui::createLabel(lua_State *L_p)
{
QLayout *layout = nullptr;
QWidget *parent = nullptr;
QString labelText = "LuaEngine";
QString labelInput = "LuaEngine";
if (getArgumentCount(L_p) >= 1) {
labelText = getVariant(L_p, 1).toString();
labelInput = getVariant(L_p, 1).toString();
if (getArgumentCount(L_p) >= 2) {
lpForPointer(getPointer(L_p, 2), &layout, &parent);
}
}
QLabel *label = new QLabel(parent);
label->setObjectName(nameForPointer(label));
label->setText(labelText);
if (labelInput.startsWith("!image:")) {
QString imagePath = pathForString(QString(labelInput).remove(0, 7), (LuaEngine*)engineRegistry->getEngine(L_p));
double pixelRatio = 1;
if (getArgumentCount(L_p) >= 3) {
bool pixelRatioOk;
double newPixelRatio = getVariant(L_p, 3).toDouble(&pixelRatioOk);
if (pixelRatioOk) {
pixelRatio = newPixelRatio;
}
}
QPixmap labelImage(imagePath);
labelImage.setDevicePixelRatio(pixelRatio);
label->setPixmap(labelImage);
}
else {
if (labelInput.startsWith(" !")) {
labelInput = QString(labelInput).remove(0, 1);
}
label->setText(labelInput);
}
if (layout != nullptr) {
layout->addWidget(label);
}
@ -1621,6 +1637,28 @@ QWidget* LuaEngineGui::windowForObject(QObject *object)
return nullptr;
}
QString LuaEngineGui::pathForString(const QString &string, LuaEngine *engine)
{
if (QFileInfo(string).isRelative()) {
QString engineType = engine->property("EngineType").toString();
if (engineType == "Runtime") {
QString scriptPath = engine->property("ScriptPath").toString();
QString scriptDirPath = QFileInfo(scriptPath).canonicalPath();
QString newImagePath = QString("%1/%2").arg(scriptDirPath, string);
if (QFile(newImagePath).exists()) {
return newImagePath;
}
}
else {
QString newImagePath = QString("%1/%2").arg(QApplication::applicationDirPath(), string);
if (QFile(newImagePath).exists()) {
return newImagePath;
}
}
}
return string;
}
void LuaEngineGui::lpForPointer(void *pointer, QLayout **layout, QWidget **parent)
{
if (pointer != NULL) {

View file

@ -53,6 +53,7 @@ public:
static int setWidgetParent(lua_State *L_p);
static int setWidgetReadOnly(lua_State *L_p);
static int setWidgetSize(lua_State *L_p);
static int setWidgetSizePolicy(lua_State *L_p);
static int setWidgetValue(lua_State *L_p);
static int setWidgetVisible(lua_State *L_p);
static int layoutAddLayout(lua_State *L_p);
@ -96,6 +97,7 @@ public:
private:
lua_State *L;
static QWidget* windowForObject(QObject *object);
static QString pathForString(const QString &string, LuaEngine *engine);
static void lpForPointer(void *pointer, QLayout **layout, QWidget **parent);
static QString nameForPointer(void *pointer);
};