Skip to content

Commit

Permalink
Added support to put labels above, below or on the line (or any combi…
Browse files Browse the repository at this point in the history
…nation).

The orientation above/below is determined from the map orientation or line direction (left/right side).


git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@11050 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Jul 12, 2009
1 parent 5619ba7 commit 3cbacd8
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 36 deletions.
22 changes: 17 additions & 5 deletions src/core/pal/feature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,11 @@ namespace pal
double alpha;
double cost;

unsigned long flags = layer->getArrangementFlags();
if ( flags == 0 )
flags = FLAG_ON_LINE; // default flag
bool reversed = false;

//LinkedList<PointSet*> *shapes_final;

//shapes_final = new LinkedList<PointSet*>(ptrPSetCompare);
Expand Down Expand Up @@ -476,10 +481,18 @@ namespace pal
#ifdef _DEBUG_FULL_
std::cout << " Create new label" << std::endl;
#endif
if ( layer->arrangement == P_LINE_AROUND )
if ( layer->arrangement == P_LINE )
{
positions->push_back( new LabelPosition( i, bx + cos( beta ) *distlabel , by + sin( beta ) *distlabel, xrm, yrm, alpha, cost, this ) ); // Line
positions->push_back( new LabelPosition( i, bx - cos( beta ) * ( distlabel + yrm ) , by - sin( beta ) * ( distlabel + yrm ), xrm, yrm, alpha, cost, this ) ); // Line
std::cout << alpha*180/M_PI << std::endl;
if ( flags & FLAG_MAP_ORIENTATION )
reversed = ( alpha >= M_PI/2 || alpha < -M_PI/2 );

if ( (!reversed && (flags & FLAG_ABOVE_LINE)) || (reversed && (flags & FLAG_BELOW_LINE)) )
positions->push_back( new LabelPosition( i, bx + cos( beta ) *distlabel , by + sin( beta ) *distlabel, xrm, yrm, alpha, cost, this ) ); // Line
if ( (!reversed && (flags & FLAG_BELOW_LINE)) || (reversed && (flags & FLAG_ABOVE_LINE)) )
positions->push_back( new LabelPosition( i, bx - cos( beta ) * ( distlabel + yrm ) , by - sin( beta ) * ( distlabel + yrm ), xrm, yrm, alpha, cost, this ) ); // Line
if ( flags & FLAG_ON_LINE )
positions->push_back( new LabelPosition( i, bx - yrm*cos( beta ) / 2, by - yrm*sin( beta ) / 2, xrm, yrm, alpha, cost, this ) ); // Line
}
else if (layer->arrangement == P_HORIZ)
{
Expand All @@ -488,7 +501,7 @@ namespace pal
}
else
{
positions->push_back( new LabelPosition( i, bx - yrm*cos( beta ) / 2, by - yrm*sin( beta ) / 2, xrm, yrm, alpha, cost, this ) ); // Line
// an invalid arrangement?
}

