From: jose guzman Date: Thu, 8 Oct 2015 21:52:39 +0000 (+0200) Subject: undo redo for images in viewer X-Git-Tag: v0.1~339^2 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=e8d3eed5ce148875548884fb9d2284b712ba7867;p=cpPlugins.git undo redo for images in viewer --- diff --git a/appli/ImageMPR/CMakeLists.txt b/appli/ImageMPR/CMakeLists.txt index a35c436..7031e14 100644 --- a/appli/ImageMPR/CMakeLists.txt +++ b/appli/ImageMPR/CMakeLists.txt @@ -10,11 +10,12 @@ IF(USE_QT4) SET( App_QT_SOURCES - ImageMPR.cxx + ImageMPR.cxx ) SET( App_SOURCES - main.cxx + MementoState.cxx + main.cxx ) SET( App_QT_HEADERS @@ -22,6 +23,7 @@ IF(USE_QT4) ) SET( App_HEADERS + MementoState.h ) FILE(GLOB App_QT_UI "${App_SOURCE_DIR}/*.ui") FILE(GLOB App_QT_RES "${App_SOURCE_DIR}/*.qrc") diff --git a/appli/ImageMPR/ImageMPR.cxx b/appli/ImageMPR/ImageMPR.cxx index 0100a13..0823bb2 100644 --- a/appli/ImageMPR/ImageMPR.cxx +++ b/appli/ImageMPR/ImageMPR.cxx @@ -1,8 +1,10 @@ #include "ImageMPR.h" +#include "MementoState.h" #include "ui_ImageMPR.h" #include #include +#include #include #include @@ -26,7 +28,8 @@ ImageMPR::ImageMPR( QWidget* parent ) m_MeshReaderClass( "" ), m_MeshWriterClass( "" ), m_MeshCutterClass( "" ), - m_Image( NULL ) + m_Image( NULL ), + m_state(1) { this->m_UI->setupUi( this ); @@ -56,6 +59,14 @@ ImageMPR::ImageMPR( QWidget* parent ) this->m_UI->actionOpenInputPolyData, SIGNAL( triggered( ) ), this, SLOT( _triggered_actionOpenInputPolyData( ) ) ); + QObject::connect( + this->m_UI->actionUndo, SIGNAL(triggered()), + this, SLOT(_triggered_actionUndo()) + ); + QObject::connect( + this->m_UI->actionRedo, SIGNAL(triggered()), + this, SLOT(_triggered_actionRedo()) + ); // Start: load all disponible plugins this->_LoadPlugins( @@ -272,6 +283,8 @@ _triggered_actionOpenInputImage( ) this->m_MPRObjects->ActivateInteractors( ); this->m_MPRObjects->ResetCameras( ); this->m_MPRObjects->RenderAll( ); + + MementoState(m_state, this->m_Image); } else QMessageBox::critical( @@ -417,7 +430,10 @@ _triggered_actionImageToImage( ) if( this->m_Image.IsNotNull( ) ) this->m_MPRObjects->SetImage( this->m_Image->GetVTK< vtkImageData >( ) - ); + + ); + m_state++; + MementoState(m_state, this->m_Image); } else QMessageBox::critical( @@ -477,4 +493,50 @@ _triggered_actionImageToMesh( ) ); } +// ------------------------------------------------------------------------- +void ImageMPR:: +_triggered_actionUndo() +{ + MementoState memento = MementoState(); + + if (this->m_state>1) + { + this->m_state--; + this->m_MPRObjects->SetImage( + memento.getMemento(this->m_state)->GetOutput() + ); + } else + { + QMessageBox::critical( + this, + tr("Error message"), + tr("No history to undo") + ); + } + +} + +// ------------------------------------------------------------------------- +void ImageMPR:: +_triggered_actionRedo() +{ + MementoState memento = MementoState(); + try + { + this->m_state++; + this->m_MPRObjects->SetImage( + memento.getMemento(this->m_state)->GetOutput() + ); + } + catch (int err) + { + QMessageBox::critical( + this, + tr("Error message"), + tr("No history to redo") + ); + } +} + + // eof - $RCSfile$ diff --git a/appli/ImageMPR/ImageMPR.h b/appli/ImageMPR/ImageMPR.h index 82e2e19..e690a51 100644 --- a/appli/ImageMPR/ImageMPR.h +++ b/appli/ImageMPR/ImageMPR.h @@ -16,6 +16,9 @@ #include #include #include +#include +#include + // ------------------------------------------------------------------------- namespace Ui @@ -23,9 +26,93 @@ namespace Ui class ImageMPR; } +//========================================================================== +// Plugins types +//typedef cpPlugins::Interface::Interface TPluginsInterface; +//typedef cpPlugins::Interface::Object TPluginObject; +//typedef cpPlugins::Interface::DataObject TPluginData; +//typedef cpPlugins::Interface::Image TPluginImage; +//typedef cpPlugins::Interface::ImplicitFunction TPluginImplicitFunction; +//typedef cpPlugins::Interface::Mesh TPluginMesh; +//typedef cpPlugins::Interface::ProcessObject TPluginFilter; +//typedef cpPlugins::Interface::Parameters TParameters; +// +//typedef cpExtensions::Visualization::MPRObjects TMPRObjects; + + + + +//class MementoState +//{ +//private: +// long m_Id; +// +//public: +// +// MementoState() +// { +// m_Id = 0; +// }; +// +// MementoState(long id, TPluginImage* _img) +// { +// char * base = "state"; +// std::ostringstream oss; +// oss << id; +// std::string str = oss.str(); +// auto complement = str.c_str(); +// +// std::string fileName(base); +// fileName.append(complement); +// fileName.append(".mhd"); +// +// std::string fileNameRaw(base); +// fileNameRaw.append(complement); +// fileNameRaw.append(".raw"); +// save(fileName, fileNameRaw, _img); +// } +// +// vtkSmartPointer getMemento(long id) +// { +// char * base = "state"; +// std::ostringstream oss; +// oss << id; +// std::string str = oss.str(); +// auto complement = str.c_str(); +// +// std::string fileName(base); +// fileName.append(complement); +// fileName.append(".mhd"); +// return load(fileName); +// } +// +// void save(const std::string& filename, const std::string& filenameRaw, TPluginImage* img) { +// vtkSmartPointer writer = +// vtkSmartPointer::New(); +// writer->SetInputData(img->GetVTK< vtkImageData >()); +// writer->SetFileName(filename.c_str()); +// writer->SetRAWFileName(filenameRaw.c_str()); +// writer->Write(); +// } +// +// vtkSmartPointer load(const std::string& filename) { +// vtkSmartPointer reader = +// vtkSmartPointer::New(); +// reader->SetFileName(filename.c_str()); +// reader->Update(); +// return reader; +// } +// +//}; + +//========================================================================== + // ------------------------------------------------------------------------- /** */ + + + class ImageMPR : public QMainWindow { @@ -60,7 +147,8 @@ private slots: void _triggered_actionOpenInputPolyData( ); void _triggered_actionImageToImage( ); void _triggered_actionImageToMesh( ); - + void _triggered_actionUndo(); + void _triggered_actionRedo(); private: Ui::ImageMPR* m_UI; @@ -86,6 +174,9 @@ private: // Visualization stuff vtkSmartPointer< TMPRObjects > m_MPRObjects; + // Memento stuff + unsigned long m_state; + /* TODO vtkSmartPointer< vtkOrientationMarkerWidget > m_3DOrientationWidget; */ diff --git a/appli/ImageMPR/ImageMPR.ui b/appli/ImageMPR/ImageMPR.ui index 8e0ee04..8fd0d12 100644 --- a/appli/ImageMPR/ImageMPR.ui +++ b/appli/ImageMPR/ImageMPR.ui @@ -40,7 +40,7 @@ Qt::Horizontal - + true @@ -51,7 +51,7 @@ - + true @@ -67,7 +67,7 @@ Qt::Horizontal - + true @@ -78,7 +78,7 @@ - + true @@ -101,7 +101,7 @@ 0 0 718 - 27 + 21 @@ -126,7 +126,15 @@ Image to mesh + + + Edit + + + + + @@ -175,6 +183,22 @@ Open segmentation + + + Undo + + + Ctrl+Z + + + + + Redo + + + Ctrl+Y + + diff --git a/appli/ImageMPR/MementoState.cxx b/appli/ImageMPR/MementoState.cxx new file mode 100644 index 0000000..d55ceae --- /dev/null +++ b/appli/ImageMPR/MementoState.cxx @@ -0,0 +1,69 @@ +#include "MementoState.h" + +#include +#include + + +// ------------------------------------------------------------------------- +MementoState::MementoState() +{ + m_Id = 0; +}; + +// ------------------------------------------------------------------------- +MementoState::MementoState(long id, TPluginImage* _img) +{ + char * base = "state"; + std::ostringstream oss; + oss << id; + std::string str = oss.str(); + auto complement = str.c_str(); + + std::string fileName(base); + fileName.append(complement); + fileName.append(".mhd"); + + std::string fileNameRaw(base); + fileNameRaw.append(complement); + fileNameRaw.append(".raw"); + save(fileName, fileNameRaw, _img); +} + +// ------------------------------------------------------------------------- +vtkSmartPointer +MementoState::getMemento(long id) +{ + char * base = "state"; + std::ostringstream oss; + oss << id; + std::string str = oss.str(); + auto complement = str.c_str(); + + std::string fileName(base); + fileName.append(complement); + fileName.append(".mhd"); + return load(fileName); +} + +// ------------------------------------------------------------------------- +void MementoState:: +save(const std::string& filename, const std::string& filenameRaw, TPluginImage* img) { + vtkSmartPointer writer = + vtkSmartPointer::New(); + writer->SetInputData(img->GetVTK< vtkImageData >()); + writer->SetFileName(filename.c_str()); + writer->SetRAWFileName(filenameRaw.c_str()); + writer->Write(); +} + +// ------------------------------------------------------------------------- +vtkSmartPointer +MementoState::load(const std::string& filename) { + vtkSmartPointer reader = + vtkSmartPointer::New(); + reader->SetFileName(filename.c_str()); + reader->Update(); + return reader; +} + +// eof - $RCSfile$ diff --git a/appli/ImageMPR/MementoState.h b/appli/ImageMPR/MementoState.h new file mode 100644 index 0000000..35befec --- /dev/null +++ b/appli/ImageMPR/MementoState.h @@ -0,0 +1,67 @@ +#ifndef __MEMENTOSTATE__H__ +#define __MEMENTOSTATE__H__ + +#include + + +// vtk stuff +#include +#include + +// Plugins interface +#include +#include +#include +#include +#include + +#include + +// ------------------------------------------------------------------------- +namespace Ui +{ + class ImageMPR; +} + +//========================================================================== +// Plugins types +typedef cpPlugins::Interface::Interface TPluginsInterface; +typedef cpPlugins::Interface::Object TPluginObject; +typedef cpPlugins::Interface::DataObject TPluginData; +typedef cpPlugins::Interface::Image TPluginImage; +typedef cpPlugins::Interface::ImplicitFunction TPluginImplicitFunction; +typedef cpPlugins::Interface::Mesh TPluginMesh; +typedef cpPlugins::Interface::ProcessObject TPluginFilter; +typedef cpPlugins::Interface::Parameters TParameters; + +typedef cpExtensions::Visualization::MPRObjects TMPRObjects; + +class MementoState +{ +private: + long m_Id; + +public: + + MementoState(); + MementoState(long id, TPluginImage* _img); + vtkSmartPointer getMemento(long id); +private: + void save(const std::string& filename, const std::string& filenameRaw, TPluginImage* img); + vtkSmartPointer load(const std::string& filename); + +}; + +//========================================================================== + +// ------------------------------------------------------------------------- +/** + */ + + + + + +#endif // __MEMENTOSTATE__H__ + +// eof - $RCSfile$