Skip to content

Commit f3c8e3f

Browse files
author
wonder
committedNov 15, 2009
Added support for ColorBrewer palettes - available as a new type of color ramps.
git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@12121 c8812cc2-4d05-0410-92ff-de0c093fc19c

10 files changed

+736
-1
lines changed
 

‎src/app/qgisapp.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,7 @@ void QgisApp::createActions()
10491049
connect( mActionAbout, SIGNAL( triggered() ), this, SLOT( about() ) );
10501050

10511051
mActionStyleManagerV2 = new QAction( tr("Style manager..."), this );
1052+
shortcuts->registerAction( mActionStyleManagerV2 );
10521053
mActionStyleManagerV2->setStatusTip( tr( "Show style manager V2" ) );
10531054
connect( mActionStyleManagerV2, SIGNAL( triggered() ), this, SLOT( showStyleManagerV2() ) );
10541055
}

‎src/core/symbology-ng/qgscolorbrewerpalette.h

Lines changed: 346 additions & 0 deletions
Large diffs are not rendered by default.

‎src/core/symbology-ng/qgssymbollayerv2utils.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,8 @@ QgsVectorColorRampV2* QgsSymbolLayerV2Utils::loadColorRamp(QDomElement& element)
563563
return QgsVectorGradientColorRampV2::create(props);
564564
else if (rampType == "random")
565565
return QgsVectorRandomColorRampV2::create(props);
566+
else if (rampType == "colorbrewer")
567+
return QgsVectorColorBrewerColorRampV2::create(props);
566568
else
567569
{
568570
QgsDebugMsg("unknown colorramp type " + rampType);

‎src/core/symbology-ng/qgsvectorcolorrampv2.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,66 @@ void QgsVectorRandomColorRampV2::updateColors()
114114
mColors.append( QColor::fromHsv(h,s,v) );
115115
}
116116
}
117+
118+
119+
////////////
120+
121+
QgsVectorColorBrewerColorRampV2::QgsVectorColorBrewerColorRampV2(QString schemeName, int colors)
122+
: mSchemeName(schemeName), mColors(colors)
123+
{
124+
loadPalette();
125+
}
126+
127+
QgsVectorColorRampV2* QgsVectorColorBrewerColorRampV2::create(const QgsStringMap& props)
128+
{
129+
QString schemeName = DEFAULT_COLORBREWER_SCHEMENAME;
130+
int colors = DEFAULT_COLORBREWER_COLORS;
131+
132+
if (props.contains("schemeName"))
133+
schemeName = props["schemeName"];
134+
if (props.contains("colors"))
135+
colors = props["colors"].toInt();
136+
137+
return new QgsVectorColorBrewerColorRampV2(schemeName, colors);
138+
}
139+
140+
#include "qgscolorbrewerpalette.h"
141+
142+
void QgsVectorColorBrewerColorRampV2::loadPalette()
143+
{
144+
mPalette = QgsColorBrewerPalette::listSchemeColors(mSchemeName, mColors);
145+
}
146+
147+
QStringList QgsVectorColorBrewerColorRampV2::listSchemeNames()
148+
{
149+
return QgsColorBrewerPalette::listSchemes();
150+
}
151+
152+
QList<int> QgsVectorColorBrewerColorRampV2::listSchemeVariants(QString schemeName)
153+
{
154+
return QgsColorBrewerPalette::listSchemeVariants(schemeName);
155+
}
156+
157+
QColor QgsVectorColorBrewerColorRampV2::color(double value) const
158+
{
159+
if (mPalette.isEmpty() || value < 0 || value > 1)
160+
return QColor(255,0,0); // red color as a warning :)
161+
162+
int paletteEntry = (int) (value * mPalette.count());
163+
if (paletteEntry > mPalette.count())
164+
paletteEntry = mPalette.count()-1;
165+
return mPalette.at(paletteEntry);
166+
}
167+
168+
QgsVectorColorRampV2* QgsVectorColorBrewerColorRampV2::clone() const
169+
{
170+
return new QgsVectorColorBrewerColorRampV2(mSchemeName, mColors);
171+
}
172+
173+
QgsStringMap QgsVectorColorBrewerColorRampV2::properties() const
174+
{
175+
QgsStringMap map;
176+
map["schemeName"] = mSchemeName;
177+
map["colors"] = QString::number(mColors);
178+
return map;
179+
}

‎src/core/symbology-ng/qgsvectorcolorrampv2.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,42 @@ class QgsVectorRandomColorRampV2 : public QgsVectorColorRampV2
100100
QList<QColor> mColors;
101101
};
102102

