Skip to content
Permalink
Browse files

Fixed bugs with feature count, maintenance of QgsLegendSymbolItemV2

  • Loading branch information
wonder-sk committed Aug 8, 2014
1 parent f96484f commit 372b7311a84268256bbe9fc840b79c9e4aee3afb
@@ -34,24 +34,26 @@ class QgsLayerTreeModelLegendNode : QObject
};



/**
* Implementation of legend node interface for displaying preview of vector symbols and their labels
* and allowing interaction with the symbol / renderer.
*
* @note added in 2.6
*/
/*
class QgsSymbolV2LegendNode : QgsLayerTreeModelLegendNode
{
%TypeHeaderCode
#include <qgsmaplayerlegend.h>
%End
public:
QgsSymbolV2LegendNode( QgsLayerTreeLayer* nodeLayer, QgsSymbolV2* symbol, const QString& label, int rendererRef = -1 );
QgsSymbolV2LegendNode( QgsLayerTreeLayer* nodeLayer, const QgsLegendSymbolItemV2& item );

virtual Qt::ItemFlags flags() const;
virtual QVariant data( int role ) const;
virtual bool setData( const QVariant& value, int role );
};
};*/


/**
@@ -64,7 +64,7 @@ void QgsComposerLegendItem::writeXMLChildren( QDomElement& elem, QDomDocument& d
////////////////QgsComposerBaseSymbolItem

QgsComposerBaseSymbolItem::QgsComposerBaseSymbolItem()
: QgsComposerLegendItem( QgsComposerLegendStyle::Symbol )
: QgsComposerLegendItem( QgsComposerLegendStyle::Symbol )
{

}
@@ -162,7 +162,7 @@ QgsComposerSymbolV2Item::QgsComposerSymbolV2Item()
}

QgsComposerSymbolV2Item::QgsComposerSymbolV2Item( const QgsLegendSymbolItemV2& item )
: mItem( item )
: mItem( item )
{
setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
}
@@ -189,7 +189,7 @@ QVariant QgsComposerSymbolV2Item::data( int role ) const
if ( role == Qt::DecorationRole )
{
if ( mIcon.isNull() )
mIcon = QgsSymbolLayerV2Utils::symbolPreviewIcon( mItem.symbol, QSize( 30, 30 ) );
mIcon = QgsSymbolLayerV2Utils::symbolPreviewIcon( mItem.symbol(), QSize( 30, 30 ) );
return mIcon;
}
else if ( role == Qt::DisplayRole || role == Qt::EditRole )
@@ -202,7 +202,7 @@ QVariant QgsComposerSymbolV2Item::data( int role ) const
// so that single symbol layers are still drawn on single line
if ( parentLayerItem()->rowCount() > 1 || !lbl.isEmpty() )
{
lbl += QString( " [%1]" ).arg( parentVectorLayer()->featureCount( mItem.symbol ) );
lbl += QString( " [%1]" ).arg( parentVectorLayer()->featureCount( mItem.legacyRuleKey() ) );
}
}
return lbl;
@@ -219,14 +219,14 @@ QStandardItem* QgsComposerSymbolV2Item::clone() const
void QgsComposerSymbolV2Item::writeXML( QDomElement& elem, QDomDocument& doc ) const
{
QDomElement vectorClassElem = doc.createElement( "VectorClassificationItemNg" );
if ( mItem.symbol )
if ( mItem.symbol() )
{
QgsSymbolV2Map saveSymbolMap;
saveSymbolMap.insert( "classificationSymbol", mItem.symbol );
saveSymbolMap.insert( "classificationSymbol", mItem.symbol() );
QDomElement symbolsElem = QgsSymbolLayerV2Utils::saveSymbols( saveSymbolMap, "symbols", doc );
vectorClassElem.appendChild( symbolsElem );
}
vectorClassElem.setAttribute( "text", mItem.label );
vectorClassElem.setAttribute( "text", mItem.label() );
vectorClassElem.setAttribute( "userText", userText() );
elem.appendChild( vectorClassElem );
}
@@ -240,7 +240,7 @@ void QgsComposerSymbolV2Item::readXML( const QDomElement& itemElem, bool xServer
return;
}

mItem.label = itemElem.attribute( "text", "" );
mItem.setLabel( itemElem.attribute( "text", "" ) );
setUserText( itemElem.attribute( "userText", "" ) );
QDomElement symbolsElem = itemElem.firstChildElement( "symbols" );
if ( !symbolsElem.isNull() )
@@ -253,10 +253,10 @@ void QgsComposerSymbolV2Item::readXML( const QDomElement& itemElem, bool xServer
QgsSymbolV2* symbolNg = mapIt.value();
if ( symbolNg )
{
delete mItem.symbol;
mItem.symbol = symbolNg;
mItem.setSymbol( symbolNg );
}
}
qDeleteAll( loadSymbolMap );
}
}

@@ -267,7 +267,7 @@ void QgsComposerSymbolV2Item::setSymbolV2( QgsSymbolV2* s )

QSizeF QgsComposerSymbolV2Item::drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const
{
QgsSymbolV2* s = mItem.symbol;
QgsSymbolV2* s = mItem.symbol();
if ( !s )
{
return QSizeF();
@@ -422,7 +422,7 @@ QString QgsComposerSymbolV2Item::label() const
}
else
{
return mItem.label;
return mItem.label();
}
}
}
@@ -435,8 +435,8 @@ QgsComposerRasterSymbolItem::QgsComposerRasterSymbolItem()
}

QgsComposerRasterSymbolItem::QgsComposerRasterSymbolItem( const QColor& color, const QString& label )
: mColor( color )
, mLabel( label )
: mColor( color )
, mLabel( label )
{
}

@@ -519,7 +519,7 @@ QgsComposerRasterImageItem::QgsComposerRasterImageItem()
}

QgsComposerRasterImageItem::QgsComposerRasterImageItem( const QImage& image )
: mImage( image )
: mImage( image )
{

}
@@ -156,7 +156,7 @@ class CORE_EXPORT QgsComposerSymbolV2Item : public QgsComposerBaseSymbolItem
@deprecated */
Q_DECL_DEPRECATED void setSymbolV2( QgsSymbolV2* s );
/** @deprecated */
Q_DECL_DEPRECATED QgsSymbolV2* symbolV2() const { return mItem.symbol; }
Q_DECL_DEPRECATED QgsSymbolV2* symbolV2() const { return mItem.symbol(); }

