bool QgsMapToolLabel::currentFeature( QgsFeature& f, bool fetchGeom ) { QgsVectorLayer* vlayer = currentLayer(); if ( !vlayer ) { return false; } return vlayer->featureAtId( mCurrentLabelPos.featureId, f, fetchGeom, true ); }
QString QgsMapToolLabel::currentLabelText() { 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->featureAtId( mCurrentLabelPos.featureId, f, false, true ) ) { return f.attributeMap()[labelFieldId].toString(); } } return ""; }
void QgsLabelPropertyDialog::init( const QString& layerId, int featureId ) { if ( !mMapRenderer ) { return; } //get feature attributes QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) ); if ( !vlayer ) { return; } QgsFeature f; if ( !vlayer->featureAtId( featureId, f, false, true ) ) { return; } const QgsAttributeMap& attributeValues = f.attributeMap(); //get layerproperties. Problem: only for pallabeling... QgsPalLabeling* lbl = dynamic_cast<QgsPalLabeling*>( mMapRenderer->labelingEngine() ); if ( !lbl ) { return; } blockElementSignals( true ); //get label field and fill line edit QString labelFieldName = vlayer->customProperty( "labeling/fieldName" ).toString(); if ( !labelFieldName.isEmpty() ) { mCurrentLabelField = vlayer->fieldNameIndex( labelFieldName ); mLabelTextLineEdit->setText( attributeValues[mCurrentLabelField].toString() ); const QgsFieldMap& layerFields = vlayer->pendingFields(); switch ( layerFields[mCurrentLabelField].type() ) { case QVariant::Double: mLabelTextLineEdit->setValidator( new QDoubleValidator( this ) ); break; case QVariant::Int: case QVariant::UInt: case QVariant::LongLong: mLabelTextLineEdit->setValidator( new QIntValidator( this ) ); break; default: break; } } //get attributes of the feature and fill data defined values QgsPalLayerSettings& layerSettings = lbl->layer( layerId ); mLabelFont = layerSettings.textFont; //set all the gui elements to the default values mFontSizeSpinBox->setValue( layerSettings.textFont.pointSizeF() ); mBufferColorButton->setColor( layerSettings.textColor ); mLabelDistanceSpinBox->setValue( layerSettings.dist ); mBufferSizeSpinBox->setValue( layerSettings.bufferSize ); mMinScaleSpinBox->setValue( layerSettings.scaleMin ); mMaxScaleSpinBox->setValue( layerSettings.scaleMax ); mHaliComboBox->setCurrentIndex( mHaliComboBox->findText( "Left" ) ); mValiComboBox->setCurrentIndex( mValiComboBox->findText( "Bottom" ) ); disableGuiElements(); mDataDefinedProperties = layerSettings.dataDefinedProperties; QMap< QgsPalLayerSettings::DataDefinedProperties, int >::const_iterator propIt = mDataDefinedProperties.constBegin(); for ( ; propIt != mDataDefinedProperties.constEnd(); ++propIt ) { switch ( propIt.key() ) { case QgsPalLayerSettings::Show: mShowLabelChkbx->setEnabled( true ); mShowLabelChkbx->setChecked( attributeValues[propIt.value()].toInt() != 0 ); break; case QgsPalLayerSettings::AlwaysShow: mAlwaysShowChkbx->setEnabled( true ); mAlwaysShowChkbx->setChecked( attributeValues[propIt.value()].toBool() ); break; case QgsPalLayerSettings::MinScale: mMinScaleSpinBox->setEnabled( true ); mMinScaleSpinBox->setValue( attributeValues[propIt.value()].toInt() ); break; case QgsPalLayerSettings::MaxScale: mMaxScaleSpinBox->setEnabled( true ); mMaxScaleSpinBox->setValue( attributeValues[propIt.value()].toInt() ); break; case QgsPalLayerSettings::Size: mFontSizeSpinBox->setEnabled( true ); mLabelFont.setPointSizeF( attributeValues[propIt.value()].toDouble() ); mFontSizeSpinBox->setValue( attributeValues[propIt.value()].toDouble() ); break; case QgsPalLayerSettings::BufferSize: mBufferSizeSpinBox->setEnabled( true ); mBufferSizeSpinBox->setValue( attributeValues[propIt.value()].toDouble() ); break; case QgsPalLayerSettings::PositionX: mXCoordSpinBox->setEnabled( true ); mXCoordSpinBox->setValue( attributeValues[propIt.value()].toDouble() ); break; case QgsPalLayerSettings::PositionY: mYCoordSpinBox->setEnabled( true ); mYCoordSpinBox->setValue( attributeValues[propIt.value()].toDouble() ); break; case QgsPalLayerSettings::LabelDistance: mLabelDistanceSpinBox->setEnabled( true ); mLabelDistanceSpinBox->setValue( attributeValues[propIt.value()].toDouble() ); break; case QgsPalLayerSettings::Hali: mHaliComboBox->setEnabled( true ); mHaliComboBox->setCurrentIndex( mHaliComboBox->findText( attributeValues[propIt.value()].toString() ) ); break; case QgsPalLayerSettings::Vali: mValiComboBox->setEnabled( true ); mValiComboBox->setCurrentIndex( mValiComboBox->findText( attributeValues[propIt.value()].toString() ) ); break; case QgsPalLayerSettings::BufferColor: mBufferColorButton->setEnabled( true ); mBufferColorButton->setColor( QColor( attributeValues[propIt.value()].toString() ) ); break; case QgsPalLayerSettings::Color: mFontColorButton->setEnabled( true ); mFontColorButton->setColor( QColor( attributeValues[propIt.value()].toString() ) ); break; case QgsPalLayerSettings::Rotation: mRotationSpinBox->setEnabled( true ); mRotationSpinBox->setValue( attributeValues[propIt.value()].toDouble() ); break; //font related properties case QgsPalLayerSettings::Bold: mLabelFont.setBold( attributeValues[propIt.value()].toBool() ); break; case QgsPalLayerSettings::Italic: mLabelFont.setItalic( attributeValues[propIt.value()].toBool() ); break; case QgsPalLayerSettings::Underline: mLabelFont.setUnderline( attributeValues[propIt.value()].toBool() ); break; case QgsPalLayerSettings::Strikeout: mLabelFont.setStrikeOut( attributeValues[propIt.value()].toBool() ); break; case QgsPalLayerSettings::Family: mLabelFont.setFamily( attributeValues[propIt.value()].toString() ); break; default: break; } } mFontPushButton->setEnabled( labelFontEditingPossible() ); blockElementSignals( false ); }
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; }