33 : QToolButton( parent )
34 , mDialogTitle( dialogTitle.isEmpty() ? tr(
"Symbol Settings" ) : dialogTitle )
38 setAcceptDrops(
true );
39 connect(
this, &QAbstractButton::clicked,
this, &QgsSymbolButton::showSettingsDialog );
42 mMenu =
new QMenu(
this );
43 connect( mMenu, &QMenu::aboutToShow,
this, &QgsSymbolButton::prepareMenu );
45 setPopupMode( QToolButton::MenuButtonPopup );
48 QSize size = QToolButton::minimumSizeHint();
50 mSizeHint = QSize( size.width(), std::max( size.height(), fontHeight ) );
90 void QgsSymbolButton::showSettingsDialog()
93 if ( mExpressionContextGenerator )
117 dialog.setWindowTitle( mDialogTitle );
133 void QgsSymbolButton::updateSymbolFromWidget()
139 void QgsSymbolButton::cleanUpSymbolSelector(
QgsPanelWidget *container )
170 mExpressionContextGenerator = generator;
175 mSymbol.reset( symbol );
182 QColor opaque = color;
183 opaque.setAlphaF( 1.0 );
185 if ( opaque == mSymbol->color() )
188 mSymbol->setColor( opaque );
201 if ( symbol && symbol->type() == mType )
213 bool hasAlpha =
false;
214 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
224 if ( e->button() == Qt::RightButton )
226 QToolButton::showMenu();
229 else if ( e->button() == Qt::LeftButton )
231 mDragStartPosition = e->pos();
233 QToolButton::mousePressEvent( e );
240 if ( !( e->buttons() & Qt::LeftButton ) )
243 QToolButton::mouseMoveEvent( e );
247 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
250 QToolButton::mouseMoveEvent( e );
255 QDrag *drag =
new QDrag(
this );
258 drag->exec( Qt::CopyAction );
266 bool hasAlpha =
false;
268 if ( colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
273 e->acceptProposedAction();
274 updatePreview( mimeColor );
289 bool hasAlpha =
false;
290 if ( colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
293 e->acceptProposedAction();
294 mimeColor.setAlphaF( 1.0 );
295 mSymbol->setColor( mimeColor );
303 void QgsSymbolButton::prepareMenu()
311 QAction *configureAction =
new QAction( tr(
"Configure Symbol…" ),
this );
312 mMenu->addAction( configureAction );
313 connect( configureAction, &QAction::triggered,
this, &QgsSymbolButton::showSettingsDialog );
315 QAction *copySymbolAction =
new QAction( tr(
"Copy Symbol" ),
this );
316 mMenu->addAction( copySymbolAction );
318 QAction *pasteSymbolAction =
new QAction( tr(
"Paste Symbol" ),
this );
322 if ( tempSymbol && tempSymbol->type() == mType )
329 pasteSymbolAction->setEnabled(
false );
331 mMenu->addAction( pasteSymbolAction );
334 mMenu->addSeparator();
337 colorWheel->
setColor( mSymbol->color() );
341 mMenu->addAction( colorAction );
344 QColor alphaColor = mSymbol->color();
345 alphaColor.setAlphaF( mSymbol->opacity() );
351 double opacity = color.alphaF();
352 mSymbol->setOpacity( opacity );
358 mMenu->addAction( alphaAction );
362 QList< QgsColorScheme * >::iterator it = schemeList.begin();
363 for ( ; it != schemeList.end(); ++it )
367 mMenu->addAction( colorAction );
372 mMenu->addSeparator();
374 QAction *copyColorAction =
new QAction( tr(
"Copy Color" ),
this );
375 mMenu->addAction( copyColorAction );
378 QAction *pasteColorAction =
new QAction( tr(
"Paste Color" ),
this );
382 bool hasAlpha =
false;
383 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
385 pasteColorAction->setIcon( createColorIcon( clipColor ) );
389 pasteColorAction->setEnabled(
false );
391 mMenu->addAction( pasteColorAction );
395 void QgsSymbolButton::addRecentColor(
const QColor &color )
403 if ( e->type() == QEvent::EnabledChange )
407 QToolButton::changeEvent( e );
413 QToolButton::showEvent( e );
418 QToolButton::resizeEvent( event );
424 void QgsSymbolButton::updatePreview(
const QColor &color,
QgsSymbol *tempSymbol )
426 std::unique_ptr< QgsSymbol > previewSymbol;
429 previewSymbol.reset( tempSymbol->
clone() );
431 previewSymbol.reset( mSymbol->clone() );
433 if ( color.isValid() )
434 previewSymbol->setColor( color );
436 QSize currentIconSize;
440 if ( !mIconSize.isValid() )
443 QStyleOptionToolButton opt;
444 initStyleOption( &opt );
445 QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
449 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
451 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
454 currentIconSize = mIconSize;
460 currentIconSize = QSize( width() - 10, height() - 6 );
462 currentIconSize = QSize( width() - 10, height() - 12 );
466 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
473 setIconSize( currentIconSize );
479 int height =
static_cast< int >( width / 1.61803398875 );
483 QBuffer buffer( &data );
484 pm.save( &buffer,
"PNG", 100 );
485 setToolTip( QStringLiteral(
"<img src='data:image/png;base64, %3'>" ).arg( QString( data.toBase64() ) ) );
488 bool QgsSymbolButton::colorFromMimeData(
const QMimeData *mimeData, QColor &resultColor,
bool &hasAlpha )
493 if ( mimeColor.isValid() )
495 resultColor = mimeColor;
503 QPixmap QgsSymbolButton::createColorIcon(
const QColor &color )
const 507 QPixmap pixmap( iconSize, iconSize );
508 pixmap.fill( Qt::transparent );
514 p.setBrush( QBrush( color ) );
517 p.setPen( QColor( 197, 197, 197 ) );
518 p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
525 mDialogTitle = title;
535 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 QPixmap symbolPreviewPixmap(QgsSymbol *symbol, QSize size, int padding=0, QgsRenderContext *customContext=nullptr)
Returns a pixmap preview for a color ramp.
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. ...
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.
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...
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.
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.
static QgsMarkerSymbol * createSimple(const QgsStringMap &properties)
Create a marker symbol with one symbol layer: SimpleMarker with specified properties.
void appendScopes(const QList< QgsExpressionContextScope * > &scopes)
Appends a list of scopes to the end of the context.
Represents a vector layer which manages a vector based data sets.
QList< QgsColorScheme * > schemes() const
Returns all color schemes in the registry.
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.