QGIS API Documentation  2.99.0-Master (e077efd)
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 
26 #include <QPainter>
27 #include <QSettings>
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  , mBehaviour( QgsColorButton::ShowDialog )
44  , mColorDialogTitle( cdt.isEmpty() ? tr( "Select Color" ) : cdt )
45  , mColor( QColor() )
46  , mDefaultColor( QColor() ) //default to invalid color
47  , mAllowAlpha( 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 : QgsColorSchemeRegistry::instance();
59 
60  setAcceptDrops( true );
61  setMinimumSize( QSize( 24, 16 ) );
62  connect( this, SIGNAL( clicked() ), this, SLOT( buttonClicked() ) );
63 
64  //setup dropdown menu
65  mMenu = new QMenu( this );
66  connect( mMenu, SIGNAL( aboutToShow() ), this, SLOT( prepareMenu() ) );
67  setMenu( mMenu );
68  setPopupMode( QToolButton::MenuButtonPopup );
69 }
70 
72 {
73 }
74 
76 {
77  //make sure height of button looks good under different platforms
78 #ifdef Q_OS_WIN
79  return QSize( 120, 22 );
80 #else
81  return QSize( 120, 28 );
82 #endif
83 }
84 
86 {
87  static QPixmap transpBkgrd;
88 
89  if ( transpBkgrd.isNull() )
90  transpBkgrd = QgsApplication::getThemePixmap( QStringLiteral( "/transp-background_8x8.png" ) );
91 
92  return transpBkgrd;
93 }
94 
95 void QgsColorButton::showColorDialog()
96 {
98  if ( panel && panel->dockMode() )
99  {
100  QColor currentColor = color();
102  colorWidget->setPanelTitle( mColorDialogTitle );
103  colorWidget->setAllowAlpha( mAllowAlpha );
104 
105  if ( currentColor.isValid() )
106  {
107  colorWidget->setPreviousColor( currentColor );
108  }
109 
110  connect( colorWidget, SIGNAL( currentColorChanged( QColor ) ), this, SLOT( setValidTemporaryColor( QColor ) ) );
111  panel->openPanel( colorWidget );
112  return;
113  }
114 
115  QColor newColor;
116  QSettings settings;
117 
118  if ( mAcceptLiveUpdates && settings.value( QStringLiteral( "/qgis/live_color_dialogs" ), false ).toBool() )
119  {
120  // live updating dialog - QgsColorDialog will automatically use native dialog if option is set
121  newColor = QgsColorDialog::getLiveColor(
122  color(), this, SLOT( setValidColor( const QColor& ) ),
123  this, mColorDialogTitle, mAllowAlpha );
124  }
125  else
126  {
127  // not using live updating dialog - first check if we need to use the limited native dialogs
128  bool useNative = settings.value( QStringLiteral( "/qgis/native_color_dialogs" ), false ).toBool();
129  if ( useNative )
130  {
131  // why would anyone want this? who knows.... maybe the limited nature of native dialogs helps ease the transition for MapInfo users?
132  newColor = QColorDialog::getColor( color(), this, mColorDialogTitle, mAllowAlpha ? QColorDialog::ShowAlphaChannel : ( QColorDialog::ColorDialogOption )0 );
133  }
134  else
135  {
136  QgsColorDialog dialog( this, 0, color() );
137  dialog.setTitle( mColorDialogTitle );
138  dialog.setAllowAlpha( mAllowAlpha );
139 
140  if ( dialog.exec() )
141  {
142  newColor = dialog.color();
143  }
144  }
145  }
146 
147  if ( newColor.isValid() )
148  {
149  setValidColor( newColor );
150  }
151 
152  // reactivate button's window
153  activateWindow();
154 }
155 
157 {
158  if ( !mDefaultColor.isValid() )
159  {
160  return;
161  }
162 
163  setColor( mDefaultColor );
164 }
165 
167 {
168  setColor( QColor() );
169 }
170 
171 bool QgsColorButton::event( QEvent *e )
172 {
173  if ( e->type() == QEvent::ToolTip )
174  {
175  QString name = this->color().name();
176  int hue = this->color().hue();
177  int value = this->color().value();
178  int saturation = this->color().saturation();
179  QString info = QString( "HEX: %1 \n"
180  "RGB: %2 \n"
181  "HSV: %3,%4,%5" ).arg( name,
183  .arg( hue ).arg( saturation ).arg( value );
184  setToolTip( info );
185  }
186  return QToolButton::event( e );
187 }
188 
190 {
191  if ( mAllowAlpha )
192  {
193  QColor noColor = QColor( mColor );
194  noColor.setAlpha( 0 );
195  setColor( noColor );
196  }
197 }
198 
199 void QgsColorButton::mousePressEvent( QMouseEvent *e )
200 {
201  if ( mPickingColor )
202  {
203  //don't show dialog if in color picker mode
204  e->accept();
205  return;
206  }
207 
208  if ( e->button() == Qt::RightButton )
209  {
210  QToolButton::showMenu();
211  return;
212  }
213  else if ( e->button() == Qt::LeftButton )
214  {
215  mDragStartPosition = e->pos();
216  }
217  QToolButton::mousePressEvent( e );
218 }
219 
220 bool QgsColorButton::colorFromMimeData( const QMimeData * mimeData, QColor& resultColor )
221 {
222  bool hasAlpha = false;
223  QColor mimeColor = QgsSymbolLayerUtils::colorFromMimeData( mimeData, hasAlpha );
224 
225  if ( mimeColor.isValid() )
226  {
227  if ( !mAllowAlpha )
228  {
229  //remove alpha channel
230  mimeColor.setAlpha( 255 );
231  }
232  else if ( !hasAlpha )
233  {
234  //mime color has no explicit alpha component, so keep existing alpha
235  mimeColor.setAlpha( mColor.alpha() );
236  }
237  resultColor = mimeColor;
238  return true;
239  }
240 
241  //could not get color from mime data
242  return false;
243 }
244 
245 void QgsColorButton::mouseMoveEvent( QMouseEvent *e )
246 {
247  if ( mPickingColor )
248  {
249  //currently in color picker mode
250  if ( e->buttons() & Qt::LeftButton )
251  {
252  //if left button depressed, sample color under cursor and temporarily update button color
253  //to give feedback to user
254  QPixmap snappedPixmap = QPixmap::grabWindow( QApplication::desktop()->winId(), e->globalPos().x(), e->globalPos().y(), 1, 1 );
255  QImage snappedImage = snappedPixmap.toImage();
256  QColor hoverColor = snappedImage.pixel( 0, 0 );
257  setButtonBackground( hoverColor );
258  }
259  e->accept();
260  return;
261  }
262 
263  //handle dragging colors from button
264 
265  if ( !( e->buttons() & Qt::LeftButton ) || !mColor.isValid() )
266  {
267  //left button not depressed or no color set, so not a drag
268  QToolButton::mouseMoveEvent( e );
269  return;
270  }
271 
272  if (( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
273  {
274  //mouse not moved, so not a drag
275  QToolButton::mouseMoveEvent( e );
276  return;
277  }
278 
279  //user is dragging color
280  QDrag *drag = new QDrag( this );
281  drag->setMimeData( QgsSymbolLayerUtils::colorToMimeData( mColor ) );
282  drag->setPixmap( QgsColorWidget::createDragIcon( mColor ) );
283  drag->exec( Qt::CopyAction );
284  setDown( false );
285 }
286 
287 void QgsColorButton::mouseReleaseEvent( QMouseEvent *e )
288 {
289  if ( mPickingColor )
290  {
291  //end color picking operation by sampling the color under cursor
292  stopPicking( e->globalPos() );
293  e->accept();
294  return;
295  }
296 
297  QToolButton::mouseReleaseEvent( e );
298 }
299 
300 void QgsColorButton::stopPicking( QPointF eventPos, bool sampleColor )
301 {
302  //release mouse and keyboard, and reset cursor
303  releaseMouse();
304  releaseKeyboard();
305  unsetCursor();
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 = QPixmap::grabWindow( QApplication::desktop()->winId(), 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 behaviour
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 void QgsColorButton::setValidColor( const QColor& newColor )
370 {
371  if ( newColor.isValid() )
372  {
373  setColor( newColor );
374  addRecentColor( newColor );
375  }
376 }
377 
378 void QgsColorButton::setValidTemporaryColor( const QColor& newColor )
379 {
380  if ( newColor.isValid() )
381  {
382  setColor( newColor );
383  }
384 }
385 
386 QPixmap QgsColorButton::createMenuIcon( const QColor &color, const bool showChecks )
387 {
388  //create an icon pixmap
389  QPixmap pixmap( 16, 16 );
390  pixmap.fill( Qt::transparent );
391 
392  QPainter p;
393  p.begin( &pixmap );
394 
395  //start with checkboard pattern
396  if ( showChecks )
397  {
398  QBrush checkBrush = QBrush( transparentBackground() );
399  p.setPen( Qt::NoPen );
400  p.setBrush( checkBrush );
401  p.drawRect( 0, 0, 15, 15 );
402  }
403 
404  //draw color over pattern
405  p.setBrush( QBrush( color ) );
406 
407  //draw border
408  p.setPen( QColor( 197, 197, 197 ) );
409  p.drawRect( 0, 0, 15, 15 );
410  p.end();
411  return pixmap;
412 }
413 
414 void QgsColorButton::buttonClicked()
415 {
416  switch ( mBehaviour )
417  {
418  case ShowDialog:
419  showColorDialog();
420  return;
421  case SignalOnly:
422  emit colorClicked( mColor );
423  return;
424  }
425 }
426 
427 void QgsColorButton::prepareMenu()
428 {
429  //we need to tear down and rebuild this menu every time it is shown. Otherwise the space allocated to any
430  //QgsColorSwatchGridAction is not recalculated by Qt and the swatch grid may not be the correct size
431  //for the number of colors shown in the grid. Note that we MUST refresh color swatch grids every time this
432  //menu is opened, otherwise color schemes like the recent color scheme grid are meaningless
433  mMenu->clear();
434 
435  if ( mShowNull )
436  {
437  QAction* nullAction = new QAction( tr( "Clear color" ), this );
438  nullAction->setIcon( createMenuIcon( Qt::transparent, false ) );
439  mMenu->addAction( nullAction );
440  connect( nullAction, SIGNAL( triggered() ), this, SLOT( setToNull() ) );
441  }
442 
443  //show default color option if set
444  if ( mDefaultColor.isValid() )
445  {
446  QAction* defaultColorAction = new QAction( tr( "Default color" ), this );
447  defaultColorAction->setIcon( createMenuIcon( mDefaultColor ) );
448  mMenu->addAction( defaultColorAction );
449  connect( defaultColorAction, SIGNAL( triggered() ), this, SLOT( setToDefaultColor() ) );
450  }
451 
452  if ( mShowNoColorOption && mAllowAlpha )
453  {
454  QAction* noColorAction = new QAction( mNoColorString, this );
455  noColorAction->setIcon( createMenuIcon( Qt::transparent, false ) );
456  mMenu->addAction( noColorAction );
457  connect( noColorAction, SIGNAL( triggered() ), this, SLOT( setToNoColor() ) );
458  }
459 
460  mMenu->addSeparator();
461  QgsColorWheel* colorWheel = new QgsColorWheel( mMenu );
462  colorWheel->setColor( color() );
463  QgsColorWidgetAction* colorAction = new QgsColorWidgetAction( colorWheel, mMenu, mMenu );
464  colorAction->setDismissOnColorSelection( false );
465  connect( colorAction, SIGNAL( colorChanged( const QColor& ) ), this, SLOT( setColor( const QColor& ) ) );
466  mMenu->addAction( colorAction );
467 
468 
469  if ( mColorSchemeRegistry )
470  {
471  //get schemes with ShowInColorButtonMenu flag set
472  QList< QgsColorScheme* > schemeList = mColorSchemeRegistry->schemes( QgsColorScheme::ShowInColorButtonMenu );
473  QList< QgsColorScheme* >::iterator it = schemeList.begin();
474  for ( ; it != schemeList.end(); ++it )
475  {
476  QgsColorSwatchGridAction* colorAction = new QgsColorSwatchGridAction( *it, mMenu, mContext, this );
477  colorAction->setBaseColor( mColor );
478  mMenu->addAction( colorAction );
479  connect( colorAction, SIGNAL( colorChanged( const QColor& ) ), this, SLOT( setValidColor( const QColor& ) ) );
480  connect( colorAction, SIGNAL( colorChanged( const QColor& ) ), this, SLOT( addRecentColor( const QColor& ) ) );
481  }
482  }
483 
484  mMenu->addSeparator();
485 
486  QAction* copyColorAction = new QAction( tr( "Copy color" ), this );
487  mMenu->addAction( copyColorAction );
488  connect( copyColorAction, SIGNAL( triggered() ), this, SLOT( copyColor() ) );
489 
490  QAction* pasteColorAction = new QAction( tr( "Paste color" ), this );
491  //enable or disable paste action based on current clipboard contents. We always show the paste
492  //action, even if it's disabled, to give hint to the user that pasting colors is possible
493  QColor clipColor;
494  if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor ) )
495  {
496  pasteColorAction->setIcon( createMenuIcon( clipColor ) );
497  }
498  else
499  {
500  pasteColorAction->setEnabled( false );
501  }
502  mMenu->addAction( pasteColorAction );
503  connect( pasteColorAction, SIGNAL( triggered() ), this, SLOT( pasteColor() ) );
504 
505 #ifndef Q_OS_MAC
506  //disabled for OSX, as it is impossible to grab the mouse under OSX
507  //see note for QWidget::grabMouse() re OSX Cocoa
508  //http://qt-project.org/doc/qt-4.8/qwidget.html#grabMouse
509  QAction* pickColorAction = new QAction( tr( "Pick color" ), this );
510  mMenu->addAction( pickColorAction );
511  connect( pickColorAction, SIGNAL( triggered() ), this, SLOT( activatePicker() ) );
512 #endif
513  QAction* chooseColorAction = new QAction( tr( "Choose color..." ), this );
514  mMenu->addAction( chooseColorAction );
515  connect( chooseColorAction, SIGNAL( triggered() ), this, SLOT( showColorDialog() ) );
516 }
517 
519 {
520  if ( e->type() == QEvent::EnabledChange )
521  {
523  }
524  QToolButton::changeEvent( e );
525 }
526 
527 #if 0 // causes too many cyclical updates, but may be needed on some platforms
528 void QgsColorButton::paintEvent( QPaintEvent* e )
529 {
530  QToolButton::paintEvent( e );
531 
532  if ( !mBackgroundSet )
533  {
535  }
536 }
537 #endif
538 
539 void QgsColorButton::showEvent( QShowEvent* e )
540 {
542  QToolButton::showEvent( e );
543 }
544 
545 void QgsColorButton::resizeEvent( QResizeEvent *event )
546 {
547  QToolButton::resizeEvent( event );
548  //recalculate icon size and redraw icon
549  mIconSize = QSize();
550  setButtonBackground( mColor );
551 }
552 
553 void QgsColorButton::setColor( const QColor &color )
554 {
555  QColor oldColor = mColor;
556  mColor = color;
557 
558  // handle when initially set color is same as default (Qt::black); consider it a color change
559  if ( oldColor != mColor || ( mColor == QColor( Qt::black ) && !mColorSet ) )
560  {
562  if ( isEnabled() )
563  {
564  // TODO: May be beneficial to have the option to set color without emitting this signal.
565  // Now done by blockSignals( bool ) where button is used
566  emit colorChanged( mColor );
567  }
568  }
569  mColorSet = true;
570 }
571 
572 void QgsColorButton::addRecentColor( const QColor& color )
573 {
575 }
576 
578 {
579  QColor backgroundColor = color;
580 
581  if ( !color.isValid() )
582  {
583  backgroundColor = mColor;
584  }
585 
586  QSize currentIconSize;
587  //icon size is button size with a small margin
588  if ( menu() )
589  {
590  if ( !mIconSize.isValid() )
591  {
592  //calculate size of push button part of widget (ie, without the menu dropdown button part)
593  QStyleOptionToolButton opt;
594  initStyleOption( &opt );
595  QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
596  this );
597  //make sure height of icon looks good under different platforms
598 #ifdef Q_OS_WIN
599  mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
600 #else
601  mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
602 #endif
603  }
604  currentIconSize = mIconSize;
605  }
606  else
607  {
608  //no menu
609 #ifdef Q_OS_WIN
610  currentIconSize = QSize( width() - 10, height() - 6 );
611 #else
612  currentIconSize = QSize( width() - 10, height() - 12 );
613 #endif
614  }
615 
616  if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
617  {
618  return;
619  }
620 
621  //create an icon pixmap
622  QPixmap pixmap( currentIconSize );
623  pixmap.fill( Qt::transparent );
624 
625  if ( backgroundColor.isValid() )
626  {
627  QRect rect( 0, 0, currentIconSize.width(), currentIconSize.height() );
628  QPainter p;
629  p.begin( &pixmap );
630  p.setRenderHint( QPainter::Antialiasing );
631  p.setPen( Qt::NoPen );
632  if ( mAllowAlpha && backgroundColor.alpha() < 255 )
633  {
634  //start with checkboard pattern
635  QBrush checkBrush = QBrush( transparentBackground() );
636  p.setBrush( checkBrush );
637  p.drawRoundedRect( rect, 3, 3 );
638  }
639 
640  //draw semi-transparent color on top
641  p.setBrush( backgroundColor );
642  p.drawRoundedRect( rect, 3, 3 );
643  p.end();
644  }
645 
646  setIconSize( currentIconSize );
647  setIcon( pixmap );
648 }
649 
651 {
652  //copy color
653  QApplication::clipboard()->setMimeData( QgsSymbolLayerUtils::colorToMimeData( mColor ) );
654 }
655 
657 {
658  QColor clipColor;
659  if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor ) )
660  {
661  //paste color
662  setColor( clipColor );
663  addRecentColor( clipColor );
664  }
665 }
666 
668 {
669  //pick color
670  QPixmap samplerPixmap = QPixmap(( const char ** ) sampler_cursor );
671  setCursor( QCursor( samplerPixmap, 0, 0 ) );
672  grabMouse();
673  grabKeyboard();
674  mPickingColor = true;
675 }
676 
677 QColor QgsColorButton::color() const
678 {
679  return mColor;
680 }
681 
683 {
684  mAllowAlpha = allowAlpha;
685 }
686 
687 void QgsColorButton::setColorDialogTitle( const QString& title )
688 {
689  mColorDialogTitle = title;
690 }
691 
692 QString QgsColorButton::colorDialogTitle() const
693 {
694  return mColorDialogTitle;
695 }
696 
698 {
699  setMenu( showMenu ? mMenu : nullptr );
700  setPopupMode( showMenu ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
701  //force recalculation of icon size
702  mIconSize = QSize();
703  setButtonBackground( mColor );
704 }
705 
707 {
708  mBehaviour = behaviour;
709 }
710 
712 {
713  mDefaultColor = color;
714 }
715 
717 {
718  mShowNull = showNull;
719 }
720 
722 {
723  return mShowNull;
724 }
725 
727 {
728  return !mColor.isValid();
729 }
730 
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 cancelling color pick via keypress, and sample via space bar press...
bool isNull() const
Returns true if the current color is null.
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.
bool event(QEvent *e) override
void setAllowAlpha(const bool allowAlpha)
Sets whether alpha modification (transparency) is permitted for the color dialog. ...
bool showMenu() const
Returns whether the drop down menu is shown for the button.
virtual ~QgsColorButton()
void setShowMenu(const bool showMenu)
Sets whether the drop down menu should be shown for the button.
Emit colorClicked signal only, no dialog.
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 behaviour is set to SignalOnly.
bool allowAlpha() const
Returns whether alpha modification (transparency) is permitted for the color.
Base class for any widget that can be shown as a inline panel.
void mouseReleaseEvent(QMouseEvent *e) override
Reimplemented to allow color picking.
void resizeEvent(QResizeEvent *event) override
bool showNull() const
Returns whether the set to null (clear) option is shown in the button&#39;s drop down menu...
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 &theName)
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...
void setBehaviour(const Behaviour behaviour)
Sets the behaviour for 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.
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.
static void addRecentColor(const QColor &color)
Adds a color to the list of recent colors.
Show a color picker dialog when clicked.
void setAllowAlpha(const bool allowAlpha)
Sets whether alpha modification (transparency) is permitted for the color dialog. ...
QColor color() const
Returns the current color for the dialog.
void setPreviousColor(const QColor &color)
Sets the color to show in an optional "previous color" section.
static QgsColorSchemeRegistry * instance()
Returns the global instance pointer, creating the object on the first call.
void colorChanged(const QColor &color)
Is emitted whenever a new color is set for the button.
A custom QGIS dialog for selecting a color.
const char * sampler_cursor[]
Definition: qgscursors.cpp:167
void setAllowAlpha(const bool allowAlpha)
Sets whether alpha modification (transparency) is permitted for the color.
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.
Behaviour
Specifies the behaviour when the button is clicked.
void copyColor()
Copies the current color to the clipboard.
static QColor getLiveColor(const QColor &initialColor, QObject *updateObject, const char *updateSlot, QWidget *parent=nullptr, const QString &title=QString(), const bool allowAlpha=true)
Return a color selection from a color dialog, with live updating of interim selections.
void setColor(const QColor &color)
Sets the current color for the button.
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 button.
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.
Behaviour behaviour() const
Returns the behaviour for when the button is clicked.
void setBaseColor(const QColor &baseColor)
Sets the base color for the color grid.