QGIS API Documentation  2.99.0-Master (37c43df)
qgscolorrampcombobox.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscolorrampcombobox.cpp
3  ---------------------
4  begin : October 2010
5  copyright : (C) 2010 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 #include "qgscolorrampcombobox.h"
16 
17 #include "qgssymbollayerutils.h"
18 #include "qgscolorramp.h"
19 #include "qgsstyle.h"
20 #include "qgsstylemanagerdialog.h"
21 
27 
29 
31  : QComboBox( parent )
32  , mStyle( nullptr )
33  , mSourceColorRamp( nullptr )
34  , mShowGradientOnly( false )
35 {
36 }
37 
39 {
40  delete mSourceColorRamp;
41 }
42 
44 {
45  if ( count() != 0 )
46  return; // already populated!
47 
48  mStyle = style;
49 
50  setIconSize( rampIconSize );
51 
52  QStringList rampNames = mStyle->colorRampNames();
53  for ( QStringList::iterator it = rampNames.begin(); it != rampNames.end(); ++it )
54  {
55  QScopedPointer< QgsColorRamp > ramp( style->colorRamp( *it ) );
56 
57  if ( !mShowGradientOnly || ramp->type() == QLatin1String( "gradient" ) )
58  {
59  QIcon icon = QgsSymbolLayerUtils::colorRampPreviewIcon( ramp.data(), rampIconSize );
60 
61  addItem( icon, *it );
62  }
63  }
64 
65  if ( !mShowGradientOnly )
66  addItem( tr( "Random colors" ) );
67  addItem( tr( "New color ramp..." ) );
68  connect( this, SIGNAL( activated( int ) ), SLOT( colorRampChanged( int ) ) );
69 }
70 
72 {
73  QString rampName = currentText();
74 
75  if ( rampName == tr( "Random colors" ) )
76  {
77  return new QgsRandomColorRamp();
78  }
79  else if ( rampName == QLatin1String( "[source]" ) && mSourceColorRamp )
80  return mSourceColorRamp->clone();
81  else
82  return mStyle->colorRamp( rampName );
83 }
84 
86 {
87  int index = currentIndex();
88  return index == count() - 1; //create new ramp is last item in combobox
89 }
90 
92 {
93  delete mSourceColorRamp;
94  mSourceColorRamp = sourceRamp->clone();
95 
97  if ( itemText( 0 ) == QLatin1String( "[source]" ) )
98  setItemIcon( 0, icon );
99  else
100  insertItem( 0, icon, QStringLiteral( "[source]" ) );
101  setCurrentIndex( 0 );
102 }
103 
105 {
106  if ( index != count() - 1 )
107  return;
108 
109  // last item: "new color ramp..."
110  QString rampName;
111  if ( !mShowGradientOnly )
112  {
114  }
115  else
116  {
117  rampName = QgsStyleManagerDialog::addColorRampStatic( this, mStyle, QStringLiteral( "Gradient" ) );
118  }
119  if ( rampName.isEmpty() )
120  return;
121 
122  // put newly added ramp into the combo
123  QScopedPointer< QgsColorRamp > ramp( mStyle->colorRamp( rampName ) );
124  QIcon icon = QgsSymbolLayerUtils::colorRampPreviewIcon( ramp.data(), rampIconSize );
125 
126  blockSignals( true ); // avoid calling this method again!
127  insertItem( index, icon, rampName );
128  blockSignals( false );
129 
130  // ... and set it as active
131  setCurrentIndex( index );
132 
133  // make sure the color ramp is stored
134  mStyle->save();
135 }
136 
138 {
140  bool panelMode = panel && panel->dockMode();
141 
142  QScopedPointer< QgsColorRamp > currentRamp( currentColorRamp() );
143  if ( !currentRamp )
144  return;
145 
146  if ( currentRamp->type() == QLatin1String( "gradient" ) )
147  {
148  QgsGradientColorRamp* gradRamp = static_cast<QgsGradientColorRamp*>( currentRamp.data() );
149  QgsGradientColorRampDialog dlg( *gradRamp, this );
150  if ( dlg.exec() )
151  {
152  setSourceColorRamp( dlg.ramp().clone() );
153  emit sourceRampEdited();
154  }
155  }
156  else if ( currentRamp->type() == QLatin1String( "random" ) )
157  {
158  QgsLimitedRandomColorRamp* randRamp = static_cast<QgsLimitedRandomColorRamp*>( currentRamp.data() );
159  if ( panelMode )
160  {
161  QgsLimitedRandomColorRampWidget* widget = new QgsLimitedRandomColorRampWidget( *randRamp, this );
162  widget->setPanelTitle( tr( "Edit ramp" ) );
163  connect( widget, SIGNAL( changed() ), this, SLOT( rampWidgetUpdated() ) );
164  panel->openPanel( widget );
165  }
166  else
167  {
168  QgsLimitedRandomColorRampDialog dlg( *randRamp, this );
169  if ( dlg.exec() )
170  {
171  setSourceColorRamp( dlg.ramp().clone() );
172  emit sourceRampEdited();
173  }
174  }
175  }
176  else if ( currentRamp->type() == QLatin1String( "preset" ) )
177  {
178  QgsPresetSchemeColorRamp* presetRamp = static_cast<QgsPresetSchemeColorRamp*>( currentRamp.data() );
179  if ( panelMode )
180  {
181  QgsPresetColorRampWidget* widget = new QgsPresetColorRampWidget( *presetRamp, this );
182  widget->setPanelTitle( tr( "Edit ramp" ) );
183  connect( widget, SIGNAL( changed() ), this, SLOT( rampWidgetUpdated() ) );
184  panel->openPanel( widget );
185  }
186  else
187  {
188  QgsPresetColorRampDialog dlg( *presetRamp, this );
189  if ( dlg.exec() )
190  {
191  setSourceColorRamp( dlg.ramp().clone() );
192  emit sourceRampEdited();
193  }
194  }
195  }
196  else if ( currentRamp->type() == QLatin1String( "colorbrewer" ) )
197  {
198  QgsColorBrewerColorRamp* brewerRamp = static_cast<QgsColorBrewerColorRamp*>( currentRamp.data() );
199  if ( panelMode )
200  {
201  QgsColorBrewerColorRampWidget* widget = new QgsColorBrewerColorRampWidget( *brewerRamp, this );
202  widget->setPanelTitle( tr( "Edit ramp" ) );
203  connect( widget, SIGNAL( changed() ), this, SLOT( rampWidgetUpdated() ) );
204  panel->openPanel( widget );
205  }
206  else
207  {
208  QgsColorBrewerColorRampDialog dlg( *brewerRamp, this );
209  if ( dlg.exec() )
210  {
211  setSourceColorRamp( dlg.ramp().clone() );
212  emit sourceRampEdited();
213  }
214  }
215  }
216  else if ( currentRamp->type() == QLatin1String( "cpt-city" ) )
217  {
218  QgsCptCityColorRamp* cptCityRamp = static_cast<QgsCptCityColorRamp*>( currentRamp.data() );
219  QgsCptCityColorRampDialog dlg( *cptCityRamp, this );
220  if ( dlg.exec() )
221  {
222  if ( dlg.saveAsGradientRamp() )
223  {
225  }
226  else
227  {
228  setSourceColorRamp( dlg.ramp().clone() );
229  }
230  emit sourceRampEdited();
231  }
232  }
233 }
234 
235 void QgsColorRampComboBox::rampWidgetUpdated()
236 {
237  QgsLimitedRandomColorRampWidget* limitedRampWidget = qobject_cast< QgsLimitedRandomColorRampWidget* >( sender() );
238  if ( limitedRampWidget )
239  {
240  setSourceColorRamp( limitedRampWidget->ramp().clone() );
241  emit sourceRampEdited();
242  return;
243  }
244  QgsColorBrewerColorRampWidget* colorBrewerRampWidget = qobject_cast< QgsColorBrewerColorRampWidget* >( sender() );
245  if ( colorBrewerRampWidget )
246  {
247  setSourceColorRamp( colorBrewerRampWidget->ramp().clone() );
248  emit sourceRampEdited();
249  return;
250  }
251  QgsPresetColorRampWidget* presetRampWidget = qobject_cast< QgsPresetColorRampWidget* >( sender() );
252  if ( presetRampWidget )
253  {
254  setSourceColorRamp( presetRampWidget->ramp().clone() );
255  emit sourceRampEdited();
256  return;
257  }
258 }
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
static unsigned index
QgsColorRampComboBox(QWidget *parent=nullptr)
bool dockMode()
Return the dock mode state.
bool save(QString filename=QString())
Saves style into a file (will use current filename if empty string is passed)
Definition: qgsstyle.cpp:423
A widget which allows users to modify the properties of a QgsLimitedRandomColorRamp.
QgsColorRamp * mSourceColorRamp
virtual QgsColorBrewerColorRamp * clone() const override
Creates a clone of the color ramp.
virtual QgsColorRamp * clone() const =0
Creates a clone of the color ramp.
A widget which allows users to modify the properties of a QgsPresetSchemeColorRamp.
Abstract base class for color ramps.
Definition: qgscolorramp.h:29
QgsPresetSchemeColorRamp ramp
Base class for any widget that can be shown as a inline panel.
A dialog which allows users to modify the properties of a QgsColorBrewerColorRamp.
QgsColorRamp * colorRamp(const QString &name) const
Returns a new copy of the specified color ramp.
Definition: qgsstyle.cpp:265
A dialog which allows users to modify the properties of a QgsGradientColorRamp.
A dialog which allows users to modify the properties of a QgsPresetSchemeColorRamp.
A dialog which allows users to modify the properties of a QgsLimitedRandomColorRamp.
QgsColorRamp * currentColorRamp() const
Returns a new instance of the current color ramp or NULL if there is no active color ramp...
virtual QgsGradientColorRamp * clone() const override
Creates a clone of the color ramp.
Constrained random color ramp, which returns random colors based on preset parameters.
Definition: qgscolorramp.h:236
static QgsPanelWidget * findParentPanel(QWidget *widget)
Traces through the parents of a widget to find if it is contained within a QgsPanelWidget widget...
virtual QgsPresetSchemeColorRamp * clone() const override
Creates a clone of the color ramp.
static QIcon colorRampPreviewIcon(QgsColorRamp *ramp, QSize size, int padding=0)
Returns an icon preview for a color ramp.
bool createNewColorRampSelected() const
Returns true if the current selection in the combo box is the option for creating a new color ramp...
void colorRampChanged(int index)
void editSourceRamp()
Triggers a dialog which allows users to edit the current source ramp for the combo box...
static QString addColorRampStatic(QWidget *parent, QgsStyle *style, QString RampType=QString())
open add color ramp dialog, return color ramp&#39;s name if the ramp has been added
A scheme based color ramp consisting of a list of predefined colors.
Definition: qgscolorramp.h:396
Totally random color ramp.
Definition: qgscolorramp.h:357
QgsGradientColorRamp * cloneGradientRamp() const
QStringList colorRampNames()
Returns a list of names of color ramps.
Definition: qgsstyle.cpp:281
virtual QgsLimitedRandomColorRamp * clone() const override
Creates a clone of the color ramp.
A widget which allows users to modify the properties of a QgsColorBrewerColorRamp.
virtual QgsCptCityColorRamp * clone() const override
Creates a clone of the color ramp.
QgsPresetSchemeColorRamp ramp
void setSourceColorRamp(QgsColorRamp *sourceRamp)
Adds or selects the current color ramp to show in the combo box.
Color ramp utilising "Color Brewer" preset color schemes.
Definition: qgscolorramp.h:458
A dialog which allows users to modify the properties of a QgsCptCityColorRamp.
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
Definition: qgscolorramp.h:108
void populate(QgsStyle *style)
initialize the combo box with color ramps from the style
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
void sourceRampEdited()
Emitted when the user has edited the current source ramp.
bool saveAsGradientRamp() const
Returns true if the ramp should be converted to a QgsGradientColorRamp.