diff --git a/src/app/qgisapp.cpp b/src/app/qgisapp.cpp index c95308003773..fc47dae0486c 100644 --- a/src/app/qgisapp.cpp +++ b/src/app/qgisapp.cpp @@ -689,7 +689,7 @@ void QgisApp::onActiveLayerChanged( QgsMapLayer *layer ) } else { - mUndoWidget->destroyStack(); + mUndoWidget->unsetStack(); } updateUndoActions(); } diff --git a/src/app/qgsundowidget.cpp b/src/app/qgsundowidget.cpp index 7572e6bbac5b..7845c39ac474 100644 --- a/src/app/qgsundowidget.cpp +++ b/src/app/qgsundowidget.cpp @@ -23,52 +23,79 @@ QgsUndoWidget::QgsUndoWidget( QWidget *parent, QgsMapCanvas *mapCanvas ) : QgsPanelWidget( parent ) { - setupUi( this ); + setObjectName( QStringLiteral( "UndoWidget" ) ); + resize( 200, 223 ); + setMinimumSize( QSize( 200, 220 ) ); + mDockWidgetContents = new QWidget( this ); + mDockWidgetContents->setObjectName( QStringLiteral( "dockWidgetContents" ) ); + mGridLayout = new QGridLayout( mDockWidgetContents ); + mGridLayout->setObjectName( QStringLiteral( "gridLayout" ) ); + mGridLayout->setContentsMargins( 0, 0, 0, 0 ); + QSpacerItem *spacerItem = new QSpacerItem( 20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding ); - connect( undoButton, &QAbstractButton::clicked, this, &QgsUndoWidget::undo ); - connect( redoButton, &QAbstractButton::clicked, this, &QgsUndoWidget::redo ); + mGridLayout->addItem( spacerItem, 0, 0, 1, 1 ); - undoButton->setDisabled( true ); - redoButton->setDisabled( true ); + mUndoButton = new QPushButton( mDockWidgetContents ); + mUndoButton->setObjectName( QStringLiteral( "undoButton" ) ); + mUndoButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionUndo.svg" ) ) ); + + mGridLayout->addWidget( mUndoButton, 1, 0, 1, 1 ); + + mRedoButton = new QPushButton( mDockWidgetContents ); + mRedoButton->setObjectName( QStringLiteral( "redoButton" ) ); + mRedoButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionRedo.svg" ) ) ); + + mGridLayout->addWidget( mRedoButton, 1, 1, 1, 1 ); + + QSpacerItem *spacerItem1 = new QSpacerItem( 20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding ); + + mGridLayout->addItem( spacerItem1, 0, 1, 1, 1 ); + + setLayout( mGridLayout ); + + setWindowTitle( QApplication::translate( "UndoWidget", "Undo/Redo" ) ); + mUndoButton->setText( QApplication::translate( "UndoWidget", "Undo" ) ); + mRedoButton->setText( QApplication::translate( "UndoWidget", "Redo" ) ); + + connect( mUndoButton, &QAbstractButton::clicked, this, &QgsUndoWidget::undo ); + connect( mRedoButton, &QAbstractButton::clicked, this, &QgsUndoWidget::redo ); + + mUndoButton->setDisabled( true ); + mRedoButton->setDisabled( true ); mMapCanvas = mapCanvas; mUndoView = new QUndoView( this ); - gridLayout->addWidget( mUndoView, 0, 0, 1, 2 ); - mUndoStack = nullptr; - mPreviousIndex = 0; - mPreviousCount = 0; + mGridLayout->addWidget( mUndoView, 0, 0, 1, 2 ); } void QgsUndoWidget::setButtonsVisible( bool show ) { - undoButton->setVisible( show ); - redoButton->setVisible( show ); + mUndoButton->setVisible( show ); + mRedoButton->setVisible( show ); } -void QgsUndoWidget::destroyStack() +void QgsUndoWidget::unsetStack() { - if ( mUndoStack ) - { - // do not clear undo stack here, just null pointer - mUndoStack = nullptr; - } + // we don't have ownership of the stack, don't delete it! + mUndoStack = nullptr; + if ( mUndoView ) { mUndoView->close(); delete mUndoView; - mUndoView = new QUndoView( dockWidgetContents ); - gridLayout->addWidget( mUndoView, 0, 0, 1, 2 ); + mUndoView = new QUndoView( mDockWidgetContents ); + mGridLayout->addWidget( mUndoView, 0, 0, 1, 2 ); } } void QgsUndoWidget::undoChanged( bool value ) { - undoButton->setDisabled( !value ); + mUndoButton->setDisabled( !value ); emit undoStackChanged(); } void QgsUndoWidget::redoChanged( bool value ) { - redoButton->setDisabled( !value ); + mRedoButton->setDisabled( !value ); emit undoStackChanged(); } @@ -138,10 +165,10 @@ void QgsUndoWidget::setUndoStack( QUndoStack *undoStack ) mPreviousIndex = mUndoStack->index(); mPreviousCount = mUndoStack->count(); - mUndoView = new QUndoView( dockWidgetContents ); + mUndoView = new QUndoView( mDockWidgetContents ); mUndoView->setStack( undoStack ); mUndoView->setObjectName( QStringLiteral( "undoView" ) ); - gridLayout->addWidget( mUndoView, 0, 0, 1, 2 ); + mGridLayout->addWidget( mUndoView, 0, 0, 1, 2 ); // setWidget( dockWidgetContents ); connect( mUndoStack, &QUndoStack::canUndoChanged, this, &QgsUndoWidget::undoChanged ); connect( mUndoStack, &QUndoStack::canRedoChanged, this, &QgsUndoWidget::redoChanged ); @@ -149,55 +176,7 @@ void QgsUndoWidget::setUndoStack( QUndoStack *undoStack ) // gets triggered also when a new command is added to stack, and twice when clicking a command in QUndoView connect( mUndoStack, &QUndoStack::indexChanged, this, &QgsUndoWidget::indexChanged ); - undoButton->setDisabled( !mUndoStack->canUndo() ); - redoButton->setDisabled( !mUndoStack->canRedo() ); -} - - - -void QgsUndoWidget::setupUi( QWidget *UndoWidget ) -{ - if ( UndoWidget->objectName().isEmpty() ) - UndoWidget->setObjectName( QStringLiteral( "UndoWidget" ) ); - UndoWidget->resize( 200, 223 ); - UndoWidget->setMinimumSize( QSize( 200, 220 ) ); - dockWidgetContents = new QWidget( UndoWidget ); - dockWidgetContents->setObjectName( QStringLiteral( "dockWidgetContents" ) ); - gridLayout = new QGridLayout( dockWidgetContents ); - gridLayout->setObjectName( QStringLiteral( "gridLayout" ) ); - 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 ); - undoButton->setObjectName( QStringLiteral( "undoButton" ) ); - undoButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionUndo.svg" ) ) ); - - gridLayout->addWidget( undoButton, 1, 0, 1, 1 ); - - redoButton = new QPushButton( dockWidgetContents ); - redoButton->setObjectName( QStringLiteral( "redoButton" ) ); - redoButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionRedo.svg" ) ) ); - - gridLayout->addWidget( redoButton, 1, 1, 1, 1 ); - - spacerItem1 = new QSpacerItem( 20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding ); - - gridLayout->addItem( spacerItem1, 0, 1, 1, 1 ); - - UndoWidget->setLayout( gridLayout ); - - retranslateUi( UndoWidget ); - - QMetaObject::connectSlotsByName( UndoWidget ); -} // setupUi - -void QgsUndoWidget::retranslateUi( QWidget *UndoWidget ) -{ - UndoWidget->setWindowTitle( QApplication::translate( "UndoWidget", "Undo/Redo" ) ); - undoButton->setText( QApplication::translate( "UndoWidget", "Undo" ) ); - redoButton->setText( QApplication::translate( "UndoWidget", "Redo" ) ); - Q_UNUSED( UndoWidget ) + mUndoButton->setDisabled( !mUndoStack->canUndo() ); + mRedoButton->setDisabled( !mUndoStack->canRedo() ); } diff --git a/src/app/qgsundowidget.h b/src/app/qgsundowidget.h index 8ca490b566e4..aa12c8515124 100644 --- a/src/app/qgsundowidget.h +++ b/src/app/qgsundowidget.h @@ -39,16 +39,8 @@ class APP_EXPORT QgsUndoWidget : public QgsPanelWidget { Q_OBJECT public: - QWidget *dockWidgetContents = nullptr; - QGridLayout *gridLayout = nullptr; - QSpacerItem *spacerItem = nullptr; - QPushButton *undoButton = nullptr; - QPushButton *redoButton = nullptr; - QSpacerItem *spacerItem1 = nullptr; QgsUndoWidget( QWidget *parent, QgsMapCanvas *mapCanvas ); - void setupUi( QWidget *UndoWidget ); - void retranslateUi( QWidget *UndoWidget ); /** * Setting new undo stack for undo view @@ -64,10 +56,10 @@ class APP_EXPORT QgsUndoWidget : public QgsPanelWidget /** * Handles destroying of stack when active layer is changed */ - void destroyStack(); + void unsetStack(); //! Access to dock's contents - QWidget *dockContents() { return dockWidgetContents; } + QWidget *dockContents() { return mDockWidgetContents; } public slots: @@ -104,8 +96,13 @@ class APP_EXPORT QgsUndoWidget : public QgsPanelWidget QUndoStack *mUndoStack = nullptr; QgsMapCanvas *mMapCanvas = nullptr; - int mPreviousIndex; - int mPreviousCount; + int mPreviousIndex = 0; + int mPreviousCount = 0; + + QWidget *mDockWidgetContents = nullptr; + QGridLayout *mGridLayout = nullptr; + QPushButton *mUndoButton = nullptr; + QPushButton *mRedoButton = nullptr; };