Skip to content

Commit 2e39e60

Browse files
committedJun 13, 2014
[layertree] respect bold font settings for layer/group
1 parent 9af0b37 commit 2e39e60

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed
 

‎python/gui/layertree/qgslayertreemodel.sip

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ class QgsLayerTreeModel : QAbstractItemModel
8888
//! Set index of the current item. May be used by view. Item marked as current is underlined.
8989
void setCurrentIndex( const QModelIndex& currentIndex );
9090

91+
//! Set font for a particular type of layer tree node. nodeType should come from QgsLayerTreeNode::NodeType enumeration
92+
void setLayerTreeNodeFont( int nodeType, const QFont& font );
93+
//! Get font for a particular type of layer tree node. nodeType should come from QgsLayerTreeNode::NodeType enumeration
94+
QFont layerTreeNodeFont( int nodeType ) const;
95+
9196
signals:
9297

9398
};

‎src/app/qgisapp.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2338,7 +2338,14 @@ void QgisApp::setupLayerTreeViewFromSettings()
23382338
{
23392339
QSettings s;
23402340

2341-
mLayerTreeView->layerTreeModel()->setFlag( QgsLayerTreeModel::ShowRasterPreviewIcon, s.value( "/qgis/createRasterLegendIcons", false ).toBool() );
2341+
QgsLayerTreeModel* model = mLayerTreeView->layerTreeModel();
2342+
model->setFlag( QgsLayerTreeModel::ShowRasterPreviewIcon, s.value( "/qgis/createRasterLegendIcons", false ).toBool() );
2343+
2344+
QFont fontLayer, fontGroup;
2345+
fontLayer.setBold( s.value( "/qgis/legendLayersBold", true ).toBool() );
2346+
fontGroup.setBold( s.value( "/qgis/legendGroupsBold", false ).toBool() );
2347+
model->setLayerTreeNodeFont( QgsLayerTreeNode::NodeLayer, fontLayer );
2348+
model->setLayerTreeNodeFont( QgsLayerTreeNode::NodeGroup, fontGroup );
23422349
}
23432350

23442351

‎src/gui/layertree/qgslayertreemodel.cpp

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ QgsLayerTreeModel::QgsLayerTreeModel( QgsLayerTreeGroup* rootNode, QObject *pare
4040
connect( mRootNode, SIGNAL( willRemoveChildren( QgsLayerTreeNode*, int, int ) ), this, SLOT( nodeWillRemoveChildren( QgsLayerTreeNode*, int, int ) ) );
4141
connect( mRootNode, SIGNAL( removedChildren( QgsLayerTreeNode*, int, int ) ), this, SLOT( nodeRemovedChildren() ) );
4242
connect( mRootNode, SIGNAL( visibilityChanged( QgsLayerTreeNode*, Qt::CheckState ) ), this, SLOT( nodeVisibilityChanged( QgsLayerTreeNode* ) ) );
43+
44+
mFontLayer.setBold( true );
4345
}
4446

4547
QgsLayerTreeModel::~QgsLayerTreeModel()
@@ -244,11 +246,9 @@ QVariant QgsLayerTreeModel::data( const QModelIndex &index, int role ) const
244246
}
245247
else if ( role == Qt::FontRole )
246248
{
247-
QFont f;
249+
QFont f( QgsLayerTree::isLayer( node ) ? mFontLayer : ( QgsLayerTree::isGroup( node ) ? mFontGroup : QFont() ) );
248250
if ( node->customProperty( "embedded" ).toInt() )
249251
f.setItalic( true );
250-
if ( QgsLayerTree::isLayer( node ) )
251-
f.setBold( true );
252252
if ( index == mCurrentIndex )
253253
f.setUnderline( true );
254254
return f;
@@ -452,6 +452,43 @@ void QgsLayerTreeModel::setCurrentIndex( const QModelIndex& currentIndex )
452452
emit dataChanged( currentIndex, currentIndex );
453453
}
454454

455+
456+
void QgsLayerTreeModel::setLayerTreeNodeFont( int nodeType, const QFont& font )
457+
{
458+
if ( nodeType == QgsLayerTreeNode::NodeGroup )
459+
{
460+
if ( mFontGroup != font )
461+
{
462+
mFontGroup = font;
463+
recursivelyEmitDataChanged();
464+
}
465+
}
466+
else if ( nodeType == QgsLayerTreeNode::NodeLayer )
467+
{
468+
if ( mFontLayer != font )
469+
{
470+
mFontLayer = font;
471+
recursivelyEmitDataChanged();
472+
}
473+
}
474+
else
475+
QgsDebugMsg( "invalid node type" );
476+
}
477+
478+
479+
QFont QgsLayerTreeModel::layerTreeNodeFont( int nodeType ) const
480+
{
481+
if ( nodeType == QgsLayerTreeNode::NodeGroup )
482+
return mFontGroup;
483+
else if ( nodeType == QgsLayerTreeNode::NodeLayer )
484+
return mFontLayer;
485+
else
486+
{
487+
QgsDebugMsg( "invalid node type" );
488+
return QFont();
489+
}
490+
}
491+
455492
void QgsLayerTreeModel::nodeWillAddChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo )
456493
{
457494
Q_ASSERT( node );
@@ -747,6 +784,20 @@ void QgsLayerTreeModel::disconnectFromLayer( QgsLayerTreeLayer* nodeLayer )
747784
}
748785
}
749786

787+
void QgsLayerTreeModel::recursivelyEmitDataChanged( const QModelIndex& idx )
788+
{
789+
QgsLayerTreeNode* node = index2node( idx );
790+
if ( !node )
791+
return;
792+
793+
int count = node->children().count();
794+
if ( count == 0 )
795+
return;
796+
emit dataChanged( index( 0, 0, idx ), index( count - 1, 0, idx ) );
797+
for ( int i = 0; i < count; ++i )
798+
recursivelyEmitDataChanged( index( i, 0, idx ) );
799+
}
800+
750801

751802
Qt::DropActions QgsLayerTreeModel::supportedDropActions() const
752803
{

‎src/gui/layertree/qgslayertreemodel.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define QGSLAYERTREEMODEL_H
1818

1919
#include <QAbstractItemModel>
20+
#include <QFont>
2021
#include <QIcon>
2122

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

137+
//! Set font for a particular type of layer tree node. nodeType should come from QgsLayerTreeNode::NodeType enumeration
138+
void setLayerTreeNodeFont( int nodeType, const QFont& font );
139+
//! Get font for a particular type of layer tree node. nodeType should come from QgsLayerTreeNode::NodeType enumeration
140+
QFont layerTreeNodeFont( int nodeType ) const;
141+
136142
signals:
137143

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

167+
//! emit dataChanged() for layer tree node items
168+
void recursivelyEmitDataChanged( const QModelIndex& index = QModelIndex() );
169+
161170
static QgsLayerTreeModelSymbologyNode* index2symnode( const QModelIndex& index );
162171

163172
static const QIcon& iconGroup();
@@ -171,6 +180,9 @@ class GUI_EXPORT QgsLayerTreeModel : public QAbstractItemModel
171180
QMap<QgsLayerTreeLayer*, QList<QgsLayerTreeModelSymbologyNode*> > mSymbologyNodes;
172181
//! Current index - will be underlined
173182
QPersistentModelIndex mCurrentIndex;
183+
184+
QFont mFontLayer;
185+
QFont mFontGroup;
174186
};
175187

176188
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )

0 commit comments

Comments
 (0)