[libcalamaresui] Polish ImageRegistry

- do static initialization more carefully
 - float -> qreal (double) because that's what the Qt API expects,
   to reduce type-conversion warnings
 - apply current coding style
This commit is contained in:
Adriaan de Groot 2019-08-09 16:52:54 +02:00
parent ec08a293b2
commit 8a7f32d3aa
2 changed files with 42 additions and 26 deletions

View File

@ -2,6 +2,8 @@
* *
* SPDX-License-Identifier: GPLv3+ * SPDX-License-Identifier: GPLv3+
* License-Filename: LICENSES/GPLv3+-ImageRegistry * License-Filename: LICENSES/GPLv3+-ImageRegistry
*
* Copyright 2019, Adriaan de Groot <groot@kde.org>
*/ */
/* /*
@ -23,25 +25,22 @@
#include "ImageRegistry.h" #include "ImageRegistry.h"
#include <QSvgRenderer>
#include <QPainter> #include <QPainter>
#include <QSvgRenderer>
#include <qicon.h> #include <qicon.h>
static QHash< QString, QHash< int, QHash< qint64, QPixmap > > > s_cache; static QHash< QString, QHash< int, QHash< qint64, QPixmap > > > s_cache;
ImageRegistry* ImageRegistry::s_instance = 0;
ImageRegistry* ImageRegistry*
ImageRegistry::instance() ImageRegistry::instance()
{ {
static ImageRegistry* s_instance = new ImageRegistry();
return s_instance; return s_instance;
} }
ImageRegistry::ImageRegistry() ImageRegistry::ImageRegistry() {}
{
s_instance = this;
}
QIcon QIcon
@ -52,14 +51,18 @@ ImageRegistry::icon( const QString& image, CalamaresUtils::ImageMode mode )
qint64 qint64
ImageRegistry::cacheKey( const QSize& size, float opacity, QColor tint ) ImageRegistry::cacheKey( const QSize& size, qreal opacity, QColor tint )
{ {
return size.width() * 100 + size.height() * 10 + ( opacity * 100.0 ) + tint.value(); return size.width() * 100 + size.height() * 10 + static_cast< qint64 >( opacity * 100.0 ) + tint.value();
} }
QPixmap QPixmap
ImageRegistry::pixmap( const QString& image, const QSize& size, CalamaresUtils::ImageMode mode, float opacity, QColor tint ) ImageRegistry::pixmap( const QString& image,
const QSize& size,
CalamaresUtils::ImageMode mode,
qreal opacity,
QColor tint )
{ {
Q_ASSERT( !( size.width() < 0 || size.height() < 0 ) ); Q_ASSERT( !( size.width() < 0 || size.height() < 0 ) );
if ( size.width() < 0 || size.height() < 0 ) if ( size.width() < 0 || size.height() < 0 )
@ -115,18 +118,15 @@ ImageRegistry::pixmap( const QString& image, const QSize& size, CalamaresUtils::
pixmap = p; pixmap = p;
} }
else else
{
pixmap = QPixmap( image ); pixmap = QPixmap( image );
}
if ( !pixmap.isNull() ) if ( !pixmap.isNull() )
{ {
switch ( mode ) if ( mode == CalamaresUtils::RoundedCorners )
{ {
case CalamaresUtils::RoundedCorners:
pixmap = CalamaresUtils::createRoundedImage( pixmap, size ); pixmap = CalamaresUtils::createRoundedImage( pixmap, size );
break;
default:
break;
} }
if ( !size.isNull() && pixmap.size() != size ) if ( !size.isNull() && pixmap.size() != size )
@ -140,8 +140,10 @@ ImageRegistry::pixmap( const QString& image, const QSize& size, CalamaresUtils::
pixmap = pixmap.scaledToWidth( size.width(), Qt::SmoothTransformation ); pixmap = pixmap.scaledToWidth( size.width(), Qt::SmoothTransformation );
} }
else else
{
pixmap = pixmap.scaled( size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ); pixmap = pixmap.scaled( size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
} }
}
putInCache( image, size, mode, opacity, pixmap, tint ); putInCache( image, size, mode, opacity, pixmap, tint );
} }
@ -151,7 +153,12 @@ ImageRegistry::pixmap( const QString& image, const QSize& size, CalamaresUtils::
void void
ImageRegistry::putInCache( const QString& image, const QSize& size, CalamaresUtils::ImageMode mode, float opacity, const QPixmap& pixmap, QColor tint ) ImageRegistry::putInCache( const QString& image,
const QSize& size,
CalamaresUtils::ImageMode mode,
qreal opacity,
const QPixmap& pixmap,
QColor tint )
{ {
QHash< qint64, QPixmap > subsubcache; QHash< qint64, QPixmap > subsubcache;
QHash< int, QHash< qint64, QPixmap > > subcache; QHash< int, QHash< qint64, QPixmap > > subcache;

View File

@ -2,6 +2,8 @@
* *
* SPDX-License-Identifier: GPLv3+ * SPDX-License-Identifier: GPLv3+
* License-Filename: LICENSES/GPLv3+-ImageRegistry * License-Filename: LICENSES/GPLv3+-ImageRegistry
*
* Copyright 2019, Adriaan de Groot <groot@kde.org>
*/ */
/* /*
@ -26,8 +28,8 @@
#include <QPixmap> #include <QPixmap>
#include "utils/CalamaresUtilsGui.h"
#include "UiDllMacro.h" #include "UiDllMacro.h"
#include "utils/CalamaresUtilsGui.h"
class UIDLLEXPORT ImageRegistry class UIDLLEXPORT ImageRegistry
{ {
@ -37,13 +39,20 @@ public:
explicit ImageRegistry(); explicit ImageRegistry();
QIcon icon( const QString& image, CalamaresUtils::ImageMode mode = CalamaresUtils::Original ); QIcon icon( const QString& image, CalamaresUtils::ImageMode mode = CalamaresUtils::Original );
QPixmap pixmap( const QString& image, const QSize& size, CalamaresUtils::ImageMode mode = CalamaresUtils::Original, float opacity = 1.0, QColor tint = QColor( 0, 0, 0, 0 ) ); QPixmap pixmap( const QString& image,
const QSize& size,
CalamaresUtils::ImageMode mode = CalamaresUtils::Original,
qreal opacity = 1.0,
QColor tint = QColor( 0, 0, 0, 0 ) );
private: private:
qint64 cacheKey( const QSize& size, float opacity, QColor tint ); qint64 cacheKey( const QSize& size, qreal opacity, QColor tint );
void putInCache( const QString& image, const QSize& size, CalamaresUtils::ImageMode mode, float opacity, const QPixmap& pixmap, QColor tint ); void putInCache( const QString& image,
const QSize& size,
static ImageRegistry* s_instance; CalamaresUtils::ImageMode mode,
qreal opacity,
const QPixmap& pixmap,
QColor tint );
}; };
#endif // IMAGE_REGISTRY_H #endif // IMAGE_REGISTRY_H