2012-05-18 19:35:43 +02:00
|
|
|
/***************************************************************************
|
|
|
|
qgsundowidget.cpp
|
|
|
|
---------------------
|
|
|
|
begin : June 2009
|
|
|
|
copyright : (C) 2009 by Martin Dobias
|
2012-10-08 00:29:13 +02:00
|
|
|
email : wonder dot sk at gmail dot com
|
2012-05-18 19:35:43 +02:00
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************/
|
2009-06-14 14:21:44 +00:00
|
|
|
#include "qgsundowidget.h"
|
|
|
|
|
2014-05-16 17:28:10 +07:00
|
|
|
#include "qgsapplication.h"
|
2016-07-06 22:27:17 +02:00
|
|
|
#include "qgslogger.h"
|
2015-06-10 13:59:08 +02:00
|
|
|
#include "qgsmaplayer.h"
|
2015-06-10 14:54:12 +02:00
|
|
|
#include "qgsmapcanvas.h"
|
2009-06-14 14:21:44 +00:00
|
|
|
|
|
|
|
|
2017-03-03 08:42:00 +01:00
|
|
|
QgsUndoWidget::QgsUndoWidget( QWidget *parent, QgsMapCanvas *mapCanvas )
|
|
|
|
: QgsPanelWidget( parent )
|
2009-06-14 14:21:44 +00:00
|
|
|
{
|
2009-06-26 15:29:46 +00:00
|
|
|
setupUi( this );
|
2009-06-14 14:21:44 +00:00
|
|
|
|
2017-03-30 11:11:28 +10:00
|
|
|
connect( undoButton, &QAbstractButton::clicked, this, &QgsUndoWidget::undo );
|
|
|
|
connect( redoButton, &QAbstractButton::clicked, this, &QgsUndoWidget::redo );
|
2010-05-16 14:44:42 +00:00
|
|
|
|
2009-06-14 14:21:44 +00:00
|
|
|
undoButton->setDisabled( true );
|
|
|
|
redoButton->setDisabled( true );
|
|
|
|
mMapCanvas = mapCanvas;
|
2016-05-10 10:32:30 +10:00
|
|
|
mUndoView = new QUndoView( this );
|
2013-01-25 18:21:46 -07:00
|
|
|
gridLayout->addWidget( mUndoView, 0, 0, 1, 2 );
|
2015-12-14 11:21:07 +11:00
|
|
|
mUndoStack = nullptr;
|
2012-12-14 01:30:13 -07:00
|
|
|
mPreviousIndex = 0;
|
|
|
|
mPreviousCount = 0;
|
2009-06-14 14:21:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QgsUndoWidget::destroyStack()
|
|
|
|
{
|
2015-12-16 12:15:00 +01:00
|
|
|
if ( mUndoStack )
|
2009-06-26 15:29:46 +00:00
|
|
|
{
|
2013-02-10 22:38:41 -07:00
|
|
|
// do not clear undo stack here, just null pointer
|
2015-12-14 11:21:07 +11:00
|
|
|
mUndoStack = nullptr;
|
2009-06-26 15:29:46 +00:00
|
|
|
}
|
2015-12-16 12:15:00 +01:00
|
|
|
if ( mUndoView )
|
2009-06-26 15:29:46 +00:00
|
|
|
{
|
|
|
|
mUndoView->close();
|
2013-02-10 22:38:41 -07:00
|
|
|
delete mUndoView;
|
|
|
|
mUndoView = new QUndoView( dockWidgetContents );
|
|
|
|
gridLayout->addWidget( mUndoView, 0, 0, 1, 2 );
|
2009-06-26 15:29:46 +00:00
|
|
|
}
|
2009-06-14 14:21:44 +00:00
|
|
|
}
|
|
|
|
|
2009-06-26 15:29:46 +00:00
|
|
|
void QgsUndoWidget::undoChanged( bool value )
|
2009-06-14 14:21:44 +00:00
|
|
|
{
|
2009-06-26 15:29:46 +00:00
|
|
|
undoButton->setDisabled( !value );
|
|
|
|
emit undoStackChanged();
|
2009-06-14 14:21:44 +00:00
|
|
|
}
|
|
|
|
|
2009-06-26 15:29:46 +00:00
|
|
|
void QgsUndoWidget::redoChanged( bool value )
|
2009-06-14 14:21:44 +00:00
|
|
|
{
|
2009-06-26 15:29:46 +00:00
|
|
|
redoButton->setDisabled( !value );
|
|
|
|
emit undoStackChanged();
|
2009-06-14 14:21:44 +00:00
|
|
|
}
|
|
|
|
|
2012-12-14 01:30:13 -07:00
|
|
|
void QgsUndoWidget::indexChanged( int curIndx )
|
2009-06-14 14:21:44 +00:00
|
|
|
{
|
2012-12-14 01:30:13 -07:00
|
|
|
// this is called twice when a non-current command is clicked in QUndoView
|
|
|
|
// first call has offset, second call will have offset of 0
|
|
|
|
int curCount = 0;
|
2012-12-18 02:12:20 -07:00
|
|
|
bool canRedo = true;
|
2012-12-14 01:30:13 -07:00
|
|
|
if ( mUndoStack )
|
|
|
|
{
|
2012-12-18 02:12:20 -07:00
|
|
|
canRedo = mUndoStack->canRedo();
|
2012-12-14 01:30:13 -07:00
|
|
|
curCount = mUndoStack->count();
|
|
|
|
}
|
2017-08-25 02:12:54 +10:00
|
|
|
int offset = std::abs( mPreviousIndex - curIndx );
|
2012-12-18 02:12:20 -07:00
|
|
|
|
|
|
|
// when individually redoing, differentiate between last redo and a new command added to stack
|
|
|
|
bool lastRedo = ( mPreviousIndex == ( mPreviousCount - 1 ) && mPreviousCount == curCount && !canRedo );
|
|
|
|
|
2013-02-10 22:38:41 -07:00
|
|
|
if ( offset != 0 )
|
2012-12-18 02:12:20 -07:00
|
|
|
{
|
2016-09-09 13:47:59 +02:00
|
|
|
QgsDebugMsgLevel( QString( "curIndx : %1" ).arg( curIndx ), 4 );
|
|
|
|
QgsDebugMsgLevel( QString( "offset : %1" ).arg( offset ), 4 );
|
|
|
|
QgsDebugMsgLevel( QString( "curCount: %1" ).arg( curCount ), 4 );
|
2012-12-18 02:12:20 -07:00
|
|
|
if ( lastRedo )
|
2013-03-30 11:37:30 +01:00
|
|
|
{
|
2016-09-09 13:47:59 +02:00
|
|
|
QgsDebugMsgLevel( QString( "lastRedo: true" ), 4 );
|
2013-03-30 11:37:30 +01:00
|
|
|
}
|
2012-12-18 02:12:20 -07:00
|
|
|
}
|
|
|
|
|
2013-02-10 22:38:41 -07:00
|
|
|
// avoid canvas redraws when only new command was added to stack (i.e. no user undo/redo action)
|
|
|
|
// or when user has clicked back in QUndoView history then added a new command to the stack
|
2012-12-18 02:12:20 -07:00
|
|
|
if ( offset > 1 || ( offset == 1 && ( canRedo || lastRedo ) ) )
|
2012-12-14 01:30:13 -07:00
|
|
|
{
|
2012-12-18 02:12:20 -07:00
|
|
|
if ( mMapCanvas )
|
2013-02-10 22:38:41 -07:00
|
|
|
{
|
2012-12-18 02:12:20 -07:00
|
|
|
mMapCanvas->refresh();
|
2013-02-10 22:38:41 -07:00
|
|
|
}
|
2012-12-14 01:30:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mPreviousIndex = curIndx;
|
|
|
|
mPreviousCount = curCount;
|
2009-06-14 14:21:44 +00:00
|
|
|
}
|
|
|
|
|
2014-10-20 22:50:15 +02:00
|
|
|
void QgsUndoWidget::undo()
|
2009-06-14 14:21:44 +00:00
|
|
|
{
|
2009-06-26 15:29:46 +00:00
|
|
|
if ( mUndoStack )
|
2009-06-14 14:21:44 +00:00
|
|
|
mUndoStack->undo();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QgsUndoWidget::redo()
|
|
|
|
{
|
2009-06-26 15:29:46 +00:00
|
|
|
if ( mUndoStack )
|
2009-06-14 14:21:44 +00:00
|
|
|
mUndoStack->redo();
|
|
|
|
}
|
|
|
|
|
2017-03-03 08:42:00 +01:00
|
|
|
void QgsUndoWidget::setUndoStack( QUndoStack *undoStack )
|
2009-06-14 14:21:44 +00:00
|
|
|
{
|
2015-12-16 12:15:00 +01:00
|
|
|
if ( mUndoView )
|
2009-06-14 14:21:44 +00:00
|
|
|
{
|
2009-06-26 15:29:46 +00:00
|
|
|
mUndoView->close();
|
|
|
|
delete mUndoView;
|
2015-12-14 11:21:07 +11:00
|
|
|
mUndoView = nullptr;
|
2009-06-14 14:21:44 +00:00
|
|
|
}
|
2009-06-26 15:29:46 +00:00
|
|
|
|
2009-06-14 14:21:44 +00:00
|
|
|
mUndoStack = undoStack;
|
2012-12-14 01:30:13 -07:00
|
|
|
mPreviousIndex = mUndoStack->index();
|
|
|
|
mPreviousCount = mUndoStack->count();
|
2009-06-26 15:29:46 +00:00
|
|
|
|
|
|
|
mUndoView = new QUndoView( dockWidgetContents );
|
|
|
|
mUndoView->setStack( undoStack );
|
2016-10-23 08:30:26 +10:00
|
|
|
mUndoView->setObjectName( QStringLiteral( "undoView" ) );
|
2009-06-26 15:29:46 +00:00
|
|
|
gridLayout->addWidget( mUndoView, 0, 0, 1, 2 );
|
2016-05-10 10:32:30 +10:00
|
|
|
// setWidget( dockWidgetContents );
|
2017-03-30 11:11:28 +10:00
|
|
|
connect( mUndoStack, &QUndoStack::canUndoChanged, this, &QgsUndoWidget::undoChanged );
|
|
|
|
connect( mUndoStack, &QUndoStack::canRedoChanged, this, &QgsUndoWidget::redoChanged );
|
2012-12-14 01:30:13 -07:00
|
|
|
|
|
|
|
// gets triggered also when a new command is added to stack, and twice when clicking a command in QUndoView
|
2017-03-30 11:11:28 +10:00
|
|
|
connect( mUndoStack, &QUndoStack::indexChanged, this, &QgsUndoWidget::indexChanged );
|
2009-06-14 14:21:44 +00:00
|
|
|
|
2010-05-16 14:44:42 +00:00
|
|
|
undoButton->setDisabled( !mUndoStack->canUndo() );
|
|
|
|
redoButton->setDisabled( !mUndoStack->canRedo() );
|
2009-06-14 14:21:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-05-10 10:32:30 +10:00
|
|
|
void QgsUndoWidget::setupUi( QWidget *UndoWidget )
|
2009-06-14 14:21:44 +00:00
|
|
|
{
|
2009-06-26 15:29:46 +00:00
|
|
|
if ( UndoWidget->objectName().isEmpty() )
|
2016-10-23 08:30:26 +10:00
|
|
|
UndoWidget->setObjectName( QStringLiteral( "UndoWidget" ) );
|
2009-10-04 20:50:48 +00:00
|
|
|
UndoWidget->resize( 200, 223 );
|
|
|
|
UndoWidget->setMinimumSize( QSize( 200, 220 ) );
|
2009-06-26 15:29:46 +00:00
|
|
|
dockWidgetContents = new QWidget( UndoWidget );
|
2016-10-23 08:30:26 +10:00
|
|
|
dockWidgetContents->setObjectName( QStringLiteral( "dockWidgetContents" ) );
|
2009-06-26 15:29:46 +00:00
|
|
|
gridLayout = new QGridLayout( dockWidgetContents );
|
2016-10-23 08:30:26 +10:00
|
|
|
gridLayout->setObjectName( QStringLiteral( "gridLayout" ) );
|
2009-06-26 15:29:46 +00:00
|
|
|
gridLayout->setContentsMargins( 0, 0, 0, 0 );
|
|
|
|
spacerItem = new QSpacerItem( 20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding );
|
|
|
|
|
|
|
|
gridLayout->addItem( spacerItem, 0, 0, 1, 1 );
|
|
|
|
|
|
|
|
undoButton = new QPushButton( dockWidgetContents );
|
2016-10-23 08:30:26 +10:00
|
|
|
undoButton->setObjectName( QStringLiteral( "undoButton" ) );
|
|
|
|
undoButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionUndo.svg" ) ) );
|
2009-06-26 15:29:46 +00:00
|
|
|
|
|
|
|
gridLayout->addWidget( undoButton, 1, 0, 1, 1 );
|
|
|
|
|
|
|
|
redoButton = new QPushButton( dockWidgetContents );
|
2016-10-23 08:30:26 +10:00
|
|
|
redoButton->setObjectName( QStringLiteral( "redoButton" ) );
|
|
|
|
redoButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionRedo.svg" ) ) );
|
2009-06-26 15:29:46 +00:00
|
|
|
|
|
|
|
gridLayout->addWidget( redoButton, 1, 1, 1, 1 );
|
|
|
|
|
|
|
|
spacerItem1 = new QSpacerItem( 20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding );
|
|
|
|
|
|
|
|
gridLayout->addItem( spacerItem1, 0, 1, 1, 1 );
|
|
|
|
|
2016-05-10 10:32:30 +10:00
|
|
|
UndoWidget->setLayout( gridLayout );
|
2009-06-26 15:29:46 +00:00
|
|
|
|
|
|
|
retranslateUi( UndoWidget );
|
|
|
|
|
|
|
|
QMetaObject::connectSlotsByName( UndoWidget );
|
2009-06-14 14:21:44 +00:00
|
|
|
} // setupUi
|
|
|
|
|
2016-05-10 10:32:30 +10:00
|
|
|
void QgsUndoWidget::retranslateUi( QWidget *UndoWidget )
|
2009-06-14 14:21:44 +00:00
|
|
|
{
|
2015-12-14 11:21:07 +11:00
|
|
|
UndoWidget->setWindowTitle( QApplication::translate( "UndoWidget", "Undo/Redo Panel", nullptr, QApplication::UnicodeUTF8 ) );
|
|
|
|
undoButton->setText( QApplication::translate( "UndoWidget", "Undo", nullptr, QApplication::UnicodeUTF8 ) );
|
|
|
|
redoButton->setText( QApplication::translate( "UndoWidget", "Redo", nullptr, QApplication::UnicodeUTF8 ) );
|
2009-06-26 15:29:46 +00:00
|
|
|
Q_UNUSED( UndoWidget );
|
2009-06-14 14:21:44 +00:00
|
|
|
}
|
|
|
|
|