Skip to content

Commit

Permalink
dxf export: fix display of check states on upper levels
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Dec 16, 2014
1 parent 2363ae5 commit 8076e53
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
78 changes: 70 additions & 8 deletions src/app/qgsdxfexportdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,51 @@ QVariant QgsVectorLayerAndAttributeModel::data( const QModelIndex& idx, int role
if ( idx.column() == 0 )
{
if ( role == Qt::CheckStateRole )
return mCheckedIndexes.contains( idx ) ? Qt::Checked : Qt::Unchecked;
{
if ( !idx.isValid() )
return QVariant();

if ( mCheckedLeafs.contains( idx ) )
return Qt::Checked;

bool hasChecked = false, hasUnchecked = false;
int n;
for ( n = 0; !hasChecked || !hasUnchecked; n++ )
{
QVariant v = data( idx.child( n, 0 ), role );
if ( !v.isValid() )
break;

switch ( v.toInt() )
{
case Qt::PartiallyChecked:
// parent of partially checked child shared state
return Qt::PartiallyChecked;

case Qt::Checked:
hasChecked = true;
break;

case Qt::Unchecked:
hasUnchecked = true;
break;
}
}

// unchecked leaf
if ( n == 0 )
return Qt::Unchecked;

// both
if ( hasChecked && hasUnchecked )
return Qt::PartiallyChecked;

if ( hasChecked )
return Qt::Checked;

Q_ASSERT( hasUnchecked );
return Qt::Unchecked;
}
else
return QgsLayerTreeModel::data( idx, role );
}
Expand Down Expand Up @@ -165,10 +209,28 @@ bool QgsVectorLayerAndAttributeModel::setData( const QModelIndex &index, const Q
{
if ( index.column() == 0 && role == Qt::CheckStateRole )
{
if ( value.toInt() == Qt::Checked )
mCheckedIndexes.append( index );
else
mCheckedIndexes.removeAll( index );
int i = 0;
for ( i = 0; ; i++ )
{
QModelIndex child = index.child( i, 0 );
if ( !child.isValid() )
break;

setData( child, value, role );
}

if ( i == 0 )
{
if ( value.toInt() == Qt::Checked )
mCheckedLeafs.insert( index );
else if ( value.toInt() == Qt::Unchecked )
mCheckedLeafs.remove( index );
else
Q_ASSERT( "expected checked or unchecked" );

emit dataChanged( QModelIndex(), index );
}

return true;
}

Expand All @@ -194,7 +256,7 @@ QList< QPair<QgsVectorLayer *, int> > QgsVectorLayerAndAttributeModel::layers()
QList< QPair<QgsVectorLayer *, int> > layers;
QHash< QgsMapLayer *, int > layerIdx;

foreach ( const QModelIndex &idx, mCheckedIndexes )
foreach ( const QModelIndex &idx, mCheckedLeafs )
{
QgsLayerTreeNode *node = index2node( idx );
if ( QgsLayerTree::isGroup( node ) )
Expand Down Expand Up @@ -245,7 +307,7 @@ void QgsVectorLayerAndAttributeModel::applyVisibilityPreset( const QString &name
if ( visibleLayers.isEmpty() )
return;

mCheckedIndexes.clear();
mCheckedLeafs.clear();
applyVisibility( visibleLayers, rootGroup() );

emit dataChanged( QModelIndex(), QModelIndex() );
Expand All @@ -265,7 +327,7 @@ void QgsVectorLayerAndAttributeModel::applyVisibility( QSet<QString> &visibleLay
if ( vl && visibleLayers.contains( vl->id() ) )
{
visibleLayers.remove( vl->id() );
mCheckedIndexes.append( node2index( child ) );
mCheckedLeafs.insert( node2index( child ) );
}
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsdxfexportdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class QgsVectorLayerAndAttributeModel : public QgsLayerTreeModel

private:
QHash<const QgsVectorLayer *, int> mAttributeIdx;
QModelIndexList mCheckedIndexes;
QSet<QModelIndex> mCheckedLeafs;

void applyVisibility( QSet<QString> &visibleLayers, QgsLayerTreeNode *node );
};
Expand Down

0 comments on commit 8076e53

Please sign in to comment.