QGIS API Documentation  2.17.0-Master (6f7b933)
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 "qgssymbollayerv2utils.h"
18 #include "qgsvectorcolorrampv2.h"
19 #include "qgsstylev2.h"
21 
26 
28 
30  : QComboBox( parent )
31  , mStyle( nullptr )
32  , mSourceColorRamp( nullptr )
33  , mShowGradientOnly( false )
34 {
35 }
36 
38 {
39  delete mSourceColorRamp;
40 }
41 
43 {
44  if ( count() != 0 )
45  return; // already populated!
46 
47  mStyle = style;
48 
50 
51  QStringList rampNames = mStyle->colorRampNames();
52  for ( QStringList::iterator it = rampNames.begin(); it != rampNames.end(); ++it )
53  {
54  QgsVectorColorRampV2* ramp = style->colorRamp( *it );
55 
56  if ( !mShowGradientOnly || ramp->type() == "gradient" )
57  {
59 
60  addItem( icon, *it );
61  }
62  delete ramp;
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 QgsRandomColorsV2();
78  }
79  else if ( rampName == "[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 ) == "[source]" )
98  setItemIcon( 0, icon );
99  else
100  insertItem( 0, icon, "[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 = QgsStyleV2ManagerDialog::addColorRampStatic( this, mStyle, "Gradient" );
118  }
119  if ( rampName.isEmpty() )
120  return;
121 
122  // put newly added ramp into the combo
123  QgsVectorColorRampV2* ramp = mStyle->colorRamp( rampName );
125 
126  blockSignals( true ); // avoid calling this method again!
127  insertItem( index, icon, rampName );
128  blockSignals( false );
129 
130  delete ramp;
131 
132  // ... and set it as active
133  setCurrentIndex( index );
134 
135  // make sure the color ramp is stored
136  mStyle->save();
137 }
138 
140 {
141  QgsVectorColorRampV2* currentRamp = currentColorRamp();
142  if ( !currentRamp )
143  return;
144 
145  QScopedPointer<QgsVectorColorRampV2> newRamp( currentRamp->clone() );
146 
147  if ( newRamp->type() == "gradient" )
148  {
149  QgsVectorGradientColorRampV2* gradRamp = static_cast<QgsVectorGradientColorRampV2*>( newRamp.data() );
150  QgsVectorGradientColorRampV2Dialog dlg( gradRamp, this );
151  if ( dlg.exec() && gradRamp )
152  {
153  setSourceColorRamp( gradRamp );
154  emit sourceRampEdited();
155  }
156  }
157  else if ( newRamp->type() == "random" )
158  {
159  QgsVectorRandomColorRampV2* randRamp = static_cast<QgsVectorRandomColorRampV2*>( newRamp.data() );
160  QgsVectorRandomColorRampV2Dialog dlg( randRamp, this );
161  if ( dlg.exec() )
162  {
163  setSourceColorRamp( randRamp );
164  emit sourceRampEdited();
165  }
166  }
167  else if ( newRamp->type() == "colorbrewer" )
168  {
169  QgsVectorColorBrewerColorRampV2* brewerRamp = static_cast<QgsVectorColorBrewerColorRampV2*>( newRamp.data() );
170  QgsVectorColorBrewerColorRampV2Dialog dlg( brewerRamp, this );
171  if ( dlg.exec() )
172  {
173  setSourceColorRamp( brewerRamp );
174  emit sourceRampEdited();
175  }
176  }
177  else if ( newRamp->type() == "cpt-city" )
178  {
179  QgsCptCityColorRampV2* cptCityRamp = static_cast<QgsCptCityColorRampV2*>( newRamp.data() );
180  QgsCptCityColorRampV2Dialog dlg( cptCityRamp, this );
181  if ( dlg.exec() && cptCityRamp )
182  {
183  setSourceColorRamp( cptCityRamp );
184  emit sourceRampEdited();
185  }
186  }
187 }
QgsVectorColorRampV2 * currentColorRamp()
return new instance of the current color ramp or NULL if there is no active color ramp ...
static unsigned index
QgsVectorColorRampV2 * colorRamp(const QString &name)
return a NEW copy of color ramp
Definition: qgsstylev2.cpp:257
QgsColorRampComboBox(QWidget *parent=nullptr)
static QIcon colorRampPreviewIcon(QgsVectorColorRampV2 *ramp, QSize size)
virtual QString type() const =0
Returns a string representing the color ramp type.
QStyle * style() const
void setIconSize(const QSize &size)
int exec()
QString itemText(int index) const
const QPixmap * icon() const
QStringList colorRampNames()
return a list of names of color ramps
Definition: qgsstylev2.cpp:273
QString tr(const char *sourceText, const char *disambiguation, int n)
void insertItem(int index, const QString &text, const QVariant &userData)
bool save(QString filename=QString())
save style into a file (will use current filename if empty string is passed)
Definition: qgsstylev2.cpp:357
void addItem(const QString &text, const QVariant &userData)
int count() const
virtual QgsVectorColorRampV2 * clone() const =0
Creates a clone of the color ramp.
bool isEmpty() const
void activated(int index)
void colorRampChanged(int index)
void editSourceRamp()
Triggers a dialog which allows users to edit the current source ramp for the combo box...
iterator end()
bool blockSignals(bool block)
Random color ramp, which returns random colors based on preset parameters.
void setSourceColorRamp(QgsVectorColorRampV2 *sourceRamp)
add/select color ramp which was used previously by the renderer
bool createNewColorRampSelected() const
Returns true if the current selection in the combo box is the option for creating a new color ramp...
QgsVectorColorRampV2 * mSourceColorRamp
void setItemIcon(int index, const QIcon &icon)
int currentIndex() const
static QString addColorRampStatic(QWidget *parent, QgsStyleV2 *style, QString RampType=QString())
open add color ramp dialog, return color ramp&#39;s name if the ramp has been added
QString currentText() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Abstract base class for color ramps.
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
void sourceRampEdited()
Emitted when the user has edited the current source ramp.
iterator begin()
void populate(QgsStyleV2 *style)
initialize the combo box with color ramps from the style