# Selected segment and applied effect history on undo/redo

**URL:** <https://discourse.slicer.org/t/selected-segment-and-applied-effect-history-on-undo-redo/21880>\
**Category:** Development\
**Created:** [February 9, 2022, 9:20pm UTC](https://discourse.slicer.org/t/selected-segment-and-applied-effect-history-on-undo-redo/21880 "2022-02-09T21:20:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [February 9, 2022, 9:20pm UTC](https://discourse.slicer.org/t/selected-segment-and-applied-effect-history-on-undo-redo/21880/1 "2022-02-09T21:20:22Z")

</div>

Hi Devs.

Our application has a fixed segmentation workflow comprised of several segmentation steps. Each step may create 1 or more segments, select it and execute 1 or more segmentEditorEffects.

The problem with the current undo/redo functionality is that it cannot save the selected segment history along with the segmentation state after the user executes a segmentation step. Also the segmentationHistory does not save the applied effect name.

I would like to access some how this information so I can set up my custom undo/redo logic according to our segmentation steps so it is easy to navigate for the user.

Maybe accessing this signals would be useful:  
[https://github.com/Slicer/Slicer/blob/1178ed47833e8d921b36a17e0763828e56e9029b/Modules/Loadable/Segmentations/Widgets/qMRMLSegmentEditorWidget.cxx#L466-L469](https://github.com/Slicer/Slicer/blob/1178ed47833e8d921b36a17e0763828e56e9029b/Modules/Loadable/Segmentations/Widgets/qMRMLSegmentEditorWidget.cxx#L466-L469)

But the segmentEditorWidget.SegmentsTableView should be public I think.

Any ideas of how to solve this? Would Slicer core need to be updated for this use case? What would be the best way to do it?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [February 9, 2022, 9:39pm UTC](https://discourse.slicer.org/t/selected-segment-and-applied-effect-history-on-undo-redo/21880/2 "2022-02-09T21:39:50Z")

</div>

> [@mau\_igna\_06](#):
>
> The problem with the current undo/redo functionality is that it cannot save the selected segment history along with the segmentation state after the user executes a segmentation step.

If the default segmentation states that are saved are ideal for your workflow then you can disable undo/redo in the segment editor and instantiate your own `vtkSegmentationHistory` class and save and restore states whenever you see fit.

> [@mau\_igna\_06](#):
>
> Also the segmentationHistory does not save the applied effect name.

An optional “description” input argument could be added to vtkSegmentationHistory::SaveState(). Effects could put in that description what they are about to do, such as “Erode by 3mm”. That description could be shown in the undo/redo button tooltip. Pull request is welcome (i.e., you implement it and Slicer developers review and integrate).

> [@mau\_igna\_06](#):
>
> Maybe accessing this signals would be useful:  
> [Slicer/Modules/Loadable/Segmentations/Widgets/qMRMLSegmentEditorWidget.cxx at 1178ed47833e8d921b36a17e0763828e56e9029b · Slicer/Slicer · GitHub](https://github.com/Slicer/Slicer/blob/1178ed47833e8d921b36a17e0763828e56e9029b/Modules/Loadable/Segmentations/Widgets/qMRMLSegmentEditorWidget.cxx#L466-L469)

The currently selected segment is available in the segment editor node. You can observe that node if you want to get notified about selection change.

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [February 12, 2022, 6:11pm UTC](https://discourse.slicer.org/t/selected-segment-and-applied-effect-history-on-undo-redo/21880/3 "2022-02-12T18:11:54Z")

</div>

I was able to bypass the problem by using the info here: [Observe modified segment - #2 by lassoan](https://discourse.slicer.org/t/observe-modified-segment/7528/2) to create a log of executed effects (I could do that because we have segmentation steps that execute sequentially fixed segmentEditor effects or allow user-interactions like paint effect)  
And we decided to only give support to undo/redo the paint steps of our workflow.

However, there is a [bug](https://github.com/Slicer/Slicer/issues/6183) on the segment editor that breaks our custom undo/redo logic.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [February 14, 2022, 10:37pm UTC](https://discourse.slicer.org/t/selected-segment-and-applied-effect-history-on-undo-redo/21880/4 "2022-02-14T22:37:14Z")

</div>

Thanks for reporting the issue, indeed some of the saved undo states were indeed unnecessary - requiring users to do extra undo/redo clicks. The problem is now [fixed](https://github.com/Slicer/Slicer/commit/d547e8a34f9877574e1d7aadd64f4f7571591e47).
