The future of KAction

Alexander Dymo adymo at mksat.net
Tue Nov 15 17:53:52 GMT 2005


On Tuesday 15 November 2005 15:02, Simon Hausmann wrote:
> GuiEditor editor(this);
> editor.startMenuBar();
>   editor.startMenu("file", "&File");
>     editor.addAction(newDocumentAction);
>     editor.addAction(fileOpenAction);
>     editor.addSeparator();
>     editor.startMenu("printTo", "&Print To");
>       editor.addActions(KPrinter::printToActions());
>     editor.menuFinished();
>     editor.addSeparator();
>     editor.addAction(quitAction);
>   editor.menuFinished();
>    ...
>    editor.startMenu("settings", "&Settings");
>      ...
>      editor.addActionGroup("plugin settings");
>      ...
>    editor.menuFinished();
>    ...
> editor.menuFinished();
> editor.startToolBar();
>   editor.addAction(newDocumentAction);
>   editor.addSeparator();
>   editor.addWidget(searchComboBox);
> editor.show();

That's much better than xml IMHO.

> void Plugin::buildGui()
> {
>     GuiEditor editor(this);
>
>     editor.startMenuBar();
>     // .. in case the application doesn't want us to populate
>     // the settings menu or doesn't provide one in the first place
>     if (editor.tryToEditMenu("settings")) {
>         editor.addActionToGroup("plugin settings", mySettingsAction);

Why do we need to call ::tryToEditMenu()? Plugin should never care whether
the host application wants or doesn't want to add an action. It should
just call
	editor.addActionToGroup("plugin settings", mySettingsAction);
If you really want to know whether your action was added, you can
make ::addActionToGroup return bool.


The application's part looks as following IMO:

void MyWindow::setupUi() {
	GuiEditor editor(this);
	...
	editor.installActionFilter(this);	// not really necessary 
	//because we used GuiEditor::GuiEditor(this)
}

bool MyWindow::actionFilter(KAction *action, const QString &menu, 
	const QString &group)
{
	if (menu == "settings")
		return false;
	else if ((menu == "view") && (group == "navigation"))
	{
		if (action->objectName() == "go back")
			return false;
		return true;
	}
	return true;
}




More information about the kde-core-devel mailing list