ContextMenuInterface.hh 10.4 KB
Newer Older
 Jan Möbius committed Aug 05, 2009 1 /*===========================================================================*\  Jan Möbius committed Nov 25, 2010 2 3 * * * OpenFlipper *  Jan Möbius committed Jan 26, 2011 4 * Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *  Jan Möbius committed Nov 25, 2010 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 * www.openflipper.org * * * *--------------------------------------------------------------------------- * * This file is part of OpenFlipper. * * * * OpenFlipper 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 3 of * * the License, or (at your option) any later version with the * * following exceptions: * * * * If other files instantiate templates or use macros * * or inline functions from this file, or you compile this file and * * link it with other files to produce an executable, this file does * * not by itself cause the resulting executable to be covered by the * * GNU Lesser General Public License. This exception does not however * * invalidate any other reasons why the executable file might be * * covered by the GNU Lesser General Public License. * * * * OpenFlipper 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 for more details. * * * * You should have received a copy of the GNU LesserGeneral Public * * License along with OpenFlipper. If not, * * see . * * *  Jan Möbius committed Aug 05, 2009 33 34 35 \*===========================================================================*/ /*===========================================================================*\  Jan Möbius committed Nov 25, 2010 36 37 38 39 40 * * * $Revision$ * * $LastChangedBy$ * * $Date$ * * *  Jan Möbius committed Aug 05, 2009 41 \*===========================================================================*/  Jan Möbius committed Aug 29, 2008 42 43   Jan Möbius committed Jan 27, 2009 44 45 #ifndef CONTEXTMENUINTERFACE_HH #define CONTEXTMENUINTERFACE_HH  Jan Möbius committed Aug 29, 2008 46 47 48 49  #include #include #include  Jan Möbius committed Jan 27, 2009 50   Jan Möbius committed Mar 09, 2009 51   Jan Möbius committed Feb 23, 2011 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 /** \file ContextMenuInterface.hh * * Interface for adding context menus to OpenFlippers UI from a plugin.\ref contextmenuInterfacePage */ /** Enum to specify the type of context menus */ enum ContextMenuType { /// The Menu will be shown when an object was picked CONTEXTOBJECTMENU, /// The Menu will be shown when a node was picked CONTEXTNODEMENU, /// The Menu will be shown when the background was picked CONTEXTBACKGROUNDMENU  Jan Möbius committed Aug 29, 2008 70 };  Jan Möbius committed Jan 27, 2009 71   Jan Möbius committed Feb 23, 2011 72   Jan Möbius committed Feb 23, 2011 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100  /** \brief Interface class for creating custom context menus * * \ref contextmenuInterfacePage "Detailed description" * \n * \n * Using this interface you can create custom context menus for your plugin. You * can choose between context menus for objects, nodes or the background.\n * Before a menu of the requested type is shown, an update function for the specific type * will be invoked by the core. * You have to create a QAction. The signals and slots of your actions have to be connected * to your plugin. Just connect them as usual. Only visibility of the menu is handled * by the core. You can also add submenus to the context menus. Just add the action for * the menu ( menu->menuAction() ) */ class ContextMenuInterface { public : /// Destructor virtual ~ContextMenuInterface() {}; signals: /** \brief Add an entry for a context Menu * * Create an Action (Can also be the action of a Menu) and register this menu as a context menu to the core.  Jan Möbius committed Feb 25, 2011 101  * This Action will be visible when you right click in the viewer widget on an item  Jan Möbius committed Feb 23, 2011 102 103  * of the given context menu type. You can add a whole menu here by adding the action: * menu->menuAction() of your own menu.  Jan Möbius committed Feb 24, 2011 104  *  Jan Möbius committed Mar 15, 2011 105 106  * @param _action Pointer to the new Action * @param _type Type context menu type  Jan Möbius committed Feb 23, 2011 107  */  Jan Möbius committed Feb 24, 2011 108  virtual void addContextMenuItem(QAction* _action , ContextMenuType _type) {};  Jan Möbius committed Feb 23, 2011 109 110 111 112  /** \brief Add an entry for a context Menu * * Create an action (Can also be the action of a Menu) and register this action as a context menu entry to the core.  Jan Möbius committed Feb 25, 2011 113  * This Action will only be visible if the picked object is of the given DataType.  Jan Möbius committed Feb 23, 2011 114 115 116 117 118 119 120 121  * To support multiple object types with your menu, you can emit this signal multiple * times with the same action but different DataTypes. You can add a whole Menu here by adding the action: * menu->menuAction() * * @param _action Pointer to the new action * @param _objectType Type of the picked object * @param _type Type of the context Menu ( See ContextMenuType ) */  Jan Möbius committed Feb 24, 2011 122  virtual void addContextMenuItem(QAction* _action ,DataType _objectType , ContextMenuType _type ) {};  Jan Möbius committed Feb 23, 2011 123   Marcel Campen committed Jan 27, 2012 124 125 126  /// hide the main context menu virtual void hideContextMenu() {};  Jan Möbius committed Feb 23, 2011 127 128 129 130 131 private slots: /** When the main application requests a context menu, this slot is called before showing the window. * If an object is picked the id will be given in this call so you can change the contents of your menu * depending on the given object.  Jan Möbius committed Feb 24, 2011 132  *  Jan Möbius committed Feb 23, 2011 133 134  * @param _objectId id of the object */  Jan Möbius committed Feb 24, 2011 135  virtual void slotUpdateContextMenu( int _objectId ) {};  Jan Möbius committed Feb 23, 2011 136 137 138 139  /** When the main application requests a context menu, this slot is called before showing the window. * This slot will be called indicating that a scenegraph node not belonging to an object * has been picked.  Jan Möbius committed Feb 24, 2011 140  *  Jan Möbius committed Feb 23, 2011 141 142  * @param _node id of the picked node */  Jan Möbius committed Feb 24, 2011 143  virtual void slotUpdateContextMenuNode( int _nodeId ) {};  Jan Möbius committed Feb 23, 2011 144 145  /** When the main application requests a context menu, this slot is called before showing the window.  Jan Möbius committed Feb 24, 2011 146 147  * This slot will be called indicating that the background has been picked. *  Jan Möbius committed Feb 23, 2011 148 149 150 151 152 153  */ virtual void slotUpdateContextMenuBackground( ) {}; };  Jan Möbius committed Feb 23, 2011 154 155 156 157 158 159 160 161 /** \page contextmenuInterfacePage Context Menu Interface \image html ContextMenuInterface.png \n The ContextMenuInterface can be used by plugins to add menu entries to OpenFlippers UI. The entries will be added to OpenFlippers contextMenus in the glView (see image). You can choose between context menus for objects, nodes or the background.\n The given context menu entries will be shown when the user right clicks on the corresponding  Jan Möbius committed Feb 25, 2011 162 primitive (node,object) in the GL viewer.  Jan Möbius committed Feb 23, 2011 163 164 165 166 167 168 169 170 171 172  To use the ContextMenuInterface:
