Skip to content

Commit

Permalink
Improved categorized renderer settings in renderer dialog.
Browse files Browse the repository at this point in the history
Fixed some bugs with marker cache image size, categorized renderer.
Added dump() functions for easier debugging.


git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@10805 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed May 16, 2009
1 parent 7877deb commit 333842b
Show file tree
Hide file tree
Showing 12 changed files with 402 additions and 206 deletions.
5 changes: 4 additions & 1 deletion python/core/symbology-ng-core.sip
Expand Up @@ -29,6 +29,9 @@ public:

virtual ~QgsFeatureRendererV2();

virtual QString dump();


void renderFeature(QgsFeature& feature, QgsRenderContext& context);

protected:
Expand Down Expand Up @@ -304,7 +307,7 @@ public:

QImage bigSymbolPreviewImage();

void dump();
QString dump();

virtual QgsSymbolV2* clone() const = 0 /Factory/;

Expand Down
21 changes: 21 additions & 0 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -684,6 +684,8 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )
{
if (mRendererV2 == NULL)
return FALSE;

QgsDebugMsg("rendering v2:\n" + mRendererV2->dump());

mRendererV2->startRender(rendererContext);

Expand Down Expand Up @@ -3714,3 +3716,22 @@ QgsVectorOverlay* QgsVectorLayer::findOverlayByType( const QString& typeName )
}
return 0; //not found
}


QgsFeatureRendererV2* QgsVectorLayer::rendererV2()
{
return mRendererV2;
}
void QgsVectorLayer::setRendererV2(QgsFeatureRendererV2* r)
{
delete mRendererV2;
mRendererV2 = r;
}
bool QgsVectorLayer::isUsingRendererV2()
{
return mUsingRendererV2;
}
void QgsVectorLayer::setUsingRendererV2(bool usingRendererV2)
{
mUsingRendererV2 = usingRendererV2;
}
8 changes: 4 additions & 4 deletions src/core/qgsvectorlayer.h
Expand Up @@ -163,13 +163,13 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
void setRenderer( QgsRenderer * r );

/** Return renderer V2. Added in QGIS 1.2 */
QgsFeatureRendererV2* rendererV2() { return mRendererV2; }
QgsFeatureRendererV2* rendererV2();
/** Set renderer V2. Added in QGIS 1.2 */
void setRendererV2(QgsFeatureRendererV2* r) { mRendererV2 = r; }
void setRendererV2(QgsFeatureRendererV2* r);
/** Return whether using renderer V2. Added in QGIS 1.2 */
bool isUsingRendererV2() { return mUsingRendererV2; }
bool isUsingRendererV2();
/** set whether to use renderer V2 for drawing. Added in QGIS 1.2 */
void setUsingRendererV2(bool usingRendererV2) { mUsingRendererV2 = usingRendererV2; }
void setUsingRendererV2(bool usingRendererV2);

/** Returns point, line or polygon */
QGis::GeometryType geometryType() const;
Expand Down
17 changes: 10 additions & 7 deletions src/core/symbology-ng/qgsmarkersymbollayerv2.cpp
Expand Up @@ -148,16 +148,22 @@ void QgsSimpleMarkerSymbolLayerV2::startRender(QgsRenderContext& context)
// cache the marker
// TODO: use caching only when drawing to screen (not printer)
// TODO: decide whether to use QImage or QPixmap - based on the render context
double s = mSize+2;
mCache = QImage(QSize(s,s), QImage::Format_ARGB32_Premultiplied);

// calculate necessary image size for the cache
int pw = (( mPen.width() == 0 ? 1 : mPen.width() ) + 1 ) / 2 * 2; // make even (round up); handle cosmetic pen
int imageSize = (( int ) mSize + pw ) / 2 * 2 + 1; // make image width, height odd; account for pen width

double center = ((double) imageSize / 2) + 0.5; // add 1/2 pixel for proper rounding when the figure's coordinates are added

mCache = QImage(QSize(imageSize,imageSize), QImage::Format_ARGB32_Premultiplied);
mCache.fill(0);

