How to dynamically enable/disable menu actions on-demand

Andreas Pakulat apaku at gmx.de
Thu Apr 2 13:53:08 UTC 2009


On 02.04.09 13:03:33, David Nolden wrote:
> Anyone has an idea how to do that the best way?
> 
> The problem: When opening the "Navigation" menu, for example the "Jump to 
> Declaration" action is active, even though there is no declaration under the 
> cursor.
> 
> >From what I understand there is a system in KDE to put the UI into "states", 
> dynamically activating actions based on that.
> 
> However that is not flexible enough. I'm also facing this problem in some 
> other places: For example I want to add an "Open project for current file" 
> action to the project menu, and it should only be active if:
> - There is a file open
> - The file is not within a loaded project
> 
> So what's actually needed is this. A way of deciding on a per-action basis 
> whether the action should be activated/deactivated, while showing the menu.

The usual way to do this (apart from the state stuff above) is by
enabling/disabling the action whenever the state of the IDE changes, i.e.
if a file is opened check wether its part of any project. If not enable the
action, else disable it. Then also check for closing documents. All that in
the plugin where the action is provided.
 
> If there is no such mechanism yet, I'm proposing we use something like this:
> Create a subclass of KAction, called KDevAction, that has a signal 
> "showing(KAction*)", that should always be emitted when a menu containing the 
> action is shown, and that can be used by the action owners to check the 
> context, and enable/disable it.

I don't like that at all, simply because we'd most probably end up with 2
or three such classes as we'd need it not just for KAction but possibly
also some of its subclasses.

Andreas

-- 
Expect a letter from a friend who will ask a favor of you.




More information about the KDevelop-devel mailing list