Avoid include of QSize

This commit is contained in:
Magne Sjaastad 2024-02-17 11:10:44 +01:00
parent 4aacb31566
commit 84ee29cdc9
2 changed files with 71 additions and 14 deletions

View File

@ -46,14 +46,50 @@ using namespace caf;
class caf::IconProvider::Impl
{
public:
Impl()
: m_active( false )
, m_preferredSize( QSize( 16, 16 ) )
{
}
Impl( const Impl& rhs )
{
m_active = rhs.m_active;
m_iconResourceString = rhs.m_iconResourceString;
m_overlayResourceString = rhs.m_overlayResourceString;
m_backgroundColorStrings = rhs.m_backgroundColorStrings;
m_preferredSize = rhs.m_preferredSize;
if ( rhs.m_pixmap )
{
m_pixmap = std::make_unique<QPixmap>( *rhs.m_pixmap );
}
}
bool m_active;
QString m_iconResourceString;
QString m_overlayResourceString;
std::vector<QString> m_backgroundColorStrings;
QSize m_preferredSize;
QPixmap m_pixmap;
std::unique_ptr<QPixmap> m_pixmap;
};
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
IconProvider::IconProvider()
: m_impl( new Impl )
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
IconProvider::IconProvider( const QString& iconResourceString )
: m_impl( new Impl )
{
m_impl->m_active = true;
m_impl->m_iconResourceString = iconResourceString;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -82,7 +118,7 @@ caf::IconProvider::IconProvider( const QPixmap& pixmap )
: m_impl( new Impl )
{
m_impl->m_active = true;
m_impl->m_pixmap = pixmap;
m_impl->m_pixmap = std::make_unique<QPixmap>( pixmap );
m_impl->m_preferredSize = pixmap.size();
}
@ -102,6 +138,8 @@ IconProvider::IconProvider( const IconProvider& rhs )
m_impl = std::make_unique<Impl>( *rhs.m_impl );
else
m_impl = std::make_unique<Impl>();
copyPixmap( rhs );
}
//--------------------------------------------------------------------------------------------------
@ -109,7 +147,12 @@ IconProvider::IconProvider( const IconProvider& rhs )
//--------------------------------------------------------------------------------------------------
IconProvider& IconProvider::operator=( const IconProvider& rhs )
{
*m_impl = *rhs.m_impl;
m_impl->m_active = rhs.m_impl->m_active;
m_impl->m_iconResourceString = rhs.m_impl->m_iconResourceString;
m_impl->m_overlayResourceString = rhs.m_impl->m_overlayResourceString;
m_impl->m_backgroundColorStrings = rhs.m_impl->m_backgroundColorStrings;
m_impl->m_preferredSize = rhs.m_impl->m_preferredSize;
copyPixmap( rhs );
return *this;
}
@ -129,7 +172,7 @@ bool caf::IconProvider::valid() const
{
if ( isGuiApplication() )
{
if ( !m_impl->m_pixmap.isNull() ) return true;
if ( m_impl->m_pixmap && !m_impl->m_pixmap->isNull() ) return true;
if ( backgroundColorsAreValid() )
{
@ -170,7 +213,7 @@ std::unique_ptr<QIcon> IconProvider::icon( const QSize& size ) const
return nullptr;
}
if ( !m_impl->m_pixmap.isNull() ) return std::unique_ptr<QIcon>( new QIcon( m_impl->m_pixmap ) );
if ( m_impl->m_pixmap ) return std::unique_ptr<QIcon>( new QIcon( *m_impl->m_pixmap ) );
QPixmap pixmap( size );
@ -287,7 +330,7 @@ void IconProvider::setBackgroundColorGradient( const std::vector<QString>& color
//--------------------------------------------------------------------------------------------------
void caf::IconProvider::setPixmap( const QPixmap& pixmap )
{
m_impl->m_pixmap = pixmap;
m_impl->m_pixmap.reset( new QPixmap( pixmap ) );
}
//--------------------------------------------------------------------------------------------------
@ -298,6 +341,17 @@ bool caf::IconProvider::isGuiApplication()
return dynamic_cast<QApplication*>( QCoreApplication::instance() ) != nullptr;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void IconProvider::copyPixmap( const IconProvider& rhs )
{
if ( rhs.m_impl->m_pixmap )
{
m_impl->m_pixmap = std::unique_ptr<QPixmap>( new QPixmap( *rhs.m_impl->m_pixmap ) );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@ -35,13 +35,13 @@
//##################################################################################################
#pragma once
#include <QSize>
#include <memory>
#include <vector>
class QIcon;
class QPixmap;
class QString;
class QSize;
namespace caf
{
@ -52,8 +52,10 @@ namespace caf
class IconProvider
{
public:
IconProvider( const QSize& preferredSize = QSize( 16, 16 ) );
IconProvider( const QString& iconResourceString, const QSize& preferredSize = QSize( 16, 16 ) );
IconProvider();
IconProvider( const QSize& preferredSize );
IconProvider( const QString& iconResourceString );
IconProvider( const QString& iconResourceString, const QSize& preferredSize );
IconProvider( const QPixmap& pixmap );
IconProvider( const IconProvider& rhs );
IconProvider& operator=( const IconProvider& rhs );
@ -75,6 +77,7 @@ public:
private:
static bool isGuiApplication();
void copyPixmap( const IconProvider& rhs );
bool backgroundColorsAreValid() const;