Skip to content
Permalink
Browse files

[composer] Double click legend item to edit text (fix #13578)

  • Loading branch information
nyalldawson committed Jun 16, 2016
1 parent aa0e6d8 commit dbf8d89459fb37921c1ce0ddb36ef5c1dd4a608d
Showing with 68 additions and 56 deletions.
  1. +66 −56 src/app/composer/qgscomposerlegendwidget.cpp
  2. +2 −0 src/app/composer/qgscomposerlegendwidget.h
@@ -710,62 +710,7 @@ void QgsComposerLegendWidget::on_mEditPushButton_clicked()
}

QModelIndex idx = mItemTreeView->selectionModel()->currentIndex();
QgsLayerTreeModel* model = mItemTreeView->layerTreeModel();
QgsLayerTreeNode* currentNode = model->index2node( idx );
QgsLayerTreeModelLegendNode* legendNode = model->index2legendNode( idx );
QString currentText;

if ( QgsLayerTree::isGroup( currentNode ) )
{
currentText = QgsLayerTree::toGroup( currentNode )->name();
}
else if ( QgsLayerTree::isLayer( currentNode ) )
{
currentText = QgsLayerTree::toLayer( currentNode )->layerName();
QVariant v = currentNode->customProperty( "legend/title-label" );
if ( !v.isNull() )
currentText = v.toString();
}
else if ( legendNode )
{
currentText = legendNode->data( Qt::EditRole ).toString();
}

bool ok;
QString newText = QInputDialog::getText( this, tr( "Legend item properties" ), tr( "Item text" ),
QLineEdit::Normal, currentText, &ok );
if ( !ok || newText == currentText )
return;

mLegend->beginCommand( tr( "Legend item edited" ) );

if ( QgsLayerTree::isGroup( currentNode ) )
{
QgsLayerTree::toGroup( currentNode )->setName( newText );
}
else if ( QgsLayerTree::isLayer( currentNode ) )
{
currentNode->setCustomProperty( "legend/title-label", newText );

// force update of label of the legend node with embedded icon (a bit clumsy i know)
QList<QgsLayerTreeModelLegendNode*> nodes = model->layerLegendNodes( QgsLayerTree::toLayer( currentNode ) );
if ( nodes.count() == 1 && nodes[0]->isEmbeddedInParent() )
nodes[0]->setUserLabel( QString() );
}
else if ( legendNode )
{
QList<int> order = QgsMapLayerLegendUtils::legendNodeOrder( legendNode->layerNode() );
//find unfiltered row number
QList<QgsLayerTreeModelLegendNode*> layerLegendNodes = model->layerOriginalLegendNodes( legendNode->layerNode() );
int unfilteredRowIndex = layerLegendNodes.indexOf( legendNode );
int originalIndex = ( unfilteredRowIndex >= 0 && unfilteredRowIndex < order.count() ? order[unfilteredRowIndex] : -1 );
QgsMapLayerLegendUtils::setLegendNodeUserLabel( legendNode->layerNode(), originalIndex, newText );
model->refreshLayerLegend( legendNode->layerNode() );
}

mLegend->adjustBoxSize();
mLegend->updateItem();
mLegend->endCommand();
on_mItemTreeView_doubleClicked( idx );
}

void QgsComposerLegendWidget::resetLayerNodeToDefaults()
@@ -995,6 +940,71 @@ void QgsComposerLegendWidget::updateFilterLegendByAtlasButton()
mFilterLegendByAtlasCheckBox->setEnabled( atlas.enabled() && atlas.coverageLayer() && atlas.coverageLayer()->geometryType() == QGis::Polygon );
}

void QgsComposerLegendWidget::on_mItemTreeView_doubleClicked( const QModelIndex &idx )
{
if ( !mLegend || !idx.isValid() )
{
return;
}

QgsLayerTreeModel* model = mItemTreeView->layerTreeModel();
QgsLayerTreeNode* currentNode = model->index2node( idx );
QgsLayerTreeModelLegendNode* legendNode = model->index2legendNode( idx );
QString currentText;

if ( QgsLayerTree::isGroup( currentNode ) )
{
currentText = QgsLayerTree::toGroup( currentNode )->name();
}
else if ( QgsLayerTree::isLayer( currentNode ) )
{
currentText = QgsLayerTree::toLayer( currentNode )->layerName();
QVariant v = currentNode->customProperty( "legend/title-label" );
if ( !v.isNull() )
currentText = v.toString();
}
else if ( legendNode )
{
currentText = legendNode->data( Qt::EditRole ).toString();
}

bool ok;
QString newText = QInputDialog::getText( this, tr( "Legend item properties" ), tr( "Item text" ),
QLineEdit::Normal, currentText, &ok );
if ( !ok || newText == currentText )
return;

mLegend->beginCommand( tr( "Legend item edited" ) );

if ( QgsLayerTree::isGroup( currentNode ) )
{
QgsLayerTree::toGroup( currentNode )->setName( newText );
}
else if ( QgsLayerTree::isLayer( currentNode ) )
{
currentNode->setCustomProperty( "legend/title-label", newText );

// force update of label of the legend node with embedded icon (a bit clumsy i know)
QList<QgsLayerTreeModelLegendNode*> nodes = model->layerLegendNodes( QgsLayerTree::toLayer( currentNode ) );
if ( nodes.count() == 1 && nodes[0]->isEmbeddedInParent() )
nodes[0]->setUserLabel( QString() );
}
else if ( legendNode )
{
QList<int> order = QgsMapLayerLegendUtils::legendNodeOrder( legendNode->layerNode() );
//find unfiltered row number
QList<QgsLayerTreeModelLegendNode*> layerLegendNodes = model->layerOriginalLegendNodes( legendNode->layerNode() );
int unfilteredRowIndex = layerLegendNodes.indexOf( legendNode );
int originalIndex = ( unfilteredRowIndex >= 0 && unfilteredRowIndex < order.count() ? order[unfilteredRowIndex] : -1 );
QgsMapLayerLegendUtils::setLegendNodeUserLabel( legendNode->layerNode(), originalIndex, newText );
model->refreshLayerLegend( legendNode->layerNode() );
}

mLegend->adjustBoxSize();
mLegend->updateItem();
mLegend->endCommand();
}


//
// QgsComposerLegendMenuProvider
@@ -99,6 +99,8 @@ class QgsComposerLegendWidget: public QgsComposerItemBaseWidget, private Ui::Qgs
/** Update the enabling state of the filter by atlas button */
void updateFilterLegendByAtlasButton();

void on_mItemTreeView_doubleClicked( const QModelIndex &index );

private:
QgsComposerLegendWidget();
void blockAllSignals( bool b );

0 comments on commit dbf8d89

Please sign in to comment.
You can’t perform that action at this time.