Skip to content

Commit 94491b8

Browse files
committed
[FEATURE] Add style options for legend layer/groups, and live refresh of all legend style options
- Based of off work from Stefan Ziegler (pull request #331) - Options inherited by embedded groups (which are still always italic) - Original layer name now saved to project file (instead of adjusted display name) - Fix bug where setting capitalization, then saving/reopening would permanently set capitalization - [API] Add originalName() to QgsMapLayer to access original (pre-adjusted name) - [API] Add setupFont() method to legend group items - [API] Add updateLegendItemStyles() and updateLegendItemSymbologies() to QgsLegend to allow style option changes to become active without app restart
1 parent 9dffa42 commit 94491b8

18 files changed

+307
-82
lines changed

python/core/qgsmaplayer.sip

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ class QgsMapLayer : QObject
7373
*/
7474
const QString & name() const;
7575

76+
/** Get the original name of the layer
77+
* @note added in 1.9
78+
*/
79+
const QString & originalName() const;
80+
7681
void setTitle( const QString& title );
7782
const QString& title() const;
7883

@@ -207,8 +212,8 @@ class QgsMapLayer : QObject
207212
@note emitSignal added in 1.4 */
208213
void setCrs( const QgsCoordinateReferenceSystem& srs, bool emitSignal = true );
209214

210-
/** A convenience function to capitalise the layer name */
211-
static QString capitaliseLayerName( const QString name );
215+
/** A convenience function to (un)capitalise the layer name */
216+
static QString capitaliseLayerName( const QString& name );
212217

213218
/** Retrieve the style URI for this layer
214219
* (either as a .qml file on disk or as a

src/app/legend/qgslegend.cpp

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,8 @@ QgsLegendGroup* QgsLegend::addEmbeddedGroup( const QString& groupName, const QSt
970970
group->setEmbedded( true );
971971
group->setProjectPath( projectFilePath );
972972

973-
QFont groupFont;
973+
// start with already set font style
974+
QFont groupFont = group->font( 0 );
974975
groupFont.setItalic( true );
975976
group->setFont( 0, groupFont );
976977
setCurrentItem( group );
@@ -1075,7 +1076,8 @@ void QgsLegend::addLayers( QList<QgsMapLayer *> theLayerList )
10751076
QgsLegendLayer* llayer = new QgsLegendLayer( layer );
10761077
if ( !QgsProject::instance()->layerIsEmbedded( layer->id() ).isEmpty() )
10771078
{
1078-
QFont itemFont;
1079+
// start with already set font style
1080+
QFont itemFont = llayer->font( 0 );
10791081
itemFont.setItalic( true );
10801082
llayer->setFont( 0, itemFont );
10811083
}
@@ -1897,7 +1899,8 @@ bool QgsLegend::readXML( QgsLegendGroup *parent, const QDomNode &node )
18971899

18981900
if ( currentLayer->layer() && !QgsProject::instance()->layerIsEmbedded( currentLayer->layer()->id() ).isEmpty() )
18991901
{
1900-
QFont itemFont;
1902+
// start with already set font style
1903+
QFont itemFont = currentLayer->font( 0 );
19011904
itemFont.setItalic( true );
19021905
currentLayer->setFont( 0, itemFont );
19031906
}
@@ -3094,3 +3097,35 @@ QList< LegendLayerAction > QgsLegend::legendLayerActions( QgsMapLayer::LayerType
30943097
{
30953098
return mLegendLayerActionMap.contains( type ) ? mLegendLayerActionMap.value( type ) : QList< LegendLayerAction >() ;
30963099
}
3100+
3101+
void QgsLegend::updateLegendItemStyles()
3102+
{
3103+
QgsLegendGroup* lg = 0;
3104+
QgsLegendLayer* ll = 0;
3105+
for ( QTreeWidgetItem* theItem = firstItem(); theItem; theItem = nextItem( theItem ) )
3106+
{
3107+
ll = dynamic_cast<QgsLegendLayer *>( theItem );
3108+
if ( ll )
3109+
{
3110+
ll->setupFont();
3111+
// map layer name capitalize option may have changed
3112+
ll->layer()->setLayerName( ll->layer()->originalName() );
3113+
continue;
3114+
}
3115+
3116+
lg = dynamic_cast<QgsLegendGroup *>( theItem );
3117+
if ( lg )
3118+
{
3119+
lg->setupFont();
3120+
}
3121+
}
3122+
update();
3123+
}
3124+
3125+
void QgsLegend::updateLegendItemSymbologies()
3126+
{
3127+
foreach ( QgsLegendLayer* ll, legendLayers() )
3128+
{
3129+
ll->refreshSymbology( ll->layer()->id() );
3130+
}
3131+
}

src/app/legend/qgslegend.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,17 @@ class QgsLegend : public QTreeWidget
382382
void removeLegendLayerActionsForLayer( QgsMapLayer* layer );
383383
QList< LegendLayerAction > legendLayerActions( QgsMapLayer::LayerType type ) const;
384384

385+
/** Slot to update styles for legend items, since
386+
* QgsLegend::item doesn't work in app stylesheet for individual legend types
387+
* @note added in QGIS 1.9
388+
*/
389+
void updateLegendItemStyles();
390+
391+
/** Slot to update symbology for legend items
392+
* @note added in QGIS 1.9
393+
*/
394+
void updateLegendItemSymbologies();
395+
385396
protected:
386397

387398
/*!Event handler for mouse movements.

src/app/legend/qgslegendgroup.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ QgsLegendGroup::QgsLegendGroup( QTreeWidgetItem * theItem, QString theName )
2727
setCheckState( 0, Qt::Checked );
2828
QIcon myIcon = QgsApplication::getThemeIcon( "/mActionFolder.png" );
2929
setIcon( 0, myIcon );
30+
setupFont();
3031
mEmbedded = false;
3132
mDrawingOrder = -1;
3233
}
@@ -38,6 +39,7 @@ QgsLegendGroup::QgsLegendGroup( QTreeWidget* theListView, QString theString )
3839
setCheckState( 0, Qt::Checked );
3940
QIcon myIcon = QgsApplication::getThemeIcon( "/mActionFolder.png" );
4041
setIcon( 0, myIcon );
42+
setupFont();
4143
mEmbedded = false;
4244
mDrawingOrder = -1;
4345
}
@@ -50,13 +52,22 @@ QgsLegendGroup::QgsLegendGroup( QString name ): QgsLegendItem()
5052
QIcon myIcon = QgsApplication::getThemeIcon( + "/mActionFolder.png" );
5153
setText( 0, name );
5254
setIcon( 0, myIcon );
55+
setupFont();
5356
mEmbedded = false;
5457
mDrawingOrder = -1;
5558
}
5659

5760
QgsLegendGroup::~QgsLegendGroup()
5861
{}
5962

63+
void QgsLegendGroup::setupFont()
64+
{
65+
QSettings settings;
66+
QFont myFont = font( 0 );
67+
//visually differentiate group labels from the rest
68+
myFont.setBold( settings.value( "/qgis/legendGroupsBold", false ).toBool() );
69+
setFont( 0, myFont );
70+
}
6071

6172
bool QgsLegendGroup::insert( QgsLegendItem* theItem )
6273
{

src/app/legend/qgslegendgroup.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ class QgsLegendGroup : public QgsLegendItem
3232
QgsLegendGroup( QString name );
3333
~QgsLegendGroup();
3434

35+
/** Helper method to set font characteristics.
36+
* Not to be confused with setFont() which is inherited
37+
* from the QTreeWidgetItem base class.
38+
* @note added in QGIS 1.9
39+
*/
40+
void setupFont();
41+
3542
bool insert( QgsLegendItem* theItem );
3643
/**Returns all legend layers under this group (including those of subgroups by default)*/
3744
QList<QgsLegendLayer*> legendLayers( bool recurse = true );

src/app/legend/qgslegendlayer.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,12 @@ void QgsLegendLayer::setCheckState( int column, Qt::CheckState state )
112112
}
113113
}
114114

