This example shows how to create and modify a toolbar from both new and existing menu items and GUI elements.
The code fragments are from exampleToolbarPluginInit.cxx code.
- Register the menu that is to be used in the plugin. While the order is not strictly important, it represents how the elements are created in the system (menus first, then toolbars):
DtQtMenuAssembler::instance(de).registerMenu(new DtExampleToolbarMenu(de));
DtQtMenuAssembler::instance(de).registerMenu(new DtExampleToolbarMenuItem(de));
- Register the toolbars that are going to be created with the DtQtToolbarAssembler registerToolbar method:
DtQtToolbarAssembler::instance(de).registerToolbar(new DtExampleToolbar(de));
- The toolbar assembler interface allows you to add menu items, menus, separators and GUI elements (widgets) to the toolbar. To add a menu item and separator:
DtToolbarPath::toolbar("DtExampleToolbar").item("DtWireframeItem"));
DtToolbarPath::toolbar("DtExampleToolbar").item("DtShadowsItem"));
DtToolbarPath::toolbar("DtExampleToolbar").separator("DtSeparator_1"));
DtToolbarPath::toolbar("DtExampleToolbar").item("DtExampleToolbarMenuItem"));
- To add a menu:
DtToolbarPath::toolbar("DtExampleToolbar").separator("DtSeparator_2"));
DtToolbarPath::toolbar("DtExampleToolbar").mainMenu("DtFileMenu").menu("DtOpenMenu"));
- You can also create menus that only appear as part of the toolbar and not on the main menu. To do this, the menus and menu items must be registered with the DtQtMenuAssembler class. However, the menu path definitions are registered with the toolbar configuration:
DtQtMenuAssembler::instance(de).registerMenu(new DtExampleSceneToolbarMenu(de));
DtMenuPath::mainMenu("DtExampleSceneToolbarMenu"));
DtMenuPath::mainMenu("DtExampleSceneToolbarMenu").item("DtNewSceneItem"));
DtMenuPath::mainMenu("DtExampleSceneToolbarMenu").item("DtOpenSceneItem"));
- And reference this new menu the same way as one created in the menu system:
DtToolbarPath::toolbar("DtExampleToolbar").separator("DtSeparator_3"));
DtToolbarPath::toolbar("DtExampleToolbar").mainMenu("DtExampleSceneToolbarMenu"));
- To add a widget, the widget creator, a subclass of DtWidgetBuilder, must be register with the DtQtToolbarAssember registerWidgetBuilder method. It can then be referenced by the widget call on the toolbar path:
DtQtToolbarAssembler::instance(de).registerWidgetBuilder("DtToolbarExampleCombo",
new DtToolbarExampleCombo);
DtToolbarPath::toolbar("DtExampleToolbar").separator("DtSeparator_4"));
DtToolbarPath::toolbar("DtExampleToolbar").widget("DtToolbarExampleCombo"));
Building the Example
VR-Vantage includes pre-built versions of the example plug-in. To build it yourself, follow the instructions at Building VR-Vantage Examples, Applications, and Plug-ins.
Running the Example
This example is a plug-in. You can run it by running ./bin/exampleToolbar_stealth.bat (on Windows) or ./bin/exampleToolbar_stealth.sh (on Linux). For more information about running examples, please see Running Applications and Examples.
Example Source Files
exampleToolbarPluginInit.cxx
{
return true;
}
namespace makVrv
{
namespace vrvExampleToolbarPlugin
{
{
try
{
if(igApp)
{
new DtToolbarExampleCombo);
}
}
DtCATCH_AND_PROPAGATE(DtCorruptedState)
}
}
}
exampleToolbarMenu.h
#pragma once
namespace makVrv
{
class DtDe;
namespace vrvExampleToolbarPlugin
{
{
public:
DtExampleToolbar(DtDe&);
virtual ~DtExampleToolbar();
};
{
public:
DtExampleToolbarMenu(DtDe& de);
virtual ~DtExampleToolbarMenu();
};
{
public:
DtExampleSceneToolbarMenu(DtDe& de);
virtual ~DtExampleSceneToolbarMenu();
};
{
public:
virtual ~DtExampleToolbarMenuItem();
virtual void on_triggered();
};
{
public:
DtToolbarExampleCombo();
virtual ~DtToolbarExampleCombo();
};
}
}
exampleToolbarMenu.cxx
#include <QtWidgets/QToolBar>
#include <QtWidgets/QAction>
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QMenu>
#include <QtWidgets/QComboBox>
namespace makVrv
{
namespace vrvExampleToolbarPlugin
{
: DtToolbar(de, "DtExampleToolbar")
{
}
DtExampleToolbar::~DtExampleToolbar()
{
}
{
bar->setWindowTitle(QObject::tr("Example Toolbar"));
bar->setObjectName(QObject::tr("Example Toolbar"));
return bar;
}
DtExampleToolbarMenu::DtExampleToolbarMenu(DtDe& de)
: DtMenu(de, std::
string(
"DtExampleToolbarMenu"))
{
}
DtExampleToolbarMenu::~DtExampleToolbarMenu()
{
}
QMenu* DtExampleToolbarMenu::createMenu( DtDe&,
QWidget* parent )
{
QMenu* menu =
new QMenu(QMenu::tr(
"&Toolbars Example"),parent);
return menu;
}
DtExampleSceneToolbarMenu::DtExampleSceneToolbarMenu(DtDe& de)
: DtMenu(de, std::
string(
"DtExampleSceneToolbarMenu"))
{
}
DtExampleSceneToolbarMenu::~DtExampleSceneToolbarMenu()
{
}
QMenu* DtExampleSceneToolbarMenu::createMenu( DtDe&,
QWidget* parent )
{
QMenu* menu =
new QMenu(QMenu::tr(
"&Scene Toolbars Example"),parent);
menu->setIcon(QIcon(QString::fromStdString(myDe.dePathConfiguration().dataPath() +
std::string(
"/Icons/TerrainScene.svg"))));
return menu;
}
DtExampleToolbarMenuItem::DtExampleToolbarMenuItem(DtDe& de)
: DtMenuItem(std::
string(
"DtExampleToolbarMenuItem"))
{
}
DtExampleToolbarMenuItem::~DtExampleToolbarMenuItem()
{
}
QAction* DtExampleToolbarMenuItem::createAction( DtDe&,
QWidget* parent )
{
act->setText(DtMenuItem::tr("&Example Toolbar Item..."));
return act;
}
void DtExampleToolbarMenuItem::on_triggered()
{
if(action)
{
parent = action->parentWidget();
}
QMessageBox::information(parent, "Toolbar Example", "This dialog was triggered from the toolbar example menu item");
}
DtToolbarExampleCombo::DtToolbarExampleCombo()
: DtWidgetBuilder()
{
}
DtToolbarExampleCombo::~DtToolbarExampleCombo()
{
}
{
combo->addItem("Item 1");
combo->addItem("Item 2");
combo->addItem("Item 3");
return combo;
}
}
}