Commit 453cccbf authored by David Thompson's avatar David Thompson
Browse files

Allow plugins to provide custom context menus.

This adds a new interface class to inherit that will be queried
whenever a context menu is requested.
parent 5fd66b37
Pipeline #212242 failed with stages
ParaView now supports plugins that add to or replace the default context menu,
via the pqContextMenuInterface class. The default ParaView context menu code
has been moved out of pqPipelineContextMenuBehavior into pqDefaultContextMenu.
Context menu interface objects have a priority; when preparing a menu in
response to user action, the objects are sorted by descending priority.
This allows plugins to place menu items relative to others (such as the
default menu) as well as preempt all interfaces with lower priority by
indicating (with their return value) that the behavior should stop iterating
over context-menu interfaces.
There is an example in `Examples/Plugins/ContextMenu` and documentation
in `Utilities/Doxygen/pages/PluginHowto.md`.
......@@ -53,6 +53,7 @@ endif ()
add_example(Plugins/AddPQProxy)
add_example(Plugins/Autostart)
add_example(Plugins/ComplexPluginArchitecture)
add_example(Plugins/ContextMenu)
add_example(Plugins/DockWidget)
add_example(Plugins/ElevationFilter)
add_example(Plugins/GUIMyToolBar)
......
cmake_minimum_required(VERSION 3.12)
project(ContextMenu)
find_package(ParaView REQUIRED)
include(GNUInstallDirs)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")
set("_paraview_plugin_default_${CMAKE_PROJECT_NAME}" ON)
paraview_plugin_scan(
PLUGIN_FILES "${CMAKE_CURRENT_SOURCE_DIR}/Plugin/paraview.plugin"
PROVIDES_PLUGINS plugins
REQUIRES_MODULES required_modules)
foreach (module IN LISTS required_modules)
if (NOT TARGET "${module}")
message("Missing required module: ${module}")
return ()
endif ()
endforeach ()
paraview_plugin_build(
RUNTIME_DESTINATION "${CMAKE_INSTALL_BINDIR}"
LIBRARY_DESTINATION "${CMAKE_INSTALL_LIBDIR}"
LIBRARY_SUBDIRECTORY "${PARAVIEW_PLUGIN_SUBDIR}"
PLUGINS ${plugins})
set(interfaces
ExampleContextMenu
)
set(sources
ExampleContextMenu.cxx
ExampleContextMenu.h
)
paraview_add_plugin(ContextMenu
VERSION "1.0"
UI_INTERFACES ${interfaces}
SOURCES ${sources}
)
/*=========================================================================
Program: ParaView
Module: ExampleContextMenu.cxx
Copyright (c) 2005,2006 Sandia Corporation, Kitware Inc.
All rights reserved.
ParaView is a free software; you can redistribute it and/or modify it
under the terms of the ParaView license version 1.2.
See License_v1.2.txt for the full ParaView license.
A copy of this license can be obtained by contacting
Kitware Inc.
28 Corporate Drive
Clifton Park, NY 12065
USA
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
========================================================================*/
#include "ExampleContextMenu.h"
#include "pqDataRepresentation.h"
#include "pqPipelineSource.h"
#include <QAction>
#include <QMenu>
#include <QString>
#include <iostream>
//-----------------------------------------------------------------------------
ExampleContextMenu::ExampleContextMenu()
{
}
//-----------------------------------------------------------------------------
ExampleContextMenu::ExampleContextMenu(QObject* parent)
: QObject(parent)
{
}
//-----------------------------------------------------------------------------
ExampleContextMenu::~ExampleContextMenu()
{
}
bool ExampleContextMenu::contextMenu(QMenu* menu, pqView* viewContext, const QPoint& viewPoint,
pqRepresentation* dataContext, const QList<unsigned int>& dataBlockContext) const
{
(void)viewContext;
(void)viewPoint;
(void)dataBlockContext;
auto rep = dynamic_cast<pqDataRepresentation*>(dataContext);
auto inp = rep ? rep->getInput() : nullptr;
// Only provide a context menu for box sources (i.e., pipeline
// objects created by clicking on the "Sources->Alphabetical->Box"
// menu item).
//
// Note that we could also make our menu dependent on the view type
// or other arguments that we are currently ignoring.
if (inp && inp->getSMName().startsWith("Box", Qt::CaseInsensitive) &&
inp->getSMGroup() == "sources")
{
// Provide a single contextual action for boxes:
QAction* twiddleThumbs = menu->addAction(QString("Twiddle thumbs"));
QObject::connect(twiddleThumbs, SIGNAL(triggered()), this, SLOT(twiddleThumbsAction()));
// Returning true here indicates that lower-priority pqContextMenuInterface objects
// should *not* be given a chance to modify the context menu (i.e., terminate early).
// Falling through and returning false allows other interfaces to add or even modify
// existing menu entries.
return true;
}
// When this plugin cannot provide a relevant context menu, it should return false.
// This will give other plugins a chance to provide a menu; if they cannot, it
// ParaView will provide a default.
return false;
}
void ExampleContextMenu::twiddleThumbsAction()
{
// Do something in response to user choosing our contextual
// action. Note that we could have stored the click location,
// data representation, and selected blocks as internal
// state to be used by this action.
std::cout << "Twiddling thumbs as we speak.\n";
}
/*=========================================================================
Program: ParaView
Module: ExampleContextMenu.h
Copyright (c) 2005,2006 Sandia Corporation, Kitware Inc.
All rights reserved.
ParaView is a free software; you can redistribute it and/or modify it
under the terms of the ParaView license version 1.2.
See License_v1.2.txt for the full ParaView license.
A copy of this license can be obtained by contacting
Kitware Inc.
28 Corporate Drive
Clifton Park, NY 12065
USA
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
========================================================================*/
#ifndef ExampleContextMenu_h
#define ExampleContextMenu_h
#include "pqContextMenuInterface.h"
/**
* @class ExampleContextMenu
* @brief Add a context menu to pqViews.
*
* ExampleContextMenu is the interface which plugins adding a context menu
* to pqViews should implement. One would typically use the `add_paraview_context_menu`
* CMake macro to ensure an instance of the class is created and registered with
* the pqPipelineContextMenuBehavior class (which is responsible for creating
* the context menu).
*/
class ExampleContextMenu : public QObject, public pqContextMenuInterface
{
Q_OBJECT
Q_INTERFACES(pqContextMenuInterface)
public:
ExampleContextMenu();
ExampleContextMenu(QObject* parent);
virtual ~ExampleContextMenu();
/// This method is called when a context menu is requested,
/// usually by a right click in a pqView instance.
///
/// This method should return true if the context is one
/// handled by this instance (and presumably it will modify
/// the provided QMenu) and false otherwise (indicating the
/// context is not one this instance handles).
///
/// Each registered interface is called until one returns
/// true, so your implementation should return false as
/// quickly as possible.
///
/// If dataContext is a pqDataRepresentation and holds
/// multiblock data, the dataBlockContext is a list of
/// block IDs to which the menu actions should apply.
bool contextMenu(QMenu* menu, pqView* viewContext, const QPoint& viewPoint,
pqRepresentation* dataContext, const QList<unsigned int>& dataBlockContext) const override;
/// Run before the default context menu (which has a priority of 0).
int priority() const override { return 1; }
public slots:
virtual void twiddleThumbsAction();
private:
Q_DISABLE_COPY(ExampleContextMenu)
};
#endif
NAME
ContextMenu
DESCRIPTION
An example plugin that implements a render-view context menu.
REQUIRES_MODULES
ParaView::pqComponents
......@@ -85,6 +85,7 @@ set(classes
pqDataAssemblyPropertyWidget
pqDataQueryReaction
pqDataTimeStepBehavior
pqDefaultContextMenu
pqDefaultMainWindow
pqDefaultViewBehavior
pqDeleteReaction
......
This diff is collapsed.
/*=========================================================================
Program: ParaView
Module: $RCSfile$
Copyright (c) 2005,2006 Sandia Corporation, Kitware Inc.
All rights reserved.
ParaView is a free software; you can redistribute it and/or modify it
under the terms of the ParaView license version 1.2.
See License_v1.2.txt for the full ParaView license.
A copy of this license can be obtained by contacting
Kitware Inc.
28 Corporate Drive
Clifton Park, NY 12065
USA
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
========================================================================*/
#ifndef pqDefaultContextMenu_h
#define pqDefaultContextMenu_h
#include "pqApplicationComponentsModule.h"
#include "pqContextMenuInterface.h"
#include "vtkType.h"
#include <QList> // needed for QList.
#include <QObject>
#include <QPoint> // needed for QPoint.
#include <QPointer>
class pqDataRepresentation;
class pqPipelineRepresentation;
class pqView;
class QAction;
class QMenu;
/**
* This interface creates ParaView's default context menu in render views.
* It has priority 0, so you can modify the QMenu it creates
* by using a lower (negative) priority in your own custom interface.
* You can eliminate the default menu by assigning your custom interface a
* positive priority and have its contextMenu() method return true.
*
* @sa pqPipelineContextMenuBehavior
*/
class PQAPPLICATIONCOMPONENTS_EXPORT pqDefaultContextMenu : public QObject,
public pqContextMenuInterface
{
Q_OBJECT
Q_INTERFACES(pqContextMenuInterface)
using Superclass = pqContextMenuInterface;
public:
pqDefaultContextMenu(QObject* parent = 0);
~pqDefaultContextMenu() override;
/**
* Create ParaView's default context menu.
* It will always return false (i.e., allow lower-priority menus to append/modify
* its output).
*/
bool contextMenu(QMenu* menu, pqView* viewContext, const QPoint& viewPoint,
pqRepresentation* dataContext, const QList<unsigned int>& dataBlockContext) const override;
/// The priority is used to order calls to pqContextMenuInterface::contextMenu among
/// all registered instances of pqContextMenuInterface.
int priority() const override { return 0; }
protected Q_SLOTS:
/**
* called to hide the representation.
*/
void hide();
/**
* called to hide the block. the action which emits the signal will
* contain the block index in its data().
*/
void hideBlock();
/**
* called to show only the selected block. the action which emits the
* signal will contain the block index in its data().
*/
void showOnlyBlock();
/**
* called to show all blocks.
*/
void showAllBlocks();
/**
* called to unset the visibility flag for the block. after this call the
* block will inherit the visibility from its parent. the action which
* emits the signal will contain the block index in its data()
*/
void unsetBlockVisibility();
/**
* called to set the color for the block. the action which emits the
* signal will contain the block index in its data()
*/
void setBlockColor();
/**
* called to unset the color for the block. the action which emits the
* signal will contain the block index in its data()
*/
void unsetBlockColor();
/**
* called to set the opacity for the block. the action which emits the
* signal will contain the block index in its data()
*/
void setBlockOpacity();
/**
* called to unset the opacity for the block. the action which emits the
* signal will contain the block index in its data()
*/
void unsetBlockOpacity();
/**
* called to change the representation type.
*/
void reprTypeChanged(QAction* action);
/**
* called to change the coloring mode.
*/
void colorMenuTriggered(QAction* action);
protected:
/**
* called to build the color arrays submenu.
*/
virtual void buildColorFieldsMenu(pqPipelineRepresentation* pipelineRepr, QMenu* menu) const;
/**
* return the name of the block from its flat index
*/
QString lookupBlockName(unsigned int flatIndex) const;
mutable QPoint Position;
mutable QPointer<pqDataRepresentation> PickedRepresentation;
mutable QList<unsigned int> PickedBlocks;
private:
Q_DISABLE_COPY(pqDefaultContextMenu)
};
#endif
......@@ -39,6 +39,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <QPoint> // needed for QPoint.
#include <QPointer>
class pqContextMenuInterface;
class pqDataRepresentation;
class pqPipelineRepresentation;
class pqView;
......@@ -63,75 +64,13 @@ public:
~pqPipelineContextMenuBehavior() override;
protected Q_SLOTS:
/**
* Called when a new view is added. We add actions to the widget for context
* menu if the view is a render-view.
*/
void onViewAdded(pqView*);
/**
* called to hide the representation.
*/
void hide();
/**
* called to hide the block. the action which emits the signal will
* contain the block index in its data().
*/
void hideBlock();
/**
* called to show only the selected block. the action which emits the
* signal will contain the block index in its data().
*/
void showOnlyBlock();
/**
* called to show all blocks.
*/
void showAllBlocks();
/**
* called to unset the visibility flag for the block. after this call the
* block will inherit the visibility from its parent. the action which
* emits the signal will contain the block index in its data()
*/
void unsetBlockVisibility();
/**
* called to set the color for the block. the action which emits the
* signal will contain the block index in its data()
*/
void setBlockColor();
/**
* called to unset the color for the block. the action which emits the
* signal will contain the block index in its data()
*/
void unsetBlockColor();
/**
* called to set the opacity for the block. the action which emits the
* signal will contain the block index in its data()
*/
void setBlockOpacity();
/**
* called to unset the opacity for the block. the action which emits the
* signal will contain the block index in its data()
*/
void unsetBlockOpacity();
/**
* called to change the representation type.
*/
void reprTypeChanged(QAction* action);
/**
* called to change the coloring mode.
*/
void colorMenuTriggered(QAction* action);
protected:
/**
* called to build the context menu for the given representation. If the
......@@ -140,11 +79,6 @@ protected:
*/
virtual void buildMenu(pqDataRepresentation* repr, unsigned int blockIndex);
/**
* called to build the color arrays submenu.
*/
virtual void buildColorFieldsMenu(pqPipelineRepresentation* pipelineRepr, QMenu* menu);
/**
* event filter to capture the right-click. We don't directly use mechanisms
* from QWidget to popup the context menu since all of those mechanism seem
......@@ -153,15 +87,8 @@ protected:
*/
bool eventFilter(QObject* caller, QEvent* e) override;
/**
* return the name of the block from its flat index
*/
QString lookupBlockName(unsigned int flatIndex) const;
QMenu* Menu;
QPoint Position;
QPointer<pqDataRepresentation> PickedRepresentation;
QList<unsigned int> PickedBlocks;
private:
Q_DISABLE_COPY(pqPipelineContextMenuBehavior)
......
......@@ -58,6 +58,7 @@ set(classes
pqCompositeDataInformationTreeModel
pqCompositePropertyWidgetDecorator
pqCompositeTreePropertyWidget
pqContextMenuInterface
pqCustomFilterDefinitionModel
pqCustomFilterDefinitionWizard
pqCustomFilterManager
......
/*=========================================================================
Program: ParaView
Module: pqContextMenuInterface.cxx
Copyright (c) 2005,2006 Sandia Corporation, Kitware Inc.
All rights reserved.
ParaView is a free software; you can redistribute it and/or modify it
under the terms of the ParaView license version 1.2.
See License_v1.2.txt for the full ParaView license.
A copy of this license can be obtained by contacting
Kitware Inc.
28 Corporate Drive
Clifton Park, NY 12065
USA
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
========================================================================*/
#include "pqContextMenuInterface.h"
//-----------------------------------------------------------------------------
pqContextMenuInterface::pqContextMenuInterface()
{
}
//-----------------------------------------------------------------------------
pqContextMenuInterface::~pqContextMenuInterface()
{
}
/*=========================================================================
Program: ParaView
Module: pqContextMenuInterface.h