Skip to content
Permalink
Browse files

Added random color ramp to as an example of a discrete color ramp.

git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@10802 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder
wonder committed May 15, 2009
1 parent 76923cb commit 7877debda083b7e147f085593c44f910e61963c1
@@ -3,6 +3,8 @@

#include "qgssymbollayerv2utils.h"

#include <stdlib.h> // for random()

QgsVectorGradientColorRampV2::QgsVectorGradientColorRampV2(QColor color1, QColor color2)
: mColor1(color1), mColor2(color2)
{
@@ -40,3 +42,75 @@ QgsStringMap QgsVectorGradientColorRampV2::properties() const
map["color2"] = QgsSymbolLayerV2Utils::encodeColor(mColor2);
return map;
}

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


QgsVectorRandomColorRampV2::QgsVectorRandomColorRampV2(int count, int hueMin, int hueMax,
int satMin, int satMax, int valMin, int valMax )
: mCount(count), mHueMin(hueMin), mHueMax(hueMax),
mSatMin(satMin), mSatMax(satMax), mValMin(valMin), mValMax(valMax)
{
updateColors();
}

QgsVectorColorRampV2* QgsVectorRandomColorRampV2::create(const QgsStringMap& props)
{
int count = DEFAULT_RANDOM_COUNT;
int hueMin = DEFAULT_RANDOM_HUE_MIN, hueMax = DEFAULT_RANDOM_HUE_MAX;
int satMin = DEFAULT_RANDOM_SAT_MIN, satMax = DEFAULT_RANDOM_SAT_MAX;
int valMin = DEFAULT_RANDOM_VAL_MIN, valMax = DEFAULT_RANDOM_VAL_MAX;

if (props.contains("count")) count = props["count"].toInt();
if (props.contains("hueMin")) hueMin = props["hueMin"].toInt();
if (props.contains("hueMax")) hueMax = props["hueMax"].toInt();
if (props.contains("satMin")) satMin = props["satMin"].toInt();
if (props.contains("satMax")) satMax = props["satMax"].toInt();
if (props.contains("valMin")) valMin = props["valMin"].toInt();
if (props.contains("valMax")) valMax = props["valMax"].toInt();

return new QgsVectorRandomColorRampV2(count, hueMin, hueMax, satMin, satMax, valMin, valMax);
}

QColor QgsVectorRandomColorRampV2::color(double value) const
{
int colorCnt = mColors.count();
int colorIdx = (int) ( value * colorCnt );

if (colorIdx >= 0 && colorIdx < colorCnt)
return mColors.at( colorIdx );

return QColor();
}

QgsVectorColorRampV2* QgsVectorRandomColorRampV2::clone() const
{
return new QgsVectorRandomColorRampV2(mCount, mHueMin, mHueMax, mSatMin, mSatMax, mValMin, mValMax);
}

QgsStringMap QgsVectorRandomColorRampV2::properties() const
{
QgsStringMap map;
map["count"] = QString::number(mCount);
map["hueMin"] = QString::number(mHueMin);
map["hueMax"] = QString::number(mHueMax);
map["satMin"] = QString::number(mSatMin);
map["satMax"] = QString::number(mSatMax);
map["valMin"] = QString::number(mValMin);
map["valMax"] = QString::number(mValMax);
return map;
}

void QgsVectorRandomColorRampV2::updateColors()
{
int h,s,v;

mColors.clear();
for (int i = 0; i < mCount; i++)
{
h = (random() % (mHueMax-mHueMin+1)) + mHueMin;
s = (random() % (mSatMax-mSatMin+1)) + mSatMin;
v = (random() % (mValMax-mValMin+1)) + mValMin;
mColors.append( QColor::fromHsv(h,s,v) );
}
}
@@ -50,4 +50,54 @@ class QgsVectorGradientColorRampV2 : public QgsVectorColorRampV2
QColor mColor1, mColor2;
};

#define DEFAULT_RANDOM_COUNT 10
#define DEFAULT_RANDOM_HUE_MIN 0
#define DEFAULT_RANDOM_HUE_MAX 359
#define DEFAULT_RANDOM_VAL_MIN 0
#define DEFAULT_RANDOM_VAL_MAX 255
#define DEFAULT_RANDOM_SAT_MIN 0
#define DEFAULT_RANDOM_SAT_MAX 255

