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 );
125 dialog.setContext( symbolContext );
140 void QgsSymbolButton::updateSymbolFromWidget()
146 void QgsSymbolButton::cleanUpSymbolSelector(
QgsPanelWidget *container )
187 mExpressionContextGenerator = generator;
199 QColor opaque = color;
200 opaque.setAlphaF( 1.0 );
202 if ( opaque == mSymbol->color() )
205 mSymbol->setColor( opaque );
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 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
557 int width =
static_cast< int >(
Qgis::UI_SCALE_FACTOR * fontMetrics().horizontalAdvance(
'X' ) * 23 );
559 int height =
static_cast< int >( width / 1.61803398875 );
563 QBuffer buffer( &data );
564 pm.save( &buffer,
"PNG", 100 );
565 setToolTip( QStringLiteral(
"<img src='data:image/png;base64, %3'>" ).arg( QString( data.toBase64() ) ) );
568 bool QgsSymbolButton::colorFromMimeData(
const QMimeData *mimeData, QColor &resultColor,
bool &hasAlpha )
573 if ( mimeColor.isValid() )
575 resultColor = mimeColor;
583 QPixmap QgsSymbolButton::createColorIcon(
const QColor &color )
const
588 pixmap.fill( Qt::transparent );
594 p.setBrush( QBrush( color ) );
597 p.setPen( QColor( 197, 197, 197 ) );
603 void QgsSymbolButton::stopPicking( QPoint eventPos,
bool samplingColor )
608 QgsApplication::restoreOverrideCursor();
609 setMouseTracking(
false );
610 mPickingColor =
false;
612 if ( !samplingColor )
621 addRecentColor( newColor );
624 void QgsSymbolButton::showColorDialog()
629 QColor currentColor = mSymbol->color();
634 if ( currentColor.isValid() )
641 if ( newColor.isValid() )
643 setColor( newColor );
650 QgsColorDialog dialog(
this, Qt::WindowFlags(), mSymbol->color() );
651 dialog.setTitle( tr(
"Symbol Color" ) );
652 dialog.setAllowOpacity(
true );
654 if ( dialog.exec() && dialog.color().isValid() )
665 mDialogTitle = title;
675 return mSymbol.get();