24 : QToolButton( parent )
25 , mShape(
QgsStyle::defaultStyle()->defaultPatch(
QgsSymbol::Fill, QSizeF( 10, 5 ) ) )
26 , mDialogTitle( dialogTitle.isEmpty() ? tr(
"Legend Patch Shape" ) : dialogTitle )
30 connect(
this, &QAbstractButton::clicked,
this, &QgsLegendPatchShapeButton::showSettingsDialog );
33 mMenu =
new QMenu(
this );
34 connect( mMenu, &QMenu::aboutToShow,
this, &QgsLegendPatchShapeButton::prepareMenu );
36 setPopupMode( QToolButton::MenuButtonPopup );
39 QSize size = QToolButton::minimumSizeHint();
41 mSizeHint = QSize( size.width(), std::max( size.height(), fontHeight ) );
56 if ( mPreviewSymbol->type() != type )
88 mPreviewSymbol.reset( symbol );
92 void QgsLegendPatchShapeButton::showSettingsDialog()
144 mIsDefault = mShape.
isNull();
172 if ( e->button() == Qt::RightButton )
174 QToolButton::showMenu();
177 QToolButton::mousePressEvent( e );
180 void QgsLegendPatchShapeButton::prepareMenu()
184 QAction *configureAction =
new QAction( tr(
"Configure Patch…" ),
this );
185 mMenu->addAction( configureAction );
186 connect( configureAction, &QAction::triggered,
this, &QgsLegendPatchShapeButton::showSettingsDialog );
188 QAction *defaultAction =
new QAction( tr(
"Reset to Default" ),
this );
189 mMenu->addAction( defaultAction );
190 connect( defaultAction, &QAction::triggered,
this, [ = ] {
setToDefault(); emit
changed(); } );
192 mMenu->addSeparator();
197 for (
const QString &name : qgis::as_const( patchNames ) )
205 QAction *action =
new QAction( name,
this );
206 action->setIcon( icon );
207 connect( action, &QAction::triggered,
this, [ = ] { loadPatchFromStyle( name ); } );
208 mMenu->addAction( action );
214 void QgsLegendPatchShapeButton::loadPatchFromStyle(
const QString &name )
225 if ( e->type() == QEvent::EnabledChange )
229 QToolButton::changeEvent( e );
235 QToolButton::showEvent( e );
240 QToolButton::resizeEvent( event );
246 void QgsLegendPatchShapeButton::updatePreview()
248 QSize currentIconSize;
252 if ( !mIconSize.isValid() )
255 QStyleOptionToolButton opt;
256 initStyleOption( &opt );
257 QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
261 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
263 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
266 currentIconSize = mIconSize;
272 currentIconSize = QSize( width() - 10, height() - 6 );
274 currentIconSize = QSize( width() - 10, height() - 12 );
278 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
285 setIconSize( currentIconSize );
291 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
294 int width =
static_cast< int >(
Qgis::UI_SCALE_FACTOR * fontMetrics().horizontalAdvance(
'X' ) * 23 );
296 int height =
static_cast< int >( width / 1.61803398875 );
300 QBuffer buffer( &data );
301 pm.save( &buffer,
"PNG", 100 );
302 setToolTip( QStringLiteral(
"<img src='data:image/png;base64, %3'>" ).arg( QString( data.toBase64() ) ) );
307 mDialogTitle = title;
static const double UI_SCALE_FACTOR
UI scaling factor.
static QgsFillSymbol * createSimple(const QVariantMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties.
Represents a patch shape for use in map legends.
bool isNull() const
Returns true if the patch shape is a null QgsLegendPatchShape, which indicates that the default legen...
QgsSymbol::SymbolType symbolType() const
Returns the symbol type associated with this patch.
static QgsLineSymbol * createSimple(const QVariantMap &properties)
Create a line symbol with one symbol layer: SimpleLine with specified properties.
static QgsMarkerSymbol * createSimple(const QVariantMap &properties)
Create a marker symbol with one symbol layer: SimpleMarker with specified properties.
A bar for displaying non-blocking messages to the user.
@ LegendPatchShapeEntity
Legend patch shape (since QGIS 3.14)
static QgsStyle * defaultStyle()
Returns default application-wide style.
QgsLegendPatchShape defaultPatch(QgsSymbol::SymbolType type, QSizeF size) const
Returns the default legend patch shape for the given symbol type.
QStringList symbolsOfFavorite(StyleEntity type) const
Returns the symbol names which are flagged as favorite.
QgsLegendPatchShape legendPatchShape(const QString &name) const
Returns the legend patch shape with the specified name.
static QPixmap symbolPreviewPixmap(const QgsSymbol *symbol, QSize size, int padding=0, QgsRenderContext *customContext=nullptr, bool selected=false, const QgsExpressionContext *expressionContext=nullptr, const QgsLegendPatchShape *shape=nullptr)
Returns a pixmap preview for a color ramp.
static QIcon symbolPreviewIcon(const QgsSymbol *symbol, QSize size, int padding=0, QgsLegendPatchShape *shape=nullptr)
Returns an icon preview for a color ramp.
Abstract base class for all rendered symbols.
SymbolType
Type of the symbol.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...