Building custom applications with C++

One can build custom applications with HippoDraw's C++ class library along with Qt.

Custom application with canvas

To build an application with the CanvasWindow and Inspector, it can be as simple as the one shown below...

#include "QtApp.h"
int main ( int argc, char** argv)
{
QtApp app ( argc, argv );
app.setFirstWindow();
return app.exec();
}

Your custom code, say to generate the data source data should be inserted before the call to app.exec() as that function starts the Qt event loop and doesn't return until the application is terminated.

If you already have a Qt application, then you can add the HippoDraw CanvasWindow and Inspector to your application by adding code to your main with something like the implementation of QtApp::setFirstWindow does. The result might look like this ...

#include "qt/CanvasWindow.h"
#include "qapplication.h"
int main ( int argc, char** argv)
{
MyApp app ( argc, argv );
CanvasWindow * window = new CanvasWindow ();
window->show();
return app.exec();
}

One doesn't need to create the CanvasWindow before starting the Qt event loop. It can be created at any time.

Custom application with widget in window

This section describes how to put a HippoDraw plot in a single window.

First the QApplication object is created and the NTupleController is used to create an NTuple by reading a file as shown here.

int main ( int argc, char **argv )
{
QApplication app ( argc, argv );
const string filename ( "../../../hippodraw/examples/aptuple.tnt" );
NTupleController * nt_controller = NTupleController::instance ();
NTuple * nt
= nt_controller->createNTuple ( filename );

In a custom application, you will probably have other ways of creating the NTuple. If you want the NTuple visible to the Inspector, then you must register it with the NTupleController like this ...

NTuple * nt = // create your NTuple somehow
NTupleController * nt_controller = NTupleController::instance ();
nt_controller->registerNTuple ( nt );

Next a display is created bound to the NTuple and one of its columns.

const string histo ( "Histogram" );
vector < string > bindings;
bindings.push_back ( "Cost" );
DisplayController * dc_controller = DisplayController::instance ();
PlotterBase * plotter
= dc_controller->createDisplay ( histo, *nt, bindings );

Note that DisplayController creates the appropriate class derived from PlotterBase for the kind of display you requested.

Plotter objects are used both for canvas items and widgets. In this case, a QtViewWidget is created and the plotter is attached to it.

QtViewWidget * view = new QtViewWidget ( );
view->setPlotter ( plotter );

Finally, the view it set into the Qt main window, resized, captioned, and the event loop started.

view->resize ( 200, 200 );
app.setMainWidget( view );
view->setCaption ( "Qt HippoDraw - View widget" );
view->show();
int result = a.exec();

Note that the above code used methods that QtViewWidget inherits from Qt's QWidget class.

Don't forget to clean up when you are done.

delete view;
delete nt;
return result;
}

The resulting application window looks like this ...

widget_window.png
QtViewWidget set as application's main window.

The complete code is shown below ...

1 
7 #include "qt/QtViewWidget.h"
8 
9 #include "controllers/DisplayController.h"
10 #include "datasrcs/NTupleController.h"
11 #include "datasrcs/NTuple.h"
12 #include "plotters/PlotterBase.h"
13 
14 #include <qapplication.h>
15 
16 #include <string>
17 #include <vector>
18 
19 using std::string;
20 using std::vector;
21 
22 
30 using namespace hippodraw;
31 
32 int main ( int argc, char **argv )
33 {
34  QApplication a( argc, argv );
35 
36  const string filename ( "../../../hippodraw/examples/aptuple.tnt" );
37  NTupleController * nt_controller = NTupleController::instance ();
38  DataSource * nt
39  = nt_controller->createNTuple ( filename );
40 
41  const string histo ( "Histogram" );
42  vector < string > bindings;
43  bindings.push_back ( "Cost" );
44 
45  DisplayController * dc_controller = DisplayController::instance ();
46  PlotterBase * plotter
47  = dc_controller->createDisplay ( histo, *nt, bindings );
48 
49  QtViewWidget * view = new QtViewWidget ( );
50  view->setPlotter ( plotter );
51 
52  view->resize ( 200, 200 );
53  a.setMainWidget( view );
54  view->setCaption ( "Qt HippoDraw - View widget" );
55  view->show();
56 
57  int result = a.exec();
58 
59  delete view;
60  delete nt;
61 
62  return result;
63 }

Custom application with custom widget in Qt Designer

