ContextMenu.h

Go to the documentation of this file.
00001 /*
00002  * The information in this file is
00003  * Copyright(c) 2007 Ball Aerospace & Technologies Corporation
00004  * and is subject to the terms and conditions of the
00005  * GNU Lesser General Public License Version 2.1
00006  * The license text is available from   
00007  * http://www.gnu.org/licenses/lgpl.html
00008  */
00009 
00010 #ifndef CONTEXTMENU_H
00011 #define CONTEXTMENU_H
00012 
00013 #include "ConfigurationSettings.h"
00014 #include "ContextMenuAction.h"
00015 #include "ModelServices.h"
00016 
00017 #include <string>
00018 #include <vector>
00019 
00020 class QAction;
00021 class QObject;
00022 class QPoint;
00023 class SessionItem;
00024 
00025 /**
00026  *  Provides access to customize a context menu for one or more session items.
00027  *
00028  *  A ContextMenu is created by objects that display a context menu to the user
00029  *  for one or more session items.  An object usually creates the menu before
00030  *  it emits a signal that allows attached object to add actions to the menu or
00031  *  to remove one or more of the default actions.
00032  *
00033  *  Actions can be added to or removed from the menu using ContextMenuAction
00034  *  objects, which gives all objects that need to modify the actions a chance
00035  *  to add or remove actions.  The actual list of actions that are displayed to
00036  *  the user is not created until the menu is shown.
00037  *
00038  *  @see     \ref contextmenus "Customizing context menus"
00039  */
00040 class ContextMenu
00041 {
00042 public:
00043    SETTING(LogActions, ContextMenu, bool, false)
00044 
00045    /**
00046     *  Returns the session items for which the context menu will display
00047     *  actions.
00048     *
00049     *  @return  The session items for which the context menu has been
00050     *           requested.
00051     */
00052    virtual const std::vector<SessionItem*>& getSessionItems() const = 0;
00053 
00054    /**
00055     *  Returns only the session items of a specified type for which the context
00056     *  menu will display actions.
00057     *
00058     *  This is a convenience method that gets all session items for which the
00059     *  context menu has been requested filters the returned items based on the
00060     *  specified type.
00061     *
00062     *  @return  The session items of the specified type for which the context
00063     *           menu has been requested.  If the returned vector is empty, no
00064     *           session items of the specified type are included in those items
00065     *           for which the context menu is requested.  The pointers in the
00066     *           returned vector are guaranteed to be non-\b NULL.
00067     *
00068     *  @see     getSessionItems()
00069     */
00070    template<typename T>
00071    std::vector<T*> getSessionItems() const
00072    {
00073       std::vector<T*> items;
00074 
00075       const std::vector<SessionItem*>& allItems = getSessionItems();
00076       for (std::vector<SessionItem*>::const_iterator iter = allItems.begin(); iter != allItems.end(); ++iter)
00077       {
00078          T* pItem = model_cast<T*>(*iter);
00079          if (pItem != NULL)
00080          {
00081             items.push_back(pItem);
00082          }
00083       }
00084 
00085       return items;
00086    }
00087 
00088    /**
00089     *  Returns the context menu mouse click location.
00090     *
00091     *  @return  The mouse click location for the context menu in global
00092     *           coordinates.
00093     */
00094    virtual const QPoint& getMouseLocation() const = 0;
00095 
00096    /**
00097     *  Returns a pointer to an object that can be used as a parent for a menu
00098     *  action.
00099     *
00100     *  This method provides a QObject that can optionally be used as the parent
00101     *  for a menu action.  By using this object as a parent, the action will
00102     *  automatically be deleted when the context menu itself is deleted.
00103     *
00104     *  @return  A pointer to the object that can be used as a parent object for
00105     *           menu actions.
00106     *
00107     *  @warning Actions created with the returned object as a parent should not
00108     *           be stored, since the action will automatically be deleted when
00109     *           the context menu is deleted.
00110     */
00111    virtual QObject* getActionParent() const = 0;
00112 
00113    /**
00114     *  Adds an action to a context menu.
00115     *
00116     *  @param   menuAction
00117     *           The action to add to the context menu.
00118     *
00119     *  @return  Returns \b true if the action was successfully added to the
00120     *           menu.  Returns \b false if the given action has already been
00121     *           added to the menu.
00122     *
00123     *  @see     ContextMenuAction, addAction(QAction*, const std::string&),
00124     *           addActionBefore(), addActionAfter(), removeAction()
00125     */
00126    virtual bool addAction(const ContextMenuAction& menuAction) = 0;
00127 
00128    /**
00129     *  Adds an action to a context menu.
00130     *
00131     *  This is a convenience method that creates a ContextMenuAction to append
00132     *  the given action to the list of actions that will be displayed in the
00133     *  context menu and calls addAction(const ContextMenuAction&).
00134     *
00135     *  @param   pAction
00136     *           The action to add to the context menu.
00137     *  @param   id
00138     *           The unique identifier for the action.
00139     *
00140     *  @return  Returns \b true if the action was successfully added to the
00141     *           menu.  Returns \b false if the given action has already been
00142     *           added to the menu, if \em pAction is \b NULL, or if \em id is
00143     *           empty.
00144     *
00145     *  @see     addActionBefore(), addActionAfter(), removeAction()
00146     */
00147    virtual bool addAction(QAction* pAction, const std::string& id) = 0;
00148 
00149    /**
00150     *  Inserts an action into a context menu before another action.
00151     *
00152     *  This is a convenience method that creates a ContextMenuAction to add
00153     *  the given action to the list of actions that will be displayed in the
00154     *  context menu, sets the placement of the action to appear before
00155     *  the given action, and calls addAction(const ContextMenuAction&).
00156     *
00157     *  @param   pAction
00158     *           The action to add to the context menu.
00159     *  @param   id
00160     *           The unique identifier for the action to add.
00161     *  @param   beforeId
00162     *           The unique identifier for the action that should appear below
00163     *           the added action.  If \em beforeId is empty, this method is
00164     *           identical to addAction(QAction*, const std::string&).
00165     *
00166     *  @return  Returns \b true if the action was successfully added to the
00167     *           menu.  Returns \b false if the given action has already been
00168     *           added to the menu, if \em pAction is \b NULL, or if \em id is
00169     *           empty.
00170     *
00171     *  @see     addActionAfter(), removeAction()
00172     */
00173    virtual bool addActionBefore(QAction* pAction, const std::string& id, const std::string& beforeId) = 0;
00174 
00175    /**
00176     *  Inserts an action into a context menu after another action.
00177     *
00178     *  This is a convenience method that creates a ContextMenuAction to add
00179     *  the given action to the list of actions that will be displayed in the
00180     *  context menu, sets the placement of the action to appear after
00181     *  the given action, and calls addAction(const ContextMenuAction&).
00182     *
00183     *  @param   pAction
00184     *           The action to add to the context menu.
00185     *  @param   id
00186     *           The unique identifier for the action to add.
00187     *  @param   afterId
00188     *           The unique identifier for the action that should appear above
00189     *           the added action.  If \em afterId is empty, this method is
00190     *           identical to addAction(QAction*, const std::string&).
00191     *
00192     *  @return  Returns \b true if the action was successfully added to the
00193     *           menu.  Returns \b false if the given action has already been
00194     *           added to the menu, if \em pAction is \b NULL, or if \em id is
00195     *           empty.
00196     *
00197     *  @see     addActionBefore(), removeAction()
00198     */
00199    virtual bool addActionAfter(QAction* pAction, const std::string& id, const std::string& afterId) = 0;
00200 
00201    /**
00202     *  Removes an action from the context menu.
00203     *
00204     *  This method registers the given action to be removed from the context
00205     *  menu.  The action is not removed until all slot methods that are
00206     *  connected to the about-to-show context menu signal are called.  This
00207     *  allows for correct placement of all actions in the menu before any
00208     *  actions are removed.
00209     *
00210     *  @param   id
00211     *           The unique identifier for the action to remove from the menu.
00212     *
00213     *  @see     addAction(const ContextMenuAction&)
00214     */
00215    virtual void removeAction(const std::string& id) = 0;
00216 
00217 protected:
00218    /**
00219     *  This object does not need to be destroyed directly.  It will be deleted
00220     *  by the object that creates and displays the context menu.
00221     */
00222    virtual ~ContextMenu() {}
00223 };
00224 
00225 #endif

Software Development Kit - Opticks 4.9.0 Build 16218