QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgscolorbrewercolorrampdialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscolorbrewercolorrampdialog.cpp
3 ---------------------
4 begin : November 2009
5 copyright : (C) 2009 by Martin Dobias
6 email : wonder dot sk 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
17#include "moc_qgscolorbrewercolorrampdialog.cpp"
18
19#include "qgscolorramp.h"
20#include "qgssymbollayerutils.h"
21#include "qgshelp.h"
22
23#include <QAbstractButton>
24#include <QDialogButtonBox>
25
26#if 0 // unused
27static void updateColorButton( QAbstractButton *button, QColor color )
28{
29 QPixmap p( 20, 20 );
30 p.fill( color );
31 button->setIcon( QIcon( p ) );
32}
33#endif
34
36
37
39 : QgsPanelWidget( parent )
40 , mRamp( ramp )
41{
42
43 setupUi( this );
44
45 const QSize iconSize( 50, 16 );
46 cboSchemeName->setIconSize( iconSize );
47
48 const QStringList schemes = QgsColorBrewerColorRamp::listSchemeNames();
49 const auto constSchemes = schemes;
50 for ( const QString &schemeName : constSchemes )
51 {
52 // create a preview icon using five color variant
53 QgsColorBrewerColorRamp *r = new QgsColorBrewerColorRamp( schemeName, 5 );
54 const QIcon icon = QgsSymbolLayerUtils::colorRampPreviewIcon( r, iconSize );
55 delete r;
56 cboSchemeName->addItem( icon, schemeName );
57 }
58
59 updateUi();
60 connect( cboSchemeName, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsColorBrewerColorRampWidget::setSchemeName );
61 connect( cboColors, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsColorBrewerColorRampWidget::setColors );
62}
63
65{
66 mRamp = ramp;
67 updateUi();
68 emit changed();
69}
70
71void QgsColorBrewerColorRampWidget::populateVariants()
72{
73 const QString oldVariant = cboColors->currentText();
74
75 cboColors->clear();
76 const QString schemeName = cboSchemeName->currentText();
77 const QList<int> variants = QgsColorBrewerColorRamp::listSchemeVariants( schemeName );
78 const auto constVariants = variants;
79 for ( const int variant : constVariants )
80 {
81 cboColors->addItem( QString::number( variant ) );
82 }
83
84 // try to set the original variant again (if exists)
85 int idx = cboColors->findText( oldVariant );
86 if ( idx == -1 ) // not found?
87 {
88 // use the last item
89 idx = cboColors->count() - 1;
90 }
91 cboColors->setCurrentIndex( idx );
92}
93
94void QgsColorBrewerColorRampWidget::updatePreview()
95{
96 const QSize size( 300, 40 );
97 lblPreview->setPixmap( QgsSymbolLayerUtils::colorRampPreviewPixmap( &mRamp, size ) );
98}
99
100void QgsColorBrewerColorRampWidget::updateUi()
101{
102 whileBlocking( cboSchemeName )->setCurrentIndex( cboSchemeName->findText( mRamp.schemeName() ) );
103 populateVariants();
104 whileBlocking( cboColors )->setCurrentIndex( cboColors->findText( QString::number( mRamp.colors() ) ) );
105 updatePreview();
106}
107
108void QgsColorBrewerColorRampWidget::setSchemeName()
109{
110 // populate list of variants
111 populateVariants();
112
113 mRamp.setSchemeName( cboSchemeName->currentText() );
114 updatePreview();
115 emit changed();
116}
117
118void QgsColorBrewerColorRampWidget::setColors()
119{
120 const int num = cboColors->currentText().toInt();
121 mRamp.setColors( num );
122 updatePreview();
123 emit changed();
124}
125
127 : QDialog( parent )
128{
129 QVBoxLayout *vLayout = new QVBoxLayout();
130 mWidget = new QgsColorBrewerColorRampWidget( ramp );
131 connect( mWidget, &QgsPanelWidget::panelAccepted, this, &QDialog::reject );
132 vLayout->addWidget( mWidget );
133 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok, Qt::Horizontal );
134 connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
135 connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
136 connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsColorBrewerColorRampDialog::showHelp );
137 vLayout->addWidget( mButtonBox );
138 setLayout( vLayout );
139 setWindowTitle( tr( "ColorBrewer Ramp" ) );
141}
142
144{
145 return mButtonBox;
146}
147
148void QgsColorBrewerColorRampDialog::showHelp()
149{
150 QgsHelp::openHelp( QStringLiteral( "style_library/style_manager.html#setting-a-color-ramp" ) );
151}
QgsColorBrewerColorRampDialog(const QgsColorBrewerColorRamp &ramp, QWidget *parent=nullptr)
Constructor for QgsColorBrewerColorRampDialog.
void changed()
Emitted when the dialog settings change.
QDialogButtonBox * buttonBox() const
Returns a reference to the dialog's button box.
A widget which allows users to modify the properties of a QgsColorBrewerColorRamp.
void setRamp(const QgsColorBrewerColorRamp &ramp)
Sets the color ramp to show in the dialog.
void changed()
Emitted when the dialog settings change.
QgsColorBrewerColorRampWidget(const QgsColorBrewerColorRamp &ramp, QWidget *parent=nullptr)
Constructor for QgsColorBrewerColorRampWidget.
Color ramp utilising "Color Brewer" preset color schemes.
static QList< int > listSchemeVariants(const QString &schemeName)
Returns a list of the valid variants (numbers of colors) for a specified color brewer scheme name.
static QStringList listSchemeNames()
Returns a list of all valid color brewer scheme names.
QString schemeName() const
Returns the name of the color brewer color scheme.
int colors() const
Returns the number of colors in the ramp.
void setSchemeName(const QString &schemeName)
Sets the name of the color brewer color scheme.
void setColors(int colors)
Sets the number of colors in the ramp.
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:39
Base class for any widget that can be shown as a inline panel.
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
static QIcon colorRampPreviewIcon(QgsColorRamp *ramp, QSize size, int padding=0)
Returns an icon preview for a color ramp.
static QPixmap colorRampPreviewPixmap(QgsColorRamp *ramp, QSize size, int padding=0, Qt::Orientation direction=Qt::Horizontal, bool flipDirection=false, bool drawTransparentBackground=true)
Returns a pixmap preview for a color ramp.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:5821