#3048 AppFwk : Wire up functions for custom context menu

Add PdmUiObjectHandle ::defineCustomContextMenu()
This commit is contained in:
Magne Sjaastad 2018-06-15 12:40:23 +02:00
parent e8204465f9
commit d7037acedf
4 changed files with 91 additions and 2 deletions

View File

@ -10,6 +10,15 @@ include_directories (
..
)
# These headers need to go through Qt's MOC compiler
set( QOBJECT_HEADERS
cafPdmUiFieldEditorHandle.h
)
if ( NOT CMAKE_AUTOMOC )
qt4_wrap_cpp( MOC_FILES_CPP ${QOBJECT_HEADERS} )
endif()
set( PROJECT_FILES
cafInternalPdmFieldTypeSpecializations.h
@ -49,6 +58,7 @@ set( PROJECT_FILES
add_library( ${PROJECT_NAME}
${PROJECT_FILES}
${MOC_FILES_CPP}
)
target_link_libraries ( ${PROJECT_NAME}

View File

@ -37,10 +37,14 @@
#include "cafPdmUiFieldEditorHandle.h"
#include "cafPdmUiFieldHandle.h"
#include "cafPdmObjectHandle.h"
#include "cafPdmUiCommandSystemProxy.h"
#include "cafPdmUiFieldHandle.h"
#include "cafPdmUiObjectHandle.h"
#include <QAbstractScrollArea>
#include <QLabel>
#include <QMenu>
#include <QVariant>
namespace caf
@ -73,6 +77,18 @@ PdmUiFieldEditorHandle::~PdmUiFieldEditorHandle()
void PdmUiFieldEditorHandle::setField(PdmUiFieldHandle * field)
{
this->bindToPdmItem(field);
if (m_editorWidget)
{
if (field && field->isCustomContextMenuEnabled())
{
m_editorWidget->setContextMenuPolicy(Qt::CustomContextMenu);
}
else
{
m_editorWidget->setContextMenuPolicy(Qt::DefaultContextMenu);
}
}
}
//--------------------------------------------------------------------------------------------------
@ -91,6 +107,11 @@ void PdmUiFieldEditorHandle::createWidgets(QWidget * parent)
if (m_combinedWidget.isNull()) m_combinedWidget = createCombinedWidget(parent);
if (m_editorWidget.isNull()) m_editorWidget = createEditorWidget(parent);
if (m_labelWidget.isNull()) m_labelWidget = createLabelWidget(parent);
if (m_editorWidget)
{
connect(m_editorWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customMenuRequested(QPoint)));
}
}
//--------------------------------------------------------------------------------------------------
@ -127,5 +148,47 @@ void PdmUiFieldEditorHandle::updateLabelFromField(QLabel* label, const QString&
}
}
} //End of namespace caf
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiFieldEditorHandle::customMenuRequested(QPoint pos)
{
PdmObjectHandle* objectHandle = nullptr;
if (field() && field()->fieldHandle())
{
objectHandle = field()->fieldHandle()->ownerObject();
}
if (!objectHandle)
{
return;
}
auto widget = editorWidget();
if (widget)
{
QPoint globalPos;
auto abstractScrollAreaWidget = dynamic_cast<QAbstractScrollArea*>(widget);
if (abstractScrollAreaWidget)
{
// Qt doc: QAbstractScrollArea and its subclasses that map the context menu event to coordinates of the viewport().
globalPos = abstractScrollAreaWidget->viewport()->mapToGlobal(pos);
}
else
{
globalPos = widget->mapToGlobal(pos);
}
QMenu menu;
objectHandle->uiCapability()->defineCustomContextMenu(field()->fieldHandle(), &menu, widget);
if (!menu.actions().empty())
{
menu.exec(globalPos);
}
}
}
} // End of namespace caf

View File

@ -97,6 +97,7 @@ class PdmUiFieldHandle;
class PdmUiFieldEditorHandle : public PdmUiEditorHandle
{
Q_OBJECT
public:
PdmUiFieldEditorHandle();
@ -122,6 +123,9 @@ protected: // Virtual interface to override
void updateLabelFromField(QLabel* label, const QString& uiConfigName = "") const;
private slots:
void customMenuRequested(QPoint pos);
private:
QPointer<QWidget> m_combinedWidget;
QPointer<QWidget> m_editorWidget;

View File

@ -3,6 +3,7 @@
#include "cafPdmUiItem.h"
#include "cafPdmObjectCapability.h"
class QMenu;
namespace caf
{
@ -14,6 +15,7 @@ class PdmUiOrdering;
class PdmFieldHandle;
class PdmUiEditorAttribute;
class PdmUiObjectHandle : public PdmUiItem, public PdmObjectCapability
{
public:
@ -64,6 +66,7 @@ public: // Virtual
/// All field editor widgets are supposed to be created when this function is called
virtual void onEditorWidgetsCreated() {}
protected:
/// Override to customize the order and grouping of the Gui.
/// Fill up the uiOrdering object with groups and field references to create the gui structure
@ -84,6 +87,15 @@ protected:
// if user uses them on wrong type of objects
bool isInheritedFromPdmUiObject() { return true; }
/// Override used to append actions to a context menu
/// To use this method, enable custom context menu by
/// m_myField.uiCapability()->setUseCustomContextMenu(true);
friend class PdmUiFieldEditorHandle;
virtual void defineCustomContextMenu(const caf::PdmFieldHandle* fieldNeedingMenu,
QMenu* menu,
QWidget* fieldEditorWidget)
{}
private:
/// Helper method for the TreeItem generation stuff
void addDefaultUiTreeChildren(PdmUiTreeOrdering* uiTreeOrdering);