QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskGraphicProvider.cpp
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#include "QskGraphicProvider.h"
7#include "QskGraphicProviderMap.h"
8#include "QskGraphic.h"
9#include "QskSkinManager.h"
10#include "QskSkin.h"
11
12#include <qmutex.h>
13#include <qcache.h>
14#include <qdebug.h>
15#include <qurl.h>
16#include <qglobalstatic.h>
17
18Q_GLOBAL_STATIC( QskGraphicProviderMap, qskGraphicProviders )
19
20class QskGraphicProvider::PrivateData
21{
22 public:
23 // caching of graphics
24 QCache< QString, const QskGraphic > cache;
25 QMutex mutex;
26};
27
28QskGraphicProvider::QskGraphicProvider( QObject* parent )
29 : QObject( parent )
30 , m_data( new PrivateData() )
31{
32}
33
34QskGraphicProvider::~QskGraphicProvider()
35{
36}
37
38void QskGraphicProvider::setCacheSize( int size )
39{
40 if ( size < 0 )
41 size = 0;
42
43 QMutexLocker locker( &m_data->mutex );
44 m_data->cache.setMaxCost( size );
45}
46
47int QskGraphicProvider::cacheSize() const
48{
49 QMutexLocker locker( &m_data->mutex );
50 return m_data->cache.maxCost();
51}
52
53void QskGraphicProvider::clearCache()
54{
55 QMutexLocker locker( &m_data->mutex );
56 m_data->cache.clear();
57}
58
59const QskGraphic* QskGraphicProvider::requestGraphic( const QString& id ) const
60{
61 const QskGraphic* graphic = nullptr;
62
63 {
64 QMutexLocker locker( &m_data->mutex );
65 graphic = m_data->cache.object( id );
66 }
67
68 if ( graphic == nullptr )
69 {
70 graphic = loadGraphic( id );
71
72 if ( graphic == nullptr )
73 {
74 qWarning() << "QskGraphicProvider: can't load" << id;
75 return nullptr;
76 }
77
78 {
79 QMutexLocker locker( &m_data->mutex );
80
81 if( auto cached = m_data->cache.object( id ) )
82 {
83 delete graphic;
84 graphic = cached;
85 }
86 else
87 {
88 const int cost = 1; // TODO ...
89 m_data->cache.insert( id, graphic, cost );
90 }
91 }
92 }
93
94 return graphic;
95}
96
97void Qsk::addGraphicProvider(
98 const QString& providerId, QskGraphicProvider* provider )
99{
100 if ( qskGraphicProviders )
101 qskGraphicProviders->insert( providerId, provider );
102}
103
104QskGraphicProvider* Qsk::graphicProvider( const QString& providerId )
105{
106 if ( auto skin = qskSkinManager->skin() )
107 {
108 if ( auto provider = skin->graphicProvider( providerId ) )
109 return provider;
110 }
111
112 if ( qskGraphicProviders )
113 return qskGraphicProviders->provider( providerId );
114
115 return nullptr;
116}
117
118QskGraphic Qsk::loadGraphic( const char* source )
119{
120 return loadGraphic( QUrl( source ) );
121}
122
123QskGraphic Qsk::loadGraphic( const QUrl& url )
124{
125 static QskGraphic nullGraphic;
126
127 QString imageId = url.toString( QUrl::RemoveScheme |
128 QUrl::RemoveAuthority | QUrl::NormalizePathSegments );
129
130 if ( imageId.isEmpty() )
131 return nullGraphic;
132
133 if ( imageId[ 0 ] == '/' )
134 imageId = imageId.mid( 1 );
135
136 const QString providerId = url.host();
137
138 const QskGraphic* graphic = nullptr;
139
140 if ( const auto provider = Qsk::graphicProvider( providerId ) )
141 graphic = provider->requestGraphic( imageId );
142
143 return graphic ? *graphic : nullGraphic;
144}
145
146#include "moc_QskGraphicProvider.cpp"
A paint device for scalable graphics.
Definition QskGraphic.h:28