From ca82f46e5f04beebc25aee7dbd4d210c57fb506a Mon Sep 17 00:00:00 2001 From: Syping Date: Mon, 18 May 2020 01:07:26 +0200 Subject: [PATCH] add getWidgetPixelRatio and setObjectImage --- src/luaengine/main.cpp | 5 +- src/luaengineapp/main.cpp | 1 + src/luaenginegui/luaengine/LuaEngineGui.cpp | 92 ++++++++++++++++++++- src/luaenginegui/luaengine/LuaEngineGui.h | 2 + 4 files changed, 96 insertions(+), 4 deletions(-) diff --git a/src/luaengine/main.cpp b/src/luaengine/main.cpp index 2a54682..bb84d51 100644 --- a/src/luaengine/main.cpp +++ b/src/luaengine/main.cpp @@ -45,7 +45,8 @@ int main(int argc, char *argv[]) if (arguments.length() >= 2) { QVariant rtPath = arguments.first(); arguments.removeFirst(); - QFile luaScript(arguments.first().toString()); + QVariant scriptPath = arguments.first(); + QFile luaScript(scriptPath.toString()); if (!luaScript.open(QIODevice::ReadOnly)) { QTextStream(stderr) << "Error: Failed to open \"" << arguments.first().toString() << "\"." << endl; return 1; @@ -62,6 +63,8 @@ int main(int argc, char *argv[]) LuaEngineIO::pushClass(&luaEngineGui); LuaEngineOS::pushClass(&luaEngineGui); luaEngineGui.pushVariant("_LuaEngineRT", rtPath); + luaEngineGui.setProperty("EngineType", "Runtime"); + luaEngineGui.setProperty("ScriptPath", scriptPath); luaEngineGui.executeLuaScript(luaScript.readAll()); if (luaEngineGui.executeLuaFunction("main", arguments, true)) { diff --git a/src/luaengineapp/main.cpp b/src/luaengineapp/main.cpp index e5c0dac..ef699c1 100644 --- a/src/luaengineapp/main.cpp +++ b/src/luaengineapp/main.cpp @@ -77,6 +77,7 @@ int main(int argc, char *argv[]) LuaEngineGui luaEngineGui; LuaEngineIO::pushClass(&luaEngineGui); LuaEngineOS::pushClass(&luaEngineGui); + luaEngineGui.setProperty("EngineType", "Portable"); luaEngineGui.executeLuaScript(luaScript); QVariantList arguments; diff --git a/src/luaenginegui/luaengine/LuaEngineGui.cpp b/src/luaenginegui/luaengine/LuaEngineGui.cpp index 2a3f53b..2e9e6bd 100644 --- a/src/luaenginegui/luaengine/LuaEngineGui.cpp +++ b/src/luaenginegui/luaengine/LuaEngineGui.cpp @@ -15,6 +15,7 @@ * limitations under the License. *****************************************************************************/ +#include "LuaEngineRegistry.h" #include "LuaEngineGui.h" #include #include @@ -79,6 +80,10 @@ void LuaEngineGui::pushClass(lua_State *L_p) // Gui pushVariant(L_p, "GuiExecuted", "GuiExecuted"); + // Object + pushFunction(L_p, "getWindow", getWindow); + pushFunction(L_p, "setObjectImage", setObjectImage); + // Widget pushFunction(L_p, "closeWidget", closeWidget); pushFunction(L_p, "executeWidget", executeWidget); @@ -86,6 +91,7 @@ void LuaEngineGui::pushClass(lua_State *L_p) pushFunction(L_p, "isWidgetChecked", isWidgetChecked); pushFunction(L_p, "isWidgetEnabled", isWidgetEnabled); pushFunction(L_p, "isWidgetVisible", isWidgetVisible); + pushFunction(L_p, "getWidgetPixelRatio", getWidgetPixelRatio); pushFunction(L_p, "getWidgetText", getWidgetText); pushFunction(L_p, "setWidgetChecked", setWidgetChecked); pushFunction(L_p, "setWidgetEnabled", setWidgetEnabled); @@ -178,9 +184,6 @@ void LuaEngineGui::pushClass(lua_State *L_p) pushVariant(L_p, "SizePolicyPreferred", (int)QSizePolicy::Preferred); pushVariant(L_p, "SizePolicyExpanding", (int)QSizePolicy::Expanding); pushVariant(L_p, "SizePolicyIgnored", (int)QSizePolicy::Ignored); - - // Object - pushFunction(L_p, "getWindow", getWindow); } void LuaEngineGui::pushClass(LuaEngine *luaEngine) @@ -426,6 +429,77 @@ int LuaEngineGui::setMenuShortcut(lua_State *L_p) return 0; } +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; + } + } + } + double pixelRatio = 1; + if (getArgumentCount(L_p) >= 3) { + bool pixelRatioOk; + double newPixelRatio = getVariant(L_p, 3).toDouble(&pixelRatioOk); + if (pixelRatioOk) { + pixelRatio = newPixelRatio; + } + } + if (((QObject*)pointer)->inherits("QCheckBox")) { + QPixmap objectImage(imagePath); + objectImage.setDevicePixelRatio(pixelRatio); + QIcon objectIcon; + objectIcon.addPixmap(objectImage); + ((QCheckBox*)pointer)->setIcon(objectIcon); + } + else if (((QObject*)pointer)->inherits("QLabel")) { + QPixmap objectImage(imagePath); + objectImage.setDevicePixelRatio(pixelRatio); + ((QLabel*)pointer)->setPixmap(objectImage); + } + else if (((QObject*)pointer)->inherits("QPushButton")) { + QPixmap objectImage(imagePath); + objectImage.setDevicePixelRatio(pixelRatio); + QIcon objectIcon; + objectIcon.addPixmap(objectImage); + ((QPushButton*)pointer)->setIcon(objectIcon); + } + else if (((QObject*)pointer)->inherits("QRadioButton")) { + QPixmap objectImage(imagePath); + objectImage.setDevicePixelRatio(pixelRatio); + QIcon objectIcon; + objectIcon.addPixmap(objectImage); + ((QRadioButton*)pointer)->setIcon(objectIcon); + } + else if (((QObject*)pointer)->inherits("QToolButton")) { + QPixmap objectImage(imagePath); + objectImage.setDevicePixelRatio(pixelRatio); + QIcon objectIcon; + objectIcon.addPixmap(objectImage); + ((QToolButton*)pointer)->setIcon(objectIcon); + } + return 0; + } + } + return 0; +} + int LuaEngineGui::setWidgetChecked(lua_State *L_p) { if (getArgumentCount(L_p) >= 1) { @@ -1279,6 +1353,18 @@ int LuaEngineGui::getWindow(lua_State *L_p) return 0; } +int LuaEngineGui::getWidgetPixelRatio(lua_State *L_p) +{ + if (getArgumentCount(L_p) >= 1) { + void *pointer = getPointer(L_p, 1); + if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) { + pushVariant(L_p, ((QWidget*)pointer)->devicePixelRatioF()); + return 1; + } + } + return 0; +} + int LuaEngineGui::getWidgetText(lua_State *L_p) { if (getArgumentCount(L_p) >= 1) { diff --git a/src/luaenginegui/luaengine/LuaEngineGui.h b/src/luaenginegui/luaengine/LuaEngineGui.h index f8fba00..837ea90 100644 --- a/src/luaenginegui/luaengine/LuaEngineGui.h +++ b/src/luaenginegui/luaengine/LuaEngineGui.h @@ -40,6 +40,7 @@ public: static int showWidget(lua_State *L_p); static int setLayoutMargins(lua_State *L_p); static int setMenuShortcut(lua_State *L_p); + static int setObjectImage(lua_State *L_p); static int setWidgetChecked(lua_State *L_p); static int setWidgetEnabled(lua_State *L_p); static int setWidgetFixed(lua_State *L_p); @@ -82,6 +83,7 @@ public: static int isWidgetChecked(lua_State *L_p); static int isWidgetEnabled(lua_State *L_p); static int isWidgetVisible(lua_State *L_p); + static int getWidgetPixelRatio(lua_State *L_p); static int getWidgetText(lua_State *L_p); static int getWindow(lua_State *L_p);