<!--
    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="tuto08_mesher_with_generic_scene">
    <requirement id="sight::module::ui" />
    <requirement id="sight::module::viz::scene3d" />
    <requirement id="sight::module::ui::qt" />
    <requirement id="sight::module::service" />

    <extension implements="sight::app::extension::config">
        <id>tuto08_mesher_with_generic_scene_app_cfg</id>
        <config>
            <!-- ******************************* Objects declaration ****************************** -->

            <object uid="modelSeries" type="sight::data::model_series" deferred="true" />
            <object uid="imageSeries" type="sight::data::image_series" />
            <object uid="tf" type="sight::data::transfer_function" />
            <object uid="reconstruction" type="sight::data::reconstruction" deferred="true" />
            <object uid="snapshot" type="sight::data::image" />

            <object uid="threshold_value" type="sight::data::integer" value="255" />
            <object uid="reduction_value" type="sight::data::real" value="50" />

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

            <service uid="main_frame" type="sight::module::ui::frame">
                <gui>
                    <frame>
                        <name>tuto08_mesher_with_generic_scene</name>
                        <icon>tuto08_mesher_with_generic_scene/tuto.ico</icon>
                    </frame>
                    <menubar />
                </gui>
                <registry>
                    <menubar sid="menu_bar_view" start="true" />
                    <view sid="main_view" start="true" />
                </registry>
            </service>

            <service uid="progress_bar_view" type="sight::module::ui::job_bar" />

            <service uid="menu_bar_view" type="sight::module::ui::menubar">
                <gui>
                    <layout>
                        <menu name="File" />
                        <menu name="Mesher" />
                    </layout>
                </gui>
                <registry>
                    <menu sid="menu_file_view" start="true" />
                    <menu sid="menu_mesher_view" start="true" />
                </registry>
            </service>

            <service uid="menu_file_view" type="sight::module::ui::menu">
                <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="open_image_act" start="true" />
                    <menuItem sid="save_model_series_act" start="true" />
                    <menuItem sid="quit_act" start="true" />
                </registry>
            </service>

            <service uid="menu_mesher_view" type="sight::module::ui::menu">
                <gui>
                    <layout>
                        <menuItem name="Create Mesh" />
                    </layout>
                </gui>
                <registry>
                    <menuItem sid="create_mesh_act" start="true" />
                </registry>
            </service>

            <service uid="main_view" type="sight::module::ui::view">
                <gui>
                    <layout type="sight::ui::layout::line">
                        <orientation value="vertical" />
                        <view proportion="1" />
                        <view proportion="0" minHeight="30" resizable="false" backgroundColor="#3E4453" />
                    </layout>
                </gui>
                <registry>
                    <view sid="container_view" start="true" />
                    <view sid="scene_editors_view" start="true" />
                </registry>
            </service>

            <service uid="container_view" type="sight::module::ui::view">
                <gui>
                    <layout type="sight::ui::layout::line">
                        <orientation value="horizontal" />
                        <view proportion="2" />
                        <view proportion="1" backgroundColor="#36393E" />
                    </layout>
                </gui>
                <registry>
                    <view sid="generic_scene_srv" start="true" />
                    <view sid="right_view" start="true" />
                </registry>
            </service>

            <service uid="right_view" type="sight::module::ui::view">
                <gui>
                    <layout type="sight::ui::layout::line">
                        <orientation value="vertical" />
                        <view proportion="0" />
                        <spacer />
                    </layout>
                </gui>
                <registry>
                    <view sid="organs_view" start="true" />
                </registry>
            </service>

            <service uid="organs_view" type="sight::module::ui::view">
                <gui>
                    <layout type="sight::ui::layout::toolbox">
                        <icon_color value="#FFFFFF" />
                        <view caption="Organs" expanded="true" />
                        <view caption="Material" expanded="true" />
                        <view caption="Representation" />
                        <view caption="Threshold" expanded="true" />
                    </layout>
                </gui>
                <registry>
                    <view sid="list_organ_editor_srv" start="false" />
                    <view sid="organ_material_editor_srv" start="false" />
                    <view sid="representation_editor_srv" start="false" />
                    <view sid="threshold_slider_srv" start="false" />
                </registry>
            </service>

            <service uid="scene_editors_view" type="sight::module::ui::view">
                <gui>
                    <layout type="sight::ui::layout::line">
                        <orientation value="horizontal" />
                        <view proportion="0" minWidth="50" />
                        <view proportion="1" />
                        <view proportion="0" minWidth="30" />
                    </layout>
                </gui>
                <registry>
                    <view sid="show_negato_srv" start="true" />
                    <view sid="slider_index_editor_srv" start="false" />
                    <view sid="snapshot_srv" start="true" />
                </registry>
            </service>

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

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

                    <layer id="default" order="1">
                        <adaptor uid="trackball_interactor_adp" />
                        <adaptor uid="model_series_adp" />
                        <adaptor uid="negato_adp" />
                        <adaptor uid="snapshot_adp" />
                    </layer>
                </scene>
            </service>

            <service uid="trackball_interactor_adp" type="sight::module::viz::scene3d::adaptor::trackball_camera">
                <config priority="0" />
            </service>

            <service uid="model_series_adp" type="sight::module::viz::scene3d::adaptor::model_series" auto_connect="true">
                <in key="model" uid="${modelSeries}" />
            </service>

            <service uid="negato_adp" type="sight::module::viz::scene3d::adaptor::negato3d" auto_connect="true">
                <in key="image" uid="imageSeries" />
                <inout key="tf" uid="tf" />
                <config sliceIndex="axial" interactive="true" />
            </service>

            <service uid="snapshot_adp" type="sight::module::viz::scene3d::adaptor::fragments_info">
                <inout key="image" uid="snapshot" />
            </service>

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

            <service uid="open_image_act" type="sight::module::ui::action" />
            <service uid="save_model_series_act" type="sight::module::ui::action" />

            <service uid="quit_act" type="sight::module::ui::quit" />

            <service uid="create_mesh_act" type="sight::module::ui::action" />

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

            <service uid="snapshot_srv" type="sight::module::ui::qt::com::signal_button">
                <config>
                    <checkable>false</checkable>
                    <icon>sight::module::ui::icons/frame.svg</icon>
                </config>
            </service>

            <service uid="threshold_slider_srv" type="sight::module::ui::qt::settings">
                <inout group="keys">
                    <key uid="${threshold_value}" />
                    <key uid="${reduction_value}" />
                </inout>
                <ui>
                    <item name="Threshold" widget="slider" min="0" max="255" emit_on_release="true" />
                    <item name="Reduction" widget="slider" min="0." max="99." emit_on_release="true" />
                </ui>
            </service>

            <service uid="show_negato_srv" type="sight::module::ui::qt::com::signal_button">
                <config>
                    <checkable>true</checkable>
                    <icon>sight::module::ui::icons/cross.svg</icon>
                    <icon2>sight::module::ui::icons/layers.svg</icon2>
                    <iconWidth>40</iconWidth>
                    <iconHeight>16</iconHeight>
                    <checked>true</checked>
                </config>
            </service>

            <service uid="list_organ_editor_srv" type="sight::module::ui::qt::model::model_series_list" auto_connect="true">
                <inout key="modelSeries" uid="modelSeries" />
                <columns>
                    <organ_name>@organ_name</organ_name>
                </columns>
            </service>

            <service uid="slider_index_editor_srv" type="sight::module::ui::qt::image::slice_index_position_editor" auto_connect="true">
                <inout key="image" uid="imageSeries" />
                <config orientation="axial" label="index" display_axis_selector="false" display_step_buttons="false" />
            </service>

            <service uid="mesher_srv" type="sight::module::filter::mesh::vtk_mesher">
                <in key="imageSeries" uid="imageSeries" />
                <out key="modelSeries" uid="modelSeries" />
                <properties percent_reduction="${reduction_value}" threshold="${threshold_value}" />
            </service>

            <service uid="model_series_writer_srv" type="sight::module::ui::io::selector">
                <inout key="data" uid="modelSeries" />
                <type mode="writer" />
            </service>

            <service uid="image_reader_srv" type="sight::module::ui::io::selector">
                <inout key="data" uid="imageSeries" />
                <type mode="reader" />
            </service>

            <service uid="updater_reconst_srv" type="sight::module::data::select_object">
                <out key="object" uid="reconstruction" />
            </service>

            <service uid="organ_material_editor_srv" type="sight::module::ui::qt::reconstruction::organ_material_editor" auto_connect="true">
                <inout key="reconstruction" uid="reconstruction" />
            </service>

            <service uid="representation_editor_srv" type="sight::module::ui::qt::reconstruction::representation_editor" auto_connect="true">
                <inout key="reconstruction" uid="reconstruction" />
            </service>

            <!-- Write the snapshot image -->
            <service uid="image_writer_srv" type="sight::module::io::bitmap::writer">
                <in key="data" uid="snapshot" />
                <dialog policy="always" />
                <backends enable="all" mode="best" />
            </service>

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

            <connect>
                <signal>model_series_writer_srv/job_created</signal>
                <slot>progress_bar_view/show_job</slot>
            </connect>

            <connect>
                <signal>list_organ_editor_srv/reconstruction_selected</signal>
                <slot>updater_reconst_srv/add</slot>
            </connect>

            <connect>
                <signal>snapshot_srv/clicked</signal>
                <slot>image_writer_srv/update</slot>
            </connect>

            <!--
                Connection for 3D image slice:
                Connect the button (show_negato_srv) signal "toggled" to the image adaptor (negato_adp)
                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 map. It is recommanded to used because the adaptors
                exists only if the object is present.
            -->
            <connect>
                <signal>show_negato_srv/toggled</signal>
                <slot>negato_adp/update_visibility</slot>
            </connect>

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

            <connect>
                <signal>open_image_act/clicked</signal>
                <slot>image_reader_srv/update</slot>
            </connect>

            <connect>
                <signal>save_model_series_act/clicked</signal>
                <slot>model_series_writer_srv/update</slot>
            </connect>

            <connect>
                <signal>create_mesh_act/clicked</signal>
                <slot>mesher_srv/update</slot>
            </connect>

            <start uid="main_frame" />
            <start uid="progress_bar_view" />
            <start uid="image_writer_srv" />
            <start uid="updater_reconst_srv" />
            <start uid="representation_editor_srv" />
            <start uid="organ_material_editor_srv" />
            <start uid="slider_index_editor_srv" />
            <start uid="threshold_slider_srv" />
            <start uid="list_organ_editor_srv" />
            <start uid="mesher_srv" />
            <start uid="trackball_interactor_adp" />
            <start uid="model_series_adp" />
            <start uid="negato_adp" />
            <start uid="snapshot_adp" />
            <start uid="image_reader_srv" />
            <start uid="model_series_writer_srv" />
        </config>
    </extension>
</plugin>
