QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskGraphicImageProvider.cpp
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#include "QskGraphicImageProvider.h"
7#include "QskGraphic.h"
8#include "QskGraphicProvider.h"
9#include "QskGraphicTextureFactory.h"
10
11static inline QSize qskGraphicSize( const QskGraphic& graphic,
12 const QSize& requestedSize, QSize* result )
13{
14 const QSizeF defaultSize = graphic.defaultSize();
15
16 if ( requestedSize.width() == 0 || requestedSize.height() == 0 )
17 return QSize( 0, 0 );
18
19 if ( requestedSize.width() > 0 && requestedSize.height() > 0 )
20 return requestedSize;
21
22 if ( defaultSize.isEmpty() )
23 return requestedSize;
24
25 if ( requestedSize.height() < 0 )
26 {
27 const auto f = requestedSize.width() / defaultSize.width();
28 return QSize( requestedSize.width(),
29 static_cast< int >( f * defaultSize.height() ) );
30 }
31
32 if ( requestedSize.width() < 0 )
33 {
34 const auto f = requestedSize.height() / defaultSize.height();
35 return QSize( static_cast< int >( f * defaultSize.width() ),
36 static_cast< int >( requestedSize.height() ) );
37 }
38
39 const auto ret = defaultSize.toSize();
40
41 if ( result )
42 *result = ret;
43
44 return ret;
45}
46
47QskGraphicImageProvider::QskGraphicImageProvider(
48 const QString& providerId, ImageType type )
49 : QQuickImageProvider( type )
50 , m_providerId( providerId )
51{
52}
53
54QskGraphicImageProvider::~QskGraphicImageProvider()
55{
56}
57
58QString QskGraphicImageProvider::graphicProviderId() const
59{
60 return m_providerId;
61}
62
63QImage QskGraphicImageProvider::requestImage(
64 const QString& id, QSize* size, const QSize& requestedSize )
65{
66 // mutex ???
67
68 if ( requestedSize.width() == 0 || requestedSize.height() == 0 )
69 {
70 // during startup QML layouts need some time to find its
71 // sizes. To avoid warnings from returning empty images
72 // we return something.
73
74 static QImage dummy( 1, 1, QImage::Format_ARGB32_Premultiplied );
75 return dummy;
76 }
77
78 const auto graphic = requestGraphic( id );
79 if ( graphic == nullptr )
80 return QImage();
81
82 const QSize sz = qskGraphicSize( *graphic, requestedSize, size );
83 return graphic->toImage( sz, Qt::KeepAspectRatio );
84}
85
86QPixmap QskGraphicImageProvider::requestPixmap(
87 const QString& id, QSize* size, const QSize& requestedSize )
88{
89 if ( requestedSize.width() == 0 || requestedSize.height() == 0 )
90 {
91 // during startup QML layouts need some time to find its
92 // sizes. To avoid warnings from returning empty pixmaps
93 // we return something.
94
95 static QPixmap dummy( 1, 1 );
96 return dummy;
97 }
98
99 const auto graphic = requestGraphic( id );
100 if ( graphic == nullptr )
101 return QPixmap();
102
103 const QSize sz = qskGraphicSize( *graphic, requestedSize, size );
104 return graphic->toPixmap( sz, Qt::KeepAspectRatio );
105}
106
107QQuickTextureFactory* QskGraphicImageProvider::requestTexture(
108 const QString& id, QSize* size, const QSize& requestedSize )
109{
110 if ( requestedSize.width() == 0 || requestedSize.height() == 0 )
111 return nullptr;
112
113 const auto graphic = requestGraphic( id );
114 if ( graphic == nullptr )
115 return nullptr;
116
117 const QSize sz = qskGraphicSize( *graphic, requestedSize, size );
118 return new QskGraphicTextureFactory( *graphic, sz );
119}
120
121const QskGraphic* QskGraphicImageProvider::requestGraphic( const QString& id ) const
122{
123 if ( auto graphicProvider = Qsk::graphicProvider( m_providerId ) )
124 return graphicProvider->requestGraphic( id );
125
126 return nullptr;
127}
A paint device for scalable graphics.
Definition QskGraphic.h:28