ItemType itemType() const { return SymbologyV2Item; }

@@ -165,7 +165,7 @@ class CORE_EXPORT QgsComposerSymbolV2Item : public QgsComposerBaseSymbolItem
QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const;

//! @note added in 2.6
QString ruleKey() const { return mItem.key; }
QString ruleKey() const { return mItem.ruleKey(); }

//! @note added in 2.6
const QgsLegendSymbolItemV2& itemData() const { return mItem; }
@@ -35,8 +35,8 @@
#include <QMessageBox>

QgsLegendModel::QgsLegendModel()
: QStandardItemModel()
, mAutoUpdate( true )
: QStandardItemModel()
, mAutoUpdate( true )
{
setColumnCount( 2 );

@@ -160,7 +160,7 @@ void QgsLegendModel::setLayerSet( const QStringList& layerIds, double scaleDenom
if ( !sItem )
continue;

if ( sItem->itemData().label == rule )
if ( sItem->itemData().label() == rule )
{
QStandardItem* takenSItem = lItem->takeChild( j );
lItem->removeRows( 0, lItem->rowCount() );
@@ -189,13 +189,9 @@ void QgsLegendModel::setLayerSet( const QStringList& layerIds, double scaleDenom
if ( !sItem )
continue;

if ( sItem->itemData().scaleDenomMin > 0 && sItem->itemData().scaleDenomMax > 0 &&
( sItem->itemData().scaleDenomMin > scaleDenominator || sItem->itemData().scaleDenomMax < scaleDenominator ) )
{
if ( !sItem->itemData().isScaleOK( scaleDenominator ) )
lItem->removeRow( j );
}
}

}
}
}
@@ -68,23 +68,21 @@ bool QgsLayerTreeModelLegendNode::setData( const QVariant& value, int role )
// -------------------------------------------------------------------------


