QGIS API Documentation 3.43.0-Master (58029bba303)
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 "moc_qgscolorbutton.cpp"
18#include "qgscolordialog.h"
19#include "qgsapplication.h"
20#include "qgssymbollayerutils.h"
21#include "qgscolorswatchgrid.h"
23#include "qgscolorwidgets.h"
24#include "qgssettings.h"
25#include "qgsproject.h"
26#include "qgsguiutils.h"
27#include "qgsgui.h"
28#include "qgscolortooltip_p.h"
29
30#include <QPainter>
31#include <QMouseEvent>
32#include <QMenu>
33#include <QClipboard>
34#include <QDrag>
35#include <QStyle>
36#include <QStyleOptionToolButton>
37#include <QWidgetAction>
38#include <QScreen>
39#include <QLabel>
40#include <QGridLayout>
41#include <QPushButton>
42#include <QBuffer>
43
44QgsColorButton::QgsColorButton( QWidget *parent, const QString &cdt, QgsColorSchemeRegistry *registry )
45 : QToolButton( parent )
46 , mColorDialogTitle( cdt.isEmpty() ? tr( "Select Color" ) : cdt )
47 , mNoColorString( tr( "No color" ) )
48{
49 //if a color scheme registry was specified, use it, otherwise use the global instance
50 mColorSchemeRegistry = registry ? registry : QgsApplication::colorSchemeRegistry();
51
52 setAcceptDrops( true );
53 setMinimumSize( QSize( 24, 16 ) );
54 connect( this, &QAbstractButton::clicked, this, &QgsColorButton::buttonClicked );
55
56 //setup drop-down menu
57 mMenu = new QMenu( this );
58 connect( mMenu, &QMenu::aboutToShow, this, &QgsColorButton::prepareMenu );
59 setMenu( mMenu );
60 setPopupMode( QToolButton::MenuButtonPopup );
61
62#ifdef Q_OS_WIN
63 mMinimumSize = QSize( 120, 22 );
64#else
65 mMinimumSize = QSize( 120, 28 );
66#endif
67
68 mMinimumSize.setHeight( std::max( static_cast<int>( Qgis::UI_SCALE_FACTOR * fontMetrics().height() * 1.1 ), mMinimumSize.height() ) );
69
70 // If project colors change, we need to redraw the button, as it may be set to follow a project color
73 } );
74}
75
77{
78 return mMinimumSize;
79}
80
82{
83 return mMinimumSize;
84}
85
87{
88 static QPixmap sTranspBkgrd;
89
90 if ( sTranspBkgrd.isNull() )
91 sTranspBkgrd = QgsApplication::getThemePixmap( QStringLiteral( "/transp-background_8x8.png" ) );
92
93 return sTranspBkgrd;
94}
95
96void QgsColorButton::showColorDialog()
97{
99 if ( panel && panel->dockMode() )
100 {
101 const QColor currentColor = color();
103 colorWidget->setPanelTitle( mColorDialogTitle );
104 colorWidget->setAllowOpacity( mAllowOpacity );
105
106 if ( currentColor.isValid() )
107 {
108 colorWidget->setPreviousColor( currentColor );
109 }
110
111 connect( colorWidget, &QgsCompoundColorWidget::currentColorChanged, this, &QgsColorButton::setValidTemporaryColor );
112 panel->openPanel( colorWidget );
113 return;
114 }
115
116 QColor newColor;
117 const QgsSettings settings;
118
119 // first check if we need to use the limited native dialogs
120 const bool useNative = settings.value( QStringLiteral( "qgis/native_color_dialogs" ), false ).toBool();
121 if ( useNative )
122 {
123 // why would anyone want this? who knows.... maybe the limited nature of native dialogs helps ease the transition for MapInfo users?
124 newColor = QColorDialog::getColor( color(), this, mColorDialogTitle, mAllowOpacity ? QColorDialog::ShowAlphaChannel : ( QColorDialog::ColorDialogOption ) 0 );
125 }
126 else
127 {
128 QgsColorDialog dialog( this, Qt::WindowFlags(), color() );
129 dialog.setTitle( mColorDialogTitle );
130 dialog.setAllowOpacity( mAllowOpacity );
131
132 if ( dialog.exec() )
133 {
134 newColor = dialog.color();
135 }
136 }
137
138 if ( newColor.isValid() )
139 {
140 setValidColor( newColor );
141 }
142
143 // reactivate button's window
144 activateWindow();
145}
146
148{
149 if ( !mDefaultColor.isValid() )
150 {
151 return;
152 }
153
154 setColor( mDefaultColor );
155}
156
158{
159 setColor( QColor() );
160 emit cleared();
161}
162
164{
165 linkToProjectColor( QString() );
166 emit unlinked();
167}
168
169bool QgsColorButton::event( QEvent *e )
170{
171 if ( e->type() == QEvent::ToolTip && isEnabled() )
172 {
173 QColor c = linkedProjectColor();
174 const bool isProjectColor = c.isValid();
175 if ( !isProjectColor )
176 c = mColor;
177
178 QString info = ( isProjectColor ? QStringLiteral( "<p>%1: %2</p>" ).arg( tr( "Linked color" ), mLinkedColorName ) : QString() );
179
180 info += QgsColorTooltip::htmlDescription( c, this );
181
182 setToolTip( info );
183 }
184 return QToolButton::event( e );
185}
186
188{
189 QColor noColor = QColor( mColor );
190 noColor.setAlpha( 0 );
191 setColor( noColor );
192}
193
195{
196 if ( mPickingColor )
197 {
198 //don't show dialog if in color picker mode
199 e->accept();
200 return;
201 }
202
203 if ( e->button() == Qt::RightButton )
204 {
205 QToolButton::showMenu();
206 return;
207 }
208 else if ( e->button() == Qt::LeftButton )
209 {
210 mDragStartPosition = e->pos();
211 }
212 QToolButton::mousePressEvent( e );
213}
214
215bool QgsColorButton::colorFromMimeData( const QMimeData *mimeData, QColor &resultColor )
216{
217 if ( !mimeData )
218 return false;
219
220 bool hasAlpha = false;
221 QColor mimeColor = QgsSymbolLayerUtils::colorFromMimeData( mimeData, hasAlpha );
222
223 if ( mimeColor.isValid() )
224 {
225 if ( !mAllowOpacity )
226 {
227 //remove alpha channel
228 mimeColor.setAlpha( 255 );
229 }
230 else if ( !hasAlpha )
231 {
232 //mime color has no explicit alpha component, so keep existing alpha
233 mimeColor.setAlpha( mColor.alpha() );
234 }
235 resultColor = mimeColor;
236 return true;
237 }
238
239 //could not get color from mime data
240 return false;
241}
242
243void QgsColorButton::mouseMoveEvent( QMouseEvent *e )
244{
245 if ( mPickingColor )
246 {
247 setButtonBackground( QgsGui::sampleColor( e->globalPos() ) );
248 e->accept();
249 return;
250 }
251
252 //handle dragging colors from button
253 QColor c = linkedProjectColor();
254 if ( !c.isValid() )
255 c = mColor;
256
257 if ( !( e->buttons() & Qt::LeftButton ) || !c.isValid() )
258 {
259 //left button not depressed or no color set, so not a drag
260 QToolButton::mouseMoveEvent( e );
261 return;
262 }
263
264 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
265 {
266 //mouse not moved, so not a drag
267 QToolButton::mouseMoveEvent( e );
268 return;
269 }
270
271 //user is dragging color
272 QDrag *drag = new QDrag( this );
273 drag->setMimeData( QgsSymbolLayerUtils::colorToMimeData( c ) );
274 drag->setPixmap( QgsColorWidget::createDragIcon( c ) );
275 drag->exec( Qt::CopyAction );
276 setDown( false );
277}
278
280{
281 if ( mPickingColor )
282 {
283 //end color picking operation by sampling the color under cursor
284 stopPicking( e->globalPos() );
285 e->accept();
286 return;
287 }
288
289 QToolButton::mouseReleaseEvent( e );
290}
291
292void QgsColorButton::stopPicking( QPoint eventPos, bool samplingColor )
293{
294 //release mouse and keyboard, and reset cursor
295 releaseMouse();
296 releaseKeyboard();
297 QgsApplication::restoreOverrideCursor();
298 setMouseTracking( false );
299 mPickingColor = false;
300
301 if ( !samplingColor )
302 {
303 //not sampling color, restore old color
305 return;
306 }
307
308 setColor( QgsGui::sampleColor( eventPos ) );
309 addRecentColor( mColor );
310}
311
312QColor QgsColorButton::linkedProjectColor() const
313{
314 QList<QgsProjectColorScheme *> projectSchemes;
315 QgsApplication::colorSchemeRegistry()->schemes( projectSchemes );
316 if ( projectSchemes.length() > 0 )
317 {
318 QgsProjectColorScheme *scheme = projectSchemes.at( 0 );
319 const QgsNamedColorList colors = scheme->fetchColors();
320 for ( const auto &color : colors )
321 {
322 if ( color.second.isEmpty() )
323 continue;
324
325 if ( color.second == mLinkedColorName )
326 {
327 return color.first;
328 }
329 }
330 }
331 return QColor();
332}
333
335{
336 if ( !mPickingColor )
337 {
338 //if not picking a color, use default tool button behavior
339 QToolButton::keyPressEvent( e );
340 return;
341 }
342
343 //cancel picking, sampling the color if space was pressed
344 stopPicking( QCursor::pos(), e->key() == Qt::Key_Space );
345}
346
347void QgsColorButton::dragEnterEvent( QDragEnterEvent *e )
348{
349 const bool isProjectColor = linkedProjectColor().isValid();
350 if ( isProjectColor )
351 return;
352
353 //is dragged data valid color data?
354 QColor mimeColor;
355 if ( colorFromMimeData( e->mimeData(), mimeColor ) )
356 {
357 //if so, we accept the drag, and temporarily change the button's color
358 //to match the dragged color. This gives immediate feedback to the user
359 //that colors can be dropped here
360 e->acceptProposedAction();
361 setButtonBackground( mimeColor );
362 }
363}
364
365void QgsColorButton::dragLeaveEvent( QDragLeaveEvent *e )
366{
367 Q_UNUSED( e )
368 //reset button color
370}
371
372void QgsColorButton::dropEvent( QDropEvent *e )
373{
374 const bool isProjectColor = linkedProjectColor().isValid();
375 if ( isProjectColor )
376 return;
377
378 //is dropped data valid color data?
379 QColor mimeColor;
380 if ( colorFromMimeData( e->mimeData(), mimeColor ) )
381 {
382 //accept drop and set new color
383 e->acceptProposedAction();
384 setColor( mimeColor );
385 addRecentColor( mimeColor );
386 }
387}
388
389void QgsColorButton::wheelEvent( QWheelEvent *event )
390{
391 if ( mAllowOpacity && isEnabled() && !isNull() )
392 {
393 const double increment = ( ( event->modifiers() & Qt::ControlModifier ) ? 0.01 : 0.1 ) * ( event->angleDelta().y() > 0 ? 1 : -1 );
394 const double alpha = std::min( std::max( 0.0, mColor.alphaF() + increment ), 1.0 );
395 mColor.setAlphaF( alpha );
396
398 emit colorChanged( mColor );
399 event->accept();
400 }
401 else
402 {
403 QToolButton::wheelEvent( event );
404 }
405}
406
407void QgsColorButton::setValidColor( const QColor &newColor )
408{
409 if ( newColor.isValid() )
410 {
411 setColor( newColor );
412 addRecentColor( newColor );
413 }
414}
415
416void QgsColorButton::setValidTemporaryColor( const QColor &newColor )
417{
418 if ( newColor.isValid() )
419 {
420 setColor( newColor );
421 }
422}
423
424QPixmap QgsColorButton::createMenuIcon( const QColor &color, const bool showChecks )
425{
426 const int iconSize = QgsGuiUtils::scaleIconSize( 16 );
427
428 //create an icon pixmap
429 QPixmap pixmap( iconSize, iconSize );
430 pixmap.fill( Qt::transparent );
431
432 QPainter p;
433 p.begin( &pixmap );
434
435 //start with checkboard pattern
436 if ( showChecks )
437 {
438 const QBrush checkBrush = QBrush( transparentBackground() );
439 p.setPen( Qt::NoPen );
440 p.setBrush( checkBrush );
441 p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
442 }
443
444 //draw color over pattern
445 p.setBrush( QBrush( color ) );
446
447 //draw border
448 p.setPen( QColor( 197, 197, 197 ) );
449 p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
450 p.end();
451 return pixmap;
452}
453
454void QgsColorButton::buttonClicked()
455{
456 if ( linkedProjectColor().isValid() )
457 {
458 QToolButton::showMenu();
459 }
460 else
461 {
462 switch ( mBehavior )
463 {
464 case ShowDialog:
465 showColorDialog();
466 return;
467 case SignalOnly:
468 emit colorClicked( mColor );
469 return;
470 }
471 }
472}
473
474void QgsColorButton::prepareMenu()
475{
476 //we need to tear down and rebuild this menu every time it is shown. Otherwise the space allocated to any
477 //QgsColorSwatchGridAction is not recalculated by Qt and the swatch grid may not be the correct size
478 //for the number of colors shown in the grid. Note that we MUST refresh color swatch grids every time this
479 //menu is opened, otherwise color schemes like the recent color scheme grid are meaningless
480 mMenu->clear();
481
482 const bool isProjectColor = linkedProjectColor().isValid();
483
484 if ( !isProjectColor )
485 {
486 if ( mShowNull )
487 {
488 QAction *nullAction = new QAction( mNullColorString.isEmpty() ? tr( "Clear Color" ) : mNullColorString, this );
489 nullAction->setIcon( createMenuIcon( Qt::transparent, false ) );
490 mMenu->addAction( nullAction );
491 connect( nullAction, &QAction::triggered, this, &QgsColorButton::setToNull );
492 }
493
494 //show default color option if set
495 if ( mDefaultColor.isValid() )
496 {
497 QAction *defaultColorAction = new QAction( tr( "Default Color" ), this );
498 defaultColorAction->setIcon( createMenuIcon( mDefaultColor ) );
499 mMenu->addAction( defaultColorAction );
500 connect( defaultColorAction, &QAction::triggered, this, &QgsColorButton::setToDefaultColor );
501 }
502
503 if ( mShowNoColorOption )
504 {
505 QAction *noColorAction = new QAction( mNoColorString, this );
506 noColorAction->setIcon( createMenuIcon( Qt::transparent, false ) );
507 mMenu->addAction( noColorAction );
508 connect( noColorAction, &QAction::triggered, this, &QgsColorButton::setToNoColor );
509 }
510
511 mMenu->addSeparator();
512 QgsColorWheel *colorWheel = new QgsColorWheel( mMenu );
513 colorWheel->setColor( color() );
514 QgsColorWidgetAction *colorAction = new QgsColorWidgetAction( colorWheel, mMenu, mMenu );
515 colorAction->setDismissOnColorSelection( false );
516 connect( colorAction, &QgsColorWidgetAction::colorChanged, this, &QgsColorButton::setColor );
517 mMenu->addAction( colorAction );
518 if ( mAllowOpacity )
519 {
521 alphaRamp->setColor( color() );
522 QgsColorWidgetAction *alphaAction = new QgsColorWidgetAction( alphaRamp, mMenu, mMenu );
523 alphaAction->setDismissOnColorSelection( false );
524 connect( alphaAction, &QgsColorWidgetAction::colorChanged, this, &QgsColorButton::setColor );
525 connect( alphaAction, &QgsColorWidgetAction::colorChanged, colorWheel, [colorWheel]( const QColor &color ) { colorWheel->setColor( color, false ); } );
526 connect( colorAction, &QgsColorWidgetAction::colorChanged, alphaRamp, [alphaRamp]( const QColor &color ) { alphaRamp->setColor( color, false ); } );
527 mMenu->addAction( alphaAction );
528 }
529
530 if ( mColorSchemeRegistry )
531 {
532 //get schemes with ShowInColorButtonMenu flag set
533 QList<QgsColorScheme *> schemeList = mColorSchemeRegistry->schemes( QgsColorScheme::ShowInColorButtonMenu );
534 QList<QgsColorScheme *>::iterator it = schemeList.begin();
535 for ( ; it != schemeList.end(); ++it )
536 {
537 QgsColorSwatchGridAction *colorAction = new QgsColorSwatchGridAction( *it, mMenu, mContext, this );
538 colorAction->setBaseColor( mColor );
539 mMenu->addAction( colorAction );
540 connect( colorAction, &QgsColorSwatchGridAction::colorChanged, this, &QgsColorButton::setValidColor );
541 connect( colorAction, &QgsColorSwatchGridAction::colorChanged, this, &QgsColorButton::addRecentColor );
542 }
543 }
544
545 mMenu->addSeparator();
546 }
547
548 if ( isProjectColor )
549 {
550 QAction *unlinkAction = new QAction( tr( "Unlink Color" ), mMenu );
551 mMenu->addAction( unlinkAction );
552 connect( unlinkAction, &QAction::triggered, this, &QgsColorButton::unlink );
553 }
554
555 QAction *copyColorAction = new QAction( tr( "Copy Color" ), this );
556 mMenu->addAction( copyColorAction );
557 connect( copyColorAction, &QAction::triggered, this, &QgsColorButton::copyColor );
558
559 if ( !isProjectColor )
560 {
561 QAction *pasteColorAction = new QAction( tr( "Paste Color" ), this );
562 //enable or disable paste action based on current clipboard contents. We always show the paste
563 //action, even if it's disabled, to give hint to the user that pasting colors is possible
564 QColor clipColor;
565 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor ) )
566 {
567 pasteColorAction->setIcon( createMenuIcon( clipColor ) );
568 }
569 else
570 {
571 pasteColorAction->setEnabled( false );
572 }
573 mMenu->addAction( pasteColorAction );
574 connect( pasteColorAction, &QAction::triggered, this, &QgsColorButton::pasteColor );
575
576 QAction *pickColorAction = new QAction( tr( "Pick Color" ), this );
577 mMenu->addAction( pickColorAction );
578 connect( pickColorAction, &QAction::triggered, this, &QgsColorButton::activatePicker );
579
580 QAction *chooseColorAction = new QAction( tr( "Choose Color…" ), this );
581 mMenu->addAction( chooseColorAction );
582 connect( chooseColorAction, &QAction::triggered, this, &QgsColorButton::showColorDialog );
583 }
584}
585
587{
588 if ( e->type() == QEvent::EnabledChange )
589 {
591 }
592 QToolButton::changeEvent( e );
593}
594
595#if 0 // causes too many cyclical updates, but may be needed on some platforms
596void QgsColorButton::paintEvent( QPaintEvent *e )
597{
598 QToolButton::paintEvent( e );
599
600 if ( !mBackgroundSet )
601 {
603 }
604}
605#endif
606
607void QgsColorButton::showEvent( QShowEvent *e )
608{
610 QToolButton::showEvent( e );
611}
612
613void QgsColorButton::resizeEvent( QResizeEvent *event )
614{
615 QToolButton::resizeEvent( event );
616 //recalculate icon size and redraw icon
617 mIconSize = QSize();
619}
620
621void QgsColorButton::setColor( const QColor &color )
622{
623 const QColor oldColor = mColor;
624 mColor = color;
625
626 // handle when initially set color is same as default (Qt::black); consider it a color change
627 if ( oldColor != mColor || ( mColor == QColor( Qt::black ) && !mColorSet ) )
628 {
630 if ( isEnabled() )
631 {
632 // TODO: May be beneficial to have the option to set color without emitting this signal.
633 // Now done by blockSignals( bool ) where button is used
634 emit colorChanged( mColor );
635 }
636 }
637 mColorSet = true;
638}
639
640void QgsColorButton::addRecentColor( const QColor &color )
641{
643}
644
645void QgsColorButton::setButtonBackground( const QColor &color )
646{
647 QColor backgroundColor = color;
648 bool isProjectColor = false;
649 if ( !backgroundColor.isValid() && !mLinkedColorName.isEmpty() )
650 {
651 backgroundColor = linkedProjectColor();
652 isProjectColor = backgroundColor.isValid();
653 if ( !isProjectColor )
654 {
655 mLinkedColorName.clear(); //color has been deleted, renamed, etc...
656 emit unlinked();
657 }
658 }
659 if ( !backgroundColor.isValid() )
660 {
661 backgroundColor = mColor;
662 }
663
664 QSize currentIconSize;
665 //icon size is button size with a small margin
666 if ( menu() )
667 {
668 if ( !mIconSize.isValid() )
669 {
670 //calculate size of push button part of widget (ie, without the menu drop-down button part)
671 QStyleOptionToolButton opt;
672 initStyleOption( &opt );
673 const QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton, this );
674 //make sure height of icon looks good under different platforms
675#ifdef Q_OS_WIN
676 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
677#else
678 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
679#endif
680 }
681 currentIconSize = mIconSize;
682 }
683 else
684 {
685 //no menu
686#ifdef Q_OS_WIN
687 currentIconSize = QSize( width() - 10, height() - 6 );
688#else
689 currentIconSize = QSize( width() - 10, height() - 12 );
690#endif
691 }
692
693 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
694 {
695 return;
696 }
697
698 //create an icon pixmap
699 const double pixelRatio = devicePixelRatioF();
700 QPixmap pixmap( currentIconSize * pixelRatio );
701 pixmap.setDevicePixelRatio( pixelRatio );
702 pixmap.fill( Qt::transparent );
703
704 if ( backgroundColor.isValid() )
705 {
706 const QRectF rect( 0, 0, currentIconSize.width(), currentIconSize.height() );
707 QPainter p;
708 p.begin( &pixmap );
709 p.setRenderHint( QPainter::Antialiasing );
710 p.setPen( Qt::NoPen );
711 if ( mAllowOpacity && backgroundColor.alpha() < 255 )
712 {
713 //start with checkboard pattern
714 const QBrush checkBrush = QBrush( transparentBackground() );
715 p.setBrush( checkBrush );
716 p.drawRoundedRect( rect, 3, 3 );
717
718 //draw semi-transparent color on top
719 p.setBrush( backgroundColor );
720 p.drawRoundedRect( rect, 3, 3 );
721
722 //draw fully opaque color on the left side
723 const QRectF clipRect( 0, 0, static_cast<qreal>( currentIconSize.width() ) / 2.0, currentIconSize.height() );
724 p.setClipRect( clipRect );
725 backgroundColor.setAlpha( 255 );
726 p.setBrush( backgroundColor );
727 p.drawRoundedRect( rect, 3, 3 );
728 }
729 else
730 {
731 p.setBrush( backgroundColor );
732 p.drawRoundedRect( rect, 3, 3 );
733 }
734 p.end();
735 }
736
737 setIconSize( currentIconSize );
738 setIcon( pixmap );
739}
740
742{
743 //copy color
744 QColor c = linkedProjectColor();
745 if ( !c.isValid() )
746 c = mColor;
747 QApplication::clipboard()->setMimeData( QgsSymbolLayerUtils::colorToMimeData( c ) );
748}
749
751{
752 QColor clipColor;
753 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor ) )
754 {
755 //paste color
756 setColor( clipColor );
757 addRecentColor( clipColor );
758 }
759}
760
762{
763 //activate picker color
764 // Store current color
765 mCurrentColor = mColor;
766 QApplication::setOverrideCursor( QgsApplication::getThemeCursor( QgsApplication::Cursor::Sampler ) );
767 grabMouse();
768 grabKeyboard();
769 mPickingColor = true;
770 setMouseTracking( true );
771}
772
774{
775 QColor c = linkedProjectColor();
776 if ( !c.isValid() )
777 c = mColor;
778 return c;
779}
780
781void QgsColorButton::setAllowOpacity( const bool allow )
782{
783 mAllowOpacity = allow;
784}
785
786void QgsColorButton::setColorDialogTitle( const QString &title )
787{
788 mColorDialogTitle = title;
789}
790
792{
793 return mColorDialogTitle;
794}
795
796void QgsColorButton::setShowMenu( const bool showMenu )
797{
798 mShowMenu = showMenu;
799 setMenu( showMenu ? mMenu : nullptr );
800 setPopupMode( showMenu ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
801 //force recalculation of icon size
802 mIconSize = QSize();
804}
805
807{
808 mBehavior = behavior;
809}
810
811void QgsColorButton::setDefaultColor( const QColor &color )
812{
813 mDefaultColor = color;
814}
815
816void QgsColorButton::setShowNull( bool showNull, const QString &nullString )
817{
818 mShowNull = showNull;
819 mNullColorString = nullString;
820}
821
823{
824 return mShowNull;
825}
826
828{
829 return !mColor.isValid();
830}
831
832void QgsColorButton::linkToProjectColor( const QString &name )
833{
834 mLinkedColorName = name;
836}
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition qgis.h:5933
static QPixmap getThemePixmap(const QString &name, const QColor &foreColor=QColor(), const QColor &backColor=QColor(), int size=16)
Helper to get a theme icon as a pixmap.
static QCursor getThemeCursor(Cursor cursor)
Helper to get a theme cursor.
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application's color scheme registry, used for managing color schemes.
@ Sampler
Color/Value picker.
void setDefaultColor(const QColor &color)
Sets the default color for the button, which is shown in the button's drop-down menu for the "default...
void mouseReleaseEvent(QMouseEvent *e) override
Reimplemented to allow color picking.
void dragLeaveEvent(QDragLeaveEvent *e) override
Reimplemented to reset button appearance after drag leave.
void colorChanged(const QColor &color)
Emitted whenever a new color is set for the button.
void copyColor()
Copies the current color to the clipboard.
void setButtonBackground(const QColor &color=QColor())
Sets the background pixmap for the button based upon color and transparency.
void mousePressEvent(QMouseEvent *e) override
Reimplemented to detect right mouse button clicks on the color button and allow dragging colors.
bool event(QEvent *e) override
void dropEvent(QDropEvent *e) override
Reimplemented to accept dropped colors.
static const QPixmap & transparentBackground()
Returns a checkboard pattern pixmap for use as a background to transparent colors.
Behavior
Specifies the behavior when the button is clicked.
@ ShowDialog
Show a color picker dialog when clicked.
@ SignalOnly
Emit colorClicked signal only, no dialog.
void setShowMenu(bool showMenu)
Sets whether the drop-down menu should be shown for the button.
QgsColorButton(QWidget *parent=nullptr, const QString &cdt=QString(), QgsColorSchemeRegistry *registry=nullptr)
Construct a new color ramp button.
QSize sizeHint() const override
static QPixmap createMenuIcon(const QColor &color, bool showChecks=true)
Creates an icon for displaying a color in a drop-down menu.
void setBehavior(Behavior behavior)
Sets the behavior for when the button is clicked.
void setColorDialogTitle(const QString &title)
Set the title for the color chooser dialog window.
QSize minimumSizeHint() const override
void setShowNull(bool showNull, const QString &nullString=QString())
Sets whether a set to null (clear) option is shown in the button's drop-down menu.
void linkToProjectColor(const QString &name)
Sets the button to link to an existing project color, by color name.
void unlinked()
Emitted when the color is unlinked, e.g.
void setAllowOpacity(bool allowOpacity)
Sets whether opacity modification (transparency) is permitted for the color.
void setToNoColor()
Sets color to a totally transparent color.
void activatePicker()
Activates the color picker tool, which allows for sampling a color from anywhere on the screen.
void mouseMoveEvent(QMouseEvent *e) override
Reimplemented to allow dragging colors from button.
void showEvent(QShowEvent *e) override
void setToDefaultColor()
Sets color to the button's default color, if set.
void colorClicked(const QColor &color)
Emitted when the button is clicked, if the button's behavior is set to SignalOnly.
void cleared()
Emitted when the color is cleared (set to null).
QString colorDialogTitle
void dragEnterEvent(QDragEnterEvent *e) override
Reimplemented to accept dragged colors.
void unlink()
Unlinks the button from a project color.
void setToNull()
Sets color to null.
void wheelEvent(QWheelEvent *event) override
bool isNull() const
Returns true if the current color is null.
void keyPressEvent(QKeyEvent *e) override
Reimplemented to allow canceling color pick via keypress, and sample via space bar press.
void resizeEvent(QResizeEvent *event) override
bool showNull() const
Returns whether the set to null (clear) option is shown in the button's drop-down menu.
void pasteColor()
Pastes a color from the clipboard to the color button.
void setColor(const QColor &color)
Sets the current color for the button.
void changeEvent(QEvent *e) override
A custom QGIS dialog for selecting a color.
A color ramp widget.
@ Horizontal
Horizontal ramp.
Registry of color schemes.
QList< QgsColorScheme * > schemes() const
Returns all color schemes in the registry.
@ ShowInColorButtonMenu
Show scheme in color button drop-down menu.
A color swatch grid which can be embedded into a menu.
void setBaseColor(const QColor &baseColor)
Sets the base color for the color grid.
void colorChanged(const QColor &color)
Emitted when a color has been selected from the widget.
static QString htmlDescription(QColor color, T *widget)
Returns an HTML desciption given a color with a preview image of the color.
A color wheel widget.
void setColor(const QColor &color, bool emitSignals=false) override
An action containing a color widget, which can be embedded into a menu.
void setDismissOnColorSelection(bool dismiss)
Sets whether the parent menu should be dismissed and closed when a color is selected from the action'...
void colorChanged(const QColor &color)
Emitted when a color has been selected from the widget.
static QPixmap createDragIcon(const QColor &color)
Create an icon for dragging colors.
virtual void setColor(const QColor &color, bool emitSignals=false)
Sets the color for the widget.
@ Alpha
Alpha component (opacity) of color.
A custom QGIS widget for selecting a color, including options for selecting colors via hue wheel,...
@ LayoutVertical
Use a narrower, vertically stacked layout.
void currentColorChanged(const QColor &color)
Emitted when the dialog's color changes.
void setPreviousColor(const QColor &color)
Sets the color to show in an optional "previous color" section.
void setAllowOpacity(bool allowOpacity)
Sets whether opacity modification (transparency) is permitted for the color dialog.
static QColor sampleColor(QPoint point)
Samples the color on screen at the specified global point (pixel).
Definition qgsgui.cpp:283
Base class for any widget that can be shown as an inline panel.
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
static QgsPanelWidget * findParentPanel(QWidget *widget)
Traces through the parents of a widget to find if it is contained within a QgsPanelWidget widget.
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
bool dockMode()
Returns the dock mode state.
A color scheme which contains project specific colors set through project properties dialog.
QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor()) override
Gets a list of colors from the scheme.
static QgsProject * instance()
Returns the QgsProject singleton instance.
void projectColorsChanged()
Emitted whenever the project's color scheme has been changed.
static void addRecentColor(const QColor &color)
Adds a color to the list of recent colors.
Stores settings for use within QGIS.
Definition qgssettings.h:65
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
static QColor colorFromMimeData(const QMimeData *data, bool &hasAlpha)
Attempts to parse mime data as a color.
static QMimeData * colorToMimeData(const QColor &color)
Creates mime data from a color.
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c