VR-Vantage 1.4.1 API Class Documentation
exampleEmbeddedSlave

This example shows how to create your own QT application and embed a VR-Vantage slave display engine in it.

Use any master VR-Vantage application (VR-Vantage XR, VR-Vantage Stealth or VR-Vantage IG for example) to control the visuals embedded in this application.

Create and show the main application:

   QApplication app(argc, argv);
   ExampleEmbeddedSlaveMainWindow mainWindow; 
   mainWindow.showMaximized();

Run the application:

   return app.exec();

The example application has a GUI derived from QMainWindow. This is a very common way of building QT applications.

The main window class owns some key VR-Vantage objects. It owns a DtDe (display engine), which is the core component for adding visualization to any application. It also owns a DtPluginManager, which is responsible for loading all the VR-Vantage plug-ins.

The OpenGL graphics context needs to be owned and managed by the QT main window. We connect it the VR-Vantage display engine (and consequently OSG) via the DtOsgAdapterQtWidget.

In addition, main window owns some QT widgets for menus and toolbars.

The real work begins in the constructor of the main window.

First, create the display engine, prepare it to be put into slave mode, create a plug-in manager, and then initialize load the plug-ins.

void ExampleEmbeddedSlaveMainWindow::createDisplayEngine()
{
   DtDeInitializer init;

   // Configure DtDeInitializer
   init.setDeMode(DtDeInitializer::SlaveMode);

   myDe = new DtDe();
   myDe->setDeInitializer(init);

   myPluginManager = new DtPluginManager(*myDe);
   myPluginManager->initializePlugins();
}

Now we create the GUI. The interesting thing here is the DtQtIgTickerProvider. This provides us with a "ticker" that ticks the display engine as fast as the QT GUI will go. The rest of the function just sets up menus, toolbars, and the other things that would be part of this application.

void ExampleEmbeddedSlaveMainWindow::buildGui()
{ 
   // Create an IG ticker to tick during Qt's idle time.
   DtQtDeTickerProvider& tickerProvider = DtQtDeTickerProvider::instance(*myDe);
   tickerProvider.ticker(this);

   setWindowTitle(tr("Embedded Slave Example"));
   setWindowIcon(QIcon(":/images/Mak-de.png"));

   createActions();
   createMenus();
   createToolBars();
   createStatusBar();
}

During the call to preDisplayEngineInitialize(), we override the functionality set up by plug-ins. We want to create to use a DtOsgAdapterQtWidget to own the DtEmbeddedOsgWindow and we do that by installing a new creator with the DtWindowFactory.

   // Set the display window creator function to create use the Adapter widget.
   DtWindowFactory::instance(*myDe).setCreator(
      DtWindowConfiguration::EmbeddedWindow,
      new ExampleEmbeddedSlaveMainWindowCreator(this));

We have to tell our application framework not to try and create a QApplication, which already exists. We also have to tell it not to create a main window, since that already exists too.

   // Register the qApp with the DtQApplicationProvider.
   DtQApplicationProvider::instance(*myDe).setQAppInstance(qApp);

   // Register null with the DtIgMainWindowProvider so that no main window
   // is ever created.  This DtDe instance is embedded in a QMainWindow already.
   DtDeMainWindowProvider::registerInstance(*myDe, NULL);

Finally, everything is all set up and we can initialize the display engine:

Building the Example

VR-Vantage includes pre-built versions of the example application. To build it yourself, follow the instructions at Building VR-Vantage Examples, Applications, and Plug-ins.

Running the Example

This example is an application. You can run it by running ./bin/exampleEmbeddedSlave.exe (on Windows) or ./bin/exampleEmbeddedSlave (on Linux). For more information about running examples, please see Running Applications and Examples.

Example Source Files


exampleEmbeddedSlaveMain.cxx

/****************************************************************************** 
** Copyright (c) 2008 MAK Technologies, Inc. 
** All rights reserved. 
******************************************************************************/ 
/****************************************************************************** 
** $RCSfile: exampleEmbeddedSlaveMain.cxx,v $ $Revision: 1.3 $ $State: Exp $ 
******************************************************************************/ 

// Include this header before any Qt header.
#include <vrvUtil/signalslib.h>
#include <QtGui/QApplication>
#include "exampleEmbeddedSlaveMainWindow.h"

int main(int argc, char *argv[])
{
   //
   // This example illustrates how to embed a slave display engine into a preexisting
   // application.   
   //
   QApplication app(argc, argv);
   ExampleEmbeddedSlaveMainWindow mainWindow; 
   mainWindow.showMaximized();

   //
   // Notice how the preexisting applications event loop wasn't changed.
   //
   return app.exec();
} 

exampleEmbeddedSlaveMainWindow.h