QPainter p;
p.begin(&mCache);
p.setRenderHint( QPainter::Antialiasing );
p.setBrush(mBrush);
p.setPen(mPen);
p.translate(QPointF(s/2.0, s/2.0));
p.translate(QPointF(center, center));
drawMarker(&p);
p.end();
}
Expand All @@ -176,13 +182,10 @@ void QgsSimpleMarkerSymbolLayerV2::renderPoint(const QPointF& point, QgsRenderCo
//p->translate(point);

//drawMarker(p);
double s = mSize+2;
double s = mCache.width();
//if (mCache.isValid())
p->drawImage(point + QPointF(-s/2.0, -s/2.0), mCache);
//else
//qDebug("WTF!!!!!!!!!");

//p->translate(-point); // TODO: maybe save+restore is faster?
//p->restore();
}

Expand Down
56 changes: 41 additions & 15 deletions src/core/symbology-ng/qgsrendererv2.cpp
Expand Up @@ -236,6 +236,12 @@ void QgsFeatureRendererV2::renderFeature(QgsFeature& feature, QgsRenderContext&
}
}

QString QgsFeatureRendererV2::dump()
{
return "UNKNOWN RENDERER\n";
}


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

QgsSingleSymbolRendererV2::QgsSingleSymbolRendererV2(QgsSymbolV2* symbol)
Expand Down Expand Up @@ -280,6 +286,12 @@ void QgsSingleSymbolRendererV2::setSymbol(QgsSymbolV2* s)
mSymbol = s;
}

QString QgsSingleSymbolRendererV2::dump()
{
return QString("SINGLE: %1").arg(mSymbol->dump());
}


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

QgsRendererCategoryV2::QgsRendererCategoryV2(const QgsRendererCategoryV2& cat)
Expand All @@ -300,6 +312,11 @@ void QgsRendererCategoryV2::setSymbol(QgsSymbolV2* s)
mSymbol = s;
}

QString QgsRendererCategoryV2::dump()
{
return QString("%1::%2::%3\n").arg(mValue.toString()).arg(mLabel).arg(mSymbol->dump());
}

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

QgsCategorizedSymbolRendererV2::QgsCategorizedSymbolRendererV2(int attrNum, QgsCategoryList categories)
Expand All @@ -322,25 +339,23 @@ QgsCategorizedSymbolRendererV2::~QgsCategorizedSymbolRendererV2()
mCategories.clear(); // this should also call destructors of symbols
}

QgsSymbolV2* QgsCategorizedSymbolRendererV2::symbolForValue(QVariant value)
void QgsCategorizedSymbolRendererV2::rebuildHash()
{
static QHash<QString, QgsSymbolV2*> symbolHash;
static bool first = true;

// init the hash table if not yet filled
// TODO: special case for int, double
if (first)
mSymbolHash.clear();

for (int i = 0; i < mCategories.count(); ++i)
{
for (int i = 0; i < mCategories.count(); ++i)
{
QgsRendererCategoryV2& cat = mCategories[i];
symbolHash.insert(cat.value().toString(), cat.symbol());
}
first = false;
QgsRendererCategoryV2& cat = mCategories[i];
mSymbolHash.insert(cat.value().toString(), cat.symbol());
}
}

