[Developers] Changed menu-handling in OD
Kristofer Tingdahl
kristofer.tingdahl at dgb-group.com
Tue Jul 12 16:48:20 CEST 2005
Dear co-developers,
In order to make OpendTect more open to it's plugins, the way the menus
in the tree are handled is changed.
The difference is not very large, and it follows the same concept. The
code does however much better.
The basic idea is that each object in the scene can have a menu. That
menu is accessed via
MenuHandler* menu =
ODMainWin()->applMgr().visServer()->getMenu( id, true );
Once you have the menu, you can hook up to it's notifier's:
menu->createnotifier.notify( mCB( this, myclass, createMenuCB ));
menu->handlenotifier.notify( mCB( this, myclass, handleMenuCB ));
and myclass::createMenu(CallBacker*) will be called every time the menu
is about to be displayed, and myclass::handleMenuCB(CallBacker*) will be
called every time the user has clicked on one of the items in the menu.
The createMenu should be implemented like this:
void myclass::createMenuCB( CallBacker* callback )
{
uiMenuHandler* menu = dynamic_cast<uiMenuHandler*>(callback);
mAddMenuItem( menu, &mymenuitem, true, false );
mAddMenuItem( menu, &mysubmenu, true, false );
mAddMenuItem( &mysubmenu, &mysubmenuitem1, true, false );
mAddMenuItem( &mysubmenu, &mysubmenuitem2, true, false );
}
The code will make a menu with two items, and the second item will have
a submenu with two items. The first boolean says whether the item should
be enabled, the second one says where there should be a check before it.
The menuitems are instantiations of MenuItem and should be stored in
your class. They hold information about the item itself (like text,
enabled or not enabled, checked or not checked, information on where in
the menu it should be placed. In addition, it has an unique id that is
set when the item is inserted into the menu.
When the user has clicked an item, the handleMenuCB will be called,
where you have to check if any of 'your' items where clicked by
comparing the id of the clicked item with the id:s of your items. If one
of your items where clicked, it is important to set the isHandled flag
on the menu.
void myclass::handleMenuCB(CallBacker* callback )
{
mCBCapsuleUnpackWithCaller( int, mnuid, caller, callback );
uiMenuHandler* menu = dynamic_cast<uiMenuHandler*>(caller);
if ( mnuid==-1 || menu->isHandled() )
return;
if ( mnuid==mymenuitem.id )
{
menu->setIsHandled(true);
do_something();
}
else if ( mnuid==mysubmenuitem1.id )
{
menu->setIsHandled(true);
do_something_else();
}
else if ( mnuid==mymenusubitem2.id )
{
menu->setIsHandled(true);
do_something_else();
}
}
The new menu-system for the tree is available through CVS
(http://lists.opendtect.org/pipermail/developers/2005-February/000015.html) and in the next official release of Open dTect.
Happy Programming,
Kristofer Tingdahl
--
-- dGB Earth Sciences USA
-- One Sugar Creek Center Boulevard, Sugar Land, TX, 77478, USA
-- http://www.dgb-group.com
-- Tel: +1 281 625 5318 , Fax: +1 281 240 3944
More information about the Developers
mailing list