Exemplo n.º 1
0
void QgsDataDefinedButton::init( const QgsVectorLayer* vl,
                                 const QgsDataDefined* datadefined,
                                 DataTypes datatypes,
                                 QString description )
{
  mVectorLayer = vl;
  // construct default property if none or incorrect passed in
  if ( !datadefined )
  {
    mProperty.insert( "active", "0" );
    mProperty.insert( "useexpr", "0" );
    mProperty.insert( "expression", "" );
    mProperty.insert( "field", "" );
  }
  else
  {
    mProperty.insert( "active", datadefined->isActive() ? "1" : "0" );
    mProperty.insert( "useexpr", datadefined->useExpression() ? "1" : "0" );
    mProperty.insert( "expression", datadefined->expressionString() );
    mProperty.insert( "field", datadefined->field() );
  }

  mDataTypes = datatypes;
  mInputDescription = description;
  mFullDescription = QString( "" );
  mUsageInfo = QString( "" );
  mCurrentDefinition = QString( "" );

  mActionExpression = 0;

  if ( mIconDataDefine.isNull() )
  {
    mIconDataDefine = QgsApplication::getThemeIcon( "/mIconDataDefine.svg" );
    mIconDataDefineOn = QgsApplication::getThemeIcon( "/mIconDataDefineOn.svg" );
    mIconDataDefineError = QgsApplication::getThemeIcon( "/mIconDataDefineError.svg" );
    mIconDataDefineExpression = QgsApplication::getThemeIcon( "/mIconDataDefineExpression.svg" );
    mIconDataDefineExpressionOn = QgsApplication::getThemeIcon( "/mIconDataDefineExpressionOn.svg" );
    mIconDataDefineExpressionError = QgsApplication::getThemeIcon( "/mIconDataDefineExpressionError.svg" );
  }

  // set default icon properties
  setFixedSize( 28, 24 );
  setStyleSheet( QString( "QToolButton{ background: none; border: none;}" ) );
  setIconSize( QSize( 24, 24 ) );
  setPopupMode( QToolButton::InstantPopup );

  mDefineMenu = new QMenu( this );
  connect( mDefineMenu, SIGNAL( aboutToShow() ), this, SLOT( aboutToShowMenu() ) );
  connect( mDefineMenu, SIGNAL( triggered( QAction* ) ), this, SLOT( menuActionTriggered( QAction* ) ) );

  mFieldsMenu = new QMenu( this );

  mActionActive = new QAction( this );
  QFont f = mActionActive->font();
  f.setBold( true );
  mActionActive->setFont( f );

  mActionDescription = new QAction( tr( "Description..." ), this );

  mActionExpDialog = new QAction( tr( "Edit..." ), this );
  mActionPasteExpr = new QAction( tr( "Paste" ), this );
  mActionCopyExpr = new QAction( tr( "Copy" ), this );
  mActionClearExpr = new QAction( tr( "Clear" ), this );

  // set up data types string
  mActionDataTypes = 0;
  mDataTypesString = QString( "" );

  QStringList ts;
  if ( mDataTypes.testFlag( AnyType ) || mDataTypes.testFlag( String ) )
  {
    ts << tr( "string" );
  }
  if ( mDataTypes.testFlag( AnyType ) || mDataTypes.testFlag( Int ) )
  {
    ts << tr( "int" );
  }
  if ( mDataTypes.testFlag( AnyType ) || mDataTypes.testFlag( Double ) )
  {
    ts << tr( "double" );
  }

  if ( !ts.isEmpty() )
  {
    mDataTypesString = ts.join( ", " );
    mActionDataTypes = new QAction( tr( "Field type: " ) + mDataTypesString, this );

    // list fields and types in submenu, since there may be many
    mActionDataTypes->setMenu( mFieldsMenu );
  }

  if ( mVectorLayer )
  {
    // store just a list of fields of unknown type or those that match the expected type
    const QgsFields& fields = mVectorLayer->pendingFields();
    for ( int i = 0; i < fields.count(); ++i )
    {
      const QgsField& f = fields.at( i );
      bool fieldMatch = false;
      // NOTE: these are the only QVariant enums supported at this time (see QgsField)
      QString fieldType;
      switch ( f.type() )
      {
        case QVariant::String:
          fieldMatch = mDataTypes.testFlag( String );
          fieldType = tr( "string" );
          break;
        case QVariant::Int:
          fieldMatch = mDataTypes.testFlag( Int );
          fieldType = tr( "integer" );
          break;
        case QVariant::Double:
          fieldMatch = mDataTypes.testFlag( Double );
          fieldType = tr( "double" );
          break;
        case QVariant::Invalid:
        default:
          fieldMatch = true; // field type is unknown
          fieldType = tr( "unknown type" );
      }
      if ( fieldMatch || mDataTypes.testFlag( AnyType ) )
      {
        mFieldNameList << f.name();
        mFieldTypeList << fieldType;
      }
    }
  }

  setMenu( mDefineMenu );

  // set up sibling widget connections
  connect( this, SIGNAL( dataDefinedActivated( bool ) ), this, SLOT( disableEnabledWidgets( bool ) ) );
  connect( this, SIGNAL( dataDefinedActivated( bool ) ), this, SLOT( checkCheckedWidgets( bool ) ) );

  updateGui();
}