Skip to content

Commit 75c686b

Browse files
author
mhugent
committedDec 3, 2010
Added undo/redo buttons in composer, fix undo problems when deleting grouped items
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@14831 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

8 files changed

+120
-24
lines changed

8 files changed

+120
-24
lines changed
 

‎src/app/composer/qgscomposer.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title ): QMainWindow(),
151151
viewMenu->addAction( mActionZoomIn );
152152
viewMenu->addAction( mActionZoomOut );
153153
viewMenu->addAction( mActionZoomAll );
154+
viewMenu->addAction( mActionUndo );
155+
viewMenu->addAction( mActionRedo );
154156
viewMenu->addSeparator();
155157
viewMenu->addAction( mActionRefreshView );
156158

@@ -198,7 +200,17 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title ): QMainWindow(),
198200
mView = new QgsComposerView( mViewFrame );
199201
connectSlots();
200202

203+
//init undo/redo buttons
201204
mComposition = new QgsComposition( mQgis->mapCanvas()->mapRenderer() );
205+
mActionUndo->setEnabled( false );
206+
mActionRedo->setEnabled( false );
207+
if ( mComposition->undoStack() )
208+
{
209+
connect( mComposition->undoStack(), SIGNAL( canUndoChanged( bool ) ), mActionUndo, SLOT( setEnabled( bool ) ) );
210+
connect( mComposition->undoStack(), SIGNAL( canRedoChanged( bool ) ), mActionRedo, SLOT( setEnabled( bool ) ) );
211+
}
212+
213+
202214
mComposition->setParent( mView );
203215
mView->setComposition( mComposition );
204216

@@ -257,6 +269,8 @@ void QgsComposer::setupTheme()
257269
mActionZoomIn->setIcon( QgisApp::getThemeIcon( "/mActionZoomIn.png" ) );
258270
mActionZoomOut->setIcon( QgisApp::getThemeIcon( "/mActionZoomOut.png" ) );
259271
mActionRefreshView->setIcon( QgisApp::getThemeIcon( "/mActionDraw.png" ) );
272+
mActionUndo->setIcon( QgisApp::getThemeIcon( "/mActionUndo.png" ) );
273+
mActionRedo->setIcon( QgisApp::getThemeIcon( "/mActionRedo.png" ) );
260274
mActionAddImage->setIcon( QgisApp::getThemeIcon( "/mActionAddImage.png" ) );
261275
mActionAddNewMap->setIcon( QgisApp::getThemeIcon( "/mActionAddMap.png" ) );
262276
mActionAddNewLabel->setIcon( QgisApp::getThemeIcon( "/mActionLabel.png" ) );
@@ -1059,6 +1073,22 @@ void QgsComposer::on_mActionAlignBottom_triggered()
10591073
}
10601074
}
10611075

1076+
void QgsComposer::on_mActionUndo_triggered()
1077+
{
1078+
if ( mComposition && mComposition->undoStack() )
1079+
{
1080+
mComposition->undoStack()->undo();
1081+
}
1082+
}
1083+
1084+
void QgsComposer::on_mActionRedo_triggered()
1085+
{
1086+
if ( mComposition && mComposition->undoStack() )
1087+
{
1088+
mComposition->undoStack()->redo();
1089+
}
1090+
}
1091+
10621092
void QgsComposer::moveEvent( QMoveEvent *e ) { saveWindowState(); }
10631093

10641094
void QgsComposer::resizeEvent( QResizeEvent *e )
@@ -1332,9 +1362,19 @@ void QgsComposer::readXML( const QDomElement& composerElem, const QDomDocument&
13321362

13331363
if ( mUndoView )
13341364
{
1335-
mUndoView->setStack( mComposition->undoStack() );
1365+
//init undo/redo buttons
1366+
mActionUndo->setEnabled( false );
1367+
mActionRedo->setEnabled( false );
1368+
if ( mComposition->undoStack() )
1369+
{
1370+
mUndoView->setStack( mComposition->undoStack() );
1371+
connect( mComposition->undoStack(), SIGNAL( canUndoChanged( bool ) ), mActionUndo, SLOT( setEnabled( bool ) ) );
1372+
connect( mComposition->undoStack(), SIGNAL( canRedoChanged( bool ) ), mActionRedo, SLOT( setEnabled( bool ) ) );
1373+
}
13361374
}
13371375

1376+
1377+
13381378
setSelectionTool();
13391379
}
13401380

