38 : QToolButton( parent )
39 , mDialogTitle( dialogTitle.isEmpty() ? tr(
"Symbol Settings" ) : dialogTitle )
43 setAcceptDrops(
true );
44 connect(
this, &QAbstractButton::clicked,
this, &QgsSymbolButton::showSettingsDialog );
47 mMenu =
new QMenu(
this );
48 connect( mMenu, &QMenu::aboutToShow,
this, &QgsSymbolButton::prepareMenu );
50 setPopupMode( QToolButton::MenuButtonPopup );
53 QSize size = QToolButton::minimumSizeHint();
55 mSizeHint = QSize( size.width(), std::max( size.height(), fontHeight ) );
95 void QgsSymbolButton::showSettingsDialog()
98 if ( mExpressionContextGenerator )
124 dialog.setWindowTitle( mDialogTitle );
140 void QgsSymbolButton::updateSymbolFromWidget()
146 void QgsSymbolButton::cleanUpSymbolSelector(
QgsPanelWidget *container )
187 mExpressionContextGenerator = generator;
192 mSymbol.reset( symbol );
199 QColor opaque = color;
200 opaque.setAlphaF( 1.0 );
202 if ( opaque == mSymbol->color() )
205 mSymbol->setColor( opaque );
218 if ( symbol && symbol->type() == mType )
230 bool hasAlpha =
false;
231 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
248 if ( e->button() == Qt::RightButton )
250 QToolButton::showMenu();
253 else if ( e->button() == Qt::LeftButton )
255 mDragStartPosition = e->pos();
257 QToolButton::mousePressEvent( e );
271 if ( !( e->buttons() & Qt::LeftButton ) )
274 QToolButton::mouseMoveEvent( e );
278 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
281 QToolButton::mouseMoveEvent( e );
286 QDrag *drag =
new QDrag(
this );
289 drag->exec( Qt::CopyAction );
298 stopPicking( e->globalPos() );
303 QToolButton::mouseReleaseEvent( e );
308 if ( !mPickingColor )
311 QToolButton::keyPressEvent( e );
316 stopPicking( QCursor::pos(), e->key() == Qt::Key_Space );
323 bool hasAlpha =
false;
325 if ( colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
330 e->acceptProposedAction();
331 updatePreview( mimeColor );
346 bool hasAlpha =
false;
347 if ( colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
350 e->acceptProposedAction();
351 mimeColor.setAlphaF( 1.0 );
352 mSymbol->setColor( mimeColor );
360 void QgsSymbolButton::prepareMenu()
368 QAction *configureAction =
new QAction( tr(
"Configure Symbol…" ),
this );
369 mMenu->addAction( configureAction );
370 connect( configureAction, &QAction::triggered,
this, &QgsSymbolButton::showSettingsDialog );
372 QAction *copySymbolAction =
new QAction( tr(
"Copy Symbol" ),
this );
373 mMenu->addAction( copySymbolAction );
375 QAction *pasteSymbolAction =
new QAction( tr(
"Paste Symbol" ),
this );
379 if ( tempSymbol && tempSymbol->type() == mType )
386 pasteSymbolAction->setEnabled(
false );
388 mMenu->addAction( pasteSymbolAction );
391 mMenu->addSeparator();
394 colorWheel->
setColor( mSymbol->color() );
398 mMenu->addAction( colorAction );
401 QColor alphaColor = mSymbol->color();
402 alphaColor.setAlphaF( mSymbol->opacity() );
408 double opacity = color.alphaF();
409 mSymbol->setOpacity( opacity );
415 mMenu->addAction( alphaAction );
419 QList< QgsColorScheme * >::iterator it = schemeList.begin();
420 for ( ; it != schemeList.end(); ++it )
424 mMenu->addAction( colorAction );
429 mMenu->addSeparator();
431 QAction *copyColorAction =
new QAction( tr(
"Copy Color" ),
this );
432 mMenu->addAction( copyColorAction );
435 QAction *pasteColorAction =
new QAction( tr(
"Paste Color" ),
this );
439 bool hasAlpha =
false;
440 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
442 pasteColorAction->setIcon( createColorIcon( clipColor ) );
446 pasteColorAction->setEnabled(
false );
448 mMenu->addAction( pasteColorAction );
451 QAction *pickColorAction =
new QAction( tr(
"Pick Color" ),
this );
452 mMenu->addAction( pickColorAction );
453 connect( pickColorAction, &QAction::triggered,
this, &QgsSymbolButton::activatePicker );
455 QAction *chooseColorAction =
new QAction( tr(
"Choose Color…" ),
this );
456 mMenu->addAction( chooseColorAction );
457 connect( chooseColorAction, &QAction::triggered,
this, &QgsSymbolButton::showColorDialog );
460 void QgsSymbolButton::addRecentColor(
const QColor &color )
465 void QgsSymbolButton::activatePicker()
471 mPickingColor =
true;
472 setMouseTracking(
true );
478 if ( e->type() == QEvent::EnabledChange )
482 QToolButton::changeEvent( e );
488 QToolButton::showEvent( e );
493 QToolButton::resizeEvent( event );
499 void QgsSymbolButton::updatePreview(
const QColor &color,
QgsSymbol *tempSymbol )
501 std::unique_ptr< QgsSymbol > previewSymbol;
504 previewSymbol.reset( tempSymbol->
clone() );
506 previewSymbol.reset( mSymbol->clone() );
508 if ( color.isValid() )
509 previewSymbol->setColor( color );
511 QSize currentIconSize;
515 if ( !mIconSize.isValid() )
518 QStyleOptionToolButton opt;
519 initStyleOption( &opt );
520 QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
524 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
526 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
529 currentIconSize = mIconSize;
535 currentIconSize = QSize( width() - 10, height() - 6 );
537 currentIconSize = QSize( width() - 10, height() - 12 );
541 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
548 setIconSize( currentIconSize );
554 int height =
static_cast< int >( width / 1.61803398875 );
558 QBuffer buffer( &data );
559 pm.save( &buffer,
"PNG", 100 );
560 setToolTip( QStringLiteral(
"<img src='data:image/png;base64, %3'>" ).arg( QString( data.toBase64() ) ) );
563 bool QgsSymbolButton::colorFromMimeData(
const QMimeData *mimeData, QColor &resultColor,
bool &hasAlpha )
568 if ( mimeColor.isValid() )
570 resultColor = mimeColor;
578 QPixmap QgsSymbolButton::createColorIcon(
const QColor &color )
const 582 QPixmap pixmap( iconSize, iconSize );
583 pixmap.fill( Qt::transparent );
589 p.setBrush( QBrush( color ) );
592 p.setPen( QColor( 197, 197, 197 ) );
593 p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
598 void QgsSymbolButton::stopPicking( QPoint eventPos,
bool samplingColor )
603 QgsApplication::restoreOverrideCursor();
604 setMouseTracking(
false );
605 mPickingColor =
false;
607 if ( !samplingColor )
616 addRecentColor( newColor );
619 void QgsSymbolButton::showColorDialog()
624 QColor currentColor = mSymbol->color();
629 if ( currentColor.isValid() )
636 if ( newColor.isValid() )
648 dialog.
setTitle( tr(
"Symbol Color" ) );
649 dialog.setAllowOpacity(
true );
651 if ( dialog.exec() && dialog.color().isValid() )
662 mDialogTitle = title;
672 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.
void setTitle(const QString &title)
Sets the title for the color dialog.
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...
static QMimeData * symbolToMimeData(const QgsSymbol *symbol)
Creates new mime data from a symbol.
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.
A custom QGIS dialog for selecting a color.
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 QPixmap symbolPreviewPixmap(const QgsSymbol *symbol, QSize size, int padding=0, QgsRenderContext *customContext=nullptr, bool selected=false, const QgsExpressionContext *expressionContext=nullptr)
Returns a pixmap preview for a color ramp.
static QCursor getThemeCursor(Cursor cursor)
Helper to get a theme cursor.
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 sampleColor(QPoint point)
Samples the color on screen at the specified global point (pixel).
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.