One can use HippoDraw's QtViewWidget as a custom widget within Qt Designer. When doing so, your main program may look like this

#include <qapplication.h>
#include "QtViewWidgetWindow.h"
int main( int argc, char ** argv )
{
QApplication app ( argc, argv );
QtViewWidgetWindow w;
w.show();
app.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return app.exec();
}

In this example, the class QtViewWidgetWindow was built with Qt Designer. It was created by asking for a new main window. QtViewWidget was inserted in Qt Designer as a custom widget. The results look like this ...

custom_widget_designer.png
QtViewWidget as custom widget

In this example, the fileOpen() slot as implemented like the implementation in the Custom application with canvas example. But in addition, the implementation creates an Inspector and signals it to update itself with the created plotter. The code look like this ...

m_inspector = new Inspector ();
m_inspector->show();
QCustomEvent * event = new QCustomEvent ( QEvent::User, plotter );
QApplication::postEvent ( m_inspector, event );

One might have expected a direct call to Inspector::update following the Observer pattern instead of this implementation. However, it was found that under the Windows operating system, such a direct call caused problems with the OS's threading model. So the QCustomEvent is used to avoid the problem.

Some part of the application may cause a change to the plotter. When that happens, one needs to update the Inspector. Following the Observer pattern, when something in the plotter changes, it sends an update message to its observer which is the QtViewWidget. It in turn, needs to send a message to its parent which is the QtViewWidgetWindow object. It uses a QCustomEvent to do this, so the implementation of the QtViewWidgetWindow must implement it something like this ...

void QtViewWidgetWindow::customEvent ( QCustomEvent * event )
{
void * data = event->data();
QCustomEvent * e = new QCustomEvent ( QEvent::User, data );
QApplication::postEvent ( m_inspector, e );
}

After opening the file, the window looks like this ...

custom_window.png
QtViewWidgetWindow after opening file

The complete source code is show below ...

1 /* -*- mode: c++ -*- */
2 /****************************************************************************
3 ** ui.h extension file, included from the uic-generated form implementation.
4 **
5 ** If you wish to add, delete or rename functions or slots use
6 ** Qt Designer which will update this file, preserving your code. Create an
7 ** init() function in place of a constructor, and a destroy() function in
8 ** place of a destructor.
9 *****************************************************************************/
10 
11 using std::string;
12 using std::vector;
13 
14 using namespace hippodraw;
15 
18 void QtViewWidgetWindow::init()
19 {
20 
21 }
22 
23 void QtViewWidgetWindow::fileOpen()
24 {
25 #if QT_VERSION < 0x040000 // 3.1.0
26  QString file_name
27  = QFileDialog::getOpenFileName ( QString::null, // starting directory
28  "ntuple (*.*)", // filter
29  this, // parent
30  "open ntuple file", // internal name
31  "Open ntuple" ); // actual window caption
32 #else
33  QString file_name
34  = Q3FileDialog::getOpenFileName ( QString::null, // starting directory
35  "ntuple (*.*)", // filter
36  this, // parent
37  "open ntuple file", // internal name
38  "Open ntuple" ); // actual window caption
39 #endif
40  if ( file_name.isEmpty () ) return;
41 
42 #if QT_VERSION < 0x030100 // 3.1.0
43  const string filename ( file_name );
44 #else
45  const string filename( file_name.latin1() );
46 #endif
47  NTupleController * nt_controller = NTupleController::instance ();
48  DataSource * nt
49  = nt_controller->createNTuple ( filename );
50 
51  const string histo ( "Histogram" );
52  vector < string > bindings;
53  bindings.push_back ( "Cost" );
54 
55  DisplayController * dc_controller = DisplayController::instance ();
56  PlotterBase * plotter
57  = dc_controller->createDisplay ( histo, *nt, bindings );
58 
59  m_view->setPlotter ( plotter );
60 
61  m_inspector = new Inspector ();
62  m_view -> setInspector ( m_inspector );
63 
64  m_inspector->show();
65  m_view->update ( plotter );
66  PlotterEvent * event = new PlotterEvent ( plotter );
67  QApplication::postEvent ( m_inspector, event );
68 }
69 
70 void QtViewWidgetWindow::filePrint()
71 {
72 
73 }
74 
75 void QtViewWidgetWindow::fileExit()
76 {
77 qApp->quit();
78 }
79 
80 void QtViewWidgetWindow::helpAbout()
81 {
82 
83 }

Generated for HippoDraw by doxygen