QgsSymbolV2* QgsCategorizedSymbolRendererV2::symbolForValue(QVariant value)
{
// TODO: special case for int, double

QHash<QString, QgsSymbolV2*>::iterator it = symbolHash.find(value.toString());
if (it == symbolHash.end())
QHash<QString, QgsSymbolV2*>::iterator it = mSymbolHash.find(value.toString());
if (it == mSymbolHash.end())
{
QgsDebugMsg("attribute value not found: " + value.toString());
return NULL;
Expand Down Expand Up @@ -405,6 +420,9 @@ void QgsCategorizedSymbolRendererV2::deleteAllCategories()

void QgsCategorizedSymbolRendererV2::startRender(QgsRenderContext& context)
{
// make sure that the hash table is up to date
rebuildHash();

QgsCategoryList::iterator it = mCategories.begin();
for ( ; it != mCategories.end(); ++it)
it->symbol()->startRender(context);
Expand All @@ -424,6 +442,14 @@ QList<int> QgsCategorizedSymbolRendererV2::usedAttributes()
return lst;
}

QString QgsCategorizedSymbolRendererV2::dump()
{
QString s = QString("CATEGORIZED: idx %1\n").arg(mAttrNum);
for (int i=0; i<mCategories.count();i++)
s += mCategories[i].dump();
return s;
}


/////////////////////////
// graduated
Expand Down
17 changes: 16 additions & 1 deletion src/core/symbology-ng/qgsrendererv2.h
Expand Up @@ -38,6 +38,9 @@ class QgsFeatureRendererV2
virtual ~QgsFeatureRendererV2() {}

void renderFeature(QgsFeature& feature, QgsRenderContext& context);

//! for debugging
virtual QString dump();

//TODO: symbols() for symbol levels

Expand Down Expand Up @@ -66,6 +69,8 @@ class QgsSingleSymbolRendererV2 : public QgsFeatureRendererV2
QgsSymbolV2* symbol() const;
void setSymbol(QgsSymbolV2* s);

virtual QString dump();

protected:
QgsSymbolV2* mSymbol;
};
Expand All @@ -90,6 +95,9 @@ class QgsRendererCategoryV2

void setSymbol(QgsSymbolV2* s);
void setLabel(QString label) { mLabel = label; }

// debugging
QString dump();

protected:
QVariant mValue;
Expand All @@ -115,6 +123,8 @@ class QgsCategorizedSymbolRendererV2 : public QgsFeatureRendererV2

virtual QList<int> usedAttributes();

virtual QString dump();

const QgsCategoryList& categories() { return mCategories; }

//! return index of category with specified value (-1 if not found)
Expand All @@ -132,7 +142,12 @@ class QgsCategorizedSymbolRendererV2 : public QgsFeatureRendererV2
protected:
QgsCategoryList mCategories;
int mAttrNum;


//! hashtable for faster access to symbols
QHash<QString, QgsSymbolV2*> mSymbolHash;

void rebuildHash();

QgsSymbolV2* symbolForValue(QVariant value);
};

Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgssymbollayerv2.cpp
Expand Up @@ -29,7 +29,7 @@ void QgsMarkerSymbolLayerV2::drawPreviewIcon(QPainter* painter, QSize size)
context.setPainter(painter);

startRender(context);
renderPoint(QPointF(size.width()/2 + 0.5, size.height()/2 + 0.5), context);
renderPoint(QPointF(size.width()/2, size.height()/2), context);
stopRender(context);
}

Expand Down
13 changes: 12 additions & 1 deletion src/core/symbology-ng/qgssymbolv2.cpp
Expand Up @@ -190,12 +190,23 @@ QImage QgsSymbolV2::bigSymbolPreviewImage()
}


void QgsSymbolV2::dump()
QString QgsSymbolV2::dump()
{
QString t;
switch (type())
{
case QgsSymbolV2::Marker: t = "MARKER"; break;
case QgsSymbolV2::Line: t = "LINE"; break;
case QgsSymbolV2::Fill: t = "FILL"; break;
default: Q_ASSERT(0 && "unknown symbol type");
}
QString s = QString("%1 SYMBOL (%2 layers) color %3").arg(t).arg(mLayers.count()).arg(QgsSymbolLayerV2Utils::encodeColor(color()));

for (QgsSymbolLayerV2List::iterator it = mLayers.begin(); it != mLayers.end(); ++it)
{
// TODO:
}
return s;
}

QgsSymbolLayerV2List QgsSymbolV2::cloneLayers() const
Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgssymbolv2.h
Expand Up @@ -64,7 +64,7 @@ class QgsSymbolV2

QImage bigSymbolPreviewImage();

void dump();
QString dump();

virtual QgsSymbolV2* clone() const = 0;

