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