Skip to content

Commit

Permalink
[FEATURE] Fix #6482, options for showing upside-down labels
Browse files Browse the repository at this point in the history
- Adv Labeling option to 'Show upside-down labels': never, when rotation defined, or always
- 'Never' (default) option is same as before, labels with 90 <= angle < 270 are turned so their text is always upright
- 'When rotation defined' option shows upside-down labels if their rotation is layer- or data-defined (dynamic labels are turned upright)
- 'Always' option shows upside-down labels at layer- or data-defined rotations and for dynamic labels
  • Loading branch information
dakcarto committed Oct 10, 2012
1 parent f262caa commit e125e98
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 119 deletions.
8 changes: 8 additions & 0 deletions python/core/qgspallabeling.sip
Expand Up @@ -27,6 +27,13 @@ class QgsPalLayerSettings
MapOrientation = 8
};

enum UpsideDownLabels
{
Upright, // upside-down labels (90 <= angle < 270) are shown upright
ShowDefined, // show upside down when rotation is layer- or data-defined
ShowAll // show upside down for all labels, including dynamic ones
};

// increment iterator in _writeDataDefinedPropertyMap() when adding more
enum DataDefinedProperties
{
Expand Down Expand Up @@ -105,6 +112,7 @@ class QgsPalLayerSettings
// Adds '<' or '>' to the label string pointing to the direction of the line / polygon ring
// Works only if Placement == Line
bool addDirectionSymbol;
unsigned int upsidedownLabels; // whether, or how, to show upsidedown labels
bool fontSizeInMapUnits; //true if font size is in map units (otherwise in points)
bool bufferSizeInMapUnits; //true if buffer is in map units (otherwise in mm)
bool labelOffsetInMapUnits; //true if label offset is in map units (otherwise in mm)
Expand Down
30 changes: 30 additions & 0 deletions src/app/qgslabelinggui.cpp
Expand Up @@ -173,6 +173,24 @@ QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsM
chkMergeLines->setChecked( lyr.mergeLines );
mMinSizeSpinBox->setValue( lyr.minFeatureSize );
chkAddDirectionSymbol->setChecked( lyr.addDirectionSymbol );

// upside-down labels
switch ( lyr.upsidedownLabels )
{
case QgsPalLayerSettings::Upright:
mUpsidedownRadioOff->setChecked( true );
break;
case QgsPalLayerSettings::ShowDefined:
mUpsidedownRadioDefined->setChecked( true );
break;
case QgsPalLayerSettings::ShowAll:
mUpsidedownRadioAll->setChecked( true );
break;
default:
mUpsidedownRadioOff->setChecked( true );
break;
}

wrapCharacterEdit->setText( lyr.wrapChar );
chkPreserveRotation->setChecked( lyr.preserveRotation );

Expand Down Expand Up @@ -428,6 +446,18 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
{
lyr.addDirectionSymbol = false;
}
if ( mUpsidedownRadioOff->isChecked() )
{
lyr.upsidedownLabels = QgsPalLayerSettings::Upright;
}
else if ( mUpsidedownRadioDefined->isChecked() )
{
lyr.upsidedownLabels = QgsPalLayerSettings::ShowDefined;
}
else if ( mUpsidedownRadioAll->isChecked() )
{
lyr.upsidedownLabels = QgsPalLayerSettings::ShowAll;
}
lyr.minFeatureSize = mMinSizeSpinBox->value();
lyr.fontSizeInMapUnits = ( mFontSizeUnitComboBox->currentIndex() == 1 );
lyr.wrapChar = wrapCharacterEdit->text();
Expand Down
4 changes: 4 additions & 0 deletions src/core/pal/feature.h
Expand Up @@ -276,6 +276,10 @@ namespace pal
double getLabelDistance() const { return f->distlabel; }
void setLabelInfo( LabelInfo* info ) { f->labelInfo = info; }

bool getFixedRotation() { return f->fixedRotation; }
double getLabelAngle() { return f->fixedAngle; }
bool getFixedPosition() { return f->fixedPos; }

int getNumSelfObstacles() const { return nbHoles; }
PointSet* getSelfObstacle( int i ) { return holes[i]; }

Expand Down
59 changes: 42 additions & 17 deletions src/core/pal/labelposition.cpp
Expand Up @@ -93,30 +93,55 @@ namespace pal
if ( feature->getLayer()->getArrangement() != P_CURVED &&
this->alpha > M_PI / 2 && this->alpha <= 3*M_PI / 2 )
{
tx = x[0];
ty = y[0];
bool uprightLabel = false;

x[0] = x[2];
y[0] = y[2];
switch ( feature->getLayer()->getUpsidedownLabels() )
{
case Layer::Upright:
uprightLabel = true;
break;
case Layer::ShowDefined:
// upright only dynamic labels
if ( !feature->getFixedRotation() || ( !feature->getFixedPosition() && feature->getLabelAngle() == 0.0 ) )
{
uprightLabel = true;
}
break;
case Layer::ShowAll:
break;
default:
uprightLabel = true;
}

if ( uprightLabel )
{
tx = x[0];
ty = y[0];

x[0] = x[2];
y[0] = y[2];

x[2] = tx;
y[2] = ty;
x[2] = tx;
y[2] = ty;

tx = x[1];
ty = y[1];
tx = x[1];
ty = y[1];

x[1] = x[3];
y[1] = y[3];
x[1] = x[3];
y[1] = y[3];

x[3] = tx;
y[3] = ty;
x[3] = tx;
y[3] = ty;

upsideDown = true;
if ( this->alpha < M_PI )
this->alpha += M_PI;
else
this->alpha -= M_PI;

if ( this->alpha < M_PI )
this->alpha += M_PI;
else
this->alpha -= M_PI;
// labels with text shown upside down are not classified as upsideDown,
// only those whose boundary points have been inverted
upsideDown = true;
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/core/pal/layer.h
Expand Up @@ -75,6 +75,13 @@ namespace pal

public:
enum LabelMode { LabelPerFeature, LabelPerFeaturePart };
enum UpsideDownLabels
{
Upright, // upside-down labels (90 <= angle < 270) are shown upright
ShowDefined, // show upside down when rotation is layer- or data-defined
ShowAll // show upside down for all labels, including dynamic ones
};

bool getDisplayAll() const { return displayAll; }

protected:
Expand Down Expand Up @@ -106,6 +113,8 @@ namespace pal
LabelMode mode;
bool mergeLines;

UpsideDownLabels upsidedownLabels;

// indexes (spatial and id)
RTree<FeaturePart*, double, 2, double, 8, 4> *rtree;
HashTable<Feature*> *hashtable;
Expand Down Expand Up @@ -279,6 +288,9 @@ namespace pal
void setMergeConnectedLines( bool m ) { mergeLines = m; }
bool getMergeConnectedLines() const { return mergeLines; }

void setUpsidedownLabels( UpsideDownLabels ud ) { upsidedownLabels = ud; }
UpsideDownLabels getUpsidedownLabels() const { return upsidedownLabels; }

/**
* \brief register a feature in the layer
*
Expand Down
15 changes: 15 additions & 0 deletions src/core/qgspallabeling.cpp
Expand Up @@ -174,6 +174,7 @@ QgsPalLayerSettings::QgsPalLayerSettings()
vectorScaleFactor = 1.0;
rasterCompressFactor = 1.0;
addDirectionSymbol = false;
upsidedownLabels = Upright;
fontSizeInMapUnits = false;
bufferSizeInMapUnits = false;
labelOffsetInMapUnits = true;
Expand Down Expand Up @@ -221,6 +222,7 @@ QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s )
vectorScaleFactor = s.vectorScaleFactor;
rasterCompressFactor = s.rasterCompressFactor;
addDirectionSymbol = s.addDirectionSymbol;
upsidedownLabels = s.upsidedownLabels;
fontSizeInMapUnits = s.fontSizeInMapUnits;
bufferSizeInMapUnits = s.bufferSizeInMapUnits;
distInMapUnits = s.distInMapUnits;
Expand Down Expand Up @@ -400,6 +402,7 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
displayAll = layer->customProperty( "labeling/displayAll", QVariant( false ) ).toBool();
mergeLines = layer->customProperty( "labeling/mergeLines" ).toBool();
addDirectionSymbol = layer->customProperty( "labeling/addDirectionSymbol" ).toBool();
upsidedownLabels = ( UpsideDownLabels ) layer->customProperty( "labeling/upsidedownLabels", QVariant( Upright ) ).toUInt();
minFeatureSize = layer->customProperty( "labeling/minFeatureSize" ).toDouble();
fontSizeInMapUnits = layer->customProperty( "labeling/fontSizeInMapUnits" ).toBool();
bufferSizeInMapUnits = layer->customProperty( "labeling/bufferSizeInMapUnits" ).toBool();
Expand Down Expand Up @@ -458,6 +461,7 @@ void QgsPalLayerSettings::writeToLayer( QgsVectorLayer* layer )
layer->setCustomProperty( "labeling/displayAll", displayAll );
layer->setCustomProperty( "labeling/mergeLines", mergeLines );
layer->setCustomProperty( "labeling/addDirectionSymbol", addDirectionSymbol );
layer->setCustomProperty( "labeling/upsidedownLabels", ( unsigned int )upsidedownLabels );
layer->setCustomProperty( "labeling/minFeatureSize", minFeatureSize );
layer->setCustomProperty( "labeling/fontSizeInMapUnits", fontSizeInMapUnits );
layer->setCustomProperty( "labeling/bufferSizeInMapUnits", bufferSizeInMapUnits );
Expand Down Expand Up @@ -1064,6 +1068,17 @@ int QgsPalLabeling::prepareLayer( QgsVectorLayer* layer, QSet<int>& attrIndices,
// set whether adjacent lines should be merged
l->setMergeConnectedLines( lyr.mergeLines );

// set how to show upside-down labels
Layer::UpsideDownLabels upsdnlabels;
switch ( lyr.upsidedownLabels )
{
case QgsPalLayerSettings::Upright: upsdnlabels = Layer::Upright; break;
case QgsPalLayerSettings::ShowDefined: upsdnlabels = Layer::ShowDefined; break;
case QgsPalLayerSettings::ShowAll: upsdnlabels = Layer::ShowAll; break;
default: Q_ASSERT( "unsupported upside-down label setting" && 0 ); return 0;
}
l->setUpsidedownLabels( upsdnlabels );

// fix for font size in map units causing font to show pointsize at small map scales
int pixelFontSize = lyr.sizeToPixel( lyr.textFont.pointSizeF(), ctx );

Expand Down
8 changes: 8 additions & 0 deletions src/core/qgspallabeling.h
Expand Up @@ -81,6 +81,13 @@ class CORE_EXPORT QgsPalLayerSettings
MapOrientation = 8
};

enum UpsideDownLabels
{
Upright, // upside-down labels (90 <= angle < 270) are shown upright
ShowDefined, // show upside down when rotation is layer- or data-defined
ShowAll // show upside down for all labels, including dynamic ones
};

// increment iterator in _writeDataDefinedPropertyMap() when adding more
enum DataDefinedProperties
{
Expand Down Expand Up @@ -159,6 +166,7 @@ class CORE_EXPORT QgsPalLayerSettings
// Adds '<' or '>' to the label string pointing to the direction of the line / polygon ring
// Works only if Placement == Line
bool addDirectionSymbol;
unsigned int upsidedownLabels; // whether, or how, to show upsidedown labels
bool fontSizeInMapUnits; //true if font size is in map units (otherwise in points)
bool bufferSizeInMapUnits; //true if buffer is in map units (otherwise in mm)
bool labelOffsetInMapUnits; //true if label offset is in map units (otherwise in mm)
Expand Down

0 comments on commit e125e98

Please sign in to comment.