2018-11-23 13:33:59 +01:00
/////////////////////////////////////////////////////////////////////////////////
//
2018-11-27 14:43:25 +01:00
// Copyright (C) 2018- Equinor ASA
2018-11-23 13:33:59 +01:00
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RimAnnotationInViewCollection.h"
2018-11-27 15:43:44 +01:00
#include "RiaApplication.h"
2018-12-03 13:14:04 +01:00
#include "RimAnnotationCollection.h"
#include "RimAnnotationGroupCollection.h"
2018-11-30 08:59:22 +01:00
#include "RimCase.h"
2018-11-27 15:43:44 +01:00
#include "RimProject.h"
2018-11-23 13:33:59 +01:00
#include "RimGridView.h"
2018-11-26 14:48:06 +01:00
#include "RimTextAnnotation.h"
2018-12-03 13:14:04 +01:00
#include "RimReachCircleAnnotation.h"
#include "RimUserDefinedPolylinesAnnotation.h"
#include "RimPolylinesFromFileAnnotation.h"
#include "RimTextAnnotationInView.h"
#include "RimReachCircleAnnotationInView.h"
#include "RimUserDefinedPolylinesAnnotationInView.h"
#include "RimPolylinesFromFileAnnotationInView.h"
2018-11-23 13:33:59 +01:00
2018-11-30 08:59:22 +01:00
#include <cvfBoundingBox.h>
#include <cafPdmUiDoubleSliderEditor.h>
2018-12-03 13:14:04 +01:00
//--------------------------------------------------------------------------------------------------
/// Internal function
//--------------------------------------------------------------------------------------------------
caf :: PdmObject * sourcePdmAnnotation ( const caf :: PdmObject * annotationInView )
{
auto t = dynamic_cast < const RimTextAnnotationInView *> ( annotationInView );
if ( t )
{
return t -> sourceAnnotation ();
}
auto c = dynamic_cast < const RimReachCircleAnnotationInView *> ( annotationInView );
if ( c )
{
return c -> sourceAnnotation ();
}
auto up = dynamic_cast < const RimUserDefinedPolylinesAnnotationInView *> ( annotationInView );
if ( up )
{
return up -> sourceAnnotation ();
}
auto pf = dynamic_cast < const RimPolylinesFromFileAnnotationInView *> ( annotationInView );
if ( pf )
{
return pf -> sourceAnnotation ();
}
return nullptr ;
}
2018-11-23 13:33:59 +01:00
CAF_PDM_SOURCE_INIT ( RimAnnotationInViewCollection , "Annotations" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimAnnotationInViewCollection :: RimAnnotationInViewCollection ()
{
2018-11-28 15:40:20 +01:00
CAF_PDM_InitObject ( "Annotations" , ":/Annotations16x16.png" , "" , "" );
2018-11-23 13:33:59 +01:00
2018-11-29 10:13:19 +01:00
CAF_PDM_InitField ( & m_annotationPlaneDepth , "AnnotationPlaneDepth" , 0.0 , "Annotation Plane Depth" , "" , "" , "" );
CAF_PDM_InitField ( & m_snapAnnotations , "SnapAnnotations" , false , "Snap Annotations to Plane" , "" , "" , "" );
2018-11-30 08:59:22 +01:00
m_annotationPlaneDepth . uiCapability () -> setUiEditorTypeName ( caf :: PdmUiDoubleSliderEditor :: uiEditorTypeName ());
m_annotationPlaneDepth . uiCapability () -> setUiLabelPosition ( caf :: PdmUiItemInfo :: LabelPosType :: TOP );
2018-12-03 13:14:04 +01:00
CAF_PDM_InitFieldNoDefault ( & m_globalTextAnnotations , "TextAnnotationsInView" , "Global Text Annotations" , "" , "" , "" );
CAF_PDM_InitFieldNoDefault ( & m_globalReachCircleAnnotations , "ReachCircleAnnotationsInView" , "Global Reach Circle Annotations" , "" , "" , "" );
CAF_PDM_InitFieldNoDefault ( & m_globalUserDefinedPolylineAnnotations , "UserDefinedPolylinesAnnotationsInView" , "Global User Defined Polylines Annotations" , "" , "" , "" );
CAF_PDM_InitFieldNoDefault ( & m_globalPolylineFromFileAnnotations , "PolylinesFromFileAnnotationsInView" , "Global Polylines From File Annotations" , "" , "" , "" );
m_globalTextAnnotations . uiCapability () -> setUiHidden ( true );
m_globalReachCircleAnnotations . uiCapability () -> setUiHidden ( true );
m_globalUserDefinedPolylineAnnotations . uiCapability () -> setUiHidden ( true );
m_globalPolylineFromFileAnnotations . uiCapability () -> setUiHidden ( true );
2018-12-03 15:28:07 +01:00
m_globalTextAnnotations = new RimAnnotationGroupCollection ();
m_globalReachCircleAnnotations = new RimAnnotationGroupCollection ();
m_globalUserDefinedPolylineAnnotations = new RimAnnotationGroupCollection ();
m_globalPolylineFromFileAnnotations = new RimAnnotationGroupCollection ();
m_globalTextAnnotations -> uiCapability () -> setUiName ( "Global Text Annotations" );
m_globalReachCircleAnnotations -> uiCapability () -> setUiName ( "Global Reach Circle Annotations" );
m_globalUserDefinedPolylineAnnotations -> uiCapability () -> setUiName ( "Global User Defined Polylines Annotations" );
m_globalPolylineFromFileAnnotations -> uiCapability () -> setUiName ( "Global Polylines From File Annotations" );
m_globalTextAnnotations -> uiCapability () -> setUiIcon ( QIcon ( ":/TextAnnotation16x16.png" ));
m_globalReachCircleAnnotations -> uiCapability () -> setUiIcon ( QIcon ( ":/ReachCircle16x16.png" ));
m_globalUserDefinedPolylineAnnotations -> uiCapability () -> setUiIcon ( QIcon ( ":/PolylinesFromFile16x16.png" ));
m_globalPolylineFromFileAnnotations -> uiCapability () -> setUiIcon ( QIcon ( ":/PolylinesFromFile16x16.png" ));
2018-11-23 13:33:59 +01:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimAnnotationInViewCollection ::~ RimAnnotationInViewCollection ()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2018-12-03 13:14:04 +01:00
double RimAnnotationInViewCollection :: annotationPlaneZ () const
2018-11-23 13:33:59 +01:00
{
2018-12-03 13:14:04 +01:00
return - m_annotationPlaneDepth ();
2018-11-23 13:33:59 +01:00
}
2018-11-29 10:13:19 +01:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2018-12-03 13:14:04 +01:00
bool RimAnnotationInViewCollection :: snapAnnotations () const
2018-11-29 10:13:19 +01:00
{
2018-12-03 13:14:04 +01:00
return m_snapAnnotations ;
2018-11-29 10:13:19 +01:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2018-12-03 13:14:04 +01:00
std :: vector < RimTextAnnotationInView *> RimAnnotationInViewCollection :: globalTextAnnotations () const
2018-11-29 10:13:19 +01:00
{
2018-12-03 13:14:04 +01:00
std :: vector < RimTextAnnotationInView *> annotations ;
for ( auto & a : m_globalTextAnnotations -> annotations ())
{
annotations . push_back ( dynamic_cast < RimTextAnnotationInView *> ( a ));
}
return annotations ;
2018-11-29 10:13:19 +01:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2018-12-03 13:14:04 +01:00
std :: vector < RimReachCircleAnnotationInView *> RimAnnotationInViewCollection :: globalReachCircleAnnotations () const
2018-11-29 10:13:19 +01:00
{
2018-12-03 13:14:04 +01:00
std :: vector < RimReachCircleAnnotationInView *> annotations ;
for ( auto & a : m_globalReachCircleAnnotations -> annotations ())
{
annotations . push_back ( dynamic_cast < RimReachCircleAnnotationInView *> ( a ));
}
return annotations ;
}
2018-11-30 08:59:22 +01:00
2018-12-03 13:14:04 +01:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std :: vector < RimUserDefinedPolylinesAnnotationInView *> RimAnnotationInViewCollection :: globalUserDefinedPolylineAnnotations () const
{
std :: vector < RimUserDefinedPolylinesAnnotationInView *> annotations ;
for ( auto & a : m_globalUserDefinedPolylineAnnotations -> annotations ())
{
annotations . push_back ( dynamic_cast < RimUserDefinedPolylinesAnnotationInView *> ( a ));
}
return annotations ;
2018-11-29 10:13:19 +01:00
}
2018-11-23 13:33:59 +01:00
//--------------------------------------------------------------------------------------------------
2018-12-03 13:14:04 +01:00
///
2018-11-23 13:33:59 +01:00
//--------------------------------------------------------------------------------------------------
2018-12-03 13:14:04 +01:00
std :: vector < RimPolylinesFromFileAnnotationInView *> RimAnnotationInViewCollection :: globalPolylineFromFileAnnotations () const
2018-11-23 13:33:59 +01:00
{
2018-12-03 13:14:04 +01:00
std :: vector < RimPolylinesFromFileAnnotationInView *> annotations ;
for ( auto & a : m_globalPolylineFromFileAnnotations -> annotations ())
2018-11-23 13:33:59 +01:00
{
2018-12-03 13:14:04 +01:00
annotations . push_back ( dynamic_cast < RimPolylinesFromFileAnnotationInView *> ( a ));
2018-11-23 13:33:59 +01:00
}
2018-12-03 13:14:04 +01:00
return annotations ;
}
//--------------------------------------------------------------------------------------------------
/// Called when the global annotation collection has changed
//--------------------------------------------------------------------------------------------------
void RimAnnotationInViewCollection :: onGlobalCollectionChanged ( const RimAnnotationCollection * globalCollection )
{
// Sync annotations from global annotation collection
auto globals = globalCollection -> allPdmAnnotations ();
auto locals = allGlobalPdmAnnotations ();
std :: vector < const caf :: PdmObject *> globalAnnotationsToDelete ;
std :: set < caf :: PdmObject *> globalsSet ( globals . begin (), globals . end ());
for ( const auto local : locals )
{
auto sourceAnnotation = sourcePdmAnnotation ( local );
if ( globalsSet . count ( sourceAnnotation ) > 0 )
{
globalsSet . erase ( sourceAnnotation );
}
else
{
globalAnnotationsToDelete . push_back ( local );
}
}
// Remove deleted global annotations
for ( auto a : globalAnnotationsToDelete )
{
deleteGlobalAnnotation ( a );
}
// Add newly added global annotations
for ( const auto & global : globalsSet )
{
addGlobalAnnotation ( global );
}
updateConnectedEditors ();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
size_t RimAnnotationInViewCollection :: annotationsCount () const
{
return m_textAnnotations -> m_annotations . size () + allGlobalPdmAnnotations (). size ();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimAnnotationInViewCollection :: defineUiOrdering ( QString uiConfigName , caf :: PdmUiOrdering & uiOrdering )
{
uiOrdering . add ( & m_snapAnnotations );
if ( m_snapAnnotations ())
uiOrdering . add ( & m_annotationPlaneDepth );
uiOrdering . skipRemainingFields ( true );
2018-11-23 13:33:59 +01:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2018-12-03 13:14:04 +01:00
void RimAnnotationInViewCollection :: fieldChangedByUi ( const caf :: PdmFieldHandle * changedField , const QVariant & oldValue , const QVariant & newValue )
2018-11-23 13:33:59 +01:00
{
2018-12-03 13:14:04 +01:00
scheduleRedrawOfRelevantViews ();
2018-11-23 13:33:59 +01:00
}
2018-11-30 08:59:22 +01:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimAnnotationInViewCollection :: defineEditorAttribute ( const caf :: PdmFieldHandle * field ,
QString uiConfigName ,
caf :: PdmUiEditorAttribute * attribute )
{
if ( field == & m_annotationPlaneDepth )
{
auto * attr = dynamic_cast < caf :: PdmUiDoubleSliderEditorAttribute *> ( attribute );
if ( attr )
{
RimCase * rimCase ;
firstAncestorOrThisOfType ( rimCase );
if ( rimCase )
{
auto bb = rimCase -> allCellsBoundingBox ();
attr -> m_minimum = - bb . max (). z ();
attr -> m_maximum = - bb . min (). z ();
}
else
{
attr -> m_minimum = 0 ;
attr -> m_maximum = 10000 ;
}
}
}
}
2018-12-03 13:14:04 +01:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std :: vector < caf :: PdmObject *> RimAnnotationInViewCollection :: allGlobalPdmAnnotations () const
{
std :: vector < caf :: PdmObject *> all ;
all . insert ( all . end (), m_globalTextAnnotations -> m_annotations . begin (), m_globalTextAnnotations -> m_annotations . end ());
all . insert ( all . end (), m_globalReachCircleAnnotations -> m_annotations . begin (), m_globalReachCircleAnnotations -> m_annotations . end ());
all . insert ( all . end (), m_globalUserDefinedPolylineAnnotations -> m_annotations . begin (), m_globalUserDefinedPolylineAnnotations -> m_annotations . end ());
all . insert ( all . end (), m_globalPolylineFromFileAnnotations -> m_annotations . begin (), m_globalPolylineFromFileAnnotations -> m_annotations . end ());
return all ;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimAnnotationInViewCollection :: addGlobalAnnotation ( caf :: PdmObject * annotation )
{
auto t = dynamic_cast < RimTextAnnotation *> ( annotation );
if ( t )
{
m_globalTextAnnotations -> addAnnotation ( new RimTextAnnotationInView ( t ));
return ;
}
auto c = dynamic_cast < RimReachCircleAnnotation *> ( annotation );
if ( c )
{
m_globalReachCircleAnnotations -> addAnnotation ( new RimReachCircleAnnotationInView ( c ));
return ;
}
auto up = dynamic_cast < RimUserDefinedPolylinesAnnotation *> ( annotation );
if ( up )
{
m_globalUserDefinedPolylineAnnotations -> addAnnotation ( new RimUserDefinedPolylinesAnnotationInView ( up ));
return ;
}
auto pf = dynamic_cast < RimPolylinesFromFileAnnotation *> ( annotation );
if ( pf )
{
m_globalPolylineFromFileAnnotations -> addAnnotation ( new RimPolylinesFromFileAnnotationInView ( pf ));
return ;
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimAnnotationInViewCollection :: deleteGlobalAnnotation ( const caf :: PdmObject * annotation )
{
for ( size_t i = 0 ; i < m_globalTextAnnotations -> m_annotations . size (); i ++ )
{
if ( m_globalTextAnnotations -> m_annotations [ i ] == annotation )
{
m_globalTextAnnotations -> m_annotations . erase ( i );
return ;
}
}
for ( size_t i = 0 ; i < m_globalReachCircleAnnotations -> m_annotations . size (); i ++ )
{
if ( m_globalReachCircleAnnotations -> m_annotations [ i ] == annotation )
{
m_globalReachCircleAnnotations -> m_annotations . erase ( i );
return ;
}
}
for ( size_t i = 0 ; i < m_globalUserDefinedPolylineAnnotations -> m_annotations . size (); i ++ )
{
if ( m_globalUserDefinedPolylineAnnotations -> m_annotations [ i ] == annotation )
{
m_globalUserDefinedPolylineAnnotations -> m_annotations . erase ( i );
return ;
}
}
for ( size_t i = 0 ; i < m_globalPolylineFromFileAnnotations -> m_annotations . size (); i ++ )
{
if ( m_globalPolylineFromFileAnnotations -> m_annotations [ i ] == annotation )
{
m_globalPolylineFromFileAnnotations -> m_annotations . erase ( i );
return ;
}
}
}