Exemple #1
0
QString QgsActionManager::expandAction( QString action, const QgsAttributeMap &attributes,
                                        uint clickedOnValue )
{
  // This function currently replaces all %% characters in the action
  // with the value from values[clickedOnValue].second, and then
  // searches for all strings that go %attribute_name, where
  // attribute_name is found in values[x].first, and replaces any that
  // it finds by values[s].second.

  // Additional substitutions could include symbols for $CWD, $HOME,
  // etc (and their OSX and Windows equivalents)

  // This function will potentially fall apart if any of the
  // substitutions produce text that could match another
  // substitution. May be better to adopt a two pass approach - identify
  // all matches and their substitutions and then do a second pass
  // for the actual substitutions.

  QString expanded_action;
  if ( attributes.contains( clickedOnValue ) )
    expanded_action = action.replace( "%%", attributes[clickedOnValue].toString() );
  else
    expanded_action = action;

  const QgsFields &fields = mLayer->fields();

  for ( int i = 0; i < 4; i++ )
  {
    for ( QgsAttributeMap::const_iterator it = attributes.begin(); it != attributes.end(); ++it )
    {
      int attrIdx = it.key();
      if ( attrIdx < 0 || attrIdx >= fields.count() )
        continue;

      QString to_replace;
      switch ( i )
      {
        case 0:
          to_replace = "[%" + fields[attrIdx].name() + ']';
          break;
        case 1:
          to_replace = "[%" + mLayer->attributeDisplayName( attrIdx ) + ']';
          break;
        case 2:
          to_replace = '%' + fields[attrIdx].name();
          break;
        case 3:
          to_replace = '%' + mLayer->attributeDisplayName( attrIdx );
          break;
      }

      expanded_action = expanded_action.replace( to_replace, it.value().toString() );
    }
  }

  return expanded_action;
}
Exemple #2
0
void QgsGPXProvider::changeAttributeValues( QgsGPSObject& obj, const QgsAttributeMap& attrs )
{
  QgsAttributeMap::const_iterator aIter;

  // TODO:
  if ( attrs.contains( NameAttr ) )
    obj.name = attrs[NameAttr].toString();
  if ( attrs.contains( CmtAttr ) )
    obj.cmt = attrs[CmtAttr].toString();
  if ( attrs.contains( DscAttr ) )
    obj.desc = attrs[DscAttr].toString();
  if ( attrs.contains( SrcAttr ) )
    obj.src = attrs[SrcAttr].toString();
  if ( attrs.contains( URLAttr ) )
    obj.url = attrs[URLAttr].toString();
  if ( attrs.contains( URLNameAttr ) )
    obj.urlname = attrs[URLNameAttr].toString();

  // waypoint-specific attributes
  QgsWaypoint* wpt = dynamic_cast<QgsWaypoint*>( &obj );
  if ( wpt != NULL )
  {
    if ( attrs.contains( SymAttr ) )
      wpt->sym = attrs[SymAttr].toString();
    if ( attrs.contains( EleAttr ) )
    {
      bool eleIsOK;
      double ele = attrs[EleAttr].toDouble( &eleIsOK );
      if ( eleIsOK )
        wpt->ele = ele;
    }
  }

  // route- and track-specific attributes
  QgsGPSExtended* ext = dynamic_cast<QgsGPSExtended*>( &obj );
  if ( ext != NULL )
  {
    if ( attrs.contains( NumAttr ) )
    {
      bool eleIsOK;
      double ele = attrs[NumAttr].toDouble( &eleIsOK );
      if ( eleIsOK )
        wpt->ele = ele;
    }
  }
}
bool QgsVectorLayerEditBuffer::changeAttributeValues( QgsFeatureId fid, const QgsAttributeMap &newValues, const QgsAttributeMap &oldValues )
{
  bool success = true;
  for ( auto it = newValues.constBegin() ; it != newValues.constEnd(); ++it )
  {
    const int field = it.key();
    const QVariant newValue = it.value();
    QVariant oldValue;

    if ( oldValues.contains( field ) )
      oldValue = oldValues[field];

    success &= changeAttributeValue( fid, field, newValue, oldValue );
  }

  return success;
}
bool QgsFeatureAction::editFeature()
{
  bool res = false;

  if ( !mLayer )
    return res;

  QgsAttributeDialog *dialog = newDialog( false );

  if ( !mLayer->isEditable() )
  {
    res = dialog->exec();
  }
  else
  {
    QgsAttributeMap src = mFeature.attributeMap();

    if ( dialog->exec() )
    {
      mLayer->beginEditCommand( text() );

      const QgsAttributeMap &dst = mFeature.attributeMap();
      for ( QgsAttributeMap::const_iterator it = dst.begin(); it != dst.end(); it++ )
      {
        if ( !src.contains( it.key() ) || it.value() != src[it.key()] )
        {
          mLayer->changeAttributeValue( mFeature.id(), it.key(), it.value() );
        }
      }

      mLayer->endEditCommand();
      res = true;
    }
    else
    {
      res = false;
    }
  }

  delete dialog;
  return res;
}
bool QgsFeatureAction::addFeature( const QgsAttributeMap& defaultAttributes )
{
  if ( !mLayer || !mLayer->isEditable() )
    return false;

  QgsVectorDataProvider *provider = mLayer->dataProvider();

  QSettings settings;
  bool reuseLastValues = settings.value( "/qgis/digitizing/reuseLastValues", false ).toBool();
  QgsDebugMsg( QString( "reuseLastValues: %1" ).arg( reuseLastValues ) );

  // add the fields to the QgsFeature
  const QgsFields& fields = mLayer->pendingFields();
  mFeature.initAttributes( fields.count() );
  for ( int idx = 0; idx < fields.count(); ++idx )
  {
    if ( defaultAttributes.contains( idx ) )
    {
      QgsDebugMsg( QString( "Using specified default %1 for %2" ).arg( defaultAttributes.value( idx ).toString() ).arg( idx ) );
      mFeature.setAttribute( idx, defaultAttributes.value( idx ) );
    }
    else if ( reuseLastValues && mLastUsedValues.contains( mLayer ) && mLastUsedValues[ mLayer ].contains( idx ) )
    {
      QgsDebugMsg( QString( "reusing %1 for %2" ).arg( mLastUsedValues[ mLayer ][idx].toString() ).arg( idx ) );
      mFeature.setAttribute( idx, mLastUsedValues[ mLayer ][idx] );
    }
    else
    {
      mFeature.setAttribute( idx, provider->defaultValue( idx ) );
    }
  }

  bool res = false;

  mLayer->beginEditCommand( text() );

  // show the dialog to enter attribute values
  bool isDisabledAttributeValuesDlg = settings.value( "/qgis/digitizing/disable_enter_attribute_values_dialog", false ).toBool();
  // override application-wide setting with any layer setting
  switch ( mLayer->featureFormSuppress() )
  {
    case QgsVectorLayer::SuppressOn:
      isDisabledAttributeValuesDlg = true;
      break;
    case QgsVectorLayer::SuppressOff:
      isDisabledAttributeValuesDlg = false;
      break;
    case QgsVectorLayer::SuppressDefault:
      break;
  }
  if ( isDisabledAttributeValuesDlg )
  {
    res = mLayer->addFeature( mFeature );
  }
  else
  {
    QgsAttributes origValues;
    if ( reuseLastValues )
      origValues = mFeature.attributes();

    QgsAttributeDialog *dialog = newDialog( false );
    if ( dialog->exec() )
    {
      if ( reuseLastValues )
      {
        for ( int idx = 0; idx < fields.count(); ++idx )
        {
          const QgsAttributes &newValues = mFeature.attributes();
          if ( origValues[idx] != newValues[idx] )
          {
            QgsDebugMsg( QString( "saving %1 for %2" ).arg( mLastUsedValues[ mLayer ][idx].toString() ).arg( idx ) );
            mLastUsedValues[ mLayer ][idx] = newValues[idx];
          }
        }
      }

      res = mLayer->addFeature( mFeature );
    }
    else
    {
      QgsDebugMsg( "Adding feature to layer failed" );
      res = false;
    }
  }

  if ( res )
    mLayer->endEditCommand();
  else
    mLayer->destroyEditCommand();

  return res;
}
bool QgsFeatureAction::addFeature()
{
  if ( !mLayer || !mLayer->isEditable() )
    return false;

  QgsVectorDataProvider *provider = mLayer->dataProvider();

  QSettings settings;
  bool reuseLastValues = settings.value( "/qgis/digitizing/reuseLastValues", false ).toBool();
  QgsDebugMsg( QString( "reuseLastValues: %1" ).arg( reuseLastValues ) );

  // add the fields to the QgsFeature
  const QgsFieldMap fields = mLayer->pendingFields();
  for ( QgsFieldMap::const_iterator it = fields.constBegin(); it != fields.constEnd(); ++it )
  {
    if ( reuseLastValues && mLastUsedValues.contains( mLayer ) && mLastUsedValues[ mLayer ].contains( it.key() ) )
    {
      QgsDebugMsg( QString( "reusing %1 for %2" ).arg( mLastUsedValues[ mLayer ][ it.key()].toString() ).arg( it.key() ) );
      mFeature.addAttribute( it.key(), mLastUsedValues[ mLayer ][ it.key()] );
    }
    else
    {
      mFeature.addAttribute( it.key(), provider->defaultValue( it.key() ) );
    }
  }

  bool res = false;

  mLayer->beginEditCommand( text() );

  // show the dialog to enter attribute values
  bool isDisabledAttributeValuesDlg = settings.value( "/qgis/digitizing/disable_enter_attribute_values_dialog", false ).toBool();
  if ( isDisabledAttributeValuesDlg )
  {
    res = mLayer->addFeature( mFeature );
  }
  else
  {
    QgsAttributeMap origValues;
    if ( reuseLastValues )
      origValues = mFeature.attributeMap();

    QgsAttributeDialog *dialog = newDialog( false );
    if ( dialog->exec() )
    {
      if ( reuseLastValues )
      {
        for ( QgsFieldMap::const_iterator it = fields.constBegin(); it != fields.constEnd(); ++it )
        {
          const QgsAttributeMap &newValues = mFeature.attributeMap();
          if ( newValues.contains( it.key() )
               && origValues.contains( it.key() )
               && origValues[ it.key()] != newValues[ it.key()] )
          {
            QgsDebugMsg( QString( "saving %1 for %2" ).arg( mLastUsedValues[ mLayer ][ it.key()].toString() ).arg( it.key() ) );
            mLastUsedValues[ mLayer ][ it.key()] = newValues[ it.key()];
          }
        }
      }

      res = mLayer->addFeature( mFeature );
    }
    else
    {
      QgsDebugMsg( "Adding feature to layer failed" );
      res = false;
    }
  }

  if ( res )
    mLayer->endEditCommand();
  else
    mLayer->destroyEditCommand();

  return res;
}
bool QgsFeatureAction::addFeature( const QgsAttributeMap& defaultAttributes, bool showModal )
{
  if ( !mLayer || !mLayer->isEditable() )
    return false;

  QgsVectorDataProvider *provider = mLayer->dataProvider();

  QSettings settings;
  bool reuseLastValues = settings.value( "/qgis/digitizing/reuseLastValues", false ).toBool();
  QgsDebugMsg( QString( "reuseLastValues: %1" ).arg( reuseLastValues ) );

  // add the fields to the QgsFeature
  const QgsFields& fields = mLayer->fields();
  mFeature->initAttributes( fields.count() );
  for ( int idx = 0; idx < fields.count(); ++idx )
  {
    QVariant v;

    if ( defaultAttributes.contains( idx ) )
    {
      v = defaultAttributes.value( idx );
    }
    else if ( reuseLastValues && sLastUsedValues.contains( mLayer ) && sLastUsedValues[ mLayer ].contains( idx ) )
    {
      v = sLastUsedValues[ mLayer ][idx];
    }
    else
    {
      v = provider->defaultValue( idx );
    }

    mFeature->setAttribute( idx, v );
  }

  //show the dialog to enter attribute values
  //only show if enabled in settings and layer has fields
  bool isDisabledAttributeValuesDlg = ( fields.count() == 0 ) || settings.value( "/qgis/digitizing/disable_enter_attribute_values_dialog", false ).toBool();

  // override application-wide setting with any layer setting
  switch ( mLayer->editFormConfig()->suppress() )
  {
    case QgsEditFormConfig::SuppressOn:
      isDisabledAttributeValuesDlg = true;
      break;
    case QgsEditFormConfig::SuppressOff:
      isDisabledAttributeValuesDlg = false;
      break;
    case QgsEditFormConfig::SuppressDefault:
      break;
  }
  if ( isDisabledAttributeValuesDlg )
  {
    mLayer->beginEditCommand( text() );
    mFeatureSaved = mLayer->addFeature( *mFeature );

    if ( mFeatureSaved )
      mLayer->endEditCommand();
    else
      mLayer->destroyEditCommand();
  }
  else
  {
    QgsAttributeDialog *dialog = newDialog( false );
    dialog->setIsAddDialog( true );
    dialog->setEditCommandMessage( text() );

    connect( dialog->attributeForm(), SIGNAL( featureSaved( const QgsFeature & ) ), this, SLOT( onFeatureSaved( const QgsFeature & ) ) );

    if ( !showModal )
    {
      setParent( dialog ); // keep dialog until the dialog is closed and destructed
      dialog->show(); // will also delete the dialog on close (show() is overridden)
      mFeature = 0;
      return true;
    }

    dialog->setAttribute( Qt::WA_DeleteOnClose );
    dialog->exec();
  }

  // Will be set in the onFeatureSaved SLOT
  return mFeatureSaved;
}
Exemple #8
0
bool QgsFeatureAction::addFeature( const QgsAttributeMap& defaultAttributes )
{
  if ( !mLayer || !mLayer->isEditable() )
    return false;

  QgsVectorDataProvider *provider = mLayer->dataProvider();

  QSettings settings;
  bool reuseLastValues = settings.value( "/qgis/digitizing/reuseLastValues", false ).toBool();
  QgsDebugMsg( QString( "reuseLastValues: %1" ).arg( reuseLastValues ) );

  // add the fields to the QgsFeature
  const QgsFields& fields = mLayer->pendingFields();
  mFeature.initAttributes( fields.count() );
  for ( int idx = 0; idx < fields.count(); ++idx )
  {
    QVariant v;

    if ( defaultAttributes.contains( idx ) )
    {
      v = defaultAttributes.value( idx );
    }
    else if ( reuseLastValues && sLastUsedValues.contains( mLayer ) && sLastUsedValues[ mLayer ].contains( idx ) )
    {
      v = sLastUsedValues[ mLayer ][idx];
    }
    else
    {
      v = provider->defaultValue( idx );
    }

    mFeature.setAttribute( idx, v );
  }

  // show the dialog to enter attribute values
  bool isDisabledAttributeValuesDlg = settings.value( "/qgis/digitizing/disable_enter_attribute_values_dialog", false ).toBool();
  // override application-wide setting with any layer setting
  switch ( mLayer->featureFormSuppress() )
  {
    case QgsVectorLayer::SuppressOn:
      isDisabledAttributeValuesDlg = true;
      break;
    case QgsVectorLayer::SuppressOff:
      isDisabledAttributeValuesDlg = false;
      break;
    case QgsVectorLayer::SuppressDefault:
      break;
  }
  if ( isDisabledAttributeValuesDlg )
  {
    mLayer->beginEditCommand( text() );
    mFeatureSaved = mLayer->addFeature( mFeature );

    if ( mFeatureSaved )
      mLayer->endEditCommand();
    else
      mLayer->destroyEditCommand();
  }
  else
  {
    QgsAttributeDialog *dialog = newDialog( false );
    dialog->setIsAddDialog( true );
    dialog->setEditCommandMessage( text() );

    connect( dialog->attributeForm(), SIGNAL( featureSaved( QgsFeature ) ), this, SLOT( onFeatureSaved( QgsFeature ) ) );

    dialog->exec();
  }

  // Will be set in the onFeatureSaved SLOT
  return mFeatureSaved;
}