QGIS API Documentation  2.99.0-Master (ae4d26a)
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 "qgscursors.h"
22 #include "qgscolorswatchgrid.h"
23 #include "qgscolorschemeregistry.h"
24 #include "qgscolorwidgets.h"
25 #include "qgssettings.h"
26 
27 #include <QPainter>
28 #include <QTemporaryFile>
29 #include <QMouseEvent>
30 #include <QMenu>
31 #include <QClipboard>
32 #include <QDrag>
33 #include <QDesktopWidget>
34 #include <QStyle>
35 #include <QStyleOptionToolButton>
36 #include <QWidgetAction>
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  QPixmap snappedPixmap = QPixmap::grabWindow( QApplication::desktop()->winId(), e->globalPos().x(), e->globalPos().y(), 1, 1 );
250  QImage snappedImage = snappedPixmap.toImage();
251  QColor hoverColor = snappedImage.pixel( 0, 0 );
252  setButtonBackground( hoverColor );
253  }
254  e->accept();
255  return;
256  }
257 
258  //handle dragging colors from button
259 
260  if ( !( e->buttons() & Qt::LeftButton ) || !mColor.isValid() )
261  {
262  //left button not depressed or no color set, so not a drag
263  QToolButton::mouseMoveEvent( e );
264  return;
265  }
266 
267  if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
268  {
269  //mouse not moved, so not a drag
270  QToolButton::mouseMoveEvent( e );
271  return;
272  }
273 
274  //user is dragging color
275  QDrag *drag = new QDrag( this );
276  drag->setMimeData( QgsSymbolLayerUtils::colorToMimeData( mColor ) );
277  drag->setPixmap( QgsColorWidget::createDragIcon( mColor ) );
278  drag->exec( Qt::CopyAction );
279  setDown( false );
280 }
281 
282 void QgsColorButton::mouseReleaseEvent( QMouseEvent *e )
283 {
284  if ( mPickingColor )
285  {
286  //end color picking operation by sampling the color under cursor
287  stopPicking( e->globalPos() );
288  e->accept();
289  return;
290  }
291 
292  QToolButton::mouseReleaseEvent( e );
293 }
294 
295 void QgsColorButton::stopPicking( QPointF eventPos, bool sampleColor )
296 {
297  //release mouse and keyboard, and reset cursor
298  releaseMouse();
299  releaseKeyboard();
300  unsetCursor();
301  mPickingColor = false;
302 
303  if ( !sampleColor )
304  {
305  //not sampling color, nothing more to do
306  return;
307  }
308 
309  //grab snapshot of pixel under mouse cursor
310  QPixmap snappedPixmap = QPixmap::grabWindow( QApplication::desktop()->winId(), eventPos.x(), eventPos.y(), 1, 1 );
311  QImage snappedImage = snappedPixmap.toImage();
312  //extract color from pixel and set color
313  setColor( snappedImage.pixel( 0, 0 ) );
314  addRecentColor( mColor );
315 }
316 
317 void QgsColorButton::keyPressEvent( QKeyEvent *e )
318 {
319  if ( !mPickingColor )
320  {
321  //if not picking a color, use default tool button behavior
322  QToolButton::keyPressEvent( e );
323  return;
324  }
325 
326  //cancel picking, sampling the color if space was pressed
327  stopPicking( QCursor::pos(), e->key() == Qt::Key_Space );
328 }
329 
330 void QgsColorButton::dragEnterEvent( QDragEnterEvent *e )
331 {
332  //is dragged data valid color data?
333  QColor mimeColor;
334  if ( colorFromMimeData( e->mimeData(), mimeColor ) )
335  {
336  //if so, we accept the drag, and temporarily change the button's color
337  //to match the dragged color. This gives immediate feedback to the user
338  //that colors can be dropped here
339  e->acceptProposedAction();
340  setButtonBackground( mimeColor );
341  }
342 }
343 
344 void QgsColorButton::dragLeaveEvent( QDragLeaveEvent *e )
345 {
346  Q_UNUSED( e );
347  //reset button color
348  setButtonBackground( mColor );
349 }
350 
351 void QgsColorButton::dropEvent( QDropEvent *e )
352 {
353  //is dropped data valid color data?
354  QColor mimeColor;
355  if ( colorFromMimeData( e->mimeData(), mimeColor ) )
356  {
357  //accept drop and set new color
358  e->acceptProposedAction();
359  setColor( mimeColor );
360  addRecentColor( mimeColor );
361  }
362 }
363 
364 void QgsColorButton::setValidColor( const QColor &newColor )
365 {
366  if ( newColor.isValid() )
367  {
368  setColor( newColor );
369  addRecentColor( newColor );
370  }
371 }
372 
373 void QgsColorButton::setValidTemporaryColor( const QColor &newColor )
374 {
375  if ( newColor.isValid() )
376  {
377  setColor( newColor );
378  }
379 }
380 
381 QPixmap QgsColorButton::createMenuIcon( const QColor &color, const bool showChecks )
382 {
383  //create an icon pixmap
384  QPixmap pixmap( 16, 16 );
385  pixmap.fill( Qt::transparent );
386 
387  QPainter p;
388  p.begin( &pixmap );
389 
390  //start with checkboard pattern
391  if ( showChecks )
392  {
393  QBrush checkBrush = QBrush( transparentBackground() );
394  p.setPen( Qt::NoPen );
395  p.setBrush( checkBrush );
396  p.drawRect( 0, 0, 15, 15 );
397  }
398 
399  //draw color over pattern
400  p.setBrush( QBrush( color ) );
401 
402  //draw border
403  p.setPen( QColor( 197, 197, 197 ) );
404  p.drawRect( 0, 0, 15, 15 );
405  p.end();
406  return pixmap;
407 }
408 
409 void QgsColorButton::buttonClicked()
410 {
411  switch ( mBehavior )
412  {
413  case ShowDialog:
414  showColorDialog();
415  return;
416  case SignalOnly:
417  emit colorClicked( mColor );
418  return;
419  }
420 }
421 
422 void QgsColorButton::prepareMenu()
423 {
424  //we need to tear down and rebuild this menu every time it is shown. Otherwise the space allocated to any
425  //QgsColorSwatchGridAction is not recalculated by Qt and the swatch grid may not be the correct size
426  //for the number of colors shown in the grid. Note that we MUST refresh color swatch grids every time this
427  //menu is opened, otherwise color schemes like the recent color scheme grid are meaningless
428  mMenu->clear();
429 
430  if ( mShowNull )
431  {
432  QAction *nullAction = new QAction( tr( "Clear color" ), this );
433  nullAction->setIcon( createMenuIcon( Qt::transparent, false ) );
434  mMenu->addAction( nullAction );
435  connect( nullAction, &QAction::triggered, this, &QgsColorButton::setToNull );
436  }
437 
438  //show default color option if set
439  if ( mDefaultColor.isValid() )
440  {
441  QAction *defaultColorAction = new QAction( tr( "Default color" ), this );
442  defaultColorAction->setIcon( createMenuIcon( mDefaultColor ) );
443  mMenu->addAction( defaultColorAction );
444  connect( defaultColorAction, &QAction::triggered, this, &QgsColorButton::setToDefaultColor );
445  }
446 
447  if ( mShowNoColorOption && mAllowOpacity )
448  {
449  QAction *noColorAction = new QAction( mNoColorString, this );
450  noColorAction->setIcon( createMenuIcon( Qt::transparent, false ) );
451  mMenu->addAction( noColorAction );
452  connect( noColorAction, &QAction::triggered, this, &QgsColorButton::setToNoColor );
453  }
454 
455  mMenu->addSeparator();
456  QgsColorWheel *colorWheel = new QgsColorWheel( mMenu );
457  colorWheel->setColor( color() );
458  QgsColorWidgetAction *colorAction = new QgsColorWidgetAction( colorWheel, mMenu, mMenu );
459  colorAction->setDismissOnColorSelection( false );
460  connect( colorAction, &QgsColorWidgetAction::colorChanged, this, &QgsColorButton::setColor );
461  mMenu->addAction( colorAction );
462  if ( mAllowOpacity )
463  {
465  alphaRamp->setColor( color() );
466  QgsColorWidgetAction *alphaAction = new QgsColorWidgetAction( alphaRamp, mMenu, mMenu );
467  alphaAction->setDismissOnColorSelection( false );
468  connect( alphaAction, &QgsColorWidgetAction::colorChanged, this, &QgsColorButton::setColor );
469  connect( alphaAction, &QgsColorWidgetAction::colorChanged, colorWheel, [colorWheel]( const QColor & color ) { colorWheel->setColor( color, false ); }
470  );
471  connect( colorAction, &QgsColorWidgetAction::colorChanged, alphaRamp, [alphaRamp]( const QColor & color ) { alphaRamp->setColor( color, false ); }
472  );
473  mMenu->addAction( alphaAction );
474  }
475 
476  if ( mColorSchemeRegistry )
477  {
478  //get schemes with ShowInColorButtonMenu flag set
479  QList< QgsColorScheme * > schemeList = mColorSchemeRegistry->schemes( QgsColorScheme::ShowInColorButtonMenu );
480  QList< QgsColorScheme * >::iterator it = schemeList.begin();
481  for ( ; it != schemeList.end(); ++it )
482  {
483  QgsColorSwatchGridAction *colorAction = new QgsColorSwatchGridAction( *it, mMenu, mContext, this );
484  colorAction->setBaseColor( mColor );
485  mMenu->addAction( colorAction );
486  connect( colorAction, &QgsColorSwatchGridAction::colorChanged, this, &QgsColorButton::setValidColor );
487  connect( colorAction, &QgsColorSwatchGridAction::colorChanged, this, &QgsColorButton::addRecentColor );
488  }
489  }
490 
491  mMenu->addSeparator();
492 
493  QAction *copyColorAction = new QAction( tr( "Copy color" ), this );
494  mMenu->addAction( copyColorAction );
495  connect( copyColorAction, &QAction::triggered, this, &QgsColorButton::copyColor );
496 
497  QAction *pasteColorAction = new QAction( tr( "Paste color" ), this );
498  //enable or disable paste action based on current clipboard contents. We always show the paste
499  //action, even if it's disabled, to give hint to the user that pasting colors is possible
500  QColor clipColor;
501  if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor ) )
502  {
503  pasteColorAction->setIcon( createMenuIcon( clipColor ) );
504  }
505  else
506  {
507  pasteColorAction->setEnabled( false );
508  }
509  mMenu->addAction( pasteColorAction );
510  connect( pasteColorAction, &QAction::triggered, this, &QgsColorButton::pasteColor );
511 
512  //disabled for OSX, as it is impossible to grab the mouse under OSX
513  //see note for QWidget::grabMouse() re OSX Cocoa
514  //http://qt-project.org/doc/qt-4.8/qwidget.html#grabMouse
515  QAction *pickColorAction = new QAction( tr( "Pick color" ), this );
516  mMenu->addAction( pickColorAction );
517  connect( pickColorAction, &QAction::triggered, this, &QgsColorButton::activatePicker );
518 
519  QAction *chooseColorAction = new QAction( tr( "Choose color..." ), this );
520  mMenu->addAction( chooseColorAction );
521  connect( chooseColorAction, &QAction::triggered, this, &QgsColorButton::showColorDialog );
522 }
523 
525 {
526  if ( e->type() == QEvent::EnabledChange )
527  {
529  }
530  QToolButton::changeEvent( e );
531 }
532 
533 #if 0 // causes too many cyclical updates, but may be needed on some platforms
534 void QgsColorButton::paintEvent( QPaintEvent *e )
535 {
536  QToolButton::paintEvent( e );
537 
538  if ( !mBackgroundSet )
539  {
541  }
542 }
543 #endif
544 
545 void QgsColorButton::showEvent( QShowEvent *e )
546 {
548  QToolButton::showEvent( e );
549 }
550 
551 void QgsColorButton::resizeEvent( QResizeEvent *event )
552 {
553  QToolButton::resizeEvent( event );
554  //recalculate icon size and redraw icon
555  mIconSize = QSize();
556  setButtonBackground( mColor );
557 }
558 
559 void QgsColorButton::setColor( const QColor &color )
560 {
561  QColor oldColor = mColor;
562  mColor = color;
563 
564  // handle when initially set color is same as default (Qt::black); consider it a color change
565  if ( oldColor != mColor || ( mColor == QColor( Qt::black ) && !mColorSet ) )
566  {
568  if ( isEnabled() )
569  {
570  // TODO: May be beneficial to have the option to set color without emitting this signal.
571  // Now done by blockSignals( bool ) where button is used
572  emit colorChanged( mColor );
573  }
574  }
575  mColorSet = true;
576 }
577 
578 void QgsColorButton::addRecentColor( const QColor &color )
579 {
581 }
582 
584 {
585  QColor backgroundColor = color;
586 
587  if ( !color.isValid() )
588  {
589  backgroundColor = mColor;
590  }
591 
592  QSize currentIconSize;
593  //icon size is button size with a small margin
594  if ( menu() )
595  {
596  if ( !mIconSize.isValid() )
597  {
598  //calculate size of push button part of widget (ie, without the menu drop-down button part)
599  QStyleOptionToolButton opt;
600  initStyleOption( &opt );
601  QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
602  this );
603  //make sure height of icon looks good under different platforms
604 #ifdef Q_OS_WIN
605  mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
606 #else
607  mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
608 #endif
609  }
610  currentIconSize = mIconSize;
611  }
612  else
613  {
614  //no menu
615 #ifdef Q_OS_WIN
616  currentIconSize = QSize( width() - 10, height() - 6 );
617 #else
618  currentIconSize = QSize( width() - 10, height() - 12 );
619 #endif
620  }
621 
622  if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
623  {
624  return;
625  }
626 
627  //create an icon pixmap
628  QPixmap pixmap( currentIconSize );
629  pixmap.fill( Qt::transparent );
630 
631  if ( backgroundColor.isValid() )
632  {
633  QRect rect( 0, 0, currentIconSize.width(), currentIconSize.height() );
634  QPainter p;
635  p.begin( &pixmap );
636  p.setRenderHint( QPainter::Antialiasing );
637  p.setPen( Qt::NoPen );
638  if ( mAllowOpacity && backgroundColor.alpha() < 255 )
639  {
640  //start with checkboard pattern
641  QBrush checkBrush = QBrush( transparentBackground() );
642  p.setBrush( checkBrush );
643  p.drawRoundedRect( rect, 3, 3 );
644  }
645 
646  //draw semi-transparent color on top
647  p.setBrush( backgroundColor );
648  p.drawRoundedRect( rect, 3, 3 );
649  p.end();
650  }
651 
652  setIconSize( currentIconSize );
653  setIcon( pixmap );
654 }
655 
657 {
658  //copy color
659  QApplication::clipboard()->setMimeData( QgsSymbolLayerUtils::colorToMimeData( mColor ) );
660 }
661 
663 {
664  QColor clipColor;
665  if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor ) )
666  {
667  //paste color
668  setColor( clipColor );
669  addRecentColor( clipColor );
670  }
671 }
672 
674 {
675  //pick color
676  QPixmap samplerPixmap = QPixmap( ( const char ** ) sampler_cursor );
677  setCursor( QCursor( samplerPixmap, 0, 0 ) );
678  grabMouse();
679  grabKeyboard();
680  mPickingColor = true;
681 }
682 
683 QColor QgsColorButton::color() const
684 {
685  return mColor;
686 }
687 
688 void QgsColorButton::setAllowOpacity( const bool allow )
689 {
690  mAllowOpacity = allow;
691 }
692 
693 void QgsColorButton::setColorDialogTitle( const QString &title )
694 {
695  mColorDialogTitle = title;
696 }
697 
698 QString QgsColorButton::colorDialogTitle() const
699 {
700  return mColorDialogTitle;
701 }
702 
704 {
705  setMenu( showMenu ? mMenu : nullptr );
706  setPopupMode( showMenu ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
707  //force recalculation of icon size
708  mIconSize = QSize();
709  setButtonBackground( mColor );
710 }
711 
713 {
714  mBehavior = behavior;
715 }
716 
718 {
719  mDefaultColor = color;
720 }
721 
723 {
724  mShowNull = showNull;
725 }
726 
728 {
729  return mShowNull;
730 }
731 
733 {
734  return !mColor.isValid();
735 }
736 
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.
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.
const char * sampler_cursor[]
Definition: qgscursors.cpp:169
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.