aboutsummaryrefslogtreecommitdiffstats
path: root/tools/balsamui/converterthread.cpp
blob: 4307ba4ad4f404be6f33b8fbafe02cc061c7eaa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "converterthread.h"

#include <QtQuick3DAssetImport/private/qssgassetimportmanager_p.h>

#include <QFuture>
#include <QtConcurrent/QtConcurrent>

ConverterThread::ConverterThread(QObject *parent) : QThread(parent) { }

ConverterThread::~ConverterThread()
{
    mutex.lock();
    abort = true;
    condition.wakeOne();
    mutex.unlock();
    wait();
}

void ConverterThread::convert(QStringList filenames, QDir outputPath, QJsonObject options)
{
    QMutexLocker locker(&mutex);

    // Write settings
    m_filenames = filenames;
    m_outputPath = outputPath;
    m_options = options;

    if (!isRunning()) {
        start(LowPriority);
    } else {
        condition.wakeOne();
    }
}

void ConverterThread::run()
{
    forever {
        if (abort)
            return;

        // Copy settings
        mutex.lock();
        auto filenames = m_filenames;
        auto outputPath = m_outputPath;
        auto options = m_options;
        mutex.unlock();

        emit convertStart(QString("Converting %1 files...").arg(filenames.size()));

        std::atomic<int> failCounter = 0;
        std::atomic<int> fileCounter = 0;
        const int numFiles = filenames.size();

        auto convertFile = [&](const QString &filename) {
            if (abort)
                return;
            QString error;
            QSSGAssetImportManager assetImporter;
            assetImporter.importFile(filename, outputPath, options, &error);
            const size_t ctr = ++fileCounter;
            failCounter += error.isEmpty() ? 0 : 1;

            if (!error.isEmpty())
                emit convertUpdate(
                        QString("[%1/%2] Failed to convert '%3': %4").arg(QString::number(ctr), QString::number(numFiles), filename, error));
            else
                emit convertUpdate(
                        QString("[%1/%2] Successfully converted '%3'").arg(QString::number(ctr), QString::number(numFiles), filename));
        };

        // Convert in parallel
        QtConcurrent::blockingMap(filenames, convertFile);

        if (failCounter > 0)
            emit convertDone(QString("\nConversion done, failed to convert %1 of %2 files")
                                     .arg(QString::number(failCounter), QString::number(numFiles)));
        else
            emit convertDone(QString("\nSuccessfully converted all files!"));

        // Abort or wait for signal
        mutex.lock();
        if (!abort)
            condition.wait(&mutex);
        mutex.unlock();
    }
}