source: calamares/trunk/fuentes/src/libcalamaresui/utils/ImageRegistry.cpp @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 17 months ago

sync with github

File size: 4.9 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   SPDX-License-Identifier: GPLv3+
4 *   License-Filename: LICENSES/GPLv3+-ImageRegistry
5 */
6
7/*
8 *   Copyright 2012, Christian Muehlhaeuser <muesli@tomahawk-player.org>
9
10    This program is free software: you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation, either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22*/
23
24#include "ImageRegistry.h"
25
26#include <QSvgRenderer>
27#include <QPainter>
28#include <qicon.h>
29
30static QHash< QString, QHash< int, QHash< qint64, QPixmap > > > s_cache;
31ImageRegistry* ImageRegistry::s_instance = 0;
32
33
34ImageRegistry*
35ImageRegistry::instance()
36{
37    return s_instance;
38}
39
40
41ImageRegistry::ImageRegistry()
42{
43    s_instance = this;
44}
45
46
47QIcon
48ImageRegistry::icon( const QString& image, CalamaresUtils::ImageMode mode )
49{
50    return pixmap( image, CalamaresUtils::defaultIconSize(), mode );
51}
52
53
54qint64
55ImageRegistry::cacheKey( const QSize& size, float opacity, QColor tint )
56{
57    return size.width() * 100 + size.height() * 10 + ( opacity * 100.0 ) + tint.value();
58}
59
60
61QPixmap
62ImageRegistry::pixmap( const QString& image, const QSize& size, CalamaresUtils::ImageMode mode, float opacity, QColor tint )
63{
64    if ( size.width() < 0 || size.height() < 0 )
65    {
66        Q_ASSERT( false );
67        return QPixmap();
68    }
69
70    QHash< qint64, QPixmap > subsubcache;
71    QHash< int, QHash< qint64, QPixmap > > subcache;
72
73    if ( s_cache.contains( image ) )
74    {
75        subcache = s_cache.value( image );
76
77        if ( subcache.contains( mode ) )
78        {
79            subsubcache = subcache.value( mode );
80
81            const qint64 ck = cacheKey( size, opacity, tint );
82            if ( subsubcache.contains( ck ) )
83            {
84                return subsubcache.value( ck );
85            }
86        }
87    }
88
89    // Image not found in cache. Let's load it.
90    QPixmap pixmap;
91    if ( image.toLower().endsWith( ".svg" ) )
92    {
93        QSvgRenderer svgRenderer( image );
94        QPixmap p( size.isNull() || size.height() == 0 || size.width() == 0 ? svgRenderer.defaultSize() : size );
95        p.fill( Qt::transparent );
96
97        QPainter pixPainter( &p );
98        pixPainter.setOpacity( opacity );
99        svgRenderer.render( &pixPainter );
100        pixPainter.end();
101
102        if ( tint.alpha() > 0 )
103        {
104            QImage resultImage( p.size(), QImage::Format_ARGB32_Premultiplied );
105            QPainter painter( &resultImage );
106            painter.drawPixmap( 0, 0, p );
107            painter.setCompositionMode( QPainter::CompositionMode_Screen );
108            painter.fillRect( resultImage.rect(), tint );
109            painter.end();
110
111            resultImage.setAlphaChannel( p.toImage().alphaChannel() );
112            p = QPixmap::fromImage( resultImage );
113       }
114
115        pixmap = p;
116    }
117    else
118        pixmap = QPixmap( image );
119
120    if ( !pixmap.isNull() )
121    {
122        switch ( mode )
123        {
124            case CalamaresUtils::RoundedCorners:
125                pixmap = CalamaresUtils::createRoundedImage( pixmap, size );
126                break;
127
128            default:
129                break;
130        }
131
132        if ( !size.isNull() && pixmap.size() != size )
133        {
134            if ( size.width() == 0 )
135            {
136                pixmap = pixmap.scaledToHeight( size.height(), Qt::SmoothTransformation );
137            }
138            else if ( size.height() == 0 )
139            {
140                pixmap = pixmap.scaledToWidth( size.width(), Qt::SmoothTransformation );
141            }
142            else
143                pixmap = pixmap.scaled( size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
144        }
145
146        putInCache( image, size, mode, opacity, pixmap, tint );
147    }
148
149    return pixmap;
150}
151
152
153void
154ImageRegistry::putInCache( const QString& image, const QSize& size, CalamaresUtils::ImageMode mode, float opacity, const QPixmap& pixmap, QColor tint )
155{
156    QHash< qint64, QPixmap > subsubcache;
157    QHash< int, QHash< qint64, QPixmap > > subcache;
158
159    if ( s_cache.contains( image ) )
160    {
161        subcache = s_cache.value( image );
162
163        if ( subcache.contains( mode ) )
164        {
165            subsubcache = subcache.value( mode );
166
167/*            if ( subsubcache.contains( size.width() * size.height() ) )
168            {
169                Q_ASSERT( false );
170            }*/
171        }
172    }
173
174    subsubcache.insert( cacheKey( size, opacity, tint ), pixmap );
175    subcache.insert( mode, subsubcache );
176    s_cache.insert( image, subcache );
177}
Note: See TracBrowser for help on using the repository browser.