Skip to content

Commit

Permalink
[composer] Show selected items in bold in items tree panel (fix #11057)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 25, 2014
1 parent 2055950 commit d70235f
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 17 deletions.
6 changes: 6 additions & 0 deletions python/core/composer/qgscomposition.sip
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,12 @@ class QgsComposition : QGraphicsScene
* @param item item to set as selected
* @note added in version 2.3*/
void setSelectedItem( QgsComposerItem* item );

/**Clears any selected items in the composition. Call this method rather than
* QGraphicsScene::clearSelection, as the latter does not correctly emit signals to allow
* the composition's model to update.
* @note added in version 2.5*/
void setAllUnselected();

/**Refreshes a data defined property for the composition by reevaluating the property's value
* and redrawing the composition with this new value.
Expand Down
5 changes: 5 additions & 0 deletions src/core/composer/qgscomposeritem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ void QgsComposerItem::setSelected( bool s )
{
QgsDebugMsg( "entered." );
QGraphicsRectItem::setSelected( s );
//inform model that id data has changed
if ( mComposition )
{
mComposition->itemsModel()->updateItemSelectStatus( this );
}
update(); //to draw selection boxes
}

Expand Down
33 changes: 33 additions & 0 deletions src/core/composer/qgscomposermodel.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,20 @@ QVariant QgsComposerModel::data( const QModelIndex &index, int role ) const
return QVariant();
}

case Qt::FontRole:
if ( index.column() == ItemId && item->isSelected() )
{
//draw name of selected items in bold
QFont boldFont;
boldFont.setBold( true );
return boldFont;
}
else
{
return QVariant();
}
break;

default:
return QVariant();
}
Expand Down Expand Up @@ -499,6 +513,25 @@ void QgsComposerModel::updateItemVisibility( QgsComposerItem *item )
emit dataChanged( itemIndex, itemIndex );
}

void QgsComposerModel::updateItemSelectStatus( QgsComposerItem *item )
{
if ( !item )
{
//nothing to do
return;
}

//need to get QModelIndex of item
QModelIndex itemIndex = indexForItem( item, ItemId );
if ( !itemIndex.isValid() )
{
return;
}

//emit signal for item visibility change
emit dataChanged( itemIndex, itemIndex );
}

bool QgsComposerModel::reorderItemUp( QgsComposerItem *item )
{
if ( mItemsInScene.first() == item )
Expand Down
12 changes: 12 additions & 0 deletions src/core/composer/qgscomposermodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ class CORE_EXPORT QgsComposerModel: public QAbstractItemModel
* @param item item to update
* @see updateItemLockStatus
* @see updateItemVisibility
* @see updateItemSelectStatus
* @note added in QGIS 2.5
*/
void updateItemDisplayName( QgsComposerItem *item );
Expand All @@ -200,6 +201,7 @@ class CORE_EXPORT QgsComposerModel: public QAbstractItemModel
* @param item item to update
* @see updateItemDisplayName
* @see updateItemVisibility
* @see updateItemSelectStatus
* @note added in QGIS 2.5
*/
void updateItemLockStatus( QgsComposerItem *item );
Expand All @@ -208,10 +210,20 @@ class CORE_EXPORT QgsComposerModel: public QAbstractItemModel
* @param item item to update
* @see updateItemDisplayName
* @see updateItemLockStatus
* @see updateItemSelectStatus
* @note added in QGIS 2.5
*/
void updateItemVisibility( QgsComposerItem *item );

/**Must be called when an item's selection status changes
* @param item item to update
* @see updateItemDisplayName
* @see updateItemVisibility
* @see updateItemLockStatus
* @note added in QGIS 2.5
*/
void updateItemSelectStatus( QgsComposerItem *item );

public slots:

/**Sets an item as the current selection from a QModelIndex
Expand Down
29 changes: 24 additions & 5 deletions src/core/composer/qgscomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,30 @@ void QgsComposition::refreshItems()

void QgsComposition::setSelectedItem( QgsComposerItem *item )
{
clearSelection();
setAllUnselected();
item->setSelected( true );
emit selectedItemChanged( item );
}

void QgsComposition::setAllUnselected()
{
//we can't use QGraphicsScene::clearSelection, as that emits no signals
//and we don't know which items are being unselected
//accordingly, we can't inform the composition model of selection changes
//instead, do the clear selection manually...
QList<QGraphicsItem *> selectedItemList = selectedItems();
QList<QGraphicsItem *>::iterator itemIter = selectedItemList.begin();

for ( ; itemIter != selectedItemList.end(); ++itemIter )
{
QgsComposerItem* composerItem = dynamic_cast<QgsComposerItem *>( *itemIter );
if ( composerItem )
{
composerItem->setSelected( false );
}
}
}

void QgsComposition::refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property )
{
//updates data defined properties and redraws composition to match
Expand Down Expand Up @@ -982,7 +1001,7 @@ void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocumen
pasteShiftPos = *pos - minItemPos;

//since we are pasting items, clear the existing selection
clearSelection();
setAllUnselected();
}

if ( pasteInPlace )
Expand Down Expand Up @@ -1384,7 +1403,7 @@ void QgsComposition::selectNextByZOrder( ZValueDirection direction )
}

//ok, found a good target item
clearSelection();
setAllUnselected();
selectedItem->setSelected( true );
emit selectedItemChanged( selectedItem );
}
Expand Down Expand Up @@ -1658,7 +1677,7 @@ void QgsComposition::lockSelectedItems()
subcommand->saveAfterState();
}

clearSelection();
setAllUnselected();
mUndoStack->push( parentCommand );
QgsProject::instance()->dirty( true );
}
Expand All @@ -1670,7 +1689,7 @@ void QgsComposition::unlockAllItems()
QUndoCommand* parentCommand = new QUndoCommand( tr( "Items unlocked" ) );

//first, clear the selection
clearSelection();
setAllUnselected();

QList<QGraphicsItem *> itemList = items();
QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
Expand Down
6 changes: 6 additions & 0 deletions src/core/composer/qgscomposition.h
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,12 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene
* @note added in version 2.3*/
void setSelectedItem( QgsComposerItem* item );

/**Clears any selected items in the composition. Call this method rather than
* QGraphicsScene::clearSelection, as the latter does not correctly emit signals to allow
* the composition's model to update.
* @note added in version 2.5*/
void setAllUnselected();

/**Refreshes a data defined property for the composition by reevaluating the property's value
* and redrawing the composition with this new value.
* @param property data defined property to refresh. If property is set to
Expand Down
24 changes: 12 additions & 12 deletions src/gui/qgscomposerview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ void QgsComposerView::mousePressEvent( QMouseEvent* e )
if (( !selectedItem->selected() ) && //keep selection if an already selected item pressed
!( e->modifiers() & Qt::ShiftModifier ) ) //keep selection if shift key pressed
{
composition()->clearSelection();
composition()->setAllUnselected();
}

if (( e->modifiers() & Qt::ShiftModifier ) && ( selectedItem->selected() ) )
Expand Down Expand Up @@ -372,7 +372,7 @@ void QgsComposerView::mousePressEvent( QMouseEvent* e )
}
newScaleBar->applyDefaultSize(); //4 segments, 1/5 of composer map width

composition()->clearSelection();
composition()->setAllUnselected();
newScaleBar->setSelected( true );
emit selectedItemChanged( newScaleBar );

Expand Down Expand Up @@ -447,7 +447,7 @@ void QgsComposerView::addShape( Tool currentTool )
composition()->addComposerShape( composerShape );
removeRubberBand();

composition()->clearSelection();
composition()->setAllUnselected();
composerShape->setSelected( true );
emit selectedItemChanged( composerShape );

Expand Down Expand Up @@ -511,7 +511,7 @@ void QgsComposerView::endMarqueeSelect( QMouseEvent* e )
else
{
//not adding to or removing from selection, so clear current selection
composition()->clearSelection();
composition()->setAllUnselected();
}

if ( !mRubberBandItem || ( mRubberBandItem->rect().width() < 0.1 && mRubberBandItem->rect().height() < 0.1 ) )
Expand Down Expand Up @@ -743,7 +743,7 @@ void QgsComposerView::mouseReleaseEvent( QMouseEvent* e )
QgsComposerArrow* composerArrow = new QgsComposerArrow( mRubberBandLineItem->line().p1(), mRubberBandLineItem->line().p2(), composition() );
composition()->addComposerArrow( composerArrow );

composition()->clearSelection();
composition()->setAllUnselected();
composerArrow->setSelected( true );
emit selectedItemChanged( composerArrow );

Expand Down Expand Up @@ -772,7 +772,7 @@ void QgsComposerView::mouseReleaseEvent( QMouseEvent* e )
QgsComposerMap* composerMap = new QgsComposerMap( composition(), mRubberBandItem->transform().dx(), mRubberBandItem->transform().dy(), mRubberBandItem->rect().width(), mRubberBandItem->rect().height() );
composition()->addComposerMap( composerMap );

composition()->clearSelection();
composition()->setAllUnselected();
composerMap->setSelected( true );
emit selectedItemChanged( composerMap );

Expand All @@ -794,7 +794,7 @@ void QgsComposerView::mouseReleaseEvent( QMouseEvent* e )
newPicture->setSceneRect( QRectF( mRubberBandItem->transform().dx(), mRubberBandItem->transform().dy(), mRubberBandItem->rect().width(), mRubberBandItem->rect().height() ) );
composition()->addComposerPicture( newPicture );

composition()->clearSelection();
composition()->setAllUnselected();
newPicture->setSelected( true );
emit selectedItemChanged( newPicture );

Expand Down Expand Up @@ -823,7 +823,7 @@ void QgsComposerView::mouseReleaseEvent( QMouseEvent* e )

composition()->addComposerLabel( newLabelItem );

composition()->clearSelection();
composition()->setAllUnselected();
newLabelItem->setSelected( true );
emit selectedItemChanged( newLabelItem );

Expand All @@ -846,7 +846,7 @@ void QgsComposerView::mouseReleaseEvent( QMouseEvent* e )
composition()->addComposerLegend( newLegend );
newLegend->updateLegend();

composition()->clearSelection();
composition()->setAllUnselected();
newLegend->setSelected( true );
emit selectedItemChanged( newLegend );

Expand All @@ -873,7 +873,7 @@ void QgsComposerView::mouseReleaseEvent( QMouseEvent* e )
}
composition()->addComposerTable( newTable );

composition()->clearSelection();
composition()->setAllUnselected();
newTable->setSelected( true );
emit selectedItemChanged( newTable );

Expand Down Expand Up @@ -902,7 +902,7 @@ void QgsComposerView::mouseReleaseEvent( QMouseEvent* e )
composerHtml->addFrame( frame );
composition()->endMultiFrameCommand();

composition()->clearSelection();
composition()->setAllUnselected();
frame->setSelected( true );
emit selectedItemChanged( frame );

Expand Down Expand Up @@ -1263,7 +1263,7 @@ void QgsComposerView::selectNone()
return;
}

composition()->clearSelection();
composition()->setAllUnselected();
}

void QgsComposerView::selectInvert()
Expand Down

0 comments on commit d70235f

Please sign in to comment.