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