#4014 Implement caf::PdmUiVec3dEditor and a RicVec3dPickEventHandler

This commit is contained in:
Gaute Lindkvist 2019-02-05 16:03:17 +01:00
parent 5d35678d97
commit 058b9cfb7a
6 changed files with 439 additions and 0 deletions

View File

@ -49,6 +49,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RicSelectOrCreateViewFeatureImpl.h
${CMAKE_CURRENT_LIST_DIR}/RicPickEventHandler.h
${CMAKE_CURRENT_LIST_DIR}/RicContourMapPickEventHandler.h
${CMAKE_CURRENT_LIST_DIR}/RicVec3dPickEventHandler.h
# General delete of any object in a child array field
${CMAKE_CURRENT_LIST_DIR}/RicDeleteItemExec.h
@ -127,6 +128,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RicSelectOrCreateViewFeatureImpl.cpp
${CMAKE_CURRENT_LIST_DIR}/RicPickEventHandler.cpp
${CMAKE_CURRENT_LIST_DIR}/RicContourMapPickEventHandler.cpp
${CMAKE_CURRENT_LIST_DIR}/RicVec3dPickEventHandler.cpp
# General delete of any object in a child array field
${CMAKE_CURRENT_LIST_DIR}/RicDeleteItemExec.cpp

View File