115-
void QgsLegendLayer::setupFont() //private method
115+
void QgsLegendLayer::setupFont()
116116
{
117+
QSettings settings;
117118
QFont myFont = font( 0 );
118-
myFont.setBold( true ); //visually differentiate layer labels from the rest
119+
//visually differentiate layer labels from the rest
120+
myFont.setBold( settings.value( "/qgis/legendLayersBold", true ).toBool() );
119121
setFont( 0, myFont );
120122
}
121123

src/app/legend/qgslegendlayer.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ class QgsLegendLayer : public QgsLegendItem
5353
/**Updates symbology of the layer and copies symbology to other layer files in the group*/
5454
void refreshSymbology( const QString& key, double widthScale = 1.0 );
5555

56+
/** Helper method to set font characteristics.
57+
* Not to be confused with setFont() which is inherited
58+
* from the QTreeWidgetItem base class.
59+
*/
60+
void setupFont();
61+
5662
/** called to add appropriate menu items to legend's popup menu */
5763
void addToPopupMenu( QMenu& theMenu );
5864

@@ -122,11 +128,6 @@ class QgsLegendLayer : public QgsLegendItem
122128
QPixmap getOriginalPixmap();
123129

124130
private:
125-
/** Helper method to make the font bold from all ctors.
126-
* Not to be confused with setFont() which is inherited
127-
* from the QTreeWidgetItem base class.
128-
*/
129-
void setupFont();
130131

