QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 "qgssymbollayerutils.h"
20 #include "qgshelp.h"
21 
22 #include <QAbstractButton>
23 #include <QDialogButtonBox>
24 
25 #if 0 // unused
26 static void updateColorButton( QAbstractButton *button, QColor color )
27 {
28  QPixmap p( 20, 20 );
29  p.fill( color );
30  button->setIcon( QIcon( p ) );
31 }
32 #endif
33 
35 
36 
38  : QgsPanelWidget( parent )
39  , mRamp( ramp )
40 {
41 
42  setupUi( this );
43 
44  QSize iconSize( 50, 16 );
45  cboSchemeName->setIconSize( iconSize );
46 
47  QStringList schemes = QgsColorBrewerColorRamp::listSchemeNames();
48  Q_FOREACH ( const QString &schemeName, schemes )
49  {
50  // create a preview icon using five color variant
51  QgsColorBrewerColorRamp *r = new QgsColorBrewerColorRamp( schemeName, 5 );
52  QIcon icon = QgsSymbolLayerUtils::colorRampPreviewIcon( r, iconSize );
53  delete r;
54  cboSchemeName->addItem( icon, schemeName );
55  }
56 
57  updateUi();
58  connect( cboSchemeName, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsColorBrewerColorRampWidget::setSchemeName );
59  connect( cboColors, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsColorBrewerColorRampWidget::setColors );
60 }
61 
63 {
64  mRamp = ramp;
65  updateUi();
66  emit changed();
67 }
68 
69 void QgsColorBrewerColorRampWidget::populateVariants()
70 {
71  QString oldVariant = cboColors->currentText();
72 
73  cboColors->clear();
74  QString schemeName = cboSchemeName->currentText();
75  QList<int> variants = QgsColorBrewerColorRamp::listSchemeVariants( schemeName );
76  Q_FOREACH ( int variant, variants )
77  {
78  cboColors->addItem( QString::number( variant ) );
79  }
80 
81  // try to set the original variant again (if exists)
82  int idx = cboColors->findText( oldVariant );
83  if ( idx == -1 ) // not found?
84  {
85  // use the last item
86  idx = cboColors->count() - 1;
87  }
88  cboColors->setCurrentIndex( idx );
89 }
90 
91 void QgsColorBrewerColorRampWidget::updatePreview()
92 {
93  QSize size( 300, 40 );
94  lblPreview->setPixmap( QgsSymbolLayerUtils::colorRampPreviewPixmap( &mRamp, size ) );
95 }
96 
97 void QgsColorBrewerColorRampWidget::updateUi()
98 {
99  whileBlocking( cboSchemeName )->setCurrentIndex( cboSchemeName->findText( mRamp.schemeName() ) );
100  populateVariants();
101  whileBlocking( cboColors )->setCurrentIndex( cboColors->findText( QString::number( mRamp.colors() ) ) );
102  updatePreview();
103 }
104 
105 void QgsColorBrewerColorRampWidget::setSchemeName()
106 {
107  // populate list of variants
108  populateVariants();
109 
110  mRamp.setSchemeName( cboSchemeName->currentText() );
111  updatePreview();
112  emit changed();
113 }
114 
115 void QgsColorBrewerColorRampWidget::setColors()
116 {
117  int num = cboColors->currentText().toInt();
118  mRamp.setColors( num );
119  updatePreview();
120  emit changed();
121 }
122 
124  : QDialog( parent )
125 {
126  QVBoxLayout *vLayout = new QVBoxLayout();
127  mWidget = new QgsColorBrewerColorRampWidget( ramp );
128  vLayout->addWidget( mWidget );
129  QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok, Qt::Horizontal );
130  connect( bbox, &QDialogButtonBox::accepted, this, &QDialog::accept );
131  connect( bbox, &QDialogButtonBox::rejected, this, &QDialog::reject );
132  connect( bbox, &QDialogButtonBox::helpRequested, this, &QgsColorBrewerColorRampDialog::showHelp );
133  vLayout->addWidget( bbox );
134  setLayout( vLayout );
135  setWindowTitle( tr( "ColorBrewer Ramp" ) );
137 }
138 
139 void QgsColorBrewerColorRampDialog::showHelp()
140 {
141  QgsHelp::openHelp( QStringLiteral( "working_with_vector/style_library.html#color-ramp" ) );
142 }
void changed()
Emitted when the dialog settings change.
void setRamp(const QgsColorBrewerColorRamp &ramp)
Sets the color ramp to show in the dialog.
static QList< int > listSchemeVariants(const QString &schemeName)
Returns a list of the valid variants (numbers of colors) for a specified color brewer scheme name...
int colors() const
Returns the number of colors in the ramp.
Definition: qgscolorramp.h:575
Base class for any widget that can be shown as a inline panel.
void changed()
Emitted when the dialog settings change.
QgsColorBrewerColorRampDialog(const QgsColorBrewerColorRamp &ramp, QWidget *parent=nullptr)
Constructor for QgsColorBrewerColorRampDialog.
static QIcon colorRampPreviewIcon(QgsColorRamp *ramp, QSize size, int padding=0)
Returns an icon preview for a color ramp.
void setSchemeName(const QString &schemeName)
Sets the name of the color brewer color scheme.
Definition: qgscolorramp.h:583
static QPixmap colorRampPreviewPixmap(QgsColorRamp *ramp, QSize size, int padding=0)
Returns a pixmap preview for a color ramp.
A widget which allows users to modify the properties of a QgsColorBrewerColorRamp.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:225
QgsColorBrewerColorRampWidget(const QgsColorBrewerColorRamp &ramp, QWidget *parent=nullptr)
Constructor for QgsColorBrewerColorRampWidget.
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:35
Color ramp utilising "Color Brewer" preset color schemes.
Definition: qgscolorramp.h:535
QgsColorBrewerColorRamp ramp() const
Returns a color ramp representing the current settings from the dialog.
void setColors(int colors)
Sets the number of colors in the ramp.
Definition: qgscolorramp.h:591
QString schemeName() const
Returns the name of the color brewer color scheme.
Definition: qgscolorramp.h:569