QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 
23 
25  QComboBox( parent ), mStyle( NULL ), mSourceColorRamp( NULL ), mShowGradientOnly( false )
26 {
27 }
28 
30 {
31  delete mSourceColorRamp;
32 }
33 
35 {
36  if ( count() != 0 )
37  return; // already populated!
38 
39  mStyle = style;
40 
41  setIconSize( rampIconSize );
42 
43  QStringList rampNames = mStyle->colorRampNames();
44  for ( QStringList::iterator it = rampNames.begin(); it != rampNames.end(); ++it )
45  {
46  QgsVectorColorRampV2* ramp = style->colorRamp( *it );
47 
48  if ( !mShowGradientOnly || ramp->type() == "gradient" )
49  {
51 
52  addItem( icon, *it );
53  }
54  delete ramp;
55  }
56 
57  if ( !mShowGradientOnly )
58  addItem( tr( "Random colors" ) );
59  addItem( tr( "New color ramp..." ) );
60  connect( this, SIGNAL( activated( int ) ), SLOT( colorRampChanged( int ) ) );
61 }
62 
64 {
65  QString rampName = currentText();
66 
67  if ( rampName == tr( "Random colors" ) )
68  {
69  return new QgsRandomColorsV2();
70  }
71  else if ( rampName == "[source]" && mSourceColorRamp )
72  return mSourceColorRamp->clone();
73  else
74  return mStyle->colorRamp( rampName );
75 }
76 
78 {
79  mSourceColorRamp = sourceRamp->clone();
80 
82  if ( itemText( 0 ) == "[source]" )
83  setItemIcon( 0, icon );
84  else
85  insertItem( 0, icon, "[source]" );
86  setCurrentIndex( 0 );
87 }
88 
90 {
91  if ( index != count() - 1 )
92  return;
93 
94  // last item: "new color ramp..."
95  QString rampName;
96  if ( !mShowGradientOnly )
97  {
99  }
100  else
101  {
102  rampName = QgsStyleV2ManagerDialog::addColorRampStatic( this, mStyle, "Gradient" );
103  }
104  if ( rampName.isEmpty() )
105  return;
106 
107  // put newly added ramp into the combo
108  QgsVectorColorRampV2* ramp = mStyle->colorRamp( rampName );
110 
111  blockSignals( true ); // avoid calling this method again!
112  insertItem( index, icon, rampName );
113  blockSignals( false );
114 
115  delete ramp;
116 
117  // ... and set it as active
118  setCurrentIndex( index );
119 
120  // make sure the color ramp is stored
121  mStyle->save();
122 }
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
static QIcon icon(QString icon)
QStringList colorRampNames()
return a list of names of color ramps
Definition: qgsstylev2.cpp:276
bool save(QString filename=QString())
save style into a file (will use current filename if empty string is passed)
Definition: qgsstylev2.cpp:360
virtual QgsVectorColorRampV2 * clone() const =0
void colorRampChanged(int index)
QgsColorRampComboBox(QWidget *parent=0)
void setSourceColorRamp(QgsVectorColorRampV2 *sourceRamp)
add/select color ramp which was used previously by the renderer
QgsVectorColorRampV2 * mSourceColorRamp
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
void populate(QgsStyleV2 *style)
initialize the combo box with color ramps from the style
#define tr(sourceText)