‎src/app/composer/qgscomposer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
203203
//!Align selected composer items to bottom
204204
void on_mActionAlignBottom_triggered();
205205

206+
//!Undo last composer change
207+
void on_mActionUndo_triggered();
208+
209+
//!Redo last composer change
210+
void on_mActionRedo_triggered();
211+
206212
//! Save window state
207213
void saveWindowState();
208214

‎src/app/composer/qgscomposershapewidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ QgsComposerShapeWidget::QgsComposerShapeWidget( QgsComposerShape* composerShape
3939

4040
blockAllSignals( false );
4141

42-
connect( mShapeComboBox, SIGNAL( itemChanged() ), this, SLOT( setGuiElementValues() ) );
42+
connect( mShapeComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( setGuiElementValues() ) );
4343
}
4444

4545
QgsComposerShapeWidget::~QgsComposerShapeWidget()

‎src/app/composer/qgscomposertablewidget.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,16 @@ void QgsComposerTableWidget::on_mComposerMapComboBox_activated( int index )
146146
const QgsComposition* tableComposition = mComposerTable->composition();
147147
if ( tableComposition )
148148
{
149-
mComposerTable->beginCommand( tr( "Table map changed" ) );
149+
if ( sender() ) //only create command if called from GUI
150+
{
151+
mComposerTable->beginCommand( tr( "Table map changed" ) );
152+
}
150153
mComposerTable->setComposerMap( tableComposition->getComposerMapById( mapId ) );
151154
mComposerTable->update();
152-
mComposerTable->endCommand();
155+
if ( sender() )
156+
{
157+
mComposerTable->endCommand();
158+
}
153159
}
154160
}
155161

‎src/core/composer/qgscomposeritemgroup.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ QgsComposerItemGroup::~QgsComposerItemGroup()
3131
QSet<QgsComposerItem*>::iterator itemIt = mItems.begin();
3232
for ( ; itemIt != mItems.end(); ++itemIt )
3333
{
34-
emit childItemDeleted( *itemIt );
34+
if ( *itemIt )
35+
{
36+
mComposition->removeItem( *itemIt );
37+
( *itemIt )->setFlag( QGraphicsItem::ItemIsSelectable, true );
38+
}
3539
}
3640
}
3741

@@ -162,13 +166,3 @@ void QgsComposerItemGroup::drawFrame( QPainter* p )
162166
p->drawRect( QRectF( 0, 0, rect().width(), rect().height() ) );
163167
}
164168
}
165-
166-
bool QgsComposerItemGroup::writeXML( QDomElement& elem, QDomDocument & doc ) const
167-
{
168-
return true; //soon...
169-
}
170-
171-
bool QgsComposerItemGroup::readXML( const QDomElement& itemElem, const QDomDocument& doc )
172-
{
173-
return false; //soon...
174-
}

‎src/core/composer/qgscomposeritemgroup.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ class CORE_EXPORT QgsComposerItemGroup: public QgsComposerItem
4848
* @param elem is Dom element corresponding to 'Composer' tag
4949
* @param doc is the Dom document
5050
*/
51-
bool writeXML( QDomElement& elem, QDomDocument & doc ) const;
51+
bool writeXML( QDomElement& elem, QDomDocument & doc ) const {}
5252

5353
/** sets state from Dom document
5454
* @param itemElem is Dom node corresponding to item tag
5555
* @param doc is the Dom document
5656
*/
57-
bool readXML( const QDomElement& itemElem, const QDomDocument& doc );
57+
bool readXML( const QDomElement& itemElem, const QDomDocument& doc ) {}
58+
59+
QSet<QgsComposerItem*> items() { return mItems; }
5860

5961
signals:
6062
void childItemDeleted( QgsComposerItem* item );