103+
104+
#define DEFAULT_COLORBREWER_SCHEMENAME "Spectral"
105+
#define DEFAULT_COLORBREWER_COLORS 5
106+
107+
class QgsVectorColorBrewerColorRampV2 : public QgsVectorColorRampV2
108+
{
109+
public:
110+
QgsVectorColorBrewerColorRampV2(QString schemeName = DEFAULT_COLORBREWER_SCHEMENAME,
111+
int colors = DEFAULT_COLORBREWER_COLORS);
112+
113+
static QgsVectorColorRampV2* create(const QgsStringMap& properties = QgsStringMap());
114+
115+
virtual QColor color(double value) const;
116+
117+
virtual QString type() const { return "colorbrewer"; }
118+
119+
virtual QgsVectorColorRampV2* clone() const;
120+
121+
virtual QgsStringMap properties() const;
122+
123+
QString schemeName() const { return mSchemeName; }
124+
int colors() const { return mColors; }
125+
126+
void setSchemeName(QString schemeName) { mSchemeName = schemeName; loadPalette(); }
127+
void setColors(int colors) { mColors = colors; loadPalette(); }
128+
129+
static QStringList listSchemeNames();
130+
static QList<int> listSchemeVariants(QString schemeName);
131+
132+
protected:
133+
134+
void loadPalette();
135+
136+
QString mSchemeName;
137+
int mColors;
138+
QList<QColor> mPalette;
139+
};
140+
103141
#endif

‎src/gui/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ symbology-ng/qgssymbollevelsv2dialog.cpp
1515
symbology-ng/qgssymbolv2selectordialog.cpp
1616
symbology-ng/qgsvectorgradientcolorrampv2dialog.cpp
1717
symbology-ng/qgsvectorrandomcolorrampv2dialog.cpp
18+
symbology-ng/qgsvectorcolorbrewercolorrampv2dialog.cpp
1819

1920
qgisgui.cpp
2021
qgisinterface.cpp
@@ -57,6 +58,7 @@ symbology-ng/qgssymbollevelsv2dialog.h
5758
symbology-ng/qgssymbolv2selectordialog.h
5859
symbology-ng/qgsvectorgradientcolorrampv2dialog.h
5960
symbology-ng/qgsvectorrandomcolorrampv2dialog.h
61+
symbology-ng/qgsvectorcolorbrewercolorrampv2dialog.h
6062

6163
qgscomposerview.h
6264
qgsdetaileditemdelegate.h

‎src/gui/symbology-ng/qgsstylev2managerdialog.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "qgssymbolv2propertiesdialog.h"
1010
#include "qgsvectorgradientcolorrampv2dialog.h"
1111
#include "qgsvectorrandomcolorrampv2dialog.h"
12+
#include "qgsvectorcolorbrewercolorrampv2dialog.h"
1213

