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