From fcabd1685ddf5123180d88356489107106e52730 Mon Sep 17 00:00:00 2001 From: Syping Date: Tue, 19 May 2020 20:50:03 +0200 Subject: [PATCH] + createListItem, createListView, getObjectParent, LEListWidgetItem, setParent -> setObjectParent --- src/luaenginecore/luaengine/LuaEngine.cpp | 23 ++- src/luaenginecore/luaengine/LuaEngine.h | 3 +- .../luaengine/LEListWidgetItem.cpp | 38 +++++ src/luaenginegui/luaengine/LEListWidgetItem.h | 45 ++++++ src/luaenginegui/luaengine/LuaEngineGui.cpp | 133 ++++++++++++++---- src/luaenginegui/luaengine/LuaEngineGui.h | 5 +- src/luaenginegui/luaenginegui.pro | 2 + 7 files changed, 216 insertions(+), 33 deletions(-) create mode 100644 src/luaenginegui/luaengine/LEListWidgetItem.cpp create mode 100644 src/luaenginegui/luaengine/LEListWidgetItem.h diff --git a/src/luaenginecore/luaengine/LuaEngine.cpp b/src/luaenginecore/luaengine/LuaEngine.cpp index 0a68f4e..1c9d989 100644 --- a/src/luaenginecore/luaengine/LuaEngine.cpp +++ b/src/luaenginecore/luaengine/LuaEngine.cpp @@ -31,9 +31,10 @@ LuaEngine::LuaEngine(QObject *parent, bool loadBaseLibraries) : QObject(parent) pushVariant("DeleteInstant", 0); pushVariant("DeleteLater", 1); pushFunction("delete", luaObjectDelete_p); - pushFunction("getParent", luaObjectParent_p); pushFunction("connect", luaTriggerConnect_p); pushFunction("disconnect", luaTriggerDisconnect_p); + pushFunction("getObjectParent", luaObjectGetParent_p); + pushFunction("setObjectParent", luaObjectSetParent_p); pushFunction("luaEngineVersion", luaEngineVersion_p); pushFunction("luaEnginePlatform", luaEnginePlatform_p); } @@ -427,7 +428,7 @@ int LuaEngine::luaObjectDelete_p(lua_State *L_p) return 0; } -int LuaEngine::luaObjectParent_p(lua_State *L_p) +int LuaEngine::luaObjectGetParent_p(lua_State *L_p) { if (getArgumentCount(L_p) >= 1) { void *pointer = getPointer(L_p, 1); @@ -439,6 +440,18 @@ int LuaEngine::luaObjectParent_p(lua_State *L_p) return 0; } +int LuaEngine::luaObjectSetParent_p(lua_State *L_p) +{ + if (getArgumentCount(L_p) >= 2) { + void *o_pointer = getPointer(L_p, 1); + void *p_pointer = getPointer(L_p, 2); + if (o_pointer != NULL && p_pointer != NULL && ((QObject*)o_pointer)->inherits("QObject") && ((QObject*)p_pointer)->inherits("QObject")) { + ((QObject*)o_pointer)->setParent((QObject*)p_pointer); + } + } + return 0; +} + int LuaEngine::luaTriggerConnect_p(lua_State *L_p) { if (getArgumentCount(L_p) >= 3) { @@ -455,7 +468,7 @@ int LuaEngine::luaTriggerConnect_p(lua_State *L_p) QMetaMethod slot = engine->metaObject()->method(slotIndex); QString funcStorage; QTextStream(&funcStorage) << "__ConnectFunc_" << object << "_" << signal.name(); - pushVariant(L_p, funcStorage.toUtf8().data(), getVariant(L_p, 3).toString()); + engine->setProperty(funcStorage.toUtf8().data(), getVariant(L_p, 3)); QObject::connect(object, signal, engine, slot); } } @@ -480,7 +493,7 @@ int LuaEngine::luaTriggerDisconnect_p(lua_State *L_p) QMetaMethod slot = engine->metaObject()->method(slotIndex); QString funcStorage; QTextStream(&funcStorage) << "__ConnectFunc_" << object << "_" << signal.name(); - pushVariant(L_p, funcStorage.toUtf8().data(), QVariant()); + engine->setProperty(funcStorage.toUtf8().data(), QVariant()); QObject::disconnect(object, signal, engine, slot); } } @@ -494,6 +507,6 @@ void LuaEngine::luaTriggerSlot_p() QMetaMethod signal = sender()->metaObject()->method(senderSignalIndex()); QString funcStorage; QTextStream(&funcStorage) << "__ConnectFunc_" << sender() << "_" << signal.name(); - QString luaConnectFunc = getVariant(funcStorage.toUtf8().data()).toString(); + QString luaConnectFunc = property(funcStorage.toUtf8().data()).toString(); executeLuaFunction(luaConnectFunc.toUtf8().data(), QVariant::fromValue((void*)sender())); } diff --git a/src/luaenginecore/luaengine/LuaEngine.h b/src/luaenginecore/luaengine/LuaEngine.h index 51bf964..cbf15de 100644 --- a/src/luaenginecore/luaengine/LuaEngine.h +++ b/src/luaenginecore/luaengine/LuaEngine.h @@ -82,7 +82,8 @@ private: static int luaEngineVersion_p(lua_State *L_p); static int luaEnginePlatform_p(lua_State *L_p); static int luaObjectDelete_p(lua_State *L_p); - static int luaObjectParent_p(lua_State *L_p); + static int luaObjectGetParent_p(lua_State *L_p); + static int luaObjectSetParent_p(lua_State *L_p); static int luaTriggerConnect_p(lua_State *L_p); static int luaTriggerDisconnect_p(lua_State *L_p); diff --git a/src/luaenginegui/luaengine/LEListWidgetItem.cpp b/src/luaenginegui/luaengine/LEListWidgetItem.cpp new file mode 100644 index 0000000..b8ecf54 --- /dev/null +++ b/src/luaenginegui/luaengine/LEListWidgetItem.cpp @@ -0,0 +1,38 @@ +/***************************************************************************** +* luaEngine Lua Engine for Qt +* Copyright (C) 2020 Syping +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*****************************************************************************/ + +#include "LEListWidgetItem.h" + +LEListWidgetItem::LEListWidgetItem(const QString &text, QListWidget *parent) +{ + item_p.setText(text); + parent->addItem(&item_p); + QObject::connect(parent, &QListWidget::currentItemChanged, this, &LEListWidgetItem::listWidgetItemChanged); +} + +QListWidgetItem* LEListWidgetItem::item() +{ + return &item_p; +} + +void LEListWidgetItem::listWidgetItemChanged(QListWidgetItem *current, QListWidgetItem *previous) +{ + Q_UNUSED(previous) + if (current == &item_p) { + emit selected(); + } +} diff --git a/src/luaenginegui/luaengine/LEListWidgetItem.h b/src/luaenginegui/luaengine/LEListWidgetItem.h new file mode 100644 index 0000000..39a7d63 --- /dev/null +++ b/src/luaenginegui/luaengine/LEListWidgetItem.h @@ -0,0 +1,45 @@ +/***************************************************************************** +* luaEngine Lua Engine for Qt +* Copyright (C) 2020 Syping +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*****************************************************************************/ + +#ifndef LELISTWIDGETITEM_H +#define LELISTWIDGETITEM_H + +#include "LuaEngineGui_global.h" +#include "LuaEngine.h" +#include +#include +#include +#include + +class LUAENGINEGUISHARED_EXPORT LEListWidgetItem : public QObject +{ + Q_OBJECT +public: + LEListWidgetItem(const QString &text, QListWidget *parent = nullptr); + QListWidgetItem* item(); + +private slots: + void listWidgetItemChanged(QListWidgetItem *current, QListWidgetItem *previous); + +private: + QListWidgetItem item_p; + +signals: + void selected(); +}; + +#endif // LELISTWIDGETITEM_H diff --git a/src/luaenginegui/luaengine/LuaEngineGui.cpp b/src/luaenginegui/luaengine/LuaEngineGui.cpp index 2eddae4..e4e842d 100644 --- a/src/luaenginegui/luaengine/LuaEngineGui.cpp +++ b/src/luaenginegui/luaengine/LuaEngineGui.cpp @@ -1,6 +1,6 @@ /***************************************************************************** * luaEngine Lua Engine for Qt -* Copyright (C) 2018-2019 Syping +* Copyright (C) 2018-2020 Syping * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ *****************************************************************************/ #include "LuaEngineRegistry.h" +#include "LEListWidgetItem.h" #include "LuaEngineGui.h" #include #include @@ -34,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -102,6 +104,7 @@ void LuaEngineGui::pushClass(lua_State *L_p) pushFunction(L_p, "setWidgetLayout", setWidgetLayout); pushFunction(L_p, "setWidgetMaximum", setWidgetMaximum); pushFunction(L_p, "setWidgetMinimum", setWidgetMinimum); + pushFunction(L_p, "setWidgetParent", setWidgetParent); pushFunction(L_p, "setWidgetReadOnly", setWidgetReadOnly); pushFunction(L_p, "setWidgetSize", setWidgetSize); pushFunction(L_p, "setWidgetValue", setWidgetValue); @@ -137,6 +140,10 @@ void LuaEngineGui::pushClass(lua_State *L_p) // Line Edit pushFunction(L_p, "createLineEdit", createLineEdit); + // List View + pushFunction(L_p, "createListItem", createListItem); + pushFunction(L_p, "createListView", createListView); + // Main Window pushFunction(L_p, "createMainWindow", createMainWindow); pushFunction(L_p, "createCentralWidget", createCentralWidget); @@ -484,7 +491,14 @@ int LuaEngineGui::setObjectImage(lua_State *L_p) pixelRatio = newPixelRatio; } } - if (((QObject*)pointer)->inherits("QAction")) { + if (((QObject*)pointer)->inherits("LEListWidgetItem")) { + QPixmap objectImage(imagePath); + objectImage.setDevicePixelRatio(pixelRatio); + QIcon objectIcon; + objectIcon.addPixmap(objectImage); + ((LEListWidgetItem*)pointer)->item()->setIcon(objectIcon); + } + else if (((QObject*)pointer)->inherits("QAction")) { QPixmap objectImage(imagePath); objectImage.setDevicePixelRatio(pixelRatio); QIcon objectIcon; @@ -535,7 +549,10 @@ int LuaEngineGui::setObjectText(lua_State *L_p) if (getArgumentCount(L_p) >= 2) { void *pointer = getPointer(L_p, 1); if (pointer != NULL) { - if (((QObject*)pointer)->inherits("QAction")) { + if (((QObject*)pointer)->inherits("LEListWidgetItem")) { + ((LEListWidgetItem*)pointer)->item()->setText(getVariant(L_p, 2).toString()); + } + else if (((QObject*)pointer)->inherits("QAction")) { ((QAction*)pointer)->setText(getVariant(L_p, 2).toString()); } else if (((QObject*)pointer)->inherits("QCheckBox")) { @@ -621,26 +638,35 @@ int LuaEngineGui::setWidgetFixed(lua_State *L_p) { if (getArgumentCount(L_p) >= 1) { void *pointer = getPointer(L_p, 1); - if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) { - bool isFixed = true; - if (getArgumentCount(L_p) >= 2) { - isFixed = getVariant(L_p, 2).toBool(); + if (pointer != NULL) { + if (((QObject*)pointer)->inherits("QToolBar")) { + bool isFixed = true; + if (getArgumentCount(L_p) >= 2) { + isFixed = getVariant(L_p, 2).toBool(); + } + ((QToolBar*)pointer)->setMovable(!isFixed); } - ((QObject*)pointer)->setProperty("isFixed", isFixed); - QVariant isSizeSet = ((QObject*)pointer)->property("isSizeSet"); - if (isFixed && ((QWidget*)pointer)->isVisible()) { - ((QWidget*)pointer)->setMinimumSize(((QWidget*)pointer)->sizeHint()); - ((QWidget*)pointer)->setMaximumSize(((QWidget*)pointer)->sizeHint()); + else if (((QObject*)pointer)->inherits("QWidget")) { + bool isFixed = true; + if (getArgumentCount(L_p) >= 2) { + isFixed = getVariant(L_p, 2).toBool(); + } + ((QObject*)pointer)->setProperty("isFixed", isFixed); + QVariant isSizeSet = ((QObject*)pointer)->property("isSizeSet"); + if (isFixed && ((QWidget*)pointer)->isVisible()) { + ((QWidget*)pointer)->setMinimumSize(((QWidget*)pointer)->sizeHint()); + ((QWidget*)pointer)->setMaximumSize(((QWidget*)pointer)->sizeHint()); + } + else if (isFixed && (isSizeSet.type() == QVariant::Bool && isSizeSet.toBool())) { + ((QWidget*)pointer)->setMinimumSize(((QWidget*)pointer)->size()); + ((QWidget*)pointer)->setMaximumSize(((QWidget*)pointer)->size()); + } + else if (((QWidget*)pointer)->isVisible()) { + ((QWidget*)pointer)->setMinimumSize(((QWidget*)pointer)->sizeHint()); + ((QWidget*)pointer)->setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX); + } + return 0; } - else if (isFixed && (isSizeSet.type() == QVariant::Bool && isSizeSet.toBool())) { - ((QWidget*)pointer)->setMinimumSize(((QWidget*)pointer)->size()); - ((QWidget*)pointer)->setMaximumSize(((QWidget*)pointer)->size()); - } - else if (((QWidget*)pointer)->isVisible()) { - ((QWidget*)pointer)->setMinimumSize(((QWidget*)pointer)->sizeHint()); - ((QWidget*)pointer)->setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX); - } - return 0; } } return 0; @@ -651,7 +677,10 @@ int LuaEngineGui::setWidgetImageSize(lua_State *L_p) if (getArgumentCount(L_p) >= 3) { void *pointer = getPointer(L_p, 1); if (pointer != NULL) { - if (((QObject*)pointer)->inherits("QPushButton")) { + if (((QObject*)pointer)->inherits("QListWidget")) { + ((QListWidget*)pointer)->setIconSize(QSize(getVariant(L_p, 2).toInt(), getVariant(L_p, 3).toInt())); + } + else if (((QObject*)pointer)->inherits("QPushButton")) { ((QPushButton*)pointer)->setIconSize(QSize(getVariant(L_p, 2).toInt(), getVariant(L_p, 3).toInt())); } else if (((QObject*)pointer)->inherits("QToolBar")) { @@ -704,6 +733,18 @@ int LuaEngineGui::setWidgetMinimum(lua_State *L_p) return 0; } +int LuaEngineGui::setWidgetParent(lua_State *L_p) +{ + if (getArgumentCount(L_p) >= 2) { + void *o_pointer = getPointer(L_p, 1); + void *p_pointer = getPointer(L_p, 2); + if (o_pointer != NULL && p_pointer != NULL && ((QObject*)o_pointer)->inherits("QWidget") && ((QObject*)p_pointer)->inherits("QWidget")) { + ((QWidget*)o_pointer)->setParent((QWidget*)p_pointer); + } + } + return 0; +} + int LuaEngineGui::setWidgetReadOnly(lua_State *L_p) { if (getArgumentCount(L_p) >= 1) { @@ -1022,6 +1063,43 @@ int LuaEngineGui::createLineEdit(lua_State *L_p) return 1; } +int LuaEngineGui::createListItem(lua_State *L_p) +{ + if (getArgumentCount(L_p) >= 2) { + void *pointer = getPointer(L_p, 2); + if (pointer != NULL) { + if (((QObject*)pointer)->inherits("QListWidget")) { + LEListWidgetItem *listWidgetItem = new LEListWidgetItem(getVariant(L_p, 1).toString(), (QListWidget*)pointer); + listWidgetItem->setObjectName(nameForPointer(listWidgetItem)); + pushPointer(L_p, listWidgetItem); + return 1; + } + } + } + return 0; +} + +int LuaEngineGui::createListView(lua_State *L_p) +{ + QLayout *layout = nullptr; + QWidget *parent = nullptr; + QListWidget::SelectionMode selectionMode = QListWidget::SingleSelection; + if (getArgumentCount(L_p) >= 1) { + lpForPointer(getPointer(L_p, 1), &layout, &parent); + if (getArgumentCount(L_p) >= 2) { + selectionMode = (QListWidget::SelectionMode)getVariant(L_p, 2).toInt(); + } + } + QListWidget *listWidget = new QListWidget(parent); + listWidget->setObjectName(nameForPointer(listWidget)); + listWidget->setSelectionMode(selectionMode); + if (layout != nullptr) { + layout->addWidget(listWidget); + } + pushPointer(L_p, listWidget); + return 1; +} + int LuaEngineGui::createMainWindow(lua_State *L_p) { QWidget *parent = nullptr; @@ -1311,15 +1389,18 @@ int LuaEngineGui::createTextEdit(lua_State *L_p) int LuaEngineGui::createToolBar(lua_State *L_p) { - if (getArgumentCount(L_p) >= 1) { - void *pointer = getPointer(L_p, 1); + if (getArgumentCount(L_p) >= 2) { + void *pointer = getPointer(L_p, 2); if (pointer != NULL && ((QObject*)pointer)->inherits("QMainWindow")) { Qt::ToolBarArea toolBarArea = Qt::TopToolBarArea; - if (getArgumentCount(L_p) >= 2) { - toolBarArea = (Qt::ToolBarArea)getVariant(L_p, 2).toInt(); + if (getArgumentCount(L_p) >= 3) { + toolBarArea = (Qt::ToolBarArea)getVariant(L_p, 3).toInt(); } QToolBar *toolBar = new QToolBar((QWidget*)pointer); toolBar->setObjectName(nameForPointer(toolBar)); + toolBar->setWindowTitle(getVariant(L_p, 1).toString()); + toolBar->setFloatable(false); + toolBar->setMovable(false); ((QMainWindow*)pointer)->addToolBar(toolBarArea, toolBar); pushPointer(L_p, toolBar); return 1; diff --git a/src/luaenginegui/luaengine/LuaEngineGui.h b/src/luaenginegui/luaengine/LuaEngineGui.h index fb64bca..93e8128 100644 --- a/src/luaenginegui/luaengine/LuaEngineGui.h +++ b/src/luaenginegui/luaengine/LuaEngineGui.h @@ -1,6 +1,6 @@ /***************************************************************************** * luaEngine Lua Engine for Qt -* Copyright (C) 2018-2019 Syping +* Copyright (C) 2018-2020 Syping * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,6 +50,7 @@ public: static int setWidgetLayout(lua_State *L_p); static int setWidgetMaximum(lua_State *L_p); static int setWidgetMinimum(lua_State *L_p); + static int setWidgetParent(lua_State *L_p); static int setWidgetReadOnly(lua_State *L_p); static int setWidgetSize(lua_State *L_p); static int setWidgetValue(lua_State *L_p); @@ -66,6 +67,8 @@ public: static int createLabel(lua_State *L_p); static int createLayout(lua_State *L_p); static int createLineEdit(lua_State *L_p); + static int createListItem(lua_State *L_p); + static int createListView(lua_State *L_p); static int createMainWindow(lua_State *L_p); static int createMenu(lua_State *L_p); static int createMenuBar(lua_State *L_p); diff --git a/src/luaenginegui/luaenginegui.pro b/src/luaenginegui/luaenginegui.pro index 4d0c7ac..b767b27 100644 --- a/src/luaenginegui/luaenginegui.pro +++ b/src/luaenginegui/luaenginegui.pro @@ -40,9 +40,11 @@ INCLUDEPATH += \ ../luaenginecore/luaengine SOURCES += \ + luaengine/LEListWidgetItem.cpp \ luaengine/LuaEngineGui.cpp HEADERS += \ + luaengine/LEListWidgetItem.h \ luaengine/LuaEngineGui.h \ luaengine/LuaEngineGui_global.h