Action system refactor

Sven Langkamp sven.langkamp at gmail.com
Wed Feb 13 20:41:28 UTC 2013


Hi,


I recently got a bit fed up with the way in which we handle action
enable/disable. Currently every place that adds new actions needs to
implement a updateGUI method and update every action manually. In
several place this updating logic is broke because it doesn't cover
all the cases. I started a refactoring to clear this situation.

The basic idea is as following: There is a central class which updates
the action (KisActionManager), every KisAction (inherits from KAction
and is a drop in replacement) has flags to control activation and
condition that need to be met.

Exampe:
    m_fillForegroundColor  = new KisAction(i18n("Fill with Foreground
Color"), this);
    m_fillForegroundColor->setActivationFlags(KisAction::ACTIVE_DEVICE);
    m_fillForegroundColor->setActivationConditions(KisAction::ACTIVE_NODE_EDITABLE);
    actionManager->addAction("fill_selection_foreground_color",
m_fillForegroundColor, collection);

In this case the action would be enabled if there is an active device
and the active node is editable. There can ActivationFlags be multiple
flags/conditions by combing them like option1 | option2. The action
will only be active if one of the flags and all of the conditions are
active.

Currently I have identified these:
    enum ActivationFlag {
        NONE = 0,
        ACTIVE_NODE = 1,
        ACTIVE_DEVICE = 2,
        ACTIVE_LAYER = 4,
        ACTIVE_SHAPE_LAYER = 8,
        PIXELS_SELECTED = 16,
        SHAPES_SELECTED = 32,
        PIXEL_SELECTION_WITH_PIXELS = 64,
        PIXELS_IN_CLIPBOARD = 128,
        SHAPES_IN_CLIPBOARD = 256
    };

    enum ActivationCondition {
        NO_CONDITION = 0,
        ACTIVE_NODE_EDITABLE = 1,
        SELECTION_EDITABLE = 2
    };

The advantage of the system is that for many actions no explicit
update is needed, it's easier to see what the action does and it's
consistent. One downside is that not every action is covered by this
e.g. some action need special cases so some of the updateGUI code will
remain.

Comments?


Sven


More information about the kimageshop mailing list