void QgsMapToolChangeLabelProperties::canvasReleaseEvent( QMouseEvent *e ) { Q_UNUSED( e ); QgsVectorLayer* vlayer = currentLayer(); if ( mLabelRubberBand && mCanvas && vlayer ) { QString labeltext = QString(); // NULL QString signifies no expression bool settingsOk; QgsPalLayerSettings& labelSettings = currentLabelSettings( &settingsOk ); if ( settingsOk && labelSettings.isExpression ) { labeltext = mCurrentLabelPos.labelText; } QgsLabelPropertyDialog d( mCurrentLabelPos.layerID, mCurrentLabelPos.featureId, mCurrentLabelPos.labelFont, labeltext, 0 ); if ( d.exec() == QDialog::Accepted ) { const QgsAttributeMap& changes = d.changedProperties(); if ( changes.size() > 0 ) { vlayer->beginEditCommand( tr( "Changed properties for label" ) + QString( " '%1'" ).arg( currentLabelText( 24 ) ) ); QgsAttributeMap::const_iterator changeIt = changes.constBegin(); for ( ; changeIt != changes.constEnd(); ++changeIt ) { vlayer->changeAttributeValue( mCurrentLabelPos.featureId, changeIt.key(), changeIt.value() ); } vlayer->endEditCommand(); mCanvas->refresh(); } } deleteRubberBands(); } }
void QgsMapToolLabel::currentAlignment( QString& hali, QString& vali ) { hali = "Left"; vali = "Bottom"; QgsFeature f; if ( !currentFeature( f ) ) { return; } const QgsAttributeMap& featureAttributes = f.attributeMap(); bool settingsOk; QgsPalLayerSettings& labelSettings = currentLabelSettings( &settingsOk ); if ( settingsOk ) { QMap< QgsPalLayerSettings::DataDefinedProperties, int > ddProperties = labelSettings.dataDefinedProperties; QMap< QgsPalLayerSettings::DataDefinedProperties, int >::const_iterator haliIter = ddProperties.find( QgsPalLayerSettings::Hali ); if ( haliIter != ddProperties.constEnd() ) { hali = featureAttributes[*haliIter].toString(); } QMap< QgsPalLayerSettings::DataDefinedProperties, int >::const_iterator valiIter = ddProperties.find( QgsPalLayerSettings::Vali ); if ( valiIter != ddProperties.constEnd() ) { vali = featureAttributes[*valiIter].toString(); } } }
bool QgsMapToolLabel::preserveRotation() { bool labelSettingsOk; QgsPalLayerSettings& labelSettings = currentLabelSettings( &labelSettingsOk ); if ( labelSettingsOk ) { return labelSettings.preserveRotation; } return true; // default, so there is no accidental data loss }
QString QgsMapToolLabel::currentLabelText( int trunc ) { bool settingsOk; QgsPalLayerSettings& labelSettings = currentLabelSettings( &settingsOk ); if ( !settingsOk ) { return ""; } if ( labelSettings.isExpression ) { QString labelText = mCurrentLabelPos.labelText; if ( trunc > 0 && labelText.length() > trunc ) { labelText.truncate( trunc ); labelText += "..."; } return labelText; } else { QgsVectorLayer* vlayer = currentLayer(); if ( !vlayer ) { return ""; } QString labelField = vlayer->customProperty( "labeling/fieldName" ).toString(); if ( !labelField.isEmpty() ) { int labelFieldId = vlayer->fieldNameIndex( labelField ); QgsFeature f; if ( vlayer->getFeatures( QgsFeatureRequest().setFilterFid( mCurrentLabelPos.featureId ).setFlags( QgsFeatureRequest::NoGeometry ) ).nextFeature( f ) ) { QString labelText = f.attribute( labelFieldId ).toString(); if ( trunc > 0 && labelText.length() > trunc ) { labelText.truncate( trunc ); labelText += "..."; } return labelText; } } } return ""; }
QFont QgsMapToolLabel::labelFontCurrentFeature() { QFont font; QgsVectorLayer* vlayer = currentLayer(); bool labelSettingsOk; QgsPalLayerSettings& labelSettings = currentLabelSettings( &labelSettingsOk ); if ( labelSettingsOk && vlayer ) { font = labelSettings.textFont; QgsFeature f; if ( vlayer->getFeatures( QgsFeatureRequest().setFilterFid( mCurrentLabelPos.featureId ).setFlags( QgsFeatureRequest::NoGeometry ) ).nextFeature( f ) ) { //size int sizeIndx = dataDefinedColumnIndex( QgsPalLayerSettings::Size, vlayer ); if ( sizeIndx != -1 ) { if ( labelSettings.fontSizeInMapUnits ) { font.setPixelSize( labelSettings.sizeToPixel( f.attribute( sizeIndx ).toDouble(), QgsRenderContext(), QgsPalLayerSettings::MapUnits, true ) ); } else { font.setPointSizeF( f.attribute( sizeIndx ).toDouble() ); } } //family int fmIndx = dataDefinedColumnIndex( QgsPalLayerSettings::Family, vlayer ); if ( fmIndx != -1 ) { font.setFamily( f.attribute( fmIndx ).toString() ); } //underline int ulIndx = dataDefinedColumnIndex( QgsPalLayerSettings::Underline, vlayer ); if ( ulIndx != -1 ) { font.setUnderline( f.attribute( ulIndx ).toBool() ); } //strikeout int soIndx = dataDefinedColumnIndex( QgsPalLayerSettings::Strikeout, vlayer ); if ( soIndx != -1 ) { font.setStrikeOut( f.attribute( soIndx ).toBool() ); } //bold int boIndx = dataDefinedColumnIndex( QgsPalLayerSettings::Bold, vlayer ); if ( boIndx != -1 ) { font.setBold( f.attribute( boIndx ).toBool() ); } //italic int itIndx = dataDefinedColumnIndex( QgsPalLayerSettings::Italic, vlayer ); if ( itIndx != -1 ) { font.setItalic( f.attribute( itIndx ).toBool() ); } // TODO: Add other font data defined values (word spacing, etc.) } } return font; }
bool QgsMapToolLabel::rotationPoint( QgsPoint& pos, bool ignoreUpsideDown ) { QVector<QgsPoint> cornerPoints = mCurrentLabelPos.cornerPoints; if ( cornerPoints.size() < 4 ) { return false; } if ( mCurrentLabelPos.upsideDown && !ignoreUpsideDown ) { pos = mCurrentLabelPos.cornerPoints.at( 2 ); } else { pos = mCurrentLabelPos.cornerPoints.at( 0 ); } //alignment always center/center and rotation 0 for diagrams if ( mCurrentLabelPos.isDiagram ) { pos.setX( pos.x() + mCurrentLabelPos.labelRect.width() / 2.0 ); pos.setY( pos.y() + mCurrentLabelPos.labelRect.height() / 2.0 ); return true; } //adapt pos depending on data defined alignment QString haliString, valiString; currentAlignment( haliString, valiString ); if ( !mCurrentLabelPos.isPinned ) { haliString = "Center"; valiString = "Half"; } QFont labelFont = labelFontCurrentFeature(); QFontMetricsF labelFontMetrics( labelFont ); //label text? QString labelText = currentLabelText(); bool labelSettingsOk; QgsPalLayerSettings& labelSettings = currentLabelSettings( &labelSettingsOk ); if ( !labelSettingsOk ) { return false; } double labelSizeX, labelSizeY; labelSettings.calculateLabelSize( &labelFontMetrics, labelText, labelSizeX, labelSizeY ); double xdiff = 0; double ydiff = 0; if ( haliString.compare( "Center", Qt::CaseInsensitive ) == 0 ) { xdiff = labelSizeX / 2.0; } else if ( haliString.compare( "Right", Qt::CaseInsensitive ) == 0 ) { xdiff = labelSizeX; } if ( valiString.compare( "Top", Qt::CaseInsensitive ) == 0 || valiString.compare( "Cap", Qt::CaseInsensitive ) == 0 ) { ydiff = labelSizeY; } else { double descentRatio = 1 / labelFontMetrics.ascent() / labelFontMetrics.height(); if ( valiString.compare( "Base", Qt::CaseInsensitive ) == 0 ) { ydiff = labelSizeY * descentRatio; } else if ( valiString.compare( "Half", Qt::CaseInsensitive ) == 0 ) { ydiff = labelSizeY * descentRatio; ydiff = labelSizeY * 0.5 * ( 1 - descentRatio ); } } double angle = mCurrentLabelPos.rotation; double xd = xdiff * cos( angle ) - ydiff * sin( angle ); double yd = xdiff * sin( angle ) + ydiff * cos( angle ); if ( mCurrentLabelPos.upsideDown && !ignoreUpsideDown ) { pos.setX( pos.x() - xd ); pos.setY( pos.y() - yd ); } else { pos.setX( pos.x() + xd ); pos.setY( pos.y() + yd ); } return true; }
QFont QgsMapToolLabel::labelFontCurrentFeature() { QFont font; QgsVectorLayer* vlayer = currentLayer(); bool labelSettingsOk; QgsPalLayerSettings& layerSettings = currentLabelSettings( &labelSettingsOk ); if ( labelSettingsOk && vlayer ) { font = layerSettings.textFont; QgsFeature f; if ( vlayer->featureAtId( mCurrentLabelPos.featureId, f, false, true ) ) { const QgsAttributeMap& attributes = f.attributeMap(); QMap< QgsPalLayerSettings::DataDefinedProperties, int > ddProperties = layerSettings.dataDefinedProperties; //size QMap< QgsPalLayerSettings::DataDefinedProperties, int >::const_iterator sizeIt = ddProperties.find( QgsPalLayerSettings::Size ); if ( sizeIt != ddProperties.constEnd() ) { if ( layerSettings.fontSizeInMapUnits ) { font.setPixelSize( layerSettings.sizeToPixel( attributes[*sizeIt].toDouble(), QgsRenderContext() ) ); } else { font.setPointSizeF( attributes[*sizeIt].toDouble() ); } } //family QMap< QgsPalLayerSettings::DataDefinedProperties, int >::const_iterator familyIt = ddProperties.find( QgsPalLayerSettings::Family ); if ( familyIt != ddProperties.constEnd() ) { font.setFamily( attributes[*sizeIt].toString() ); } //underline QMap< QgsPalLayerSettings::DataDefinedProperties, int >::const_iterator underlineIt = ddProperties.find( QgsPalLayerSettings::Underline ); if ( familyIt != ddProperties.constEnd() ) { font.setUnderline( attributes[*underlineIt].toBool() ); } //strikeout QMap< QgsPalLayerSettings::DataDefinedProperties, int >::const_iterator strikeoutIt = ddProperties.find( QgsPalLayerSettings::Strikeout ); if ( strikeoutIt != ddProperties.constEnd() ) { font.setStrikeOut( attributes[*strikeoutIt].toBool() ); } //bold QMap< QgsPalLayerSettings::DataDefinedProperties, int >::const_iterator boldIt = ddProperties.find( QgsPalLayerSettings::Bold ); if ( boldIt != ddProperties.constEnd() ) { font.setBold( attributes[*boldIt].toBool() ); } //italic QMap< QgsPalLayerSettings::DataDefinedProperties, int >::const_iterator italicIt = ddProperties.find( QgsPalLayerSettings::Italic ); if ( italicIt != ddProperties.constEnd() ) { font.setItalic( attributes[*italicIt].toBool() ); } } } return font; }