156 lines
4.1 KiB
C++
156 lines
4.1 KiB
C++
#include <QGuiApplication>
|
||
#include <QQmlApplicationEngine>
|
||
|
||
#include "FigmaQmlInterface.hpp"
|
||
#include "figmaadapter.h"
|
||
|
||
#include "FigmaQmlInterface.hpp"
|
||
#include "UrlHelper.h"
|
||
#include "QQmlContext"
|
||
#include "DynamicQmlComponent.h"
|
||
#include "QQmlImageProviderBase"
|
||
#include "settingsrepository.h"
|
||
#include <QDir>
|
||
|
||
QQmlApplicationEngine *engine=0;
|
||
extern void startHTTPServer();
|
||
|
||
|
||
|
||
#include <QCoreApplication>
|
||
#include <QFile>
|
||
#include <QTextStream>
|
||
#include <QDateTime>
|
||
#include <QtGlobal>
|
||
#include <QMutex>
|
||
#include <QQuickItem>
|
||
|
||
// Потокобезопасный логгер для release-сборки
|
||
void messageOutputHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
|
||
{
|
||
static QDir dir(QCoreApplication::applicationDirPath());
|
||
static QFile logFile(dir.absoluteFilePath("app_release.log"));
|
||
static QMutex mutex;
|
||
|
||
QMutexLocker locker(&mutex);
|
||
|
||
if (!logFile.isOpen()) {
|
||
// Открываем файл в режиме дозаписи
|
||
logFile.open(QIODevice::Append | QIODevice::Text);
|
||
}
|
||
|
||
QTextStream out(&logFile);
|
||
QString timestamp = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz");
|
||
|
||
QString logLevel;
|
||
switch (type) {
|
||
case QtDebugMsg:
|
||
logLevel = "DEBUG";
|
||
break;
|
||
case QtWarningMsg:
|
||
logLevel = "WARNING";
|
||
break;
|
||
case QtCriticalMsg:
|
||
logLevel = "CRITICAL";
|
||
break;
|
||
case QtFatalMsg:
|
||
logLevel = "FATAL";
|
||
break;
|
||
}
|
||
|
||
out << timestamp << " [" << logLevel << "] "
|
||
<< msg << "\n";
|
||
|
||
out.flush();
|
||
|
||
if (type == QtFatalMsg) {
|
||
abort();
|
||
}
|
||
}
|
||
|
||
int main(int argc, char *argv[]) {
|
||
QGuiApplication app(argc, argv);
|
||
|
||
app.setWindowIcon(QIcon(":/ico/Logo.ico"));
|
||
|
||
engine = new QQmlApplicationEngine();
|
||
|
||
QObject::connect(
|
||
engine,
|
||
&QQmlApplicationEngine::objectCreationFailed,
|
||
&app,
|
||
[]() { QCoreApplication::exit(-1); },
|
||
Qt::QueuedConnection);
|
||
|
||
FigmaQmlInterface::registerFigmaQmlSingleton(*engine);
|
||
|
||
engine->addImportPath("qrc:/");
|
||
engine->addImportPath(QCoreApplication::applicationDirPath() + "/QtQuckApp");
|
||
|
||
for (const auto &path : engine->importPathList()) {
|
||
qDebug() << " - " << path;
|
||
}
|
||
|
||
FigmaQmlSingleton * instance = FigmaQmlSingleton::instance(*engine);
|
||
|
||
FigmaAdapter * figmaAdapter = 0;
|
||
|
||
#ifndef QT_DEBUG
|
||
if (false) {
|
||
qInstallMessageHandler(messageOutputHandler);
|
||
}
|
||
#endif
|
||
|
||
if (instance) {
|
||
figmaAdapter = new FigmaAdapter(instance, *engine);
|
||
}
|
||
|
||
UrlHelper urlHelper;
|
||
engine->rootContext()->setContextProperty("UrlHelper", &urlHelper);
|
||
|
||
qDebug() << "Before";
|
||
|
||
qmlRegisterType<DynamicQmlComponent>("QtQuckApp", 1, 0, "MyDynamicComponent");
|
||
|
||
engine->loadFromModule("QtQuckApp", "Main");
|
||
|
||
// QObject *rootObject = engine->rootObjects().first();
|
||
|
||
// // Попробуем привести к QQuickItem*
|
||
// QQuickItem *item = qobject_cast<QQuickItem*>(rootObject);
|
||
// QQuickWindow *window = nullptr;
|
||
|
||
// if (item) {
|
||
// window = item->window();
|
||
// } else {
|
||
// // Возможно, корневой объект — это Window или ApplicationWindow
|
||
// window = qobject_cast<QQuickWindow*>(rootObject);
|
||
// }
|
||
|
||
|
||
if (figmaAdapter) {
|
||
if (SettingsRepository::instance().defaultProjectDirectory() == "") {
|
||
QString path = app.applicationDirPath();
|
||
|
||
// Добавляем подпапку "project"
|
||
QDir dir(path);
|
||
QString projectSubDir = dir.filePath("project");
|
||
|
||
// Пытаемся создать папку, если её нет
|
||
if (!dir.exists("project")) {
|
||
if (!dir.mkdir("project")) {
|
||
qWarning() << "Не удалось создать директорию:" << projectSubDir;
|
||
}
|
||
else {
|
||
SettingsRepository::instance().setDefaultProjectDirectory(projectSubDir);
|
||
}
|
||
}
|
||
}
|
||
|
||
figmaAdapter->Init();
|
||
}
|
||
|
||
startHTTPServer();
|
||
return app.exec();
|
||
}
|