summaryrefslogtreecommitdiff
path: root/src/plugins/qmldesigner/designercore/imagecache/synchronousimagecache.cpp
blob: e9a07b2b311453511f8caacf80ceeb147174bda7 (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
97
98
99
100
101
102
103
104
105
// 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 "synchronousimagecache.h"

#include "imagecachecollectorinterface.h"
#include "imagecachestorage.h"
#include "timestampprovider.h"

#include <thread>

namespace QmlDesigner {

namespace {

Utils::PathString createId(Utils::PathString filePath, Utils::SmallString extraId)
{
    return extraId.empty() ? Utils::PathString{filePath}
                           : Utils::PathString::join({filePath, "+", extraId});
}
} // namespace

QImage SynchronousImageCache::image(Utils::PathString filePath,
                                    Utils::SmallString extraId,
                                    const ImageCache::AuxiliaryData &auxiliaryData)
{
    const auto id = createId(filePath, extraId);

    const auto timeStamp = m_timeStampProvider.timeStamp(filePath);
    const auto entry = m_storage.fetchImage(id, timeStamp);

    if (entry)
        return *entry;

    const auto &[image, midSizeImage, smallImage] = m_collector.createImage(filePath,
                                                                            extraId,
                                                                            auxiliaryData);

    m_storage.storeImage(id, timeStamp, image, midSizeImage, smallImage);

    return image;
}

QImage SynchronousImageCache::midSizeImage(Utils::PathString filePath,
                                           Utils::SmallString extraId,
                                           const ImageCache::AuxiliaryData &auxiliaryData)
{
    const auto id = createId(filePath, extraId);

    const auto timeStamp = m_timeStampProvider.timeStamp(filePath);
    const auto entry = m_storage.fetchMidSizeImage(id, timeStamp);

    if (entry)
        return *entry;

    const auto &[image, midSizeImage, smallImage] = m_collector.createImage(filePath,
                                                                            extraId,
                                                                            auxiliaryData);

    m_storage.storeImage(id, timeStamp, image, midSizeImage, smallImage);

    return midSizeImage;
}

QImage SynchronousImageCache::smallImage(Utils::PathString filePath,
                                         Utils::SmallString extraId,
                                         const ImageCache::AuxiliaryData &auxiliaryData)
{
    const auto id = createId(filePath, extraId);

    const auto timeStamp = m_timeStampProvider.timeStamp(filePath);
    const auto entry = m_storage.fetchSmallImage(id, timeStamp);

    if (entry)
        return *entry;

    const auto &[image, midSizeImage, smallImage] = m_collector.createImage(filePath,
                                                                            extraId,
                                                                            auxiliaryData);

    m_storage.storeImage(id, timeStamp, image, midSizeImage, smallImage);

    return smallImage;
}

QIcon SynchronousImageCache::icon(Utils::PathString filePath,
                                  Utils::SmallString extraId,
                                  const ImageCache::AuxiliaryData &auxiliaryData)
{
    const auto id = createId(filePath, extraId);

    const auto timeStamp = m_timeStampProvider.timeStamp(filePath);
    const auto entry = m_storage.fetchIcon(id, timeStamp);

    if (entry)
        return *entry;

    const auto icon = m_collector.createIcon(filePath, extraId, auxiliaryData);

    m_storage.storeIcon(id, timeStamp, icon);

    return icon;
}

} // namespace QmlDesigner