17#include "moc_qgscolorbutton.cpp"
36#include <QStyleOptionToolButton>
37#include <QWidgetAction>
45 : QToolButton( parent )
46 , mColorDialogTitle( cdt.isEmpty() ? tr(
"Select Color" ) : cdt )
47 , mNoColorString( tr(
"No color" ) )
52 setAcceptDrops(
true );
53 setMinimumSize( QSize( 24, 16 ) );
54 connect(
this, &QAbstractButton::clicked,
this, &QgsColorButton::buttonClicked );
57 mMenu =
new QMenu(
this );
58 connect( mMenu, &QMenu::aboutToShow,
this, &QgsColorButton::prepareMenu );
60 setPopupMode( QToolButton::MenuButtonPopup );
63 mMinimumSize = QSize( 120, 22 );
65 mMinimumSize = QSize( 120, 28 );
68 mMinimumSize.setHeight( std::max(
static_cast<int>(
Qgis::UI_SCALE_FACTOR * fontMetrics().height() * 1.1 ), mMinimumSize.height() ) );
88 static QPixmap sTranspBkgrd;
90 if ( sTranspBkgrd.isNull() )
96void QgsColorButton::showColorDialog()
101 const QColor currentColor =
color();
106 if ( currentColor.isValid() )
120 const bool useNative = settings.
value( QStringLiteral(
"qgis/native_color_dialogs" ),
false ).toBool();
124 newColor = QColorDialog::getColor(
color(),
this, mColorDialogTitle, mAllowOpacity ? QColorDialog::ShowAlphaChannel : ( QColorDialog::ColorDialogOption ) 0 );
129 dialog.setTitle( mColorDialogTitle );
130 dialog.setAllowOpacity( mAllowOpacity );
134 newColor = dialog.color();
138 if ( newColor.isValid() )
140 setValidColor( newColor );
149 if ( !mDefaultColor.isValid() )
171 if ( e->type() == QEvent::ToolTip && isEnabled() )
173 QColor
c = linkedProjectColor();
174 const bool isProjectColor =
c.isValid();
175 if ( !isProjectColor )
178 QString info = ( isProjectColor ? QStringLiteral(
"<p>%1: %2</p>" ).arg( tr(
"Linked color" ), mLinkedColorName ) : QString() );
184 return QToolButton::event( e );
189 QColor noColor = QColor( mColor );
190 noColor.setAlpha( 0 );
203 if ( e->button() == Qt::RightButton )
205 QToolButton::showMenu();
208 else if ( e->button() == Qt::LeftButton )
210 mDragStartPosition = e->pos();
212 QToolButton::mousePressEvent( e );
215bool QgsColorButton::colorFromMimeData(
const QMimeData *mimeData, QColor &resultColor )
217 bool hasAlpha =
false;
220 if ( mimeColor.isValid() )
222 if ( !mAllowOpacity )
225 mimeColor.setAlpha( 255 );
227 else if ( !hasAlpha )
230 mimeColor.setAlpha( mColor.alpha() );
232 resultColor = mimeColor;
250 QColor
c = linkedProjectColor();
254 if ( !( e->buttons() & Qt::LeftButton ) || !
c.isValid() )
257 QToolButton::mouseMoveEvent( e );
261 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
264 QToolButton::mouseMoveEvent( e );
269 QDrag *drag =
new QDrag(
this );
272 drag->exec( Qt::CopyAction );
281 stopPicking( e->globalPos() );
286 QToolButton::mouseReleaseEvent( e );
289void QgsColorButton::stopPicking( QPoint eventPos,
bool samplingColor )
294 QgsApplication::restoreOverrideCursor();
295 setMouseTracking(
false );
296 mPickingColor =
false;
298 if ( !samplingColor )
306 addRecentColor( mColor );
309QColor QgsColorButton::linkedProjectColor()
const
311 QList<QgsProjectColorScheme *> projectSchemes;
313 if ( projectSchemes.length() > 0 )
317 for (
const auto &
color : colors )
319 if (
color.second.isEmpty() )
322 if (
color.second == mLinkedColorName )
333 if ( !mPickingColor )
336 QToolButton::keyPressEvent( e );
341 stopPicking( QCursor::pos(), e->key() == Qt::Key_Space );
346 const bool isProjectColor = linkedProjectColor().isValid();
347 if ( isProjectColor )
352 if ( colorFromMimeData( e->mimeData(), mimeColor ) )
357 e->acceptProposedAction();
371 const bool isProjectColor = linkedProjectColor().isValid();
372 if ( isProjectColor )
377 if ( colorFromMimeData( e->mimeData(), mimeColor ) )
380 e->acceptProposedAction();
382 addRecentColor( mimeColor );
388 if ( mAllowOpacity && isEnabled() && !
isNull() )
390 const double increment = ( (
event->modifiers() & Qt::ControlModifier ) ? 0.01 : 0.1 ) * (
event->angleDelta().y() > 0 ? 1 : -1 );
391 const double alpha = std::min( std::max( 0.0, mColor.alphaF() + increment ), 1.0 );
392 mColor.setAlphaF( alpha );
400 QToolButton::wheelEvent(
event );
404void QgsColorButton::setValidColor(
const QColor &newColor )
406 if ( newColor.isValid() )
409 addRecentColor( newColor );
413void QgsColorButton::setValidTemporaryColor(
const QColor &newColor )
415 if ( newColor.isValid() )
426 QPixmap pixmap( iconSize, iconSize );
427 pixmap.fill( Qt::transparent );
436 p.setPen( Qt::NoPen );
437 p.setBrush( checkBrush );
438 p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
442 p.setBrush( QBrush(
color ) );
445 p.setPen( QColor( 197, 197, 197 ) );
446 p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
451void QgsColorButton::buttonClicked()
453 if ( linkedProjectColor().isValid() )
455 QToolButton::showMenu();
471void QgsColorButton::prepareMenu()
479 const bool isProjectColor = linkedProjectColor().isValid();
481 if ( !isProjectColor )
485 QAction *nullAction =
new QAction( mNullColorString.isEmpty() ? tr(
"Clear Color" ) : mNullColorString, this );
487 mMenu->addAction( nullAction );
492 if ( mDefaultColor.isValid() )
494 QAction *defaultColorAction =
new QAction( tr(
"Default Color" ),
this );
496 mMenu->addAction( defaultColorAction );
500 if ( mShowNoColorOption )
502 QAction *noColorAction =
new QAction( mNoColorString,
this );
503 noColorAction->setIcon(
createMenuIcon( Qt::transparent,
false ) );
504 mMenu->addAction( noColorAction );
508 mMenu->addSeparator();
514 mMenu->addAction( colorAction );
524 mMenu->addAction( alphaAction );
527 if ( mColorSchemeRegistry )
531 QList<QgsColorScheme *>::iterator it = schemeList.begin();
532 for ( ; it != schemeList.end(); ++it )
536 mMenu->addAction( colorAction );
542 mMenu->addSeparator();
545 if ( isProjectColor )
547 QAction *unlinkAction =
new QAction( tr(
"Unlink Color" ), mMenu );
548 mMenu->addAction( unlinkAction );
552 QAction *copyColorAction =
new QAction( tr(
"Copy Color" ),
this );
553 mMenu->addAction( copyColorAction );
556 if ( !isProjectColor )
558 QAction *pasteColorAction =
new QAction( tr(
"Paste Color" ),
this );
562 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor ) )
568 pasteColorAction->setEnabled(
false );
570 mMenu->addAction( pasteColorAction );
573 QAction *pickColorAction =
new QAction( tr(
"Pick Color" ),
this );
574 mMenu->addAction( pickColorAction );
577 QAction *chooseColorAction =
new QAction( tr(
"Choose Color…" ),
this );
578 mMenu->addAction( chooseColorAction );
579 connect( chooseColorAction, &QAction::triggered,
this, &QgsColorButton::showColorDialog );
585 if ( e->type() == QEvent::EnabledChange )
589 QToolButton::changeEvent( e );
593void QgsColorButton::paintEvent( QPaintEvent *e )
595 QToolButton::paintEvent( e );
597 if ( !mBackgroundSet )
607 QToolButton::showEvent( e );
612 QToolButton::resizeEvent(
event );
620 const QColor oldColor = mColor;
624 if ( oldColor != mColor || ( mColor == QColor( Qt::black ) && !mColorSet ) )
637void QgsColorButton::addRecentColor(
const QColor &color )
644 QColor backgroundColor =
color;
645 bool isProjectColor =
false;
646 if ( !backgroundColor.isValid() && !mLinkedColorName.isEmpty() )
648 backgroundColor = linkedProjectColor();
649 isProjectColor = backgroundColor.isValid();
650 if ( !isProjectColor )
652 mLinkedColorName.clear();
656 if ( !backgroundColor.isValid() )
658 backgroundColor = mColor;
661 QSize currentIconSize;
665 if ( !mIconSize.isValid() )
668 QStyleOptionToolButton opt;
669 initStyleOption( &opt );
670 const QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
this );
673 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
675 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
678 currentIconSize = mIconSize;
684 currentIconSize = QSize( width() - 10, height() - 6 );
686 currentIconSize = QSize( width() - 10, height() - 12 );
690 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
696 const double pixelRatio = devicePixelRatioF();
697 QPixmap pixmap( currentIconSize * pixelRatio );
698 pixmap.setDevicePixelRatio( pixelRatio );
699 pixmap.fill( Qt::transparent );
701 if ( backgroundColor.isValid() )
703 const QRectF rect( 0, 0, currentIconSize.width(), currentIconSize.height() );
706 p.setRenderHint( QPainter::Antialiasing );
707 p.setPen( Qt::NoPen );
708 if ( mAllowOpacity && backgroundColor.alpha() < 255 )
712 p.setBrush( checkBrush );
713 p.drawRoundedRect( rect, 3, 3 );
716 p.setBrush( backgroundColor );
717 p.drawRoundedRect( rect, 3, 3 );
720 const QRectF clipRect( 0, 0,
static_cast<qreal
>( currentIconSize.width() ) / 2.0, currentIconSize.height() );
721 p.setClipRect( clipRect );
722 backgroundColor.setAlpha( 255 );
723 p.setBrush( backgroundColor );
724 p.drawRoundedRect( rect, 3, 3 );
728 p.setBrush( backgroundColor );
729 p.drawRoundedRect( rect, 3, 3 );
734 setIconSize( currentIconSize );
741 QColor
c = linkedProjectColor();
750 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor ) )
754 addRecentColor( clipColor );
762 mCurrentColor = mColor;
766 mPickingColor =
true;
767 setMouseTracking(
true );
772 QColor
c = linkedProjectColor();
780 mAllowOpacity = allow;
785 mColorDialogTitle = title;
790 return mColorDialogTitle;
796 setMenu(
showMenu ? mMenu : nullptr );
797 setPopupMode(
showMenu ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
810 mDefaultColor =
color;
816 mNullColorString = nullString;
826 return !mColor.isValid();
831 mLinkedColorName = name;
static const double UI_SCALE_FACTOR
UI scaling factor.
static QPixmap getThemePixmap(const QString &name, const QColor &foreColor=QColor(), const QColor &backColor=QColor(), int size=16)
Helper to get a theme icon as a pixmap.
static QCursor getThemeCursor(Cursor cursor)
Helper to get a theme cursor.
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application's color scheme registry, used for managing color schemes.
@ Sampler
Color/Value picker.
A custom QGIS dialog for selecting a color.
Registry of color schemes.
QList< QgsColorScheme * > schemes() const
Returns all color schemes in the registry.
@ ShowInColorButtonMenu
Show scheme in color button drop-down menu.
A color swatch grid which can be embedded into a menu.
void setBaseColor(const QColor &baseColor)
Sets the base color for the color grid.
void colorChanged(const QColor &color)
Emitted when a color has been selected from the widget.
void setColor(const QColor &color, bool emitSignals=false) override
static QColor sampleColor(QPoint point)
Samples the color on screen at the specified global point (pixel).
A color scheme which contains project specific colors set through project properties dialog.
QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor()) override
Gets a list of colors from the scheme.
static QgsProject * instance()
Returns the QgsProject singleton instance.
void projectColorsChanged()
Emitted whenever the project's color scheme has been changed.
static void addRecentColor(const QColor &color)
Adds a color to the list of recent colors.
This class is a composition of two QSettings instances:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
static QColor colorFromMimeData(const QMimeData *data, bool &hasAlpha)
Attempts to parse mime data as a color.
static QMimeData * colorToMimeData(const QColor &color)
Creates mime data from a color.
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c