Files
ResInsight/ApplicationLibCode/ProjectDataModel/Annotations/RimTextAnnotation.cpp
T

257 lines
11 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2018- Equinor ASA
//
// 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 "RimTextAnnotation.h"
#include "RimAnnotationCollection.h"
#include "RimAnnotationGroupCollection.h"
#include "RimAnnotationInViewCollection.h"
#include "RimAnnotationTextAppearance.h"
#include "RimGridView.h"
#include "RimProject.h"
#include "AnnotationCommands/RicTextAnnotation3dEditor.h"
#include "RicVec3dPickEventHandler.h"
#include "cafCmdFeatureManager.h"
#include "cafPdmObjectScriptingCapability.h"
#include "cafPdmUiPickableLineEditor.h"
#include "cafPdmUiPushButtonEditor.h"
#include "cafPdmUiTextEditor.h"
#include "cafPdmUiTreeOrdering.h"
#include <QAction>
#include <QPointer>
#include <memory>
CAF_PDM_SOURCE_INIT( RimTextAnnotation, "TextAnnotation", "RimTextAnnotation" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimTextAnnotation::RimTextAnnotation()
{
CAF_PDM_InitScriptableObject( "TextAnnotation", ":/TextAnnotation16x16.png" );
setUi3dEditorTypeName( RicTextAnnotation3dEditor::uiEditorTypeName() );
CAF_PDM_InitField( &m_anchorPointXyd, "AnchorPointXyd", Vec3d::ZERO, "Anchor Point" );
m_anchorPointXyd.uiCapability()->setUiEditorTypeName( caf::PdmUiPickableLineEditor::uiEditorTypeName() );
CAF_PDM_InitField( &m_anchorPointPickEnabledButtonField, "AnchorPointPick", false, "" );
m_anchorPointPickEnabledButtonField.xmlCapability()->disableIO();
m_anchorPointPickEnabledButtonField.uiCapability()->setUiHidden( true );
CAF_PDM_InitField( &m_labelPointXyd, "LabelPointXyd", Vec3d::ZERO, "Label Point" );
m_labelPointXyd.uiCapability()->setUiEditorTypeName( caf::PdmUiPickableLineEditor::uiEditorTypeName() );
CAF_PDM_InitField( &m_labelPointPickEnabledButtonField, "LabelPointPick", false, "" );
m_labelPointPickEnabledButtonField.xmlCapability()->disableIO();
m_labelPointPickEnabledButtonField.uiCapability()->setUiHidden( true );
CAF_PDM_InitField( &m_text, "Text", QString( "(New text)" ), "Text" );
m_text.uiCapability()->setUiEditorTypeName( caf::PdmUiTextEditor::uiEditorTypeName() );
m_isChecked.registerKeywordAlias( "IsActive" );
m_isChecked.uiCapability()->setUiHidden( true );
CAF_PDM_InitFieldNoDefault( &m_textAppearance, "TextAppearance", "Text Appearance" );
m_textAppearance = new RimAnnotationTextAppearance();
m_textAppearance.uiCapability()->setUiTreeChildrenHidden( true );
CAF_PDM_InitFieldNoDefault( &m_nameProxy, "NameProxy", "Name Proxy" );
m_nameProxy.registerGetMethod( this, &RimTextAnnotation::extractNameFromText );
m_nameProxy.uiCapability()->setUiReadOnly( true );
m_anchorPointPickEventHandler = std::make_shared<RicVec3dPickEventHandler>( &m_anchorPointXyd );
m_labelPointPickEventHandler = std::make_shared<RicVec3dPickEventHandler>( &m_labelPointXyd, 0.1 );
setDeletable( true );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimTextAnnotation::~RimTextAnnotation()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::Vec3d RimTextAnnotation::anchorPoint() const
{
auto pos = m_anchorPointXyd();
pos.z() = -pos.z();
return pos;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimTextAnnotation::setAnchorPoint( const Vec3d& pointXyz )
{
m_anchorPointXyd = pointXyz;
m_anchorPointXyd.v().z() *= -1;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::Vec3d RimTextAnnotation::labelPoint() const
{
auto pos = m_labelPointXyd();
pos.z() = -pos.z();
return pos;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimTextAnnotation::setLabelPoint( const Vec3d& pointXyz )
{
m_labelPointXyd = pointXyz;
m_labelPointXyd.v().z() *= -1;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimTextAnnotation::setText( const QString& text )
{
m_text = text;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
const QString& RimTextAnnotation::text() const
{
return m_text();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimTextAnnotation::defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering& uiOrdering )
{
uiOrdering.add( &m_anchorPointXyd );
m_anchorPointXyd.uiCapability()->setAttribute( caf::PdmUiPickableLineEditor::Keys::PICK_EVENT_HANDLER, m_anchorPointPickEventHandler );
m_anchorPointXyd.uiCapability()->setAttribute( caf::PdmUiPickableLineEditor::Keys::ENABLE_PICKING,
(bool)m_anchorPointPickEnabledButtonField );
uiOrdering.addNewButton( m_anchorPointPickEnabledButtonField ? "Stop" : "Pick",
[this]()
{
m_anchorPointPickEnabledButtonField = !m_anchorPointPickEnabledButtonField;
updateConnectedEditors();
},
{ .newRow = false, .totalColumnSpan = 1 } );
uiOrdering.add( &m_labelPointXyd );
m_labelPointXyd.uiCapability()->setAttribute( caf::PdmUiPickableLineEditor::Keys::PICK_EVENT_HANDLER, m_labelPointPickEventHandler );
m_labelPointXyd.uiCapability()->setAttribute( caf::PdmUiPickableLineEditor::Keys::ENABLE_PICKING, (bool)m_labelPointPickEnabledButtonField );
uiOrdering.addNewButton( m_labelPointPickEnabledButtonField ? "Stop" : "Pick",
[this]()
{
m_labelPointPickEnabledButtonField = !m_labelPointPickEnabledButtonField;
updateConnectedEditors();
},
{ .newRow = false, .totalColumnSpan = 1 } );
uiOrdering.add( &m_text );
auto appearanceGroup = uiOrdering.addNewGroup( "Text Appearance" );
m_textAppearance->uiOrdering( uiConfigName, *appearanceGroup );
uiOrdering.skipRemainingFields( true );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimTextAnnotation::fieldChangedByUi( const caf::PdmFieldHandle* changedField, const QVariant& oldValue, const QVariant& newValue )
{
if ( changedField == &m_anchorPointXyd )
{
m_anchorPointPickEnabledButtonField = false;
if ( m_labelPointXyd().isZero() )
{
m_labelPointXyd = m_anchorPointXyd;
}
updateConnectedEditors();
}
if ( changedField == &m_labelPointXyd )
{
m_labelPointPickEnabledButtonField = false;
updateConnectedEditors();
}
auto annColl = firstAncestorOrThisOfTypeAsserted<RimAnnotationCollectionBase>();
if ( annColl ) annColl->scheduleRedrawOfRelevantViews();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
caf::PdmFieldHandle* RimTextAnnotation::userDescriptionField()
{
return &m_nameProxy;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimTextAnnotation::isActive()
{
return isChecked();
}
//--------------------------------------------------------------------------------------------------
/// Returns true if annotation can be displayed due to all toggles that affect this annotation
//--------------------------------------------------------------------------------------------------
bool RimTextAnnotation::isVisible() const
{
auto coll = firstAncestorOrThisOfType<RimAnnotationGroupCollection>();
bool visible = true;
if ( coll ) visible = coll->isVisible();
if ( visible ) visible = isChecked();
return visible;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimTextAnnotation::enablePicking( bool enable )
{
m_anchorPointPickEnabledButtonField = enable;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimAnnotationTextAppearance* RimTextAnnotation::appearance() const
{
return m_textAppearance();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RimTextAnnotation::extractNameFromText() const
{
return m_text().split( "\n" ).front();
}