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