<!--
    This tutorial shows a VTK scene containing a 3D image and a 3D model.
    To use this application, you should open a 3D image mask of an organ. An used the mesher actions to creates a
    reconstruction. Then, you can update the organ color, transparence, ... using the editors in the right.
 -->
<plugin id="Tuto08MesherWithGenericScene" >

    <requirement id="sight::module::ui::base" />
    <requirement id="sight::module::viz::scene3d" />
    <requirement id="sight::module::ui::qt" />
    <requirement id="sight::module::service" />

    <extension implements="::sight::service::extension::AppConfig" >
        <id>Tuto08MesherWithGenericScene_AppCfg</id>
        <config>

            <!-- ******************************* Objects declaration ****************************** -->

            <object uid="modelSeries" type="sight::data::ModelSeries" src="deferred" />
            <object uid="imageSeries" type="sight::data::ImageSeries" />
            <object uid="image" type="sight::data::Image" src="deferred" />
            <object uid="reconstruction" type="sight::data::Reconstruction" src="deferred" />
            <object uid="snapshot" type="sight::data::Image" />

            <!-- ******************************* UI declaration *********************************** -->

            <service uid="mainFrame" type="sight::module::ui::base::SFrame" >
                <gui>
                    <frame>
                        <name>Tuto08MesherWithGenericScene</name>
                        <icon>Tuto08MesherWithGenericScene/tuto.ico</icon>
                    </frame>
                    <menuBar/>
                </gui>
                <registry>
                    <menuBar sid="menuBarView" start="true" />
                    <view sid="mainView" start="true" />
                </registry>
            </service>

            <service uid="progressBarView" type="sight::module::ui::base::SJobBar" />

            <service uid="menuBarView" type="sight::module::ui::base::SMenuBar" >
                <gui>
                    <layout>
                        <menu name="File" />
                        <menu name="Mesher" />
                    </layout>
                </gui>
                <registry>
                    <menu sid="menuFileView" start="true" />
                    <menu sid="menuMesherView" start="true" />
                </registry>
            </service>

            <service uid="menuFileView" type="sight::module::ui::base::SMenu" >
                <gui>
                    <layout>
                        <menuItem name="Open image" shortcut="Ctrl+O" />
                        <menuItem name="Save meshes" shortcut="Ctrl+S" />
                        <separator/>
                        <menuItem name="Quit" specialAction="QUIT" shortcut="Ctrl+Q" />
                    </layout>
                </gui>
                <registry>
                    <menuItem sid="openImageAct" start="true" />
                    <menuItem sid="saveModelSeriesAct" start="true" />
                    <menuItem sid="quitAct" start="true" />
                </registry>
            </service>

            <service uid="menuMesherView" type="sight::module::ui::base::SMenu" >
                <gui>
                    <layout>
                        <menuItem name="Create Mesh 50" />
                        <menuItem name="Create Mesh 80" />
                    </layout>
                </gui>
                <registry>
                    <menuItem sid="createMesh50Act" start="true" />
                    <menuItem sid="createMesh80Act" start="true" />
                </registry>
            </service>

            <service uid="mainView" type="sight::module::ui::base::SView" >
                <gui>
                    <layout type="sight::ui::base::LineLayoutManager" >
                        <orientation value="vertical" />
                        <view proportion="1" />
                        <view proportion="0" minHeight="30" resizable="false" backgroundColor="#3E4453" />
                    </layout>
                </gui>
                <registry>
                    <view sid="containerView" start="true" />
                    <view sid="sceneEditorsView" start="true" />
                </registry>
            </service>

            <service uid="containerView" type="sight::module::ui::base::SView" >
                <gui>
                    <layout type="sight::ui::base::LineLayoutManager" >
                        <orientation value="horizontal" />
                        <view proportion="2" />
                        <view proportion="1" backgroundColor="#36393E" />
                    </layout>
                </gui>
                <registry>
                    <view sid="genericSceneSrv" start="true" />
                    <view sid="organsView" start="true" />
                </registry>
            </service>

            <service uid="organsView" type="sight::module::ui::base::SView" >
                <gui>
                    <layout type="sight::ui::base::ToolboxLayoutManager" >
                        <view caption="Organs" expanded="true" />
                        <view caption="Material" expanded="true" />
                        <view caption="Representation" />
                    </layout>
                </gui>
                <registry>
                    <view sid="listOrganEditorSrv" start="false" />
                    <view sid="organMaterialEditorSrv" start="false" />
                    <view sid="representationEditorSrv" start="false" />
                </registry>
            </service>

            <service uid="sceneEditorsView" type="sight::module::ui::base::SView" >
                <gui>
                    <layout type="sight::ui::base::LineLayoutManager" >
                        <orientation value="horizontal" />
                        <view proportion="0" minWidth="50" />
                        <view proportion="1" />
                        <view proportion="0" minWidth="30" />
                    </layout>
                </gui>
                <registry>
                    <view sid="showNegatoSrv" start="true" />
                    <view sid="sliderIndexEditorSrv" start="false" />
                    <view sid="snapshotSrv" start="true" />
                </registry>
            </service>

            <!-- *************************** Begin generic scene *************************** -->

            <service uid="genericSceneSrv" type="sight::viz::scene3d::SRender" >
                <scene>
                    <background topColor="#36393E" bottomColor="#36393E" />

                    <layer id="default" order="1" />
                    <adaptor uid="trackballInteractorAdp" />
                    <adaptor uid="modelSeriesAdp" />
                    <adaptor uid="negatoAdp" />
                    <adaptor uid="snapshotAdp" />
                </scene>
            </service>

            <service uid="trackballInteractorAdp" type="sight::module::viz::scene3d::adaptor::STrackballCamera" >
                <config layer="default" priority="0" />
            </service>

            <service uid="modelSeriesAdp" type="sight::module::viz::scene3d::adaptor::SModelSeries" autoConnect="true" >
                <in key="model" uid="modelSeries" />
                <config layer="default" />
            </service>

            <service uid="negatoAdp" type="sight::module::viz::scene3d::adaptor::SNegato3D" autoConnect="true" >
                <inout key="image" uid="image" />
                <config layer="default" sliceIndex="axial" interactive="true" />
            </service>

            <service uid="snapshotAdp" type="sight::module::viz::scene3d::adaptor::SFragmentsInfo" >
                <inout key="image" uid="snapshot" />
                <config layer="default" flip="true" />
            </service>

            <!-- ******************************* Actions ****************************************** -->

            <service uid="openImageAct" type="sight::module::ui::base::com::SStarter" >
                <start uid="imageReaderSrv" />
            </service>

            <service uid="saveModelSeriesAct" type="sight::module::ui::base::com::SStarter" >
                <start_if_exists uid="modelSeriesWriterSrv" />
            </service>

            <service uid="quitAct" type="sight::module::ui::base::SQuit" />

            <service uid="createMesh50Act" type="sight::module::ui::base::com::SStarter" >
                <start_if_exists uid="mesher50Srv" />
            </service>

            <service uid="createMesh80Act" type="sight::module::ui::base::com::SStarter" >
                <start_if_exists uid="mesher80Srv" />
            </service>

            <!-- ******************************* Services ***************************************** -->

            <service uid="snapshotSrv" type="sight::module::ui::qt::com::SSignalButton" >
                <config>
                   <checkable>false</checkable>
                   <icon>sight::module::ui::flaticons/YellowPhoto.svg</icon>
                </config>
            </service>

            <service uid="showNegatoSrv" type="sight::module::ui::qt::com::SSignalButton" >
                <config>
                    <checkable>true</checkable>
                    <icon>sight::module::ui::flaticons/RedCross.svg</icon>
                    <icon2>sight::module::ui::flaticons/Layers.svg</icon2>
                    <iconWidth>40</iconWidth>
                    <iconHeight>16</iconHeight>
                    <checked>true</checked>
                </config>
            </service>

            <service uid="listOrganEditorSrv" type="sight::module::ui::qt::model::SModelSeriesList" autoConnect="true" >
                <inout key="modelSeries" uid="modelSeries" />
                <columns>
                    <organ_name>@organ_name</organ_name>
                </columns>
            </service>

            <service uid="sliderIndexEditorSrv" type="sight::module::ui::qt::image::SliceIndexPositionEditor" autoConnect="true" >
                <inout key="image" uid="image" />
                <sliceIndex>axial</sliceIndex>
            </service>

            <service uid="mesher50Srv" type="sight::module::filter::mesh::SVTKMesher" >
                <in key="imageSeries" uid="imageSeries" />
                <out key="modelSeries" uid="modelSeries" />
                <config>
                    <percentReduction>50</percentReduction>
                </config>
            </service>

            <service uid="mesher80Srv" type="sight::module::filter::mesh::SVTKMesher" >
                <in key="imageSeries" uid="imageSeries" />
                <out key="modelSeries" uid="modelSeries" />
                <config>
                    <percentReduction>80</percentReduction>
                </config>
            </service>

            <service uid="modelSeriesWriterSrv" type="sight::module::ui::base::io::SSelector" >
                <inout key="data" uid="modelSeries" />
                <type mode="writer" />
            </service>

            <service uid="imageReaderSrv" type="sight::module::ui::base::io::SSelector" >
                <inout key="data" uid="imageSeries" />
                <type mode="reader" />
            </service>

            <!-- extract a ::sight::data::Image from the ::sight::data::ImageSeries -->
            <service uid="extractImageSrv" type="sight::module::data::SExtractObj" >
               <inout key="source" uid="imageSeries" >
                   <extract from="@image" />
               </inout>
               <out group="target" >
                   <key uid="image" />
               </out>
            </service>

            <!-- This service converts a simple fwData::Image to medical Image -->
            <service uid="medicaImageConverterSrv" type="sight::module::data::MedicalImageSrv" autoConnect="true" >
                <inout key="image" uid="image" />
            </service>

            <service uid="updaterReconstSrv" type="sight::module::data::updater::SObjFromSlot" >
                <out key="object" uid="reconstruction" />
            </service>

            <service uid="organMaterialEditorSrv" type="sight::module::ui::qt::reconstruction::SOrganMaterialEditor" autoConnect="true" >
                <inout key="reconstruction" uid="reconstruction" />
            </service>

            <service uid="representationEditorSrv" type="sight::module::ui::qt::reconstruction::RepresentationEditor" autoConnect="true" >
                <inout key="reconstruction" uid="reconstruction" />
            </service>

            <!-- Write the snapshot image -->
            <service uid="imageWriterSrv" type="sight::module::ui::base::io::SSelector" >
                <inout key="data" uid="snapshot" />
                <type mode="writer" />
                <selection mode="exclude" />
                <addSelection service="::sight::module::io::atoms::SWriter" />
            </service>

            <!-- ******************************* Connections ***************************************** -->

            <connect>
                <signal>modelSeriesWriterSrv/jobCreated</signal>
                <slot>progressBarView/showJob</slot>
            </connect>

            <connect>
                <signal>listOrganEditorSrv/reconstructionSelected</signal>
                <slot>updaterReconstSrv/add</slot>
            </connect>

            <connect>
                <signal>snapshotSrv/clicked</signal>
                <slot>imageWriterSrv/update</slot>
            </connect>

            <!--
                Connection for 3D image slice:
                Connect the button (showNegatoSrv) signal "toggled" to the image adaptor (negatoAdp)
                slot "showSlice", this signals/slots contains a boolean.
                The image slices will be show or hide when the button is checked/unchecked.

                The "waitForKey" attribut means that the signal and slot are connected only if the key
                "image" is present in the scene composite. It is recommanded to used because the adaptors
                exists only if the object is present.
            -->
            <connect>
                <signal>showNegatoSrv/toggled</signal>
                <slot>negatoAdp/updateVisibility</slot>
            </connect>

            <connect>
                <signal>imageSeries/modified</signal>
                <slot>extractImageSrv/update</slot>
            </connect>

            <!-- ******************************* Start services ***************************************** -->

            <start uid="mainFrame" />
            <start uid="progressBarView" />
            <start uid="imageWriterSrv" />
            <start uid="updaterReconstSrv" />
            <start uid="medicaImageConverterSrv" />
            <start uid="representationEditorSrv" />
            <start uid="organMaterialEditorSrv" />
            <start uid="sliderIndexEditorSrv" />
            <start uid="listOrganEditorSrv" />
            <start uid="mesher50Srv" />
            <start uid="mesher80Srv" />
            <start uid="modelSeriesWriterSrv" />
            <start uid="trackballInteractorAdp" />
            <start uid="modelSeriesAdp" />
            <start uid="negatoAdp" />
            <start uid="snapshotAdp" />
            <start uid="extractImageSrv" />

        </config>
    </extension>
</plugin>