/****************************************************************************** 
** Copyright (c) 2008 MAK Technologies, Inc. 
** All rights reserved. 
******************************************************************************/ 
/****************************************************************************** 
** $RCSfile: exampleEmbeddedSlaveMainWindow.h,v $ $Revision: 1.4 $ $State: Exp $ 
******************************************************************************/ 

#ifndef exampleEmbeddedSlaveMainWindow_H
#define exampleEmbeddedSlaveMainWindow_H

// Include this header before any Qt header.
#include <vrvUtil/signalslib.h>

#include <vrvCore/DtDe.h>
#include <vrvCore/DtVrvApplication.h> 
#include <vrvCore/DtPluginManager.h>
#include <vrvOsgQt/DtOsgAdapterQtWidget.h>
#include <vrvOsgQt/DtSceneGraphTreeWidget.h>
#include <vrvCore/DtWindow.h> 
#include <vrvUtil/DtWindowConfiguration.h> 

#include <QtGui/QMainWindow>
#include <osg/Group>
#include <osg/Node>

class QAction;
class QMenu;
class QTreeWidget;
class QTreeWidgetItem;


class ExampleEmbeddedSlaveMainWindow : public QMainWindow
{

   Q_OBJECT

public:

   ExampleEmbeddedSlaveMainWindow();

   virtual ~ExampleEmbeddedSlaveMainWindow();


   // The adapter widgets. These will become owned and 
   // deleted by MainWindow.
   makVrv::DtOsgAdapterQtWidget* myMainOSGWidget;
   
public slots:

protected:

   void createDisplayEngine();
   void buildGui();
   void preDisplayEngineInitialize();         
   void initializeDisplayEngine();
   void postDisplayEngineInitialize();

   virtual void closeEvent(QCloseEvent *event);

   void createActions();
   void createMenus();
   void createToolBars();
   void createStatusBar();

protected:

   makVrv::DtDe* myDe;
   makVrv::DtPluginManager* myPluginManager;

   QMenu* myFileMenu;
   QToolBar* myFileToolBar;
   QAction* myExitAction;
};

class ExampleEmbeddedSlaveMainWindowCreator : public makVrv::DtWindowCreator
{
public:
   ExampleEmbeddedSlaveMainWindowCreator(ExampleEmbeddedSlaveMainWindow* window)
   {
      myWindow = window;
   }

   virtual makVrv::DtWindow* create(makVrv::DtDe&, makVrv::DtWindowConfiguration&);

protected:
   ExampleEmbeddedSlaveMainWindow* myWindow;   
};

#endif 

exampleEmbeddedSlaveMainWindow.cxx

/****************************************************************************** 
** Copyright (c) 2008 MAK Technologies, Inc. 
** All rights reserved. 
******************************************************************************/ 
/****************************************************************************** 
** $RCSfile: exampleEmbeddedSlaveMainWindow.cxx,v $ $Revision: 1.13 $ $State: Exp $ 
******************************************************************************/ 

// Include this header before any Qt header.
#include <vrvUtil/signalslib.h>

#include "exampleEmbeddedSlaveMainWindow.h"
#include <vrvCore/DtEventLoop.h>
#include <vrvOsgQt/DtEmbeddedOsgWindow.h>
#include <vrvCoreQt/DtQApplicationProvider.h>
#include <vrvCoreQt/DtQtDeMainWindow.h>
#include <vrvCoreQt/DtQtDeTicker.h> 
#include <vrvOsgQt/DtSceneGraphTreeWidget.h>
#include <vrvCore/DtWindowFactory.h>
#include <vrvOsg/vrvOsg.h>
#include <vrvOsgQt/vrvOsgQt.h>
#include <vrvCore/DtSceneDriver.h>
#include <vrvCoreQt/DtStaticFileDialog.h>
#include <vrvCore/DtModelDefinition.h>
#include <vrvCore/DtDeSharedState.h>
#include <vrvCore/DtWindowManager.h>
#include <vrvCore/DtScene.h>
#include <vrvCore/DtTerrain.h>
#include <vrvCore/DtTerrainPatch.h>

#include <QtGui/QApplication>
#include <QtGui/QtGui>
#include <osg/Group>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>

using namespace makVrv;

ExampleEmbeddedSlaveMainWindow::ExampleEmbeddedSlaveMainWindow() 
: QMainWindow()
, myPluginManager(0)
, myDe(0)
, myMainOSGWidget(0)
{  
   createDisplayEngine();
   buildGui();   
   preDisplayEngineInitialize();
   initializeDisplayEngine();
   postDisplayEngineInitialize();
}

ExampleEmbeddedSlaveMainWindow::~ExampleEmbeddedSlaveMainWindow()
{
   if (myMainOSGWidget)
   {
      delete myMainOSGWidget;
      myMainOSGWidget = 0;
   }

   delete myDe;   
   myDe = 0;

   delete myPluginManager;
   myPluginManager = 0;
}

