QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 );
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  connect( mWidget, &QgsPanelWidget::panelAccepted, this, &QDialog::reject );
131  vLayout->addWidget( mWidget );
132  mButtonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok, Qt::Horizontal );
133  connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
134  connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
135  connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsColorBrewerColorRampDialog::showHelp );
136  vLayout->addWidget( mButtonBox );
137  setLayout( vLayout );
138  setWindowTitle( tr( "ColorBrewer Ramp" ) );
140 }
141 
143 {
144  return mButtonBox;
145 }
146 
147 void QgsColorBrewerColorRampDialog::showHelp()
148 {
149  QgsHelp::openHelp( QStringLiteral( "style_library/style_manager.html#setting-a-color-ramp" ) );
150 }
QgsColorBrewerColorRamp::listSchemeVariants
static QList< int > listSchemeVariants(const QString &schemeName)
Returns a list of the valid variants (numbers of colors) for a specified color brewer scheme name.
Definition: qgscolorramp.cpp:551
QgsColorBrewerColorRampDialog::ramp
QgsColorBrewerColorRamp ramp
Definition: qgscolorbrewercolorrampdialog.h:89
QgsColorBrewerColorRampDialog::changed
void changed()
Emitted when the dialog settings change.
qgssymbollayerutils.h
QgsColorBrewerColorRampWidget::setRamp
void setRamp(const QgsColorBrewerColorRamp &ramp)
Sets the color ramp to show in the dialog.
Definition: qgscolorbrewercolorrampdialog.cpp:63
QgsColorBrewerColorRamp
Color ramp utilising "Color Brewer" preset color schemes.
Definition: qgscolorramp.h:578
QgsSymbolLayerUtils::colorRampPreviewPixmap
static QPixmap colorRampPreviewPixmap(QgsColorRamp *ramp, QSize size, int padding=0)
Returns a pixmap preview for a color ramp.
Definition: qgssymbollayerutils.cpp:877
QgsGuiUtils::iconSize
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
Definition: qgsguiutils.cpp:250
QgsColorBrewerColorRampDialog::buttonBox
QDialogButtonBox * buttonBox() const
Returns a reference to the dialog's button box.
Definition: qgscolorbrewercolorrampdialog.cpp:142
QgsColorBrewerColorRampWidget
A widget which allows users to modify the properties of a QgsColorBrewerColorRamp.
Definition: qgscolorbrewercolorrampdialog.h:36
qgscolorbrewercolorrampdialog.h
QgsColorBrewerColorRampWidget::changed
void changed()
Emitted when the dialog settings change.
QgsColorBrewerColorRampDialog::QgsColorBrewerColorRampDialog
QgsColorBrewerColorRampDialog(const QgsColorBrewerColorRamp &ramp, QWidget *parent=nullptr)
Constructor for QgsColorBrewerColorRampDialog.
Definition: qgscolorbrewercolorrampdialog.cpp:125
QgsPanelWidget
Base class for any widget that can be shown as a inline panel.
Definition: qgspanelwidget.h:30
whileBlocking
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:262
QgsColorBrewerColorRamp::colors
int colors() const
Returns the number of colors in the ramp.
Definition: qgscolorramp.h:625
qgscolorramp.h
QgsPanelWidget::panelAccepted
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
QgsColorBrewerColorRamp::setSchemeName
void setSchemeName(const QString &schemeName)
Sets the name of the color brewer color scheme.
Definition: qgscolorramp.h:633
QgsColorBrewerColorRamp::schemeName
QString schemeName() const
Returns the name of the color brewer color scheme.
Definition: qgscolorramp.h:619
QgsColorBrewerColorRampWidget::ramp
QgsColorBrewerColorRamp ramp
Definition: qgscolorbrewercolorrampdialog.h:38
QgsColorBrewerColorRamp::listSchemeNames
static QStringList listSchemeNames()
Returns a list of all valid color brewer scheme names.
Definition: qgscolorramp.cpp:546
QgsHelp::openHelp
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:36
QgsColorBrewerColorRamp::setColors
void setColors(int colors)
Sets the number of colors in the ramp.
Definition: qgscolorramp.h:641
QgsColorBrewerColorRampWidget::QgsColorBrewerColorRampWidget
QgsColorBrewerColorRampWidget(const QgsColorBrewerColorRamp &ramp, QWidget *parent=nullptr)
Constructor for QgsColorBrewerColorRampWidget.
Definition: qgscolorbrewercolorrampdialog.cpp:37
qgshelp.h
QgsSymbolLayerUtils::colorRampPreviewIcon
static QIcon colorRampPreviewIcon(QgsColorRamp *ramp, QSize size, int padding=0)
Returns an icon preview for a color ramp.
Definition: qgssymbollayerutils.cpp:872