36 : QToolButton( parent )
37 , mDialogTitle( dialogTitle.isEmpty() ? tr(
"Symbol Settings" ) : dialogTitle )
41 setAcceptDrops(
true );
42 connect(
this, &QAbstractButton::clicked,
this, &QgsSymbolButton::showSettingsDialog );
45 mMenu =
new QMenu(
this );
46 connect( mMenu, &QMenu::aboutToShow,
this, &QgsSymbolButton::prepareMenu );
48 setPopupMode( QToolButton::MenuButtonPopup );
51 QSize size = QToolButton::minimumSizeHint();
53 mSizeHint = QSize( size.width(), std::max( size.height(), fontHeight ) );
93 void QgsSymbolButton::showSettingsDialog()
96 if ( mExpressionContextGenerator )
121 dialog.setWindowTitle( mDialogTitle );
137 void QgsSymbolButton::updateSymbolFromWidget()
143 void QgsSymbolButton::cleanUpSymbolSelector(
QgsPanelWidget *container )
184 mExpressionContextGenerator = generator;
189 mSymbol.reset( symbol );
196 QColor opaque = color;
197 opaque.setAlphaF( 1.0 );
199 if ( opaque == mSymbol->color() )
202 mSymbol->setColor( opaque );
215 if ( symbol && symbol->type() == mType )
227 bool hasAlpha =
false;
228 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
238 if ( e->button() == Qt::RightButton )
240 QToolButton::showMenu();
243 else if ( e->button() == Qt::LeftButton )
245 mDragStartPosition = e->pos();
247 QToolButton::mousePressEvent( e );
254 if ( !( e->buttons() & Qt::LeftButton ) )
257 QToolButton::mouseMoveEvent( e );
261 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
264 QToolButton::mouseMoveEvent( e );
269 QDrag *drag =
new QDrag(
this );
272 drag->exec( Qt::CopyAction );
280 bool hasAlpha =
false;
282 if ( colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
287 e->acceptProposedAction();
288 updatePreview( mimeColor );
303 bool hasAlpha =
false;
304 if ( colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
307 e->acceptProposedAction();
308 mimeColor.setAlphaF( 1.0 );
309 mSymbol->setColor( mimeColor );
317 void QgsSymbolButton::prepareMenu()
325 QAction *configureAction =
new QAction( tr(
"Configure Symbol…" ),
this );
326 mMenu->addAction( configureAction );
327 connect( configureAction, &QAction::triggered,
this, &QgsSymbolButton::showSettingsDialog );
329 QAction *copySymbolAction =
new QAction( tr(
"Copy Symbol" ),
this );
330 mMenu->addAction( copySymbolAction );
332 QAction *pasteSymbolAction =
new QAction( tr(
"Paste Symbol" ),
this );
336 if ( tempSymbol && tempSymbol->type() == mType )
343 pasteSymbolAction->setEnabled(
false );
345 mMenu->addAction( pasteSymbolAction );
348 mMenu->addSeparator();
351 colorWheel->
setColor( mSymbol->color() );
355 mMenu->addAction( colorAction );
358 QColor alphaColor = mSymbol->color();
359 alphaColor.setAlphaF( mSymbol->opacity() );
365 double opacity = color.alphaF();
366 mSymbol->setOpacity( opacity );
372 mMenu->addAction( alphaAction );
376 QList< QgsColorScheme * >::iterator it = schemeList.begin();
377 for ( ; it != schemeList.end(); ++it )
381 mMenu->addAction( colorAction );
386 mMenu->addSeparator();
388 QAction *copyColorAction =
new QAction( tr(
"Copy Color" ),
this );
389 mMenu->addAction( copyColorAction );
392 QAction *pasteColorAction =
new QAction( tr(
"Paste Color" ),
this );
396 bool hasAlpha =
false;
397 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
399 pasteColorAction->setIcon( createColorIcon( clipColor ) );
403 pasteColorAction->setEnabled(
false );
405 mMenu->addAction( pasteColorAction );
409 void QgsSymbolButton::addRecentColor(
const QColor &color )
417 if ( e->type() == QEvent::EnabledChange )
421 QToolButton::changeEvent( e );
427 QToolButton::showEvent( e );
432 QToolButton::resizeEvent( event );
438 void QgsSymbolButton::updatePreview(
const QColor &color,
QgsSymbol *tempSymbol )
440 std::unique_ptr< QgsSymbol > previewSymbol;
443 previewSymbol.reset( tempSymbol->
clone() );
445 previewSymbol.reset( mSymbol->clone() );
447 if ( color.isValid() )
448 previewSymbol->setColor( color );
450 QSize currentIconSize;
454 if ( !mIconSize.isValid() )
457 QStyleOptionToolButton opt;
458 initStyleOption( &opt );
459 QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
463 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
465 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
468 currentIconSize = mIconSize;
474 currentIconSize = QSize( width() - 10, height() - 6 );
476 currentIconSize = QSize( width() - 10, height() - 12 );
480 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
487 setIconSize( currentIconSize );
493 int height =
static_cast< int >( width / 1.61803398875 );
497 QBuffer buffer( &data );
498 pm.save( &buffer,
"PNG", 100 );
499 setToolTip( QStringLiteral(
"<img src='data:image/png;base64, %3'>" ).arg( QString( data.toBase64() ) ) );
502 bool QgsSymbolButton::colorFromMimeData(
const QMimeData *mimeData, QColor &resultColor,
bool &hasAlpha )
507 if ( mimeColor.isValid() )
509 resultColor = mimeColor;
517 QPixmap QgsSymbolButton::createColorIcon(
const QColor &color )
const 521 QPixmap pixmap( iconSize, iconSize );
522 pixmap.fill( Qt::transparent );
528 p.setBrush( QBrush( color ) );
531 p.setPen( QColor( 197, 197, 197 ) );
532 p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
539 mDialogTitle = title;
549 return mSymbol.get();
A color swatch grid which can be embedded into a menu.
static QgsSymbol * symbolFromMimeData(const QMimeData *data)
Attempts to parse mime data as a symbol.
Abstract base class for all rendered symbols.
static const double UI_SCALE_FACTOR
UI scaling factor.
static QgsLineSymbol * createSimple(const QgsStringMap &properties)
Create a line symbol with one symbol layer: SimpleLine with specified properties. ...
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly...
static QgsFillSymbol * createSimple(const QgsStringMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties. ...
static QIcon symbolPreviewIcon(const QgsSymbol *symbol, QSize size, int padding=0)
Returns an icon preview for a color ramp.
A bar for displaying non-blocking messages to the user.
void colorChanged(const QColor &color)
Emitted when a color has been selected from the widget.
Show scheme in color button drop-down menu.
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
QMap< QString, QString > QgsStringMap
Map canvas is a class for displaying all GIS data types on a canvas.
static QgsStyle * defaultStyle()
Returns default application-wide style.
void setMessageBar(QgsMessageBar *bar)
Sets the message bar associated with the widget.
SymbolType
Type of the symbol.
QList< QgsColorScheme * > schemes() const
Returns all color schemes in the registry.
virtual QgsExpressionContext createExpressionContext() const =0
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Contains settings which reflect the context in which a symbol (or renderer) widget is shown...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setColor(const QColor &color, bool emitSignals=false) override
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
static void addRecentColor(const QColor &color)
Adds a color to the list of recent colors.
void setExpressionContext(QgsExpressionContext *context)
Sets the optional expression context used for the widget.
Abstract interface for generating an expression context.
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application's color scheme registry, used for managing color schemes. ...
void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the symbol widget is shown, e.g., the associated map canvas and expression ...
virtual QgsSymbol * clone() const =0
Returns a deep copy of this symbol.
static QMimeData * colorToMimeData(const QColor &color)
Creates mime data from a color.
static QMimeData * symbolToMimeData(QgsSymbol *symbol)
Creates new mime data from a symbol.
void appendScopes(const QList< QgsExpressionContextScope *> &scopes)
Appends a list of scopes to the end of the context.
static QgsMarkerSymbol * createSimple(const QgsStringMap &properties)
Create a marker symbol with one symbol layer: SimpleMarker with specified properties.
Represents a vector layer which manages a vector based data sets.
static QColor colorFromMimeData(const QMimeData *data, bool &hasAlpha)
Attempts to parse mime data as a color.
void setBaseColor(const QColor &baseColor)
Sets the base color for the color grid.
static QPixmap symbolPreviewPixmap(const QgsSymbol *symbol, QSize size, int padding=0, QgsRenderContext *customContext=nullptr)
Returns a pixmap preview for a color ramp.