211 lines
6.1 KiB
C++
211 lines
6.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>
|
||
|
||
|
||
class DebugStreamBuf : public std::streambuf {
|
||
public:
|
||
DebugStreamBuf() = default;
|
||
protected:
|
||
// Переопределяем метод для обработки вывода
|
||
QMessageLogger * logger = 0;
|
||
std::stringstream ss;
|
||
int overflow(int c) override {
|
||
if (c != EOF) {
|
||
if (logger) {
|
||
if (c == '\n' || c == '\0') {
|
||
auto str = QString::fromStdString(ss.str());
|
||
logger->debug() << str;
|
||
delete logger;
|
||
ss.str("");
|
||
logger = new QMessageLogger(static_cast<const char*>("D:\\projects\\Qt\\CADTest\\QtApp\\main.cpp"), 16, static_cast<const char*>(__FUNCSIG__));
|
||
}
|
||
}
|
||
else {
|
||
logger = new QMessageLogger(static_cast<const char*>("D:\\projects\\Qt\\CADTest\\QtApp\\main.cpp"), 16, static_cast<const char*>(__FUNCSIG__));
|
||
|
||
}
|
||
ss << static_cast<char>(c);
|
||
//logger->debug().nospace() << static_cast<char>(c);
|
||
// Преобразуем символ в строку и выводим через qDebug
|
||
}
|
||
return c;
|
||
}
|
||
// Переопределяем метод для обработки строки
|
||
int sync() override {
|
||
return 0; // Успешно
|
||
}
|
||
};
|
||
|
||
|
||
// Потокобезопасный логгер для 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);
|
||
// out; // <-- здесь задаем кодировку
|
||
|
||
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[]) {
|
||
DebugStreamBuf debugStreamBuf;
|
||
std::ostream debugStream(&debugStreamBuf);
|
||
// Перенаправляем std::cout на наш поток
|
||
std::streambuf* originalCoutBuffer = std::cout.rdbuf(debugStream.rdbuf());
|
||
|
||
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 (argc > 1) {
|
||
static QDir dir(QCoreApplication::applicationDirPath());
|
||
static QFile logFile(dir.absoluteFilePath("app_release.log"));
|
||
if(logFile.exists()){
|
||
logFile.remove();
|
||
}
|
||
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");
|
||
|
||
if (figmaAdapter) {
|
||
if (SettingsRepository::instance().defaultProjectDirectory() == "") {
|
||
QString path = app.applicationDirPath();
|
||
|
||
// Добавляем подпапку "project"
|
||
QDir dir(path);
|
||
QString projectSubDir = dir.filePath("projects");
|
||
|
||
// Пытаемся создать папку, если её нет
|
||
if (!dir.exists("projects")) {
|
||
if (!dir.mkdir("projects")) {
|
||
qWarning() << "Не удалось создать директорию:" << projectSubDir;
|
||
}
|
||
else {
|
||
SettingsRepository::instance().setDefaultProjectDirectory(projectSubDir);
|
||
}
|
||
}
|
||
}
|
||
|
||
figmaAdapter->Init();
|
||
}
|
||
|
||
// startHTTPServer();
|
||
return app.exec();
|
||
}
|
||
|
||
|
||
QColor colorFromHex(const QString &hex) {
|
||
// Убедитесь, что строка начинается с '#'
|
||
if (hex.startsWith('#')) {
|
||
QString hexWithoutHash = hex.mid(1); // Удаляем '#'
|
||
bool ok;
|
||
// Преобразуем строку в целое число
|
||
bool isAlpha = hexWithoutHash.size() == 8;
|
||
|
||
uint colorValue = hexWithoutHash.toUInt(&ok, isAlpha == true? 16:16);
|
||
if (ok) {
|
||
// Создаем QColor из ARGB
|
||
if(isAlpha){
|
||
return QColor::fromRgba(colorValue);
|
||
}else{
|
||
return QColor::fromRgb(colorValue);
|
||
}
|
||
|
||
}
|
||
}
|
||
// Возвращаем черный цвет по умолчанию, если произошла ошибка
|
||
return QColor(0, 0, 0);
|
||
}
|