QgsSymbolV2LegendNode::QgsSymbolV2LegendNode( QgsLayerTreeLayer* nodeLayer, QgsSymbolV2* symbol, const QString& label, const QString& ruleKey )
QgsSymbolV2LegendNode::QgsSymbolV2LegendNode( QgsLayerTreeLayer* nodeLayer, const QgsLegendSymbolItemV2& item )
: QgsLayerTreeModelLegendNode( nodeLayer )
, mSymbol( symbol ? symbol->clone() : 0 )
, mLabel( label )
, mRuleKey( ruleKey )
, mItem( item )
{
if ( nodeLayer->customProperty( "showFeatureCount", 0 ).toBool() && symbol )
mLabel = mItem.label();
if ( nodeLayer->customProperty( "showFeatureCount", 0 ).toBool() && mItem.legacyRuleKey() )
{
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( nodeLayer->layer() );
if ( vl )
mLabel += QString( " [%1]" ).arg( vl->featureCount( symbol ) );
mLabel += QString( " [%1]" ).arg( vl->featureCount( mItem.legacyRuleKey() ) );
}
}

QgsSymbolV2LegendNode::~QgsSymbolV2LegendNode()
{
delete mSymbol;
}

Qt::ItemFlags QgsSymbolV2LegendNode::flags() const
@@ -105,8 +103,8 @@ QVariant QgsSymbolV2LegendNode::data( int role ) const
else if ( role == Qt::DecorationRole )
{
QSize iconSize( 16, 16 ); // TODO: configurable
if ( mIcon.isNull() && mSymbol )
mIcon = QgsSymbolLayerV2Utils::symbolPreviewPixmap( mSymbol, iconSize );
if ( mIcon.isNull() && mItem.symbol() )
mIcon = QgsSymbolLayerV2Utils::symbolPreviewPixmap( mItem.symbol(), iconSize );
return mIcon;
}
else if ( role == Qt::CheckStateRole )
@@ -121,7 +119,7 @@ QVariant QgsSymbolV2LegendNode::data( int role ) const
if ( !vlayer || !vlayer->rendererV2() )
return QVariant();

return vlayer->rendererV2()->legendSymbolItemChecked( mRuleKey ) ? Qt::Checked : Qt::Unchecked;
return vlayer->rendererV2()->legendSymbolItemChecked( mItem.ruleKey() ) ? Qt::Checked : Qt::Unchecked;
}

return QVariant();
@@ -139,7 +137,7 @@ bool QgsSymbolV2LegendNode::setData( const QVariant& value, int role )
if ( !vlayer || !vlayer->rendererV2() )
return false;

vlayer->rendererV2()->checkLegendSymbolItem( mRuleKey, value == Qt::Checked );
vlayer->rendererV2()->checkLegendSymbolItem( mItem.ruleKey(), value == Qt::Checked );

if ( mParent->isVisible() )
vlayer->clearCacheImage();
@@ -192,7 +190,7 @@ QList<QgsLayerTreeModelLegendNode*> QgsDefaultVectorLayerLegend::createLayerTree

foreach ( const QgsLegendSymbolItemV2& i, r->legendSymbolItemsV2() )
{
nodes.append( new QgsSymbolV2LegendNode( nodeLayer, i.symbol, i.label, i.key ) );
nodes.append( new QgsSymbolV2LegendNode( nodeLayer, i ) );
}
return nodes;
}
@@ -61,6 +61,7 @@ class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
QgsLayerTreeLayer* mParent;
};

#include "qgslegendsymbolitemv2.h"

