aboutsummaryrefslogtreecommitdiffstats
path: root/tests/manual/svg/svgmanager.cpp
blob: b8d49413a5e2f40b4a2d9990b7d83ffdd41cd5a2 (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
90
91
92
93
94
95
96
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "svgmanager.h"

#include <QDir>
#include <QtQuickVectorImageGenerator/private/qquickqmlgenerator_p.h>
#include <QTemporaryFile>

SvgManager *SvgManager::g_manager = nullptr;

SvgManager::SvgManager(QObject *parent)
    : QObject(parent)
{
    Q_ASSERT(g_manager == nullptr);
    g_manager = this;
    connect(this, &SvgManager::currentIndexChanged, this, &SvgManager::currentSourceChanged, Qt::QueuedConnection);
}

SvgManager::~SvgManager()
{
    Q_ASSERT(g_manager == this);
    g_manager = nullptr;
}

void SvgManager::setCurrentIndex(int newCurrentIndex)
{
    if (m_currentIndex == newCurrentIndex)
        return;
    m_currentIndex = newCurrentIndex;
    emit currentIndexChanged();
}

QList<QUrl> SvgManager::sources() const
{
    return m_sources;
}

QString SvgManager::currentDirectory() const
{
    return m_currentDirectory;
}

void SvgManager::setCurrentDirectory(const QString &newCurrentDirectory)
{
    if (m_currentDirectory == newCurrentDirectory)
        return;
    m_currentDirectory = newCurrentDirectory;
    emit currentDirectoryChanged();

    m_sources.clear();
    if (!m_currentDirectory.isEmpty()) {
        QDir dir(m_currentDirectory);
        QList<QFileInfo> infos = dir.entryInfoList(QStringList() << QStringLiteral("*.svg"));

        for (const QFileInfo &info : infos)
            m_sources.append(QUrl::fromLocalFile(info.absoluteFilePath()));
    }
    m_currentIndex = m_sources.isEmpty() ? -1 : 0;
    emit sourcesChanged();
    emit currentIndexChanged();
}

QString SvgManager::qmlSource() const
{
    QTemporaryFile tempFile;
    if (tempFile.open()) {
        QString name = tempFile.fileName();
        {
            QQuickQmlGenerator generator(currentSource().toLocalFile(), QQuickVectorImageGenerator::CurveRenderer, tempFile.fileName());
            generator.setCommentString(QStringLiteral("Generated"));
            generator.generate();
        }
        tempFile.close();

        QFile file(name);
        if (file.open(QIODevice::ReadOnly))
            return QString::fromUtf8(file.readAll());

    }

    return QStringLiteral("import QtQuick\nRectangle { width: 100; height: 100; color: \"red\" }");;
}

qreal SvgManager::scale() const
{
    return m_scale;
}

void SvgManager::setScale(int newScale)
{
    if (qFuzzyCompare(m_scale, newScale))
        return;
    m_scale = newScale;
    emit scaleChanged();
}