QGIS API Documentation 4.3.0-Master (91d4a333d5d)
Loading...
Searching...
No Matches
qgssymbolbutton.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssymbolbutton.h
3 -----------------
4 Date : July 2017
5 Copyright : (C) 2017 by Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
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 "qgssymbolbutton.h"
17
18#include "qgsapplication.h"
19#include "qgscolordialog.h"
22#include "qgscolorswatchgrid.h"
23#include "qgscolorwidgets.h"
27#include "qgsfillsymbol.h"
28#include "qgsgui.h"
29#include "qgsguiutils.h"
30#include "qgslinesymbol.h"
31#include "qgsmarkersymbol.h"
32#include "qgspanelwidget.h"
33#include "qgsstyle.h"
34#include "qgssymbollayerutils.h"
36#include "qgsvectorlayer.h"
37
38#include <QBuffer>
39#include <QClipboard>
40#include <QDrag>
41#include <QMenu>
42#include <QMimeData>
43#include <QString>
44
45#include "moc_qgssymbolbutton.cpp"
46
47using namespace Qt::StringLiterals;
48
49QgsSymbolButton::QgsSymbolButton( QWidget *parent, const QString &dialogTitle )
50 : QToolButton( parent )
51 , mDialogTitle( dialogTitle.isEmpty() ? tr( "Symbol Settings" ) : dialogTitle )
52 , mPickerHelper( new QgsColorPickerHelper( this ) )
53{
54 mSymbol = QgsFillSymbol::createSimple( QVariantMap() );
55
56 setAcceptDrops( true );
57 connect( this, &QAbstractButton::clicked, this, &QgsSymbolButton::showSettingsDialog );
58
59 //setup dropdown menu
60 mMenu = new QMenu( this );
61 connect( mMenu, &QMenu::aboutToShow, this, &QgsSymbolButton::prepareMenu );
62 setMenu( mMenu );
63 setPopupMode( QToolButton::MenuButtonPopup );
64
65 updateSizeHint();
66
67 connect( mPickerHelper, &QgsColorPickerHelper::colorPicked, this, &QgsSymbolButton::onColorPicked );
68 connect( mPickerHelper, &QgsColorPickerHelper::canceled, this, &QgsSymbolButton::colorPickingCanceled );
69 connect( mPickerHelper, &QgsColorPickerHelper::colorHovered, this, &QgsSymbolButton::onColorHovered );
70}
71
72void QgsSymbolButton::updateSizeHint()
73{
74 //make sure height of button looks good under different platforms
75 const QSize size = QToolButton::minimumSizeHint();
76 const int fontHeight = static_cast<int>( Qgis::UI_SCALE_FACTOR * fontMetrics().height() * 1.4 );
77 switch ( mType )
78 {
80 if ( mFixedSizeConstraints )
81 {
82 if ( mSymbol )
83 {
84 mSizeHint = QSize( size.width(), std::max( size.height(), fontHeight * 3 ) );
85 setMaximumWidth( mSizeHint.height() * 1.5 );
86 setMinimumWidth( maximumWidth() );
87 }
88 else
89 {
90 mSizeHint = QSize( size.width(), fontHeight );
91 setMaximumWidth( 999999 );
92 mSizeHint.setWidth( QToolButton::sizeHint().width() );
93 }
94 }
95 else
96 {
97 setMaximumWidth( 999999 );
98 mSizeHint = QSize( size.width(), std::max( size.height(), fontHeight ) );
99 }
100 break;
101
105 mSizeHint = QSize( size.width(), std::max( size.height(), fontHeight ) );
106 break;
107 }
108
109 setMinimumHeight( mSizeHint.height() );
110
111 updateGeometry();
112}
113
115
117{
118 return mSizeHint;
119}
120
122{
123 return mSizeHint;
124}
125
127{
128 if ( type != mType )
129 {
130 switch ( type )
131 {
133 mSymbol = QgsMarkerSymbol::createSimple( QVariantMap() );
134 break;
135
137 mSymbol = QgsLineSymbol::createSimple( QVariantMap() );
138 break;
139
141 mSymbol = QgsFillSymbol::createSimple( QVariantMap() );
142 break;
143
145 break;
146 }
147 }
148 mType = type;
149 updateSizeHint();
150 updatePreview();
151}
152
153void QgsSymbolButton::showSettingsDialog()
154{
155 QgsExpressionContext context;
156 if ( mExpressionContextGenerator )
157 context = mExpressionContextGenerator->createExpressionContext();
158 else
159 {
161 }
162
163 std::unique_ptr<QgsSymbol> newSymbol;
164 if ( mSymbol )
165 {
166 newSymbol.reset( mSymbol->clone() );
167 }
168 else
169 {
170 switch ( mType )
171 {
174 break;
177 break;
180 break;
182 break;
183 }
184 }
185
186 QgsSymbolWidgetContext symbolContext;
187 symbolContext.setExpressionContext( &context );
188 symbolContext.setMapCanvas( mMapCanvas );
189 symbolContext.setMessageBar( mMessageBar );
190
191 QgsPanelWidget *panel = QgsPanelWidget::findParentPanel( this );
192 if ( panel && panel->dockMode() )
193 {
194 QgsSymbolSelectorWidget *widget = QgsSymbolSelectorWidget::createWidgetWithSymbolOwnership( std::move( newSymbol ), QgsStyle::defaultStyle(), mLayer, panel );
195 widget->setPanelTitle( mDialogTitle );
196 widget->setContext( symbolContext );
197 connect( widget, &QgsPanelWidget::changed, this, [this, widget] { updateSymbolFromWidget( widget ); } );
198 panel->openPanel( widget );
199 }
200 else
201 {
202 QgsSymbolSelectorDialog dialog( newSymbol.get(), QgsStyle::defaultStyle(), mLayer, this );
203 dialog.setWindowTitle( mDialogTitle );
204 dialog.setContext( symbolContext );
205 if ( dialog.exec() )
206 {
207 setSymbol( newSymbol.release() );
208 }
209
210 // reactivate button's window
211 activateWindow();
212 }
213}
214
215void QgsSymbolButton::updateSymbolFromWidget( QgsSymbolSelectorWidget *widget )
216{
217 setSymbol( widget->symbol()->clone() );
218}
219
221{
222 return mMapCanvas;
223}
224
229
231{
232 mMessageBar = bar;
233}
234
236{
237 return mMessageBar;
238}
239
241{
242 return mLayer;
243}
244
246{
247 mLayer = layer;
248}
249
251{
252 mExpressionContextGenerator = generator;
253}
254
256{
257 mDefaultSymbol.reset( symbol );
258}
259
261{
262 return mDefaultSymbol.get();
263}
264
266{
267 mSymbol.reset( symbol );
268 updateSizeHint();
269 updatePreview();
270 emit changed();
271}
272
273void QgsSymbolButton::setColor( const QColor &color )
274{
275 if ( !mSymbol )
276 return;
277
278 QColor opaque = color;
279 opaque.setAlphaF( 1.0 );
280
281 if ( opaque == mSymbol->color() )
282 return;
283
284 mSymbol->setColor( opaque );
285 updatePreview();
286 emit changed();
287}
288
290{
291 QApplication::clipboard()->setMimeData( QgsSymbolLayerUtils::symbolToMimeData( mSymbol.get() ).release() );
292}
293
295{
296 std::unique_ptr<QgsSymbol> symbol( QgsSymbolLayerUtils::symbolFromMimeData( QApplication::clipboard()->mimeData() ) );
297 if ( symbol && symbol->type() == mType )
298 setSymbol( symbol.release() );
299}
300
302{
303 QApplication::clipboard()->setMimeData( QgsSymbolLayerUtils::colorToMimeData( mSymbol->color() ).release() );
304}
305
307{
308 QColor clipColor;
309 bool hasAlpha = false;
310 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
311 {
312 //paste color
313 setColor( clipColor );
315 }
316}
317
319{
320 if ( e->button() == Qt::LeftButton )
321 {
322 mDragStartPosition = e->pos();
323 }
324 QToolButton::mousePressEvent( e );
325}
326
327void QgsSymbolButton::contextMenuEvent( QContextMenuEvent *e )
328{
329 showMenu();
330 e->accept();
331}
332
333void QgsSymbolButton::onColorHovered( const QColor &color )
334{
335 updatePreview( color );
336}
337
339{
340 //handle dragging colors/symbols from button
341
342 if ( !( e->buttons() & Qt::LeftButton ) )
343 {
344 //left button not depressed, so not a drag
345 QToolButton::mouseMoveEvent( e );
346 return;
347 }
348
349 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
350 {
351 //mouse not moved, so not a drag
352 QToolButton::mouseMoveEvent( e );
353 return;
354 }
355
356 //user is dragging
357 QDrag *drag = new QDrag( this );
358 drag->setMimeData( QgsSymbolLayerUtils::colorToMimeData( mSymbol->color() ).release() );
359 drag->setPixmap( QgsColorWidget::createDragIcon( mSymbol->color() ) );
360 drag->exec( Qt::CopyAction );
361 setDown( false );
362}
363
364void QgsSymbolButton::dragEnterEvent( QDragEnterEvent *e )
365{
366 //is dragged data valid color data?
367 QColor mimeColor;
368 bool hasAlpha = false;
369
370 if ( colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
371 {
372 //if so, we accept the drag, and temporarily change the button's color
373 //to match the dragged color. This gives immediate feedback to the user
374 //that colors can be dropped here
375 e->acceptProposedAction();
376 updatePreview( mimeColor );
377 }
378}
379
380void QgsSymbolButton::dragLeaveEvent( QDragLeaveEvent *e )
381{
382 Q_UNUSED( e )
383 //reset button color
384 updatePreview();
385}
386
387void QgsSymbolButton::dropEvent( QDropEvent *e )
388{
389 //is dropped data valid format data?
390 QColor mimeColor;
391 bool hasAlpha = false;
392 if ( colorFromMimeData( e->mimeData(), mimeColor, hasAlpha ) )
393 {
394 //accept drop and set new color
395 e->acceptProposedAction();
396 mimeColor.setAlphaF( 1.0 );
397 mSymbol->setColor( mimeColor );
399 updatePreview();
400 emit changed();
401 }
402 updatePreview();
403}
404
405void QgsSymbolButton::wheelEvent( QWheelEvent *event )
406{
407 if ( isEnabled() && mSymbol )
408 {
409 bool symbolChanged = false;
410 const double increment = ( ( event->modifiers() & Qt::ControlModifier ) ? 0.1 : 1 ) * ( event->angleDelta().y() > 0 ? 1 : -1 );
411 switch ( mSymbol->type() )
412 {
414 {
415 QgsMarkerSymbol *marker = qgis::down_cast<QgsMarkerSymbol *>( mSymbol.get() );
416 if ( marker )
417 {
418 const double size = std::max( 0.0, marker->size() + increment );
419 marker->setSize( size );
420 symbolChanged = true;
421 }
422 break;
423 }
424
426 {
427 QgsLineSymbol *line = qgis::down_cast<QgsLineSymbol *>( mSymbol.get() );
428 if ( line )
429 {
430 const double width = std::max( 0.0, line->width() + increment );
431 line->setWidth( width );
432 symbolChanged = true;
433 }
434 break;
435 }
436
439 break;
440 }
441
442 if ( symbolChanged )
443 {
444 updatePreview();
445 emit changed();
446 }
447
448 event->accept();
449 }
450 else
451 {
452 QToolButton::wheelEvent( event );
453 }
454}
455
456void QgsSymbolButton::prepareMenu()
457{
458 //we need to tear down and rebuild this menu every time it is shown. Otherwise the space allocated to any
459 //QgsColorSwatchGridAction is not recalculated by Qt and the swatch grid may not be the correct size
460 //for the number of colors shown in the grid. Note that we MUST refresh color swatch grids every time this
461 //menu is opened, otherwise color schemes like the recent color scheme grid are meaningless
462 mMenu->clear();
463
464 QAction *configureAction = new QAction( tr( "Configure Symbol…" ), this );
465 mMenu->addAction( configureAction );
466 connect( configureAction, &QAction::triggered, this, &QgsSymbolButton::showSettingsDialog );
467
468 QAction *copySymbolAction = new QAction( tr( "Copy Symbol" ), this );
469 copySymbolAction->setEnabled( !isNull() );
470 mMenu->addAction( copySymbolAction );
471 connect( copySymbolAction, &QAction::triggered, this, &QgsSymbolButton::copySymbol );
472
473 const int iconSize = QgsGuiUtils::scaleIconSize( 16 );
474
475 QAction *pasteSymbolAction = new QAction( tr( "Paste Symbol" ), this );
476 //enable or disable paste action based on current clipboard contents. We always show the paste
477 //action, even if it's disabled, to give hint to the user that pasting symbols is possible
478 std::unique_ptr<QgsSymbol> tempSymbol( QgsSymbolLayerUtils::symbolFromMimeData( QApplication::clipboard()->mimeData() ) );
479 if ( tempSymbol && tempSymbol->type() == mType )
480 {
481 pasteSymbolAction->setIcon( QgsSymbolLayerUtils::symbolPreviewIcon( tempSymbol.get(), QSize( iconSize, iconSize ), 1, nullptr, QgsScreenProperties( screen() ) ) );
482 }
483 else
484 {
485 pasteSymbolAction->setEnabled( false );
486 }
487 mMenu->addAction( pasteSymbolAction );
488 connect( pasteSymbolAction, &QAction::triggered, this, &QgsSymbolButton::pasteSymbol );
489
490 if ( mShowNull )
491 {
492 QAction *nullAction = new QAction( tr( "Clear Current Symbol" ), this );
493 nullAction->setEnabled( !isNull() );
494 mMenu->addAction( nullAction );
495 connect( nullAction, &QAction::triggered, this, &QgsSymbolButton::setToNull );
496 }
497
498 //show default symbol option if set
499 if ( mDefaultSymbol )
500 {
501 QAction *defaultSymbolAction = new QAction( tr( "Default Symbol" ), this );
502 defaultSymbolAction->setIcon( QgsSymbolLayerUtils::symbolPreviewIcon( mDefaultSymbol.get(), QSize( iconSize, iconSize ), 1, nullptr, QgsScreenProperties( screen() ) ) );
503 mMenu->addAction( defaultSymbolAction );
504 connect( defaultSymbolAction, &QAction::triggered, this, &QgsSymbolButton::setToDefaultSymbol );
505 }
506
507 if ( mSymbol )
508 {
509 mMenu->addSeparator();
510
511 QgsColorWheel *colorWheel = new QgsColorWheel( mMenu );
512 colorWheel->setColor( mSymbol->color() );
513 QgsColorWidgetAction *colorAction = new QgsColorWidgetAction( colorWheel, mMenu, mMenu );
514 colorAction->setDismissOnColorSelection( false );
515 connect( colorAction, &QgsColorWidgetAction::colorChanged, this, &QgsSymbolButton::setColor );
516 mMenu->addAction( colorAction );
517
518 QgsColorRampWidget *alphaRamp = new QgsColorRampWidget( mMenu, QgsColorWidget::Alpha, QgsColorRampWidget::Horizontal );
519 QColor alphaColor = mSymbol->color();
520 alphaColor.setAlphaF( mSymbol->opacity() );
521 alphaRamp->setColor( alphaColor );
522 QgsColorWidgetAction *alphaAction = new QgsColorWidgetAction( alphaRamp, mMenu, mMenu );
523 alphaAction->setDismissOnColorSelection( false );
524 connect( alphaAction, &QgsColorWidgetAction::colorChanged, this, [this]( const QColor &color ) {
525 const double opacity = color.alphaF();
526 mSymbol->setOpacity( opacity );
527 updatePreview();
528 emit changed();
529 } );
530 connect( colorAction, &QgsColorWidgetAction::colorChanged, alphaRamp, [alphaRamp]( const QColor &color ) { alphaRamp->setColor( color, false ); } );
531 mMenu->addAction( alphaAction );
532
533 //get schemes with ShowInColorButtonMenu flag set
535 QList<QgsColorScheme *>::iterator it = schemeList.begin();
536 for ( ; it != schemeList.end(); ++it )
537 {
538 QgsColorSwatchGridAction *colorAction = new QgsColorSwatchGridAction( *it, mMenu, u"symbology"_s, this );
539 colorAction->setBaseColor( mSymbol->color() );
540 mMenu->addAction( colorAction );
542 connect( colorAction, &QgsColorSwatchGridAction::colorChanged, this, &QgsSymbolButton::addRecentColor );
543 }
544
545 mMenu->addSeparator();
546
547 QAction *copyColorAction = new QAction( tr( "Copy Color" ), this );
548 mMenu->addAction( copyColorAction );
549 connect( copyColorAction, &QAction::triggered, this, &QgsSymbolButton::copyColor );
550
551 QAction *pasteColorAction = new QAction( tr( "Paste Color" ), this );
552 //enable or disable paste action based on current clipboard contents. We always show the paste
553 //action, even if it's disabled, to give hint to the user that pasting colors is possible
554 QColor clipColor;
555 bool hasAlpha = false;
556 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor, hasAlpha ) )
557 {
558 pasteColorAction->setIcon( createColorIcon( clipColor ) );
559 }
560 else
561 {
562 pasteColorAction->setEnabled( false );
563 }
564 mMenu->addAction( pasteColorAction );
565 connect( pasteColorAction, &QAction::triggered, this, &QgsSymbolButton::pasteColor );
566
567 QAction *pickColorAction = new QAction( tr( "Pick Color" ), this );
568 mMenu->addAction( pickColorAction );
569 connect( pickColorAction, &QAction::triggered, this, &QgsSymbolButton::activatePicker );
570
571 QAction *chooseColorAction = new QAction( tr( "Choose Color…" ), this );
572 mMenu->addAction( chooseColorAction );
573 connect( chooseColorAction, &QAction::triggered, this, &QgsSymbolButton::showColorDialog );
574 }
575}
576
577void QgsSymbolButton::addRecentColor( const QColor &color )
578{
580}
581
582void QgsSymbolButton::activatePicker()
583{
584 mPickerHelper->activate();
585}
586
588{
589 if ( e->type() == QEvent::EnabledChange )
590 {
591 updatePreview();
592 }
593 QToolButton::changeEvent( e );
594}
595
596void QgsSymbolButton::showEvent( QShowEvent *e )
597{
598 updatePreview();
599 QToolButton::showEvent( e );
600}
601
602void QgsSymbolButton::resizeEvent( QResizeEvent *event )
603{
604 QToolButton::resizeEvent( event );
605 //recalculate icon size and redraw icon
606 mIconSize = QSize();
607 updatePreview();
608}
609
610void QgsSymbolButton::updatePreview( const QColor &color, QgsSymbol *tempSymbol )
611{
612 QSize currentIconSize;
613 //icon size is button size with a small margin
614 if ( menu() )
615 {
616 if ( !mIconSize.isValid() )
617 {
618 //calculate size of push button part of widget (ie, without the menu dropdown button part)
619 QStyleOptionToolButton opt;
620 initStyleOption( &opt );
621 const QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton, this );
622 //make sure height of icon looks good under different platforms
623#ifdef Q_OS_WIN
624 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
625#else
626 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
627#endif
628 }
629 currentIconSize = mIconSize;
630 }
631 else
632 {
633 //no menu
634#ifdef Q_OS_WIN
635 currentIconSize = QSize( width() - 10, height() - 6 );
636#else
637 currentIconSize = QSize( width() - 10, height() - 12 );
638#endif
639 }
640
641 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
642 {
643 return;
644 }
645
646 std::unique_ptr<QgsSymbol> previewSymbol;
647
648 if ( tempSymbol )
649 {
650 previewSymbol.reset( tempSymbol->clone() );
651 }
652 else if ( mSymbol )
653 {
654 previewSymbol.reset( mSymbol->clone() );
655 }
656 else
657 {
658 setIconSize( currentIconSize );
659 setIcon( QIcon() );
660 setToolTip( QString() );
661 return;
662 }
663
664 if ( color.isValid() )
665 previewSymbol->setColor( color );
666
667 //create an icon pixmap
668 const QIcon icon = QgsSymbolLayerUtils::symbolPreviewIcon( previewSymbol.get(), currentIconSize, 0, nullptr, QgsScreenProperties( screen() ) );
669 setIconSize( currentIconSize );
670 setIcon( icon );
671
672 // set tooltip
673 // create very large preview image
674 const int width = static_cast<int>( Qgis::UI_SCALE_FACTOR * fontMetrics().horizontalAdvance( 'X' ) * 23 );
675 const int height = static_cast<int>( width / 1.61803398875 ); // golden ratio
676
677 const QPixmap pm = QgsSymbolLayerUtils::symbolPreviewPixmap( previewSymbol.get(), QSize( width, height ), height / 20, nullptr, false, nullptr, nullptr, QgsScreenProperties( screen() ) );
678 QByteArray data;
679 QBuffer buffer( &data );
680 pm.save( &buffer, "PNG", 100 );
681 setToolTip( u"<img src='data:image/png;base64, %3' width=\"%4\">"_s.arg( QString( data.toBase64() ) ).arg( width ) );
682}
683
684bool QgsSymbolButton::colorFromMimeData( const QMimeData *mimeData, QColor &resultColor, bool &hasAlpha )
685{
686 hasAlpha = false;
687 const QColor mimeColor = QgsSymbolLayerUtils::colorFromMimeData( mimeData, hasAlpha );
688
689 if ( mimeColor.isValid() )
690 {
691 resultColor = mimeColor;
692 return true;
693 }
694
695 //could not get color from mime data
696 return false;
697}
698
699QPixmap QgsSymbolButton::createColorIcon( const QColor &color ) const
700{
701 //create an icon pixmap
702 const int iconSize = QgsGuiUtils::scaleIconSize( 16 );
703 QPixmap pixmap( iconSize, iconSize );
704 pixmap.fill( Qt::transparent );
705
706 QPainter p;
707 p.begin( &pixmap );
708
709 //draw color over pattern
710 p.setBrush( QBrush( color ) );
711
712 //draw border
713 p.setPen( QColor( 197, 197, 197 ) );
714 p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
715 p.end();
716 return pixmap;
717}
718
719
720void QgsSymbolButton::onColorPicked( const QColor &color )
721{
722 setColor( color );
723 addRecentColor( color );
724}
725
726void QgsSymbolButton::colorPickingCanceled()
727{
728 updatePreview();
729}
730
731void QgsSymbolButton::showColorDialog()
732{
733 if ( !mSymbol )
734 return;
735
736 QgsPanelWidget *panel = QgsPanelWidget::findParentPanel( this );
737 if ( panel && panel->dockMode() )
738 {
739 const QColor currentColor = mSymbol->color();
740 QgsCompoundColorWidget *colorWidget = new QgsCompoundColorWidget( panel, currentColor, QgsCompoundColorWidget::LayoutVertical );
741 colorWidget->setPanelTitle( tr( "Symbol Color" ) );
742 colorWidget->setAllowOpacity( true );
743
744 if ( currentColor.isValid() )
745 {
746 colorWidget->setPreviousColor( currentColor );
747 }
748
749 connect( colorWidget, &QgsCompoundColorWidget::currentColorChanged, this, [this]( const QColor &newColor ) {
750 if ( newColor.isValid() )
751 {
752 setColor( newColor );
753 }
754 } );
755 panel->openPanel( colorWidget );
756 return;
757 }
758
759 QgsColorDialog dialog( this, Qt::WindowFlags(), mSymbol->color() );
760 dialog.setTitle( tr( "Symbol Color" ) );
761 dialog.setAllowOpacity( true );
762
763 if ( dialog.exec() && dialog.color().isValid() )
764 {
765 setColor( dialog.color() );
766 }
767
768 // reactivate button's window
769 activateWindow();
770}
771
772void QgsSymbolButton::setDialogTitle( const QString &title )
773{
774 mDialogTitle = title;
775}
776
778{
779 return mDialogTitle;
780}
781
783{
784 return mSymbol.get();
785}
786
788{
789 mShowNull = showNull;
790}
791
793{
794 return mShowNull;
795}
796
798{
799 mFixedSizeConstraints = fixedSizeConstraints;
800
801 updateSizeHint();
802}
803
805{
806 return mFixedSizeConstraints;
807}
808
810{
811 return !mSymbol;
812}
813
815{
816 setSymbol( nullptr );
817}
818
820{
821 if ( !mDefaultSymbol )
822 {
823 return;
824 }
825
826 setSymbol( mDefaultSymbol->clone() );
827}
@ Point
Points.
Definition qgis.h:400
@ Line
Lines.
Definition qgis.h:401
@ Polygon
Polygons.
Definition qgis.h:402
SymbolType
Symbol types.
Definition qgis.h:671
@ Marker
Marker symbol.
Definition qgis.h:672
@ Line
Line symbol.
Definition qgis.h:673
@ Fill
Fill symbol.
Definition qgis.h:674
@ Hybrid
Hybrid symbol.
Definition qgis.h:675
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition qgis.h:7405
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application's color scheme registry, used for managing color schemes.
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.
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.
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.
@ 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.
Abstract interface for generating an expression context.
virtual QgsExpressionContext createExpressionContext() const =0
This method needs to be reimplemented in all classes which implement this interface and return an exp...
static std::vector< std::unique_ptr< QgsExpressionContextScope > > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void appendScopes(const QList< QgsExpressionContextScope * > &scopes)
Appends a list of scopes to the end of the context.
static std::unique_ptr< QgsFillSymbol > createSimple(const QVariantMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties.
A line symbol type, for rendering LineString and MultiLineString geometries.
void setWidth(double width) const
Sets the width for the whole line symbol.
static std::unique_ptr< QgsLineSymbol > createSimple(const QVariantMap &properties)
Create a line symbol with one symbol layer: SimpleLine with specified properties.
double width() const
Returns the estimated width for the whole symbol, which is the maximum width of all marker symbol lay...
Map canvas is a class for displaying all GIS data types on a canvas.
A marker symbol type, for rendering Point and MultiPoint geometries.
void setSize(double size) const
Sets the size for the whole symbol.
static std::unique_ptr< QgsMarkerSymbol > createSimple(const QVariantMap &properties)
Create a marker symbol with one symbol layer: SimpleMarker with specified properties.
double size() const
Returns the estimated size for the whole symbol, which is the maximum size of all marker symbol layer...
A bar for displaying non-blocking messages to the user.
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.
void changed()
Emitted when the widget state changes.
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.
static void addRecentColor(const QColor &color)
Adds a color to the list of recent colors.
Stores properties relating to a screen.
static QgsStyle * defaultStyle(bool initialize=true)
Returns the default application-wide style.
Definition qgsstyle.cpp:164
void wheelEvent(QWheelEvent *event) override
void setFixedSizeConstraints(bool fixedSizeConstraints)
Sets whether the widget adopts fixed size constraints.
QSize sizeHint() const override
void changeEvent(QEvent *e) override
void setColor(const QColor &color)
Sets the current color for the symbol.
void copySymbol()
Copies the current symbol to the clipboard.
QgsSymbolButton(QWidget *parent=nullptr, const QString &dialogTitle=QString())
Construct a new symbol button.
void copyColor()
Copies the current symbol color to the clipboard.
void setDefaultSymbol(QgsSymbol *symbol)
Sets the default symbol for the button, which is shown in the button's drop-down menu for the "defaul...
void setMessageBar(QgsMessageBar *bar)
Sets the message bar associated with the widget.
~QgsSymbolButton() override
void pasteSymbol()
Pastes a symbol from the clipboard.
void setSymbol(QgsSymbol *symbol)
Sets the symbol for the button.
void mousePressEvent(QMouseEvent *e) override
void dragEnterEvent(QDragEnterEvent *e) override
void showEvent(QShowEvent *e) override
void setSymbolType(Qgis::SymbolType type)
Sets the symbol type which the button requires.
void setDialogTitle(const QString &title)
Sets the title for the symbol settings dialog window.
void setLayer(QgsVectorLayer *layer)
Sets a layer to associate with the widget.
void resizeEvent(QResizeEvent *event) override
void registerExpressionContextGenerator(QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
QSize minimumSizeHint() const override
void contextMenuEvent(QContextMenuEvent *e) override
QgsMessageBar * messageBar() const
Returns the message bar associated with the widget.
void setMapCanvas(QgsMapCanvas *canvas)
Sets a map canvas to associate with the widget.
void dropEvent(QDropEvent *e) override
void pasteColor()
Pastes a color from the clipboard to the symbol.
void changed()
Emitted when the symbol's settings are changed.
void setToDefaultSymbol()
Sets symbol to the button's default symbol, if set.
void dragLeaveEvent(QDragLeaveEvent *e) override
bool fixedSizeConstraints() const
Returns true if the widget adopts fixed size constraints.
void setToNull()
Sets symbol to to null.
void setShowNull(bool showNull)
Sets whether a set to null (clear) option is shown in the button's drop-down menu.
QgsVectorLayer * layer() const
Returns the layer associated with the widget.
QgsSymbol * symbol()
Returns the current symbol defined by the button.
const QgsSymbol * defaultSymbol() const
Returns the default symbol for the button, which is shown in the button's drop-down menu for the "def...
QgsMapCanvas * mapCanvas() const
Returns the map canvas associated with the widget.
void mouseMoveEvent(QMouseEvent *e) override
bool isNull() const
Returns true if the current symbol is null.
bool showNull() const
Returns whether the set to null (clear) option is shown in the button's drop-down menu.
static std::unique_ptr< QgsSymbol > symbolFromMimeData(const QMimeData *data)
Attempts to parse mime data as a symbol.
static QPixmap symbolPreviewPixmap(const QgsSymbol *symbol, QSize size, int padding=0, QgsRenderContext *customContext=nullptr, bool selected=false, const QgsExpressionContext *expressionContext=nullptr, const QgsLegendPatchShape *shape=nullptr, const QgsScreenProperties &screen=QgsScreenProperties())
Returns a pixmap preview for a color ramp.
static std::unique_ptr< QMimeData > symbolToMimeData(const QgsSymbol *symbol)
Creates new mime data from a symbol.
static QColor colorFromMimeData(const QMimeData *data, bool &hasAlpha)
Attempts to parse mime data as a color.
static QIcon symbolPreviewIcon(const QgsSymbol *symbol, QSize size, int padding=0, QgsLegendPatchShape *shape=nullptr, const QgsScreenProperties &screen=QgsScreenProperties())
Returns an icon preview for a color ramp.
static std::unique_ptr< QMimeData > colorToMimeData(const QColor &color)
Creates mime data from a color.
Symbol selector widget that can be used to select and build a symbol.
void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the symbol widget is shown, e.g., the associated map canvas and expression ...
QgsSymbol * symbol()
Returns the symbol that is currently active in the widget.
static QgsSymbolSelectorWidget * createWidgetWithSymbolOwnership(std::unique_ptr< QgsSymbol > symbol, QgsStyle *style, QgsVectorLayer *vl, QWidget *parent=nullptr)
Creates a QgsSymbolSelectorWidget which takes ownership of a symbol and maintains the ownership for t...
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
void setMessageBar(QgsMessageBar *bar)
Sets the message bar associated with the widget.
void setExpressionContext(QgsExpressionContext *context)
Sets the optional expression context used for the widget.
Abstract base class for all rendered symbols.
Definition qgssymbol.h:227
virtual QgsSymbol * clone() const =0
Returns a deep copy of this symbol.
static std::unique_ptr< QgsSymbol > defaultSymbol(Qgis::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
Represents a vector layer which manages a vector based dataset.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...