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() ) );
89 static QPixmap sTranspBkgrd;
91 if ( sTranspBkgrd.isNull() )
97void QgsColorButton::showColorDialog()
102 const QColor currentColor =
color();
107 if ( currentColor.isValid() )
121 const bool useNative = settings.
value( QStringLiteral(
"qgis/native_color_dialogs" ),
false ).toBool();
125 newColor = QColorDialog::getColor(
color(),
this, mColorDialogTitle, mAllowOpacity ? QColorDialog::ShowAlphaChannel : ( QColorDialog::ColorDialogOption )0 );
130 dialog.setTitle( mColorDialogTitle );
131 dialog.setAllowOpacity( mAllowOpacity );
135 newColor = dialog.color();
139 if ( newColor.isValid() )
141 setValidColor( newColor );
150 if ( !mDefaultColor.isValid() )
172 if ( e->type() == QEvent::ToolTip && isEnabled() )
174 QColor
c = linkedProjectColor();
175 const bool isProjectColor =
c.isValid();
176 if ( !isProjectColor )
179 QString info = ( isProjectColor ? QStringLiteral(
"<p>%1: %2</p>" ).arg( tr(
"Linked color" ), mLinkedColorName ) : QString() );
185 return QToolButton::event( e );
190 QColor noColor = QColor( mColor );
191 noColor.setAlpha( 0 );
204 if ( e->button() == Qt::RightButton )
206 QToolButton::showMenu();
209 else if ( e->button() == Qt::LeftButton )
211 mDragStartPosition = e->pos();
213 QToolButton::mousePressEvent( e );
216bool QgsColorButton::colorFromMimeData(
const QMimeData *mimeData, QColor &resultColor )
218 bool hasAlpha =
false;
221 if ( mimeColor.isValid() )
223 if ( !mAllowOpacity )
226 mimeColor.setAlpha( 255 );
228 else if ( !hasAlpha )
231 mimeColor.setAlpha( mColor.alpha() );
233 resultColor = mimeColor;
251 QColor
c = linkedProjectColor();
255 if ( !( e->buttons() & Qt::LeftButton ) || !
c.isValid() )
258 QToolButton::mouseMoveEvent( e );
262 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
265 QToolButton::mouseMoveEvent( e );
270 QDrag *drag =
new QDrag(
this );
273 drag->exec( Qt::CopyAction );
282 stopPicking( e->globalPos() );
287 QToolButton::mouseReleaseEvent( e );
290void QgsColorButton::stopPicking( QPoint eventPos,
bool samplingColor )
295 QgsApplication::restoreOverrideCursor();
296 setMouseTracking(
false );
297 mPickingColor =
false;
299 if ( !samplingColor )
307 addRecentColor( mColor );
310QColor QgsColorButton::linkedProjectColor()
const
312 QList<QgsProjectColorScheme *> projectSchemes;
314 if ( projectSchemes.length() > 0 )
318 for (
const auto &
color : colors )
320 if (
color.second.isEmpty() )
323 if (
color.second == mLinkedColorName )
334 if ( !mPickingColor )
337 QToolButton::keyPressEvent( e );
342 stopPicking( QCursor::pos(), e->key() == Qt::Key_Space );
347 const bool isProjectColor = linkedProjectColor().isValid();
348 if ( isProjectColor )
353 if ( colorFromMimeData( e->mimeData(), mimeColor ) )
358 e->acceptProposedAction();
372 const bool isProjectColor = linkedProjectColor().isValid();
373 if ( isProjectColor )
378 if ( colorFromMimeData( e->mimeData(), mimeColor ) )
381 e->acceptProposedAction();
383 addRecentColor( mimeColor );
389 if ( mAllowOpacity && isEnabled() && !
isNull() )
391 const double increment = ( (
event->modifiers() & Qt::ControlModifier ) ? 0.01 : 0.1 ) *
392 (
event->angleDelta().y() > 0 ? 1 : -1 );
393 const double alpha = std::min( std::max( 0.0, mColor.alphaF() + increment ), 1.0 );
394 mColor.setAlphaF( alpha );
402 QToolButton::wheelEvent(
event );
406void QgsColorButton::setValidColor(
const QColor &newColor )
408 if ( newColor.isValid() )
411 addRecentColor( newColor );
415void QgsColorButton::setValidTemporaryColor(
const QColor &newColor )
417 if ( newColor.isValid() )
428 QPixmap pixmap( iconSize, iconSize );
429 pixmap.fill( Qt::transparent );
438 p.setPen( Qt::NoPen );
439 p.setBrush( checkBrush );
440 p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
444 p.setBrush( QBrush(
color ) );
447 p.setPen( QColor( 197, 197, 197 ) );
448 p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
453void QgsColorButton::buttonClicked()
455 if ( linkedProjectColor().isValid() )
457 QToolButton::showMenu();
473void QgsColorButton::prepareMenu()
481 const bool isProjectColor = linkedProjectColor().isValid();
483 if ( !isProjectColor )
487 QAction *nullAction =
new QAction( mNullColorString.isEmpty() ? tr(
"Clear Color" ) : mNullColorString, this );
489 mMenu->addAction( nullAction );
494 if ( mDefaultColor.isValid() )
496 QAction *defaultColorAction =
new QAction( tr(
"Default Color" ),
this );
498 mMenu->addAction( defaultColorAction );
502 if ( mShowNoColorOption )
504 QAction *noColorAction =
new QAction( mNoColorString,
this );
505 noColorAction->setIcon(
createMenuIcon( Qt::transparent,
false ) );
506 mMenu->addAction( noColorAction );
510 mMenu->addSeparator();
516 mMenu->addAction( colorAction );
528 mMenu->addAction( alphaAction );
531 if ( mColorSchemeRegistry )
535 QList< QgsColorScheme * >::iterator it = schemeList.begin();
536 for ( ; it != schemeList.end(); ++it )
540 mMenu->addAction( colorAction );
546 mMenu->addSeparator();
549 if ( isProjectColor )
551 QAction *unlinkAction =
new QAction( tr(
"Unlink Color" ), mMenu );
552 mMenu->addAction( unlinkAction );
556 QAction *copyColorAction =
new QAction( tr(
"Copy Color" ),
this );
557 mMenu->addAction( copyColorAction );
560 if ( !isProjectColor )
562 QAction *pasteColorAction =
new QAction( tr(
"Paste Color" ),
this );
566 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor ) )
572 pasteColorAction->setEnabled(
false );
574 mMenu->addAction( pasteColorAction );
577 QAction *pickColorAction =
new QAction( tr(
"Pick Color" ),
this );
578 mMenu->addAction( pickColorAction );
581 QAction *chooseColorAction =
new QAction( tr(
"Choose Color…" ),
this );
582 mMenu->addAction( chooseColorAction );
583 connect( chooseColorAction, &QAction::triggered,
this, &QgsColorButton::showColorDialog );
589 if ( e->type() == QEvent::EnabledChange )
593 QToolButton::changeEvent( e );
597void QgsColorButton::paintEvent( QPaintEvent *e )
599 QToolButton::paintEvent( e );
601 if ( !mBackgroundSet )
611 QToolButton::showEvent( e );
616 QToolButton::resizeEvent(
event );
624 const QColor oldColor = mColor;
628 if ( oldColor != mColor || ( mColor == QColor( Qt::black ) && !mColorSet ) )
641void QgsColorButton::addRecentColor(
const QColor &color )
648 QColor backgroundColor =
color;
649 bool isProjectColor =
false;
650 if ( !backgroundColor.isValid() && !mLinkedColorName.isEmpty() )
652 backgroundColor = linkedProjectColor();
653 isProjectColor = backgroundColor.isValid();
654 if ( !isProjectColor )
656 mLinkedColorName.clear();
660 if ( !backgroundColor.isValid() )
662 backgroundColor = mColor;
665 QSize currentIconSize;
669 if ( !mIconSize.isValid() )
672 QStyleOptionToolButton opt;
673 initStyleOption( &opt );
674 const QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
678 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
680 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
683 currentIconSize = mIconSize;
689 currentIconSize = QSize( width() - 10, height() - 6 );
691 currentIconSize = QSize( width() - 10, height() - 12 );
695 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
701 const double pixelRatio = devicePixelRatioF();
702 QPixmap pixmap( currentIconSize * pixelRatio );
703 pixmap.setDevicePixelRatio( pixelRatio );
704 pixmap.fill( Qt::transparent );
706 if ( backgroundColor.isValid() )
708 const QRectF rect( 0, 0,
709 currentIconSize.width(),
710 currentIconSize.height() );
713 p.setRenderHint( QPainter::Antialiasing );
714 p.setPen( Qt::NoPen );
715 if ( mAllowOpacity && backgroundColor.alpha() < 255 )
719 p.setBrush( checkBrush );
720 p.drawRoundedRect( rect, 3, 3 );
724 p.setBrush( backgroundColor );
725 p.drawRoundedRect( rect, 3, 3 );
729 setIconSize( currentIconSize );
736 QColor
c = linkedProjectColor();
745 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor ) )
749 addRecentColor( clipColor );
757 mCurrentColor = mColor;
761 mPickingColor =
true;
762 setMouseTracking(
true );
767 QColor
c = linkedProjectColor();
775 mAllowOpacity = allow;
780 mColorDialogTitle = title;
785 return mColorDialogTitle;
791 setMenu(
showMenu ? mMenu : nullptr );
792 setPopupMode(
showMenu ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
805 mDefaultColor =
color;
811 mNullColorString = nullString;
821 return !mColor.isValid();
826 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