class QgsVectorRandomColorRampV2 : public QgsVectorColorRampV2
{
public:
QgsVectorRandomColorRampV2(int count = DEFAULT_RANDOM_COUNT,
int hueMin = DEFAULT_RANDOM_HUE_MIN, int hueMax = DEFAULT_RANDOM_HUE_MAX,
int satMin = DEFAULT_RANDOM_SAT_MIN, int satMax = DEFAULT_RANDOM_SAT_MAX,
int valMin = DEFAULT_RANDOM_VAL_MIN, int valMax = DEFAULT_RANDOM_VAL_MAX );

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

virtual QColor color(double value) const;

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

virtual QgsVectorColorRampV2* clone() const;

virtual QgsStringMap properties() const;

void updateColors();

int count() const { return mCount; }
int hueMin() const { return mHueMin; }
int hueMax() const { return mHueMax; }
int satMin() const { return mSatMin; }
int satMax() const { return mSatMax; }
int valMin() const { return mValMin; }
int valMax() const { return mValMax; }

void setCount(int val) { mCount = val; }
void setHueMin(int val) { mHueMin = val; }
void setHueMax(int val) { mHueMax = val; }
void setSatMin(int val) { mSatMin = val; }
void setSatMax(int val) { mSatMax = val; }
void setValMin(int val) { mValMin = val; }
void setValMax(int val) { mValMax = val; }

protected:
int mCount;
int mHueMin, mHueMax, mSatMin, mSatMax, mValMin, mValMax;
QList<QColor> mColors;
};

#endif
@@ -9,6 +9,7 @@ symbology-ng/qgsrendererv2propertiesdialog.cpp
symbology-ng/qgsstylev2managerdialog.cpp
symbology-ng/qgssymbolv2selectordialog.cpp
symbology-ng/qgsvectorgradientcolorrampv2dialog.cpp
symbology-ng/qgsvectorrandomcolorrampv2dialog.cpp

qgisgui.cpp
qgisinterface.cpp
@@ -46,6 +47,7 @@ symbology-ng/qgsrendererv2propertiesdialog.h
symbology-ng/qgsstylev2managerdialog.h
symbology-ng/qgssymbolv2selectordialog.h
symbology-ng/qgsvectorgradientcolorrampv2dialog.h
symbology-ng/qgsvectorrandomcolorrampv2dialog.h

qgscomposerview.h
qgsdetaileditemdelegate.h
@@ -8,6 +8,7 @@

#include "qgssymbolv2propertiesdialog.h"
#include "qgsvectorgradientcolorrampv2dialog.h"
#include "qgsvectorrandomcolorrampv2dialog.h"

#include <QFile>
#include <QInputDialog>
@@ -104,9 +105,17 @@ void QgsStyleV2ManagerDialog::populateList()
int itemType = currentItemType();

if (itemType < 3)
{
populateSymbols(itemType);
else
}
else if (itemType == 3)
{
populateColorRamps();
}
else
{
Q_ASSERT(0 && "not implemented");
}
}

void QgsStyleV2ManagerDialog::populateSymbols(int type)
@@ -170,11 +179,17 @@ QString QgsStyleV2ManagerDialog::currentItemName()
void QgsStyleV2ManagerDialog::addItem()
{
if (currentItemType() < 3)
{
addSymbol();
}
else if (currentItemType() == 3)
{
addColorRamp();
}
else
{
Q_ASSERT(0 && "not implemented");
}

populateList();
populateTypes();
@@ -217,18 +232,15 @@ bool QgsStyleV2ManagerDialog::addSymbol()

bool QgsStyleV2ManagerDialog::addColorRamp()
{
// TODO: random color ramp
/*
// let the user choose the color ramp type
QStringList rampTypes;
rampTypes << "Gradient" << "Random";
bool ok;
QString rampType = QInputDialog.getItem(this, "Color ramp type",
QString rampType = QInputDialog::getItem(this, "Color ramp type",
"Please select color ramp type:", rampTypes, 0, false, &ok);
if (!ok || rampType.isEmpty())
return false;
*/
QString rampType = "Gradient";


QgsVectorColorRampV2* ramp;
if (rampType == "Gradient")
{
@@ -241,22 +253,23 @@ bool QgsStyleV2ManagerDialog::addColorRamp()
}
ramp = gradRamp;
}
else
else if (rampType == "Random")
{
/*
QgsVectorRandomColorRampV2* randRamp = new QgsVectorRandomColorRampV2();
DlgRandomColorRamp dlg(randRamp, this);
QgsVectorRandomColorRampV2Dialog dlg(randRamp, this);
if (!dlg.exec())
{
delete randRamp;
return false;
}
ramp = randRamp;
*/
}
else
{
Q_ASSERT(0 && "invalid ramp type");
}

// get name
bool ok;
QString name = QInputDialog::getText(this, "Color ramp name",
"Please enter name for new color ramp:", QLineEdit::Normal, "new color ramp", &ok);
if (!ok || name.isEmpty())
@@ -274,11 +287,17 @@ bool QgsStyleV2ManagerDialog::addColorRamp()
void QgsStyleV2ManagerDialog::editItem()
{
if (currentItemType() < 3)
{
editSymbol();
}
else if (currentItemType() == 3)
{
editColorRamp();
}
else
{
Q_ASSERT(0 && "not implemented");
}

populateList();
}
@@ -322,10 +341,19 @@ bool QgsStyleV2ManagerDialog::editColorRamp()
return false;
}
}
else if (ramp->type() == "random")
{
QgsVectorRandomColorRampV2* randRamp = static_cast<QgsVectorRandomColorRampV2*>(ramp);
QgsVectorRandomColorRampV2Dialog dlg(randRamp, this);
if (!dlg.exec())
{
delete ramp;
return false;
}
}
else
{
// TODO: random color ramp
//dlg = DlgRandomColorRamp(ramp, self)
Q_ASSERT(0 && "invalid ramp type");
}