void ExampleEmbeddedSlaveMainWindow::createDisplayEngine()
{
   DtDeInitializer init;

   // Configure DtDeInitializer
   init.setDeMode(DtDeInitializer::SlaveMode);

   myDe = new DtDe();
   myDe->setDeInitializer(init);

   myPluginManager = new DtPluginManager(*myDe);

#ifndef _WIN32
   // DI-Guy links in Qt 3, which is incompatible with Qt 4 based applications.
   // The following code will exclude the DI-Guy plugin for Linux:
   std::list<std::string> excludePlugins;
   excludePlugins.push_back("../plugins/release/libvrvDiGuyPlugin.so");
   myPluginManager->setExcludedPluginFiles(excludePlugins);
#endif

   myPluginManager->initializePlugins();
}

void ExampleEmbeddedSlaveMainWindow::buildGui()
{ 
   // Create an IG ticker to tick during Qt's idle time.
   DtQtDeTickerProvider& tickerProvider = DtQtDeTickerProvider::instance(*myDe);
   tickerProvider.ticker(this);

   setWindowTitle(tr("Embedded Slave Example"));
   setWindowIcon(QIcon(":/images/Mak-de.png"));

   createActions();
   createMenus();
   createToolBars();
   createStatusBar();
}

void ExampleEmbeddedSlaveMainWindow::preDisplayEngineInitialize()
{
   // Override Display Engine Functionality

   // Set the display window creator function to create use the Adapter widget.
   DtWindowFactory::instance(*myDe).setCreator(
      DtWindowConfiguration::EmbeddedWindow,
      new ExampleEmbeddedSlaveMainWindowCreator(this));

   vrvCoreQt::init(*myDe);
   vrvOsg::init(*myDe);

   // Register the qApp with the DtQApplicationProvider.
   DtQApplicationProvider::instance(*myDe).setQAppInstance(qApp);

   // Register null with the DtIgMainWindowProvider so that no main window
   // is ever created.  This DtDe instance is embedded in a QMainWindow already.
   DtDeMainWindowProvider::registerInstance(*myDe, NULL);
}

void ExampleEmbeddedSlaveMainWindow::initializeDisplayEngine()
{
   myDe->initialize();
}

void ExampleEmbeddedSlaveMainWindow::postDisplayEngineInitialize()
{
}

void ExampleEmbeddedSlaveMainWindow::closeEvent(QCloseEvent *event)
{
   event->accept();
}


makVrv::DtWindow* ExampleEmbeddedSlaveMainWindowCreator::create( makVrv::DtDe& de, makVrv::DtWindowConfiguration& wc)
{
   DtEmbeddedOsgWindow* pWindow = 0;
   DtOsgAdapterQtWidget* pAdapter = 0;

   osg::GraphicsContext* sharedContext = 0;
   if(de.display())
   {
      const DtWindowManager::WindowList& windows = de.display()->windowManager().windows();
      if(windows.size() > 0)
      {
         DtOsgWindow* osgView = dynamic_cast<DtOsgWindow*>(windows.front());
         if(osgView)
         {
            sharedContext = osgView->graphicsContext();
         }
      }
   }

   osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits();
   traits->doubleBuffer = true;
   traits->sharedContext = sharedContext;


   if(wc.name() == "Window 1")
   {
      // Set the main OSG widget as the central widget.
      myWindow->myMainOSGWidget = new DtOsgAdapterQtWidget( de, traits.get(),DtOsgAdapterQtWidget::createFormatFromTraits(traits.get()),myWindow);   
      myWindow->setCentralWidget(myWindow->myMainOSGWidget);   
      pAdapter = myWindow->myMainOSGWidget;
   }

   if(pAdapter)
   {
      pWindow = new DtEmbeddedOsgWindow(de, wc, pAdapter);
      if(pWindow)
      {
         pAdapter->setView(pWindow);
      }
   }
 
   return pWindow;
}


void ExampleEmbeddedSlaveMainWindow::createActions()
{
   myExitAction = new QAction(QIcon(":/images/exit.png"),tr("E&xit"), this);
   myExitAction->setStatusTip(tr("Exit the application"));
   connect(myExitAction, SIGNAL(triggered()), this, SLOT(close()));
}

void ExampleEmbeddedSlaveMainWindow::createMenus()
{
   myFileMenu = this->menuBar()->addMenu(tr("&File"));
   myFileMenu->addAction(myExitAction);
}

void ExampleEmbeddedSlaveMainWindow::createToolBars()
{
   myFileToolBar = this->addToolBar(tr("File"));
}

void ExampleEmbeddedSlaveMainWindow::createStatusBar()
{
   this->statusBar()->showMessage(tr("Ready"));
}



Copyright © 2005-2012 VT MÄK Inc. All Rights Reserved (www.mak.com)