Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Don't store QImage on heap
  • Loading branch information
nyalldawson committed May 12, 2023
1 parent c87d64b commit 5f98f5c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 33 deletions.
52 changes: 22 additions & 30 deletions src/gui/qgscolorwidgets.cpp
Expand Up @@ -388,12 +388,7 @@ QgsColorWheel::QgsColorWheel( QWidget *parent )
mWheelBrush = QBrush( wheelGradient );
}

QgsColorWheel::~QgsColorWheel()
{
delete mWheelImage;
delete mTriangleImage;
delete mWidgetImage;
}
QgsColorWheel::~QgsColorWheel() = default;

QSize QgsColorWheel::sizeHint() const
{
Expand All @@ -406,14 +401,14 @@ void QgsColorWheel::paintEvent( QPaintEvent *event )
Q_UNUSED( event )
QPainter painter( this );

if ( !mWidgetImage || !mWheelImage || !mTriangleImage )
if ( mWidgetImage.isNull() || mWheelImage.isNull() || mTriangleImage.isNull() )
{
createImages( size() );
}

//draw everything in an image
mWidgetImage->fill( Qt::transparent );
QPainter imagePainter( mWidgetImage );
mWidgetImage.fill( Qt::transparent );
QPainter imagePainter( &mWidgetImage );
imagePainter.setRenderHint( QPainter::Antialiasing );

if ( mWheelDirty )
Expand All @@ -424,11 +419,11 @@ void QgsColorWheel::paintEvent( QPaintEvent *event )

//draw wheel centered on widget
const QPointF center = QPointF( width() / 2.0, height() / 2.0 );
imagePainter.drawImage( QPointF( center.x() - ( mWheelImage->width() / 2.0 ), center.y() - ( mWheelImage->height() / 2.0 ) ), *mWheelImage );
imagePainter.drawImage( QPointF( center.x() - ( mWheelImage.width() / 2.0 ), center.y() - ( mWheelImage.height() / 2.0 ) ), mWheelImage );

//draw hue marker
const int h = hue();
const double length = mWheelImage->width() / 2.0;
const double length = mWheelImage.width() / 2.0;
QLineF hueMarkerLine = QLineF( center.x(), center.y(), center.x() + length, center.y() );
hueMarkerLine.setAngle( h );
imagePainter.save();
Expand All @@ -447,7 +442,7 @@ void QgsColorWheel::paintEvent( QPaintEvent *event )
{
createTriangle();
}
imagePainter.drawImage( QPointF( center.x() - ( mWheelImage->width() / 2.0 ), center.y() - ( mWheelImage->height() / 2.0 ) ), *mTriangleImage );
imagePainter.drawImage( QPointF( center.x() - ( mWheelImage.width() / 2.0 ), center.y() - ( mWheelImage.height() / 2.0 ) ), mTriangleImage );

//draw current color marker
const double triangleRadius = length - mWheelThickness - 1;
Expand Down Expand Up @@ -476,7 +471,7 @@ void QgsColorWheel::paintEvent( QPaintEvent *event )
imagePainter.end();

//draw image onto widget
painter.drawImage( QPoint( 0, 0 ), *mWidgetImage );
painter.drawImage( QPoint( 0, 0 ), mWidgetImage );
painter.end();
}

Expand All @@ -497,12 +492,9 @@ void QgsColorWheel::createImages( const QSizeF size )
mWheelThickness = wheelSize / 15.0;

//recreate cache images at correct size
delete mWheelImage;
mWheelImage = new QImage( wheelSize, wheelSize, QImage::Format_ARGB32 );
delete mTriangleImage;
mTriangleImage = new QImage( wheelSize, wheelSize, QImage::Format_ARGB32 );
delete mWidgetImage;
mWidgetImage = new QImage( size.width(), size.height(), QImage::Format_ARGB32 );
mWheelImage = QImage( wheelSize, wheelSize, QImage::Format_ARGB32 );
mTriangleImage = QImage( wheelSize, wheelSize, QImage::Format_ARGB32 );
mWidgetImage = QImage( size.width(), size.height(), QImage::Format_ARGB32 );

//trigger a redraw for the images
mWheelDirty = true;
Expand Down Expand Up @@ -558,7 +550,7 @@ void QgsColorWheel::setColorFromPos( const QPointF pos )
const double hueRadians = h * M_PI / 180.0;
double rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
double rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
const double length = mWheelImage->width() / 2.0;
const double length = mWheelImage.width() / 2.0;
const double triangleLength = length - mWheelThickness - 1;

const double a = 0.5 * triangleLength;
Expand Down Expand Up @@ -634,7 +626,7 @@ void QgsColorWheel::mousePressEvent( QMouseEvent *event )
//create a line from the widget's center to the event
const QLineF line = QLineF( width() / 2.0, height() / 2.0, event->pos().x(), event->pos().y() );

const double innerLength = mWheelImage->width() / 2.0 - mWheelThickness;
const double innerLength = mWheelImage.width() / 2.0 - mWheelThickness;
if ( line.length() < innerLength )
{
mClickedPart = QgsColorWheel::Triangle;
Expand Down Expand Up @@ -666,16 +658,16 @@ void QgsColorWheel::mouseReleaseEvent( QMouseEvent *event )

void QgsColorWheel::createWheel()
{
if ( !mWheelImage )
if ( mWheelImage.isNull() )
{
return;
}

const int maxSize = std::min( mWheelImage->width(), mWheelImage->height() );
const int maxSize = std::min( mWheelImage.width(), mWheelImage.height() );
const double wheelRadius = maxSize / 2.0;

mWheelImage->fill( Qt::transparent );
QPainter p( mWheelImage );
mWheelImage.fill( Qt::transparent );
QPainter p( &mWheelImage );
p.setRenderHint( QPainter::Antialiasing );
p.setBrush( mWheelBrush );
p.setPen( Qt::NoPen );
Expand All @@ -695,19 +687,19 @@ void QgsColorWheel::createWheel()

void QgsColorWheel::createTriangle()
{
if ( !mWheelImage || !mTriangleImage )
if ( mWheelImage.isNull() || mTriangleImage.isNull() )
{
return;
}

const QPointF center = QPointF( mWheelImage->width() / 2.0, mWheelImage->height() / 2.0 );
mTriangleImage->fill( Qt::transparent );
const QPointF center = QPointF( mWheelImage.width() / 2.0, mWheelImage.height() / 2.0 );
mTriangleImage.fill( Qt::transparent );

QPainter imagePainter( mTriangleImage );
QPainter imagePainter( &mTriangleImage );
imagePainter.setRenderHint( QPainter::Antialiasing );

const int angle = hue();
const double wheelRadius = mWheelImage->width() / 2.0;
const double wheelRadius = mWheelImage.width() / 2.0;
const double triangleRadius = wheelRadius - mWheelThickness - 1;

//pure version of hue (at full saturation and value)
Expand Down
6 changes: 3 additions & 3 deletions src/gui/qgscolorwidgets.h
Expand Up @@ -335,13 +335,13 @@ class GUI_EXPORT QgsColorWheel : public QgsColorWidget
ControlPart mClickedPart = QgsColorWheel::None;

/*Cached image of hue wheel*/
QImage *mWheelImage = nullptr;
QImage mWheelImage;

/*Cached image of inner triangle*/
QImage *mTriangleImage = nullptr;
QImage mTriangleImage;

/*Resuable, temporary image for drawing widget*/
QImage *mWidgetImage = nullptr;
QImage mWidgetImage;

/*Whether the color wheel image requires redrawing*/
bool mWheelDirty = true;
Expand Down

0 comments on commit 5f98f5c

Please sign in to comment.