290 lines
9.3 KiB
C++
290 lines
9.3 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::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->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
|
||
) {
|
||
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);
|
||
|
||
}
|
||
|
||
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::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::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");
|
||
}
|
||
|
||
|
||
void QuickHelper::addMouseAreaElements(QObject* rootObject,
|
||
std::function<void()> onClickedAction,
|
||
std::function<void()> onEnteredAction,
|
||
std::function<void()> onExitedAction
|
||
) {
|
||
// 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: Qt.PointingHandCursor
|
||
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);
|
||
|
||
};
|