Skip to content
Permalink
Browse files
[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
  • Loading branch information
dakcarto committed Jan 28, 2013
1 parent 9dffa42 commit 94491b847979e032e4a8c5d39b667513bb192088
@@ -73,6 +73,11 @@ class QgsMapLayer : QObject
*/
const QString & name() const;

/** Get the original name of the layer
* @note added in 1.9
*/
const QString & originalName() const;

void setTitle( const QString& title );
const QString& title() const;

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

/** A convenience function to capitalise the layer name */
static QString capitaliseLayerName( const QString name );
/** A convenience function to (un)capitalise the layer name */
static QString capitaliseLayerName( const QString& name );

/** Retrieve the style URI for this layer
* (either as a .qml file on disk or as a
@@ -970,7 +970,8 @@ QgsLegendGroup* QgsLegend::addEmbeddedGroup( const QString& groupName, const QSt
group->setEmbedded( true );
group->setProjectPath( projectFilePath );

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

if ( currentLayer->layer() && !QgsProject::instance()->layerIsEmbedded( currentLayer->layer()->id() ).isEmpty() )
{
QFont itemFont;
// start with already set font style
QFont itemFont = currentLayer->font( 0 );
itemFont.setItalic( true );
currentLayer->setFont( 0, itemFont );
}
@@ -3094,3 +3097,35 @@ QList< LegendLayerAction > QgsLegend::legendLayerActions( QgsMapLayer::LayerType
{
return mLegendLayerActionMap.contains( type ) ? mLegendLayerActionMap.value( type ) : QList< LegendLayerAction >() ;
}

void QgsLegend::updateLegendItemStyles()
{
QgsLegendGroup* lg = 0;
QgsLegendLayer* ll = 0;
for ( QTreeWidgetItem* theItem = firstItem(); theItem; theItem = nextItem( theItem ) )
{
ll = dynamic_cast<QgsLegendLayer *>( theItem );
if ( ll )
{
ll->setupFont();
// map layer name capitalize option may have changed
ll->layer()->setLayerName( ll->layer()->originalName() );
continue;
}

lg = dynamic_cast<QgsLegendGroup *>( theItem );
if ( lg )
{
lg->setupFont();
}
}
update();
}

void QgsLegend::updateLegendItemSymbologies()
{
foreach ( QgsLegendLayer* ll, legendLayers() )
{
ll->refreshSymbology( ll->layer()->id() );
}
}
@@ -382,6 +382,17 @@ class QgsLegend : public QTreeWidget
void removeLegendLayerActionsForLayer( QgsMapLayer* layer );
QList< LegendLayerAction > legendLayerActions( QgsMapLayer::LayerType type ) const;

/** Slot to update styles for legend items, since
* QgsLegend::item doesn't work in app stylesheet for individual legend types
* @note added in QGIS 1.9
*/
void updateLegendItemStyles();

/** Slot to update symbology for legend items
* @note added in QGIS 1.9
*/
void updateLegendItemSymbologies();

protected:

/*!Event handler for mouse movements.
@@ -27,6 +27,7 @@ QgsLegendGroup::QgsLegendGroup( QTreeWidgetItem * theItem, QString theName )
setCheckState( 0, Qt::Checked );
QIcon myIcon = QgsApplication::getThemeIcon( "/mActionFolder.png" );
setIcon( 0, myIcon );
setupFont();
mEmbedded = false;
mDrawingOrder = -1;
}
@@ -38,6 +39,7 @@ QgsLegendGroup::QgsLegendGroup( QTreeWidget* theListView, QString theString )
setCheckState( 0, Qt::Checked );
QIcon myIcon = QgsApplication::getThemeIcon( "/mActionFolder.png" );
setIcon( 0, myIcon );
setupFont();
mEmbedded = false;
mDrawingOrder = -1;
}
@@ -50,13 +52,22 @@ QgsLegendGroup::QgsLegendGroup( QString name ): QgsLegendItem()
QIcon myIcon = QgsApplication::getThemeIcon( + "/mActionFolder.png" );
setText( 0, name );
setIcon( 0, myIcon );
setupFont();
mEmbedded = false;
mDrawingOrder = -1;
}

QgsLegendGroup::~QgsLegendGroup()
{}

void QgsLegendGroup::setupFont()
{
QSettings settings;
QFont myFont = font( 0 );
//visually differentiate group labels from the rest
myFont.setBold( settings.value( "/qgis/legendGroupsBold", false ).toBool() );
setFont( 0, myFont );
}

bool QgsLegendGroup::insert( QgsLegendItem* theItem )
{
@@ -32,6 +32,13 @@ class QgsLegendGroup : public QgsLegendItem
QgsLegendGroup( QString name );
~QgsLegendGroup();

/** Helper method to set font characteristics.
* Not to be confused with setFont() which is inherited
* from the QTreeWidgetItem base class.
* @note added in QGIS 1.9
*/
void setupFont();

bool insert( QgsLegendItem* theItem );
/**Returns all legend layers under this group (including those of subgroups by default)*/
QList<QgsLegendLayer*> legendLayers( bool recurse = true );
@@ -112,10 +112,12 @@ void QgsLegendLayer::setCheckState( int column, Qt::CheckState state )
}
}

