QGIS API Documentation 3.99.0-Master (d270888f95f)
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
18#include "qgscolorramp.h"
19#include "qgshelp.h"
20#include "qgssymbollayerutils.h"
21
22#include <QAbstractButton>
23#include <QDialogButtonBox>
24#include <QString>
25
26#include "moc_qgscolorbrewercolorrampdialog.cpp"
27
28using namespace Qt::StringLiterals;
29
30#if 0 // unused
31static void updateColorButton( QAbstractButton *button, QColor color )
32{
33 QPixmap p( 20, 20 );
34 p.fill( color );
35 button->setIcon( QIcon( p ) );
36}
37#endif
38
40
41
43 : QgsPanelWidget( parent )
44 , mRamp( ramp )
45{
46 setupUi( this );
47
48 const QSize iconSize( 50, 16 );
49 cboSchemeName->setIconSize( iconSize );
50
51 const QStringList schemes = QgsColorBrewerColorRamp::listSchemeNames();
52 const auto constSchemes = schemes;
53 for ( const QString &schemeName : constSchemes )
54 {
55 // create a preview icon using five color variant
56 QgsColorBrewerColorRamp *r = new QgsColorBrewerColorRamp( schemeName, 5 );
57 const QIcon icon = QgsSymbolLayerUtils::colorRampPreviewIcon( r, iconSize );
58 delete r;
59 cboSchemeName->addItem( icon, schemeName );
60 }
61
62 updateUi();
63 connect( cboSchemeName, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsColorBrewerColorRampWidget::setSchemeName );
64 connect( cboColors, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsColorBrewerColorRampWidget::setColors );
65}
66
68{
69 mRamp = ramp;
70 updateUi();
71 emit changed();
72}
73
74void QgsColorBrewerColorRampWidget::populateVariants()
75{
76 const QString oldVariant = cboColors->currentText();
77
78 cboColors->clear();
79 const QString schemeName = cboSchemeName->currentText();
80 const QList<int> variants = QgsColorBrewerColorRamp::listSchemeVariants( schemeName );
81 const auto constVariants = variants;
82 for ( const int variant : constVariants )
83 {
84 cboColors->addItem( QString::number( variant ) );
85 }
86
87 // try to set the original variant again (if exists)
88 int idx = cboColors->findText( oldVariant );
89 if ( idx == -1 ) // not found?
90 {
91 // use the last item
92 idx = cboColors->count() - 1;
93 }
94 cboColors->setCurrentIndex( idx );
95}
96
97void QgsColorBrewerColorRampWidget::updatePreview()
98{
99 const QSize size( 300, 40 );
100 lblPreview->setPixmap( QgsSymbolLayerUtils::colorRampPreviewPixmap( &mRamp, size ) );
101}
102
103void QgsColorBrewerColorRampWidget::updateUi()
104{
105 whileBlocking( cboSchemeName )->setCurrentIndex( cboSchemeName->findText( mRamp.schemeName() ) );
106 populateVariants();
107 whileBlocking( cboColors )->setCurrentIndex( cboColors->findText( QString::number( mRamp.colors() ) ) );
108 updatePreview();
109}
110
111void QgsColorBrewerColorRampWidget::setSchemeName()
112{
113 // populate list of variants
114 populateVariants();
115
116 mRamp.setSchemeName( cboSchemeName->currentText() );
117 updatePreview();
118 emit changed();
119}
120
121void QgsColorBrewerColorRampWidget::setColors()
122{
123 const int num = cboColors->currentText().toInt();
124 mRamp.setColors( num );
125 updatePreview();
126 emit changed();
127}
128
130 : QDialog( parent )
131{
132 QVBoxLayout *vLayout = new QVBoxLayout();
133 mWidget = new QgsColorBrewerColorRampWidget( ramp );
134 connect( mWidget, &QgsPanelWidget::panelAccepted, this, &QDialog::reject );
135 vLayout->addWidget( mWidget );
136 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok, Qt::Horizontal );
137 connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
138 connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
139 connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsColorBrewerColorRampDialog::showHelp );
140 vLayout->addWidget( mButtonBox );
141 setLayout( vLayout );
142 setWindowTitle( tr( "ColorBrewer Ramp" ) );
144}
145
147{
148 return mButtonBox;
149}
150
151void QgsColorBrewerColorRampDialog::showHelp()
152{
153 QgsHelp::openHelp( u"style_library/style_manager.html#setting-a-color-ramp"_s );
154}
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.
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:41
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
QgsPanelWidget(QWidget *parent=nullptr)
Base class for any widget that can be shown as an inline panel.
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:6804