‎src/gui/qgscomposerview.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,30 @@ void QgsComposerView::keyPressEvent( QKeyEvent * e )
394394
if ( !map || !map->isDrawing() ) //don't delete a composer map while it draws
395395
{
396396
composition()->removeItem( *itemIt );
397-
emit itemRemoved( *itemIt );
398-
399-
pushAddRemoveCommand( *itemIt, tr( "Item deleted" ), QgsAddRemoveItemCommand::Removed );
397+
QgsComposerItemGroup* itemGroup = dynamic_cast<QgsComposerItemGroup*>( *itemIt );
398+
if ( itemGroup && composition() )
399+
{
400+
//add add/remove item command for every item in the group
401+
QUndoCommand* parentCommand = new QUndoCommand( tr( "Remove item group" ) );
402+
403+
QSet<QgsComposerItem*> groupedItems = itemGroup->items();
404+
QSet<QgsComposerItem*>::iterator it = groupedItems.begin();
405+
for ( ; it != groupedItems.end(); ++it )
406+
{
407+
QgsAddRemoveItemCommand* subcommand = new QgsAddRemoveItemCommand( QgsAddRemoveItemCommand::Removed, *it, composition(), "", parentCommand );
408+
connectAddRemoveCommandSignals( subcommand );
409+
emit itemRemoved( *it );
410+
}
411+
412+
composition()->undoStack()->push( parentCommand );
413+
delete itemGroup;
414+
emit itemRemoved( itemGroup );
415+
}
416+
else
417+
{
418+
emit itemRemoved( *itemIt );
419+
pushAddRemoveCommand( *itemIt, tr( "Item deleted" ), QgsAddRemoveItemCommand::Removed );
420+
}
400421
}
401422
}
402423
}
@@ -591,8 +612,6 @@ void QgsComposerView::groupItems()
591612
return; //not enough items for a group
592613
}
593614
QgsComposerItemGroup* itemGroup = new QgsComposerItemGroup( composition() );
594-
//connect signal/slot to let item group tell if child items get removed
595-
connect( itemGroup, SIGNAL( childItemDeleted( QgsComposerItem* ) ), this, SIGNAL( itemRemoved( QgsComposerItem* ) ) );
596615

597616
QList<QgsComposerItem*>::iterator itemIter = selectionList.begin();
598617
for ( ; itemIter != selectionList.end(); ++itemIter )

‎src/ui/qgscomposerbase.ui

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>1145</width>
9+
<width>1333</width>
1010
<height>609</height>
1111
</rect>
1212
</property>
@@ -163,6 +163,8 @@
163163
<addaction name="mActionZoomIn"/>
164164
<addaction name="mActionZoomOut"/>
165165
<addaction name="mActionRefreshView"/>
166+
<addaction name="mActionUndo"/>
167+
<addaction name="mActionRedo"/>
166168
<addaction name="separator"/>
167169
<addaction name="mActionAddNewMap"/>
168170
<addaction name="mActionAddImage"/>
@@ -479,11 +481,38 @@
479481
<string>Page Setup</string>
480482
</property>
481483
</action>
484+
<action name="mActionUndo">
485+
<property name="icon">
486+
<iconset>
487+
<normalon>:/images/themes/default/mActionUndo.png</normalon>
488+
</iconset>
489+
</property>
490+
<property name="text">
491+
<string>Undo</string>
492+
</property>
493+
<property name="toolTip">
494+
<string>Revert last change</string>
495+
</property>
496+
</action>
497+
<action name="mActionRedo">
498+
<property name="icon">
499+
<iconset resource="../../images/images.qrc">
500+
<normaloff>:/images/themes/default/mActionRedo.png</normaloff>:/images/themes/default/mActionRedo.png</iconset>
501+
</property>
502+
<property name="text">
503+
<string>Redo</string>
504+
</property>
505+
<property name="toolTip">
506+
<string>Restore last change</string>
507+
</property>
508+
</action>
482509
</widget>
483510
<tabstops>
484511
<tabstop>mCompositionNameComboBox</tabstop>
485512
<tabstop>mOptionsTabWidget</tabstop>
486513
</tabstops>
487-
<resources/>
514+
<resources>
515+
<include location="../../images/images.qrc"/>
516+
</resources>
488517
<connections/>
489518
</ui>

0 commit comments

Comments
 (0)
Please sign in to comment.