diff --git a/src/gui/qgscolorbutton.cpp b/src/gui/qgscolorbutton.cpp index a8c9b406817d..0bd63a976742 100644 --- a/src/gui/qgscolorbutton.cpp +++ b/src/gui/qgscolorbutton.cpp @@ -16,7 +16,6 @@ #include "qgscolorbutton.h" #include "qgscolordialog.h" #include "qgsapplication.h" -#include "qgslogger.h" #include "qgssymbollayerutils.h" #include "qgscolorswatchgrid.h" #include "qgscolorschemeregistry.h" @@ -25,6 +24,7 @@ #include "qgsproject.h" #include "qgsguiutils.h" #include "qgsgui.h" +#include "qgscolortooltip_p.h" #include #include @@ -175,47 +175,10 @@ bool QgsColorButton::event( QEvent *e ) if ( !isProjectColor ) c = mColor; - const QString name = c.name(); - const int hue = c.hue(); - const int value = c.value(); - const int saturation = c.saturation(); + QString info = ( isProjectColor ? QStringLiteral( "

%1: %2

" ).arg( tr( "Linked color" ), mLinkedColorName ) : QString() ); - // create very large preview swatch - const int width = static_cast< int >( Qgis::UI_SCALE_FACTOR * fontMetrics().horizontalAdvance( 'X' ) * 23 ); - const int height = static_cast< int >( width / 1.61803398875 ); // golden ratio + info += QgsColorTooltip::htmlDescription( c, this ); - const 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; - p.begin( &icon ); - - //start with checkboard pattern - const QBrush checkBrush = QBrush( transparentBackground() ); - p.setPen( Qt::NoPen ); - p.setBrush( checkBrush ); - p.drawRect( margin, margin, width, height ); - - //draw color over pattern - p.setBrush( QBrush( c ) ); - - //draw border - p.setPen( QColor( 197, 197, 197 ) ); - p.drawRect( margin, margin, width, height ); - p.end(); - - QByteArray data; - QBuffer buffer( &data ); - icon.save( &buffer, "PNG", 100 ); - - const QString info = ( isProjectColor ? QStringLiteral( "

%1: %2

" ).arg( tr( "Linked color" ), mLinkedColorName ) : QString() ) - + QStringLiteral( "HEX %1
" - "RGB %2
" - "HSV %3,%4,%5

" - "" ).arg( QString( data.toBase64() ), name, - QgsSymbolLayerUtils::encodeColor( c ) ) - .arg( hue ).arg( saturation ).arg( value ); setToolTip( info ); } return QToolButton::event( e ); @@ -862,4 +825,3 @@ void QgsColorButton::linkToProjectColor( const QString &name ) mLinkedColorName = name; setButtonBackground(); } - diff --git a/src/gui/qgscolorbutton.h b/src/gui/qgscolorbutton.h index 11b370f759a2..56c5fee41519 100644 --- a/src/gui/qgscolorbutton.h +++ b/src/gui/qgscolorbutton.h @@ -535,6 +535,8 @@ class GUI_EXPORT QgsColorButton : public QToolButton * Creates the drop-down menu entries */ void prepareMenu(); + + friend class QgsColorTooltip; }; #endif diff --git a/src/gui/qgscolorswatchgrid.cpp b/src/gui/qgscolorswatchgrid.cpp index 4f2fffaeb5f9..1cbafe6e735f 100644 --- a/src/gui/qgscolorswatchgrid.cpp +++ b/src/gui/qgscolorswatchgrid.cpp @@ -16,7 +16,7 @@ #include "qgscolorswatchgrid.h" #include "qgsapplication.h" #include "qgssymbollayerutils.h" -#include "qgslogger.h" +#include "qgscolortooltip_p.h" #include #include #include @@ -121,44 +121,11 @@ void QgsColorSwatchGrid::updateTooltip( const int colorIdx ) //if color has an associated name from the color scheme, use that const QString colorName = mColors.at( colorIdx ).second; - // create very large preview swatch, because the grid itself has only tiny preview icons - const int width = static_cast< int >( Qgis::UI_SCALE_FACTOR * fontMetrics().horizontalAdvance( 'X' ) * 23 ); - const int height = static_cast< int >( width / 1.61803398875 ); // golden ratio - const 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; - p.begin( &icon ); - - //start with checkboard pattern - const QBrush checkBrush = QBrush( transparentBackground() ); - p.setPen( Qt::NoPen ); - p.setBrush( checkBrush ); - 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, width, height ); - p.end(); - - QByteArray data; - QBuffer buffer( &data ); - icon.save( &buffer, "PNG", 100 ); - QString info; if ( !colorName.isEmpty() ) info += QStringLiteral( "

