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