Skip to content

Commit 8f78beb

Browse files
committed
[ux] Show large swatch preview in color/symbol tooltips
1 parent 16a57a3 commit 8f78beb

File tree

3 files changed

+105
-17
lines changed

3 files changed

+105
-17
lines changed

src/core/symbology/qgsstylemodel.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,29 @@ QVariant QgsStyleModel::data( const QModelIndex &index, int role ) const
7171
case Name:
7272
{
7373
const QStringList tags = mStyle->tagsOfSymbol( isColorRamp ? QgsStyle::ColorrampEntity : QgsStyle::SymbolEntity, name );
74-
return role != Qt::ToolTipRole ? name
75-
: QStringLiteral( "<b>%1</b><br><i>%2</i>" ).arg( name, tags.count() > 0 ? tags.join( QStringLiteral( ", " ) ) : tr( "Not tagged" ) );
74+
75+
if ( role == Qt::ToolTipRole )
76+
{
77+
QString tooltip = QStringLiteral( "<b>%1</b><br><i>%2</i>" ).arg( name,
78+
tags.count() > 0 ? tags.join( QStringLiteral( ", " ) ) : tr( "Not tagged" ) );
79+
80+
// create very large preview image
81+
std::unique_ptr< QgsSymbol > symbol( mStyle->symbol( name ) );
82+
if ( symbol )
83+
{
84+
int size = static_cast< int >( Qgis::UI_SCALE_FACTOR * QFontMetrics( data( index, Qt::FontRole ).value< QFont >() ).width( 'X' ) * 20 );
85+
QPixmap pm = QgsSymbolLayerUtils::symbolPreviewPixmap( symbol.get(), QSize( size, size ), size / 20 );
86+
QByteArray data;
87+
QBuffer buffer( &data );
88+
pm.save( &buffer, "PNG", 100 );
89+
tooltip += QStringLiteral( "<p><img src='data:image/png;base64, %3'>" ).arg( QString( data.toBase64() ) );
90+
}
91+
return tooltip;
92+
}
93+
else
94+
{
95+
return name;
96+
}
7697
}
7798
case Tags:
7899
return mStyle->tagsOfSymbol( isColorRamp ? QgsStyle::ColorrampEntity : QgsStyle::SymbolEntity, name ).join( QStringLiteral( ", " ) );

