Exemplo n.º 1
0
QColor QgsColorButton::linkedProjectColor() const
{
  QList<QgsProjectColorScheme *> projectSchemes;
  QgsApplication::colorSchemeRegistry()->schemes( projectSchemes );
  if ( projectSchemes.length() > 0 )
  {
    QgsProjectColorScheme *scheme = projectSchemes.at( 0 );
    const QgsNamedColorList colors = scheme->fetchColors();
    for ( const auto &color : colors )
    {
      if ( color.second.isEmpty() )
        continue;

      if ( color.second == mLinkedColorName )
      {
        return color.first;
      }
    }
  }
  return QColor();
}
///@cond PRIVATE
void QgsPropertyOverrideButton::aboutToShowMenu()
{
  mDefineMenu->clear();
  // update fields so that changes made to layer's fields are reflected
  updateFieldLists();

  bool hasExp = !mExpressionString.isEmpty();
  QString ddTitle = tr( "Data defined override" );

  QAction *ddTitleAct = mDefineMenu->addAction( ddTitle );
  QFont titlefont = ddTitleAct->font();
  titlefont.setItalic( true );
  ddTitleAct->setFont( titlefont );
  ddTitleAct->setEnabled( false );

  bool addActiveAction = false;
  if ( mProperty.propertyType() == QgsProperty::ExpressionBasedProperty && hasExp )
  {
    QgsExpression exp( mExpressionString );
    // whether expression is parse-able
    addActiveAction = !exp.hasParserError();
  }
  else if ( mProperty.propertyType() == QgsProperty::FieldBasedProperty )
  {
    // whether field exists
    addActiveAction = mFieldNameList.contains( mFieldName );
  }

  if ( addActiveAction )
  {
    ddTitleAct->setText( ddTitle + " (" + ( mProperty.propertyType() == QgsProperty::ExpressionBasedProperty ? tr( "expression" ) : tr( "field" ) ) + ')' );
    mDefineMenu->addAction( mActionActive );
    mActionActive->setText( mProperty.isActive() ? tr( "Deactivate" ) : tr( "Activate" ) );
    mActionActive->setData( QVariant( !mProperty.isActive() ) );
  }

  if ( !mFullDescription.isEmpty() )
  {
    mDefineMenu->addAction( mActionDescription );
  }

  mDefineMenu->addSeparator();

  // deactivate button if field already exists
  if ( mAuxiliaryStorageEnabled && mVectorLayer )
  {
    mDefineMenu->addAction( mActionCreateAuxiliaryField );

    const QgsAuxiliaryLayer *alayer = mVectorLayer->auxiliaryLayer();

    mActionCreateAuxiliaryField->setEnabled( true );
    mActionCreateAuxiliaryField->setChecked( false );

    int index = mVectorLayer->fields().indexFromName( mFieldName );
    int srcIndex;
    if ( index >= 0 && alayer && mVectorLayer->isAuxiliaryField( index, srcIndex ) )
    {
      mActionCreateAuxiliaryField->setEnabled( false );
      mActionCreateAuxiliaryField->setChecked( true );
    }
  }

  bool fieldActive = false;
  if ( !mDataTypesString.isEmpty() )
  {
    QAction *fieldTitleAct = mDefineMenu->addAction( tr( "Attribute Field" ) );
    fieldTitleAct->setFont( titlefont );
    fieldTitleAct->setEnabled( false );

    mDefineMenu->addAction( mActionDataTypes );

    mFieldsMenu->clear();

    if ( !mFieldNameList.isEmpty() )
    {

      for ( int j = 0; j < mFieldNameList.count(); ++j )
      {
        QString fldname = mFieldNameList.at( j );
        QAction *act = mFieldsMenu->addAction( fldname + "    (" + mFieldTypeList.at( j ) + ')' );
        act->setData( QVariant( fldname ) );
        if ( mFieldName == fldname )
        {
          act->setCheckable( true );
          act->setChecked( mProperty.propertyType() == QgsProperty::FieldBasedProperty );
          fieldActive = mProperty.propertyType() == QgsProperty::FieldBasedProperty;
        }
      }
    }
    else
    {
      QAction *act = mFieldsMenu->addAction( tr( "No matching field types found" ) );
      act->setEnabled( false );
    }

    mDefineMenu->addSeparator();
  }

  mFieldsMenu->menuAction()->setCheckable( true );
  mFieldsMenu->menuAction()->setChecked( fieldActive && mProperty.propertyType() == QgsProperty::FieldBasedProperty && !mProperty.transformer() );

  bool colorActive = false;
  mColorsMenu->clear();
  if ( mDefinition.standardTemplate() == QgsPropertyDefinition::ColorWithAlpha
       || mDefinition.standardTemplate() == QgsPropertyDefinition::ColorNoAlpha )
  {
    // project colors menu
    QAction *colorTitleAct = mDefineMenu->addAction( tr( "Project Color" ) );
    colorTitleAct->setFont( titlefont );
    colorTitleAct->setEnabled( false );

    QList<QgsProjectColorScheme *> projectSchemes;
    QgsApplication::colorSchemeRegistry()->schemes( projectSchemes );
    if ( projectSchemes.length() > 0 )
    {
      QgsProjectColorScheme *scheme = projectSchemes.at( 0 );
      const QgsNamedColorList colors = scheme->fetchColors();
      for ( const auto &color : colors )
      {
        if ( color.second.isEmpty() )
          continue;

        QPixmap icon = QgsColorButton::createMenuIcon( color.first, mDefinition.standardTemplate() == QgsPropertyDefinition::ColorWithAlpha );
        QAction *act = mColorsMenu->addAction( color.second );
        act->setIcon( icon );
        if ( mProperty.propertyType() == QgsProperty::ExpressionBasedProperty && hasExp && mExpressionString == QStringLiteral( "project_color('%1')" ).arg( color.second ) )
        {
          act->setCheckable( true );
          act->setChecked( true );
          colorActive = true;
        }
      }
    }

    if ( mColorsMenu->actions().isEmpty() )
    {
      QAction *act = mColorsMenu->addAction( tr( "No colors set" ) );
      act->setEnabled( false );
    }

    mDefineMenu->addAction( mActionColors );
    mColorsMenu->menuAction()->setCheckable( true );
    mColorsMenu->menuAction()->setChecked( colorActive && !mProperty.transformer() );

    mDefineMenu->addSeparator();
  }

  QAction *exprTitleAct = mDefineMenu->addAction( tr( "Expression" ) );
  exprTitleAct->setFont( titlefont );
  exprTitleAct->setEnabled( false );

  mVariablesMenu->clear();
  bool variableActive = false;
  if ( mExpressionContextGenerator )
  {
    QgsExpressionContext context = mExpressionContextGenerator->createExpressionContext();
    QStringList variables = context.variableNames();
    Q_FOREACH ( const QString &variable, variables )
    {
      if ( context.isReadOnly( variable ) ) //only want to show user-set variables
        continue;
      if ( variable.startsWith( '_' ) ) //no hidden variables
        continue;

      QAction *act = mVariablesMenu->addAction( variable );
      act->setData( QVariant( variable ) );

      if ( mProperty.propertyType() == QgsProperty::ExpressionBasedProperty && hasExp && mExpressionString == '@' + variable )
      {
        act->setCheckable( true );
        act->setChecked( true );
        variableActive = true;
      }
    }
  }

  if ( mVariablesMenu->actions().isEmpty() )
  {
    QAction *act = mVariablesMenu->addAction( tr( "No variables set" ) );
    act->setEnabled( false );
  }

  mDefineMenu->addAction( mActionVariables );
  mVariablesMenu->menuAction()->setCheckable( true );
  mVariablesMenu->menuAction()->setChecked( variableActive && !mProperty.transformer() );

  if ( hasExp )
  {
    QString expString = mExpressionString;
    if ( expString.length() > 35 )
    {
      expString.truncate( 35 );
      expString.append( QChar( 0x2026 ) );
    }

    expString.prepend( tr( "Current: " ) );

    if ( !mActionExpression )
    {
      mActionExpression = new QAction( expString, this );
      mActionExpression->setCheckable( true );
    }
    else
    {
      mActionExpression->setText( expString );
    }
    mDefineMenu->addAction( mActionExpression );
    mActionExpression->setChecked( mProperty.propertyType() == QgsProperty::ExpressionBasedProperty && !variableActive && !colorActive && !mProperty.transformer() );

    mDefineMenu->addAction( mActionExpDialog );
    mDefineMenu->addAction( mActionCopyExpr );
    mDefineMenu->addAction( mActionPasteExpr );
    mDefineMenu->addAction( mActionClearExpr );
  }
  else
  {
    mDefineMenu->addAction( mActionExpDialog );
    mDefineMenu->addAction( mActionPasteExpr );
  }

  if ( !mDefinition.name().isEmpty() && mDefinition.supportsAssistant() )
  {
    mDefineMenu->addSeparator();
    mActionAssistant->setCheckable( mProperty.transformer() );
    mActionAssistant->setChecked( mProperty.transformer() );
    mDefineMenu->addAction( mActionAssistant );
  }
}