]> Creatis software - cpPlugins.git/commitdiff
undo redo for images in viewer
authorjose guzman <jose@gmail.com>
Thu, 8 Oct 2015 21:52:39 +0000 (23:52 +0200)
committerjose guzman <jose@gmail.com>
Thu, 8 Oct 2015 21:52:39 +0000 (23:52 +0200)
appli/ImageMPR/CMakeLists.txt
appli/ImageMPR/ImageMPR.cxx
appli/ImageMPR/ImageMPR.h
appli/ImageMPR/ImageMPR.ui
appli/ImageMPR/MementoState.cxx [new file with mode: 0644]
appli/ImageMPR/MementoState.h [new file with mode: 0644]

index a35c436fd54ac5780571ed8efebc289f23e1729d..7031e14b5283eb5fd5f0e1c80f4b3c157bec91de 100644 (file)
@@ -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")
index 0100a138a5faadb54c3841b5c0fb0baa299c5435..0823bb2fca37a7d3aef576b2d42b0c060e63855b 100644 (file)
@@ -1,8 +1,10 @@
 #include "ImageMPR.h"
+#include "MementoState.h"
 #include "ui_ImageMPR.h"
 
 #include <vtkProperty.h>
 #include <vtkRenderWindow.h>
+#include <vtkMetaImageReader.h>
 
 #include <QFileDialog>
 #include <QMessageBox>
@@ -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$
index 82e2e197f313d5a2509e27668121706c89ff511d..e690a51640dc40a634a9c99fe7c8034b7492e0c8 100644 (file)
@@ -16,6 +16,9 @@
 #include <cpPlugins/Interface/Image.h>
 #include <cpPlugins/Interface/ImplicitFunction.h>
 #include <cpPlugins/Interface/Mesh.h>
+#include <vtkMetaImageWriter.h>
+#include <vtkMetaImageReader.h>
+
 
 // -------------------------------------------------------------------------
 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<vtkMetaImageReader> 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<vtkMetaImageWriter> writer =
+//                     vtkSmartPointer<vtkMetaImageWriter>::New();
+//             writer->SetInputData(img->GetVTK< vtkImageData >());
+//             writer->SetFileName(filename.c_str());
+//             writer->SetRAWFileName(filenameRaw.c_str());
+//             writer->Write();
+//     }
+//
+//     vtkSmartPointer<vtkMetaImageReader> load(const std::string& filename) {
+//             vtkSmartPointer<vtkMetaImageReader> reader =
+//             vtkSmartPointer<vtkMetaImageReader>::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;
   */
index 8e0ee04ffef39fdbb7949e0ec908201c52e7b15d..8fd0d129dbe85921f0f4d6ed047187e6988737d5 100644 (file)
@@ -40,7 +40,7 @@
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
-        <widget class="QVTKWidget" name="m_YPlaneVTK">
+        <widget class="QVTKWidget" name="m_YPlaneVTK" native="true">
          <property name="enabled">
           <bool>true</bool>
          </property>
@@ -51,7 +51,7 @@
           </size>
          </property>
         </widget>
-        <widget class="QVTKWidget" name="m_XPlaneVTK">
+        <widget class="QVTKWidget" name="m_XPlaneVTK" native="true">
          <property name="enabled">
           <bool>true</bool>
          </property>
@@ -67,7 +67,7 @@
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
-        <widget class="QVTKWidget" name="m_ZPlaneVTK">
+        <widget class="QVTKWidget" name="m_ZPlaneVTK" native="true">
          <property name="enabled">
           <bool>true</bool>
          </property>
@@ -78,7 +78,7 @@
           </size>
          </property>
         </widget>
-        <widget class="QVTKWidget" name="m_3DVTK">
+        <widget class="QVTKWidget" name="m_3DVTK" native="true">
          <property name="enabled">
           <bool>true</bool>
          </property>
      <x>0</x>
      <y>0</y>
      <width>718</width>
-     <height>27</height>
+     <height>21</height>
     </rect>
    </property>
    <widget class="QMenu" name="menuFile">
      <string>Image to mesh</string>
     </property>
    </widget>
+   <widget class="QMenu" name="menuEdit">
+    <property name="title">
+     <string>Edit</string>
+    </property>
+    <addaction name="actionUndo"/>
+    <addaction name="actionRedo"/>
+   </widget>
    <addaction name="menuFile"/>
+   <addaction name="menuEdit"/>
    <addaction name="MenuImageToImage"/>
    <addaction name="MenuImageToMesh"/>
   </widget>
     <string>Open segmentation</string>
    </property>
   </action>
+  <action name="actionUndo">
+   <property name="text">
+    <string>Undo</string>
+   </property>
+   <property name="shortcut">
+    <string>Ctrl+Z</string>
+   </property>
+  </action>
+  <action name="actionRedo">
+   <property name="text">
+    <string>Redo</string>
+   </property>
+   <property name="shortcut">
+    <string>Ctrl+Y</string>
+   </property>
+  </action>
  </widget>
  <layoutdefault spacing="6" margin="11"/>
  <customwidgets>
diff --git a/appli/ImageMPR/MementoState.cxx b/appli/ImageMPR/MementoState.cxx
new file mode 100644 (file)
index 0000000..d55ceae
--- /dev/null
@@ -0,0 +1,69 @@
+#include "MementoState.h"
+
+#include <vtkMetaImageReader.h>
+#include <vtkMetaImageWriter.h>
+
+
+// -------------------------------------------------------------------------
+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<vtkMetaImageReader>
+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<vtkMetaImageWriter> writer =
+               vtkSmartPointer<vtkMetaImageWriter>::New();
+       writer->SetInputData(img->GetVTK< vtkImageData >());
+       writer->SetFileName(filename.c_str());
+       writer->SetRAWFileName(filenameRaw.c_str());
+       writer->Write();
+}
+
+// -------------------------------------------------------------------------
+vtkSmartPointer<vtkMetaImageReader>
+MementoState::load(const std::string& filename) {
+       vtkSmartPointer<vtkMetaImageReader> reader =
+               vtkSmartPointer<vtkMetaImageReader>::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 (file)
index 0000000..35befec
--- /dev/null
@@ -0,0 +1,67 @@
+#ifndef __MEMENTOSTATE__H__
+#define __MEMENTOSTATE__H__
+
+#include <vector>
+
+
+// vtk stuff
+#include <vtkSmartPointer.h>
+#include <cpExtensions/Visualization/MPRObjects.h>
+
+// Plugins interface
+#include <cpPlugins/Interface/Interface.h>
+#include <cpPlugins/Interface/ProcessObject.h>
+#include <cpPlugins/Interface/Image.h>
+#include <cpPlugins/Interface/ImplicitFunction.h>
+#include <cpPlugins/Interface/Mesh.h>
+
+#include <vtkMetaImageReader.h>
+
+// -------------------------------------------------------------------------
+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<vtkMetaImageReader> getMemento(long id);
+private:
+       void save(const std::string& filename, const std::string& filenameRaw, TPluginImage* img); 
+       vtkSmartPointer<vtkMetaImageReader> load(const std::string& filename);
+       
+};
+
+//==========================================================================
+
+// -------------------------------------------------------------------------
+/**
+ */
+
+
+
+
+
+#endif // __MEMENTOSTATE__H__
+
+// eof - $RCSfile$