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 );
57 const QSize size = QToolButton::minimumSizeHint();
59 mSizeHint = QSize( size.width(), std::max( size.height(), fontHeight ) );
101 void QgsSymbolButton::showSettingsDialog()
104 if ( mExpressionContextGenerator )
114 newSymbol = mSymbol->clone();
152 dialog.setWindowTitle( mDialogTitle );
153 dialog.setContext( symbolContext );
168 void QgsSymbolButton::updateSymbolFromWidget()
174 void QgsSymbolButton::cleanUpSymbolSelector(
QgsPanelWidget *container )
215 mExpressionContextGenerator = generator;
230 QColor opaque = color;
231 opaque.setAlphaF( 1.0 );
233 if ( opaque == mSymbol->color() )
236 mSymbol->setColor( opaque );
261 bool hasAlpha =
false;
262 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
279 if ( e->button() == Qt::RightButton )
281 QToolButton::showMenu();
284 else if ( e->button() == Qt::LeftButton )
286 mDragStartPosition = e->pos();
288 QToolButton::mousePressEvent( e );
302 if ( !( e->buttons() & Qt::LeftButton ) )
305 QToolButton::mouseMoveEvent( e );
309 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
312 QToolButton::mouseMoveEvent( e );
317 QDrag *drag =
new QDrag(
this );
320 drag->exec( Qt::CopyAction );
329 stopPicking( e->globalPos() );
334 QToolButton::mouseReleaseEvent( e );
339 if ( !mPickingColor )
342 QToolButton::keyPressEvent( e );
347 stopPicking( QCursor::pos(), e->key() == Qt::Key_Space );
354 bool hasAlpha =
false;
356 if ( colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
361 e->acceptProposedAction();
362 updatePreview( mimeColor );
377 bool hasAlpha =
false;
378 if ( colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
381 e->acceptProposedAction();
382 mimeColor.setAlphaF( 1.0 );
383 mSymbol->setColor( mimeColor );
393 if ( isEnabled() && mSymbol )
395 bool symbolChanged =
false;
396 const double increment = ( (
event->modifiers() & Qt::ControlModifier ) ? 0.1 : 1 ) *
397 (
event->angleDelta().y() > 0 ? 1 : -1 );
398 switch ( mSymbol->type() )
402 QgsMarkerSymbol *marker = qgis::down_cast<QgsMarkerSymbol *>( mSymbol.get() );
405 const double size = std::max( 0.0, marker->
size() + increment );
407 symbolChanged =
true;
414 QgsLineSymbol *line = qgis::down_cast<QgsLineSymbol *>( mSymbol.get() );
417 const double width = std::max( 0.0, line->
width() + increment );
419 symbolChanged =
true;
439 QToolButton::wheelEvent( event );
443 void QgsSymbolButton::prepareMenu()
451 QAction *configureAction =
new QAction( tr(
"Configure Symbol…" ),
this );
452 mMenu->addAction( configureAction );
453 connect( configureAction, &QAction::triggered,
this, &QgsSymbolButton::showSettingsDialog );
455 QAction *copySymbolAction =
new QAction( tr(
"Copy Symbol" ),
this );
456 copySymbolAction->setEnabled( !
isNull() );
457 mMenu->addAction( copySymbolAction );
460 QAction *pasteSymbolAction =
new QAction( tr(
"Paste Symbol" ),
this );
464 if ( tempSymbol && tempSymbol->type() == mType )
471 pasteSymbolAction->setEnabled(
false );
473 mMenu->addAction( pasteSymbolAction );
478 QAction *nullAction =
new QAction( tr(
"Clear Current Symbol" ),
this );
479 nullAction->setEnabled( !
isNull() );
480 mMenu->addAction( nullAction );
486 mMenu->addSeparator();
489 colorWheel->
setColor( mSymbol->color() );
493 mMenu->addAction( colorAction );
496 QColor alphaColor = mSymbol->color();
497 alphaColor.setAlphaF( mSymbol->opacity() );
503 const double opacity = color.alphaF();
504 mSymbol->setOpacity( opacity );
510 mMenu->addAction( alphaAction );
514 QList< QgsColorScheme * >::iterator it = schemeList.begin();
515 for ( ; it != schemeList.end(); ++it )
519 mMenu->addAction( colorAction );
524 mMenu->addSeparator();
526 QAction *copyColorAction =
new QAction( tr(
"Copy Color" ),
this );
527 mMenu->addAction( copyColorAction );
530 QAction *pasteColorAction =
new QAction( tr(
"Paste Color" ),
this );
534 bool hasAlpha =
false;
535 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
537 pasteColorAction->setIcon( createColorIcon( clipColor ) );
541 pasteColorAction->setEnabled(
false );
543 mMenu->addAction( pasteColorAction );
546 QAction *pickColorAction =
new QAction( tr(
"Pick Color" ),
this );
547 mMenu->addAction( pickColorAction );
548 connect( pickColorAction, &QAction::triggered,
this, &QgsSymbolButton::activatePicker );
550 QAction *chooseColorAction =
new QAction( tr(
"Choose Color…" ),
this );
551 mMenu->addAction( chooseColorAction );
552 connect( chooseColorAction, &QAction::triggered,
this, &QgsSymbolButton::showColorDialog );
556 void QgsSymbolButton::addRecentColor(
const QColor &color )
561 void QgsSymbolButton::activatePicker()
567 mPickingColor =
true;
568 setMouseTracking(
true );
574 if ( e->type() == QEvent::EnabledChange )
578 QToolButton::changeEvent( e );
584 QToolButton::showEvent( e );
589 QToolButton::resizeEvent( event );
595 void QgsSymbolButton::updatePreview(
const QColor &color,
QgsSymbol *tempSymbol )
597 QSize currentIconSize;
601 if ( !mIconSize.isValid() )
604 QStyleOptionToolButton opt;
605 initStyleOption( &opt );
606 const QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
610 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
612 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
615 currentIconSize = mIconSize;
621 currentIconSize = QSize( width() - 10, height() - 6 );
623 currentIconSize = QSize( width() - 10, height() - 12 );
627 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
632 std::unique_ptr< QgsSymbol > previewSymbol;
636 previewSymbol.reset( tempSymbol->
clone() );
640 previewSymbol.reset( mSymbol->clone() );
644 setIconSize( currentIconSize );
646 setToolTip( QString( ) );
650 if ( color.isValid() )
651 previewSymbol->setColor( color );
655 setIconSize( currentIconSize );
660 const int width =
static_cast< int >(
Qgis::UI_SCALE_FACTOR * fontMetrics().horizontalAdvance(
'X' ) * 23 );
661 const int height =
static_cast< int >( width / 1.61803398875 );
665 QBuffer buffer( &data );
666 pm.save( &buffer,
"PNG", 100 );
667 setToolTip( QStringLiteral(
"<img src='data:image/png;base64, %3'>" ).arg( QString( data.toBase64() ) ) );
670 bool QgsSymbolButton::colorFromMimeData(
const QMimeData *mimeData, QColor &resultColor,
bool &hasAlpha )
675 if ( mimeColor.isValid() )
677 resultColor = mimeColor;
685 QPixmap QgsSymbolButton::createColorIcon(
const QColor &color )
const
690 pixmap.fill( Qt::transparent );
696 p.setBrush( QBrush( color ) );
699 p.setPen( QColor( 197, 197, 197 ) );
705 void QgsSymbolButton::stopPicking( QPoint eventPos,
bool samplingColor )
710 QgsApplication::restoreOverrideCursor();
711 setMouseTracking(
false );
712 mPickingColor =
false;
714 if ( !samplingColor )
723 addRecentColor( newColor );
726 void QgsSymbolButton::showColorDialog()
734 const QColor currentColor = mSymbol->color();
739 if ( currentColor.isValid() )
746 if ( newColor.isValid() )
748 setColor( newColor );
755 QgsColorDialog dialog(
this, Qt::WindowFlags(), mSymbol->color() );
756 dialog.setTitle( tr(
"Symbol Color" ) );
757 dialog.setAllowOpacity(
true );
759 if ( dialog.exec() && dialog.color().isValid() )
770 mDialogTitle = title;
780 return mSymbol.get();