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 =
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 ...
QtViewWidget set as application's main window.
The complete code is shown below ...
7 #include "qt/QtViewWidget.h" 9 #include "controllers/DisplayController.h" 10 #include "datasrcs/NTupleController.h" 11 #include "datasrcs/NTuple.h" 12 #include "plotters/PlotterBase.h" 14 #include <qapplication.h> 30 using namespace hippodraw;
32 int main (
int argc,
char **argv )
34 QApplication a( argc, argv );
36 const string filename (
"../../../hippodraw/examples/aptuple.tnt" );
37 NTupleController * nt_controller = NTupleController::instance ();
39 = nt_controller->createNTuple ( filename );
41 const string histo (
"Histogram" );
42 vector < string > bindings;
43 bindings.push_back (
"Cost" );
45 DisplayController * dc_controller = DisplayController::instance ();
47 = dc_controller->createDisplay ( histo, *nt, bindings );
49 QtViewWidget * view =
new QtViewWidget ( );
50 view->setPlotter ( plotter );
52 view->resize ( 200, 200 );
53 a.setMainWidget( view );
54 view->setCaption (
"Qt HippoDraw - View widget" );
57 int result = a.exec();
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 ...
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 ...
QtViewWidgetWindow after opening file
The complete source code is show below ...
14 using namespace hippodraw;
18 void QtViewWidgetWindow::init()
23 void QtViewWidgetWindow::fileOpen()
25 #if QT_VERSION < 0x040000 // 3.1.0 27 = QFileDialog::getOpenFileName ( QString::null,
34 = Q3FileDialog::getOpenFileName ( QString::null,
40 if ( file_name.isEmpty () )
return;
42 #if QT_VERSION < 0x030100 // 3.1.0 43 const string filename ( file_name );
45 const string filename( file_name.latin1() );
47 NTupleController * nt_controller = NTupleController::instance ();
49 = nt_controller->createNTuple ( filename );
51 const string histo (
"Histogram" );
52 vector < string > bindings;
53 bindings.push_back (
"Cost" );
55 DisplayController * dc_controller = DisplayController::instance ();
57 = dc_controller->createDisplay ( histo, *nt, bindings );
59 m_view->setPlotter ( plotter );
61 m_inspector =
new Inspector ();
62 m_view -> setInspector ( m_inspector );
65 m_view->update ( plotter );
66 PlotterEvent *
event =
new PlotterEvent ( plotter );
67 QApplication::postEvent ( m_inspector, event );
70 void QtViewWidgetWindow::filePrint()
75 void QtViewWidgetWindow::fileExit()
80 void QtViewWidgetWindow::helpAbout()