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