OverviewQtilities provides classes to allow management of actions and shortcuts in a Qt application. In small applications it is easy to do action management manually but as applications get bigger, its useful an it saves time to use a proper action management system. The action manager allows actions and shortcuts to be linked with contexts and to change their behavior depending on the active contexts. An command editor widget is provided to allow users to change their application wide shortcut settings, or to export and import previous sessions. The action provider concept is introduced where objects can provide actions to other objects or views and lastly, a clipboard manager is provided allowing management of the application clipboard. This article will explore these features and show how to include the functionality in your application. Table of contents: |
|
Since actions registered in the action manager are associated with specific contexts it makes sense to provide an overview of the context manager first.
A context represents a set of conditions under which actions must be enabled or disabled. Thus depending on your application's state you would like different actions to be enabled and others to be disabled, or a specific shortcuts must trigger different slots in your code depending on the application state.
Contexts can easily be registered and for each context an unique ID is retured, for example:
It is possible to access and control a context through either its name or id. At any given time one or more contexts can be active and you can change the active contexts at any time:
Qtilities provides the Qtilities::Core::Constants::qti_def_CONTEXT_STANDARD context which is always active. Thus if it is needed to use a context which will always be active, the standard context must be used.
The Qtilities::Core::Interfaces::IContext interface allows objects to indicate that they represent an context. When objects representing this interface are registered in the global object pool their context will automatically be added to the context manager.
The Qtilities Debug Plugin provides an overview of the context manager's state at any time and allows you to change active contexts in your application at runtime for debugging purposes.
For more information see the Qtilities::Core::Interfaces::IContextManager documentation.
Qtilities provides an action manager which greatly simplifies action and shortcut management in Qt applications. The architecture of the action manager was inspired by the action management architecture used in Qt Creator and has proven to work very well.
The action manager is used to manage action related items in applications using the Qtilities libraries. These items includes:
All items are referenced using a string ID through the action manager interface.
For the actions which are placed in menus appropriate menu bars and menus must first be created. The following example creates a simple menu bar with some default menus:
Managed menus and menu bars implements the Qtilities::CoreGui::ActionContainer base class and access functions for these items will return an action container reference. Menus can be accessed using the Qtilities::CoreGui::ActionContainer::menu() function and menu bars through the Qtilities::CoreGui::ActionContainer::menuBar() function. For menus the menu bar function will return 0, and for menu bars the menu function will return 0.
Actions and shortcuts are created in the normal way and then registered in the action manager. To start of, lets consider a simple action which must be enabled if a specific context (called "My Context" in this example) is active, and disabled otherwise.
All actions have a Command ID and an appropriate user visible string called an "User Text ID" associated with them. In the above example the Command ID is defined by MyActions.CollapseAll
and the User Text ID is defined by the text() function on actionCollapseAll
, thus "Collapse All". Actions and shortcuts implement the Qtilities::CoreGui::Command base class and will be called commands in the rest of this article. Actions can be accessed using the Qtilities::CoreGui::Command::action() function and shortcuts through the Qtilities::CoreGui::Command::shortcut() function. For shortcuts the action function will return 0, and for actions the shortcut function will return 0. Shortcuts are registered in the same way as actions as shown in the above example.
As mentioned already, actions and shortcuts are linked to contexts, thus they react differently when different contexts are active. This allows customization in two ways:
Lets look at the proxy action approach in more detail.
The Qtilities::CoreGui::ProxyAction class represents an proxy action which is triggered when the command's shortcut is triggered, and any number of back-end actions which each represent a different context. When context changes happen in the application, the proxy action will be updated to route shortcut triggers to the correct back-end action.
Proxy actions are easily created, for example:
In the above example backedAction1
will trigger when "Context 1" is active, and backendAction2
will trigger when "Context 2" is active. Note that no back-end actions will be triggered if neither of these contexts are active.
The first example presented where we regster a single action with a context (called "Collapse All" in that example), will create a proxy action and register the given action as a back-end action in it. Thus we can add more back-end actions for the "Collapse All" action by registering more actions for the same Command ID.
In some scenarions, especially pluginable applications, it is desirable to add action place holders in your application's menus in order to have control over the layout of menus. In this way, you can add place holders (which are implemented as proxy actions without any back-end actions) and plugins can register the back-end actions when they are loaded.
Place holders are easily registered like this:
It is also possible to create an action place holder which is not a proxy action. This is very usefull in cases where you don't want to store a reference to an action yourself. For example if you want to add an Exit
action to your File
menu, you can do it directly in your main()
function. The trick here is to specify a context for your place holder so that it will be enabled for that context:
Behind the scenes this will create a default back-end action for the specified context.
For more details on registering actions, see the Qtilities::CoreGui::Interfaces::IActionManager documentation.
The Qtilities::CoreGui::Actions namespace contains a set of ready to use action and action container Command IDs. Using the defined set of Command IDs makes it is easy to place new actions before or after them when adding actions to your menus. For more information see Qtilities::CoreGui::ActionContainer::addAction() and Qtilities::CoreGui::ActionContainer::addSeperator().
Qtilities has the ability to remember shortcut configurations between different application sessions. The Qtilities::CoreGui::Interfaces::IActionManager::loadShortcutMapping() and Qtilities::CoreGui::Interfaces::IActionManager::saveShortcutMapping() functions provide this functionality. See the Qtilities examples where these functions are used to save shortcuts configurations between sessions.
The Qtilities::CoreGui::CommandEditor class provides a shortcuts page which can be added to your application's configuration widget like this:
This widgets lists all the registered commands in the action manager where the "Command Name" column refers to the commands' string id and the "Label" column refers to the user text id of actions.
The figure below shows an example of the command editor, taken from the ObjectManagementExample
.
The command editor shows actions in a categorized tree view. A category is assigned to a command like this:
The command editor also allows users to edit shortcuts for the displayed commands and highlights ambigious shortcuts (used more than once) in red.
The Qtilities Debug Plugin provides an overview of the action manager's state at any time and allows you view detailed information about registered commands.
The goal of the clipboard manager is to register back-ends (associated with the standard context) for the Qtilities::CoreGui::Actions::qti_action_EDIT_COPY, Qtilities::CoreGui::Actions::qti_action_EDIT_CUT and Qtilities::CoreGui::Actions::qti_action_EDIT_PASTE action placeholders. This is done when the CLIPBOARD_MANAGER->initialize() function is called, allowing control over disabling and enabling these three actions in Qtilities applications. For example, the paste action should only be enabled if something exists in the the clipboard. Also, when you perform a paste operation, the paste action must become disabled again. The clipboard manager provides this functionality. See the Qtilities::CoreGui::Interfaces::IClipboard class description for more details.
Qtilities : Reference Documentation | Back to top |
Copyright © 2009-2013, Jaco Naudé
|