Skip to content

Commit

Permalink
Use golden ratio for tooltip preview swatches
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 2, 2018
1 parent 62d6c8c commit a58d8d5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
7 changes: 4 additions & 3 deletions src/core/symbology/qgsstylemodel.cpp
Expand Up @@ -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 );
Expand Down
12 changes: 7 additions & 5 deletions src/gui/qgscolorbutton.cpp
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
11 changes: 6 additions & 5 deletions src/gui/qgscolorswatchgrid.cpp
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions src/gui/qgssymbolbutton.cpp
Expand Up @@ -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 ) );
}

Expand Down Expand Up @@ -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 );
Expand Down

0 comments on commit a58d8d5

Please sign in to comment.