void QgsLegendLayer::setupFont() //private method
void QgsLegendLayer::setupFont()
{
QSettings settings;
QFont myFont = font( 0 );
myFont.setBold( true ); //visually differentiate layer labels from the rest
//visually differentiate layer labels from the rest
myFont.setBold( settings.value( "/qgis/legendLayersBold", true ).toBool() );
setFont( 0, myFont );
}

@@ -53,6 +53,12 @@ class QgsLegendLayer : public QgsLegendItem
/**Updates symbology of the layer and copies symbology to other layer files in the group*/
void refreshSymbology( const QString& key, double widthScale = 1.0 );

/** Helper method to set font characteristics.
* Not to be confused with setFont() which is inherited
* from the QTreeWidgetItem base class.
*/
void setupFont();

/** called to add appropriate menu items to legend's popup menu */
void addToPopupMenu( QMenu& theMenu );

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

private:
/** Helper method to make the font bold from all ctors.
* Not to be confused with setFont() which is inherited
* from the QTreeWidgetItem base class.
*/
void setupFont();

/** Label, may be layer name or layer name + [feature count] */
QString label();
@@ -21,6 +21,7 @@
#include "qgis.h"
#include "qgisapp.h"
#include "qgisappstylesheet.h"
#include "qgslegend.h"
#include "qgsmapcanvas.h"
#include "qgsmaprenderer.h"
#include "qgsgenericprojectionselector.h"
@@ -504,6 +505,8 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
chkUseQPixmap->setChecked( !( settings.value( "/qgis/use_qimage_to_render", true ).toBool() ) );
chkAddedVisibility->setChecked( settings.value( "/qgis/new_layers_visible", true ).toBool() );
cbxLegendClassifiers->setChecked( settings.value( "/qgis/showLegendClassifiers", false ).toBool() );
mLegendLayersBoldChkBx->setChecked( settings.value( "/qgis/legendLayersBold", true ).toBool() );
mLegendGroupsBoldChkBx->setChecked( settings.value( "/qgis/legendGroupsBold", false ).toBool() );
cbxHideSplash->setChecked( settings.value( "/qgis/hideSplash", false ).toBool() );
cbxShowTips->setChecked( settings.value( "/qgis/showTips", true ).toBool() );
cbxAttributeTableDocked->setChecked( settings.value( "/qgis/dockAttributeTable", false ).toBool() );
@@ -562,7 +565,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
myBlue = settings.value( "/qgis/default_measure_color_blue", 180 ).toInt();
pbnMeasureColor->setColor( QColor( myRed, myGreen, myBlue ) );

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

chbAskToSaveProjectChanges->setChecked( settings.value( "qgis/askToSaveProjectChanges", QVariant( true ) ).toBool() );
chbWarnOldProjectVersion->setChecked( settings.value( "/qgis/warnOldProjectVersion", QVariant( true ) ).toBool() );
@@ -1003,7 +1006,12 @@ void QgsOptions::saveOptions()
settings.setValue( "/Map/identifyMode", cmbIdentifyMode->itemData( cmbIdentifyMode->currentIndex() ).toInt() );
settings.setValue( "/Map/identifyAutoFeatureForm", cbxAutoFeatureForm->isChecked() );
settings.setValue( "/Map/identifyRadius", spinBoxIdentifyValue->value() );
bool showLegendClassifiers = settings.value( "/qgis/showLegendClassifiers", false ).toBool();
settings.setValue( "/qgis/showLegendClassifiers", cbxLegendClassifiers->isChecked() );
bool legendLayersBold = settings.value( "/qgis/legendLayersBold", true ).toBool();
settings.setValue( "/qgis/legendLayersBold", mLegendLayersBoldChkBx->isChecked() );
bool legendGroupsBold = settings.value( "/qgis/legendGroupsBold", false ).toBool();
settings.setValue( "/qgis/legendGroupsBold", mLegendGroupsBoldChkBx->isChecked() );
settings.setValue( "/qgis/hideSplash", cbxHideSplash->isChecked() );
settings.setValue( "/qgis/showTips", cbxShowTips->isChecked() );
settings.setValue( "/qgis/dockAttributeTable", cbxAttributeTableDocked->isChecked() );
@@ -1019,6 +1027,7 @@ void QgsOptions::saveOptions()
settings.setValue( "/qgis/dockSnapping", cbxSnappingOptionsDocked->isChecked() );
settings.setValue( "/qgis/addPostgisDC", cbxAddPostgisDC->isChecked() );
settings.setValue( "/qgis/addNewLayersToCurrentGroup", cbxAddNewLayersToCurrentGroup->isChecked() );
bool createRasterLegendIcons = settings.value( "/qgis/createRasterLegendIcons", true ).toBool();
settings.setValue( "/qgis/createRasterLegendIcons", cbxCreateRasterLegendIcons->isChecked() );
settings.setValue( "/qgis/copyGeometryAsWKT", cbxCopyWKTGeomFromTable->isChecked() );
settings.setValue( "/qgis/new_layers_visible", chkAddedVisibility->isChecked() );
@@ -1027,6 +1036,7 @@ void QgsOptions::saveOptions()
settings.setValue( "/qgis/use_qimage_to_render", !( chkUseQPixmap->isChecked() ) );
settings.setValue( "/qgis/use_symbology_ng", chkUseSymbologyNG->isChecked() );
settings.setValue( "/qgis/legendDoubleClickAction", cmbLegendDoubleClickAction->currentIndex() );
bool legendLayersCapitalise = settings.value( "/qgis/capitaliseLayerName", false ).toBool();
settings.setValue( "/qgis/capitaliseLayerName", capitaliseCheckBox->isChecked() );

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