%1

" ).arg( colorName ); - info += QStringLiteral( "HEX %1
" - "RGB %2
" - "HSV %3,%4,%5

" ).arg( color.name(), - QgsSymbolLayerUtils::encodeColor( color ) ) - .arg( color.hue() ).arg( color.saturation() ).arg( color.value() ); - info += QStringLiteral( "" ).arg( QString( data.toBase64() ) ); + info += QgsColorTooltip::htmlDescription( color, this ); setToolTip( info ); diff --git a/src/gui/qgscolorswatchgrid.h b/src/gui/qgscolorswatchgrid.h index 1f47f3a7ec38..f89e066cdb03 100644 --- a/src/gui/qgscolorswatchgrid.h +++ b/src/gui/qgscolorswatchgrid.h @@ -174,6 +174,8 @@ class GUI_EXPORT QgsColorSwatchGrid : public QWidget * \returns checkboard pixmap */ QPixmap transparentBackground(); + + friend class QgsColorTooltip; }; diff --git a/src/gui/qgscolortooltip_p.h b/src/gui/qgscolortooltip_p.h new file mode 100644 index 000000000000..8766ca7e034a --- /dev/null +++ b/src/gui/qgscolortooltip_p.h @@ -0,0 +1,94 @@ +/*************************************************************************** + qgscolortooltip_p.h + --------------------- + begin : 2024/08/21 + copyright : (C) 2024 by Julien Cabieces + email : julien dot cabieces at oslandia dot com + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#ifndef QGSCOLORTOOLTIP_P_H +#define QGSCOLORTOOLTIP_P_H + +#include +#include + +#include "qgssymbollayerutils.h" + +//! helper class to generate color tooltip +class QgsColorTooltip +{ + public: + + //! Returns an HTML desciption given a \a color with a preview image of the color + template + static QString htmlDescription( QColor color, T *widget ) + { + // create very large preview swatch + const int width = static_cast< int >( Qgis::UI_SCALE_FACTOR * widget->fontMetrics().horizontalAdvance( 'X' ) * 23 ); + const int height = static_cast< int >( width / 1.61803398875 ); // golden ratio + + const 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; + p.begin( &icon ); + + //start with checkboard pattern + const QBrush checkBrush = QBrush( widget->transparentBackground() ); + p.setPen( Qt::NoPen ); + p.setBrush( checkBrush ); + p.drawRect( margin, margin, width, height ); + + //draw color over pattern + p.setBrush( QBrush( color ) ); + + //draw border + p.setPen( QColor( 197, 197, 197 ) ); + p.drawRect( margin, margin, width, height ); + p.end(); + + QByteArray data; + QBuffer buffer( &data ); + icon.save( &buffer, "PNG", 100 ); + + QString info = QStringLiteral( "HEX %1
" ).arg( color.name() ); + + if ( color.spec() == QColor::Spec::Cmyk ) + { + const float cyan = color.cyanF() * 100.f; + const float magenta = color.magentaF() * 100.f; + const float yellow = color.yellowF() * 100.f; + const float black = color.blackF() * 100.f; + const float alpha = color.alphaF() * 100.f; + + info += QStringLiteral( "CMYKA %1,%2,%3,%4,%5

" ) + .arg( cyan, 0, 'f', 2 ).arg( magenta, 0, 'f', 2 ) + .arg( yellow, 0, 'f', 2 ).arg( black, 0, 'f', 2 ) + .arg( alpha, 0, 'f', 2 ); + } + else + { + const int hue = color.hue(); + const int value = color.value(); + const int saturation = color.saturation(); + + info += QStringLiteral( "RGB %1
" + "HSV %2,%3,%4

" ).arg( QgsSymbolLayerUtils::encodeColor( color ) ) + .arg( hue ).arg( saturation ).arg( value ); + } + + info += QStringLiteral( "" ).arg( QString( data.toBase64() ) ); + + return info; + } +}; + +#endif