30 #include <QMouseEvent> 34 #include <QDesktopWidget> 36 #include <QStyleOptionToolButton> 37 #include <QWidgetAction> 40 #include <QGridLayout> 41 #include <QPushButton> 44 : QToolButton( parent )
45 , mColorDialogTitle( cdt.isEmpty() ? tr(
"Select Color" ) : cdt )
46 , mNoColorString( tr(
"No color" ) )
51 setAcceptDrops(
true );
52 setMinimumSize( QSize( 24, 16 ) );
53 connect(
this, &QAbstractButton::clicked,
this, &QgsColorButton::buttonClicked );
56 mMenu =
new QMenu(
this );
57 connect( mMenu, &QMenu::aboutToShow,
this, &QgsColorButton::prepareMenu );
59 setPopupMode( QToolButton::MenuButtonPopup );
62 mMinimumSize = QSize( 120, 22 );
64 mMinimumSize = QSize( 120, 28 );
67 mMinimumSize.setHeight( std::max( static_cast<int>(
Qgis::UI_SCALE_FACTOR * fontMetrics().height() * 1.1 ), mMinimumSize.height() ) );
91 static QPixmap sTranspBkgrd;
93 if ( sTranspBkgrd.isNull() )
99 void QgsColorButton::showColorDialog()
104 QColor currentColor =
color();
109 if ( currentColor.isValid() )
123 bool useNative = settings.
value( QStringLiteral(
"qgis/native_color_dialogs" ),
false ).toBool();
127 newColor = QColorDialog::getColor(
color(),
this, mColorDialogTitle, mAllowOpacity ? QColorDialog::ShowAlphaChannel : ( QColorDialog::ColorDialogOption )0 );
132 dialog.
setTitle( mColorDialogTitle );
137 newColor = dialog.
color();
141 if ( newColor.isValid() )
143 setValidColor( newColor );
152 if ( !mDefaultColor.isValid() )
173 if ( e->type() == QEvent::ToolTip )
175 QColor
c = linkedProjectColor();
176 bool isProjectColor = c.isValid();
177 if ( !isProjectColor )
180 QString name = c.name();
182 int value = c.value();
183 int saturation = c.saturation();
187 int height =
static_cast< int >( width / 1.61803398875 );
189 int margin =
static_cast< int >( height * 0.1 );
190 QImage icon = QImage( width + 2 * margin, height + 2 * margin, QImage::Format_ARGB32 );
191 icon.fill( Qt::transparent );
198 p.setPen( Qt::NoPen );
199 p.setBrush( checkBrush );
200 p.drawRect( margin, margin, width, height );
203 p.setBrush( QBrush( c ) );
206 p.setPen( QColor( 197, 197, 197 ) );
207 p.drawRect( margin, margin, width, height );
211 QBuffer buffer( &data );
212 icon.save( &buffer,
"PNG", 100 );
214 QString info = ( isProjectColor ? QStringLiteral(
"<p>%1: %2</p>" ).arg( tr(
"Linked color" ), mLinkedColorName ) : QString() )
215 + QStringLiteral(
"<b>HEX</b> %1<br>" 217 "<b>HSV</b> %3,%4,%5<p>" 218 "<img src='data:image/png;base64, %0'>" ).arg( QString( data.toBase64() ), name,
220 .arg( hue ).arg( saturation ).arg( value );
223 return QToolButton::event( e );
230 QColor noColor = QColor( mColor );
231 noColor.setAlpha( 0 );
245 if ( e->button() == Qt::RightButton )
247 QToolButton::showMenu();
250 else if ( e->button() == Qt::LeftButton )
252 mDragStartPosition = e->pos();
254 QToolButton::mousePressEvent( e );
257 bool QgsColorButton::colorFromMimeData(
const QMimeData *mimeData, QColor &resultColor )
259 bool hasAlpha =
false;
262 if ( mimeColor.isValid() )
264 if ( !mAllowOpacity )
267 mimeColor.setAlpha( 255 );
269 else if ( !hasAlpha )
272 mimeColor.setAlpha( mColor.alpha() );
274 resultColor = mimeColor;
292 QColor
c = linkedProjectColor();
296 if ( !( e->buttons() & Qt::LeftButton ) || !c.isValid() )
299 QToolButton::mouseMoveEvent( e );
303 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
306 QToolButton::mouseMoveEvent( e );
311 QDrag *drag =
new QDrag(
this );
314 drag->exec( Qt::CopyAction );
323 stopPicking( e->globalPos() );
328 QToolButton::mouseReleaseEvent( e );
331 void QgsColorButton::stopPicking( QPoint eventPos,
bool samplingColor )
336 QgsApplication::restoreOverrideCursor();
337 setMouseTracking(
false );
338 mPickingColor =
false;
340 if ( !samplingColor )
348 addRecentColor( mColor );
351 QColor QgsColorButton::linkedProjectColor()
const 353 QList<QgsProjectColorScheme *> projectSchemes;
355 if ( projectSchemes.length() > 0 )
359 for (
const auto &
color : colors )
361 if (
color.second.isEmpty() )
364 if (
color.second == mLinkedColorName )
375 if ( !mPickingColor )
378 QToolButton::keyPressEvent( e );
383 stopPicking( QCursor::pos(), e->key() == Qt::Key_Space );
388 const bool isProjectColor = linkedProjectColor().isValid();
389 if ( isProjectColor )
394 if ( colorFromMimeData( e->mimeData(), mimeColor ) )
399 e->acceptProposedAction();
413 const bool isProjectColor = linkedProjectColor().isValid();
414 if ( isProjectColor )
419 if ( colorFromMimeData( e->mimeData(), mimeColor ) )
422 e->acceptProposedAction();
424 addRecentColor( mimeColor );
428 void QgsColorButton::setValidColor(
const QColor &newColor )
430 if ( newColor.isValid() )
433 addRecentColor( newColor );
437 void QgsColorButton::setValidTemporaryColor(
const QColor &newColor )
439 if ( newColor.isValid() )
450 QPixmap pixmap( iconSize, iconSize );
451 pixmap.fill( Qt::transparent );
460 p.setPen( Qt::NoPen );
461 p.setBrush( checkBrush );
462 p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
466 p.setBrush( QBrush( color ) );
469 p.setPen( QColor( 197, 197, 197 ) );
470 p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
475 void QgsColorButton::buttonClicked()
477 if ( linkedProjectColor().isValid() )
479 QToolButton::showMenu();
495 void QgsColorButton::prepareMenu()
503 const bool isProjectColor = linkedProjectColor().isValid();
505 if ( !isProjectColor )
509 QAction *nullAction =
new QAction( tr(
"Clear Color" ),
this );
511 mMenu->addAction( nullAction );
516 if ( mDefaultColor.isValid() )
518 QAction *defaultColorAction =
new QAction( tr(
"Default Color" ),
this );
520 mMenu->addAction( defaultColorAction );
524 if ( mShowNoColorOption && mAllowOpacity )
526 QAction *noColorAction =
new QAction( mNoColorString,
this );
527 noColorAction->setIcon(
createMenuIcon( Qt::transparent,
false ) );
528 mMenu->addAction( noColorAction );
532 mMenu->addSeparator();
538 mMenu->addAction( colorAction );
550 mMenu->addAction( alphaAction );
553 if ( mColorSchemeRegistry )
557 QList< QgsColorScheme * >::iterator it = schemeList.begin();
558 for ( ; it != schemeList.end(); ++it )
562 mMenu->addAction( colorAction );
568 mMenu->addSeparator();
571 if ( isProjectColor )
573 QAction *unlinkAction =
new QAction( tr(
"Unlink Color" ), mMenu );
574 mMenu->addAction( unlinkAction );
578 QAction *copyColorAction =
new QAction( tr(
"Copy Color" ),
this );
579 mMenu->addAction( copyColorAction );
582 if ( !isProjectColor )
584 QAction *pasteColorAction =
new QAction( tr(
"Paste Color" ),
this );
588 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor ) )
594 pasteColorAction->setEnabled(
false );
596 mMenu->addAction( pasteColorAction );
599 QAction *pickColorAction =
new QAction( tr(
"Pick Color" ),
this );
600 mMenu->addAction( pickColorAction );
603 QAction *chooseColorAction =
new QAction( tr(
"Choose Color…" ),
this );
604 mMenu->addAction( chooseColorAction );
605 connect( chooseColorAction, &QAction::triggered,
this, &QgsColorButton::showColorDialog );
611 if ( e->type() == QEvent::EnabledChange )
615 QToolButton::changeEvent( e );
618 #if 0 // causes too many cyclical updates, but may be needed on some platforms 619 void QgsColorButton::paintEvent( QPaintEvent *e )
621 QToolButton::paintEvent( e );
623 if ( !mBackgroundSet )
633 QToolButton::showEvent( e );
638 QToolButton::resizeEvent( event );
646 QColor oldColor = mColor;
650 if ( oldColor != mColor || ( mColor == QColor( Qt::black ) && !mColorSet ) )
663 void QgsColorButton::addRecentColor(
const QColor &
color )
670 QColor backgroundColor =
color;
671 bool isProjectColor =
false;
672 if ( !backgroundColor.isValid() && !mLinkedColorName.isEmpty() )
674 backgroundColor = linkedProjectColor();
675 isProjectColor = backgroundColor.isValid();
676 if ( !isProjectColor )
678 mLinkedColorName.clear();
682 if ( !backgroundColor.isValid() )
684 backgroundColor = mColor;
687 QSize currentIconSize;
691 if ( !mIconSize.isValid() )
694 QStyleOptionToolButton opt;
695 initStyleOption( &opt );
696 QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
700 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
702 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
705 currentIconSize = mIconSize;
711 currentIconSize = QSize( width() - 10, height() - 6 );
713 currentIconSize = QSize( width() - 10, height() - 12 );
717 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
723 QPixmap pixmap( currentIconSize );
724 pixmap.fill( Qt::transparent );
726 if ( backgroundColor.isValid() )
728 QRect rect( 0, 0, currentIconSize.width(), currentIconSize.height() );
731 p.setRenderHint( QPainter::Antialiasing );
732 p.setPen( Qt::NoPen );
733 if ( mAllowOpacity && backgroundColor.alpha() < 255 )
737 p.setBrush( checkBrush );
738 p.drawRoundedRect( rect, 3, 3 );
742 p.setBrush( backgroundColor );
743 p.drawRoundedRect( rect, 3, 3 );
747 setIconSize( currentIconSize );
754 QColor
c = linkedProjectColor();
763 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor ) )
767 addRecentColor( clipColor );
775 mCurrentColor = mColor;
779 mPickingColor =
true;
780 setMouseTracking(
true );
785 QColor
c = linkedProjectColor();
793 mAllowOpacity = allow;
798 mColorDialogTitle = title;
803 return mColorDialogTitle;
809 setMenu( showMenu ? mMenu :
nullptr );
810 setPopupMode( showMenu ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
823 mDefaultColor =
color;
838 return !mColor.isValid();
843 mLinkedColorName = name;
A color swatch grid which can be embedded into a menu.
A color scheme which contains project specific colors set through project properties dialog...
Registry of color schemes.
static const double UI_SCALE_FACTOR
UI scaling factor.
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.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly...
void colorChanged(const QColor &color)
Emitted when a color has been selected from the widget.
void projectColorsChanged()
Emitted whenever the project's color scheme has been changed.
Show scheme in color button drop-down menu.
void setAllowOpacity(bool allowOpacity)
Sets whether opacity modification (transparency) is permitted for the color dialog.
static QPixmap getThemePixmap(const QString &name)
Helper to get a theme icon as a pixmap.
static QString encodeColor(const QColor &color)
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
QList< QgsColorScheme * > schemes() const
Returns all color schemes in the registry.
void setTitle(const QString &title)
Sets the title for the color dialog.
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
void setColor(const QColor &color, bool emitSignals=false) override
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.
QColor color() const
Returns the current color for the dialog.
A custom QGIS dialog for selecting a color.
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application's color scheme registry, used for managing color schemes. ...
static QMimeData * colorToMimeData(const QColor &color)
Creates mime data from a color.
static QgsProject * instance()
Returns the QgsProject singleton instance.
static QCursor getThemeCursor(Cursor cursor)
Helper to get a theme cursor.
QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor()) override
Gets a list of colors from the scheme.
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.