45#include "moc_qgssymbolbutton.cpp"
47using namespace Qt::StringLiterals;
50 : QToolButton( parent )
56 setAcceptDrops(
true );
57 connect(
this, &QAbstractButton::clicked,
this, &QgsSymbolButton::showSettingsDialog );
60 mMenu =
new QMenu(
this );
61 connect( mMenu, &QMenu::aboutToShow,
this, &QgsSymbolButton::prepareMenu );
63 setPopupMode( QToolButton::MenuButtonPopup );
72void QgsSymbolButton::updateSizeHint()
75 const QSize size = QToolButton::minimumSizeHint();
80 if ( mFixedSizeConstraints )
84 mSizeHint = QSize( size.width(), std::max( size.height(), fontHeight * 3 ) );
85 setMaximumWidth( mSizeHint.height() * 1.5 );
86 setMinimumWidth( maximumWidth() );
90 mSizeHint = QSize( size.width(), fontHeight );
91 setMaximumWidth( 999999 );
92 mSizeHint.setWidth( QToolButton::sizeHint().width() );
97 setMaximumWidth( 999999 );
98 mSizeHint = QSize( size.width(), std::max( size.height(), fontHeight ) );
105 mSizeHint = QSize( size.width(), std::max( size.height(), fontHeight ) );
109 setMinimumHeight( mSizeHint.height() );
153void QgsSymbolButton::showSettingsDialog()
156 if ( mExpressionContextGenerator )
163 std::unique_ptr<QgsSymbol> newSymbol;
166 newSymbol.reset( mSymbol->clone() );
186 QgsSymbolWidgetContext symbolContext;
203 dialog.setWindowTitle( mDialogTitle );
204 dialog.setContext( symbolContext );
252 mExpressionContextGenerator = generator;
257 mDefaultSymbol.reset(
symbol );
262 return mDefaultSymbol.get();
278 QColor opaque = color;
279 opaque.setAlphaF( 1.0 );
281 if ( opaque == mSymbol->color() )
284 mSymbol->setColor( opaque );
309 bool hasAlpha =
false;
310 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
320 if ( e->button() == Qt::LeftButton )
322 mDragStartPosition = e->pos();
324 QToolButton::mousePressEvent( e );
333void QgsSymbolButton::onColorHovered(
const QColor &color )
335 updatePreview( color );
342 if ( !( e->buttons() & Qt::LeftButton ) )
345 QToolButton::mouseMoveEvent( e );
349 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
352 QToolButton::mouseMoveEvent( e );
357 QDrag *drag =
new QDrag(
this );
360 drag->exec( Qt::CopyAction );
368 bool hasAlpha =
false;
370 if ( colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
375 e->acceptProposedAction();
376 updatePreview( mimeColor );
391 bool hasAlpha =
false;
392 if ( colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
395 e->acceptProposedAction();
396 mimeColor.setAlphaF( 1.0 );
397 mSymbol->setColor( mimeColor );
407 if ( isEnabled() && mSymbol )
409 bool symbolChanged =
false;
410 const double increment = ( (
event->modifiers() & Qt::ControlModifier ) ? 0.1 : 1 ) * (
event->angleDelta().y() > 0 ? 1 : -1 );
411 switch ( mSymbol->type() )
415 QgsMarkerSymbol *marker = qgis::down_cast<QgsMarkerSymbol *>( mSymbol.get() );
418 const double size = std::max( 0.0, marker->
size() + increment );
420 symbolChanged =
true;
427 QgsLineSymbol *line = qgis::down_cast<QgsLineSymbol *>( mSymbol.get() );
430 const double width = std::max( 0.0, line->
width() + increment );
432 symbolChanged =
true;
452 QToolButton::wheelEvent( event );
456void QgsSymbolButton::prepareMenu()
464 QAction *configureAction =
new QAction( tr(
"Configure Symbol…" ),
this );
465 mMenu->addAction( configureAction );
466 connect( configureAction, &QAction::triggered,
this, &QgsSymbolButton::showSettingsDialog );
468 QAction *copySymbolAction =
new QAction( tr(
"Copy Symbol" ),
this );
469 copySymbolAction->setEnabled( !
isNull() );
470 mMenu->addAction( copySymbolAction );
475 QAction *pasteSymbolAction =
new QAction( tr(
"Paste Symbol" ),
this );
479 if ( tempSymbol && tempSymbol->type() == mType )
485 pasteSymbolAction->setEnabled(
false );
487 mMenu->addAction( pasteSymbolAction );
492 QAction *nullAction =
new QAction( tr(
"Clear Current Symbol" ),
this );
493 nullAction->setEnabled( !
isNull() );
494 mMenu->addAction( nullAction );
499 if ( mDefaultSymbol )
501 QAction *defaultSymbolAction =
new QAction( tr(
"Default Symbol" ),
this );
503 mMenu->addAction( defaultSymbolAction );
509 mMenu->addSeparator();
511 QgsColorWheel *colorWheel =
new QgsColorWheel( mMenu );
512 colorWheel->
setColor( mSymbol->color() );
513 QgsColorWidgetAction *colorAction =
new QgsColorWidgetAction( colorWheel, mMenu, mMenu );
516 mMenu->addAction( colorAction );
519 QColor alphaColor = mSymbol->color();
520 alphaColor.setAlphaF( mSymbol->opacity() );
522 QgsColorWidgetAction *alphaAction =
new QgsColorWidgetAction( alphaRamp, mMenu, mMenu );
525 const double opacity = color.alphaF();
526 mSymbol->setOpacity( opacity );
531 mMenu->addAction( alphaAction );
535 QList<QgsColorScheme *>::iterator it = schemeList.begin();
536 for ( ; it != schemeList.end(); ++it )
538 QgsColorSwatchGridAction *colorAction =
new QgsColorSwatchGridAction( *it, mMenu, u
"symbology"_s,
this );
540 mMenu->addAction( colorAction );
545 mMenu->addSeparator();
547 QAction *copyColorAction =
new QAction( tr(
"Copy Color" ),
this );
548 mMenu->addAction( copyColorAction );
551 QAction *pasteColorAction =
new QAction( tr(
"Paste Color" ),
this );
555 bool hasAlpha =
false;
556 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
558 pasteColorAction->setIcon( createColorIcon( clipColor ) );
562 pasteColorAction->setEnabled(
false );
564 mMenu->addAction( pasteColorAction );
567 QAction *pickColorAction =
new QAction( tr(
"Pick Color" ),
this );
568 mMenu->addAction( pickColorAction );
569 connect( pickColorAction, &QAction::triggered,
this, &QgsSymbolButton::activatePicker );
571 QAction *chooseColorAction =
new QAction( tr(
"Choose Color…" ),
this );
572 mMenu->addAction( chooseColorAction );
573 connect( chooseColorAction, &QAction::triggered,
this, &QgsSymbolButton::showColorDialog );
577void QgsSymbolButton::addRecentColor(
const QColor &color )
582void QgsSymbolButton::activatePicker()
584 mPickerHelper->activate();
589 if ( e->type() == QEvent::EnabledChange )
593 QToolButton::changeEvent( e );
599 QToolButton::showEvent( e );
604 QToolButton::resizeEvent( event );
610void QgsSymbolButton::updatePreview(
const QColor &color,
QgsSymbol *tempSymbol )
612 QSize currentIconSize;
616 if ( !mIconSize.isValid() )
619 QStyleOptionToolButton opt;
620 initStyleOption( &opt );
621 const QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
this );
624 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
626 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
629 currentIconSize = mIconSize;
635 currentIconSize = QSize( width() - 10, height() - 6 );
637 currentIconSize = QSize( width() - 10, height() - 12 );
641 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
646 std::unique_ptr<QgsSymbol> previewSymbol;
650 previewSymbol.reset( tempSymbol->
clone() );
654 previewSymbol.reset( mSymbol->clone() );
658 setIconSize( currentIconSize );
660 setToolTip( QString() );
664 if ( color.isValid() )
665 previewSymbol->setColor( color );
669 setIconSize( currentIconSize );
674 const int width =
static_cast<int>(
Qgis::UI_SCALE_FACTOR * fontMetrics().horizontalAdvance(
'X' ) * 23 );
675 const int height =
static_cast<int>( width / 1.61803398875 );
679 QBuffer buffer( &data );
680 pm.save( &buffer,
"PNG", 100 );
681 setToolTip( u
"<img src='data:image/png;base64, %3' width=\"%4\">"_s.arg( QString( data.toBase64() ) ).arg( width ) );
684bool QgsSymbolButton::colorFromMimeData(
const QMimeData *mimeData, QColor &resultColor,
bool &hasAlpha )
689 if ( mimeColor.isValid() )
691 resultColor = mimeColor;
699QPixmap QgsSymbolButton::createColorIcon(
const QColor &color )
const
703 QPixmap pixmap( iconSize, iconSize );
704 pixmap.fill( Qt::transparent );
710 p.setBrush( QBrush( color ) );
713 p.setPen( QColor( 197, 197, 197 ) );
714 p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
720void QgsSymbolButton::onColorPicked(
const QColor &color )
723 addRecentColor( color );
726void QgsSymbolButton::colorPickingCanceled()
731void QgsSymbolButton::showColorDialog()
739 const QColor currentColor = mSymbol->color();
744 if ( currentColor.isValid() )
750 if ( newColor.isValid() )
759 QgsColorDialog dialog(
this, Qt::WindowFlags(), mSymbol->color() );
760 dialog.setTitle( tr(
"Symbol Color" ) );
761 dialog.setAllowOpacity(
true );
763 if ( dialog.exec() && dialog.color().isValid() )
774 mDialogTitle = title;
784 return mSymbol.get();
806 return mFixedSizeConstraints;
821 if ( !mDefaultSymbol )
static const double UI_SCALE_FACTOR
UI scaling factor.
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application's color scheme registry, used for managing color schemes.
A utility class for sampling colors from the screen.
void colorPicked(const QColor &color)
Emitted when the user has finished picking a color from the screen.
void colorHovered(const QColor &color)
Emitted when the user hovers over a color from the screen.
void canceled()
Emitted when the user cancels the picking operation.
QList< QgsColorScheme * > schemes() const
Returns all color schemes in the registry.
@ ShowInColorButtonMenu
Show scheme in color button drop-down menu.
void setBaseColor(const QColor &baseColor)
Sets the base color for the color grid.
void colorChanged(const QColor &color)
Emitted when a color has been selected from the widget.
void setColor(const QColor &color, bool emitSignals=false) override
Abstract interface for generating an expression context.
virtual QgsExpressionContext createExpressionContext() const =0
This method needs to be reimplemented in all classes which implement this interface and return an exp...
static std::vector< std::unique_ptr< QgsExpressionContextScope > > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void appendScopes(const QList< QgsExpressionContextScope * > &scopes)
Appends a list of scopes to the end of the context.
static std::unique_ptr< QgsFillSymbol > createSimple(const QVariantMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties.
A line symbol type, for rendering LineString and MultiLineString geometries.
void setWidth(double width) const
Sets the width for the whole line symbol.
static std::unique_ptr< QgsLineSymbol > createSimple(const QVariantMap &properties)
Create a line symbol with one symbol layer: SimpleLine with specified properties.
double width() const
Returns the estimated width for the whole symbol, which is the maximum width of all marker symbol lay...
Map canvas is a class for displaying all GIS data types on a canvas.
A marker symbol type, for rendering Point and MultiPoint geometries.
void setSize(double size) const
Sets the size for the whole symbol.
static std::unique_ptr< QgsMarkerSymbol > createSimple(const QVariantMap &properties)
Create a marker symbol with one symbol layer: SimpleMarker with specified properties.
double size() const
Returns the estimated size for the whole symbol, which is the maximum size of all marker symbol layer...
A bar for displaying non-blocking messages to the user.
static void addRecentColor(const QColor &color)
Adds a color to the list of recent colors.
Stores properties relating to a screen.
static QgsStyle * defaultStyle(bool initialize=true)
Returns the default application-wide style.
static std::unique_ptr< QgsSymbol > symbolFromMimeData(const QMimeData *data)
Attempts to parse mime data as a symbol.
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 std::unique_ptr< QMimeData > symbolToMimeData(const QgsSymbol *symbol)
Creates new mime data from a symbol.
static QColor colorFromMimeData(const QMimeData *data, bool &hasAlpha)
Attempts to parse mime data as a color.
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.
static std::unique_ptr< QMimeData > colorToMimeData(const QColor &color)
Creates mime data from a color.
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
void setMessageBar(QgsMessageBar *bar)
Sets the message bar associated with the widget.
void setExpressionContext(QgsExpressionContext *context)
Sets the optional expression context used for the widget.
Abstract base class for all rendered symbols.
virtual QgsSymbol * clone() const =0
Returns a deep copy of this symbol.
static std::unique_ptr< QgsSymbol > defaultSymbol(Qgis::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
Represents a vector layer which manages a vector based dataset.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...