Skip to content

Commit 3cbacd8

Browse files
author
wonder
committed
Added support to put labels above, below or on the line (or any combination).
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
1 parent 5619ba7 commit 3cbacd8

File tree

8 files changed

+115
-36
lines changed

8 files changed

+115
-36
lines changed

src/core/pal/feature.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,11 @@ namespace pal
372372
double alpha;
373373
double cost;
374374

375+
unsigned long flags = layer->getArrangementFlags();
376+
if ( flags == 0 )
377+
flags = FLAG_ON_LINE; // default flag
378+
bool reversed = false;
379+
375380
//LinkedList<PointSet*> *shapes_final;
376381

377382
//shapes_final = new LinkedList<PointSet*>(ptrPSetCompare);
@@ -476,10 +481,18 @@ namespace pal
476481
#ifdef _DEBUG_FULL_
477482
std::cout << " Create new label" << std::endl;
478483
#endif
479-
if ( layer->arrangement == P_LINE_AROUND )
484+
if ( layer->arrangement == P_LINE )
480485
{
481-
positions->push_back( new LabelPosition( i, bx + cos( beta ) *distlabel , by + sin( beta ) *distlabel, xrm, yrm, alpha, cost, this ) ); // Line
482-
positions->push_back( new LabelPosition( i, bx - cos( beta ) * ( distlabel + yrm ) , by - sin( beta ) * ( distlabel + yrm ), xrm, yrm, alpha, cost, this ) ); // Line
486+
std::cout << alpha*180/M_PI << std::endl;
487+
if ( flags & FLAG_MAP_ORIENTATION )
488+
reversed = ( alpha >= M_PI/2 || alpha < -M_PI/2 );
489+
490+
if ( (!reversed && (flags & FLAG_ABOVE_LINE)) || (reversed && (flags & FLAG_BELOW_LINE)) )
491+
positions->push_back( new LabelPosition( i, bx + cos( beta ) *distlabel , by + sin( beta ) *distlabel, xrm, yrm, alpha, cost, this ) ); // Line
492+
if ( (!reversed && (flags & FLAG_BELOW_LINE)) || (reversed && (flags & FLAG_ABOVE_LINE)) )
493+
positions->push_back( new LabelPosition( i, bx - cos( beta ) * ( distlabel + yrm ) , by - sin( beta ) * ( distlabel + yrm ), xrm, yrm, alpha, cost, this ) ); // Line
494+
if ( flags & FLAG_ON_LINE )
495+
positions->push_back( new LabelPosition( i, bx - yrm*cos( beta ) / 2, by - yrm*sin( beta ) / 2, xrm, yrm, alpha, cost, this ) ); // Line
483496
}
484497
else if (layer->arrangement == P_HORIZ)
485498
{
@@ -488,7 +501,7 @@ namespace pal
488501
}
489502
else
490503
{
491-
positions->push_back( new LabelPosition( i, bx - yrm*cos( beta ) / 2, by - yrm*sin( beta ) / 2, xrm, yrm, alpha, cost, this ) ); // Line
504+
// an invalid arrangement?
492505
}
493506

494507
l += dist;
@@ -846,7 +859,6 @@ namespace pal
846859
nbp = setPositionForPoint( cx, cy, scale, lPos, delta );
847860
break;
848861
case P_LINE:
849-
case P_LINE_AROUND:
850862
nbp = setPositionForLine( scale, lPos, mapShape, delta );
851863
break;
852864
default:

src/core/pal/layer.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@
5757
namespace pal
5858
{
5959

60-
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 )
60+
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 )
61+
: pal( pal ), obstacle( obstacle ), active( active ),
62+
toLabel( toLabel ), label_unit( label_unit ),
63+
min_scale( min_scale ), max_scale( max_scale ),
64+
arrangement( arrangement ), arrangementFlags( 0 )
6165
{
6266

6367
this->name = new char[strlen( lyrName ) +1];

src/core/pal/layer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ namespace pal
9595

9696
Arrangement arrangement;
9797

98+
/** optional flags used for some placement methods */
99+
unsigned long arrangementFlags;
100+
98101
// indexes (spatial and id)
99102
RTree<Feature*, double, 2, double, 8, 4> *rtree;
100103
HashTable<Cell<Feature*>*> *hashtable;
@@ -166,6 +169,9 @@ namespace pal
166169
*/
167170
void setArrangement( Arrangement arrangement );
168171

172+
unsigned long getArrangementFlags() const { return arrangementFlags; }
173+
void setArrangementFlags( unsigned long flags ) { arrangementFlags = flags; }
174+
169175
/**
170176
* \brief get units for label size
171177
*/

src/core/pal/pal.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,20 @@ namespace pal
101101
P_POINT_OVER, /** arranges candidates over a point (centroid for polygon)*/
102102
P_LINE, /**< Only for lines and polygons, arranges candidates over the line or the polygon perimeter */
103103
P_HORIZ, /**< Only for polygon, arranges candidates horizontaly */
104-
P_FREE, /**< Only for polygon, arranges candidates with respect of polygon orientation */
105-
P_LINE_AROUND /**< Only for lines and polygons, arranges candidates above and below the line or the polygon perimeter */
104+
P_FREE /**< Only for polygon, arranges candidates with respect of polygon orientation */
106105
};
107106

