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