Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 committed Jul 4, 2009
1 parent dfd7490 commit 2429be4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/plugins/labeling/labeling.cpp
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/labeling/labelinggui.cpp
Expand Up @@ -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
Expand Down
58 changes: 34 additions & 24 deletions src/plugins/labeling/pallabeling.cpp
Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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);
}


Expand Down Expand Up @@ -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++)
{
Expand All @@ -180,7 +204,7 @@ LayerSettings PalLabeling::layer(QString layerId)
return mLayers.at(i);
}
}
return LayerSettings();
return mInvalidLayer;
}


Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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 )
{
Expand Down
11 changes: 8 additions & 3 deletions src/plugins/labeling/pallabeling.h
Expand Up @@ -4,6 +4,7 @@
class QPainter;
class QgsMapRenderer;
class QgsRectangle;
class QgsCoordinateTransform;

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

enum Placement
Expand All @@ -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);
Expand All @@ -60,7 +62,8 @@ class LayerSettings
QFontMetrics* fontMetrics;
int fontBaseline;
const QgsMapToPixel* xform;
QgsPoint ptZero;
const QgsCoordinateTransform* ct;
QgsPoint ptZero, ptOne;
QList<MyLabel*> geometries;
};

Expand All @@ -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);
Expand All @@ -110,6 +113,8 @@ class PalLabeling

protected:
QList<LayerSettings> mLayers;
LayerSettings mInvalidLayer;

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

0 comments on commit 2429be4

Please sign in to comment.