Skip to content
Permalink
Browse files

Use golden ratio for tooltip preview swatches

  • Loading branch information
nyalldawson committed Oct 2, 2018
1 parent 62d6c8c commit a58d8d5411755abf8d79b9c559a694b959d3dc08
@@ -74,15 +74,16 @@ QVariant QgsStyleModel::data( const QModelIndex &index, int role ) const

if ( role == Qt::ToolTipRole )
{
QString tooltip = QStringLiteral( "<b>%1</b><br><i>%2</i>" ).arg( name,
QString tooltip = QStringLiteral( "<h3>%1</h3><p><i>%2</i>" ).arg( name,
tags.count() > 0 ? tags.join( QStringLiteral( ", " ) ) : tr( "Not tagged" ) );

// create very large preview image
std::unique_ptr< QgsSymbol > symbol( mStyle->symbol( name ) );
if ( symbol )
{
int size = static_cast< int >( Qgis::UI_SCALE_FACTOR * QFontMetrics( data( index, Qt::FontRole ).value< QFont >() ).width( 'X' ) * 20 );
QPixmap pm = QgsSymbolLayerUtils::symbolPreviewPixmap( symbol.get(), QSize( size, size ), size / 20 );
int width = static_cast< int >( Qgis::UI_SCALE_FACTOR * QFontMetrics( data( index, Qt::FontRole ).value< QFont >() ).width( 'X' ) * 23 );
int height = static_cast< int >( width / 1.61803398875 ); // golden ratio
QPixmap pm = QgsSymbolLayerUtils::symbolPreviewPixmap( symbol.get(), QSize( width, height ), height / 20 );
QByteArray data;
QBuffer buffer( &data );
pm.save( &buffer, "PNG", 100 );
@@ -163,9 +163,11 @@ bool QgsColorButton::event( QEvent *e )
int saturation = mColor.saturation();

// create very large preview swatch
int size = static_cast< int >( Qgis::UI_SCALE_FACTOR * fontMetrics().width( 'X' ) * 15 );
int margin = static_cast< int >( size * 0.1 );
QImage icon = QImage( size + 2 * margin, size + 2 * margin, QImage::Format_ARGB32 );
int width = static_cast< int >( Qgis::UI_SCALE_FACTOR * fontMetrics().width( 'X' ) * 23 );
int height = static_cast< int >( width / 1.61803398875 ); // golden ratio

int margin = static_cast< int >( height * 0.1 );
QImage icon = QImage( width + 2 * margin, height + 2 * margin, QImage::Format_ARGB32 );
icon.fill( Qt::transparent );

QPainter p;
@@ -175,14 +177,14 @@ bool QgsColorButton::event( QEvent *e )
QBrush checkBrush = QBrush( transparentBackground() );
p.setPen( Qt::NoPen );
p.setBrush( checkBrush );
p.drawRect( margin, margin, size, size );
p.drawRect( margin, margin, width, height );

//draw color over pattern
p.setBrush( QBrush( mColor ) );

//draw border
p.setPen( QColor( 197, 197, 197 ) );
p.drawRect( margin, margin, size, size );
p.drawRect( margin, margin, width, height );
p.end();

QByteArray data;
@@ -123,9 +123,10 @@ void QgsColorSwatchGrid::updateTooltip( const int colorIdx )
QString colorName = mColors.at( colorIdx ).second;

// create very large preview swatch, because the grid itself has only tiny preview icons
int size = static_cast< int >( Qgis::UI_SCALE_FACTOR * fontMetrics().width( 'X' ) * 15 );
int margin = static_cast< int >( size * 0.1 );
QImage icon = QImage( size + 2 * margin, size + 2 * margin, QImage::Format_ARGB32 );
int width = static_cast< int >( Qgis::UI_SCALE_FACTOR * fontMetrics().width( 'X' ) * 23 );
int height = static_cast< int >( width / 1.61803398875 ); // golden ratio
int margin = static_cast< int >( height * 0.1 );
QImage icon = QImage( width + 2 * margin, height + 2 * margin, QImage::Format_ARGB32 );
icon.fill( Qt::transparent );

QPainter p;
@@ -135,14 +136,14 @@ void QgsColorSwatchGrid::updateTooltip( const int colorIdx )
QBrush checkBrush = QBrush( transparentBackground() );
p.setPen( Qt::NoPen );
p.setBrush( checkBrush );
p.drawRect( margin, margin, size, size );
p.drawRect( margin, margin, width, height );

//draw color over pattern
p.setBrush( QBrush( mColors.at( colorIdx ).first ) );

//draw border
p.setPen( QColor( 197, 197, 197 ) );
p.drawRect( margin, margin, size, size );
p.drawRect( margin, margin, width, height );
p.end();

QByteArray data;
@@ -45,7 +45,7 @@ QgsSymbolButton::QgsSymbolButton( QWidget *parent, const QString &dialogTitle )

//make sure height of button looks good under different platforms
QSize size = QToolButton::minimumSizeHint();
int fontHeight = Qgis::UI_SCALE_FACTOR * fontMetrics().height() * 1.4;
int fontHeight = static_cast< int >( Qgis::UI_SCALE_FACTOR * fontMetrics().height() * 1.4 );
mSizeHint = QSize( size.width(), std::max( size.height(), fontHeight ) );
}

@@ -473,8 +473,10 @@ void QgsSymbolButton::updatePreview( const QColor &color, QgsSymbol *tempSymbol

// set tooltip
// create very large preview image
int size = static_cast< int >( Qgis::UI_SCALE_FACTOR * fontMetrics().width( 'X' ) * 20 );
QPixmap pm = QgsSymbolLayerUtils::symbolPreviewPixmap( previewSymbol.get(), QSize( size, size ), size / 20 );
int width = static_cast< int >( Qgis::UI_SCALE_FACTOR * fontMetrics().width( 'X' ) * 23 );
int height = static_cast< int >( width / 1.61803398875 ); // golden ratio

QPixmap pm = QgsSymbolLayerUtils::symbolPreviewPixmap( previewSymbol.get(), QSize( width, height ), height / 20 );
QByteArray data;
QBuffer buffer( &data );
pm.save( &buffer, "PNG", 100 );

0 comments on commit a58d8d5

Please sign in to comment.
You can’t perform that action at this time.