441 lines
14 KiB
C++
441 lines
14 KiB
C++
#include "figmaadapter.h"
|
|
// #include "FigmaQmlInterface.hpp"
|
|
#include "QDebug"
|
|
#include "QQuickItem"
|
|
#include <QJSEngine>
|
|
#include <QQmlContext>
|
|
#include <QQmlEngine>
|
|
#include <callbackwrapper.h>
|
|
|
|
#include "dynamicloader.h"
|
|
#include "quickhelper.h"
|
|
|
|
#include "mainpageactivity.h"
|
|
#include "mainpagepresentor.h"
|
|
#include "settingspageactivity.h"
|
|
#include "settingsrepository.h"
|
|
#include "FigmaStyles.h"
|
|
#include "thread"
|
|
#include "singalizer.h"
|
|
#include "projectpageactivity.h"
|
|
#include <QStorageInfo>
|
|
|
|
const char * HOME_ITEM = "HomeItem";
|
|
const char * PROJECT_ITEM = "ProjectsItem";
|
|
const char * SETTINGS_ITEM = "SettingsItem";
|
|
const char * CLOSE_ITEM = "CloseItem";
|
|
|
|
FigmaAdapter::FigmaAdapter(void * figmaQtInstance, QQmlApplicationEngine &engine, QObject *parent)
|
|
: QObject{parent} , engine(engine)
|
|
{
|
|
figmaAdapter = figmaQtInstance;
|
|
// connect(getFigmaAdapter(), &FigmaQmlSingleton::eventReceived, this, &FigmaAdapter::eventReceived);
|
|
// connect(getFigmaAdapter(), &FigmaQmlSingleton::viewLoaded, this, &FigmaAdapter::viewLoaded);
|
|
loader = new DynamicLoader(&engine,this);
|
|
connect(loader,&DynamicLoader::componentCreated, this, &FigmaAdapter::componentCreated);
|
|
connect(loader,&DynamicLoader::errorOccurred, this, &FigmaAdapter::errorOccurred);
|
|
saveSettingClickCallback = [this]{saveSettingClick();};
|
|
}
|
|
|
|
FigmaAdapter::~FigmaAdapter()
|
|
{
|
|
if(isFigmaAdapterLyfe){
|
|
*isFigmaAdapterLyfe = false;
|
|
}
|
|
}
|
|
|
|
void FigmaAdapter::Init()
|
|
{
|
|
auto list = engine.rootObjects();
|
|
if (list.empty()) {
|
|
return;
|
|
}
|
|
QObject *rootObject = engine.rootObjects().first();
|
|
|
|
updateElement(rootObject, HOME_ITEM, [this](QObject* rootObject, std::function<void()> onClickAction){
|
|
QuickHelper::addMouseAreaElements(rootObject,
|
|
[this]{ homeItemClick();},
|
|
[this,rootObject]{headerEntered((QQuickItem*)rootObject);},
|
|
[this,rootObject]{headerExited((QQuickItem*)rootObject);}
|
|
);
|
|
},0);
|
|
|
|
updateElement(rootObject, PROJECT_ITEM, [this](QObject* rootObject, std::function<void()> onClickAction){
|
|
QuickHelper::addMouseAreaElements(rootObject,
|
|
[this]{ projectsItemClick();},
|
|
[this,rootObject]{headerEntered((QQuickItem*)rootObject);},
|
|
[this,rootObject]{headerExited((QQuickItem*)rootObject);}
|
|
);
|
|
},0);
|
|
updateElement(rootObject, SETTINGS_ITEM, [this](QObject* rootObject, std::function<void()> onClickAction){
|
|
QuickHelper::addMouseAreaElements(rootObject,
|
|
[this]{ settingsItemClick();},
|
|
[this,rootObject]{headerEntered((QQuickItem*)rootObject);},
|
|
[this,rootObject]{headerExited((QQuickItem*)rootObject);}
|
|
);
|
|
},0);
|
|
|
|
updateElement(rootObject, CLOSE_ITEM, [this](QObject* rootObject, std::function<void()> onClickAction){
|
|
QuickHelper::addMouseAreaElements(rootObject,
|
|
[this]{ closeItemClick();},
|
|
[this,rootObject]{headerEntered((QQuickItem*)rootObject);},
|
|
[this,rootObject]{headerExited((QQuickItem*)rootObject, QML::CORLOR_MENU_ITEM_BASE_EXIT);}
|
|
);
|
|
},0);
|
|
|
|
labelAvailibleSizeButton = QuickHelper::getQuickItem(rootObject, LABEL_AVAILIBLE_SIZE);
|
|
rectAvailibleSize = QuickHelper::getQuickItem(rootObject, RECT_AVAILIBLE_SIZE_BAR);
|
|
rectPassedBar = QuickHelper::getQuickItem(rootObject, RECT_AVAILIBLE_PASSED_BAR);
|
|
componentLabelAvailibleSize = QuickHelper::getQuickItem(rootObject, COMPONENT_LABEL_AVAILIBLE_SIZE);
|
|
homeItemClick();
|
|
|
|
if (rectAvailibleSize && rectPassedBar){
|
|
progressBarController = QuickHelper::addProgressBar(rectAvailibleSize,rectAvailibleSize,rectPassedBar);
|
|
}
|
|
|
|
isFigmaAdapterLyfe = new bool(true);
|
|
std::thread th([this] {
|
|
bool *check = isFigmaAdapterLyfe;
|
|
while (*check) {
|
|
updateStorageInfo();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
|
}
|
|
delete check;
|
|
});
|
|
|
|
th.detach();
|
|
|
|
if (componentLabelAvailibleSize) {
|
|
QObject *addedComponent = 0;
|
|
QuickHelper::addMouseAreaElements(componentLabelAvailibleSize->parent(),
|
|
[this]{if(clickableStorageInfo->isEnabled())settingsItemClick();},
|
|
[this]{if(clickableStorageInfo->isEnabled())headerEntered((QQuickItem*)componentLabelAvailibleSize->parent());},
|
|
[this]{if(clickableStorageInfo->isEnabled())headerExited((QQuickItem*)componentLabelAvailibleSize->parent());}, &addedComponent
|
|
);
|
|
|
|
if (addedComponent) {
|
|
clickableStorageInfo = qobject_cast<QQuickItem*>(addedComponent);
|
|
}
|
|
}
|
|
|
|
if (labelAvailibleSizeButton) {
|
|
QuickHelper::setPorpetryEnumerator(
|
|
labelAvailibleSizeButton,"FontSizeMode", "fontSizeMode", "HorizontalFit"
|
|
);
|
|
}
|
|
|
|
}
|
|
|
|
void FigmaAdapter::updateElement(QObject *rootObject, const QString &elementName,
|
|
std::function<void(QObject*, std::function<void()>)> updateItemFunction,
|
|
std::function<void()> onClick)
|
|
{
|
|
QuickHelper::updateElement(rootObject,elementName,updateItemFunction, onClick);
|
|
}
|
|
|
|
void FigmaAdapter::homeItemClick()
|
|
{
|
|
qDebug()<< "homeItemClick";
|
|
loadPage(QML::Pages::MAIN,[this]{homeItemLoaded();});
|
|
}
|
|
|
|
void FigmaAdapter::homeItemLoaded()
|
|
{
|
|
QObject *rootObject = engine.rootObjects().first();
|
|
QQuickItem * mainViewItem = QuickHelper::getQuickItem(rootObject, "MainView");
|
|
|
|
if(!mainViewItem) return;
|
|
// QObjectList listChildren = mainViewItem->children();
|
|
|
|
QQuickItem * mainPage;
|
|
mainPage = QuickHelper::getQuickItem(mainViewItem, "MainPage");
|
|
|
|
MainPageActivity * mainPageActivity = 0;
|
|
|
|
if (rootObject && mainViewItem) {
|
|
mainPageActivity = new MainPageActivity(mainViewItem,mainPage);
|
|
activity = mainPageActivity;
|
|
}
|
|
|
|
MainPagePresentor *presentor = new MainPagePresentor(mainPageActivity, mainPage);
|
|
engine.rootContext()->setContextProperty("mainViewPresentor", presentor);
|
|
}
|
|
|
|
#include "projectpageppesentor.h"
|
|
|
|
void FigmaAdapter::projectsItemClick()
|
|
{
|
|
qDebug()<< "projectsItemClick";
|
|
loadPage(QML::Pages::PROJECT,[this]{projectsItemLoaded();});
|
|
}
|
|
|
|
void FigmaAdapter::projectsItemLoaded()
|
|
{
|
|
QObject *rootObject = engine.rootObjects().first();
|
|
QQuickItem * mainViewItem = QuickHelper::getQuickItem(rootObject, "MainView");
|
|
|
|
if (!mainViewItem) return;
|
|
|
|
QQuickItem * projectPage;
|
|
projectPage = QuickHelper::getQuickItem(mainViewItem, "ProjectPage");
|
|
|
|
ProjectPageActivity * projectPageActivity = 0;
|
|
|
|
if (rootObject && mainViewItem) {
|
|
projectPageActivity = new ProjectPageActivity(mainViewItem, projectPage);
|
|
activity = projectPageActivity;
|
|
}
|
|
|
|
ProjectPagePesentor * presentor = new ProjectPagePesentor(projectPageActivity, projectPage);
|
|
}
|
|
|
|
void FigmaAdapter::settingsItemClick()
|
|
{
|
|
qDebug()<< "settingsItemClick";
|
|
loadPage(QML::Pages::SETTINGS, [this]{settingsItemLoaded();});
|
|
}
|
|
|
|
void FigmaAdapter::settingsItemLoaded() {
|
|
QObject *rootObject = engine.rootObjects().first();
|
|
QQuickItem * mainViewItem = QuickHelper::getQuickItem(rootObject, "MainView");
|
|
|
|
if(!mainViewItem) return;
|
|
|
|
QQuickItem * settingsPage;
|
|
settingsPage = QuickHelper::getQuickItem(mainViewItem, "SettingsPage");
|
|
|
|
SettingsPageActivity * settingsPageActivity = 0;
|
|
|
|
if (rootObject && mainViewItem) {
|
|
settingsPageActivity = new SettingsPageActivity(mainViewItem,saveSettingClickCallback, settingsPage);
|
|
activity = settingsPageActivity;
|
|
}
|
|
}
|
|
|
|
void FigmaAdapter::closeItemClick()
|
|
{
|
|
qDebug()<< "closeItemClick";
|
|
exit(0);
|
|
}
|
|
|
|
void FigmaAdapter::loadPage(const QString& pageName, std::function<void()> onPageLoaded) {
|
|
QObject *rootObject = engine.rootObjects().first();
|
|
|
|
QString page = pageName;//DEFAULT_PAGE_PATH +
|
|
|
|
auto updateFunc = [this, page, onPageLoaded](QObject* rootObject, std::function<void()> ) {
|
|
doActionOnSuccessAllowChangePaged = [this, rootObject,page, onPageLoaded]{
|
|
QQuickItem* parentItem = qobject_cast<QQuickItem*>(rootObject);
|
|
|
|
Helper::clearChildren(parentItem);
|
|
|
|
if (!parentItem) {
|
|
qWarning() << "[C++] Root object is not a QQuickItem";
|
|
return;
|
|
}
|
|
|
|
QQmlEngine* engine = qmlEngine(rootObject);
|
|
|
|
if (!engine) {
|
|
qWarning() << "[C++] Failed to get QQmlEngine";
|
|
return;
|
|
}
|
|
|
|
QQuickItem *createdComponent = 0;
|
|
loader->createComponent(page , parentItem, &createdComponent);
|
|
|
|
if (createdComponent) {
|
|
if (activity) {
|
|
activity->pageChanged();
|
|
activity = 0;
|
|
}
|
|
onPageLoaded();
|
|
}
|
|
};
|
|
|
|
doActionOnBlockAllowChangePaged = []{};
|
|
|
|
if (activity) {
|
|
activity->isAllowChangePage(doActionOnSuccessAllowChangePaged,doActionOnBlockAllowChangePaged);
|
|
} else {
|
|
doActionOnSuccessAllowChangePaged();
|
|
}
|
|
};
|
|
|
|
updateElement(rootObject,"MainView", updateFunc, 0);
|
|
}
|
|
|
|
quint64 getFreeDiskSpace(const QString &path, qint64*totalBytes = 0)
|
|
{
|
|
QStorageInfo storage(path);
|
|
|
|
if (!storage.isValid() || !storage.isReady()) {
|
|
qWarning() << "Неверный путь или диск не готов:" << path;
|
|
return 0;
|
|
}
|
|
|
|
if (storage.isReadOnly()) {
|
|
qWarning() << "Диск доступен только для чтения:" << path;
|
|
return 0;
|
|
}
|
|
|
|
if (totalBytes) {
|
|
*totalBytes = storage.bytesTotal();
|
|
}
|
|
|
|
return storage.bytesAvailable();
|
|
}
|
|
|
|
void FigmaAdapter::hideStorageInfo() {
|
|
if (clickableStorageInfo) {
|
|
clickableStorageInfo->setEnabled(true);
|
|
}
|
|
|
|
if (componentLabelAvailibleSize) {
|
|
componentLabelAvailibleSize->setVisible(false);
|
|
}
|
|
|
|
if (rectAvailibleSize) {
|
|
rectAvailibleSize->setVisible(false);
|
|
}
|
|
|
|
if (rectPassedBar){
|
|
rectPassedBar->setVisible(false);
|
|
}
|
|
|
|
if (labelAvailibleSizeButton) {
|
|
labelAvailibleSizeButton->setProperty("text", "Хранилище не задано");
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
void FigmaAdapter::showStorageInfo(const QString &path) {
|
|
if (clickableStorageInfo) {
|
|
clickableStorageInfo->setEnabled(false);
|
|
}
|
|
|
|
if (componentLabelAvailibleSize) {
|
|
componentLabelAvailibleSize->setVisible(true);
|
|
}
|
|
|
|
if (rectAvailibleSize){
|
|
rectAvailibleSize->setVisible(true);
|
|
}
|
|
|
|
if (rectPassedBar) {
|
|
rectPassedBar->setVisible(true);
|
|
}
|
|
|
|
qint64 totalBytes = 0;
|
|
quint64 freeBytes = getFreeDiskSpace(path, &totalBytes);
|
|
|
|
if (freeBytes < SettingsRepository::instance().memoryLimitMinBytes()) {
|
|
emit Signalizer::get().reachedLimitAvailibleMemortSize();
|
|
}
|
|
|
|
QString numberFormat = QString::number(freeBytes / (1024.0 * 1024.0 * 1024.0), 'f', 2);
|
|
|
|
if (labelAvailibleSizeButton) {
|
|
labelAvailibleSizeButton->setProperty("text", numberFormat + " Гб");
|
|
}
|
|
|
|
if (progressBarController) {
|
|
double percent = 1.0 *freeBytes / totalBytes;
|
|
percent = 1 - percent;
|
|
percent*=100;
|
|
QMetaObject::invokeMethod(progressBarController, "setPercent",
|
|
Q_ARG(QVariant,percent ));
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
void FigmaAdapter::updateStorageInfo()
|
|
{
|
|
QString path = SettingsRepository::instance().defaultProjectDirectory();
|
|
|
|
if (path == "") {
|
|
hideStorageInfo();
|
|
return;
|
|
}
|
|
|
|
showStorageInfo(path);
|
|
|
|
}
|
|
|
|
#include "httpproxyserver.h"
|
|
|
|
void FigmaAdapter::saveSettingClick()
|
|
{
|
|
qDebug() << "FigmaAdapter::saveSettingClick";
|
|
SettingsPageActivity* activitySettings = (SettingsPageActivity*)(activity);
|
|
|
|
// update projectdir
|
|
{
|
|
QFileInfo url(activitySettings->textProjectPath);
|
|
|
|
if (url.isDir()) {
|
|
SettingsRepository::instance().setDefaultProjectDirectory(activitySettings->textProjectPath);
|
|
}
|
|
}
|
|
|
|
{
|
|
QUrl url(activitySettings->textRemoteAddress);
|
|
|
|
if (url.isValid()) {
|
|
SettingsRepository::instance().setRemoteAddressUrl(activitySettings->textRemoteAddress);
|
|
QString host = url.host();
|
|
int port = url.port();
|
|
HttpProxyServer::Instance->setTargetHost(host);
|
|
HttpProxyServer::Instance->setTargetPort(port);
|
|
}
|
|
}
|
|
}
|
|
|
|
void FigmaAdapter::eventReceived(const QString &element, const QString &event)
|
|
{
|
|
qDebug() << "event: "<< event << " element: " << element;
|
|
}
|
|
|
|
void FigmaAdapter::componentCreated(QQuickItem *item)
|
|
{
|
|
|
|
}
|
|
|
|
void FigmaAdapter::errorOccurred(const QString &message)
|
|
{
|
|
qDebug()<< message;
|
|
}
|
|
|
|
|
|
void FigmaAdapter::headerEntered(QQuickItem *item)
|
|
{
|
|
if (item->isEnabled()) {
|
|
item->setProperty("color", QML::CORLOR_MENU_ITEM_SELECTED);
|
|
}
|
|
}
|
|
|
|
void FigmaAdapter::headerExited(QQuickItem *item, const QColor & color)
|
|
{
|
|
if (item->isEnabled()) {
|
|
item->setProperty("color", color);
|
|
}
|
|
}
|
|
|
|
void FigmaAdapter::Helper::clearChildren(QQuickItem *parentItem)
|
|
{
|
|
const auto children = parentItem->children();
|
|
|
|
if (children.size()) {
|
|
|
|
for (QObject* child : children) {
|
|
if (QQuickItem* item = qobject_cast<QQuickItem*>(child)) {
|
|
item->setParentItem(nullptr);
|
|
item->setParent(nullptr);
|
|
item->deleteLater();
|
|
}
|
|
}
|
|
}
|
|
}
|