1314
#include <QFile>
1415
#include <QInputDialog>
@@ -234,7 +235,7 @@ bool QgsStyleV2ManagerDialog::addColorRamp()
234235
{
235236
// let the user choose the color ramp type
236237
QStringList rampTypes;
237-
rampTypes << "Gradient" << "Random";
238+
rampTypes << "Gradient" << "Random" << "ColorBrewer";
238239
bool ok;
239240
QString rampType = QInputDialog::getItem(this, "Color ramp type",
240241
"Please select color ramp type:", rampTypes, 0, false, &ok);
@@ -264,6 +265,17 @@ bool QgsStyleV2ManagerDialog::addColorRamp()
264265
}
265266
ramp = randRamp;
266267
}
268+
else if (rampType == "ColorBrewer")
269+
{
270+
QgsVectorColorBrewerColorRampV2* brewerRamp = new QgsVectorColorBrewerColorRampV2();
271+
QgsVectorColorBrewerColorRampV2Dialog dlg(brewerRamp, this);
272+
if (!dlg.exec())
273+
{
274+
delete brewerRamp;
275+
return false;
276+
}
277+
ramp = brewerRamp;
278+
}
267279
else
268280
{
269281
Q_ASSERT(0 && "invalid ramp type");
@@ -351,6 +363,16 @@ bool QgsStyleV2ManagerDialog::editColorRamp()
351363
return false;
352364
}
353365
}
366+
else if (ramp->type() == "colorbrewer")
367+
{
368+
QgsVectorColorBrewerColorRampV2* brewerRamp = static_cast<QgsVectorColorBrewerColorRampV2*>(ramp);
369+
QgsVectorColorBrewerColorRampV2Dialog dlg(brewerRamp, this);
370+
if (!dlg.exec())
371+
{
372+
delete ramp;
373+
return false;
374+
}
375+
}
354376
else
355377
{
356378
Q_ASSERT(0 && "invalid ramp type");
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
#include "qgsvectorcolorbrewercolorrampv2dialog.h"
3+
4+
#include "qgsvectorcolorrampv2.h"
5+
6+
#include <QAbstractButton>
7+
8+
static void updateColorButton(QAbstractButton* button, QColor color)
9+
{
10+
QPixmap p(20,20);
11+
p.fill(color);
12+
button->setIcon(QIcon(p));
13+
}
14+
15+
/////////
16+
17+
18+
QgsVectorColorBrewerColorRampV2Dialog::QgsVectorColorBrewerColorRampV2Dialog(QgsVectorColorBrewerColorRampV2* ramp, QWidget* parent)
19+
: QDialog(parent), mRamp(ramp)
20+
{
21+
22+
setupUi(this);
23+
24+
QSize iconSize(50,16);
25+
cboSchemeName->setIconSize(iconSize);
26+
27+
QStringList schemes = QgsVectorColorBrewerColorRampV2::listSchemeNames();
28+
foreach (QString schemeName, schemes)
29+
{
30+
// create a preview icon using five color variant
31+
QgsVectorColorBrewerColorRampV2* r = new QgsVectorColorBrewerColorRampV2(schemeName, 5);
32+
QIcon icon = QgsSymbolLayerV2Utils::colorRampPreviewIcon(r, iconSize);
33+
delete r;
34+
cboSchemeName->addItem(icon, schemeName);
35+
}
36+
37+
cboSchemeName->setCurrentIndex(cboSchemeName->findText(ramp->schemeName()));
38+
populateVariants();
39+
cboColors->setCurrentIndex(cboColors->findText(QString::number(ramp->colors())));
40+
41+
connect(cboSchemeName, SIGNAL(currentIndexChanged(int)), this, SLOT(setSchemeName()));
42+
connect(cboColors, SIGNAL(currentIndexChanged(int)), this, SLOT(setColors()));
43+
44+
updatePreview();
45+
}
46+
47+
void QgsVectorColorBrewerColorRampV2Dialog::populateVariants()
48+
{
49+
QString oldVariant = cboColors->currentText();
50+
51+
cboColors->clear();
52+
QString schemeName = cboSchemeName->currentText();
53+
QList<int> variants = QgsVectorColorBrewerColorRampV2::listSchemeVariants(schemeName);
54+
foreach (int variant, variants)
55+
{
56+
cboColors->addItem(QString::number(variant));
57+
}
58+
59+
// try to set the original variant again (if exists)
60+
cboColors->setCurrentIndex(cboColors->findText(oldVariant));
61+
}
62+
63+
void QgsVectorColorBrewerColorRampV2Dialog::updatePreview()
64+
{
65+
QSize size(300,40);
66+
lblPreview->setPixmap(QgsSymbolLayerV2Utils::colorRampPreviewPixmap(mRamp, size));
67+
}
68+
69+
void QgsVectorColorBrewerColorRampV2Dialog::setSchemeName()
70+
{
71+
// populate list of variants
72+
populateVariants();
73+
74+
mRamp->setSchemeName(cboSchemeName->currentText());
75+
updatePreview();
76+
}
77+
78+
void QgsVectorColorBrewerColorRampV2Dialog::setColors()
79+
{
80+
int num = cboColors->currentText().toInt();
81+
mRamp->setColors(num);
82+
updatePreview();
83+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
#ifndef QGSVECTORCOLORBREWERCOLORRAMPV2DIALOG_H
3+
#define QGSVECTORCOLORBREWERCOLORRAMPV2DIALOG_H
4+
5+
#include <QDialog>
6+
7+
#include "ui_qgsvectorcolorbrewercolorrampv2dialogbase.h"
8+
9+
class QgsVectorColorBrewerColorRampV2;
10+
11+
class QgsVectorColorBrewerColorRampV2Dialog : public QDialog, private Ui::QgsVectorColorBrewerColorRampV2DialogBase
12+
{
13+
Q_OBJECT
14+
15+
public:
16+
QgsVectorColorBrewerColorRampV2Dialog(QgsVectorColorBrewerColorRampV2* ramp, QWidget* parent = NULL);
17+
18+
public slots:
19+
void setSchemeName();
20+
void setColors();
21+
22+
void populateVariants();
23+
24+
protected:
25+
26+
void updatePreview();
27+
28+
QgsVectorColorBrewerColorRampV2* mRamp;
29+
};
30+
31+
#endif
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>QgsVectorColorBrewerColorRampV2DialogBase</class>
4+
<widget class="QDialog" name="QgsVectorColorBrewerColorRampV2DialogBase">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>400</width>
10+
<height>300</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>ColorBrewer ramp</string>
15+
</property>
16+
<layout class="QVBoxLayout">
17+
<item>
18+
<layout class="QGridLayout">
19+
<item row="0" column="0">
20+
<widget class="QLabel" name="label">
21+
<property name="text">
22+
<string>Scheme name:</string>
23+
</property>
24+
</widget>
25+
</item>
26+
<item row="0" column="2" rowspan="2">
27+
<spacer>
28+
<property name="orientation">
29+
<enum>Qt::Horizontal</enum>
30+
</property>
31+
<property name="sizeType">
32+
<enum>QSizePolicy::Preferred</enum>
33+
</property>
34+
<property name="sizeHint" stdset="0">
35+
<size>
36+
<width>40</width>
37+
<height>71</height>
38+
</size>
39+
</property>
40+
</spacer>
41+
</item>
42+
<item row="1" column="0">
43+
<widget class="QLabel" name="label_2">
44+
<property name="text">
45+
<string>Colors:</string>
46+
</property>
47+
</widget>
48+
</item>
49+
<item row="0" column="1">
50+
<widget class="QComboBox" name="cboSchemeName"/>
51+
</item>
52+
<item row="1" column="1">
53+
<widget class="QComboBox" name="cboColors"/>
54+
</item>
55+
</layout>
56+
</item>
57+
<item>
58+
<spacer>
59+
<property name="orientation">
60+
<enum>Qt::Vertical</enum>
61+
</property>
62+
<property name="sizeType">
63+
<enum>QSizePolicy::Preferred</enum>
64+
</property>
65+
<property name="sizeHint" stdset="0">
66+
<size>
67+
<width>20</width>
68+
<height>40</height>
69+
</size>
70+
</property>
71+
</spacer>
72+
</item>
73+
<item>
74+
<widget class="QGroupBox" name="groupBox">
75+
<property name="title">
76+
<string>Preview</string>
77+
</property>
78+
<layout class="QVBoxLayout">
79+
<item>
80+
<widget class="QLabel" name="lblPreview">
81+
<property name="frameShape">
82+
<enum>QFrame::NoFrame</enum>
83+
</property>
84+
<property name="text">
85+
<string/>
86+
</property>
87+
<property name="alignment">
88+
<set>Qt::AlignCenter</set>
89+
</property>
90+
</widget>
91+
</item>
92+
</layout>
93+
</widget>
94+
</item>
95+
<item>
96+
<widget class="QDialogButtonBox" name="buttonBox">
97+
<property name="orientation">
98+
<enum>Qt::Horizontal</enum>
99+
</property>
100+
<property name="standardButtons">
101+
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
102+
</property>
103+
</widget>
104+
</item>
105+
</layout>
106+
</widget>
107+
<tabstops>
108+
<tabstop>cboSchemeName</tabstop>
109+
<tabstop>cboColors</tabstop>
110+
<tabstop>buttonBox</tabstop>
111+
</tabstops>
112+
<resources/>
113+
<connections>
114+
<connection>
115+
<sender>buttonBox</sender>
116+
<signal>accepted()</signal>
117+
<receiver>QgsVectorColorBrewerColorRampV2DialogBase</receiver>
118+
<slot>accept()</slot>
119+
<hints>
120+
<hint type="sourcelabel">
121+
<x>258</x>
122+
<y>281</y>
123+
</hint>
124+
<hint type="destinationlabel">
125+
<x>168</x>
126+
<y>256</y>
127+
</hint>
128+
</hints>
129+
</connection>
130+
<connection>
131+
<sender>buttonBox</sender>
132+
<signal>rejected()</signal>
133+
<receiver>QgsVectorColorBrewerColorRampV2DialogBase</receiver>
134+
<slot>reject()</slot>
135+
<hints>
136+
<hint type="sourcelabel">
137+
<x>363</x>
138+
<y>273</y>
139+
</hint>
140+
<hint type="destinationlabel">
141+
<x>371</x>
142+
<y>259</y>
143+
</hint>
144+
</hints>
145+
</connection>
146+
</connections>
147+
</ui>

0 commit comments

Comments
 (0)
Please sign in to comment.