Expand Down
39 changes: 28 additions & 11 deletions src/gui/symbology-ng/qgsrendererv2propertiesdialog.cpp
Expand Up @@ -47,11 +47,14 @@ QgsRendererV2PropertiesDialog::QgsRendererV2PropertiesDialog(QgsVectorLayer* lay
labels << "Value" << "Label";
m->setHorizontalHeaderLabels(labels);
viewCategories->setModel(m);


mCategorizedSymbol = createDefaultSymbol();

connect(cboCategorizedColumn, SIGNAL(currentIndexChanged(int)), this, SLOT(categoryColumnChanged()));

connect(viewCategories, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(categoriesDoubleClicked(const QModelIndex &)));

connect(btnChangeCategorizedSymbol, SIGNAL(clicked()), this, SLOT(changeCategorizedSymbol()));
connect(btnAddCategories, SIGNAL(clicked()), this, SLOT(addCategories()));
connect(btnDeleteCategory, SIGNAL(clicked()), this, SLOT(deleteCategory()));
connect(btnDeleteAllCategories, SIGNAL(clicked()), this, SLOT(deleteAllCategories()));
Expand Down Expand Up @@ -144,7 +147,8 @@ void QgsRendererV2PropertiesDialog::updateUiFromRenderer()
radCategorized->setChecked(true);

stackedWidget->setCurrentWidget(pageCategorized);

updateCategorizedSymbolIcon();

{
int idx = rendererCategorized()->attributeIndex();
cboCategorizedColumn->setCurrentIndex(idx >= 0 ? idx : 0);
Expand Down Expand Up @@ -201,6 +205,21 @@ QgsSymbolV2* QgsRendererV2PropertiesDialog::createDefaultSymbol()
}
}

void QgsRendererV2PropertiesDialog::changeCategorizedSymbol()
{
QgsSymbolV2SelectorDialog dlg(mCategorizedSymbol, mStyle, this);
if (!dlg.exec())
return;

updateCategorizedSymbolIcon();
}

void QgsRendererV2PropertiesDialog::updateCategorizedSymbolIcon()
{
QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon(mCategorizedSymbol, btnChangeCategorizedSymbol->iconSize());
btnChangeCategorizedSymbol->setIcon(icon);
}

void QgsRendererV2PropertiesDialog::populateCategories()
{
QStandardItemModel* m = qobject_cast<QStandardItemModel*>(viewCategories->model());
Expand Down Expand Up @@ -310,16 +329,10 @@ void QgsRendererV2PropertiesDialog::addCategories()
//if (!dlg.exec())
// return;

QgsSymbolV2* newSymbol = createDefaultSymbol();
QgsSymbolV2SelectorDialog dlg(newSymbol, mStyle, this);
if (!dlg.exec())
{
delete newSymbol;
return;
}
QgsVectorColorRampV2* ramp = mStyle->colorRamp( cboCategorizedColorRamp->currentText() );

QgsCategoryList cats;
::createCategories(cats, unique_vals, newSymbol, new QgsVectorGradientColorRampV2()); // dlg.activeRamp());
::createCategories(cats, unique_vals, mCategorizedSymbol, ramp );

// TODO: if not all categories are desired, delete some!
/*
Expand Down Expand Up @@ -393,11 +406,15 @@ void QgsRendererV2PropertiesDialog::populateColorRamps()
{
QSize rampIconSize(50,16);
cboGraduatedColorRamp->setIconSize(rampIconSize);
cboCategorizedColorRamp->setIconSize(rampIconSize);

QStringList rampNames = mStyle->colorRampNames();
for (QStringList::iterator it = rampNames.begin(); it != rampNames.end(); ++it)
{
QgsVectorColorRampV2* ramp = mStyle->colorRamp(*it);
cboGraduatedColorRamp->addItem( QgsSymbolLayerV2Utils::colorRampPreviewIcon(ramp, rampIconSize), *it);
QIcon icon = QgsSymbolLayerV2Utils::colorRampPreviewIcon(ramp, rampIconSize);
cboGraduatedColorRamp->addItem(icon, *it);
cboCategorizedColorRamp->addItem(icon, *it);
delete ramp;
}
}
Expand Down

0 comments on commit 333842b

Please sign in to comment.