On Fri, Sep 15, 2006 at 09:33:12AM +0000, Angus Leeming wrote:
> Jean-Marc Lasgouttes <[EMAIL PROTECTED]> writes:
> > For command sequences, as a heuristic, the sequence is declared
> > enabled if the first action is enabled. This is not correct, but I do
> > not have a better idea.
>
> Why don't you loop over all commands in the sequence and establish if any
> actions are disabled? There are no if-blocks to complicate the logic.
That's basically what I am usually doing btw.
Rough outline is (pseudocode)
enum ActionId {
actFirst,
actFileOpen,
actFileClose,
...
actLast
};
struct Action {
bool enabled;
bool checked;
...
};
map<ActionId, QAction *> theActions; // in a singleton with
//action registered on startup or dynamicall later
typedef std::vector<Action> Actions;
void MainWindow::updateUI()
{
Actions acts;
// set default values
emit sigUpdateUI(acts);
for (int i = actFirst + 1; i < actLast; ++i)
theActions[ActionId(i)]->setEnabled(acts[i].enabled);
theActions[ActionId(i)]->setChecked(acts[i].checked);
}
}
And in views
MyView::MyView()
{
Connect(theMainWindow(), "sigUpdateUI(Actions &)",
this, "onUpdateUI(Actions &)
}
void MyView::onUpdateUI(Actions & acts)
{
acts[actFileNew].enabled = true; // I'll handle that
acts[actFileOpen].enabled = some_expression; // I'll handle that
// under certain circumstances
}
etc.
Advantage is just a single signal/slot call per 'active' item
(i.e. usally a view or two and some spcialized tools) instead
of individual calls for each action (Qt signal/slot does not
scale too well beyond a few hundred connections) and pretty
compact enabling/disabling code (usually just a single line per
_handled_ action and no code for not-handled ones)
Andre'