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