32 : QToolButton( parent )
33 , mDialogTitle( dialogTitle.isEmpty() ? tr(
"Symbol Settings" ) : dialogTitle )
37 setAcceptDrops(
true );
38 connect(
this, &QAbstractButton::clicked,
this, &QgsSymbolButton::showSettingsDialog );
41 mMenu =
new QMenu(
this );
42 connect( mMenu, &QMenu::aboutToShow,
this, &QgsSymbolButton::prepareMenu );
44 setPopupMode( QToolButton::MenuButtonPopup );
47 QSize size = QToolButton::minimumSizeHint();
49 mSizeHint = QSize( size.width(), std::max( size.height(), fontHeight ) );
89 void QgsSymbolButton::showSettingsDialog()
92 if ( mExpressionContextGenerator )
116 dialog.setWindowTitle( mDialogTitle );
132 void QgsSymbolButton::updateSymbolFromWidget()
138 void QgsSymbolButton::cleanUpSymbolSelector(
QgsPanelWidget *container )
169 mExpressionContextGenerator = generator;
174 mSymbol.reset( symbol );
181 QColor opaque = color;
182 opaque.setAlphaF( 1.0 );
184 if ( opaque == mSymbol->color() )
187 mSymbol->setColor( opaque );
200 if ( symbol && symbol->type() == mType )
212 bool hasAlpha =
false;
213 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
223 if ( e->button() == Qt::RightButton )
225 QToolButton::showMenu();
228 else if ( e->button() == Qt::LeftButton )
230 mDragStartPosition = e->pos();
232 QToolButton::mousePressEvent( e );
239 if ( !( e->buttons() & Qt::LeftButton ) )
242 QToolButton::mouseMoveEvent( e );
246 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
249 QToolButton::mouseMoveEvent( e );
254 QDrag *drag =
new QDrag(
this );
257 drag->exec( Qt::CopyAction );
265 bool hasAlpha =
false;
267 if ( colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
272 e->acceptProposedAction();
273 updatePreview( mimeColor );
288 bool hasAlpha =
false;
289 if ( colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
292 e->acceptProposedAction();
293 mimeColor.setAlphaF( 1.0 );
294 mSymbol->setColor( mimeColor );
302 void QgsSymbolButton::prepareMenu()
310 QAction *configureAction =
new QAction( tr(
"Configure Symbol…" ),
this );
311 mMenu->addAction( configureAction );
312 connect( configureAction, &QAction::triggered,
this, &QgsSymbolButton::showSettingsDialog );
314 QAction *copySymbolAction =
new QAction( tr(
"Copy Symbol" ),
this );
315 mMenu->addAction( copySymbolAction );
317 QAction *pasteSymbolAction =
new QAction( tr(
"Paste Symbol" ),
this );
321 if ( tempSymbol && tempSymbol->type() == mType )
327 pasteSymbolAction->setEnabled(
false );
329 mMenu->addAction( pasteSymbolAction );
332 mMenu->addSeparator();
335 colorWheel->
setColor( mSymbol->color() );
339 mMenu->addAction( colorAction );
342 QColor alphaColor = mSymbol->color();
343 alphaColor.setAlphaF( mSymbol->opacity() );
349 double opacity = color.alphaF();
350 mSymbol->setOpacity( opacity );
356 mMenu->addAction( alphaAction );
360 QList< QgsColorScheme * >::iterator it = schemeList.begin();
361 for ( ; it != schemeList.end(); ++it )
365 mMenu->addAction( colorAction );
370 mMenu->addSeparator();
372 QAction *copyColorAction =
new QAction( tr(
"Copy Color" ),
this );
373 mMenu->addAction( copyColorAction );
376 QAction *pasteColorAction =
new QAction( tr(
"Paste Color" ),
this );
380 bool hasAlpha =
false;
381 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
383 pasteColorAction->setIcon( createColorIcon( clipColor ) );
387 pasteColorAction->setEnabled(
false );
389 mMenu->addAction( pasteColorAction );
393 void QgsSymbolButton::addRecentColor(
const QColor &color )
401 if ( e->type() == QEvent::EnabledChange )
405 QToolButton::changeEvent( e );
411 QToolButton::showEvent( e );
416 QToolButton::resizeEvent( event );
422 void QgsSymbolButton::updatePreview(
const QColor &color,
QgsSymbol *tempSymbol )
424 std::unique_ptr< QgsSymbol > previewSymbol;
427 previewSymbol.reset( tempSymbol->
clone() );
429 previewSymbol.reset( mSymbol->clone() );
431 if ( color.isValid() )
432 previewSymbol->setColor( color );
434 QSize currentIconSize;
438 if ( !mIconSize.isValid() )
441 QStyleOptionToolButton opt;
442 initStyleOption( &opt );
443 QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
447 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
449 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
452 currentIconSize = mIconSize;
458 currentIconSize = QSize( width() - 10, height() - 6 );
460 currentIconSize = QSize( width() - 10, height() - 12 );
464 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
471 setIconSize( currentIconSize );
475 bool QgsSymbolButton::colorFromMimeData(
const QMimeData *mimeData, QColor &resultColor,
bool &hasAlpha )
480 if ( mimeColor.isValid() )
482 resultColor = mimeColor;
490 QPixmap QgsSymbolButton::createColorIcon(
const QColor &color )
const 493 QPixmap pixmap( 16, 16 );
494 pixmap.fill( Qt::transparent );
500 p.setBrush( QBrush( color ) );
503 p.setPen( QColor( 197, 197, 197 ) );
504 p.drawRect( 0, 0, 15, 15 );
511 mDialogTitle = title;
521 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.
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. ...
static QgsFillSymbol * createSimple(const QgsStringMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties. ...
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.
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...
static QIcon symbolPreviewIcon(QgsSymbol *symbol, QSize size, int padding=0)
Returns an icon preview for a color ramp.
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...
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
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
Get 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 setColor(const QColor &color, const bool emitSignals=false) override
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.