Skip to content
Permalink
Browse files

Fixed distance from feature setting, fixed labeling with on the fly p…

…rojections, fixed workaround of one segfault due copying and double release of temporary instances in LayerSettings.

git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@11021 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder
wonder committed Jul 4, 2009
1 parent dfd7490 commit 2429be4da8b7a3741a3671aa67d1e1bfb02eec88
@@ -83,7 +83,7 @@ class LabelingTool : public QgsMapTool
for (int i = 0; i < cand.count(); i++)
{
const LabelCandidate& c = cand[i];
if (c.rect.contains(pt))
if (c.rect.contains(pt)) // TODO: handle rotated candidates
{
QToolTip::showText( mCanvas->mapToGlobal(e->pos()), QString::number(c.cost), mCanvas);
break;
@@ -42,7 +42,7 @@ LabelingGui::LabelingGui( PalLabeling* lbl, QString layerId, QWidget* parent )
populatePlacementMethods();
populateFieldNames();

LayerSettings lyr = lbl->layer(layerId);
const LayerSettings& lyr = lbl->layer(layerId);
if (!lyr.layerId.isEmpty())
{
// load the labeling settings
@@ -66,14 +66,34 @@ class MyLabel : public PalGeometry
// -------------

LayerSettings::LayerSettings()
: palLayer(NULL), fontMetrics(NULL)
: palLayer(NULL), fontMetrics(NULL), ct(NULL)
{
}

LayerSettings::LayerSettings(const LayerSettings& s)
{
// copy only permanent stuff
layerId = s.layerId;
fieldName = s.fieldName;
placement = s.placement;
textFont = s.textFont;
textColor = s.textColor;
enabled = s.enabled;
priority = s.priority;
obstacle = s.obstacle;
dist = s.dist;

fontMetrics = NULL;
ct = NULL;
}


LayerSettings::~LayerSettings()
{
// pal layer is deleted internally in PAL

delete fontMetrics;
delete ct;
}

void LayerSettings::calculateLabelSize(QString text, double& labelX, double& labelY)
@@ -93,7 +113,11 @@ void LayerSettings::registerFeature(QgsFeature& f)
double labelX, labelY; // will receive label size
calculateLabelSize(labelText, labelX, labelY);

MyLabel* lbl = new MyLabel(f.id(), labelText, GEOSGeom_clone( f.geometry()->asGeos() ) );
QgsGeometry* geom = f.geometry();
if (ct != NULL) // reproject the geometry if necessary
geom->transform(*ct);

MyLabel* lbl = new MyLabel(f.id(), labelText, GEOSGeom_clone( geom->asGeos() ) );

// record the created geometry - it will be deleted at the end.
geometries.append(lbl);
@@ -103,7 +127,7 @@ void LayerSettings::registerFeature(QgsFeature& f)

// TODO: allow layer-wide feature dist in PAL...?
if (dist != 0)
palLayer->setFeatureDistlabel(lbl->strId(), dist);
palLayer->setFeatureDistlabel(lbl->strId(), fabs(ptOne.x()-ptZero.x())* dist);
}


@@ -171,7 +195,7 @@ void PalLabeling::removeLayer(QString layerId)
}
}

LayerSettings PalLabeling::layer(QString layerId)
const LayerSettings& PalLabeling::layer(QString layerId)
{
for (int i = 0; i < mLayers.count(); i++)
{
@@ -180,7 +204,7 @@ LayerSettings PalLabeling::layer(QString layerId)
return mLayers.at(i);
}
}
return LayerSettings();
return mInvalidLayer;
}


@@ -225,7 +249,12 @@ int PalLabeling::prepareLayerHook(void* context, void* layerContext, int& attrIn
lyr->fontMetrics = new QFontMetrics(lyr->textFont);
lyr->fontBaseline = lyr->fontMetrics->boundingRect("X").bottom(); // dummy text to find out how many pixels of the text are below the baseline
lyr->xform = thisClass->mMapRenderer->coordinateTransform();
if (thisClass->mMapRenderer->hasCrsTransformEnabled())
lyr->ct = new QgsCoordinateTransform( vlayer->srs(), thisClass->mMapRenderer->destinationSrs() );
else
lyr->ct = NULL;
lyr->ptZero = lyr->xform->toMapCoordinates( 0,0 );
lyr->ptOne = lyr->xform->toMapCoordinates( 1,0 );

return 1; // init successful
}
@@ -255,7 +284,6 @@ void PalLabeling::initPal()
case Falp: s = FALP; break;
}
mPal->setSearch(s);
//mPal->setSearch(FALP);

// set number of candidates generated per feature
mPal->setPointP(mCandPoint);
@@ -271,15 +299,6 @@ void PalLabeling::doLabeling(QPainter* painter, QgsRectangle extent)
QTime t;
t.start();

// make sure to delete fontmetrics otherwise it crashes inside Qt when drawing... :-(
// probably gets invalid when setting fonts in the label drawing loop
for (int i = 0; i < mLayers.count(); i++)
{
LayerSettings& lyr = mLayers[i];
delete lyr.fontMetrics;
lyr.fontMetrics = NULL;
}

// do the labeling itself
double scale = 1; // scale denominator
QgsRectangle r = extent;
@@ -289,16 +308,7 @@ void PalLabeling::doLabeling(QPainter* painter, QgsRectangle extent)
pal::Problem* problem;
try
{
//labels = mPal->labeller(scale, bbox, NULL, false);
problem = mPal->extractProblem(scale, bbox);
if ( problem )
{
// TODO: other methods
problem->chain_search();
labels = problem->getSolution(false);
}
else
labels = new std::list<Label*>();
}
catch ( std::exception& e )
{
@@ -4,6 +4,7 @@
class QPainter;
class QgsMapRenderer;
class QgsRectangle;
class QgsCoordinateTransform;

#include <QString>
#include <QFont>
@@ -27,6 +28,7 @@ class LayerSettings
{
public:
LayerSettings();
LayerSettings(const LayerSettings& s);
~LayerSettings();

enum Placement
@@ -46,7 +48,7 @@ class LayerSettings
bool enabled;
int priority; // 0 = low, 10 = high
bool obstacle; // whether it's an obstacle
double dist; // distance from the feature
double dist; // distance from the feature (in pixels)

// called from register feature hook
void calculateLabelSize(QString text, double& labelX, double& labelY);
@@ -60,7 +62,8 @@ class LayerSettings
QFontMetrics* fontMetrics;
int fontBaseline;
const QgsMapToPixel* xform;
QgsPoint ptZero;
const QgsCoordinateTransform* ct;
QgsPoint ptZero, ptOne;
QList<MyLabel*> geometries;
};

@@ -85,7 +88,7 @@ class PalLabeling

void removeLayer(QString layerId);

LayerSettings layer(QString layerId);
const LayerSettings& layer(QString layerId);

void numCandidatePositions(int& candPoint, int& candLine, int& candPolygon);
void setNumCandidatePositions(int candPoint, int candLine, int candPolygon);
@@ -110,6 +113,8 @@ class PalLabeling

protected:
QList<LayerSettings> mLayers;
LayerSettings mInvalidLayer;

QgsMapRenderer* mMapRenderer;
int mCandPoint, mCandLine, mCandPolygon;
Search mSearch;

0 comments on commit 2429be4

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