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