add getWidgetPixelRatio and setObjectImage

This commit is contained in:
Syping 2020-05-18 01:07:26 +02:00
parent 5343818dd4
commit ca82f46e5f
4 changed files with 96 additions and 4 deletions

View file

@ -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)) {

View file

@ -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;

View file

@ -15,6 +15,7 @@
* limitations under the License.
*****************************************************************************/
#include "LuaEngineRegistry.h"
#include "LuaEngineGui.h"
#include <QPlainTextEdit>
#include <QStackedWidget>
@ -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) {

View file

@ -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);