108107
/** typedef for _arrangement enumeration */
109108
typedef enum _arrangement Arrangement;
110109

110+
/** enumeration line arrangement flags. Flags can be combined. */
111+
enum LineArrangementFlags
112+
{
113+
FLAG_ON_LINE = 1,
114+
FLAG_ABOVE_LINE = 2,
115+
FLAG_BELOW_LINE = 4,
116+
FLAG_MAP_ORIENTATION = 8
117+
};
111118

112119
/**
113120
* \brief Pal main class.

src/plugins/labeling/labelinggui.cpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,18 @@ LabelingGui::LabelingGui( PalLabeling* lbl, QString layerId, QWidget* parent )
8080
radOverPoint->setChecked(true);
8181
radOverCentroid->setChecked(true);
8282
break;
83-
case LayerSettings::AroundLine:
84-
case LayerSettings::OnLine:
83+
case LayerSettings::Line:
8584
radLineParallel->setChecked(true);
8685
radPolygonPerimeter->setChecked(true);
87-
if ( lyr.placement == LayerSettings::AroundLine )
88-
{
89-
radAroundLine->setChecked(true);
90-
spinDistLine->setValue(lyr.dist);
91-
}
86+
87+
spinDistLine->setValue(lyr.dist);
88+
chkLineAbove->setChecked( lyr.placementFlags & LayerSettings::AboveLine );
89+
chkLineBelow->setChecked( lyr.placementFlags & LayerSettings::BelowLine );
90+
chkLineOn->setChecked( lyr.placementFlags & LayerSettings::OnLine );
91+
if ( lyr.placementFlags & LayerSettings::MapOrientation )
92+
radOrientationMap->setChecked(true);
9293
else
93-
radOnLine->setChecked(true);
94+
radOrientationLine->setChecked(true);
9495
break;
9596
case LayerSettings::Horizontal:
9697
radPolygonHorizontal->setChecked(true);
@@ -167,6 +168,7 @@ LayerSettings LabelingGui::layerSettings()
167168
lyr.fieldName = cboFieldName->currentText();
168169

169170
lyr.dist = 0;
171+
lyr.placementFlags = 0;
170172

171173
if ( (stackedPlacement->currentWidget() == pagePoint && radAroundPoint->isChecked())
172174
|| (stackedPlacement->currentWidget() == pagePolygon && radAroundCentroid->isChecked()) )
@@ -183,13 +185,17 @@ LayerSettings LabelingGui::layerSettings()
183185
else if ( (stackedPlacement->currentWidget() == pageLine && radLineParallel->isChecked())
184186
|| (stackedPlacement->currentWidget() == pagePolygon && radPolygonPerimeter->isChecked()) )
185187
{
186-
if (radAroundLine->isChecked())
187-
{
188-
lyr.placement = LayerSettings::AroundLine;
189-
lyr.dist = spinDistLine->value();
190-
}
191-
else
192-
lyr.placement = LayerSettings::OnLine;
188+
lyr.placement = LayerSettings::Line;
189+
lyr.dist = spinDistLine->value();
190+
if (chkLineAbove->isChecked())
191+
lyr.placementFlags |= LayerSettings::AboveLine;
192+
if (chkLineBelow->isChecked())
193+
lyr.placementFlags |= LayerSettings::BelowLine;
194+
if (chkLineOn->isChecked())
195+
lyr.placementFlags |= LayerSettings::OnLine;
196+
197+
if (radOrientationMap->isChecked())
198+
lyr.placementFlags |= LayerSettings::MapOrientation;
193199
}
194200
else if ( (stackedPlacement->currentWidget() == pageLine && radLineHorizontal->isChecked())
195201
|| (stackedPlacement->currentWidget() == pagePolygon && radPolygonHorizontal->isChecked()) )

src/plugins/labeling/labelingguibase.ui

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<x>0</x>
88
<y>0</y>
99
<width>454</width>
10-
<height>514</height>
10+
<height>526</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
@@ -175,7 +175,7 @@
175175
<item>
176176
<widget class="QStackedWidget" name="stackedOptions">
177177
<property name="currentIndex">
178-
<number>0</number>
178+
<number>1</number>
179179
</property>
180180
<widget class="QWidget" name="pageOptionsPoint">
181181
<layout class="QGridLayout" name="gridLayout_2">
@@ -232,22 +232,57 @@
232232
<widget class="QWidget" name="pageOptionsLine">
233233
<layout class="QVBoxLayout" name="verticalLayout_5">
234234
<item>
235-
<widget class="QRadioButton" name="radOnLine">
235+
<widget class="QCheckBox" name="chkLineAbove">
236236
<property name="text">
237-
<string>on line</string>
237+
<string>above line</string>
238238
</property>
239239
<property name="checked">
240240
<bool>true</bool>
241241
</property>
242242
</widget>
243243
</item>
244244
<item>
245-
<widget class="QRadioButton" name="radAroundLine">
245+
<widget class="QCheckBox" name="chkLineOn">
246+
<property name="text">
247+
<string>on line</string>
248+
</property>
249+
</widget>
250+
</item>
251+
<item>
252+
<widget class="QCheckBox" name="chkLineBelow">
246253
<property name="text">
247-
<string>around line</string>
254+
<string>below line</string>
248255
</property>
249256
</widget>
250257
</item>
258+
<item>
259+
<layout class="QHBoxLayout" name="horizontalLayout_7">
260+
<item>
261+
<widget class="QLabel" name="label">
262+
<property name="text">
263+
<string>Orientation</string>
264+
</property>
265+
</widget>
266+
</item>
267+
<item>
268+
<widget class="QRadioButton" name="radOrientationMap">
269+
<property name="text">
270+
<string>map</string>
271+
</property>
272+
<property name="checked">
273+
<bool>true</bool>
274+
</property>
275+
</widget>
276+
</item>
277+
<item>
278+
<widget class="QRadioButton" name="radOrientationLine">
279+
<property name="text">
280+
<string>line</string>
281+
</property>
282+
</widget>
283+
</item>
284+
</layout>
285+
</item>
251286
<item>
252287
<layout class="QHBoxLayout" name="horizontalLayout_3">
253288
<item>
@@ -693,8 +728,6 @@
693728
<tabstop>radPolygonFree</tabstop>
694729
<tabstop>spinDistPoint</tabstop>
695730
<tabstop>spinAngle</tabstop>
696-
<tabstop>radOnLine</tabstop>
697-
<tabstop>radAroundLine</tabstop>
698731
<tabstop>spinDistLine</tabstop>
699732
<tabstop>btnChangeFont</tabstop>
700733
<tabstop>btnTextColor</tabstop>

src/plugins/labeling/pallabeling.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ LayerSettings::LayerSettings(const LayerSettings& s)
7777
layerId = s.layerId;
7878
fieldName = s.fieldName;
7979
placement = s.placement;
80+
placementFlags = s.placementFlags;
8081
textFont = s.textFont;
8182
textColor = s.textColor;
8283
enabled = s.enabled;
@@ -239,9 +240,8 @@ int PalLabeling::prepareLayerHook(void* context, void* layerContext, int& attrIn
239240
switch (lyr->placement)
240241
{
241242
case LayerSettings::AroundPoint: arrangement = P_POINT; break;
242-
case LayerSettings::OverPoint: arrangement = P_POINT_OVER; break;
243-
case LayerSettings::OnLine: arrangement = P_LINE; break;
244-
case LayerSettings::AroundLine: arrangement = P_LINE_AROUND; break;
243+
case LayerSettings::OverPoint: arrangement = P_POINT_OVER; break;
244+
case LayerSettings::Line: arrangement = P_LINE; break;
245245
case LayerSettings::Horizontal: arrangement = P_HORIZ; break;
246246
case LayerSettings::Free: arrangement = P_FREE; break;
247247
}
@@ -257,6 +257,9 @@ int PalLabeling::prepareLayerHook(void* context, void* layerContext, int& attrIn
257257

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

260+
if ( lyr->placementFlags )
261+
l->setArrangementFlags( lyr->placementFlags );
262+
260263
// save the pal layer to our layer context (with some additional info)
261264
lyr->palLayer = l;
262265
lyr->fieldIndex = fldIndex;

src/plugins/labeling/pallabeling.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,23 @@ class LayerSettings
3535
{
3636
AroundPoint, // Point / Polygon
3737
OverPoint, // Point / Polygon
38-
OnLine, // Line / Polygon
39-
AroundLine, // Line / Polygon
38+
Line, // Line / Polygon
4039
Horizontal, // Polygon
4140
Free // Polygon
4241
};
4342

43+
enum LinePlacementFlags
44+
{
45+
OnLine = 1,
46+
AboveLine = 2,
47+
BelowLine = 4,
48+
MapOrientation = 8
49+
};
50+
4451
QString layerId;
4552
QString fieldName;
4653
Placement placement;
54+
unsigned long placementFlags;
4755
QFont textFont;
4856
QColor textColor;
4957
bool enabled;

0 commit comments

Comments
 (0)