QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 ), mStyle( NULL ), mSourceColorRamp( NULL ), mShowGradientOnly( false )
31 {
32 }
33 
35 {
36  delete mSourceColorRamp;
37 }
38 
40 {
41  if ( count() != 0 )
42  return; // already populated!
43 
44  mStyle = style;
45 
47 
48  QStringList rampNames = mStyle->colorRampNames();
49  for ( QStringList::iterator it = rampNames.begin(); it != rampNames.end(); ++it )
50  {
51  QgsVectorColorRampV2* ramp = style->colorRamp( *it );
52 
53  if ( !mShowGradientOnly || ramp->type() == "gradient" )
54  {
56 
57  addItem( icon, *it );
58  }
59  delete ramp;
60  }
61 
62  if ( !mShowGradientOnly )
63  addItem( tr( "Random colors" ) );
64  addItem( tr( "New color ramp..." ) );
65  connect( this, SIGNAL( activated( int ) ), SLOT( colorRampChanged( int ) ) );
66 }
67 
69 {
70  QString rampName = currentText();
71 
72  if ( rampName == tr( "Random colors" ) )
73  {
74  return new QgsRandomColorsV2();
75  }
76  else if ( rampName == "[source]" && mSourceColorRamp )
77  return mSourceColorRamp->clone();
78  else
79  return mStyle->colorRamp( rampName );
80 }
81 
83 {
84  int index = currentIndex();
85  return index == count() - 1; //create new ramp is last item in combobox
86 }
87 
89 {
90  mSourceColorRamp = sourceRamp->clone();
91 
93  if ( itemText( 0 ) == "[source]" )
94  setItemIcon( 0, icon );
95  else
96  insertItem( 0, icon, "[source]" );
97  setCurrentIndex( 0 );
98 }
99 
101 {
102  if ( index != count() - 1 )
103  return;
104 
105  // last item: "new color ramp..."
106  QString rampName;
107  if ( !mShowGradientOnly )
108  {
110  }
111  else
112  {
113  rampName = QgsStyleV2ManagerDialog::addColorRampStatic( this, mStyle, "Gradient" );
114  }
115  if ( rampName.isEmpty() )
116  return;
117 
118  // put newly added ramp into the combo
119  QgsVectorColorRampV2* ramp = mStyle->colorRamp( rampName );
121 
122  blockSignals( true ); // avoid calling this method again!
123  insertItem( index, icon, rampName );
124  blockSignals( false );
125 
126  delete ramp;
127 
128  // ... and set it as active
129  setCurrentIndex( index );
130 
131  // make sure the color ramp is stored
132  mStyle->save();
133 }
134 
136 {
137  QgsVectorColorRampV2* currentRamp = currentColorRamp();
138  if ( !currentRamp )
139  return;
140 
141  QScopedPointer<QgsVectorColorRampV2> newRamp( currentRamp->clone() );
142 
143  if ( newRamp->type() == "gradient" )
144  {
145  QgsVectorGradientColorRampV2* gradRamp = static_cast<QgsVectorGradientColorRampV2*>( newRamp.data() );
146  QgsVectorGradientColorRampV2Dialog dlg( gradRamp, this );
147  if ( dlg.exec() && gradRamp )
148  {
149  setSourceColorRamp( gradRamp );
150  emit sourceRampEdited();
151  }
152  }
153  else if ( newRamp->type() == "random" )
154  {
155  QgsVectorRandomColorRampV2* randRamp = static_cast<QgsVectorRandomColorRampV2*>( newRamp.data() );
156  QgsVectorRandomColorRampV2Dialog dlg( randRamp, this );
157  if ( dlg.exec() )
158  {
159  setSourceColorRamp( randRamp );
160  emit sourceRampEdited();
161  }
162  }
163  else if ( newRamp->type() == "colorbrewer" )
164  {
165  QgsVectorColorBrewerColorRampV2* brewerRamp = static_cast<QgsVectorColorBrewerColorRampV2*>( newRamp.data() );
166  QgsVectorColorBrewerColorRampV2Dialog dlg( brewerRamp, this );
167  if ( dlg.exec() )
168  {
169  setSourceColorRamp( brewerRamp );
170  emit sourceRampEdited();
171  }
172  }
173  else if ( newRamp->type() == "cpt-city" )
174  {
175  QgsCptCityColorRampV2* cptCityRamp = static_cast<QgsCptCityColorRampV2*>( newRamp.data() );
176  QgsCptCityColorRampV2Dialog dlg( cptCityRamp, this );
177  if ( dlg.exec() && cptCityRamp )
178  {
179  setSourceColorRamp( cptCityRamp );
180  emit sourceRampEdited();
181  }
182  }
183 }
QgsVectorColorRampV2 * currentColorRamp()
return new instance of the current color ramp or NULL if there is no active color ramp ...
static unsigned index
static QIcon colorRampPreviewIcon(QgsVectorColorRampV2 *ramp, QSize size)
virtual QString type() const =0
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:276
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:360
void addItem(const QString &text, const QVariant &userData)
int count() const
virtual QgsVectorColorRampV2 * clone() const =0
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)
QgsColorRampComboBox(QWidget *parent=0)
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's name if the ramp has been added
QgsVectorColorRampV2 * colorRamp(QString name)
return a NEW copy of color ramp
Definition: qgsstylev2.cpp:260
QString currentText() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
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