mStyle->addColorRamp(name, ramp);
@@ -336,11 +364,17 @@ bool QgsStyleV2ManagerDialog::editColorRamp()
void QgsStyleV2ManagerDialog::removeItem()
{
if (currentItemType() < 3)
{
removeSymbol();
}
else if (currentItemType() == 3)
{
removeColorRamp();
}
else
{
Q_ASSERT(0 && "not implemented");
}

populateList();
populateTypes();
@@ -0,0 +1,81 @@

#include "qgsvectorrandomcolorrampv2dialog.h"

#include "qgsvectorcolorrampv2.h"

#include <QColorDialog>


QgsVectorRandomColorRampV2Dialog::QgsVectorRandomColorRampV2Dialog(QgsVectorRandomColorRampV2* ramp, QWidget* parent)
: QDialog(parent), mRamp(ramp)
{
setupUi(this);

spinCount->setValue( ramp->count() );
spinHue1->setValue( ramp->hueMin() );
spinHue2->setValue( ramp->hueMax() );
spinSat1->setValue( ramp->satMin() );
spinSat2->setValue( ramp->satMax() );
spinVal1->setValue( ramp->valMin() );
spinVal2->setValue( ramp->valMax() );

connect(spinCount, SIGNAL(valueChanged(int)), this, SLOT(setCount(int)) );
connect(spinHue1, SIGNAL(valueChanged(int)), this, SLOT(setHue1(int)) );
connect(spinHue2, SIGNAL(valueChanged(int)), this, SLOT(setHue2(int)) );
connect(spinSat1, SIGNAL(valueChanged(int)), this, SLOT(setSat1(int)) );
connect(spinSat2, SIGNAL(valueChanged(int)), this, SLOT(setSat2(int)) );
connect(spinVal1, SIGNAL(valueChanged(int)), this, SLOT(setVal1(int)) );
connect(spinVal2, SIGNAL(valueChanged(int)), this, SLOT(setVal2(int)) );

updatePreview();
}

void QgsVectorRandomColorRampV2Dialog::updatePreview()
{
mRamp->updateColors();

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

void QgsVectorRandomColorRampV2Dialog::setCount( int val )
{
mRamp->setCount(val);
updatePreview();
}

void QgsVectorRandomColorRampV2Dialog::setHue1( int val )
{
mRamp->setHueMin(val);
updatePreview();
}

void QgsVectorRandomColorRampV2Dialog::setHue2( int val )
{
mRamp->setHueMax(val);
updatePreview();
}

void QgsVectorRandomColorRampV2Dialog::setSat1( int val )
{
mRamp->setSatMin(val);
updatePreview();
}

void QgsVectorRandomColorRampV2Dialog::setSat2( int val )
{
mRamp->setSatMax(val);
updatePreview();
}

void QgsVectorRandomColorRampV2Dialog::setVal1( int val )
{
mRamp->setValMin(val);
updatePreview();
}

void QgsVectorRandomColorRampV2Dialog::setVal2( int val )
{
mRamp->setValMax(val);
updatePreview();
}

0 comments on commit 7877deb

Please sign in to comment.
You can’t perform that action at this time.