// refresh legend if any legend item's state is to be changed
if ( legendLayersBold != mLegendLayersBoldChkBx->isChecked()
|| legendGroupsBold != mLegendGroupsBoldChkBx->isChecked()
|| legendLayersCapitalise != capitaliseCheckBox->isChecked() )
{
QgisApp::instance()->legend()->updateLegendItemStyles();
}

// refresh symbology for any legend items, only if needed
if ( showLegendClassifiers != cbxLegendClassifiers->isChecked()
|| createRasterLegendIcons != cbxCreateRasterLegendIcons->isChecked() )
{
QgisApp::instance()->legend()->updateLegendItemSymbologies();
}

// save app stylesheet last (in case reset becomes necessary)
if ( mStyleSheetNewOpts != mStyleSheetOldOpts )
{
@@ -591,6 +591,7 @@ void QgsRasterLayerProperties::sync()

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

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

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

void QgsRasterLayerProperties::on_mLayerOrigNameLineEd_textEdited( const QString& text )
{
leDisplayName->setText( mRasterLayer->capitaliseLayerName( text ) );
}

void QgsRasterLayerProperties::on_buttonBuildPyramids_clicked()
{
QgsRasterDataProvider* provider = mRasterLayer->dataProvider();
@@ -56,6 +56,9 @@ class QgsRasterLayerProperties : public QDialog, private Ui::QgsRasterLayerPrope
//TODO: Verify that these all need to be public
/** \brief Applies the settings made in the dialog without closing the box */
void apply();
/** \brief Slot to update layer display name as original is edited
* @note added in QGIS 1.9 */
void on_mLayerOrigNameLineEd_textEdited( const QString& text );
/** \brief this slot asks the rasterlayer to construct pyramids */
void on_buttonBuildPyramids_clicked();
/** \brief slot executed when user presses "Add Values From Display" button on the transparency page */
@@ -337,6 +337,7 @@ void QgsVectorLayerProperties::setDisplayField( QString name )
void QgsVectorLayerProperties::reset( void )
{
// populate the general information
mLayerOrigNameLineEdit->setText( layer->originalName() );
txtDisplayName->setText( layer->name() );
pbnQueryBuilder->setWhatsThis( tr( "This button opens the query "
"builder and allows you to create a subset of features to display on "
@@ -468,7 +469,7 @@ void QgsVectorLayerProperties::apply()
if ( labelDialog )
labelDialog->apply();
layer->enableLabels( labelCheckBox->isChecked() );
layer->setLayerName( displayName() );
layer->setLayerName( mLayerOrigNameLineEdit->text() );

QSet<QString> excludeAttributesWMS, excludeAttributesWFS;

@@ -584,7 +585,10 @@ QString QgsVectorLayerProperties::metadata()
return layer->metadata();
}


void QgsVectorLayerProperties::on_mLayerOrigNameLineEdit_textEdited( const QString& text )
{
txtDisplayName->setText( layer->capitaliseLayerName( text ) );
}

void QgsVectorLayerProperties::on_pbnChangeSpatialRefSys_clicked()
{
@@ -89,6 +89,11 @@ class QgsVectorLayerProperties : public QDialog, private Ui::QgsVectorLayerPrope
/** Get metadata about the layer in nice formatted html */
QString metadata();

/** Slot to update layer display name as original is edited
* @note added in QGIS 1.9
*/
void on_mLayerOrigNameLineEdit_textEdited( const QString& text );

/** Set transparency based on slider position */
void sliderTransparency_valueChanged( int theValue );

Loading

0 comments on commit 94491b8

Please sign in to comment.