QgsSymbolV2* QgsSingleSymbolRendererV2::symbolForFeature( QgsFeature& feature, QgsRenderContext &context ) { context.expressionContext().setFeature( feature ); if ( !mRotation.data() && !mSizeScale.data() ) return mSymbol.data(); const double rotation = mRotation.data() ? mRotation->evaluate( &context.expressionContext() ).toDouble() : 0; const double sizeScale = mSizeScale.data() ? mSizeScale->evaluate( &context.expressionContext() ).toDouble() : 1.; if ( mTempSymbol->type() == QgsSymbolV2::Marker ) { QgsMarkerSymbolV2* markerSymbol = static_cast<QgsMarkerSymbolV2*>( mTempSymbol.data() ); if ( mRotation.data() ) markerSymbol->setAngle( rotation ); markerSymbol->setSize( sizeScale * mOrigSize ); markerSymbol->setScaleMethod( mScaleMethod ); } else if ( mTempSymbol->type() == QgsSymbolV2::Line ) { QgsLineSymbolV2* lineSymbol = static_cast<QgsLineSymbolV2*>( mTempSymbol.data() ); lineSymbol->setWidth( sizeScale * mOrigSize ); } else if ( mTempSymbol->type() == QgsSymbolV2::Fill ) { QgsFillSymbolV2* fillSymbol = static_cast<QgsFillSymbolV2*>( mTempSymbol.data() ); if ( mRotation.data() ) fillSymbol->setAngle( rotation ); } return mTempSymbol.data(); }
QgsSymbolV2* QgsCategorizedSymbolRendererV2::symbolForFeature( QgsFeature& feature ) { QgsSymbolV2* symbol = originalSymbolForFeature( feature ); if ( !symbol ) return 0; if ( !mRotation.data() && !mSizeScale.data() ) return symbol; // no data-defined rotation/scaling - just return the symbol // find out rotation, size scale const double rotation = mRotation.data() ? mRotation->evaluate( feature ).toDouble() : 0; const double sizeScale = mSizeScale.data() ? mSizeScale->evaluate( feature ).toDouble() : 1.; // take a temporary symbol (or create it if doesn't exist) QgsSymbolV2* tempSymbol = mTempSymbols[symbol]; // modify the temporary symbol and return it if ( tempSymbol->type() == QgsSymbolV2::Marker ) { QgsMarkerSymbolV2* markerSymbol = static_cast<QgsMarkerSymbolV2*>( tempSymbol ); if ( mRotation.data() ) markerSymbol->setAngle( rotation ); markerSymbol->setSize( sizeScale * static_cast<QgsMarkerSymbolV2*>( symbol )->size() ); markerSymbol->setScaleMethod( mScaleMethod ); } else if ( tempSymbol->type() == QgsSymbolV2::Line ) { QgsLineSymbolV2* lineSymbol = static_cast<QgsLineSymbolV2*>( tempSymbol ); lineSymbol->setWidth( sizeScale * static_cast<QgsLineSymbolV2*>( symbol )->width() ); } return tempSymbol; }
QgsLegendSymbolListV2 QgsSingleSymbolRendererV2::legendSymbolItemsV2() const { QgsLegendSymbolListV2 lst; if ( mSymbol->type() == QgsSymbolV2::Marker ) { const QgsMarkerSymbolV2 * symbol = static_cast<const QgsMarkerSymbolV2 *>( mSymbol.data() ); QgsDataDefined sizeDD = symbol->dataDefinedSize(); if ( sizeDD.isActive() && sizeDD.useExpression() ) { QgsScaleExpression scaleExp( sizeDD.expressionString() ); if ( scaleExp.type() != QgsScaleExpression::Unknown ) { QgsLegendSymbolItemV2 title( nullptr, scaleExp.baseExpression(), QString() ); lst << title; Q_FOREACH ( double v, QgsSymbolLayerV2Utils::prettyBreaks( scaleExp.minValue(), scaleExp.maxValue(), 4 ) ) { QgsLegendSymbolItemV2 si( mSymbol.data(), QString::number( v ), QString() ); QgsMarkerSymbolV2 * s = static_cast<QgsMarkerSymbolV2 *>( si.symbol() ); s->setDataDefinedSize( 0 ); s->setSize( scaleExp.size( v ) ); lst << si; } return lst; } }
void QgsSymbolV2SelectorDialog::setMarkerSize( double size ) { QgsMarkerSymbolV2* markerSymbol = static_cast<QgsMarkerSymbolV2*>( mSymbol ); if ( markerSymbol->size() == size ) return; markerSymbol->setSize( size ); updateSymbolPreview(); emit symbolModified(); }
QgsSymbolV2* QgsCategorizedSymbolRendererV2::symbolForFeature( QgsFeature& feature ) { const QgsAttributeMap& attrMap = feature.attributeMap(); QgsAttributeMap::const_iterator ita = attrMap.find( mAttrNum ); if ( ita == attrMap.end() ) { QgsDebugMsg( "attribute '" + mAttrName + "' (index " + QString::number( mAttrNum ) + ") required by renderer not found" ); return NULL; } // find the right symbol for the category QgsSymbolV2* symbol = symbolForValue( *ita ); if ( symbol == NULL ) { // if no symbol found use default one //return symbolForValue( QVariant( "" ) ); // What is default? Empty string may be a legal value, and features not found // should not be rendered using empty string value category symbology. // We also need to get NULL in that case so that willRenderFeature() // may be used to count features. return 0; } if ( mRotationFieldIdx == -1 && mSizeScaleFieldIdx == -1 ) return symbol; // no data-defined rotation/scaling - just return the symbol // find out rotation, size scale double rotation = 0; double sizeScale = 1; if ( mRotationFieldIdx != -1 ) rotation = attrMap[mRotationFieldIdx].toDouble(); if ( mSizeScaleFieldIdx != -1 ) sizeScale = attrMap[mSizeScaleFieldIdx].toDouble(); // take a temporary symbol (or create it if doesn't exist) QgsSymbolV2* tempSymbol = mTempSymbols[ita->toString()]; // modify the temporary symbol and return it if ( tempSymbol->type() == QgsSymbolV2::Marker ) { QgsMarkerSymbolV2* markerSymbol = static_cast<QgsMarkerSymbolV2*>( tempSymbol ); if ( mRotationFieldIdx != -1 ) markerSymbol->setAngle( rotation ); if ( mSizeScaleFieldIdx != -1 ) markerSymbol->setSize( sizeScale * static_cast<QgsMarkerSymbolV2*>( symbol )->size() ); markerSymbol->setScaleMethod( mScaleMethod ); } else if ( tempSymbol->type() == QgsSymbolV2::Line ) { QgsLineSymbolV2* lineSymbol = static_cast<QgsLineSymbolV2*>( tempSymbol ); if ( mSizeScaleFieldIdx != -1 ) lineSymbol->setWidth( sizeScale * static_cast<QgsLineSymbolV2*>( symbol )->width() ); } return tempSymbol; }
QgsSymbolV2* QgsGraduatedSymbolRendererV2::symbolForFeature( QgsFeature& feature ) { const QgsAttributes& attrs = feature.attributes(); if ( mAttrNum < 0 || mAttrNum >= attrs.count() ) { QgsDebugMsg( "attribute required by renderer not found: " + mAttrName + "(index " + QString::number( mAttrNum ) + ")" ); return NULL; } // Null values should not be categorized if ( attrs[mAttrNum].isNull() ) return NULL; // find the right category QgsSymbolV2* symbol = symbolForValue( attrs[mAttrNum].toDouble() ); if ( symbol == NULL ) return NULL; if ( mRotationFieldIdx == -1 && mSizeScaleFieldIdx == -1 ) return symbol; // no data-defined rotation/scaling - just return the symbol // find out rotation, size scale double rotation = 0; double sizeScale = 1; if ( mRotationFieldIdx != -1 ) rotation = attrs[mRotationFieldIdx].toDouble(); if ( mSizeScaleFieldIdx != -1 ) sizeScale = attrs[mSizeScaleFieldIdx].toDouble(); // take a temporary symbol (or create it if doesn't exist) QgsSymbolV2* tempSymbol = mTempSymbols[symbol]; // modify the temporary symbol and return it if ( tempSymbol->type() == QgsSymbolV2::Marker ) { QgsMarkerSymbolV2* markerSymbol = static_cast<QgsMarkerSymbolV2*>( tempSymbol ); if ( mRotationFieldIdx != -1 ) markerSymbol->setAngle( rotation ); if ( mSizeScaleFieldIdx != -1 ) markerSymbol->setSize( sizeScale * static_cast<QgsMarkerSymbolV2*>( symbol )->size() ); markerSymbol->setScaleMethod( mScaleMethod ); } else if ( tempSymbol->type() == QgsSymbolV2::Line ) { QgsLineSymbolV2* lineSymbol = static_cast<QgsLineSymbolV2*>( tempSymbol ); if ( mSizeScaleFieldIdx != -1 ) lineSymbol->setWidth( sizeScale * static_cast<QgsLineSymbolV2*>( symbol )->width() ); } return tempSymbol; }
QgsSymbolV2* QgsCategorizedSymbolRendererV2::symbolForFeature( QgsFeature& feature ) { const QgsAttributes& attrs = feature.attributes(); QVariant value; if ( mAttrNum == -1 ) { Q_ASSERT( mExpression.data() ); value = mExpression->evaluate( &feature ); } else { value = attrs[mAttrNum]; } // find the right symbol for the category QgsSymbolV2* symbol = symbolForValue( value ); if ( symbol == NULL ) { // if no symbol found use default one return symbolForValue( QVariant( "" ) ); } if ( !mRotation.data() && !mSizeScale.data() ) return symbol; // no data-defined rotation/scaling - just return the symbol // find out rotation, size scale const double rotation = mRotation.data() ? mRotation->evaluate( feature ).toDouble() : 0; const double sizeScale = mSizeScale.data() ? mSizeScale->evaluate( feature ).toDouble() : 1.; // take a temporary symbol (or create it if doesn't exist) QgsSymbolV2* tempSymbol = mTempSymbols[value.toString()]; // modify the temporary symbol and return it if ( tempSymbol->type() == QgsSymbolV2::Marker ) { QgsMarkerSymbolV2* markerSymbol = static_cast<QgsMarkerSymbolV2*>( tempSymbol ); if ( mRotation.data() ) markerSymbol->setAngle( rotation ); markerSymbol->setSize( sizeScale * static_cast<QgsMarkerSymbolV2*>( symbol )->size() ); markerSymbol->setScaleMethod( mScaleMethod ); } else if ( tempSymbol->type() == QgsSymbolV2::Line ) { QgsLineSymbolV2* lineSymbol = static_cast<QgsLineSymbolV2*>( tempSymbol ); lineSymbol->setWidth( sizeScale * static_cast<QgsLineSymbolV2*>( symbol )->width() ); } return tempSymbol; }
QgsSymbolV2* QgsGraduatedSymbolRendererV2::symbolForFeature( QgsFeature& feature ) { const QgsAttributes& attrs = feature.attributes(); QVariant value; if ( mAttrNum < 0 || mAttrNum >= attrs.count() ) { value = mExpression->evaluate( &feature ); } else { value = attrs[mAttrNum]; } // Null values should not be categorized if ( value.isNull() ) return NULL; // find the right category QgsSymbolV2* symbol = symbolForValue( value.toDouble() ); if ( symbol == NULL ) return NULL; if ( !mRotation.data() && !mSizeScale.data() ) return symbol; // no data-defined rotation/scaling - just return the symbol // find out rotation, size scale const double rotation = mRotation.data() ? mRotation->evaluate( feature ).toDouble() : 0; const double sizeScale = mSizeScale.data() ? mSizeScale->evaluate( feature ).toDouble() : 1.; // take a temporary symbol (or create it if doesn't exist) QgsSymbolV2* tempSymbol = mTempSymbols[symbol]; // modify the temporary symbol and return it if ( tempSymbol->type() == QgsSymbolV2::Marker ) { QgsMarkerSymbolV2* markerSymbol = static_cast<QgsMarkerSymbolV2*>( tempSymbol ); if ( mRotation.data() ) markerSymbol->setAngle( rotation ); markerSymbol->setSize( sizeScale * static_cast<QgsMarkerSymbolV2*>( symbol )->size() ); markerSymbol->setScaleMethod( mScaleMethod ); } else if ( tempSymbol->type() == QgsSymbolV2::Line ) { QgsLineSymbolV2* lineSymbol = static_cast<QgsLineSymbolV2*>( tempSymbol ); lineSymbol->setWidth( sizeScale * static_cast<QgsLineSymbolV2*>( symbol )->width() ); } return tempSymbol; }
QgsSymbolV2* QgsCategorizedSymbolRendererV2::symbolForFeature( QgsFeature& feature ) { const QgsAttributeMap& attrMap = feature.attributeMap(); QgsAttributeMap::const_iterator ita = attrMap.find( mAttrNum ); if ( ita == attrMap.end() ) { QgsDebugMsg( "attribute '" + mAttrName + "' (index " + QString::number( mAttrNum ) + ") required by renderer not found" ); return NULL; } // find the right symbol for the category QgsSymbolV2* symbol = symbolForValue( *ita ); if ( symbol == NULL ) return NULL; if ( mRotationFieldIdx == -1 && mSizeScaleFieldIdx == -1 ) return symbol; // no data-defined rotation/scaling - just return the symbol // find out rotation, size scale double rotation = 0; double sizeScale = 1; if ( mRotationFieldIdx != -1 ) rotation = attrMap[mRotationFieldIdx].toDouble(); if ( mSizeScaleFieldIdx != -1 ) sizeScale = attrMap[mSizeScaleFieldIdx].toDouble(); // take a temporary symbol (or create it if doesn't exist) QgsSymbolV2* tempSymbol = mTempSymbols[ita->toString()]; // modify the temporary symbol and return it if ( tempSymbol->type() == QgsSymbolV2::Marker ) { QgsMarkerSymbolV2* markerSymbol = static_cast<QgsMarkerSymbolV2*>( tempSymbol ); if ( mRotationFieldIdx != -1 ) markerSymbol->setAngle( rotation ); if ( mSizeScaleFieldIdx != -1 ) markerSymbol->setSize( sizeScale * static_cast<QgsMarkerSymbolV2*>( symbol )->size() ); } else if ( tempSymbol->type() == QgsSymbolV2::Line ) { QgsLineSymbolV2* lineSymbol = static_cast<QgsLineSymbolV2*>( tempSymbol ); if ( mSizeScaleFieldIdx != -1 ) lineSymbol->setWidth( sizeScale * static_cast<QgsLineSymbolV2*>( symbol )->width() ); } return tempSymbol; }
QgsLegendSymbolListV2 QgsCategorizedSymbolRendererV2::legendSymbolItemsV2() const { QgsLegendSymbolListV2 lst; if ( mSourceSymbol.data() && mSourceSymbol->type() == QgsSymbolV2::Marker ) { // check that all symbols that have the same size expression QgsDataDefined ddSize; foreach ( QgsRendererCategoryV2 category, mCategories ) { const QgsMarkerSymbolV2 * symbol = static_cast<const QgsMarkerSymbolV2 *>( category.symbol() ); if ( !ddSize.hasDefaultValues() && symbol->dataDefinedSize() != ddSize ) { // no common size expression return QgsFeatureRendererV2::legendSymbolItemsV2(); } else { ddSize = symbol->dataDefinedSize(); } } if ( !ddSize.isActive() || !ddSize.useExpression() ) { return QgsFeatureRendererV2::legendSymbolItemsV2(); } QgsScaleExpression exp( ddSize.expressionString() ); if ( exp.type() != QgsScaleExpression::Unknown ) { QgsLegendSymbolItemV2 title( NULL, exp.baseExpression(), "" ); lst << title; foreach ( double v, QgsSymbolLayerV2Utils::prettyBreaks( exp.minValue(), exp.maxValue(), 4 ) ) { QgsLegendSymbolItemV2 si( mSourceSymbol.data(), QString::number( v ), "" ); QgsMarkerSymbolV2 * s = static_cast<QgsMarkerSymbolV2 *>( si.symbol() ); s->setDataDefinedSize( QgsDataDefined() ); s->setSize( exp.size( v ) ); lst << si; } // now list the categorized symbols const QgsLegendSymbolListV2 list2 = QgsFeatureRendererV2::legendSymbolItemsV2() ; foreach ( QgsLegendSymbolItemV2 item, list2 ) lst << item; return lst; }
QgsSymbolV2* QgsSingleSymbolRendererV2::symbolForFeature( QgsFeature& feature ) { if ( mRotationFieldIdx == -1 && mSizeScaleFieldIdx == -1 ) return mSymbol; double rotation = 0; double sizeScale = 1; if ( mRotationFieldIdx != -1 ) { rotation = feature.attributeMap()[mRotationFieldIdx].toDouble(); } if ( mSizeScaleFieldIdx != -1 ) { sizeScale = feature.attributeMap()[mSizeScaleFieldIdx].toDouble(); } if ( mTempSymbol->type() == QgsSymbolV2::Marker ) { QgsMarkerSymbolV2* markerSymbol = static_cast<QgsMarkerSymbolV2*>( mTempSymbol ); if ( mRotationFieldIdx != -1 ) markerSymbol->setAngle( rotation ); if ( mSizeScaleFieldIdx != -1 ) markerSymbol->setSize( sizeScale * mOrigSize ); markerSymbol->setScaleMethod( mScaleMethod ); } else if ( mTempSymbol->type() == QgsSymbolV2::Line ) { QgsLineSymbolV2* lineSymbol = static_cast<QgsLineSymbolV2*>( mTempSymbol ); if ( mSizeScaleFieldIdx != -1 ) lineSymbol->setWidth( sizeScale * mOrigSize ); } else if ( mTempSymbol->type() == QgsSymbolV2::Fill ) { QgsFillSymbolV2* fillSymbol = static_cast<QgsFillSymbolV2*>( mTempSymbol ); if ( mRotationFieldIdx != -1 ) fillSymbol->setAngle( rotation ); } return mTempSymbol; }
void QgsRendererV2Widget::changeSymbolSize() { QList<QgsSymbolV2*> symbolList = selectedSymbols(); if ( symbolList.size() < 1 ) { return; } bool ok; QgsMarkerSymbolV2* marker = dynamic_cast<QgsMarkerSymbolV2*>( symbolList.at( 0 ) ); double size = QInputDialog::getDouble( this, tr( "Size" ), tr( "Change symbol size" ), marker ? marker->size() : 0.0 , 0.0, 999999, 1, &ok ); if ( ok ) { QList<QgsSymbolV2*>::iterator symbolIt = symbolList.begin(); for ( ; symbolIt != symbolList.end(); ++symbolIt ) { marker = dynamic_cast<QgsMarkerSymbolV2*>( *symbolIt ); if ( marker ) marker->setSize( size ); } refreshSymbolView(); } }
void QgsComposerLegend::drawSymbolV2( QPainter* p, QgsSymbolV2* s, double currentYCoord, double& currentXPosition, double& symbolHeight, int layerOpacity ) const { if ( !p || !s ) { return; } double rasterScaleFactor = 1.0; if ( p ) { QPaintDevice* paintDevice = p->device(); if ( !paintDevice ) { return; } rasterScaleFactor = ( paintDevice->logicalDpiX() + paintDevice->logicalDpiY() ) / 2.0 / 25.4; } //consider relation to composer map for symbol sizes in mm bool sizeInMapUnits = s->outputUnit() == QgsSymbolV2::MapUnit; double mmPerMapUnit = 1; if ( mComposerMap ) { mmPerMapUnit = mComposerMap->mapUnitsToMM(); } QgsMarkerSymbolV2* markerSymbol = dynamic_cast<QgsMarkerSymbolV2*>( s ); //Consider symbol size for point markers double height = mSymbolHeight; double width = mSymbolWidth; double size = 0; //Center small marker symbols double widthOffset = 0; double heightOffset = 0; if ( markerSymbol ) { size = markerSymbol->size(); height = size; width = size; if ( mComposerMap && sizeInMapUnits ) { height *= mmPerMapUnit; width *= mmPerMapUnit; markerSymbol->setSize( width ); } if ( width < mSymbolWidth ) { widthOffset = ( mSymbolWidth - width ) / 2.0; } if ( height < mSymbolHeight ) { heightOffset = ( mSymbolHeight - height ) / 2.0; } } p->save(); p->translate( currentXPosition + widthOffset, currentYCoord + heightOffset ); p->scale( 1.0 / rasterScaleFactor, 1.0 / rasterScaleFactor ); if ( markerSymbol && sizeInMapUnits ) { s->setOutputUnit( QgsSymbolV2::MM ); } s->drawPreviewIcon( p, QSize( width * rasterScaleFactor, height * rasterScaleFactor ) ); if ( markerSymbol && sizeInMapUnits ) { s->setOutputUnit( QgsSymbolV2::MapUnit ); markerSymbol->setSize( size ); } p->restore(); currentXPosition += width; currentXPosition += 2 * widthOffset; symbolHeight = height + 2 * heightOffset; }