2017-09-19 16:12:00 +02:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPLv3+
|
|
|
|
* License-Filename: LICENSES/GPLv3+-ImageRegistry
|
|
|
|
*/
|
|
|
|
|
2017-09-19 16:08:30 +02:00
|
|
|
/*
|
2014-08-26 15:18:30 +02:00
|
|
|
* Copyright 2012, Christian Muehlhaeuser <muesli@tomahawk-player.org>
|
2017-09-19 16:08:30 +02:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2014-08-26 15:18:30 +02:00
|
|
|
|
|
|
|
#include "ImageRegistry.h"
|
|
|
|
|
|
|
|
#include <QSvgRenderer>
|
|
|
|
#include <QPainter>
|
2017-09-19 16:08:30 +02:00
|
|
|
#include <qicon.h>
|
2014-08-26 15:18:30 +02:00
|
|
|
|
|
|
|
#include "utils/Logger.h"
|
|
|
|
|
|
|
|
static QHash< QString, QHash< int, QHash< qint64, QPixmap > > > s_cache;
|
2017-09-19 16:08:30 +02:00
|
|
|
ImageRegistry* ImageRegistry::s_instance = 0;
|
2014-08-26 15:18:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
ImageRegistry*
|
|
|
|
ImageRegistry::instance()
|
|
|
|
{
|
|
|
|
return s_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ImageRegistry::ImageRegistry()
|
|
|
|
{
|
|
|
|
s_instance = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QIcon
|
2017-09-19 16:08:30 +02:00
|
|
|
ImageRegistry::icon( const QString& image, TomahawkUtils::ImageMode mode )
|
2014-08-26 15:18:30 +02:00
|
|
|
{
|
2017-09-19 16:08:30 +02:00
|
|
|
return pixmap( image, TomahawkUtils::defaultIconSize(), mode );
|
2014-08-26 15:18:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
qint64
|
2017-09-19 16:08:30 +02:00
|
|
|
ImageRegistry::cacheKey( const QSize& size, float opacity, QColor tint )
|
2014-08-26 15:18:30 +02:00
|
|
|
{
|
2017-09-19 16:08:30 +02:00
|
|
|
return size.width() * 100 + size.height() * 10 + ( opacity * 100.0 ) + tint.value();
|
2014-08-26 15:18:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QPixmap
|
2017-09-19 16:08:30 +02:00
|
|
|
ImageRegistry::pixmap( const QString& image, const QSize& size, TomahawkUtils::ImageMode mode, float opacity, QColor tint )
|
2014-08-26 15:18:30 +02:00
|
|
|
{
|
2017-09-19 16:08:30 +02:00
|
|
|
if ( size.width() < 0 || size.height() < 0 )
|
|
|
|
{
|
|
|
|
Q_ASSERT( false );
|
|
|
|
return QPixmap();
|
|
|
|
}
|
|
|
|
|
2014-08-26 15:18:30 +02:00
|
|
|
QHash< qint64, QPixmap > subsubcache;
|
|
|
|
QHash< int, QHash< qint64, QPixmap > > subcache;
|
|
|
|
|
|
|
|
if ( s_cache.contains( image ) )
|
|
|
|
{
|
|
|
|
subcache = s_cache.value( image );
|
|
|
|
|
|
|
|
if ( subcache.contains( mode ) )
|
|
|
|
{
|
|
|
|
subsubcache = subcache.value( mode );
|
|
|
|
|
|
|
|
const qint64 ck = cacheKey( size, opacity, tint );
|
|
|
|
if ( subsubcache.contains( ck ) )
|
|
|
|
{
|
|
|
|
return subsubcache.value( ck );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Image not found in cache. Let's load it.
|
|
|
|
QPixmap pixmap;
|
2017-09-19 16:08:30 +02:00
|
|
|
if ( image.toLower().endsWith( ".svg" ) )
|
2014-08-26 15:18:30 +02:00
|
|
|
{
|
|
|
|
QSvgRenderer svgRenderer( image );
|
2017-09-19 16:08:30 +02:00
|
|
|
QPixmap p( size.isNull() || size.height() == 0 || size.width() == 0 ? svgRenderer.defaultSize() : size );
|
2014-08-26 15:18:30 +02:00
|
|
|
p.fill( Qt::transparent );
|
|
|
|
|
|
|
|
QPainter pixPainter( &p );
|
|
|
|
pixPainter.setOpacity( opacity );
|
|
|
|
svgRenderer.render( &pixPainter );
|
|
|
|
pixPainter.end();
|
|
|
|
|
|
|
|
if ( tint.alpha() > 0 )
|
2017-09-19 16:08:30 +02:00
|
|
|
p = TomahawkUtils::tinted( p, tint );
|
2014-08-26 15:18:30 +02:00
|
|
|
|
|
|
|
pixmap = p;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pixmap = QPixmap( image );
|
|
|
|
|
|
|
|
if ( !pixmap.isNull() )
|
|
|
|
{
|
|
|
|
switch ( mode )
|
|
|
|
{
|
2017-09-19 16:08:30 +02:00
|
|
|
case TomahawkUtils::RoundedCorners:
|
|
|
|
pixmap = TomahawkUtils::createRoundedImage( pixmap, size );
|
2014-08-26 15:18:30 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !size.isNull() && pixmap.size() != size )
|
2017-09-19 16:08:30 +02:00
|
|
|
{
|
|
|
|
if ( size.width() == 0 )
|
|
|
|
{
|
|
|
|
pixmap = pixmap.scaledToHeight( size.height(), Qt::SmoothTransformation );
|
|
|
|
}
|
|
|
|
else if ( size.height() == 0 )
|
|
|
|
{
|
|
|
|
pixmap = pixmap.scaledToWidth( size.width(), Qt::SmoothTransformation );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pixmap = pixmap.scaled( size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
|
|
|
|
}
|
2014-08-26 15:18:30 +02:00
|
|
|
|
|
|
|
putInCache( image, size, mode, opacity, pixmap, tint );
|
|
|
|
}
|
|
|
|
|
|
|
|
return pixmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2017-09-19 16:08:30 +02:00
|
|
|
ImageRegistry::putInCache( const QString& image, const QSize& size, TomahawkUtils::ImageMode mode, float opacity, const QPixmap& pixmap, QColor tint )
|
2014-08-26 15:18:30 +02:00
|
|
|
{
|
2017-09-19 16:08:30 +02:00
|
|
|
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Adding to image cache:" << image << size << mode;
|
2014-08-26 15:18:30 +02:00
|
|
|
|
|
|
|
QHash< qint64, QPixmap > subsubcache;
|
|
|
|
QHash< int, QHash< qint64, QPixmap > > subcache;
|
|
|
|
|
|
|
|
if ( s_cache.contains( image ) )
|
|
|
|
{
|
|
|
|
subcache = s_cache.value( image );
|
|
|
|
|
|
|
|
if ( subcache.contains( mode ) )
|
|
|
|
{
|
|
|
|
subsubcache = subcache.value( mode );
|
|
|
|
|
|
|
|
/* if ( subsubcache.contains( size.width() * size.height() ) )
|
|
|
|
{
|
|
|
|
Q_ASSERT( false );
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
subsubcache.insert( cacheKey( size, opacity, tint ), pixmap );
|
|
|
|
subcache.insert( mode, subsubcache );
|
|
|
|
s_cache.insert( image, subcache );
|
|
|
|
}
|