Skip to content

Commit

Permalink
[layertree] respect bold font settings for layer/group
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Jun 13, 2014
1 parent 9af0b37 commit 2e39e60
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
5 changes: 5 additions & 0 deletions python/gui/layertree/qgslayertreemodel.sip
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ class QgsLayerTreeModel : QAbstractItemModel
//! Set index of the current item. May be used by view. Item marked as current is underlined.
void setCurrentIndex( const QModelIndex& currentIndex );

//! Set font for a particular type of layer tree node. nodeType should come from QgsLayerTreeNode::NodeType enumeration
void setLayerTreeNodeFont( int nodeType, const QFont& font );
//! Get font for a particular type of layer tree node. nodeType should come from QgsLayerTreeNode::NodeType enumeration
QFont layerTreeNodeFont( int nodeType ) const;

signals:

};
9 changes: 8 additions & 1 deletion src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2338,7 +2338,14 @@ void QgisApp::setupLayerTreeViewFromSettings()
{
QSettings s;

mLayerTreeView->layerTreeModel()->setFlag( QgsLayerTreeModel::ShowRasterPreviewIcon, s.value( "/qgis/createRasterLegendIcons", false ).toBool() );
QgsLayerTreeModel* model = mLayerTreeView->layerTreeModel();
model->setFlag( QgsLayerTreeModel::ShowRasterPreviewIcon, s.value( "/qgis/createRasterLegendIcons", false ).toBool() );

QFont fontLayer, fontGroup;
fontLayer.setBold( s.value( "/qgis/legendLayersBold", true ).toBool() );
fontGroup.setBold( s.value( "/qgis/legendGroupsBold", false ).toBool() );
model->setLayerTreeNodeFont( QgsLayerTreeNode::NodeLayer, fontLayer );
model->setLayerTreeNodeFont( QgsLayerTreeNode::NodeGroup, fontGroup );
}


Expand Down
57 changes: 54 additions & 3 deletions src/gui/layertree/qgslayertreemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ QgsLayerTreeModel::QgsLayerTreeModel( QgsLayerTreeGroup* rootNode, QObject *pare
connect( mRootNode, SIGNAL( willRemoveChildren( QgsLayerTreeNode*, int, int ) ), this, SLOT( nodeWillRemoveChildren( QgsLayerTreeNode*, int, int ) ) );
connect( mRootNode, SIGNAL( removedChildren( QgsLayerTreeNode*, int, int ) ), this, SLOT( nodeRemovedChildren() ) );
connect( mRootNode, SIGNAL( visibilityChanged( QgsLayerTreeNode*, Qt::CheckState ) ), this, SLOT( nodeVisibilityChanged( QgsLayerTreeNode* ) ) );

mFontLayer.setBold( true );
}

QgsLayerTreeModel::~QgsLayerTreeModel()
Expand Down Expand Up @@ -244,11 +246,9 @@ QVariant QgsLayerTreeModel::data( const QModelIndex &index, int role ) const
}
else if ( role == Qt::FontRole )
{
QFont f;
QFont f( QgsLayerTree::isLayer( node ) ? mFontLayer : ( QgsLayerTree::isGroup( node ) ? mFontGroup : QFont() ) );
if ( node->customProperty( "embedded" ).toInt() )
f.setItalic( true );
if ( QgsLayerTree::isLayer( node ) )
f.setBold( true );
if ( index == mCurrentIndex )
f.setUnderline( true );
return f;
Expand Down Expand Up @@ -452,6 +452,43 @@ void QgsLayerTreeModel::setCurrentIndex( const QModelIndex& currentIndex )
emit dataChanged( currentIndex, currentIndex );
}


void QgsLayerTreeModel::setLayerTreeNodeFont( int nodeType, const QFont& font )
{
if ( nodeType == QgsLayerTreeNode::NodeGroup )
{
if ( mFontGroup != font )
{
mFontGroup = font;
recursivelyEmitDataChanged();
}
}
else if ( nodeType == QgsLayerTreeNode::NodeLayer )
{
if ( mFontLayer != font )
{
mFontLayer = font;
recursivelyEmitDataChanged();
}
}
else
QgsDebugMsg( "invalid node type" );
}


QFont QgsLayerTreeModel::layerTreeNodeFont( int nodeType ) const
{
if ( nodeType == QgsLayerTreeNode::NodeGroup )
return mFontGroup;
else if ( nodeType == QgsLayerTreeNode::NodeLayer )
return mFontLayer;
else
{
QgsDebugMsg( "invalid node type" );
return QFont();
}
}

void QgsLayerTreeModel::nodeWillAddChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo )
{
Q_ASSERT( node );
Expand Down Expand Up @@ -747,6 +784,20 @@ void QgsLayerTreeModel::disconnectFromLayer( QgsLayerTreeLayer* nodeLayer )
}
}

void QgsLayerTreeModel::recursivelyEmitDataChanged( const QModelIndex& idx )
{
QgsLayerTreeNode* node = index2node( idx );
if ( !node )
return;

int count = node->children().count();
if ( count == 0 )
return;
emit dataChanged( index( 0, 0, idx ), index( count - 1, 0, idx ) );
for ( int i = 0; i < count; ++i )
recursivelyEmitDataChanged( index( i, 0, idx ) );
}


Qt::DropActions QgsLayerTreeModel::supportedDropActions() const
{
Expand Down
12 changes: 12 additions & 0 deletions src/gui/layertree/qgslayertreemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define QGSLAYERTREEMODEL_H

#include <QAbstractItemModel>
#include <QFont>
#include <QIcon>

class QgsLayerTreeNode;
Expand Down Expand Up @@ -133,6 +134,11 @@ class GUI_EXPORT QgsLayerTreeModel : public QAbstractItemModel
//! Set index of the current item. May be used by view. Item marked as current is underlined.
void setCurrentIndex( const QModelIndex& currentIndex );

//! Set font for a particular type of layer tree node. nodeType should come from QgsLayerTreeNode::NodeType enumeration
void setLayerTreeNodeFont( int nodeType, const QFont& font );
//! Get font for a particular type of layer tree node. nodeType should come from QgsLayerTreeNode::NodeType enumeration
QFont layerTreeNodeFont( int nodeType ) const;

signals:

protected slots:
Expand All @@ -158,6 +164,9 @@ class GUI_EXPORT QgsLayerTreeModel : public QAbstractItemModel
void connectToLayer( QgsLayerTreeLayer* nodeLayer );
void disconnectFromLayer( QgsLayerTreeLayer* nodeLayer );

//! emit dataChanged() for layer tree node items
void recursivelyEmitDataChanged( const QModelIndex& index = QModelIndex() );

static QgsLayerTreeModelSymbologyNode* index2symnode( const QModelIndex& index );

static const QIcon& iconGroup();
Expand All @@ -171,6 +180,9 @@ class GUI_EXPORT QgsLayerTreeModel : public QAbstractItemModel
QMap<QgsLayerTreeLayer*, QList<QgsLayerTreeModelSymbologyNode*> > mSymbologyNodes;
//! Current index - will be underlined
QPersistentModelIndex mCurrentIndex;

QFont mFontLayer;
QFont mFontGroup;
};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )
Expand Down

0 comments on commit 2e39e60

Please sign in to comment.