29#include "moc_qgslegendpatchshapebutton.cpp"
31using namespace Qt::StringLiterals;
34 : QToolButton( parent )
35 , mShape(
QgsStyle::defaultStyle()->defaultPatch(
Qgis::SymbolType::Fill, QSizeF( 10, 5 ) ) )
40 connect(
this, &QAbstractButton::clicked,
this, &QgsLegendPatchShapeButton::showSettingsDialog );
43 mMenu =
new QMenu(
this );
44 connect( mMenu, &QMenu::aboutToShow,
this, &QgsLegendPatchShapeButton::prepareMenu );
46 setPopupMode( QToolButton::MenuButtonPopup );
49 QSize size = QToolButton::minimumSizeHint();
51 mSizeHint = QSize( size.width(), std::max( size.height(), fontHeight ) );
68 if ( mPreviewSymbol->type() != type )
100 mPreviewSymbol.reset( symbol );
104void QgsLegendPatchShapeButton::showSettingsDialog()
153 mIsDefault = mShape.isNull();
181 if ( e->button() == Qt::RightButton )
183 QToolButton::showMenu();
186 QToolButton::mousePressEvent( e );
189void QgsLegendPatchShapeButton::prepareMenu()
193 QAction *configureAction =
new QAction( tr(
"Configure Patch…" ),
this );
194 mMenu->addAction( configureAction );
195 connect( configureAction, &QAction::triggered,
this, &QgsLegendPatchShapeButton::showSettingsDialog );
197 QAction *defaultAction =
new QAction( tr(
"Reset to Default" ),
this );
198 mMenu->addAction( defaultAction );
199 connect( defaultAction, &QAction::triggered,
this, [
this] {
204 mMenu->addSeparator();
209 for (
const QString &name : std::as_const( patchNames ) )
212 if (
shape.symbolType() == mType )
217 QAction *action =
new QAction( name,
this );
218 action->setIcon( icon );
219 connect( action, &QAction::triggered,
this, [
this, name] { loadPatchFromStyle( name ); } );
220 mMenu->addAction( action );
226void QgsLegendPatchShapeButton::loadPatchFromStyle(
const QString &name )
237 if ( e->type() == QEvent::EnabledChange )
241 QToolButton::changeEvent( e );
247 QToolButton::showEvent( e );
252 QToolButton::resizeEvent( event );
258void QgsLegendPatchShapeButton::updatePreview()
260 QSize currentIconSize;
264 if ( !mIconSize.isValid() )
267 QStyleOptionToolButton opt;
268 initStyleOption( &opt );
269 QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
this );
272 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
274 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
277 currentIconSize = mIconSize;
283 currentIconSize = QSize( width() - 10, height() - 6 );
285 currentIconSize = QSize( width() - 10, height() - 12 );
289 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
296 setIconSize( currentIconSize );
303 int height =
static_cast<int>( width / 1.61803398875 );
307 QBuffer buffer( &data );
308 pm.save( &buffer,
"PNG", 100 );
309 setToolTip( u
"<img src='data:image/png;base64, %3' width=\"%4\">"_s.arg( QString( data.toBase64() ) ).arg( width ) );
314 mDialogTitle = title;
Provides global constants and enumerations for use throughout the application.
static const double UI_SCALE_FACTOR
UI scaling factor.
static std::unique_ptr< 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.
static std::unique_ptr< QgsLineSymbol > createSimple(const QVariantMap &properties)
Create a line symbol with one symbol layer: SimpleLine with specified properties.
static std::unique_ptr< 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.
A database of saved style entities, including symbols, color ramps, text formats and others.
QgsLegendPatchShape defaultPatch(Qgis::SymbolType type, QSizeF size) const
Returns the default legend patch shape for the given symbol type.
@ LegendPatchShapeEntity
Legend patch shape.
static QgsStyle * defaultStyle(bool initialize=true)
Returns the default application-wide style.
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, const QgsScreenProperties &screen=QgsScreenProperties())
Returns a pixmap preview for a color ramp.
static QIcon symbolPreviewIcon(const QgsSymbol *symbol, QSize size, int padding=0, QgsLegendPatchShape *shape=nullptr, const QgsScreenProperties &screen=QgsScreenProperties())
Returns an icon preview for a color ramp.
Abstract base class for all rendered symbols.
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,...