Skip to content
Permalink
Browse files

WIP

  • Loading branch information
ccrook committed Sep 18, 2014
1 parent ab3ff17 commit acae80e5ea5532a309845672680f00e67fa84faa
@@ -142,6 +142,8 @@ class CORE_EXPORT QgsCategorizedSymbolRendererV2 : public QgsFeatureRendererV2

QgsSymbolV2* sourceSymbol();
void setSourceSymbol( QgsSymbolV2* sym );
//! @note added in 2.5
void updateSymbols( QgsSymbolV2* sym );

QgsVectorColorRampV2* sourceColorRamp();
void setSourceColorRamp( QgsVectorColorRampV2* ramp );
@@ -150,6 +152,9 @@ class CORE_EXPORT QgsCategorizedSymbolRendererV2 : public QgsFeatureRendererV2
bool invertedColorRamp() { return mInvertedColorRamp; }
void setInvertedColorRamp( bool inverted ) { mInvertedColorRamp = inverted; }

// Update the color ramp used. Also updates all symbols colors.
// @note added in 2.5
void updateColorRamp( QgsVectorColorRampV2* ramp, bool inverted = false );
//! @note added in 1.6
void setRotationField( QString fieldOrExpression );
//! @note added in 1.6
@@ -163,6 +163,7 @@ QgsGraduatedSymbolRendererV2::QgsGraduatedSymbolRendererV2( QString attrName, Qg
mRanges( ranges ),
mMode( Custom ),
mInvertedColorRamp( false ),
mUnits(""),
mScaleMethod( DEFAULT_SCALE_METHOD )
{
// TODO: check ranges for sanity (NULL symbols, invalid ranges)
@@ -331,15 +332,21 @@ bool QgsGraduatedSymbolRendererV2::updateRangeUpperValue( int rangeIndex, double
{
if ( rangeIndex < 0 || rangeIndex >= mRanges.size() )
return false;
mRanges[rangeIndex].setUpperValue( value );
QgsRendererRangeV2 &range=mRanges[rangeIndex];
bool isDefaultLabel= range.label() == defaultRangeLabel(range);
range.setUpperValue( value );
if( isDefaultLabel ) range.setLabel(defaultRangeLabel(range));
return true;
}

bool QgsGraduatedSymbolRendererV2::updateRangeLowerValue( int rangeIndex, double value )
{
if ( rangeIndex < 0 || rangeIndex >= mRanges.size() )
return false;
mRanges[rangeIndex].setLowerValue( value );
QgsRendererRangeV2 &range=mRanges[rangeIndex];
bool isDefaultLabel= range.label() == defaultRangeLabel(range);
range.setLowerValue( value );
if( isDefaultLabel ) range.setLabel(defaultRangeLabel(range));
return true;
}

@@ -351,6 +358,11 @@ bool QgsGraduatedSymbolRendererV2::updateRangeRenderState( int rangeIndex, bool
return true;
}

QString QgsGraduatedSymbolRendererV2::defaultRangeLabel(const QgsRendererRangeV2 & range )
{
return QString::number( range.lowerValue(), 'f', 4 ) + " - " + QString::number( range.upperValue(), 'f', 4 ) + mUnits;
}

QString QgsGraduatedSymbolRendererV2::dump() const
{
QString s = QString( "GRADUATED: attr %1\n" ).arg( mAttrName );
@@ -374,6 +386,7 @@ QgsFeatureRendererV2* QgsGraduatedSymbolRendererV2::clone() const
r->setRotationField( rotationField() );
r->setSizeScaleField( sizeScaleField() );
r->setScaleMethod( scaleMethod() );
r->setUnits( units(), false );
return r;
}

@@ -800,7 +813,8 @@ QgsGraduatedSymbolRendererV2* QgsGraduatedSymbolRendererV2::createRenderer(
Mode mode,
QgsSymbolV2* symbol,
QgsVectorColorRampV2* ramp,
bool inverted )
bool inverted,
QString units )
{
if ( classes < 1 )
return NULL;
@@ -890,12 +904,30 @@ QgsGraduatedSymbolRendererV2* QgsGraduatedSymbolRendererV2::createRenderer(
double lower, upper = minimum;
QString label;

QgsGraduatedSymbolRendererV2* r = new QgsGraduatedSymbolRendererV2( attrName, ranges );
r->setSourceSymbol( symbol->clone() );
r->setSourceColorRamp( ramp->clone() );
r->setInvertedColorRamp( inverted );
r->setMode( mode );
r->setUnits( units );

// "breaks" list contains all values at class breaks plus maximum as last break
int i = 0;
for ( QList<double>::iterator it = breaks.begin(); it != breaks.end(); ++it, ++i )
{
lower = upper; // upper border from last interval
upper = *it;

// Symbol based on range
QgsSymbolV2* newSymbol = symbol->clone();
double colorValue;
if ( inverted ) colorValue = ( breaks.count() > 1 ? ( double )( breaks.count() - i - 1 ) / ( breaks.count() - 1 ) : 0 );
else colorValue = ( breaks.count() > 1 ? ( double ) i / ( breaks.count() - 1 ) : 0 );
newSymbol->setColor( ramp->color( colorValue ) ); // color from (0 / cl-1) to (cl-1 / cl-1)

QgsRendererRangeV2 range = QgsRendererRangeV2( lower, upper, newSymbol, "" );

// Label - either StdDev label or default label for a range
if ( mode == StdDev )
{
if ( i == 0 )
@@ -913,26 +945,18 @@ QgsGraduatedSymbolRendererV2* QgsGraduatedSymbolRendererV2::createRenderer(
}
else
{
label = QString::number( lower, 'f', 4 ) + " - " + QString::number( upper, 'f', 4 );
label=r->defaultRangeLabel(range);
}
range.setLabel(label);

QgsSymbolV2* newSymbol = symbol->clone();
double colorValue;
if ( inverted ) colorValue = ( breaks.count() > 1 ? ( double )( breaks.count() - i - 1 ) / ( breaks.count() - 1 ) : 0 );
else colorValue = ( breaks.count() > 1 ? ( double ) i / ( breaks.count() - 1 ) : 0 );
newSymbol->setColor( ramp->color( colorValue ) ); // color from (0 / cl-1) to (cl-1 / cl-1)

ranges.append( QgsRendererRangeV2( lower, upper, newSymbol, label ) );
r->addClass( range );
}

QgsGraduatedSymbolRendererV2* r = new QgsGraduatedSymbolRendererV2( attrName, ranges );
r->setSourceSymbol( symbol->clone() );
r->setSourceColorRamp( ramp->clone() );
r->setInvertedColorRamp( inverted );
r->setMode( mode );
return r;
}



QgsFeatureRendererV2* QgsGraduatedSymbolRendererV2::create( QDomElement& element )
{
QDomElement symbolsElem = element.firstChildElement( "symbols" );
@@ -1019,6 +1043,12 @@ QgsFeatureRendererV2* QgsGraduatedSymbolRendererV2::create( QDomElement& element
r->setSizeScaleField( sizeScaleElem.attribute( "field" ) );
r->setScaleMethod( QgsSymbolLayerV2Utils::decodeScaleMethod( sizeScaleElem.attribute( "scalemethod" ) ) );

QDomElement unitsElem = element.firstChildElement( "units" );
if( ! unitsElem.isNull() )
{
QString unitString=unitsElem.attribute("name");
r->setUnits(unitString, false);
}
// TODO: symbol levels
return r;
}
@@ -1106,6 +1136,10 @@ QDomElement QgsGraduatedSymbolRendererV2::save( QDomDocument& doc )
sizeScaleElem.setAttribute( "scalemethod", QgsSymbolLayerV2Utils::encodeScaleMethod( mScaleMethod ) );
rendererElem.appendChild( sizeScaleElem );

QDomElement unitsElem=doc.createElement("units");
unitsElem.setAttribute("name",mUnits);
rendererElem.appendChild( unitsElem );

return rendererElem;
}

@@ -1244,7 +1278,6 @@ void QgsGraduatedSymbolRendererV2::addClass( QgsSymbolV2* symbol )
QgsSymbolV2* newSymbol = symbol->clone();
QString label = "0.0 - 0.0";
mRanges.insert( 0, QgsRendererRangeV2( 0.0, 0.0, newSymbol, label ) );

}

void QgsGraduatedSymbolRendererV2::addClass( QgsRendererRangeV2 range )
@@ -1262,6 +1295,23 @@ void QgsGraduatedSymbolRendererV2::deleteAllClasses()
mRanges.clear();
}

void QgsGraduatedSymbolRendererV2::setUnits( QString units, bool updateRanges )
{
if( updateRanges )
{
for ( QgsRangeList::iterator it = mRanges.begin(); it != mRanges.end(); ++it )
{
QString label=it->label();
if( label.endsWith(mUnits))
{
label=label.left(label.length()-mUnits.length())+units;
it->setLabel(label);
}
}
}
mUnits=units;
}

void QgsGraduatedSymbolRendererV2::moveClass( int from, int to )
{
if ( from < 0 || from >= mRanges.size() || to < 0 || to >= mRanges.size() ) return;
@@ -104,6 +104,7 @@ class CORE_EXPORT QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2
bool updateRangeLowerValue( int rangeIndex, double value );
//! @note added in 2.5
bool updateRangeRenderState( int rangeIndex, bool render );
QString defaultRangeLabel( const QgsRendererRangeV2 &range );


void addClass( QgsSymbolV2* symbol );
@@ -131,14 +132,18 @@ class CORE_EXPORT QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2
Mode mode() const { return mMode; }
void setMode( Mode mode ) { mMode = mode; }

QString units() const { return mUnits; }
void setUnits( QString units, bool updateRanges=true );

static QgsGraduatedSymbolRendererV2* createRenderer(
QgsVectorLayer* vlayer,
QString attrName,
int classes,
Mode mode,
QgsSymbolV2* symbol,
QgsVectorColorRampV2* ramp,
bool inverted = false );
bool inverted = false,
QString units = "");

//! create renderer from XML element
static QgsFeatureRendererV2* create( QDomElement& element );
@@ -209,6 +214,7 @@ class CORE_EXPORT QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2

protected:
QString mAttrName;
QString mUnits;
QgsRangeList mRanges;
Mode mMode;
QScopedPointer<QgsSymbolV2> mSourceSymbol;
@@ -338,6 +338,11 @@ void QgsCategorizedSymbolRendererV2Model::sort( int column, Qt::SortOrder order
QgsDebugMsg( "Done" );
}

void QgsCategorizedSymbolRendererV2Model::updateSymbology()
{
emit dataChanged( createIndex( 0, 0, 0 ), createIndex( mRenderer->categories().size(), 0 ) );
}

// ------------------------------ View style --------------------------------
QgsCategorizedSymbolRendererV2ViewStyle::QgsCategorizedSymbolRendererV2ViewStyle( QStyle* style )
: QProxyStyle( style )
@@ -428,6 +433,8 @@ QgsCategorizedSymbolRendererV2Widget::QgsCategorizedSymbolRendererV2Widget( QgsV
connect( btnDeleteCategories, SIGNAL( clicked() ), this, SLOT( deleteCategories() ) );
connect( btnDeleteAllCategories, SIGNAL( clicked() ), this, SLOT( deleteAllCategories() ) );
connect( btnAddCategory, SIGNAL( clicked() ), this, SLOT( addCategory() ) );
connect( cbxInvertedColorRamp, SIGNAL( toggled(bool)), this, SLOT( applyColorRamp()));
connect( cboCategorizedColorRamp, SIGNAL(currentIndexChanged(int)), this, SLOT( applyColorRamp()));

// update GUI from renderer
updateUiFromRenderer();
@@ -585,7 +592,7 @@ void QgsCategorizedSymbolRendererV2Widget::changeCategorySymbol()
mRenderer->updateCategorySymbol( catIdx, symbol );
}

static void _createCategories( QgsCategoryList& cats, QList<QVariant>& values, QgsSymbolV2* symbol, QgsVectorColorRampV2* ramp, bool invert )
static void _createCategories( QgsCategoryList& cats, QList<QVariant>& values, QgsSymbolV2* symbol )
{
// sort the categories first
QgsSymbolLayerV2Utils::sortVariantList( values, Qt::AscendingOrder );
@@ -601,9 +608,7 @@ static void _createCategories( QgsCategoryList& cats, QList<QVariant>& values, Q
{
hasNull = true;
}
double x = ( invert ? num - i : i ) / ( double ) num;
QgsSymbolV2* newSymbol = symbol->clone();
newSymbol->setColor( ramp->color( x ) );

cats.append( QgsRendererCategoryV2( value, newSymbol, value.toString(), true ) );
}
@@ -612,11 +617,34 @@ static void _createCategories( QgsCategoryList& cats, QList<QVariant>& values, Q
if ( !hasNull )
{
QgsSymbolV2* newSymbol = symbol->clone();
newSymbol->setColor( ramp->color( invert ? 0 : 1 ) );
cats.append( QgsRendererCategoryV2( QVariant( "" ), newSymbol, QString(), true ) );
}
}

void QgsCategorizedSymbolRendererV2Widget::applyColorRampToCategories(const QgsCategoryList& cats )
{

QgsVectorColorRampV2* ramp = cboCategorizedColorRamp->currentColorRamp();
bool invert = cbxInvertedColorRamp->isChecked();

if ( ramp == NULL )
{
if ( cboCategorizedColorRamp->count() == 0 )
QMessageBox::critical( this, tr( "Error" ), tr( "There are no available color ramps. You can add them in Style Manager." ) );
else
QMessageBox::critical( this, tr( "Error" ), tr( "The selected color ramp is not available." ) );
return;
}

int num=cats.length()-1;
for( int i=0; i <= num; i++ )
{
double x = ( invert ? num - i : i ) / ( double ) num;
cats.at(i).symbol()->setColor(ramp->color( x ) );
}

}

void QgsCategorizedSymbolRendererV2Widget::addCategories()
{
QString attrName = mExpressionWidget->currentField();
@@ -661,20 +689,8 @@ void QgsCategorizedSymbolRendererV2Widget::addCategories()
return;
#endif

QgsVectorColorRampV2* ramp = cboCategorizedColorRamp->currentColorRamp();

if ( ramp == NULL )
{
if ( cboCategorizedColorRamp->count() == 0 )
QMessageBox::critical( this, tr( "Error" ), tr( "There are no available color ramps. You can add them in Style Manager." ) );
else
QMessageBox::critical( this, tr( "Error" ), tr( "The selected color ramp is not available." ) );
return;
}

QgsCategoryList cats;
_createCategories( cats, unique_vals, mCategorizedSymbol, ramp, cbxInvertedColorRamp->isChecked() );

_createCategories( cats, unique_vals, mCategorizedSymbol );
bool deleteExisting = false;

if ( !mOldClassificationAttribute.isEmpty() &&
@@ -736,7 +752,9 @@ void QgsCategorizedSymbolRendererV2Widget::addCategories()
// recreate renderer
QgsCategorizedSymbolRendererV2 *r = new QgsCategorizedSymbolRendererV2( attrName, cats );
r->setSourceSymbol( mCategorizedSymbol->clone() );
r->setSourceColorRamp( ramp->clone() );
QgsVectorColorRampV2* ramp = cboCategorizedColorRamp->currentColorRamp();
if( ramp ) r->setSourceColorRamp( ramp->clone() );
r->setInvertedColorRamp( cbxInvertedColorRamp->isChecked() );
r->setScaleMethod( mRenderer->scaleMethod() );
r->setSizeScaleField( mRenderer->sizeScaleField() );
r->setRotationField( mRenderer->rotationField() );
@@ -750,6 +768,16 @@ void QgsCategorizedSymbolRendererV2Widget::addCategories()
mRenderer = r;
}

void QgsCategorizedSymbolRendererV2Widget::applyColorRamp()
{
const QgsCategoryList& categories=mRenderer->categories();
if( categories.length() > 0 )
{
applyColorRampToCategories( categories );
mModel->updateSymbology();
}
}

int QgsCategorizedSymbolRendererV2Widget::currentCategoryRow()
{
QModelIndex idx = viewCategories->selectionModel()->currentIndex();
@@ -50,6 +50,7 @@ class GUI_EXPORT QgsCategorizedSymbolRendererV2Model : public QAbstractItemModel
void deleteRows( QList<int> rows );
void removeAllRows( );
void sort( int column, Qt::SortOrder order = Qt::AscendingOrder );
void updateSymbology();

signals:
void rowsMoved();
@@ -85,6 +86,7 @@ class GUI_EXPORT QgsCategorizedSymbolRendererV2Widget : public QgsRendererV2Widg
void categoriesDoubleClicked( const QModelIndex & idx );
void addCategory();
void addCategories();
void applyColorRamp();
void deleteCategories();
void deleteAllCategories();

@@ -116,6 +118,8 @@ class GUI_EXPORT QgsCategorizedSymbolRendererV2Widget : public QgsRendererV2Widg

void changeCategorySymbol();

void applyColorRampToCategories( const QgsCategoryList& cats );

QList<QgsSymbolV2*> selectedSymbols();
QgsCategoryList selectedCategoryList();
void refreshSymbolView() { populateCategories(); }

0 comments on commit acae80e

Please sign in to comment.
You can’t perform that action at this time.