Skip to content
Permalink
Browse files
Added support for ColorBrewer palettes - available as a new type of c…
…olor ramps.

git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@12121 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Nov 15, 2009
1 parent 1ac22c1 commit f3c8e3f30e570904ec479991e75f4bc93b99dab9
@@ -1049,6 +1049,7 @@ void QgisApp::createActions()
connect( mActionAbout, SIGNAL( triggered() ), this, SLOT( about() ) );

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

Large diffs are not rendered by default.

@@ -563,6 +563,8 @@ QgsVectorColorRampV2* QgsSymbolLayerV2Utils::loadColorRamp(QDomElement& element)
return QgsVectorGradientColorRampV2::create(props);
else if (rampType == "random")
return QgsVectorRandomColorRampV2::create(props);
else if (rampType == "colorbrewer")
return QgsVectorColorBrewerColorRampV2::create(props);
else
{
QgsDebugMsg("unknown colorramp type " + rampType);
@@ -114,3 +114,66 @@ void QgsVectorRandomColorRampV2::updateColors()
mColors.append( QColor::fromHsv(h,s,v) );
}
}


////////////

QgsVectorColorBrewerColorRampV2::QgsVectorColorBrewerColorRampV2(QString schemeName, int colors)
: mSchemeName(schemeName), mColors(colors)
{
loadPalette();
}

QgsVectorColorRampV2* QgsVectorColorBrewerColorRampV2::create(const QgsStringMap& props)
{
QString schemeName = DEFAULT_COLORBREWER_SCHEMENAME;
int colors = DEFAULT_COLORBREWER_COLORS;

if (props.contains("schemeName"))
schemeName = props["schemeName"];
if (props.contains("colors"))
colors = props["colors"].toInt();

return new QgsVectorColorBrewerColorRampV2(schemeName, colors);
}

#include "qgscolorbrewerpalette.h"

void QgsVectorColorBrewerColorRampV2::loadPalette()
{
mPalette = QgsColorBrewerPalette::listSchemeColors(mSchemeName, mColors);
}

QStringList QgsVectorColorBrewerColorRampV2::listSchemeNames()
{
return QgsColorBrewerPalette::listSchemes();
}

QList<int> QgsVectorColorBrewerColorRampV2::listSchemeVariants(QString schemeName)
{
return QgsColorBrewerPalette::listSchemeVariants(schemeName);
}

QColor QgsVectorColorBrewerColorRampV2::color(double value) const
{
if (mPalette.isEmpty() || value < 0 || value > 1)
return QColor(255,0,0); // red color as a warning :)

int paletteEntry = (int) (value * mPalette.count());
if (paletteEntry > mPalette.count())
paletteEntry = mPalette.count()-1;
return mPalette.at(paletteEntry);
}

QgsVectorColorRampV2* QgsVectorColorBrewerColorRampV2::clone() const
{
return new QgsVectorColorBrewerColorRampV2(mSchemeName, mColors);
}

QgsStringMap QgsVectorColorBrewerColorRampV2::properties() const
{
QgsStringMap map;
map["schemeName"] = mSchemeName;
map["colors"] = QString::number(mColors);
return map;
}
@@ -100,4 +100,42 @@ class QgsVectorRandomColorRampV2 : public QgsVectorColorRampV2
QList<QColor> mColors;
};


#define DEFAULT_COLORBREWER_SCHEMENAME "Spectral"
#define DEFAULT_COLORBREWER_COLORS 5

class QgsVectorColorBrewerColorRampV2 : public QgsVectorColorRampV2
{
public:
QgsVectorColorBrewerColorRampV2(QString schemeName = DEFAULT_COLORBREWER_SCHEMENAME,
int colors = DEFAULT_COLORBREWER_COLORS);

static QgsVectorColorRampV2* create(const QgsStringMap& properties = QgsStringMap());

virtual QColor color(double value) const;

virtual QString type() const { return "colorbrewer"; }

virtual QgsVectorColorRampV2* clone() const;

virtual QgsStringMap properties() const;

QString schemeName() const { return mSchemeName; }
int colors() const { return mColors; }

void setSchemeName(QString schemeName) { mSchemeName = schemeName; loadPalette(); }
void setColors(int colors) { mColors = colors; loadPalette(); }

static QStringList listSchemeNames();
static QList<int> listSchemeVariants(QString schemeName);

protected:

void loadPalette();

QString mSchemeName;
int mColors;
QList<QColor> mPalette;
};

#endif
@@ -15,6 +15,7 @@ symbology-ng/qgssymbollevelsv2dialog.cpp
symbology-ng/qgssymbolv2selectordialog.cpp
symbology-ng/qgsvectorgradientcolorrampv2dialog.cpp
symbology-ng/qgsvectorrandomcolorrampv2dialog.cpp
symbology-ng/qgsvectorcolorbrewercolorrampv2dialog.cpp

qgisgui.cpp
qgisinterface.cpp
@@ -57,6 +58,7 @@ symbology-ng/qgssymbollevelsv2dialog.h
symbology-ng/qgssymbolv2selectordialog.h
symbology-ng/qgsvectorgradientcolorrampv2dialog.h
symbology-ng/qgsvectorrandomcolorrampv2dialog.h
symbology-ng/qgsvectorcolorbrewercolorrampv2dialog.h

qgscomposerview.h
qgsdetaileditemdelegate.h
@@ -9,6 +9,7 @@
#include "qgssymbolv2propertiesdialog.h"
#include "qgsvectorgradientcolorrampv2dialog.h"
#include "qgsvectorrandomcolorrampv2dialog.h"
#include "qgsvectorcolorbrewercolorrampv2dialog.h"

