QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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  const QSize iconSize( 50, 16 );
45  cboSchemeName->setIconSize( iconSize );
46 
47  const 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  const 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  const QString oldVariant = cboColors->currentText();
73 
74  cboColors->clear();
75  const QString schemeName = cboSchemeName->currentText();
76  const QList<int> variants = QgsColorBrewerColorRamp::listSchemeVariants( schemeName );
77  const auto constVariants = variants;
78  for ( const 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  const 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  const 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 }
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:36
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.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:1517