l += dist;
Expand Down Expand Up @@ -846,7 +859,6 @@ namespace pal
nbp = setPositionForPoint( cx, cy, scale, lPos, delta );
break;
case P_LINE:
case P_LINE_AROUND:
nbp = setPositionForLine( scale, lPos, mapShape, delta );
break;
default:
Expand Down
6 changes: 5 additions & 1 deletion src/core/pal/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@
namespace pal
{

Layer::Layer( const char *lyrName, double min_scale, double max_scale, Arrangement arrangement, Units label_unit, double defaultPriority, bool obstacle, bool active, bool toLabel, Pal *pal ) : pal( pal ), obstacle( obstacle ), active( active ), toLabel( toLabel ), label_unit( label_unit ), min_scale( min_scale ), max_scale( max_scale ), arrangement( arrangement )
Layer::Layer( const char *lyrName, double min_scale, double max_scale, Arrangement arrangement, Units label_unit, double defaultPriority, bool obstacle, bool active, bool toLabel, Pal *pal )
: pal( pal ), obstacle( obstacle ), active( active ),
toLabel( toLabel ), label_unit( label_unit ),
min_scale( min_scale ), max_scale( max_scale ),
arrangement( arrangement ), arrangementFlags( 0 )
{

this->name = new char[strlen( lyrName ) +1];
Expand Down
6 changes: 6 additions & 0 deletions src/core/pal/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ namespace pal

Arrangement arrangement;

/** optional flags used for some placement methods */
unsigned long arrangementFlags;

// indexes (spatial and id)
RTree<Feature*, double, 2, double, 8, 4> *rtree;
HashTable<Cell<Feature*>*> *hashtable;
Expand Down Expand Up @@ -166,6 +169,9 @@ namespace pal
*/
void setArrangement( Arrangement arrangement );

unsigned long getArrangementFlags() const { return arrangementFlags; }
void setArrangementFlags( unsigned long flags ) { arrangementFlags = flags; }

/**
* \brief get units for label size
*/
Expand Down
11 changes: 9 additions & 2 deletions src/core/pal/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,20 @@ namespace pal
P_POINT_OVER, /** arranges candidates over a point (centroid for polygon)*/
P_LINE, /**< Only for lines and polygons, arranges candidates over the line or the polygon perimeter */
P_HORIZ, /**< Only for polygon, arranges candidates horizontaly */
P_FREE, /**< Only for polygon, arranges candidates with respect of polygon orientation */
P_LINE_AROUND /**< Only for lines and polygons, arranges candidates above and below the line or the polygon perimeter */
P_FREE /**< Only for polygon, arranges candidates with respect of polygon orientation */
};

/** typedef for _arrangement enumeration */
typedef enum _arrangement Arrangement;

/** enumeration line arrangement flags. Flags can be combined. */
enum LineArrangementFlags
{
FLAG_ON_LINE = 1,
FLAG_ABOVE_LINE = 2,
FLAG_BELOW_LINE = 4,
FLAG_MAP_ORIENTATION = 8
};

/**
* \brief Pal main class.
Expand Down
36 changes: 21 additions & 15 deletions src/plugins/labeling/labelinggui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,18 @@ LabelingGui::LabelingGui( PalLabeling* lbl, QString layerId, QWidget* parent )
radOverPoint->setChecked(true);
radOverCentroid->setChecked(true);
break;
case LayerSettings::AroundLine:
case LayerSettings::OnLine:
case LayerSettings::Line:
radLineParallel->setChecked(true);
radPolygonPerimeter->setChecked(true);
if ( lyr.placement == LayerSettings::AroundLine )
{
radAroundLine->setChecked(true);
spinDistLine->setValue(lyr.dist);
}

spinDistLine->setValue(lyr.dist);
chkLineAbove->setChecked( lyr.placementFlags & LayerSettings::AboveLine );
chkLineBelow->setChecked( lyr.placementFlags & LayerSettings::BelowLine );
chkLineOn->setChecked( lyr.placementFlags & LayerSettings::OnLine );
if ( lyr.placementFlags & LayerSettings::MapOrientation )
radOrientationMap->setChecked(true);
else
radOnLine->setChecked(true);
radOrientationLine->setChecked(true);
break;
case LayerSettings::Horizontal:
radPolygonHorizontal->setChecked(true);
Expand Down Expand Up @@ -167,6 +168,7 @@ LayerSettings LabelingGui::layerSettings()
lyr.fieldName = cboFieldName->currentText();

lyr.dist = 0;
lyr.placementFlags = 0;

if ( (stackedPlacement->currentWidget() == pagePoint && radAroundPoint->isChecked())
|| (stackedPlacement->currentWidget() == pagePolygon && radAroundCentroid->isChecked()) )
Expand All @@ -183,13 +185,17 @@ LayerSettings LabelingGui::layerSettings()
else if ( (stackedPlacement->currentWidget() == pageLine && radLineParallel->isChecked())
|| (stackedPlacement->currentWidget() == pagePolygon && radPolygonPerimeter->isChecked()) )
{
if (radAroundLine->isChecked())
{
lyr.placement = LayerSettings::AroundLine;
lyr.dist = spinDistLine->value();
}
else
lyr.placement = LayerSettings::OnLine;
lyr.placement = LayerSettings::Line;
lyr.dist = spinDistLine->value();
if (chkLineAbove->isChecked())
lyr.placementFlags |= LayerSettings::AboveLine;
if (chkLineBelow->isChecked())
lyr.placementFlags |= LayerSettings::BelowLine;
if (chkLineOn->isChecked())
lyr.placementFlags |= LayerSettings::OnLine;

if (radOrientationMap->isChecked())
lyr.placementFlags |= LayerSettings::MapOrientation;
}
else if ( (stackedPlacement->currentWidget() == pageLine && radLineHorizontal->isChecked())
|| (stackedPlacement->currentWidget() == pagePolygon && radPolygonHorizontal->isChecked()) )
Expand Down
49 changes: 41 additions & 8 deletions src/plugins/labeling/labelingguibase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>454</width>
<height>514</height>
<height>526</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -175,7 +175,7 @@
<item>
<widget class="QStackedWidget" name="stackedOptions">
<property name="currentIndex">
<number>0</number>
<number>1</number>
</property>
<widget class="QWidget" name="pageOptionsPoint">
<layout class="QGridLayout" name="gridLayout_2">
Expand Down Expand Up @@ -232,22 +232,57 @@
<widget class="QWidget" name="pageOptionsLine">
<layout class="QVBoxLayout" name="verticalLayout_5">
<item>
<widget class="QRadioButton" name="radOnLine">
<widget class="QCheckBox" name="chkLineAbove">
<property name="text">
<string>on line</string>
<string>above line</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radAroundLine">
<widget class="QCheckBox" name="chkLineOn">
<property name="text">
<string>on line</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="chkLineBelow">
<property name="text">
<string>around line</string>
<string>below line</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_7">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Orientation</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radOrientationMap">
<property name="text">
<string>map</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radOrientationLine">
<property name="text">
<string>line</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
Expand Down Expand Up @@ -693,8 +728,6 @@
<tabstop>radPolygonFree</tabstop>
<tabstop>spinDistPoint</tabstop>
<tabstop>spinAngle</tabstop>
<tabstop>radOnLine</tabstop>
<tabstop>radAroundLine</tabstop>
<tabstop>spinDistLine</tabstop>
<tabstop>btnChangeFont</tabstop>
<tabstop>btnTextColor</tabstop>
Expand Down
9 changes: 6 additions & 3 deletions src/plugins/labeling/pallabeling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ LayerSettings::LayerSettings(const LayerSettings& s)
layerId = s.layerId;
fieldName = s.fieldName;
placement = s.placement;
placementFlags = s.placementFlags;
textFont = s.textFont;
textColor = s.textColor;
enabled = s.enabled;
Expand Down Expand Up @@ -239,9 +240,8 @@ int PalLabeling::prepareLayerHook(void* context, void* layerContext, int& attrIn
switch (lyr->placement)
{
case LayerSettings::AroundPoint: arrangement = P_POINT; break;
case LayerSettings::OverPoint: arrangement = P_POINT_OVER; break;
case LayerSettings::OnLine: arrangement = P_LINE; break;
case LayerSettings::AroundLine: arrangement = P_LINE_AROUND; break;
case LayerSettings::OverPoint: arrangement = P_POINT_OVER; break;
case LayerSettings::Line: arrangement = P_LINE; break;
case LayerSettings::Horizontal: arrangement = P_HORIZ; break;
case LayerSettings::Free: arrangement = P_FREE; break;
}
Expand All @@ -257,6 +257,9 @@ int PalLabeling::prepareLayerHook(void* context, void* layerContext, int& attrIn

Layer* l = thisClass->mPal->addLayer(lyr->layerId.toLocal8Bit().data(), min_scale, max_scale, arrangement, METER, priority, lyr->obstacle, true, true);

if ( lyr->placementFlags )
l->setArrangementFlags( lyr->placementFlags );

// save the pal layer to our layer context (with some additional info)
lyr->palLayer = l;
lyr->fieldIndex = fldIndex;
Expand Down
12 changes: 10 additions & 2 deletions src/plugins/labeling/pallabeling.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,23 @@ class LayerSettings
{
AroundPoint, // Point / Polygon
OverPoint, // Point / Polygon
OnLine, // Line / Polygon
AroundLine, // Line / Polygon
Line, // Line / Polygon
Horizontal, // Polygon
Free // Polygon
};

enum LinePlacementFlags
{
OnLine = 1,
AboveLine = 2,
BelowLine = 4,
MapOrientation = 8
};

QString layerId;
QString fieldName;
Placement placement;
unsigned long placementFlags;
QFont textFont;
QColor textColor;
bool enabled;
Expand Down

0 comments on commit 3cbacd8

Please sign in to comment.