.. _highlight-tutorial:

#################################
Geometry: Highlight Clicked Parts 
#################################

.. image:: ../images/tut_highlight.png
    :height: 300    
    :align: center
    
This tutorial shows how highlight parts as a response to a mouse press event.

A highlighted part is shown with a white halo. This is useful for indicating for instance 
selection. Highlighting can be toggled on any number and any type of part independently.

The highlighting mechanism applies to all types of parts:

-   Geometry parts (using Halo effect) |br|
    :class:`cee::geo::EffectHalo`
-   Data parts |br|
    :func:`cee::ug::PartSettings::setHighlight()`
-   Isosurfaces |br|
    :func:`cee::ug::Isosurface::setHighlight()`
-   Isovolumes |br|
    :func:`cee::ug::Isovolume::setHighlight()`
-   Cutting planes |br|
    :func:`cee::ug::CuttingPlane::setHighlight()`

.. note:: 
    This tutorial expect the application to have a correctly configured :class:`cee::vis::View`
    in place. See demo applications on how to set up a :class:`cee::vis::View` in your application.

**Modify mouse event**

The mouse pressed event in the viewer needs to be modified to handle picking. In this 
tutorial we will trigger a picking event for all mouse clicks. 

The tutorial code is Qt based.

In the mouse button pressed event, find the intersect ray for the current mouse coordinates.

Emit a pick signal with the computed ray. This signal must be connected to a slot in the main application.

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/QtTutorialRunner/TutorialRunnerViewer.cpp
    :language: cpp
    :lines: 62-80

**Create the model**

Create a simple geometry model with 4 boxes.

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/Highlighting.cpp
    :language: cpp
    :lines: 41,46-49

**Set up the created model**

The model is ready to use and can be added to the view. Exactly where the view exists
depends on the platform and solution. These examples uses Qt and the view is set up 
in a :class:`cee::qt::ViewerWidget`.

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/Highlighting.cpp
    :language: cpp
    :lines: 53

**Respond to the pick event**

The viewer need to get the pick event from the viewer. This can typically be in the
mouse release event. The viewer produces a Ray based on the pick coordinates using 
:func:`cee::vis::Camera::rayFromWindowCoordinates()` and passes this ray to the handling function.

Get the current geometry model. Since this tutorials don't keep local pointer to all the
possible models it can create, get the model from the view.

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/Highlighting.cpp
    :language: cpp
    :lines: 67,70

Create a hit item from the ray object received from the viewer.

rayIntersect() returns true if any part were hit byt the ray.
Check if any parts where hit. If not, leave the highlighting as it is and return.

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/Highlighting.cpp
    :language: cpp
    :lines: 77-82

If a part was hit, deselect all other parts and highlight the hit part.

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/Highlighting.cpp
    :language: cpp
    :lines: 85-95

**See the complete source code here:**

:ref:`highlight-example`