src/gui/qgscolorbutton.cpp

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,43 @@ bool QgsColorButton::event( QEvent *e )
157157
{
158158
if ( e->type() == QEvent::ToolTip )
159159
{
160-
QString name = this->color().name();
161-
int hue = this->color().hue();
162-
int value = this->color().value();
163-
int saturation = this->color().saturation();
164-
QString info = QString( "HEX: %1 \n"
165-
"RGB: %2 \n"
166-
"HSV: %3,%4,%5" ).arg( name,
167-
QgsSymbolLayerUtils::encodeColor( this->color() ) )
160+
QString name = mColor.name();
161+
int hue = mColor.hue();
162+
int value = mColor.value();
163+
int saturation = mColor.saturation();
164+
165+
// create very large preview swatch
166+
int size = static_cast< int >( Qgis::UI_SCALE_FACTOR * fontMetrics().width( 'X' ) * 15 );
167+
int margin = static_cast< int >( size * 0.1 );
168+
QImage icon = QImage( size + 2 * margin, size + 2 * margin, QImage::Format_ARGB32 );
169+
icon.fill( Qt::transparent );
170+
171+
QPainter p;
172+
p.begin( &icon );
173+
174+
//start with checkboard pattern
175+
QBrush checkBrush = QBrush( transparentBackground() );
176+
p.setPen( Qt::NoPen );
177+
p.setBrush( checkBrush );
178+
p.drawRect( margin, margin, size, size );
179+
180+
//draw color over pattern
181+
p.setBrush( QBrush( mColor ) );
182+
183+
//draw border
184+
p.setPen( QColor( 197, 197, 197 ) );
185+
p.drawRect( margin, margin, size, size );
186+
p.end();
187+
188+
QByteArray data;
189+
QBuffer buffer( &data );
190+
icon.save( &buffer, "PNG", 100 );
191+
192+
QString info = QStringLiteral( "<b>HEX</b> %1<br>"
193+
"<b>RGB</b> %2<br>"
194+
"<b>HSV</b> %3,%4,%5<p>"
195+
"<img src='data:image/png;base64, %0'>" ).arg( QString( data.toBase64() ), name,
196+
QgsSymbolLayerUtils::encodeColor( this->color() ) )
168197
.arg( hue ).arg( saturation ).arg( value );
169198
setToolTip( info );
170199
}

src/gui/qgscolorswatchgrid.cpp

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515

1616
#include "qgscolorswatchgrid.h"
1717
#include "qgsapplication.h"
18+
#include "qgssymbollayerutils.h"
1819
#include "qgslogger.h"
1920
#include <QPainter>
2021
#include <QMouseEvent>
2122
#include <QMenu>
23+
#include <QBuffer>
2224

2325
#define NUMBER_COLORS_PER_ROW 10 //number of color swatches per row
2426

@@ -115,15 +117,51 @@ void QgsColorSwatchGrid::updateTooltip( const int colorIdx )
115117
{
116118
if ( colorIdx >= 0 && colorIdx < mColors.length() )
117119
{
120+
QColor color = mColors.at( colorIdx ).first;
121+
118122
//if color has an associated name from the color scheme, use that
119123
QString colorName = mColors.at( colorIdx ).second;
120-
if ( colorName.isEmpty() )
121-
{
122-
//otherwise, build a default string
123-
QColor color = mColors.at( colorIdx ).first;
124-
colorName = QString( tr( "rgb(%1, %2, %3)" ) ).arg( color.red() ).arg( color.green() ).arg( color.blue() );
125-
}
126-
setToolTip( colorName );
124+
125+
// create very large preview swatch, because the grid itself has only tiny preview icons
126+
int size = static_cast< int >( Qgis::UI_SCALE_FACTOR * fontMetrics().width( 'X' ) * 15 );
127+
int margin = static_cast< int >( size * 0.1 );
128+
QImage icon = QImage( size + 2 * margin, size + 2 * margin, QImage::Format_ARGB32 );
129+
icon.fill( Qt::transparent );
130+
131+
QPainter p;
132+
p.begin( &icon );
133+
134+
//start with checkboard pattern
135+
QBrush checkBrush = QBrush( transparentBackground() );
136+
p.setPen( Qt::NoPen );
137+
p.setBrush( checkBrush );
138+
p.drawRect( margin, margin, size, size );
139+
140+
//draw color over pattern
141+
p.setBrush( QBrush( mColors.at( colorIdx ).first ) );
142+
143+
//draw border
144+
p.setPen( QColor( 197, 197, 197 ) );
145+
p.drawRect( margin, margin, size, size );
146+
p.end();
147+
148+
QByteArray data;
149+
QBuffer buffer( &data );
150+
icon.save( &buffer, "PNG", 100 );
151+
152+
QString info;
153+
if ( !colorName.isEmpty() )
154+
info += QStringLiteral( "<h3>%1</h3><p>" ).arg( colorName );
155+
156+
info += QStringLiteral( "<b>HEX</b> %1<br>"
157+
"<b>RGB</b> %2<br>"
158+
"<b>HSV</b> %3,%4,%5<p>" ).arg( color.name(),
159+
QgsSymbolLayerUtils::encodeColor( color ) )
160+
.arg( color.hue() ).arg( color.saturation() ).arg( color.value() );
161+
info += QStringLiteral( "<img src='data:image/png;base64, %0'>" ).arg( QString( data.toBase64() ) );
162+
163+
setToolTip( info );
164+
127165
}
128166
else
129167
{

0 commit comments

Comments
 (0)