• include ContextMenuInterface.hh in your plugins header file
• derive your plugin from the class ContextMenuInterface
• add Q_INTERFACES(ContextMenuInterface) to your plugin class
• And add the signals or slots you want to use to your plugin class (You don't need to implement all of them)
 Jan Möbius committed Feb 24, 2011 173 Usually you should implement the BaseInterface::pluginsInitialized() function from BaseInterface. In this function you can setup  Jan Möbius committed Feb 23, 2011 174 175 176 177 178 179 180 181 182 your menus.\n You have to create a QAction. The signals and slots of your actions have to be connected to your plugin. Just connect them as usual. Only visibility of the menu is handled by the core. You can also add submenus to the context menus. Just add the action for the menu ( menu->menuAction() ) Before a menu of the requested type is shown, an update function for the specific type will be invoked by the core depending on your type of context menu  Jan Möbius committed Feb 23, 2011 183 ( ContextMenuInterface::slotUpdateContextMenu(), ContextMenuInterface::slotUpdateContextMenuNode(), ContextMenuInterface::slotUpdateContextMenuBackground() ).  Jan Möbius committed Feb 23, 2011 184 185 186 187 188 189 190 191 In this function you can update entries based on the object that was clicked on. The id of the node or the object is provided by these update functions. The following code shows a simple example to create a menu entry in the context menu. \code // Setup the menus in initialize plugins  Jan Möbius committed Feb 24, 2011 192 void ExamplePlugin::pluginsInitialized()  Jan Möbius committed Feb 23, 2011 193 194 195 196 197 198 199 200 201 202 203 { // create a global QMenu in the plugin that contains our menu // in the header is: // QMenu* contextMenu_ contextMenu_ = new QMenu(tr("Select")); // Create a menu action called all in the Selection context menu // in the header is: // QAction* menuAction_ menuAction_ = contextMenu_->addAction( tr("All Mesh vertices") ); menuAction->setToolTip(tr("Select all"));  Jan Möbius committed Feb 23, 2011 204   Jan Möbius committed Feb 23, 2011 205 206 207 208 209 210  // Add the new menu to OpenFlippers context menu fot objects // Show the context menu for triangle meshes emit addContextMenuItem(contextMenu_->menuAction() , DATA_TRIANGLE_MESH , CONTEXTOBJECTMENU ); // Show the context menu for poly meshes meshes emit addContextMenuItem(contextMenu_->menuAction() , DATA_POLY_MESH , CONTEXTOBJECTMENU );  Jan Möbius committed Feb 23, 2011 211  }  Jan Möbius committed Feb 23, 2011 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242  // Example function to update the context menu based on the given object id void ExamplePlugin::slotUpdateContextMenu( int _objectId ){ // Get the corresponding object of the given id or return BaseObjectData* object = 0; if ( !PluginFunctions::getObject( _objectId, object ) ) { return; } // If its a triangle mesh, rename the to triangle mesh selection if (object->dataType( DATA_TRIANGLE_MESH ) ) menuAction_->setText("All Triangle mesh vertices"); // If its a triangle mesh, rename the to poly mesh selection if ( object->dataType( DATA_POLY_MESH ) ) menuAction_->setText("All Triangle mesh vertices"); } } \endcode Signals and slots of your menus (e.g. from an action inside it) can be directly connected to signals and slots in your plugin. Therefore the embedding of your menus into the OpenFlippers context menu list is fully transparent. */  Jan Möbius committed Mar 09, 2009 243 Q_DECLARE_INTERFACE(ContextMenuInterface,"OpenFlipper.ContextMenuInterface/1.0")  Jan Möbius committed Jan 27, 2009 244   Jan Möbius committed Aug 29, 2008 245 #endif // CONTEXTMENUINTERFACE_HH