131132
/** Label, may be layer name or layer name + [feature count] */
132133
QString label();

src/app/qgsoptions.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "qgis.h"
2222
#include "qgisapp.h"
2323
#include "qgisappstylesheet.h"
24+
#include "qgslegend.h"
2425
#include "qgsmapcanvas.h"
2526
#include "qgsmaprenderer.h"
2627
#include "qgsgenericprojectionselector.h"
@@ -504,6 +505,8 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
504505
chkUseQPixmap->setChecked( !( settings.value( "/qgis/use_qimage_to_render", true ).toBool() ) );
505506
chkAddedVisibility->setChecked( settings.value( "/qgis/new_layers_visible", true ).toBool() );
506507
cbxLegendClassifiers->setChecked( settings.value( "/qgis/showLegendClassifiers", false ).toBool() );
508+
mLegendLayersBoldChkBx->setChecked( settings.value( "/qgis/legendLayersBold", true ).toBool() );
509+
mLegendGroupsBoldChkBx->setChecked( settings.value( "/qgis/legendGroupsBold", false ).toBool() );
507510
cbxHideSplash->setChecked( settings.value( "/qgis/hideSplash", false ).toBool() );
508511
cbxShowTips->setChecked( settings.value( "/qgis/showTips", true ).toBool() );
509512
cbxAttributeTableDocked->setChecked( settings.value( "/qgis/dockAttributeTable", false ).toBool() );
@@ -562,7 +565,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
562565
myBlue = settings.value( "/qgis/default_measure_color_blue", 180 ).toInt();
563566
pbnMeasureColor->setColor( QColor( myRed, myGreen, myBlue ) );
564567

565-
capitaliseCheckBox->setChecked( settings.value( "qgis/capitaliseLayerName", QVariant( false ) ).toBool() );
568+
capitaliseCheckBox->setChecked( settings.value( "/qgis/capitaliseLayerName", QVariant( false ) ).toBool() );
566569

567570
chbAskToSaveProjectChanges->setChecked( settings.value( "qgis/askToSaveProjectChanges", QVariant( true ) ).toBool() );
568571
chbWarnOldProjectVersion->setChecked( settings.value( "/qgis/warnOldProjectVersion", QVariant( true ) ).toBool() );
@@ -1003,7 +1006,12 @@ void QgsOptions::saveOptions()
10031006
settings.setValue( "/Map/identifyMode", cmbIdentifyMode->itemData( cmbIdentifyMode->currentIndex() ).toInt() );
10041007
settings.setValue( "/Map/identifyAutoFeatureForm", cbxAutoFeatureForm->isChecked() );
10051008
settings.setValue( "/Map/identifyRadius", spinBoxIdentifyValue->value() );
1009+
bool showLegendClassifiers = settings.value( "/qgis/showLegendClassifiers", false ).toBool();
10061010
settings.setValue( "/qgis/showLegendClassifiers", cbxLegendClassifiers->isChecked() );
1011+
bool legendLayersBold = settings.value( "/qgis/legendLayersBold", true ).toBool();
1012+
settings.setValue( "/qgis/legendLayersBold", mLegendLayersBoldChkBx->isChecked() );
1013+
bool legendGroupsBold = settings.value( "/qgis/legendGroupsBold", false ).toBool();
1014+
settings.setValue( "/qgis/legendGroupsBold", mLegendGroupsBoldChkBx->isChecked() );
10071015
settings.setValue( "/qgis/hideSplash", cbxHideSplash->isChecked() );
10081016
settings.setValue( "/qgis/showTips", cbxShowTips->isChecked() );
10091017
settings.setValue( "/qgis/dockAttributeTable", cbxAttributeTableDocked->isChecked() );
@@ -1019,6 +1027,7 @@ void QgsOptions::saveOptions()
10191027
settings.setValue( "/qgis/dockSnapping", cbxSnappingOptionsDocked->isChecked() );
10201028
settings.setValue( "/qgis/addPostgisDC", cbxAddPostgisDC->isChecked() );
10211029
settings.setValue( "/qgis/addNewLayersToCurrentGroup", cbxAddNewLayersToCurrentGroup->isChecked() );
1030+
bool createRasterLegendIcons = settings.value( "/qgis/createRasterLegendIcons", true ).toBool();
10221031
settings.setValue( "/qgis/createRasterLegendIcons", cbxCreateRasterLegendIcons->isChecked() );
10231032
settings.setValue( "/qgis/copyGeometryAsWKT", cbxCopyWKTGeomFromTable->isChecked() );
10241033
settings.setValue( "/qgis/new_layers_visible", chkAddedVisibility->isChecked() );
@@ -1027,6 +1036,7 @@ void QgsOptions::saveOptions()
10271036
settings.setValue( "/qgis/use_qimage_to_render", !( chkUseQPixmap->isChecked() ) );
10281037
settings.setValue( "/qgis/use_symbology_ng", chkUseSymbologyNG->isChecked() );
10291038
settings.setValue( "/qgis/legendDoubleClickAction", cmbLegendDoubleClickAction->currentIndex() );
1039+
bool legendLayersCapitalise = settings.value( "/qgis/capitaliseLayerName", false ).toBool();
10301040
settings.setValue( "/qgis/capitaliseLayerName", capitaliseCheckBox->isChecked() );
10311041

