350 lines
10 KiB
C++
350 lines
10 KiB
C++
#include "formatconverter.h"
|
||
#include <QStringList>
|
||
#include <QFileInfo>
|
||
#include "formatfactoryregistry.h"
|
||
#include "ptxformatfactory.h"
|
||
#include "QDir"
|
||
#include "QDirIterator"
|
||
#include "workermanager.h"
|
||
#include "QLibrary"
|
||
#include "Monitor.h"
|
||
#include "converterdata.h"
|
||
|
||
#include <QFile>
|
||
#include <QTextStream>
|
||
#include <QDir>
|
||
#include <QDebug>
|
||
#include "QCoreApplication"
|
||
|
||
FormatConverter::FormatConverter(QObject *parent) : QObject(parent)
|
||
{}
|
||
|
||
QString getLogFile(const QString& fileName) {
|
||
QString appDir = QCoreApplication::applicationDirPath();
|
||
|
||
// Путь к целевой папке (например: ./logs/)
|
||
QString targetDirPath = appDir + QDir::separator() + "logs";
|
||
|
||
QDir targetDir;
|
||
|
||
// Создаём папку, если её нет
|
||
if (!targetDir.exists(targetDirPath)) {
|
||
if (!targetDir.mkpath(targetDirPath)) {
|
||
qWarning() << "Не удалось создать папку:" << targetDirPath;
|
||
return "";
|
||
}
|
||
}
|
||
QString outputFilePath = targetDir.absoluteFilePath(fileName);
|
||
|
||
return outputFilePath;
|
||
}
|
||
|
||
bool mergeLogsToOneFile(const QStringList& logFilePaths, const QString& outputFilePath) {
|
||
// Открываем выходной файл для записи
|
||
QFile outFile(outputFilePath);
|
||
if (!outFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
|
||
qWarning() << "Не удалось открыть файл для записи:" << outputFilePath;
|
||
return false;
|
||
}
|
||
|
||
QTextStream out(&outFile);
|
||
|
||
bool success = true;
|
||
|
||
// Проходим по всем файлам
|
||
for (const QString& filePath : logFilePaths) {
|
||
QFile inFile(filePath);
|
||
if (!inFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||
qWarning() << "Не удалось открыть файл для чтения:" << filePath;
|
||
success = false;
|
||
continue;
|
||
}
|
||
|
||
// Добавляем имя файла как заголовок (опционально)
|
||
out << "=== Лог из файла: " << filePath << " ===\n";
|
||
|
||
// Читаем и пишем содержимое
|
||
while (!inFile.atEnd()) {
|
||
QByteArray line = inFile.readLine();
|
||
out << QString::fromUtf8(line); // можно использовать fromLocal8Bit(), если нужно
|
||
}
|
||
|
||
out << "\n\n"; // Разделитель между логами
|
||
inFile.close();
|
||
}
|
||
|
||
outFile.close();
|
||
return success;
|
||
}
|
||
|
||
void FormatConverter::convert(QStringList listPathToConvert, const QString& projectPath)
|
||
{
|
||
ConverterData::Free();
|
||
this->projectPath = projectPath;
|
||
if(ensureLasSubdir(projectPath)) return;
|
||
|
||
sortBySuffix(listPathToConvert);
|
||
FormatFactoryRegistry& registry = FormatFactoryRegistry::Get();
|
||
registry.registerFactory("pts", std::make_shared<PtsFormatFactory>());
|
||
registry.registerFactory("las", std::make_shared<LasFormatFactory>());
|
||
startHandling();
|
||
}
|
||
|
||
int FormatConverter::ensureLasSubdir(const QString& projectPath) {
|
||
QFileInfo info(projectPath);
|
||
if (!info.isDir()) return 1;
|
||
|
||
QDir projectDir(projectPath);
|
||
projectLasPath = projectDir.filePath("las");
|
||
|
||
QDir lasDir(projectLasPath);
|
||
if (lasDir.exists()) {
|
||
// Очистить содержимое директории las
|
||
lasDir.setFilter(QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs);
|
||
for (const QFileInfo& fileInfo : lasDir.entryInfoList()) {
|
||
if (fileInfo.isDir()) {
|
||
QDir subDir(fileInfo.absoluteFilePath());
|
||
subDir.removeRecursively();
|
||
} else {
|
||
lasDir.remove(fileInfo.fileName());
|
||
}
|
||
}
|
||
} else {
|
||
// Создать директорию las
|
||
projectDir.mkdir("las");
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
void FormatConverter::convertToLasFinished()
|
||
{
|
||
qDebug() << "start convert from las finished";
|
||
|
||
QMap<QString,Monitor*>& monitorList = Monitor::getMonitorList();
|
||
auto it = monitorList.begin();
|
||
auto itEnd = monitorList.end();
|
||
|
||
bool isAllFinishedSuccessfull = true;
|
||
|
||
QStringList logFilePath;
|
||
QStringList failedFilesConvert;
|
||
|
||
while (it != itEnd) {
|
||
if (it.value()->isSuccessFinished() == false) {
|
||
auto monitor = it.value();
|
||
QFile file = ConverterData::Get().getTempFile(QString::fromStdString(monitor->getLogFileName()));
|
||
QString path = file.fileName();
|
||
logFilePath.append(path);
|
||
qDebug() <<"Path error log" << path;
|
||
isAllFinishedSuccessfull = false;
|
||
failedFilesConvert.append(it.value()->getListFiles());
|
||
}
|
||
++it;
|
||
}
|
||
|
||
it = monitorList.begin();
|
||
|
||
while (it != itEnd) {
|
||
delete it.value();
|
||
++it;
|
||
}
|
||
|
||
monitorList.clear();
|
||
|
||
if (isAllFinishedSuccessfull) {
|
||
emit convertedFinished();
|
||
} else {
|
||
QString outputlogFile = getLogFile("latest.log");
|
||
mergeLogsToOneFile(logFilePath, outputlogFile);
|
||
emit convertedFinishedWithError(failedFilesConvert, outputlogFile);
|
||
}
|
||
}
|
||
|
||
void FormatConverter::stopAllConverting()
|
||
{
|
||
convertToLas = nullptr;
|
||
WorkerManager& manager = WorkerManager::get();
|
||
manager.stopAll();
|
||
}
|
||
|
||
void FormatConverter::allWorkerFinished()
|
||
{
|
||
if (convertToLas != nullptr) {
|
||
convertToLas();
|
||
}
|
||
}
|
||
|
||
void FormatConverter::percentChangeHandler(double percent)
|
||
{
|
||
QMap<QString,Monitor*>& monitorList = Monitor::getMonitorList();
|
||
auto monitorIt = monitorList.begin();
|
||
auto monitorItEnd = monitorList.end();
|
||
|
||
double totalePercent = 0;
|
||
|
||
while (monitorIt != monitorItEnd) {
|
||
totalePercent += monitorIt.value()->getPercentComplete();
|
||
++monitorIt;
|
||
}
|
||
|
||
totalePercent /= monitorList.count();
|
||
|
||
qDebug()<< "totalePercent:" << totalePercent;
|
||
|
||
emit percentCompetedChanged(totalePercent);
|
||
}
|
||
|
||
QString removeFilePrefix(const QString& path) {
|
||
return path.startsWith("file:///")
|
||
? path.mid(8)
|
||
: path;
|
||
}
|
||
|
||
void FormatConverter::sortBySuffix(const QStringList& listPathToConvert)
|
||
{
|
||
mapFileSuffix.clear();
|
||
|
||
for (const QString& path : listPathToConvert) {
|
||
QFileInfo fileInfo(path);
|
||
const QString suffix = fileInfo.suffix().toLower();
|
||
|
||
QString filePath = fileInfo.path();//absoluteFilePath();
|
||
filePath = removeFilePrefix(filePath);
|
||
QString fileName = fileInfo.fileName();
|
||
QString fileP = filePath + "/" + fileName;
|
||
|
||
if (!suffix.isEmpty()) {
|
||
mapFileSuffix[suffix].append(fileP);
|
||
}
|
||
}
|
||
}
|
||
|
||
QStringList getFilesWithSuffixes(const QString &folderPath, const QStringList &suffixes)
|
||
{
|
||
QStringList files;
|
||
QDirIterator it(folderPath, QDir::Files | QDir::NoSymLinks, QDirIterator::Subdirectories);
|
||
while (it.hasNext()) {
|
||
it.next();
|
||
QFileInfo fileInfo = it.fileInfo();
|
||
QString suffix = fileInfo.suffix().toLower();
|
||
if (suffixes.contains(suffix)) {
|
||
files << fileInfo.absoluteFilePath();
|
||
}
|
||
}
|
||
return files;
|
||
}
|
||
|
||
void FormatConverter::startHandling()
|
||
{
|
||
listlasFormats.clear();
|
||
listlazFormats.clear();
|
||
WorkerManager::recreate();
|
||
QMap<QString,Monitor*>& monitorList = Monitor::getMonitorList();
|
||
monitorList.insert("CLAS", new Monitor());
|
||
|
||
QStringList unsupportedSuffixs;
|
||
|
||
auto it = mapFileSuffix.cbegin();
|
||
auto itEnd = mapFileSuffix.cend();
|
||
|
||
FormatFactoryRegistry& registry = FormatFactoryRegistry::Get();
|
||
|
||
std::shared_ptr<IFormatFactory> factory;
|
||
|
||
bool isOnlyLas = true;
|
||
while(it != itEnd) {
|
||
if (it.key() == "las") {
|
||
listlasFormats = it.value();
|
||
++it;
|
||
continue;
|
||
}
|
||
|
||
if (it.key() == "laz") {
|
||
listlazFormats = it.value();
|
||
++it;
|
||
continue;
|
||
}
|
||
|
||
isOnlyLas = false;
|
||
factory = registry.getFactory(it.key());
|
||
if (factory) {
|
||
factory->process(it.value(), projectLasPath);
|
||
} else {
|
||
unsupportedSuffixs.append(*it);
|
||
}
|
||
++it;
|
||
}
|
||
|
||
WorkerManager& manager = WorkerManager::get();
|
||
connect(&manager, &WorkerManager::allWorkerFinished, this, &FormatConverter::allWorkerFinished);
|
||
|
||
factory = registry.getFactory("las");
|
||
|
||
// подписываемся на события обновления прцоентов
|
||
auto monitorIt = monitorList.begin();
|
||
auto monitorItEnd = monitorList.end();
|
||
while (monitorIt != monitorItEnd) {
|
||
connect(monitorIt.value(), &Monitor::percentCompleteChange, this, &FormatConverter::percentChangeHandler);
|
||
++monitorIt;
|
||
}
|
||
|
||
|
||
if (factory) {
|
||
convertToLas = [this, factory] {
|
||
|
||
WorkerManager& manager = WorkerManager::get();
|
||
|
||
if (manager.isWorkerWasStarted) {
|
||
auto isAllWorkersFinishedSuccessfull = [this](WorkerManager& manager)
|
||
{
|
||
auto setWorkers = manager.getFinishedWowrkers();
|
||
auto workerIt = setWorkers.begin();
|
||
auto workerItEnd = setWorkers.end();
|
||
|
||
while (workerIt != workerItEnd) {
|
||
auto monitor = (*workerIt).get()->getMonitor();
|
||
if (monitor->isSuccessFinished()) return false;
|
||
++workerIt;
|
||
}
|
||
|
||
return true;
|
||
};
|
||
|
||
if (isAllWorkersFinishedSuccessfull(manager)) {
|
||
convertToLasFinished();
|
||
return;
|
||
}
|
||
manager.isWorkerWasStarted = false;
|
||
}
|
||
|
||
QStringList lasList;
|
||
lasList +=listlasFormats;
|
||
lasList +=listlazFormats;
|
||
lasList +=getFilesWithSuffixes(projectLasPath,{"las","laz"});
|
||
|
||
if (lasList.size() == 0) {
|
||
qDebug() << "LasList empty nothing convert";
|
||
return;
|
||
}
|
||
|
||
factory->process(lasList,projectPath);
|
||
|
||
convertToLas = [this] {
|
||
convertToLasFinished();
|
||
};
|
||
|
||
|
||
manager.startWorkers();
|
||
};
|
||
|
||
if (isOnlyLas) {
|
||
convertToLas();
|
||
return;
|
||
}
|
||
}
|
||
|
||
qDebug() << "start convert to las";
|
||
manager.startWorkers();
|
||
}
|