29 #include <QTemporaryFile> 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 )
347 setColor( sampleColor( eventPos ) );
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 QColor QgsColorButton::sampleColor( QPoint point )
const 430 QScreen *screen = findScreenAt( point );
435 QPixmap snappedPixmap = screen->grabWindow( QApplication::desktop()->winId(), point.x(), point.y(), 1, 1 );
436 QImage snappedImage = snappedPixmap.toImage();
437 return snappedImage.pixel( 0, 0 );
440 QScreen *QgsColorButton::findScreenAt( QPoint pos )
442 for ( QScreen *screen : QGuiApplication::screens() )
444 if ( screen->geometry().contains( pos ) )
452 void QgsColorButton::setValidColor(
const QColor &newColor )
454 if ( newColor.isValid() )
457 addRecentColor( newColor );
461 void QgsColorButton::setValidTemporaryColor(
const QColor &newColor )
463 if ( newColor.isValid() )
474 QPixmap pixmap( iconSize, iconSize );
475 pixmap.fill( Qt::transparent );
484 p.setPen( Qt::NoPen );
485 p.setBrush( checkBrush );
486 p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
490 p.setBrush( QBrush( color ) );
493 p.setPen( QColor( 197, 197, 197 ) );
494 p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
499 void QgsColorButton::buttonClicked()
501 if ( linkedProjectColor().isValid() )
503 QToolButton::showMenu();
519 void QgsColorButton::prepareMenu()
527 const bool isProjectColor = linkedProjectColor().isValid();
529 if ( !isProjectColor )
533 QAction *nullAction =
new QAction( tr(
"Clear Color" ),
this );
535 mMenu->addAction( nullAction );
540 if ( mDefaultColor.isValid() )
542 QAction *defaultColorAction =
new QAction( tr(
"Default Color" ),
this );
544 mMenu->addAction( defaultColorAction );
548 if ( mShowNoColorOption && mAllowOpacity )
550 QAction *noColorAction =
new QAction( mNoColorString,
this );
551 noColorAction->setIcon(
createMenuIcon( Qt::transparent,
false ) );
552 mMenu->addAction( noColorAction );
556 mMenu->addSeparator();
562 mMenu->addAction( colorAction );
574 mMenu->addAction( alphaAction );
577 if ( mColorSchemeRegistry )
581 QList< QgsColorScheme * >::iterator it = schemeList.begin();
582 for ( ; it != schemeList.end(); ++it )
586 mMenu->addAction( colorAction );
592 mMenu->addSeparator();
595 if ( isProjectColor )
597 QAction *unlinkAction =
new QAction( tr(
"Unlink Color" ), mMenu );
598 mMenu->addAction( unlinkAction );
602 QAction *copyColorAction =
new QAction( tr(
"Copy Color" ),
this );
603 mMenu->addAction( copyColorAction );
606 if ( !isProjectColor )
608 QAction *pasteColorAction =
new QAction( tr(
"Paste Color" ),
this );
612 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor ) )
618 pasteColorAction->setEnabled(
false );
620 mMenu->addAction( pasteColorAction );
623 QAction *pickColorAction =
new QAction( tr(
"Pick Color" ),
this );
624 mMenu->addAction( pickColorAction );
627 QAction *chooseColorAction =
new QAction( tr(
"Choose Color…" ),
this );
628 mMenu->addAction( chooseColorAction );
629 connect( chooseColorAction, &QAction::triggered,
this, &QgsColorButton::showColorDialog );
635 if ( e->type() == QEvent::EnabledChange )
639 QToolButton::changeEvent( e );
642 #if 0 // causes too many cyclical updates, but may be needed on some platforms 643 void QgsColorButton::paintEvent( QPaintEvent *e )
645 QToolButton::paintEvent( e );
647 if ( !mBackgroundSet )
657 QToolButton::showEvent( e );
662 QToolButton::resizeEvent( event );
670 QColor oldColor = mColor;
674 if ( oldColor != mColor || ( mColor == QColor( Qt::black ) && !mColorSet ) )
687 void QgsColorButton::addRecentColor(
const QColor &
color )
694 QColor backgroundColor =
color;
695 bool isProjectColor =
false;
696 if ( !backgroundColor.isValid() && !mLinkedColorName.isEmpty() )
698 backgroundColor = linkedProjectColor();
699 isProjectColor = backgroundColor.isValid();
700 if ( !isProjectColor )
702 mLinkedColorName.clear();
706 if ( !backgroundColor.isValid() )
708 backgroundColor = mColor;
711 QSize currentIconSize;
715 if ( !mIconSize.isValid() )
718 QStyleOptionToolButton opt;
719 initStyleOption( &opt );
720 QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
724 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
726 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
729 currentIconSize = mIconSize;
735 currentIconSize = QSize( width() - 10, height() - 6 );
737 currentIconSize = QSize( width() - 10, height() - 12 );
741 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
747 QPixmap pixmap( currentIconSize );
748 pixmap.fill( Qt::transparent );
750 if ( backgroundColor.isValid() )
752 QRect rect( 0, 0, currentIconSize.width(), currentIconSize.height() );
755 p.setRenderHint( QPainter::Antialiasing );
756 p.setPen( Qt::NoPen );
757 if ( mAllowOpacity && backgroundColor.alpha() < 255 )
761 p.setBrush( checkBrush );
762 p.drawRoundedRect( rect, 3, 3 );
766 p.setBrush( backgroundColor );
767 p.drawRoundedRect( rect, 3, 3 );
771 setIconSize( currentIconSize );
778 QColor
c = linkedProjectColor();
787 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor ) )
791 addRecentColor( clipColor );
799 mCurrentColor = mColor;
803 mPickingColor =
true;
804 setMouseTracking(
true );
809 QColor
c = linkedProjectColor();
817 mAllowOpacity = allow;
822 mColorDialogTitle = title;
827 return mColorDialogTitle;
833 setMenu( showMenu ? mMenu :
nullptr );
834 setPopupMode( showMenu ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
847 mDefaultColor =
color;
862 return !mColor.isValid();
867 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
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 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.