10321042
// project
@@ -1233,6 +1243,21 @@ void QgsOptions::saveOptions()
12331243
if ( mLoadedGdalDriverList )
12341244
saveGdalDriverList();
12351245

1246+
// refresh legend if any legend item's state is to be changed
1247+
if ( legendLayersBold != mLegendLayersBoldChkBx->isChecked()
1248+
|| legendGroupsBold != mLegendGroupsBoldChkBx->isChecked()
1249+
|| legendLayersCapitalise != capitaliseCheckBox->isChecked() )
1250+
{
1251+
QgisApp::instance()->legend()->updateLegendItemStyles();
1252+
}
1253+
1254+
// refresh symbology for any legend items, only if needed
1255+
if ( showLegendClassifiers != cbxLegendClassifiers->isChecked()
1256+
|| createRasterLegendIcons != cbxCreateRasterLegendIcons->isChecked() )
1257+
{
1258+
QgisApp::instance()->legend()->updateLegendItemSymbologies();
1259+
}
1260+
12361261
// save app stylesheet last (in case reset becomes necessary)
12371262
if ( mStyleSheetNewOpts != mStyleSheetOldOpts )
12381263
{

src/app/qgsrasterlayerproperties.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ void QgsRasterLayerProperties::sync()
591591

592592
//these properties (layer name and label) are provided by the qgsmaplayer superclass
593593
leLayerSource->setText( mRasterLayer->source() );
594+
mLayerOrigNameLineEd->setText( mRasterLayer->originalName() );
594595
leDisplayName->setText( mRasterLayer->name() );
595596

596597
//display the raster dimensions and no data value
@@ -749,7 +750,7 @@ void QgsRasterLayerProperties::apply()
749750
/*
750751
* General Tab
751752
*/
752-
mRasterLayer->setLayerName( leDisplayName->text() );
753+
mRasterLayer->setLayerName( mLayerOrigNameLineEd->text() );
753754

754755
// set up the scale based layer visibility stuff....
755756
mRasterLayer->toggleScaleBasedVisibility( chkUseScaleDependentRendering->isChecked() );
@@ -819,6 +820,11 @@ void QgsRasterLayerProperties::apply()
819820
updatePipeList();
820821
}//apply
821822

823+
void QgsRasterLayerProperties::on_mLayerOrigNameLineEd_textEdited( const QString& text )
824+
{
825+
leDisplayName->setText( mRasterLayer->capitaliseLayerName( text ) );
826+
}
827+
822828
void QgsRasterLayerProperties::on_buttonBuildPyramids_clicked()
823829
{
824830
QgsRasterDataProvider* provider = mRasterLayer->dataProvider();

src/app/qgsrasterlayerproperties.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class QgsRasterLayerProperties : public QDialog, private Ui::QgsRasterLayerPrope
5656
//TODO: Verify that these all need to be public
5757
/** \brief Applies the settings made in the dialog without closing the box */
5858
void apply();
59+
/** \brief Slot to update layer display name as original is edited
60+
* @note added in QGIS 1.9 */
61+
void on_mLayerOrigNameLineEd_textEdited( const QString& text );
5962
/** \brief this slot asks the rasterlayer to construct pyramids */
6063
void on_buttonBuildPyramids_clicked();
6164
/** \brief slot executed when user presses "Add Values From Display" button on the transparency page */

0 commit comments

Comments
 (0)