QGIS API Documentation  3.4.15-Madeira (e83d02e274)
qgscolorbutton.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscolorbutton.cpp - Button which displays a color
3  --------------------------------------
4  Date : 12-Dec-2006
5  Copyright : (C) 2006 by Tom Elwertowski
6  Email : telwertowski at users dot sourceforge dot net
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgscolorbutton.h"
17 #include "qgscolordialog.h"
18 #include "qgsapplication.h"
19 #include "qgslogger.h"
20 #include "qgssymbollayerutils.h"
21 #include "qgscolorswatchgrid.h"
22 #include "qgscolorschemeregistry.h"
23 #include "qgscolorwidgets.h"
24 #include "qgssettings.h"
25 
26 #include <QPainter>
27 #include <QMouseEvent>
28 #include <QMenu>
29 #include <QClipboard>
30 #include <QDrag>
31 #include <QDesktopWidget>
32 #include <QStyle>
33 #include <QStyleOptionToolButton>
34 #include <QWidgetAction>
35 #include <QScreen>
36 #include <QLabel>
37 #include <QGridLayout>
38 #include <QPushButton>
39 
40 QgsColorButton::QgsColorButton( QWidget *parent, const QString &cdt, QgsColorSchemeRegistry *registry )
41  : QToolButton( parent )
42  , mColorDialogTitle( cdt.isEmpty() ? tr( "Select Color" ) : cdt )
43  , mNoColorString( tr( "No color" ) )
44 {
45  //if a color scheme registry was specified, use it, otherwise use the global instance
46  mColorSchemeRegistry = registry ? registry : QgsApplication::colorSchemeRegistry();
47 
48  setAcceptDrops( true );
49  setMinimumSize( QSize( 24, 16 ) );
50  connect( this, &QAbstractButton::clicked, this, &QgsColorButton::buttonClicked );
51 
52  //setup drop-down menu
53  mMenu = new QMenu( this );
54  connect( mMenu, &QMenu::aboutToShow, this, &QgsColorButton::prepareMenu );
55  setMenu( mMenu );
56  setPopupMode( QToolButton::MenuButtonPopup );
57 
58 #ifdef Q_OS_WIN
59  mMinimumSize = QSize( 120, 22 );
60 #else
61  mMinimumSize = QSize( 120, 28 );
62 #endif
63 
64  mMinimumSize.setHeight( std::max( static_cast<int>( Qgis::UI_SCALE_FACTOR * fontMetrics().height() * 1.1 ), mMinimumSize.height() ) );
65 }
66 
67 
68 
70 {
71  return mMinimumSize;
72 }
73 
75 {
76  return mMinimumSize;
77 }
78 
80 {
81  static QPixmap sTranspBkgrd;
82 
83  if ( sTranspBkgrd.isNull() )
84  sTranspBkgrd = QgsApplication::getThemePixmap( QStringLiteral( "/transp-background_8x8.png" ) );
85 
86  return sTranspBkgrd;
87 }
88 
89 void QgsColorButton::showColorDialog()
90 {
92  if ( panel && panel->dockMode() )
93  {
94  QColor currentColor = color();
96  colorWidget->setPanelTitle( mColorDialogTitle );
97  colorWidget->setAllowOpacity( mAllowOpacity );
98 
99  if ( currentColor.isValid() )
100  {
101  colorWidget->setPreviousColor( currentColor );
102  }
103 
104  connect( colorWidget, &QgsCompoundColorWidget::currentColorChanged, this, &QgsColorButton::setValidTemporaryColor );
105  panel->openPanel( colorWidget );
106  return;
107  }
108 
109  QColor newColor;
110  QgsSettings settings;
111 
112  // first check if we need to use the limited native dialogs
113  bool useNative = settings.value( QStringLiteral( "qgis/native_color_dialogs" ), false ).toBool();
114  if ( useNative )
115  {
116  // why would anyone want this? who knows.... maybe the limited nature of native dialogs helps ease the transition for MapInfo users?
117  newColor = QColorDialog::getColor( color(), this, mColorDialogTitle, mAllowOpacity ? QColorDialog::ShowAlphaChannel : ( QColorDialog::ColorDialogOption )0 );
118  }
119  else
120  {
121  QgsColorDialog dialog( this, nullptr, color() );
122  dialog.setTitle( mColorDialogTitle );
123  dialog.setAllowOpacity( mAllowOpacity );
124 
125  if ( dialog.exec() )
126  {
127  newColor = dialog.color();
128  }
129  }
130 
131  if ( newColor.isValid() )
132  {
133  setValidColor( newColor );
134  }
135 
136  // reactivate button's window
137  activateWindow();
138 }
139 
141 {
142  if ( !mDefaultColor.isValid() )
143  {
144  return;
145  }
146 
147  setColor( mDefaultColor );
148 }
149 
151 {
152  setColor( QColor() );
153 }
154 
155 bool QgsColorButton::event( QEvent *e )
156 {
157  if ( e->type() == QEvent::ToolTip )
158  {
159  QString name = mColor.name();
160  int hue = mColor.hue();
161  int value = mColor.value();
162  int saturation = mColor.saturation();
163 
164  // create very large preview swatch
165  int width = static_cast< int >( Qgis::UI_SCALE_FACTOR * fontMetrics().width( 'X' ) * 23 );
166  int height = static_cast< int >( width / 1.61803398875 ); // golden ratio
167 
168  int margin = static_cast< int >( height * 0.1 );
169  QImage icon = QImage( width + 2 * margin, height + 2 * margin, QImage::Format_ARGB32 );
170  icon.fill( Qt::transparent );
171 
172  QPainter p;
173  p.begin( &icon );
174 
175  //start with checkboard pattern
176  QBrush checkBrush = QBrush( transparentBackground() );
177  p.setPen( Qt::NoPen );
178  p.setBrush( checkBrush );
179  p.drawRect( margin, margin, width, height );
180 
181  //draw color over pattern
182  p.setBrush( QBrush( mColor ) );
183 
184  //draw border
185  p.setPen( QColor( 197, 197, 197 ) );
186  p.drawRect( margin, margin, width, height );
187  p.end();
188 
189  QByteArray data;
190  QBuffer buffer( &data );
191  icon.save( &buffer, "PNG", 100 );
192 
193  QString info = QStringLiteral( "<b>HEX</b> %1<br>"
194  "<b>RGB</b> %2<br>"
195  "<b>HSV</b> %3,%4,%5<p>"
196  "<img src='data:image/png;base64, %0'>" ).arg( QString( data.toBase64() ), name,
198  .arg( hue ).arg( saturation ).arg( value );
199  setToolTip( info );
200  }
201  return QToolButton::event( e );
202 }
203 
205 {
206  if ( mAllowOpacity )
207  {
208  QColor noColor = QColor( mColor );
209  noColor.setAlpha( 0 );
210  setColor( noColor );
211  }
212 }
213 
214 void QgsColorButton::mousePressEvent( QMouseEvent *e )
215 {
216  if ( mPickingColor )
217  {
218  //don't show dialog if in color picker mode
219  e->accept();
220  return;
221  }
222 
223  if ( e->button() == Qt::RightButton )
224  {
225  QToolButton::showMenu();
226  return;
227  }
228  else if ( e->button() == Qt::LeftButton )
229  {
230  mDragStartPosition = e->pos();
231  }
232  QToolButton::mousePressEvent( e );
233 }
234 
235 bool QgsColorButton::colorFromMimeData( const QMimeData *mimeData, QColor &resultColor )
236 {
237  bool hasAlpha = false;
238  QColor mimeColor = QgsSymbolLayerUtils::colorFromMimeData( mimeData, hasAlpha );
239 
240  if ( mimeColor.isValid() )
241  {
242  if ( !mAllowOpacity )
243  {
244  //remove alpha channel
245  mimeColor.setAlpha( 255 );
246  }
247  else if ( !hasAlpha )
248  {
249  //mime color has no explicit alpha component, so keep existing alpha
250  mimeColor.setAlpha( mColor.alpha() );
251  }
252  resultColor = mimeColor;
253  return true;
254  }
255 
256  //could not get color from mime data
257  return false;
258 }
259 
260 void QgsColorButton::mouseMoveEvent( QMouseEvent *e )
261 {
262  if ( mPickingColor )
263  {
264  setButtonBackground( sampleColor( e->globalPos() ) );
265  e->accept();
266  return;
267  }
268 
269  //handle dragging colors from button
270 
271  if ( !( e->buttons() & Qt::LeftButton ) || !mColor.isValid() )
272  {
273  //left button not depressed or no color set, so not a drag
274  QToolButton::mouseMoveEvent( e );
275  return;
276  }
277 
278  if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
279  {
280  //mouse not moved, so not a drag
281  QToolButton::mouseMoveEvent( e );
282  return;
283  }
284 
285  //user is dragging color
286  QDrag *drag = new QDrag( this );
287  drag->setMimeData( QgsSymbolLayerUtils::colorToMimeData( mColor ) );
288  drag->setPixmap( QgsColorWidget::createDragIcon( mColor ) );
289  drag->exec( Qt::CopyAction );
290  setDown( false );
291 }
292 
293 void QgsColorButton::mouseReleaseEvent( QMouseEvent *e )
294 {
295  if ( mPickingColor )
296  {
297  //end color picking operation by sampling the color under cursor
298  stopPicking( e->globalPos() );
299  e->accept();
300  return;
301  }
302 
303  QToolButton::mouseReleaseEvent( e );
304 }
305 
306 void QgsColorButton::stopPicking( QPoint eventPos, bool samplingColor )
307 {
308  //release mouse and keyboard, and reset cursor
309  releaseMouse();
310  releaseKeyboard();
311  QgsApplication::restoreOverrideCursor();
312  setMouseTracking( false );
313  mPickingColor = false;
314 
315  if ( !samplingColor )
316  {
317  //not sampling color, restore old color
318  setButtonBackground( mCurrentColor );
319  return;
320  }
321 
322  setColor( sampleColor( eventPos ) );
323  addRecentColor( mColor );
324 }
325 
326 void QgsColorButton::keyPressEvent( QKeyEvent *e )
327 {
328  if ( !mPickingColor )
329  {
330  //if not picking a color, use default tool button behavior
331  QToolButton::keyPressEvent( e );
332  return;
333  }
334 
335  //cancel picking, sampling the color if space was pressed
336  stopPicking( QCursor::pos(), e->key() == Qt::Key_Space );
337 }
338 
339 void QgsColorButton::dragEnterEvent( QDragEnterEvent *e )
340 {
341  //is dragged data valid color data?
342  QColor mimeColor;
343  if ( colorFromMimeData( e->mimeData(), mimeColor ) )
344  {
345  //if so, we accept the drag, and temporarily change the button's color
346  //to match the dragged color. This gives immediate feedback to the user
347  //that colors can be dropped here
348  e->acceptProposedAction();
349  setButtonBackground( mimeColor );
350  }
351 }
352 
353 void QgsColorButton::dragLeaveEvent( QDragLeaveEvent *e )
354 {
355  Q_UNUSED( e );
356  //reset button color
357  setButtonBackground( mColor );
358 }
359 
360 void QgsColorButton::dropEvent( QDropEvent *e )
361 {
362  //is dropped data valid color data?
363  QColor mimeColor;
364  if ( colorFromMimeData( e->mimeData(), mimeColor ) )
365  {
366  //accept drop and set new color
367  e->acceptProposedAction();
368  setColor( mimeColor );
369  addRecentColor( mimeColor );
370  }
371 }
372 
373 QColor QgsColorButton::sampleColor( QPoint point ) const
374 {
375 
376  QScreen *screen = findScreenAt( point );
377  if ( ! screen )
378  {
379  return QColor();
380  }
381  QPixmap snappedPixmap = screen->grabWindow( QApplication::desktop()->winId(), point.x(), point.y(), 1, 1 );
382  QImage snappedImage = snappedPixmap.toImage();
383  return snappedImage.pixel( 0, 0 );
384 }
385 
386 QScreen *QgsColorButton::findScreenAt( QPoint pos )
387 {
388  for ( QScreen *screen : QGuiApplication::screens() )
389  {
390  if ( screen->geometry().contains( pos ) )
391  {
392  return screen;
393  }
394  }
395  return nullptr;
396 }
397 
398 void QgsColorButton::setValidColor( const QColor &newColor )
399 {
400  if ( newColor.isValid() )
401  {
402  setColor( newColor );
403  addRecentColor( newColor );
404  }
405 }
406 
407 void QgsColorButton::setValidTemporaryColor( const QColor &newColor )
408 {
409  if ( newColor.isValid() )
410  {
411  setColor( newColor );
412  }
413 }
414 
415 QPixmap QgsColorButton::createMenuIcon( const QColor &color, const bool showChecks )
416 {
417  const int iconSize = QgsGuiUtils::scaleIconSize( 16 );
418 
419  //create an icon pixmap
420  QPixmap pixmap( iconSize, iconSize );
421  pixmap.fill( Qt::transparent );
422 
423  QPainter p;
424  p.begin( &pixmap );
425 
426  //start with checkboard pattern
427  if ( showChecks )
428  {
429  QBrush checkBrush = QBrush( transparentBackground() );
430  p.setPen( Qt::NoPen );
431  p.setBrush( checkBrush );
432  p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
433  }
434 
435  //draw color over pattern
436  p.setBrush( QBrush( color ) );
437 
438  //draw border
439  p.setPen( QColor( 197, 197, 197 ) );
440  p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
441  p.end();
442  return pixmap;
443 }
444 
445 void QgsColorButton::buttonClicked()
446 {
447  switch ( mBehavior )
448  {
449  case ShowDialog:
450  showColorDialog();
451  return;
452  case SignalOnly:
453  emit colorClicked( mColor );
454  return;
455  }
456 }
457 
458 void QgsColorButton::prepareMenu()
459 {
460  //we need to tear down and rebuild this menu every time it is shown. Otherwise the space allocated to any
461  //QgsColorSwatchGridAction is not recalculated by Qt and the swatch grid may not be the correct size
462  //for the number of colors shown in the grid. Note that we MUST refresh color swatch grids every time this
463  //menu is opened, otherwise color schemes like the recent color scheme grid are meaningless
464  mMenu->clear();
465 
466  if ( mShowNull )
467  {
468  QAction *nullAction = new QAction( tr( "Clear Color" ), this );
469  nullAction->setIcon( createMenuIcon( Qt::transparent, false ) );
470  mMenu->addAction( nullAction );
471  connect( nullAction, &QAction::triggered, this, &QgsColorButton::setToNull );
472  }
473 
474  //show default color option if set
475  if ( mDefaultColor.isValid() )
476  {
477  QAction *defaultColorAction = new QAction( tr( "Default Color" ), this );
478  defaultColorAction->setIcon( createMenuIcon( mDefaultColor ) );
479  mMenu->addAction( defaultColorAction );
480  connect( defaultColorAction, &QAction::triggered, this, &QgsColorButton::setToDefaultColor );
481  }
482 
483  if ( mShowNoColorOption && mAllowOpacity )
484  {
485  QAction *noColorAction = new QAction( mNoColorString, this );
486  noColorAction->setIcon( createMenuIcon( Qt::transparent, false ) );
487  mMenu->addAction( noColorAction );
488  connect( noColorAction, &QAction::triggered, this, &QgsColorButton::setToNoColor );
489  }
490 
491  mMenu->addSeparator();
492  QgsColorWheel *colorWheel = new QgsColorWheel( mMenu );
493  colorWheel->setColor( color() );
494  QgsColorWidgetAction *colorAction = new QgsColorWidgetAction( colorWheel, mMenu, mMenu );
495  colorAction->setDismissOnColorSelection( false );
496  connect( colorAction, &QgsColorWidgetAction::colorChanged, this, &QgsColorButton::setColor );
497  mMenu->addAction( colorAction );
498  if ( mAllowOpacity )
499  {
501  alphaRamp->setColor( color() );
502  QgsColorWidgetAction *alphaAction = new QgsColorWidgetAction( alphaRamp, mMenu, mMenu );
503  alphaAction->setDismissOnColorSelection( false );
504  connect( alphaAction, &QgsColorWidgetAction::colorChanged, this, &QgsColorButton::setColor );
505  connect( alphaAction, &QgsColorWidgetAction::colorChanged, colorWheel, [colorWheel]( const QColor & color ) { colorWheel->setColor( color, false ); }
506  );
507  connect( colorAction, &QgsColorWidgetAction::colorChanged, alphaRamp, [alphaRamp]( const QColor & color ) { alphaRamp->setColor( color, false ); }
508  );
509  mMenu->addAction( alphaAction );
510  }
511 
512  if ( mColorSchemeRegistry )
513  {
514  //get schemes with ShowInColorButtonMenu flag set
515  QList< QgsColorScheme * > schemeList = mColorSchemeRegistry->schemes( QgsColorScheme::ShowInColorButtonMenu );
516  QList< QgsColorScheme * >::iterator it = schemeList.begin();
517  for ( ; it != schemeList.end(); ++it )
518  {
519  QgsColorSwatchGridAction *colorAction = new QgsColorSwatchGridAction( *it, mMenu, mContext, this );
520  colorAction->setBaseColor( mColor );
521  mMenu->addAction( colorAction );
522  connect( colorAction, &QgsColorSwatchGridAction::colorChanged, this, &QgsColorButton::setValidColor );
523  connect( colorAction, &QgsColorSwatchGridAction::colorChanged, this, &QgsColorButton::addRecentColor );
524  }
525  }
526 
527  mMenu->addSeparator();
528 
529  QAction *copyColorAction = new QAction( tr( "Copy Color" ), this );
530  mMenu->addAction( copyColorAction );
531  connect( copyColorAction, &QAction::triggered, this, &QgsColorButton::copyColor );
532 
533  QAction *pasteColorAction = new QAction( tr( "Paste Color" ), this );
534  //enable or disable paste action based on current clipboard contents. We always show the paste
535  //action, even if it's disabled, to give hint to the user that pasting colors is possible
536  QColor clipColor;
537  if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor ) )
538  {
539  pasteColorAction->setIcon( createMenuIcon( clipColor ) );
540  }
541  else
542  {
543  pasteColorAction->setEnabled( false );
544  }
545  mMenu->addAction( pasteColorAction );
546  connect( pasteColorAction, &QAction::triggered, this, &QgsColorButton::pasteColor );
547 
548  //disabled for OSX, as it is impossible to grab the mouse under OSX
549  //see note for QWidget::grabMouse() re OSX Cocoa
550  //http://qt-project.org/doc/qt-4.8/qwidget.html#grabMouse
551  QAction *pickColorAction = new QAction( tr( "Pick Color" ), this );
552  mMenu->addAction( pickColorAction );
553  connect( pickColorAction, &QAction::triggered, this, &QgsColorButton::activatePicker );
554 
555  QAction *chooseColorAction = new QAction( tr( "Choose Color…" ), this );
556  mMenu->addAction( chooseColorAction );
557  connect( chooseColorAction, &QAction::triggered, this, &QgsColorButton::showColorDialog );
558 }
559 
561 {
562  if ( e->type() == QEvent::EnabledChange )
563  {
565  }
566  QToolButton::changeEvent( e );
567 }
568 
569 #if 0 // causes too many cyclical updates, but may be needed on some platforms
570 void QgsColorButton::paintEvent( QPaintEvent *e )
571 {
572  QToolButton::paintEvent( e );
573 
574  if ( !mBackgroundSet )
575  {
577  }
578 }
579 #endif
580 
581 void QgsColorButton::showEvent( QShowEvent *e )
582 {
584  QToolButton::showEvent( e );
585 }
586 
587 void QgsColorButton::resizeEvent( QResizeEvent *event )
588 {
589  QToolButton::resizeEvent( event );
590  //recalculate icon size and redraw icon
591  mIconSize = QSize();
592  setButtonBackground( mColor );
593 }
594 
595 void QgsColorButton::setColor( const QColor &color )
596 {
597  QColor oldColor = mColor;
598  mColor = color;
599 
600  // handle when initially set color is same as default (Qt::black); consider it a color change
601  if ( oldColor != mColor || ( mColor == QColor( Qt::black ) && !mColorSet ) )
602  {
604  if ( isEnabled() )
605  {
606  // TODO: May be beneficial to have the option to set color without emitting this signal.
607  // Now done by blockSignals( bool ) where button is used
608  emit colorChanged( mColor );
609  }
610  }
611  mColorSet = true;
612 }
613 
614 void QgsColorButton::addRecentColor( const QColor &color )
615 {
617 }
618 
620 {
621  QColor backgroundColor = color;
622 
623  if ( !color.isValid() )
624  {
625  backgroundColor = mColor;
626  }
627 
628  QSize currentIconSize;
629  //icon size is button size with a small margin
630  if ( menu() )
631  {
632  if ( !mIconSize.isValid() )
633  {
634  //calculate size of push button part of widget (ie, without the menu drop-down button part)
635  QStyleOptionToolButton opt;
636  initStyleOption( &opt );
637  QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
638  this );
639  //make sure height of icon looks good under different platforms
640 #ifdef Q_OS_WIN
641  mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
642 #else
643  mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
644 #endif
645  }
646  currentIconSize = mIconSize;
647  }
648  else
649  {
650  //no menu
651 #ifdef Q_OS_WIN
652  currentIconSize = QSize( width() - 10, height() - 6 );
653 #else
654  currentIconSize = QSize( width() - 10, height() - 12 );
655 #endif
656  }
657 
658  if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
659  {
660  return;
661  }
662 
663  //create an icon pixmap
664  QPixmap pixmap( currentIconSize );
665  pixmap.fill( Qt::transparent );
666 
667  if ( backgroundColor.isValid() )
668  {
669  QRect rect( 0, 0, currentIconSize.width(), currentIconSize.height() );
670  QPainter p;
671  p.begin( &pixmap );
672  p.setRenderHint( QPainter::Antialiasing );
673  p.setPen( Qt::NoPen );
674  if ( mAllowOpacity && backgroundColor.alpha() < 255 )
675  {
676  //start with checkboard pattern
677  QBrush checkBrush = QBrush( transparentBackground() );
678  p.setBrush( checkBrush );
679  p.drawRoundedRect( rect, 3, 3 );
680  }
681 
682  //draw semi-transparent color on top
683  p.setBrush( backgroundColor );
684  p.drawRoundedRect( rect, 3, 3 );
685  p.end();
686  }
687 
688  setIconSize( currentIconSize );
689  setIcon( pixmap );
690 }
691 
693 {
694  //copy color
695  QApplication::clipboard()->setMimeData( QgsSymbolLayerUtils::colorToMimeData( mColor ) );
696 }
697 
699 {
700  QColor clipColor;
701  if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor ) )
702  {
703  //paste color
704  setColor( clipColor );
705  addRecentColor( clipColor );
706  }
707 }
708 
710 {
711  //activate picker color
712  // Store current color
713  mCurrentColor = mColor;
714  QApplication::setOverrideCursor( QgsApplication::getThemeCursor( QgsApplication::Cursor::Sampler ) );
715  grabMouse();
716  grabKeyboard();
717  mPickingColor = true;
718  setMouseTracking( true );
719 }
720 
721 QColor QgsColorButton::color() const
722 {
723  return mColor;
724 }
725 
726 void QgsColorButton::setAllowOpacity( const bool allow )
727 {
728  mAllowOpacity = allow;
729 }
730 
731 void QgsColorButton::setColorDialogTitle( const QString &title )
732 {
733  mColorDialogTitle = title;
734 }
735 
736 QString QgsColorButton::colorDialogTitle() const
737 {
738  return mColorDialogTitle;
739 }
740 
742 {
743  setMenu( showMenu ? mMenu : nullptr );
744  setPopupMode( showMenu ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
745  //force recalculation of icon size
746  mIconSize = QSize();
747  setButtonBackground( mColor );
748 }
749 
751 {
752  mBehavior = behavior;
753 }
754 
756 {
757  mDefaultColor = color;
758 }
759 
761 {
762  mShowNull = showNull;
763 }
764 
766 {
767  return mShowNull;
768 }
769 
771 {
772  return !mColor.isValid();
773 }
774 
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
A color swatch grid which can be embedded into a menu.
void setToNoColor()
Sets color to a totally transparent color.
void keyPressEvent(QKeyEvent *e) override
Reimplemented to allow canceling color pick via keypress, and sample via space bar press...
QgsColorButton(QWidget *parent=nullptr, const QString &cdt=QString(), QgsColorSchemeRegistry *registry=nullptr)
Construct a new color ramp button.
Emit colorClicked signal only, no dialog.
void setButtonBackground(const QColor &color=QColor())
Sets the background pixmap for the button based upon color and transparency.
bool dockMode()
Returns the dock mode state.
Registry of color schemes.
void setToNull()
Sets color to null.
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:152
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
bool event(QEvent *e) override
virtual void setColor(const QColor &color, bool emitSignals=false)
Sets the color for the widget.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly...
Show a color picker dialog when clicked.
void mousePressEvent(QMouseEvent *e) override
Reimplemented to detect right mouse button clicks on the color button and allow dragging colors...
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void colorClicked(const QColor &color)
Emitted when the button is clicked, if the button&#39;s behavior is set to SignalOnly.
void colorChanged(const QColor &color)
Emitted when a color has been selected from the widget.
bool isNull() const
Returns true if the current color is null.
QColor color() const
Returns the currently selected color.
Base class for any widget that can be shown as a inline panel.
void mouseReleaseEvent(QMouseEvent *e) override
Reimplemented to allow color picking.
void resizeEvent(QResizeEvent *event) override
QSize minimumSizeHint() const override
Show scheme in color button drop-down menu.
void dragLeaveEvent(QDragLeaveEvent *e) override
Reimplemented to reset button appearance after drag leave.
void pasteColor()
Pastes a color from the clipboard to the color button.
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.
QSize sizeHint() const override
void activatePicker()
Activates the color picker tool, which allows for sampling a color from anywhere on the screen...
static QString encodeColor(const QColor &color)
void changeEvent(QEvent *e) override
void setTitle(const QString &title)
Sets the title for the color dialog.
A color wheel widget.
static QgsPanelWidget * findParentPanel(QWidget *widget)
Traces through the parents of a widget to find if it is contained within a QgsPanelWidget widget...
A custom QGIS widget for selecting a color, including options for selecting colors via hue wheel...
bool showNull() const
Returns whether the set to null (clear) option is shown in the button&#39;s drop-down menu...
Alpha component (opacity) of color.
Behavior
Specifies the behavior when the button is clicked.
void setColorDialogTitle(const QString &title)
Set the title for the color chooser dialog window.
void setToDefaultColor()
Sets color to the button&#39;s default color, if set.
A color ramp widget.
void setDefaultColor(const QColor &color)
Sets the default color for the button, which is shown in the button&#39;s drop-down menu for the "default...
void setShowMenu(bool showMenu)
Sets whether the drop-down menu should be shown for the button.
void setColor(const QColor &color, bool emitSignals=false) override
void mouseMoveEvent(QMouseEvent *e) override
Reimplemented to allow dragging colors from button.
QString colorDialogTitle() const
Returns the title for the color chooser dialog window.
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.
void setAllowOpacity(bool allowOpacity)
Sets whether opacity modification (transparency) is permitted for the color dialog.
void setPreviousColor(const QColor &color)
Sets the color to show in an optional "previous color" section.
void colorChanged(const QColor &color)
Is emitted whenever a new color is set for the button.
A custom QGIS dialog for selecting a color.
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application&#39;s color scheme registry, used for managing color schemes. ...
void setAllowOpacity(bool allowOpacity)
Sets whether opacity modification (transparency) is permitted for the color.
void currentColorChanged(const QColor &color)
Emitted when the dialog&#39;s color changes.
An action containing a color widget, which can be embedded into a menu.
static QMimeData * colorToMimeData(const QColor &color)
Creates mime data from a color.
void setShowNull(bool showNull)
Sets whether a set to null (clear) option is shown in the button&#39;s drop-down menu.
void copyColor()
Copies the current color to the clipboard.
Behavior behavior() const
Returns the behavior for when the button is clicked.
bool showMenu() const
Returns whether the drop-down menu is shown for the button.
void setColor(const QColor &color)
Sets the current color for the button.
void dragEnterEvent(QDragEnterEvent *e) override
Reimplemented to accept dragged colors.
static QCursor getThemeCursor(Cursor cursor)
Helper to get a theme cursor.
void setDismissOnColorSelection(bool dismiss)
Sets whether the parent menu should be dismissed and closed when a color is selected from the action&#39;...
static const QPixmap & transparentBackground()
Returns a checkboard pattern pixmap for use as a background to transparent colors.
void setBehavior(Behavior behavior)
Sets the behavior for when the button is clicked.
static QPixmap createDragIcon(const QColor &color)
Create an icon for dragging colors.
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
QList< QgsColorScheme * > schemes() const
Returns all color schemes in the registry.
void dropEvent(QDropEvent *e) override
Reimplemented to accept dropped colors.
Use a narrower, vertically stacked layout.
void showEvent(QShowEvent *e) override
static QColor colorFromMimeData(const QMimeData *data, bool &hasAlpha)
Attempts to parse mime data as a color.
void colorChanged(const QColor &color)
Emitted when a color has been selected from the widget.
void setBaseColor(const QColor &baseColor)
Sets the base color for the color grid.