461 lines
15 KiB
C++
461 lines
15 KiB
C++
#include "settingspageactivity.h"
|
|
#include <QJSEngine>
|
|
#include <QQmlContext>
|
|
#include <QQmlEngine>
|
|
#include "QQuickItem"
|
|
#include "quickhelper.h"
|
|
#include <QDebug>
|
|
#include "dynamicloader.h"
|
|
#include <QGuiApplication>
|
|
#include <QQmlApplicationEngine>
|
|
#include "settingsrepository.h"
|
|
#include "callbackwrapper.h"
|
|
#include "settingspagemodel.h"
|
|
#include "ArrDifferentTypes.h"
|
|
|
|
#include "ArrDifferentTypes.h"
|
|
#include "FigmaStyles.h"
|
|
#include "helpers.h"
|
|
#include <views/quartsbutton.h>
|
|
|
|
extern QQmlApplicationEngine *engine;
|
|
|
|
SettingsPageActivity::SettingsPageActivity(QQuickItem *item,const std::function<void()>& onSave, QObject *parent)
|
|
: root(item), QObject(parent), onSave(&onSave)
|
|
{
|
|
if (!item) {
|
|
qWarning() << "[C++] Root object is null";
|
|
return;
|
|
}
|
|
|
|
engine = qmlEngine(item);
|
|
|
|
if (!engine) {
|
|
qWarning() << "[C++] Failed to get QQmlEngine";
|
|
return;
|
|
}
|
|
|
|
loader = new DynamicLoader(engine,this);
|
|
connect(loader, &DynamicLoader::errorOccurred, this, &SettingsPageActivity::errorOccurredDynamycLoaderHandler);
|
|
init();
|
|
}
|
|
|
|
|
|
SettingsPageActivity::~SettingsPageActivity() {
|
|
qDebug()<<textProjectPath;
|
|
qDebug()<<textRemoteAddress;
|
|
}
|
|
|
|
void SettingsPageActivity::pageChanged() {
|
|
}
|
|
|
|
bool SettingsPageActivity::isAllElementsFound(){
|
|
bool retCode = true;
|
|
|
|
fieldProjectPath = QuickHelper::getQuickItem(root, FIELD_PROJECT_PATH);
|
|
if (!fieldProjectPath) {
|
|
qWarning() << "Failed to get fieldProjectPath:" << FIELD_PROJECT_PATH;
|
|
retCode = false;
|
|
}
|
|
|
|
fieldRemoteAdress = QuickHelper::getQuickItem(root, FIELD_REMOTE_ADDRESS);
|
|
if (!fieldRemoteAdress) {
|
|
qWarning() << "Failed to get fieldRemoteAdress:" << FIELD_REMOTE_ADDRESS;
|
|
retCode = false;
|
|
}
|
|
|
|
iconProjectPath = QuickHelper::getQuickItem(root, ICON_PROJECT_PATH);
|
|
if (!iconProjectPath) {
|
|
qWarning() << "Failed to get iconProjectPath:" << ICON_PROJECT_PATH;
|
|
retCode = false;
|
|
}
|
|
|
|
buttonSave = QuickHelper::getQuickItem(root, BUTTON_SAVE);
|
|
if (!buttonSave) {
|
|
qWarning() << "Failed to get ButtonSave:" << BUTTON_SAVE;
|
|
retCode = false;
|
|
}
|
|
|
|
return retCode;
|
|
}
|
|
|
|
void doFunction(
|
|
QObject* rootObject,
|
|
const QList<QVariant>& listItems,
|
|
const QString& textComponent,
|
|
const QString& functionName,
|
|
SettingsPageActivity& activity
|
|
) {
|
|
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 SettingsPageActivity::init()
|
|
{
|
|
if(!isAllElementsFound()) return;
|
|
|
|
textProjectPath = SettingsRepository::instance().defaultProjectDirectory();
|
|
textRemoteAddress = SettingsRepository::instance().remoteAddressUrl().toString();
|
|
// textLocalPort = QString::number(SettingsRepository::instance().getLocalServerPort());
|
|
|
|
textProjectPathDefault = textProjectPath;
|
|
textRemoteAddressDefault = textRemoteAddress;
|
|
// textLocalPortDefault = textLocalPort;
|
|
|
|
fieldRemoteAdress->setProperty("text", textRemoteAddress);
|
|
fieldProjectPath->setProperty("text", textProjectPath);
|
|
// fieldLocalServerPort->setProperty("text", textLocalPort);
|
|
|
|
CallbackWrapper* wraper = new CallbackWrapper([this] {textProjectPathChanged(); }, this);
|
|
QuickHelper::textChangedSubscription(fieldProjectPath, wraper);
|
|
|
|
wraper = new CallbackWrapper([this] {textRemoteAddressPathChanged(); }, this);
|
|
QuickHelper::textChangedSubscription(fieldRemoteAdress, wraper);
|
|
|
|
// wraper = new CallbackWrapper([this] {textLocalServerPortChanged(); }, this);
|
|
// QuickHelper::textChangedSubscription(fieldLocalServerPort, wraper);
|
|
|
|
|
|
QuickHelper::moveTextFiledX(fieldRemoteAdress);
|
|
QuickHelper::moveTextFiledX(fieldProjectPath);
|
|
|
|
|
|
QuickHelper::addMouseAreaElements(iconProjectPath,
|
|
[this] {clickChooseFolder();},
|
|
[this]{buttonEntered(iconProjectPath);},
|
|
[this]{buttonExited(iconProjectPath);});
|
|
|
|
QString componentText = R"(
|
|
import QtQuckApp
|
|
|
|
FolderDialog{}
|
|
)";
|
|
|
|
QList<QVariant> list;
|
|
CallbackWrapper *wrapper = new CallbackWrapper([this]{handleChooseFolder();},iconProjectPath);
|
|
list.append(QVariant::fromValue(wrapper));
|
|
QObject* addedComponent = 0;
|
|
typesData = 0;
|
|
QuickHelper::addComponent(iconProjectPath, list, componentText, &addedComponent, &typesData);
|
|
|
|
if (addedComponent) {
|
|
QQuickItem *item = qobject_cast<QQuickItem *>(addedComponent);
|
|
if (item) {
|
|
loadProjectFolderDialog = item;
|
|
}
|
|
} else {
|
|
qDebug()<< "addedComponent failed for FolderDialog ";
|
|
}
|
|
|
|
QuickHelper::addMouseAreaElements(buttonSave,[this]{saveClick();},
|
|
[this]{if(buttonSave->isEnabled()) {buttonSave->setProperty("color", QML::CORLOR_BUTTON_FOCUSED);}},
|
|
[this]{if(buttonSave->isEnabled()) {buttonSave->setProperty("color", QML::CORLOR_BUTTON_BASE);}});
|
|
|
|
addDialogSave();
|
|
}
|
|
|
|
void SettingsPageActivity::addDialogSave() {
|
|
QQuickItem *rootItem= qobject_cast<QQuickItem*>(::engine->rootObjects()[0]->children()[0]);
|
|
QString componentText = R"(
|
|
import QtQuick 2.15
|
|
import QtQuick.Controls 2.15
|
|
import QtQuckApp
|
|
|
|
Item {
|
|
function showMe() {
|
|
dialogSave.open()
|
|
}
|
|
|
|
SaveChangesDialog {
|
|
id: dialogSave
|
|
|
|
onSaveRequested: function() {
|
|
console.log("Yes")
|
|
quickUpdaterData.arrDifferentTypes[0].invoke()
|
|
}
|
|
|
|
onCancelRequested: function() {
|
|
console.log("No")
|
|
quickUpdaterData.arrDifferentTypes[1].invoke()
|
|
}
|
|
|
|
onAbortRequested: function() {
|
|
console.log("Abort")
|
|
quickUpdaterData.arrDifferentTypes[2].invoke()
|
|
}
|
|
}
|
|
}
|
|
)";
|
|
|
|
QObject *item = 0;
|
|
QVariantList list;
|
|
|
|
CallbackWrapper * wrapperYes = new CallbackWrapper([this] {
|
|
qDebug() << "CallbackWrapperSave";
|
|
|
|
if (dialogSaveModel.onYes != nullptr) {
|
|
dialogSaveModel.onYes();
|
|
}
|
|
});
|
|
|
|
CallbackWrapper * wrapperNo = new CallbackWrapper([this] {
|
|
qDebug() << "CallbackWrapperNo";
|
|
if (dialogSaveModel.onNo != nullptr) {
|
|
dialogSaveModel.onNo();
|
|
}
|
|
});
|
|
|
|
CallbackWrapper * wrapperCancel = new CallbackWrapper([this] {
|
|
qDebug() << "CallbackWrapperCancel";
|
|
if (dialogSaveModel.onCancel != nullptr) {
|
|
dialogSaveModel.onCancel();
|
|
}
|
|
});
|
|
|
|
list.append(QVariant::fromValue(wrapperYes));
|
|
list.append(QVariant::fromValue(wrapperCancel));
|
|
list.append(QVariant::fromValue(wrapperNo));
|
|
|
|
QQuickItem* parentItem = qobject_cast<QQuickItem*>(rootItem);
|
|
|
|
if (!parentItem) {
|
|
qWarning() << "[C++] Root object is not a QQuickItem";
|
|
return;
|
|
}
|
|
|
|
SettingsPageModel *settingsPageModel = new SettingsPageModel();
|
|
settingsPageModel->setProjectPath(textProjectPath);
|
|
settingsPageModel->setRemoteAddressUri(textRemoteAddress);
|
|
engine->rootContext()->setContextProperty("settingsPageModel", settingsPageModel);
|
|
|
|
QObject* addedComponent = 0;
|
|
QuickHelper::addComponent(parentItem, list, componentText, &addedComponent);
|
|
|
|
if(addedComponent) {
|
|
dialogSaveModel.rootItem = qobject_cast<QQuickItem*>(addedComponent);
|
|
|
|
QQuickItem * saveDialogItem = QuickHelper::getQuickItem(addedComponent, "SaveDialogItem");
|
|
dialogSaveModel.contentItem = qobject_cast<QQuickItem*>(addedComponent);
|
|
// Устанавливаем кнопки
|
|
QuartsButton *buttonSave = new QuartsButton(saveDialogItem);
|
|
buttonSave->setTextElementName("AcceptLabel");
|
|
buttonSave->typeFilled = QuartsButton::Filled;
|
|
buttonSave->setElementName("AcceptButton");
|
|
|
|
QuartsButton *buttonNoSave = new QuartsButton(saveDialogItem);
|
|
buttonNoSave->setTextElementName("DontSaveLabel");
|
|
buttonNoSave->typeFilled = QuartsButton::Border;
|
|
buttonNoSave->setElementName("NoButton");
|
|
|
|
QuartsButton *buttonCancel = new QuartsButton(saveDialogItem);
|
|
buttonCancel->setTextElementName("CancelLabel");
|
|
buttonCancel->typeFilled = QuartsButton::Border;
|
|
buttonCancel->setElementName("CancelButton");
|
|
|
|
// устанавливаем события
|
|
connect(buttonSave, &QuartsButton::clicked, [saveDialogItem] { QMetaObject::invokeMethod(
|
|
saveDialogItem,
|
|
"emitOkClicked"
|
|
);});
|
|
|
|
connect(buttonNoSave, &QuartsButton::clicked, [saveDialogItem] { QMetaObject::invokeMethod(
|
|
saveDialogItem,
|
|
"emitNoClicked"
|
|
);});
|
|
|
|
connect(buttonCancel, &QuartsButton::clicked, [saveDialogItem] { QMetaObject::invokeMethod(
|
|
saveDialogItem,
|
|
"emitCancelClicked"
|
|
);});
|
|
}
|
|
}
|
|
|
|
void SettingsPageActivity::showDialogSave(std::function<void()> doYes, std::function<void()> doNo, std::function<void()> doCancel)
|
|
{
|
|
if (dialogSaveModel.rootItem == 0) return;
|
|
|
|
dialogSaveModel.onYes = doYes;
|
|
dialogSaveModel.onNo = doNo;
|
|
dialogSaveModel.onCancel = doCancel;
|
|
|
|
bool success = QMetaObject::invokeMethod(
|
|
dialogSaveModel.rootItem,
|
|
"showMe"
|
|
);
|
|
}
|
|
|
|
|
|
void SettingsPageActivity::textProjectPathChanged() {
|
|
textProjectPath = fieldProjectPath->property("text").toString();
|
|
if (textProjectPath != textProjectPathDefault) {
|
|
auto item = fieldProjectPath->parentItem();
|
|
auto objectName = item->objectName();
|
|
fieldProjectPath->parentItem()->setProperty("borderColor",QML::COLOR_FIELD_BORDER_CHANGED);
|
|
} else {
|
|
fieldProjectPath->parentItem()->setProperty("borderColor",QML::COLOR_FIELD_BORDER_DEFAULT);
|
|
}
|
|
}
|
|
|
|
void SettingsPageActivity::textRemoteAddressPathChanged()
|
|
{
|
|
textRemoteAddress = fieldRemoteAdress->property("text").toString();
|
|
if (textRemoteAddress != textRemoteAddressDefault) {
|
|
// QuickHelper::setBorderColor(fieldRemoteAdress->parentItem(),QML::COLOR_FIELD_BORDER_CHANGED,1);
|
|
fieldRemoteAdress->parentItem()->setProperty("borderColor", QML::COLOR_FIELD_BORDER_CHANGED);
|
|
} else {
|
|
// QuickHelper::setBorderColor(fieldRemoteAdress->parentItem(),QML::COLOR_FIELD_BORDER_DEFAULT,1);
|
|
fieldRemoteAdress->parentItem()->setProperty("borderColor", QML::COLOR_FIELD_BORDER_DEFAULT);
|
|
}
|
|
}
|
|
|
|
void SettingsPageActivity::textLocalServerPortChanged()
|
|
{
|
|
// textLocalPort = fieldLocalServerPort->property("text").toString();
|
|
// if (textLocalPort != textProjectPathDefault) {
|
|
// if(Helpers::isPortTextValid(textLocalPort)) {
|
|
// fieldLocalServerPort->parentItem()->setProperty("borderColor", QML::COLOR_FIELD_BORDER_CHANGED);
|
|
// }else {
|
|
// fieldLocalServerPort->parentItem()->setProperty("borderColor", QML::COLOR_FIELD_BORDER_ERROR);
|
|
// }
|
|
// } else {
|
|
// fieldLocalServerPort->parentItem()->setProperty("borderColor", QML::COLOR_FIELD_BORDER_DEFAULT);
|
|
// }
|
|
}
|
|
|
|
void SettingsPageActivity::isAllowChangePage(const std::function<void ()> &continueChange, const std::function<void ()> &blockChange)
|
|
{
|
|
updateIsNeedSafe();
|
|
if(isNeedSafe) {
|
|
showDialogSave(
|
|
[this, continueChange]{ if(onSave) (*onSave)(); continueChange();}
|
|
,[this, continueChange]{continueChange();}
|
|
,[this, blockChange]{blockChange();}
|
|
);
|
|
}
|
|
else{
|
|
continueChange();
|
|
}
|
|
|
|
};
|
|
|
|
void SettingsPageActivity::errorOccurredDynamycLoaderHandler(const QString &message)
|
|
{
|
|
}
|
|
|
|
void SettingsPageActivity::clickChooseFolder()
|
|
{
|
|
if (loadProjectFolderDialog) {
|
|
QString path = SettingsRepository::instance().defaultProjectDirectory();
|
|
QUrl url = QUrl::fromLocalFile(path);
|
|
// url.toString() → "file:///C:/Users/Name/file.txt"
|
|
|
|
path = url.toLocalFile();
|
|
// path = url.toString();
|
|
QMetaObject::invokeMethod(loadProjectFolderDialog, "openWithPath",
|
|
Q_ARG(QVariant, url));
|
|
}
|
|
}
|
|
|
|
void SettingsPageActivity::handleChooseFolder()
|
|
{
|
|
if (loadProjectFolderDialog) {
|
|
|
|
QString val = typesData->getArrDifferentTypes()[1].toString();
|
|
if(val == "") return;
|
|
|
|
textProjectPath = val;
|
|
QUrl url(textProjectPath);
|
|
|
|
textProjectPath = url.toLocalFile();
|
|
|
|
fieldProjectPath->setProperty("text", textProjectPath);
|
|
}
|
|
}
|
|
|
|
|
|
void SettingsPageActivity::buttonEntered(QQuickItem * item) {
|
|
|
|
}
|
|
|
|
void SettingsPageActivity::buttonExited(QQuickItem * item) {
|
|
|
|
}
|
|
|
|
void SettingsPageActivity::saveClick()
|
|
{
|
|
if(onSave) (*onSave)();
|
|
isNeedSafe = false;
|
|
|
|
|
|
textProjectPathDefault = textProjectPath;
|
|
textRemoteAddressDefault = textRemoteAddress;
|
|
// textLocalPortDefault = textLocalPort;
|
|
|
|
emit afterSave();
|
|
}
|
|
|
|
void SettingsPageActivity::updateIsNeedSafe()
|
|
{
|
|
isNeedSafe = !(textProjectPath == textProjectPathDefault && textRemoteAddress == textRemoteAddressDefault); //&& textLocalPort == textLocalPortDefault
|
|
}
|