QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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  const auto constSchemes = schemes;
49  for ( const QString &schemeName : constSchemes )
50  {
51  // create a preview icon using five color variant
52  QgsColorBrewerColorRamp *r = new QgsColorBrewerColorRamp( schemeName, 5 );
53  QIcon icon = QgsSymbolLayerUtils::colorRampPreviewIcon( r, iconSize );
54  delete r;
55  cboSchemeName->addItem( icon, schemeName );
56  }
57 
58  updateUi();
59  connect( cboSchemeName, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsColorBrewerColorRampWidget::setSchemeName );
60  connect( cboColors, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsColorBrewerColorRampWidget::setColors );
61 }
62 
64 {
65  mRamp = ramp;
66  updateUi();
67  emit changed();
68 }
69 
70 void QgsColorBrewerColorRampWidget::populateVariants()
71 {
72  QString oldVariant = cboColors->currentText();
73 
74  cboColors->clear();
75  QString schemeName = cboSchemeName->currentText();
76  QList<int> variants = QgsColorBrewerColorRamp::listSchemeVariants( schemeName );
77  const auto constVariants = variants;
78  for ( int variant : constVariants )
79  {
80  cboColors->addItem( QString::number( variant ) );
81  }
82 
83  // try to set the original variant again (if exists)
84  int idx = cboColors->findText( oldVariant );
85  if ( idx == -1 ) // not found?
86  {
87  // use the last item
88  idx = cboColors->count() - 1;
89  }
90  cboColors->setCurrentIndex( idx );
91 }
92 
93 void QgsColorBrewerColorRampWidget::updatePreview()
94 {
95  QSize size( 300, 40 );
96  lblPreview->setPixmap( QgsSymbolLayerUtils::colorRampPreviewPixmap( &mRamp, size ) );
97 }
98 
99 void QgsColorBrewerColorRampWidget::updateUi()
100 {
101  whileBlocking( cboSchemeName )->setCurrentIndex( cboSchemeName->findText( mRamp.schemeName() ) );
102  populateVariants();
103  whileBlocking( cboColors )->setCurrentIndex( cboColors->findText( QString::number( mRamp.colors() ) ) );
104  updatePreview();
105 }
106 
107 void QgsColorBrewerColorRampWidget::setSchemeName()
108 {
109  // populate list of variants
110  populateVariants();
111 
112  mRamp.setSchemeName( cboSchemeName->currentText() );
113  updatePreview();
114  emit changed();
115 }
116 
117 void QgsColorBrewerColorRampWidget::setColors()
118 {
119  int num = cboColors->currentText().toInt();
120  mRamp.setColors( num );
121  updatePreview();
122  emit changed();
123 }
124 
126  : QDialog( parent )
127 {
128  QVBoxLayout *vLayout = new QVBoxLayout();
129  mWidget = new QgsColorBrewerColorRampWidget( ramp );
130  vLayout->addWidget( mWidget );
131  mButtonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok, Qt::Horizontal );
132  connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
133  connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
134  connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsColorBrewerColorRampDialog::showHelp );
135  vLayout->addWidget( mButtonBox );
136  setLayout( vLayout );
137  setWindowTitle( tr( "ColorBrewer Ramp" ) );
139 }
140 
142 {
143  return mButtonBox;
144 }
145 
146 void QgsColorBrewerColorRampDialog::showHelp()
147 {
148  QgsHelp::openHelp( QStringLiteral( "working_with_vector/style_library.html#color-ramp" ) );
149 }
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...
Base class for any widget that can be shown as a inline panel.
QDialogButtonBox * buttonBox() const
Returns a reference to the dialog&#39;s button box.
QgsColorBrewerColorRamp ramp() const
Returns a color ramp representing the current settings from the dialog.
int colors() const
Returns the number of colors in the ramp.
Definition: qgscolorramp.h:575
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
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window&#39;s toolbar icons.
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.
QString schemeName() const
Returns the name of the color brewer color scheme.
Definition: qgscolorramp.h:569
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:227
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:36
Color ramp utilising "Color Brewer" preset color schemes.
Definition: qgscolorramp.h:535
void setColors(int colors)
Sets the number of colors in the ramp.
Definition: qgscolorramp.h:591