547 lines
18 KiB
C++
547 lines
18 KiB
C++
#include "quickhelper.h"
|
||
#include "QDebug"
|
||
#include <QJSEngine>
|
||
#include <QQmlContext>
|
||
#include <QQmlEngine>
|
||
#include "QQuickItem"
|
||
#include "callbackwrapper.h"
|
||
#include "qrgb.h"
|
||
#include "ArrDifferentTypes.h"
|
||
|
||
QuickHelper::QuickHelper() {}
|
||
|
||
void QuickHelper::updateElement(QObject *rootObject, const QString &elementName, std::function<void (QObject *, std::function<void ()>)> updateItemFunction, std::function<void ()> onClick)
|
||
{
|
||
QObject *item = rootObject->findChild<QObject*>(elementName);
|
||
if (item) {
|
||
updateItemFunction(item,onClick);
|
||
} else {
|
||
qWarning() << "Элемент с objectName "<< elementName << " не найден";
|
||
}
|
||
}
|
||
void QuickHelper::freeItem(QQuickItem *&item) {
|
||
if (item) {
|
||
item->setParentItem(nullptr);
|
||
item->setParent(nullptr);
|
||
item->deleteLater();
|
||
item->setVisible(false);
|
||
|
||
item = 0;
|
||
}
|
||
}
|
||
void QuickHelper::clearChildren(QQuickItem *parentItem)
|
||
{
|
||
const auto children = parentItem->children();
|
||
|
||
if (children.size()) {
|
||
|
||
for (QObject* child : children) {
|
||
if (QQuickItem* item = qobject_cast<QQuickItem*>(child)) {
|
||
item->setParent(nullptr);
|
||
item->setParentItem(nullptr);
|
||
item->setVisible(false);
|
||
item->deleteLater();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
void QuickHelper::moveTextFiledX(QQuickItem *item)
|
||
{
|
||
item->setX(item->x()-5);
|
||
item->setWidth(item->width()+5);
|
||
}
|
||
|
||
QQuickItem *QuickHelper::getQuickItem(QObject *rootObject, const QString &elementName)
|
||
{
|
||
QObject *item = rootObject->findChild<QObject*>(elementName);
|
||
|
||
if (!item) {
|
||
qWarning() << "Элемент с objectName "<< elementName << " не найден";
|
||
return 0;
|
||
}
|
||
|
||
QQuickItem* quickItem = qobject_cast<QQuickItem*>(item);
|
||
if (!quickItem) {
|
||
qWarning() << "[C++] Root object is not a QQuickItem";
|
||
return 0;
|
||
}
|
||
|
||
return quickItem;
|
||
}
|
||
|
||
void QuickHelper::doFunction(
|
||
QObject* rootObject,
|
||
const QList<QVariant>& listItems,
|
||
const QString& textComponent,
|
||
const QString& functionName
|
||
|
||
) {
|
||
qDebug() << "[C++] Starting to create function";
|
||
|
||
if (!rootObject) {
|
||
qWarning() << "[C++] Root object is null";
|
||
return;
|
||
}
|
||
|
||
QQuickItem* parentItem = qobject_cast<QQuickItem*>(rootObject);
|
||
if (!parentItem) {
|
||
qWarning() << "[C++] Root object is not a QQuickItem";
|
||
return;
|
||
}
|
||
|
||
QQmlEngine* engine = qmlEngine(rootObject);
|
||
if (!engine) {
|
||
qWarning() << "[C++] Failed to get QQmlEngine";
|
||
return;
|
||
}
|
||
|
||
// 1. Создаем ArrDifferentTypes и добавляем в контекст QML
|
||
ArrDifferentTypes* arrDifferentTypes = new ArrDifferentTypes(parentItem);
|
||
arrDifferentTypes->setArrDifferentTypes(listItems);
|
||
|
||
QQmlContext* context = new QQmlContext(engine->rootContext(), parentItem);
|
||
context->setContextProperty("quickUpdaterData", arrDifferentTypes);
|
||
|
||
QQmlComponent component(engine);
|
||
component.setData(textComponent.toLocal8Bit(), QUrl());
|
||
|
||
if (component.isError()) {
|
||
qWarning() << "QML Component errors:" << component.errors() << "\n" << textComponent;
|
||
delete context;
|
||
return;
|
||
}
|
||
|
||
QObject* updaterObject = component.create(context);
|
||
if (!updaterObject) {
|
||
qWarning() << "Error creating updaterObject:" << component.errors();
|
||
delete context;
|
||
return;
|
||
}
|
||
|
||
// 4. Настраиваем родительские связи (управление памятью через Qt-родителя)
|
||
updaterObject->setParent(parentItem);
|
||
QQuickItem *updaterObjectItem = qobject_cast<QQuickItem*>(updaterObject);
|
||
|
||
if (updaterObjectItem) {
|
||
updaterObjectItem->setParentItem(parentItem);
|
||
}
|
||
// 5. Контекст удалится автоматически при удалении updaterObject
|
||
context->setParent(updaterObject);
|
||
|
||
// 6. Вызываем функцию в QML
|
||
bool success = QMetaObject::invokeMethod(
|
||
updaterObject,
|
||
functionName.toUtf8().constData(),
|
||
Q_ARG(QVariant, QVariant::fromValue(rootObject))
|
||
);
|
||
|
||
if (!success) {
|
||
qWarning() << "Failed to invoke QML function:" << functionName;
|
||
}
|
||
}
|
||
|
||
void QuickHelper::addComponent(
|
||
QObject* rootObject,
|
||
const QList<QVariant>& listItems,
|
||
const QString& textComponent,
|
||
QObject** addedComponent,
|
||
ArrDifferentTypes** arrDiffTypesOut,
|
||
QList<QPair<QString, QObject*>> *listComponentsToSet
|
||
) {
|
||
qDebug() << "[C++] Starting to create function";
|
||
|
||
if (!rootObject) {
|
||
qWarning() << "[C++] Root object is null";
|
||
return;
|
||
}
|
||
|
||
QQuickItem* parentItem = qobject_cast<QQuickItem*>(rootObject);
|
||
if (!parentItem) {
|
||
qWarning() << "[C++] Root object is not a QQuickItem";
|
||
return;
|
||
}
|
||
|
||
QQmlEngine* engine = qmlEngine(rootObject);
|
||
if (!engine) {
|
||
qWarning() << "[C++] Failed to get QQmlEngine";
|
||
return;
|
||
}
|
||
|
||
// 1. Создаем ArrDifferentTypes и добавляем в контекст QML
|
||
ArrDifferentTypes* arrDifferentTypes = new ArrDifferentTypes(parentItem);
|
||
arrDifferentTypes->setArrDifferentTypes(listItems);
|
||
|
||
QQmlContext* context = new QQmlContext(engine->rootContext(), parentItem);
|
||
|
||
context->setContextObject(arrDifferentTypes);
|
||
context->setContextProperty("quickUpdaterData", arrDifferentTypes);
|
||
if (listComponentsToSet&&listComponentsToSet->size()) {
|
||
auto it = listComponentsToSet->begin();
|
||
auto itEnd = listComponentsToSet->end();
|
||
|
||
while (it != itEnd) {
|
||
context->setContextProperty(it->first, it->second);
|
||
++it;
|
||
}
|
||
}
|
||
|
||
QQmlComponent component(engine);
|
||
component.setData(textComponent.toLocal8Bit(), QUrl());
|
||
|
||
if (component.isError()) {
|
||
auto errors = component.errors();
|
||
qWarning() << "QML Component errors:";
|
||
for(const auto& error : errors) {
|
||
qDebug() << error.toString();
|
||
}
|
||
qWarning()<< "\n" << textComponent;
|
||
delete context;
|
||
return;
|
||
}
|
||
|
||
QObject* updaterObject = component.create(context);
|
||
if (!updaterObject) {
|
||
qWarning() << "Error creating updaterObject:" << component.errors();
|
||
delete context;
|
||
return;
|
||
}
|
||
|
||
// 4. Настраиваем родительские связи (управление памятью через Qt-родителя)
|
||
updaterObject->setParent(parentItem);
|
||
QQuickItem* updaterObjectItem = qobject_cast<QQuickItem*>(updaterObject);
|
||
if (updaterObjectItem) {
|
||
updaterObjectItem->setParentItem(parentItem);
|
||
}
|
||
|
||
// 5. Контекст удалится автоматически при удалении updaterObject
|
||
context->setParent(updaterObject);
|
||
context->setObjectName("customQMLContext");
|
||
|
||
if (addedComponent) {
|
||
*addedComponent = updaterObject;
|
||
}
|
||
|
||
if (arrDiffTypesOut) {
|
||
*arrDiffTypesOut = arrDifferentTypes;
|
||
}
|
||
}
|
||
|
||
void QuickHelper::updateColor(QObject* rootObject, QColor disabledColor, QColor enabledColor){
|
||
QString textComponent = R"(
|
||
import QtQuick 2.15
|
||
|
||
QtObject {
|
||
function updateColor(smthItem) {
|
||
if (!quickUpdaterData || quickUpdaterData.arrDifferentTypes.length < 2) {
|
||
console.log("quickUpdaterData not set or length < 2")
|
||
return
|
||
}
|
||
|
||
var colorDisabled = quickUpdaterData.arrDifferentTypes[0]
|
||
var colorEnabled = quickUpdaterData.arrDifferentTypes[1]
|
||
|
||
// Просто устанавливаем binding напрямую
|
||
smthItem.color = Qt.binding(function() {
|
||
return smthItem.enabled ? colorEnabled : colorDisabled
|
||
})
|
||
}
|
||
}
|
||
)";
|
||
QString function = "updateColor";
|
||
QVariantList list;
|
||
list.append(disabledColor);
|
||
list.append(enabledColor);
|
||
QuickHelper::doFunction(rootObject, list,textComponent,function);
|
||
}
|
||
|
||
void QuickHelper::setFunctionHandlerArg1(QObject* rootObject, const QString¶m, std::function<void(QVariant)> handler){
|
||
QString textComponent = R"(
|
||
import QtQuick 2.15
|
||
|
||
QtObject {
|
||
function setFunctionHandler(smthItem) {
|
||
if (!quickUpdaterData || quickUpdaterData.arrDifferentTypes.length < 1) {
|
||
console.log("quickUpdaterData not set or length < 1")
|
||
return
|
||
}
|
||
|
||
// Просто устанавливаем binding напрямую
|
||
smthItem.)" + param + R"( = Qt.binding(function(item) {
|
||
quickUpdaterData.arrDifferentTypes[0](item)
|
||
})
|
||
}
|
||
}
|
||
)";
|
||
QString function = "setFunctionHandler";
|
||
QVariantList list;
|
||
list.append(QVariant::fromValue(handler));
|
||
QuickHelper::doFunction(rootObject, list,textComponent,function);
|
||
}
|
||
|
||
void QuickHelper::setPorpetryEnumerator(
|
||
QQuickItem * item,
|
||
const QString & enumeratorName,
|
||
const QString & propertyName,
|
||
const QString & propertyValue
|
||
){
|
||
const QMetaObject *metaObject = item->metaObject();
|
||
int enumIndex = metaObject->indexOfEnumerator(enumeratorName.toUtf8().constData());
|
||
QMetaEnum metaEnum = metaObject->enumerator(enumIndex);
|
||
// Получаем числовое значение нужного enum
|
||
int horizontalFitValue = metaEnum.keyToValue(propertyValue.toUtf8().constData());
|
||
// Устанавливаем свойство fontSizeMode
|
||
item->setProperty(propertyName.toUtf8().constData(), horizontalFitValue);
|
||
}
|
||
|
||
void QuickHelper::setPropetryEnumerator(
|
||
QQuickItem* item,
|
||
const QString& propertyName,
|
||
const QString& propertyValue
|
||
) {
|
||
// Проверяем, является ли item объектом, который имеет нужное свойство
|
||
const QMetaObject* metaObject = item->metaObject();
|
||
|
||
// Получаем индекс свойства по его имени
|
||
int propIndex = metaObject->indexOfProperty(propertyName.toUtf8().constData());
|
||
if (propIndex == -1) {
|
||
qWarning() << "Property not found:" << propertyName;
|
||
return;
|
||
}
|
||
|
||
QMetaProperty metaProp = metaObject->property(propIndex);
|
||
QMetaEnum metaEnum = metaProp.enumerator(); // Получаем enum, связанный с этим свойством
|
||
|
||
if (!metaEnum.isValid()) {
|
||
qWarning() << "Property is not an enum:" << propertyName;
|
||
return;
|
||
}
|
||
|
||
// Получаем числовое значение из строки
|
||
int value = metaEnum.keyToValue(propertyValue.toUtf8().constData());
|
||
if (value == -1) {
|
||
qWarning() << "Enum value not found:" << propertyValue;
|
||
return;
|
||
}
|
||
|
||
// Устанавливаем значение
|
||
item->setProperty(propertyName.toUtf8().constData(), value);
|
||
}
|
||
|
||
void QuickHelper::textChangedSubscription(QObject* rootObject, CallbackWrapper* wrapper){
|
||
QString textComponent = R"(
|
||
import QtQuick 2.15
|
||
|
||
QtObject {
|
||
function addTrigger(smthItem) {
|
||
if (!quickUpdaterData) {
|
||
console.log("quickUpdaterData not set")
|
||
return
|
||
}
|
||
smthItem.textChanged.connect(function() {
|
||
quickUpdaterData.arrDifferentTypes[0].invoke()
|
||
})
|
||
}
|
||
}
|
||
|
||
)";
|
||
|
||
QVariantList list;
|
||
|
||
list.append(QVariant::fromValue(wrapper));
|
||
QuickHelper::doFunction(rootObject, list,textComponent,"addTrigger");
|
||
}
|
||
|
||
|
||
QQmlContext* QuickHelper::getContextFromQObject(QObject* qmlObject) {
|
||
if (!qmlObject) {
|
||
qWarning() << "QObject is null";
|
||
return nullptr;
|
||
}
|
||
|
||
// Получаем контекст через QML-движок
|
||
QQmlEngine* engine = qmlEngine(qmlObject);
|
||
if (!engine) {
|
||
qWarning() << "Failed to get QQmlEngine from QObject";
|
||
return nullptr;
|
||
}
|
||
|
||
// Получаем контекст объекта
|
||
QQmlContext* context = qmlContext(qmlObject);
|
||
if (!context) {
|
||
qWarning() << "Failed to get QQmlContext from QObject";
|
||
return nullptr;
|
||
}
|
||
|
||
return context;
|
||
}
|
||
|
||
void QuickHelper::addMouseAreaElements(QObject* rootObject,
|
||
std::function<void()> onClickedAction,
|
||
std::function<void()> onEnteredAction,
|
||
std::function<void()> onExitedAction,
|
||
QObject** addedComponent
|
||
) {
|
||
// 1. Создаем CallbackWrapper и добавляем в контекст QML
|
||
CallbackWrapper* wrapperClickedAction = new CallbackWrapper(onClickedAction, rootObject);
|
||
CallbackWrapper* wrapperonEnteredAction = new CallbackWrapper(onEnteredAction, rootObject);
|
||
CallbackWrapper* wrapperonExitedAction = new CallbackWrapper(onExitedAction, rootObject);
|
||
|
||
QString textComponent = R"(
|
||
import QtQuick 2.15
|
||
MouseArea {
|
||
anchors.fill: parent
|
||
hoverEnabled: true
|
||
cursorShape: parent.enabled ? Qt.PointingHandCursor : Qt.ArrowCursor
|
||
enabled: parent.enabled
|
||
onClicked: quickUpdaterData.arrDifferentTypes[0].invoke()
|
||
onEntered: quickUpdaterData.arrDifferentTypes[1].invoke()
|
||
onExited: quickUpdaterData.arrDifferentTypes[2].invoke()
|
||
}
|
||
)";
|
||
|
||
QVariantList list;
|
||
|
||
list.append(QVariant::fromValue(wrapperClickedAction));
|
||
list.append(QVariant::fromValue(wrapperonEnteredAction));
|
||
list.append(QVariant::fromValue(wrapperonExitedAction));
|
||
QuickHelper::addComponent(rootObject, list,textComponent,addedComponent);
|
||
};
|
||
|
||
void QuickHelper::addMouseAreaWithCopy(QObject* rootObject,
|
||
std::function<void()> onClickedAction,
|
||
std::function<void()> onEnteredAction,
|
||
std::function<void()> onExitedAction,
|
||
QObject** addedComponent,
|
||
ArrDifferentTypes** arrDiffTypesOut
|
||
) {
|
||
// 1. Создаем CallbackWrapper и добавляем в контекст QML
|
||
CallbackWrapper* wrapperClickedAction = new CallbackWrapper(onClickedAction, rootObject);
|
||
CallbackWrapper* wrapperonEnteredAction = new CallbackWrapper(onEnteredAction, rootObject);
|
||
CallbackWrapper* wrapperonExitedAction = new CallbackWrapper(onExitedAction, rootObject);
|
||
|
||
QString textComponent = R"(
|
||
import QtQuick 2.15
|
||
import QtQuckApp
|
||
|
||
MouseArea {
|
||
id: parentItem
|
||
anchors.fill: parent
|
||
hoverEnabled: true
|
||
cursorShape: Qt.PointingHandCursor
|
||
onClicked: {
|
||
quickUpdaterData.arrDifferentTypes[0].invoke()
|
||
copyNotification.mousePos = Qt.point(mouse.x, mouse.y)
|
||
copyNotification.parentWindow = parentItem
|
||
copyNotification.show()
|
||
}
|
||
onEntered: quickUpdaterData.arrDifferentTypes[1].invoke()
|
||
onExited: quickUpdaterData.arrDifferentTypes[2].invoke()
|
||
|
||
CopiedNotification {
|
||
id: copyNotification
|
||
}
|
||
}
|
||
)";
|
||
|
||
QVariantList list;
|
||
|
||
list.append(QVariant::fromValue(wrapperClickedAction));
|
||
list.append(QVariant::fromValue(wrapperonEnteredAction));
|
||
list.append(QVariant::fromValue(wrapperonExitedAction));
|
||
QuickHelper::addComponent(rootObject, list,textComponent,addedComponent,arrDiffTypesOut);
|
||
|
||
};
|
||
|
||
QQuickItem * QuickHelper::addProgressBar(QQuickItem* parent, QQuickItem *progressBar, QQuickItem * progressBarFilled) {
|
||
if (progressBar && progressBarFilled) {
|
||
QList<QVariant> listItems;
|
||
listItems.append(QVariant::fromValue(progressBar));
|
||
listItems.append(QVariant::fromValue(progressBarFilled));
|
||
|
||
QString textComponent = R"(
|
||
import QtQuick
|
||
import QtQuckApp
|
||
|
||
ProgressBar {
|
||
id: progressBar
|
||
anchors.centerIn: parent
|
||
parentItem: quickUpdaterData.arrDifferentTypes[0]
|
||
childItem: quickUpdaterData.arrDifferentTypes[1]
|
||
}
|
||
)";
|
||
|
||
QObject* addedComponent = 0;
|
||
QuickHelper::addComponent(parent,listItems,textComponent, &addedComponent);
|
||
QQuickItem *item = qobject_cast<QQuickItem*>(addedComponent);
|
||
return item;
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
QQuickItem * QuickHelper::addContentToItem(QQuickItem *parentItem, QString name, const QString& text,
|
||
QList<QPair<QString, QObject*>> *listComponentsToSet)
|
||
{
|
||
if (name.endsWith(".qml")) {
|
||
name = name.left(name.size()-4);
|
||
}
|
||
|
||
QString componentName = name;
|
||
QString textCompnent = R"(
|
||
import FigmaQmlInterface
|
||
import QtQuckApp
|
||
|
||
)";
|
||
|
||
textCompnent += componentName;
|
||
textCompnent += R"(
|
||
{
|
||
id: root
|
||
)";
|
||
textCompnent+= text;
|
||
textCompnent+= R"(
|
||
}
|
||
)" ;
|
||
QVariantList listItems;
|
||
QObject *createdComponent = 0;
|
||
QQuickItem *createdComponentItem = 0;
|
||
QuickHelper::addComponent(parentItem, listItems,textCompnent, &createdComponent,0,listComponentsToSet);
|
||
|
||
if (createdComponent) {
|
||
createdComponentItem = qobject_cast<QQuickItem*>(createdComponent);
|
||
}
|
||
|
||
return createdComponentItem;
|
||
}
|
||
|
||
void QuickHelper::setBorderColor(QQuickItem *item, const QColor &color, int borderWidth) {
|
||
if (!item) {
|
||
qWarning() << "Invalid item passed to setBorderColor";
|
||
return;
|
||
}
|
||
|
||
if (item->property("border").isValid()) {
|
||
QVariantMap border = item->property("border").toMap();
|
||
border["color"] = color;
|
||
if (borderWidth >= 0) {
|
||
border["width"] = borderWidth;
|
||
}
|
||
item->setProperty("border", border);
|
||
} else {
|
||
qWarning() << "Item does not have a 'border' property:" << item;
|
||
}
|
||
}
|
||
|
||
QQmlContext * QuickHelper::getCustomContext(QQuickItem* object){
|
||
if (object){
|
||
auto childObject = object->findChild<QObject*>("customQMLContext");
|
||
if(childObject){
|
||
return qobject_cast<QQmlContext*>(childObject);
|
||
}
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
|