243 lines
4.5 KiB
C++
Raw Normal View History

2017-07-21 18:21:34 +02:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
2019-06-20 12:02:28 +02:00
* This file may be used under the terms of the 3-clause BSD License
2017-07-21 18:21:34 +02:00
*****************************************************************************/
#include "Image.h"
2017-07-21 18:21:34 +02:00
2022-04-11 10:12:33 +02:00
QSK_QT_PRIVATE_BEGIN
#include <private/qquickimage_p_p.h>
QSK_QT_PRIVATE_END
2017-07-21 18:21:34 +02:00
2022-04-11 10:12:33 +02:00
class ImagePrivate : public QQuickImagePrivate
2017-07-21 18:21:34 +02:00
{
2018-08-03 08:15:28 +02:00
public:
2022-04-11 10:12:33 +02:00
ImagePrivate()
2018-08-03 08:15:28 +02:00
: sourceSizeAdjustment( false )
, deferredUpdates( true )
, dirtyPolish( false )
, dirtyUpdate( false )
2017-07-21 18:21:34 +02:00
{
}
bool sourceSizeAdjustment : 1;
bool deferredUpdates : 1;
bool dirtyPolish : 1;
bool dirtyUpdate : 1;
};
2018-08-03 08:15:28 +02:00
Image::Image( QQuickItem* parent )
2022-04-11 10:12:33 +02:00
: QQuickImage( *( new ImagePrivate() ), parent )
2017-07-21 18:21:34 +02:00
{
}
Image::~Image()
2017-07-21 18:21:34 +02:00
{
}
void Image::show()
2017-07-21 18:21:34 +02:00
{
2022-04-11 10:12:33 +02:00
setVisible( true );
2017-07-21 18:21:34 +02:00
}
void Image::hide()
2017-07-21 18:21:34 +02:00
{
2022-04-11 10:12:33 +02:00
setVisible( false );
2017-07-21 18:21:34 +02:00
}
void Image::setSourceSizeAdjustment( bool on )
2017-07-21 18:21:34 +02:00
{
2022-04-11 10:12:33 +02:00
Q_D( Image );
if ( on != d->sourceSizeAdjustment )
2017-07-21 18:21:34 +02:00
{
2022-04-11 10:12:33 +02:00
d->sourceSizeAdjustment = on;
2017-07-21 18:21:34 +02:00
Q_EMIT sourceSizeAdjustmentChanged();
}
}
bool Image::sourceSizeAdjustment() const
2017-07-21 18:21:34 +02:00
{
2022-04-11 10:12:33 +02:00
return d_func()->sourceSizeAdjustment;
2017-07-21 18:21:34 +02:00
}
void Image::setDeferredUpdates( bool on )
2017-07-21 18:21:34 +02:00
{
2022-04-11 10:12:33 +02:00
Q_D( Image );
if ( on != d->deferredUpdates )
2017-07-21 18:21:34 +02:00
{
2022-04-11 10:12:33 +02:00
d->deferredUpdates = on;
2017-07-21 18:21:34 +02:00
if ( !on )
{
// when having blocked updates we reschedule them
2022-04-11 10:12:33 +02:00
if ( d->dirtyPolish )
2017-07-21 18:21:34 +02:00
polish();
2022-04-11 10:12:33 +02:00
if ( d->dirtyUpdate )
2017-07-21 18:21:34 +02:00
update();
}
}
}
bool Image::deferredUpdates() const
2017-07-21 18:21:34 +02:00
{
2022-04-11 10:12:33 +02:00
return d_func()->deferredUpdates;
2017-07-21 18:21:34 +02:00
}
void Image::setSourceSize( const QSize& size )
{
2020-10-31 10:03:56 +01:00
if ( !( size.isEmpty() && sourceSize().isEmpty() ) )
2022-04-11 10:12:33 +02:00
Inherited::setSourceSize( size );
}
void Image::componentComplete()
2017-07-21 18:21:34 +02:00
{
2022-04-11 10:12:33 +02:00
Q_D( const Image );
if ( d->deferredUpdates && d->sourceSizeAdjustment )
2017-07-21 18:21:34 +02:00
{
// QQuickImage::componentComplete() calls load
// long before we have the final geometry
polish();
QQuickItem::componentComplete();
}
else
{
Inherited::componentComplete();
}
}
void Image::itemChange( QQuickItem::ItemChange change,
2017-07-21 18:21:34 +02:00
const QQuickItem::ItemChangeData& value )
{
Inherited::itemChange( change, value );
if ( change == ItemVisibleHasChanged )
{
2022-04-11 10:12:33 +02:00
Q_D( const Image );
2017-07-21 18:21:34 +02:00
if ( value.boolValue )
{
2022-04-11 10:12:33 +02:00
if ( d->dirtyPolish )
2017-07-21 18:21:34 +02:00
polish();
2022-04-11 10:12:33 +02:00
if ( d->dirtyUpdate )
2017-07-21 18:21:34 +02:00
update();
}
}
}
2020-10-26 17:51:31 +01:00
#if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 )
void Image::geometryChange(
const QRectF& newGeometry, const QRectF& oldGeometry )
{
Inherited::geometryChange( newGeometry, oldGeometry );
if ( newGeometry.size() != oldGeometry.size() )
adjustSourceSize( newGeometry.size() );
}
#else
2018-08-03 08:15:28 +02:00
void Image::geometryChanged(
const QRectF& newGeometry, const QRectF& oldGeometry )
2017-07-21 18:21:34 +02:00
{
Inherited::geometryChanged( newGeometry, oldGeometry );
if ( newGeometry.size() != oldGeometry.size() )
2020-10-26 17:51:31 +01:00
adjustSourceSize( newGeometry.size() );
}
#endif
void Image::adjustSourceSize( const QSizeF& size )
{
2022-04-11 10:12:33 +02:00
Q_D( const Image );
if ( d->sourceSizeAdjustment )
2017-07-21 18:21:34 +02:00
{
2022-04-11 10:12:33 +02:00
if ( d->deferredUpdates )
2017-07-21 18:21:34 +02:00
{
2020-10-26 17:51:31 +01:00
setImplicitSize( size.width(), size.height() );
polish();
}
else
{
setSourceSize( size.toSize() );
2017-07-21 18:21:34 +02:00
}
}
}
void Image::updatePolish()
2017-07-21 18:21:34 +02:00
{
2022-04-11 10:12:33 +02:00
Q_D( Image );
if ( d->deferredUpdates )
2017-07-21 18:21:34 +02:00
{
if ( !isVisible() )
{
2022-04-11 10:12:33 +02:00
d->dirtyPolish = true;
2017-07-21 18:21:34 +02:00
return;
}
2022-04-11 10:12:33 +02:00
if ( d->sourceSizeAdjustment )
2017-12-07 14:59:29 +01:00
setSourceSize( QSize( int( width() ), int( height() ) ) );
2017-07-21 18:21:34 +02:00
}
2022-04-11 10:12:33 +02:00
d->dirtyPolish = false;
2017-07-21 18:21:34 +02:00
Inherited::updatePolish();
}
QSGNode* Image::updatePaintNode( QSGNode* oldNode, UpdatePaintNodeData* data )
2017-07-21 18:21:34 +02:00
{
2022-04-11 10:12:33 +02:00
Q_D( Image );
if ( d->deferredUpdates )
2017-07-21 18:21:34 +02:00
{
if ( !isVisible() )
{
2022-04-11 10:12:33 +02:00
d->dirtyUpdate = true;
2017-07-21 18:21:34 +02:00
return oldNode;
}
}
2022-04-11 10:12:33 +02:00
d->dirtyUpdate = false;
2017-07-21 18:21:34 +02:00
return Inherited::updatePaintNode( oldNode, data );
}
bool Image::hasHeightForWidth() const
2017-07-21 18:21:34 +02:00
{
// TODO
return false;
}
qreal Image::heightForWidth( qreal width ) const
2017-07-21 18:21:34 +02:00
{
// TODO
Q_UNUSED( width )
return -1.0;
}
bool Image::hasWidthForHeight() const
2017-07-21 18:21:34 +02:00
{
// TODO
return false;
}
qreal Image::widthForHeight( qreal height ) const
2017-07-21 18:21:34 +02:00
{
// TODO
Q_UNUSED( height )
return -1.0;
}
#include "moc_Image.cpp"