@ -0,0 +1,52 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2019- 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 "RicVec3dPickEventHandler.h"
#include "Rim3dView.h"
#include "cafDisplayCoordTransform.h"
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RicVec3dPickEventHandler::RicVec3dPickEventHandler(caf::PdmField<cvf::Vec3d>* vectorField)
: m_vectorField(vectorField)
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RicVec3dPickEventHandler::handlePickEvent(const Ric3DPickEvent& eventObject)
{
const Rim3dView* rimView = eventObject.m_view;
cvf::ref<caf::DisplayCoordTransform> transForm = rimView->displayCoordTransform();
cvf::Vec3d pickedPositionInUTM = transForm->transformToDomainCoord(eventObject.m_pickItemInfos.front().globalPickedPoint());
pickedPositionInUTM.z() *= -1.0;
m_vectorField->setValueWithFieldChanged(pickedPositionInUTM);
return true;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicVec3dPickEventHandler::notifyUnregistered()
{
}

View File

@ -0,0 +1,40 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2019- 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.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "RicPickEventHandler.h"
#include "cafPdmUiCoreVec3d.h"
class Rim3dView;
//==================================================================================================
/// A 3d view pick handler for Vec3d fields
//==================================================================================================
class RicVec3dPickEventHandler : public RicPickEventHandler
{
public:
RicVec3dPickEventHandler(caf::PdmField<cvf::Vec3d>* vectorField);
bool handlePickEvent(const Ric3DPickEvent& eventObject) override;
void notifyUnregistered() override;
private:
caf::PdmField<cvf::Vec3d>* m_vectorField;
};

View File

@ -15,6 +15,19 @@ else()
endif(Qt5Core_FOUND)
# These headers need to go through Qt's MOC compiler
set (MOC_HEADER_FILES
cafPdmUiVec3dEditor.h
)
# Run MOC on the headers
add_definitions(-DCVF_USING_CMAKE)
if (Qt5Core_FOUND)
qt5_wrap_cpp(MOC_SOURCE_FILES ${MOC_HEADER_FILES} )
else()
qt4_wrap_cpp(MOC_SOURCE_FILES ${MOC_HEADER_FILES} )
endif()
add_library( ${PROJECT_NAME}
cafPdmCoreColor3f.h
cafPdmUiCoreColor3f.cpp
@ -36,6 +49,10 @@ add_library( ${PROJECT_NAME}
cafPdmXmlMat4d.cpp
cafPdmXmlMat4d.h
cafPdmFieldCvfMat4d.h
cafPdmUiVec3dEditor.cpp
${MOC_HEADER_FILES}
${MOC_SOURCE_FILES}
)
target_include_directories(${PROJECT_NAME}

View File

@ -0,0 +1,228 @@
//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) 2019- Ceetron Solutions AS
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library 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.
//
// This library 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.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library 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 Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#include "cafPdmUiVec3dEditor.h"
#include "cafFactory.h"
#include "cafPdmField.h"
#include "cafPdmObject.h"
#include "cafPdmUiDefaultObjectEditor.h"
#include "cafPdmUiFieldEditorHandle.h"
#include "cafPdmUiOrdering.h"
#include "cafPdmUniqueIdValidator.h"
#include "cafPickEventHandler.h"
#include "cafSelectionManager.h"
#include <QAction>
#include <QDoubleValidator>
#include <QHBoxLayout>
#include <QPushButton>
using namespace caf;
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QWidget* PdmUiVec3dEditor::createEditorWidget(QWidget* parent)
{
QWidget* containerWidget = new QWidget(parent);
QHBoxLayout* layout = new QHBoxLayout();
layout->setMargin(0);
containerWidget->setLayout(layout);
m_lineEdit = new QLineEdit(containerWidget);
connect(m_lineEdit, SIGNAL(editingFinished()), this, SLOT(slotEditingFinished()));
m_pickButton = new QPushButton(containerWidget);
m_pickButton->setText("Pick");
m_pickButton->setCheckable(true);
layout->addWidget(m_lineEdit);
layout->addWidget(m_pickButton);
connect(m_pickButton, SIGNAL(toggled(bool)), this, SLOT(pickButtonToggled(bool)));
return containerWidget;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QWidget* PdmUiVec3dEditor::createLabelWidget(QWidget* parent)
{
m_label = new QLabel(parent);
return m_label;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void caf::PdmUiVec3dEditor::configureAndUpdateUi(const QString& uiConfigName)
{
if (!m_label.isNull())
{
PdmUiFieldEditorHandle::updateLabelFromField(m_label, uiConfigName);
}
PdmUiVec3dEditorAttribute attributes;
caf::PdmUiObjectHandle* uiObject = uiObj(uiField()->fieldHandle()->ownerObject());
if (uiObject)
{
uiObject->editorAttribute(uiField()->fieldHandle(), uiConfigName, &attributes);
}
m_attribute = attributes;
bool isReadOnly = uiField()->isUiReadOnly(uiConfigName);
m_pickButton->setVisible(attributes.pickEventHandler != nullptr);
m_pickButton->setEnabled(!isReadOnly);
m_lineEdit->setText(uiField()->uiValue().toString());
m_lineEdit->setReadOnly(isReadOnly);
if (isReadOnly)
{
m_lineEdit->setStyleSheet("QLineEdit {"
"color: #808080;"
"background-color: #F0F0F0;}");
}
else
{
m_lineEdit->setStyleSheet("");
}
pickButtonToggled(m_attribute.startPicking);
m_lineEdit->setToolTip(uiField()->uiToolTip(uiConfigName));
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QMargins caf::PdmUiVec3dEditor::calculateLabelContentMargins() const
{
QSize editorSize = m_lineEdit->sizeHint();
QSize labelSize = m_label->sizeHint();
int heightDiff = editorSize.height() - labelSize.height();
QMargins contentMargins = m_label->contentsMargins();
if (heightDiff > 0)
{
contentMargins.setTop(contentMargins.top() + heightDiff / 2);
contentMargins.setBottom(contentMargins.bottom() + heightDiff / 2);
}
return contentMargins;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void caf::PdmUiVec3dEditor::slotEditingFinished()
{
QString textValue = m_lineEdit->text();
QVariant v = textValue;
this->setValueToField(v);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void caf::PdmUiVec3dEditor::pickButtonToggled(bool checked)
{
if (!pickEventHandler())
{
return;
}
if (checked)
{
m_lineEdit->setStyleSheet("QLineEdit {"
"color: #000000;"
"background-color: #FFDCFF;}");
m_pickButton->setText("Stop Picking");
pickEventHandler()->registerAsPickEventHandler();
m_pickButton->setChecked(true);
}
else
{
m_lineEdit->setStyleSheet("");
m_pickButton->setText("Pick");
pickEventHandler()->unregisterAsPickEventHandler();
m_pickButton->setChecked(false);
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool caf::PdmUiVec3dEditor::isMultipleFieldsWithSameKeywordSelected(PdmFieldHandle* editorField) const
{
std::vector<PdmFieldHandle*> fieldsToUpdate;
fieldsToUpdate.push_back(editorField);
// For current selection, find all fields with same keyword
std::vector<PdmUiItem*> items;
SelectionManager::instance()->selectedItems(items, SelectionManager::FIRST_LEVEL);
for (size_t i = 0; i < items.size(); i++)
{
PdmUiFieldHandle* uiField = dynamic_cast<PdmUiFieldHandle*>(items[i]);
if (!uiField) continue;
PdmFieldHandle* field = uiField->fieldHandle();
if (field && field != editorField && field->keyword() == editorField->keyword())
{
fieldsToUpdate.push_back(field);
}
}
if (fieldsToUpdate.size() > 1)
{
return true;
}
return false;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
caf::PickEventHandler* caf::PdmUiVec3dEditor::pickEventHandler()
{
return m_attribute.pickEventHandler.get();
}
// Define at this location to avoid duplicate symbol definitions in 'cafPdmUiDefaultObjectEditor.cpp' in a cotire build. The
// variables defined by the macro are prefixed by line numbers causing a crash if the macro is defined at the same line number.
CAF_PDM_UI_FIELD_EDITOR_SOURCE_INIT(PdmUiVec3dEditor);

View File

@ -0,0 +1,100 @@
//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) 2019- Ceetron Solutions AS
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library 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.
//
// This library 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.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library 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 Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#pragma once
#include "cafPdmUiLineEditor.h"
#include "cafPdmCoreVec3d.h"
#include "cvfAssert.h"
#include "cvfVector3.h"
#include <memory>
#include <vector>
class QPushButton;
namespace caf
{
class PickEventHandler;
//==================================================================================================
///
//==================================================================================================
class PdmUiVec3dEditorAttribute : public PdmUiEditorAttribute
{
public:
PdmUiVec3dEditorAttribute() : startPicking(false) {}
public:
bool startPicking;
std::shared_ptr<PickEventHandler> pickEventHandler;
};
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
class PdmUiVec3dEditor : public PdmUiFieldEditorHandle
{
Q_OBJECT
CAF_PDM_UI_FIELD_EDITOR_HEADER_INIT;
public:
PdmUiVec3dEditor() {}
~PdmUiVec3dEditor() override {}
protected:
QWidget* createEditorWidget(QWidget* parent) override;
QWidget* createLabelWidget(QWidget* parent) override;
void configureAndUpdateUi(const QString& uiConfigName) override;
QMargins calculateLabelContentMargins() const override;
protected slots:
void slotEditingFinished();
void pickButtonToggled(bool checked);
private:
bool isMultipleFieldsWithSameKeywordSelected(PdmFieldHandle* editorField) const;
PickEventHandler* pickEventHandler();
private:
QPointer<QLineEdit> m_lineEdit;
QPointer<QPushButton> m_pickButton;
QPointer<QLabel> m_label;
PdmUiVec3dEditorAttribute m_attribute;
};
} // end namespace caf