/**
* Implementation of legend node interface for displaying preview of vector symbols and their labels
@@ -71,18 +72,17 @@ class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
class CORE_EXPORT QgsSymbolV2LegendNode : public QgsLayerTreeModelLegendNode
{
public:
QgsSymbolV2LegendNode( QgsLayerTreeLayer* nodeLayer, QgsSymbolV2* symbol, const QString& label, const QString& ruleKey = QString() );
QgsSymbolV2LegendNode( QgsLayerTreeLayer* nodeLayer, const QgsLegendSymbolItemV2& item );
~QgsSymbolV2LegendNode();

virtual Qt::ItemFlags flags() const;
virtual QVariant data( int role ) const;
virtual bool setData( const QVariant& value, int role );

private:
QgsSymbolV2* mSymbol;
QgsLegendSymbolItemV2 mItem;
mutable QIcon mIcon; // cached symbol preview
QString mLabel;
QString mRuleKey;
};


@@ -3,45 +3,66 @@
#include "qgssymbolv2.h"

QgsLegendSymbolItemV2::QgsLegendSymbolItemV2()
: symbol( 0 )
, scaleDenomMin( -1 )
, scaleDenomMax( -1 )
: mSymbol( 0 )
, mOriginalSymbolPointer( 0 )
, mScaleMinDenom( -1 )
, mScaleMaxDenom( -1 )
{
}

QgsLegendSymbolItemV2::QgsLegendSymbolItemV2( QgsSymbolV2* s, const QString& lbl, const QString& k )
: symbol( s )
, label( lbl )
, key( k )
, scaleDenomMin( -1 )
, scaleDenomMax( -1 )
QgsLegendSymbolItemV2::QgsLegendSymbolItemV2( QgsSymbolV2* symbol, const QString& label, const QString& ruleKey, int scaleMinDenom, int scaleMaxDenom )
: mSymbol( symbol ? symbol->clone() : 0 )
, mLabel( label )
, mKey( ruleKey )
, mOriginalSymbolPointer( symbol )
, mScaleMinDenom( scaleMinDenom )
, mScaleMaxDenom( scaleMaxDenom )
{
}

QgsLegendSymbolItemV2::QgsLegendSymbolItemV2( const QgsLegendSymbolItemV2& other )
: symbol( 0 )
, scaleDenomMin( -1 )
, scaleDenomMax( -1 )
: mSymbol( 0 )
, mOriginalSymbolPointer( 0 )
{
*this = other;
}

QgsLegendSymbolItemV2::~QgsLegendSymbolItemV2()
{
delete symbol;
delete mSymbol;
}

QgsLegendSymbolItemV2& QgsLegendSymbolItemV2::operator=( const QgsLegendSymbolItemV2& other )
QgsLegendSymbolItemV2& QgsLegendSymbolItemV2::operator=( const QgsLegendSymbolItemV2 & other )
{
if ( this == &other )
return *this;

delete symbol;
symbol = other.symbol ? other.symbol->clone() : 0;
label = other.label;
key = other.key;
scaleDenomMin = other.scaleDenomMin;
scaleDenomMax = other.scaleDenomMax;
setSymbol( other.mSymbol ? other.mSymbol->clone() : 0 );
mLabel = other.mLabel;
mKey = other.mKey;
mOriginalSymbolPointer = other.mOriginalSymbolPointer;
mScaleMinDenom = other.mScaleMinDenom;
mScaleMaxDenom = other.mScaleMaxDenom;

return *this;
}

bool QgsLegendSymbolItemV2::isScaleOK( double scale ) const
{
if ( scale <= 0 )
return true;
if ( mScaleMinDenom <= 0 && mScaleMaxDenom <= 0 )
return true;
if ( mScaleMinDenom > 0 && mScaleMinDenom > scale )
return false;
if ( mScaleMaxDenom > 0 && mScaleMaxDenom < scale )
return false;
return true;
}

void QgsLegendSymbolItemV2::setSymbol( QgsSymbolV2* s )
{
delete mSymbol;
mSymbol = s ? s->clone() : 0;
mOriginalSymbolPointer = s;
}

0 comments on commit 372b731

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