#include <QFile>
#include <QInputDialog>
@@ -234,7 +235,7 @@ bool QgsStyleV2ManagerDialog::addColorRamp()
{
// let the user choose the color ramp type
QStringList rampTypes;
rampTypes << "Gradient" << "Random";
rampTypes << "Gradient" << "Random" << "ColorBrewer";
bool ok;
QString rampType = QInputDialog::getItem(this, "Color ramp type",
"Please select color ramp type:", rampTypes, 0, false, &ok);
@@ -264,6 +265,17 @@ bool QgsStyleV2ManagerDialog::addColorRamp()
}
ramp = randRamp;
}
else if (rampType == "ColorBrewer")
{
QgsVectorColorBrewerColorRampV2* brewerRamp = new QgsVectorColorBrewerColorRampV2();
QgsVectorColorBrewerColorRampV2Dialog dlg(brewerRamp, this);
if (!dlg.exec())
{
delete brewerRamp;
return false;
}
ramp = brewerRamp;
}
else
{
Q_ASSERT(0 && "invalid ramp type");
@@ -351,6 +363,16 @@ bool QgsStyleV2ManagerDialog::editColorRamp()
return false;
}
}
else if (ramp->type() == "colorbrewer")
{
QgsVectorColorBrewerColorRampV2* brewerRamp = static_cast<QgsVectorColorBrewerColorRampV2*>(ramp);
QgsVectorColorBrewerColorRampV2Dialog dlg(brewerRamp, this);
if (!dlg.exec())
{
delete ramp;
return false;
}
}
else
{
Q_ASSERT(0 && "invalid ramp type");
@@ -0,0 +1,83 @@

#include "qgsvectorcolorbrewercolorrampv2dialog.h"

#include "qgsvectorcolorrampv2.h"

#include <QAbstractButton>

static void updateColorButton(QAbstractButton* button, QColor color)
{
QPixmap p(20,20);
p.fill(color);
button->setIcon(QIcon(p));
}

/////////


QgsVectorColorBrewerColorRampV2Dialog::QgsVectorColorBrewerColorRampV2Dialog(QgsVectorColorBrewerColorRampV2* ramp, QWidget* parent)
: QDialog(parent), mRamp(ramp)
{

setupUi(this);

QSize iconSize(50,16);
cboSchemeName->setIconSize(iconSize);

QStringList schemes = QgsVectorColorBrewerColorRampV2::listSchemeNames();
foreach (QString schemeName, schemes)
{
// create a preview icon using five color variant
QgsVectorColorBrewerColorRampV2* r = new QgsVectorColorBrewerColorRampV2(schemeName, 5);
QIcon icon = QgsSymbolLayerV2Utils::colorRampPreviewIcon(r, iconSize);
delete r;
cboSchemeName->addItem(icon, schemeName);
}

cboSchemeName->setCurrentIndex(cboSchemeName->findText(ramp->schemeName()));
populateVariants();
cboColors->setCurrentIndex(cboColors->findText(QString::number(ramp->colors())));

connect(cboSchemeName, SIGNAL(currentIndexChanged(int)), this, SLOT(setSchemeName()));
connect(cboColors, SIGNAL(currentIndexChanged(int)), this, SLOT(setColors()));

updatePreview();
}

void QgsVectorColorBrewerColorRampV2Dialog::populateVariants()
{
QString oldVariant = cboColors->currentText();

cboColors->clear();
QString schemeName = cboSchemeName->currentText();
QList<int> variants = QgsVectorColorBrewerColorRampV2::listSchemeVariants(schemeName);
foreach (int variant, variants)
{
cboColors->addItem(QString::number(variant));
}

// try to set the original variant again (if exists)
cboColors->setCurrentIndex(cboColors->findText(oldVariant));
}

void QgsVectorColorBrewerColorRampV2Dialog::updatePreview()
{
QSize size(300,40);
lblPreview->setPixmap(QgsSymbolLayerV2Utils::colorRampPreviewPixmap(mRamp, size));
}

void QgsVectorColorBrewerColorRampV2Dialog::setSchemeName()
{
// populate list of variants
populateVariants();

mRamp->setSchemeName(cboSchemeName->currentText());
updatePreview();
}

void QgsVectorColorBrewerColorRampV2Dialog::setColors()
{
int num = cboColors->currentText().toInt();
mRamp->setColors(num);
updatePreview();
}
@@ -0,0 +1,31 @@

#ifndef QGSVECTORCOLORBREWERCOLORRAMPV2DIALOG_H
#define QGSVECTORCOLORBREWERCOLORRAMPV2DIALOG_H

#include <QDialog>

#include "ui_qgsvectorcolorbrewercolorrampv2dialogbase.h"

class QgsVectorColorBrewerColorRampV2;

class QgsVectorColorBrewerColorRampV2Dialog : public QDialog, private Ui::QgsVectorColorBrewerColorRampV2DialogBase
{
Q_OBJECT

public:
QgsVectorColorBrewerColorRampV2Dialog(QgsVectorColorBrewerColorRampV2* ramp, QWidget* parent = NULL);

public slots:
void setSchemeName();
void setColors();

void populateVariants();

protected:

void updatePreview();

QgsVectorColorBrewerColorRampV2* mRamp;
};

#endif
Loading

0 comments on commit f3c8e3f

Please sign in to comment.