42 : QToolButton( parent )
43 , mDialogTitle( dialogTitle.isEmpty() ? tr(
"Symbol Settings" ) : dialogTitle )
47 setAcceptDrops(
true );
48 connect(
this, &QAbstractButton::clicked,
this, &QgsSymbolButton::showSettingsDialog );
51 mMenu =
new QMenu(
this );
52 connect( mMenu, &QMenu::aboutToShow,
this, &QgsSymbolButton::prepareMenu );
54 setPopupMode( QToolButton::MenuButtonPopup );
59void QgsSymbolButton::updateSizeHint()
62 const QSize size = QToolButton::minimumSizeHint();
69 mSizeHint = QSize( size.width(), std::max( size.height(), fontHeight * 3 ) );
70 setMaximumWidth( mSizeHint.height() * 1.5 );
71 setMinimumWidth( maximumWidth() );
75 mSizeHint = QSize( size.width(), fontHeight );
76 setMaximumWidth( 999999 );
77 mSizeHint.setWidth( QToolButton::sizeHint().width() );
84 mSizeHint = QSize( size.width(), std::max( size.height(), fontHeight ) );
88 setMinimumHeight( mSizeHint.height( ) );
132void QgsSymbolButton::showSettingsDialog()
135 if ( mExpressionContextGenerator )
142 std::unique_ptr< QgsSymbol > newSymbol;
145 newSymbol.reset( mSymbol->clone() );
182 dialog.setWindowTitle( mDialogTitle );
183 dialog.setContext( symbolContext );
231 mExpressionContextGenerator = generator;
236 mDefaultSymbol.reset(
symbol );
241 return mDefaultSymbol.get();
257 QColor opaque = color;
258 opaque.setAlphaF( 1.0 );
260 if ( opaque == mSymbol->color() )
263 mSymbol->setColor( opaque );
288 bool hasAlpha =
false;
289 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
306 if ( e->button() == Qt::RightButton )
308 QToolButton::showMenu();
311 else if ( e->button() == Qt::LeftButton )
313 mDragStartPosition = e->pos();
315 QToolButton::mousePressEvent( e );
329 if ( !( e->buttons() & Qt::LeftButton ) )
332 QToolButton::mouseMoveEvent( e );
336 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
339 QToolButton::mouseMoveEvent( e );
344 QDrag *drag =
new QDrag(
this );
347 drag->exec( Qt::CopyAction );
356 stopPicking( e->globalPos() );
361 QToolButton::mouseReleaseEvent( e );
366 if ( !mPickingColor )
369 QToolButton::keyPressEvent( e );
374 stopPicking( QCursor::pos(), e->key() == Qt::Key_Space );
381 bool hasAlpha =
false;
383 if ( colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
388 e->acceptProposedAction();
389 updatePreview( mimeColor );
404 bool hasAlpha =
false;
405 if ( colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
408 e->acceptProposedAction();
409 mimeColor.setAlphaF( 1.0 );
410 mSymbol->setColor( mimeColor );
420 if ( isEnabled() && mSymbol )
422 bool symbolChanged =
false;
423 const double increment = ( (
event->modifiers() & Qt::ControlModifier ) ? 0.1 : 1 ) *
424 ( event->angleDelta().y() > 0 ? 1 : -1 );
425 switch ( mSymbol->type() )
429 QgsMarkerSymbol *marker = qgis::down_cast<QgsMarkerSymbol *>( mSymbol.get() );
432 const double size = std::max( 0.0, marker->
size() + increment );
434 symbolChanged =
true;
441 QgsLineSymbol *line = qgis::down_cast<QgsLineSymbol *>( mSymbol.get() );
444 const double width = std::max( 0.0, line->
width() + increment );
446 symbolChanged =
true;
466 QToolButton::wheelEvent( event );
470void QgsSymbolButton::prepareMenu()
478 QAction *configureAction =
new QAction( tr(
"Configure Symbol…" ),
this );
479 mMenu->addAction( configureAction );
480 connect( configureAction, &QAction::triggered,
this, &QgsSymbolButton::showSettingsDialog );
482 QAction *copySymbolAction =
new QAction( tr(
"Copy Symbol" ),
this );
483 copySymbolAction->setEnabled( !
isNull() );
484 mMenu->addAction( copySymbolAction );
489 QAction *pasteSymbolAction =
new QAction( tr(
"Paste Symbol" ),
this );
493 if ( tempSymbol && tempSymbol->type() == mType )
499 pasteSymbolAction->setEnabled(
false );
501 mMenu->addAction( pasteSymbolAction );
506 QAction *nullAction =
new QAction( tr(
"Clear Current Symbol" ),
this );
507 nullAction->setEnabled( !
isNull() );
508 mMenu->addAction( nullAction );
513 if ( mDefaultSymbol )
515 QAction *defaultSymbolAction =
new QAction( tr(
"Default Symbol" ),
this );
517 mMenu->addAction( defaultSymbolAction );
523 mMenu->addSeparator();
526 colorWheel->
setColor( mSymbol->color() );
530 mMenu->addAction( colorAction );
533 QColor alphaColor = mSymbol->color();
534 alphaColor.setAlphaF( mSymbol->opacity() );
540 const double opacity = color.alphaF();
541 mSymbol->setOpacity( opacity );
547 mMenu->addAction( alphaAction );
551 QList< QgsColorScheme * >::iterator it = schemeList.begin();
552 for ( ; it != schemeList.end(); ++it )
556 mMenu->addAction( colorAction );
561 mMenu->addSeparator();
563 QAction *copyColorAction =
new QAction( tr(
"Copy Color" ),
this );
564 mMenu->addAction( copyColorAction );
567 QAction *pasteColorAction =
new QAction( tr(
"Paste Color" ),
this );
571 bool hasAlpha =
false;
572 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
574 pasteColorAction->setIcon( createColorIcon( clipColor ) );
578 pasteColorAction->setEnabled(
false );
580 mMenu->addAction( pasteColorAction );
583 QAction *pickColorAction =
new QAction( tr(
"Pick Color" ),
this );
584 mMenu->addAction( pickColorAction );
585 connect( pickColorAction, &QAction::triggered,
this, &QgsSymbolButton::activatePicker );
587 QAction *chooseColorAction =
new QAction( tr(
"Choose Color…" ),
this );
588 mMenu->addAction( chooseColorAction );
589 connect( chooseColorAction, &QAction::triggered,
this, &QgsSymbolButton::showColorDialog );
593void QgsSymbolButton::addRecentColor(
const QColor &color )
598void QgsSymbolButton::activatePicker()
604 mPickingColor =
true;
605 setMouseTracking(
true );
611 if ( e->type() == QEvent::EnabledChange )
615 QToolButton::changeEvent( e );
621 QToolButton::showEvent( e );
626 QToolButton::resizeEvent( event );
632void QgsSymbolButton::updatePreview(
const QColor &color,
QgsSymbol *tempSymbol )
634 QSize currentIconSize;
638 if ( !mIconSize.isValid() )
641 QStyleOptionToolButton opt;
642 initStyleOption( &opt );
643 const QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
647 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
649 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
652 currentIconSize = mIconSize;
658 currentIconSize = QSize( width() - 10, height() - 6 );
660 currentIconSize = QSize( width() - 10, height() - 12 );
664 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
669 std::unique_ptr< QgsSymbol > previewSymbol;
673 previewSymbol.reset( tempSymbol->
clone() );
677 previewSymbol.reset( mSymbol->clone() );
681 setIconSize( currentIconSize );
683 setToolTip( QString( ) );
687 if ( color.isValid() )
688 previewSymbol->setColor( color );
692 setIconSize( currentIconSize );
697 const int width =
static_cast< int >(
Qgis::UI_SCALE_FACTOR * fontMetrics().horizontalAdvance(
'X' ) * 23 );
698 const int height =
static_cast< int >( width / 1.61803398875 );
702 QBuffer buffer( &data );
703 pm.save( &buffer,
"PNG", 100 );
704 setToolTip( QStringLiteral(
"<img src='data:image/png;base64, %3' width=\"%4\">" ).arg( QString( data.toBase64() ) ).arg( width ) );
707bool QgsSymbolButton::colorFromMimeData(
const QMimeData *mimeData, QColor &resultColor,
bool &hasAlpha )
712 if ( mimeColor.isValid() )
714 resultColor = mimeColor;
722QPixmap QgsSymbolButton::createColorIcon(
const QColor &color )
const
726 QPixmap pixmap( iconSize, iconSize );
727 pixmap.fill( Qt::transparent );
733 p.setBrush( QBrush( color ) );
736 p.setPen( QColor( 197, 197, 197 ) );
737 p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
742void QgsSymbolButton::stopPicking( QPoint eventPos,
bool samplingColor )
747 QgsApplication::restoreOverrideCursor();
748 setMouseTracking(
false );
749 mPickingColor =
false;
751 if ( !samplingColor )
760 addRecentColor( newColor );
763void QgsSymbolButton::showColorDialog()
771 const QColor currentColor = mSymbol->color();
776 if ( currentColor.isValid() )
783 if ( newColor.isValid() )
785 setColor( newColor );
792 QgsColorDialog dialog(
this, Qt::WindowFlags(), mSymbol->color() );
793 dialog.setTitle( tr(
"Symbol Color" ) );
794 dialog.setAllowOpacity(
true );
796 if ( dialog.exec() && dialog.color().isValid() )
807 mDialogTitle = title;
817 return mSymbol.get();
842 if ( !mDefaultSymbol )
SymbolType
Attribute editing capabilities which may be supported by vector data providers.
static const double UI_SCALE_FACTOR
UI scaling factor.
static QCursor getThemeCursor(Cursor cursor)
Helper to get a theme cursor.
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application's color scheme registry, used for managing color schemes.
@ Sampler
Color/Value picker.
A custom QGIS dialog for selecting a color.
QList< QgsColorScheme * > schemes() const
Returns all color schemes in the registry.
@ ShowInColorButtonMenu
Show scheme in color button drop-down menu.
A color swatch grid which can be embedded into a 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 QList< 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 QgsFillSymbol * createSimple(const QVariantMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties.
static QColor sampleColor(QPoint point)
Samples the color on screen at the specified global point (pixel).
A line symbol type, for rendering LineString and MultiLineString geometries.
void setWidth(double width) const
Sets the width for the whole line symbol.
double width() const
Returns the estimated width for the whole symbol, which is the maximum width of all marker symbol lay...
static QgsLineSymbol * createSimple(const QVariantMap &properties)
Create a line symbol with one symbol layer: SimpleLine with specified properties.
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 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()
Returns default application-wide style.
static 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 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 QMimeData * colorToMimeData(const QColor &color)
Creates mime data from a color.
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
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.
Qgis::SymbolType type() const
Returns the symbol's type.
static QgsSymbol * defaultSymbol(Qgis::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
Represents a vector layer which manages a vector based data sets.
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,...