qtroot.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright ( C ) 2000 denis Bertini.  All rights reserved.
00003 *****************************************************************************/
00004 
00005 #include "qtroot.h"
00006 
00007 #if QT_VERSION >= 0x40000
00008 //Added by qt3to4:
00009 #include <QCloseEvent>
00010 #include "q3toolbar.h"
00011 #include "q3filedialog.h"
00012 #include "q3strlist.h"
00013 #include "q3popupmenu.h"
00014 #include "q3intdict.h"
00015 #include "q3action.h"
00016 #include "q3toolbar.h"
00017 #include "qnamespace.h"
00018 #include "q3filedialog.h"
00019 #include "q3whatsthis.h"
00020 #include "Q3MimeSourceFactory"
00021 #else
00022 #include "qfiledialog.h"
00023 #include "qstrlist.h"
00024 #include "qpopupmenu.h"
00025 #include "qintdict.h"
00026 #include "qaction.h"
00027 #include "qtoolbar.h"
00028 #include "qnamespace.h"
00029 #include "qfiledialog.h"
00030 #include "qwhatsthis.h"
00031 #include "qkeycode.h"
00032 typedef QToolBar Q3ToolBar;
00033 typedef QPopupMenu Q3PopupMenu;
00034 typedef QAction Q3Action;
00035 typedef QWhatsThis Q3WhatsThis;
00036 typedef QFileDialog Q3FileDialog;
00037 typedef QMimeSourceFactory Q3MimeSourceFactory;
00038 #endif
00039 
00040 #include "stdlib.h"
00041 #include "qevent.h"
00042 #include "qpainter.h"
00043 #include "qprinter.h"
00044 #include "qtoolbutton.h"
00045 #include "qspinbox.h"
00046 #include "qtooltip.h"
00047 #include "qrect.h"
00048 #include "qpoint.h"
00049 #include "qcolordialog.h"
00050 #include "qcursor.h"
00051 #include "qimage.h"
00052 #include "qpushbutton.h"
00053 #include "Riostream.h"
00054 using namespace std;
00055 #include "qdial.h"
00056 #include "qapplication.h"
00057 #include "qimage.h"
00058 #include "qpixmap.h"
00059 #include "qtoolbutton.h"
00060 #include "qmenubar.h"
00061 #include "qfile.h"
00062 #include "qstatusbar.h"
00063 #include "qmessagebox.h"
00064 #include "qdialog.h"
00065 #include "qlabel.h"
00066 
00067 
00068 #include "filesave.xpm"
00069 #include "fileopen.xpm"
00070 #include "fileprint.xpm"
00071 #include "qtbuttonsupdate.xpm"
00072 #include "qtbuttonsclear.xpm"
00073 
00074 #include "TPad.h"
00075 #include "TList.h"
00076 #include "TObject.h"
00077 #include "TROOT.h"
00078 #include "TApplication.h"
00079 #include "TString.h"
00080 #include "TH1.h"
00081 #include "TList.h"
00082 #include "TIterator.h"
00083 #include "TMethod.h"
00084 #include "TCanvas.h"
00085 #include "TDataType.h"
00086 #include "TMethodCall.h"
00087 #include "TPad.h"
00088 #include "TObjArray.h"
00089 #include "TIterator.h"
00090 #include "TRandom.h"
00091 #include "TFrame.h"
00092 #include "TGraph.h"
00093 #include "TMath.h"
00094 
00095 // global menus
00096 
00097 const char * fileOpenText = "<img source=\"fileopen\"> "
00098 "Click this button to open a <em>new file</em>. <br><br>"
00099 "You can also select the <b>Open command</b> from the File menu.";
00100 const char * fileSaveText = "Click this button to save the file you are "
00101 "editing.  You will be prompted for a file name.\n\n"
00102 "You can also select the Save command from the File menu.\n\n"
00103 "Note that implementing this function is left as an exercise for the reader.";
00104 const char * filePrintText = "Click this button to print the file you "
00105 "are editing.\n\n"
00106 "You can also select the Print command from the File menu.";
00107 const char* updateHisto = " update histograms "; 
00108 const char* clearHisto = " clear histograms "; 
00109 
00110 
00111 //---------------------------------------------------------------------------
00112 //            Qt &Root user Application window example 
00113 //---------------------------------------------------------------------------
00114 
00115 
00116 ApplicationWindow::ApplicationWindow()
00117     : Q3MainWindow( 0, "example application main window", Qt::WDestructiveClose )
00118 {
00119    // create a printer
00120    printer = new QPrinter;
00121    // create user interface actions
00122 
00123    Q3Action *fileNewAction = new Q3Action( "New", "&New", Qt::CTRL+Qt::Key_N, this, "new" );
00124 
00125    connect( fileNewAction, SIGNAL( activated() ) , this, SLOT( newDoc() ) );
00126 
00127    Q3Action *fileOpenAction = new Q3Action( "Open File", QPixmap( fileopen ), "&Open", Qt::CTRL+Qt::Key_O, this, "open" );
00128    connect( fileOpenAction, SIGNAL( activated() ) , this, SLOT( load() ) );
00129    Q3MimeSourceFactory::defaultFactory()->setPixmap( "fileopen", QPixmap( fileopen ) );
00130    fileOpenAction->setWhatsThis( fileOpenText );
00131 
00132    Q3Action *fileSaveAction = new Q3Action( "Save File", QPixmap( filesave ), "&Save", Qt::CTRL+Qt::Key_S, this, "save" );
00133    connect( fileSaveAction, SIGNAL( activated() ) , this, SLOT( save() ) );
00134    fileSaveAction->setWhatsThis( fileSaveText );
00135 
00136    Q3Action *fileSaveAsAction = new Q3Action( "Save File As", "Save &as", 0,  this, "save as" );
00137    connect( fileSaveAsAction, SIGNAL( activated() ) , this, SLOT( saveAs() ) );
00138    fileSaveAsAction->setWhatsThis( fileSaveText );
00139 
00140    Q3Action *filePrintAction = new Q3Action( "Print File", QPixmap( fileprint ), "&Print", Qt::CTRL+Qt::Key_P, this, "print" );
00141    connect( filePrintAction, SIGNAL( activated() ) , this, SLOT( print() ) );
00142    filePrintAction->setWhatsThis( filePrintText );
00143 
00144    Q3Action *fileCloseAction = new Q3Action( "Close", "&Close", Qt::CTRL+Qt::Key_W, this, "close" );
00145    connect( fileCloseAction, SIGNAL( activated() ) , this, SLOT( close() ) );
00146 
00147    Q3Action *fileQuitAction = new Q3Action( "Quit", "&Quit", Qt::CTRL+Qt::Key_Q, this, "quit" );
00148    connect( fileQuitAction, SIGNAL( activated() ) , qApp, SLOT( quit() ) );
00149 
00150    // create button for histo handling
00151    Q3Action *Update_histo = new Q3Action("Update Histo",QPixmap("qtbuttonsupdate.xpm"),"&Update", Qt::CTRL+Qt::Key_0, this, "update");
00152    connect( Update_histo, SIGNAL( activated() ) , this, SLOT( execute() ) );
00153    Q3MimeSourceFactory::defaultFactory()->setPixmap( "update", QPixmap("qtbuttonsupdate.xpm" ) );
00154    Update_histo->setWhatsThis( updateHisto );
00155 
00156    Q3Action *clear_histo = new Q3Action("Clear Histo",QPixmap("qtbuttonsclear.xpm"),"&Clear", Qt::CTRL+Qt::Key_0, this, "clear");   connect( clear_histo, SIGNAL( activated() ) , this, SLOT( clear_histo() ) );
00157    Q3MimeSourceFactory::defaultFactory()->setPixmap( "clear", QPixmap("qtbuttonsclear.xpm" ) );
00158    clear_histo->setWhatsThis( clearHisto );
00159 
00160    // populate a tool bar with some actions
00161 
00162    Q3ToolBar* fileTools = new Q3ToolBar( this, "file operations" );
00163    fileTools->setLabel( tr( "File Operations" ) );
00164    fileOpenAction->addTo( fileTools );
00165    fileSaveAction->addTo( fileTools );
00166    filePrintAction->addTo( fileTools );
00167    Update_histo->addTo ( fileTools );
00168    clear_histo->addTo ( fileTools );
00169    (void)Q3WhatsThis::whatsThisButton( fileTools );
00170    // popuplate a menu with all actions
00171 
00172    Q3PopupMenu * file = new Q3PopupMenu( this );
00173    menuBar()->insertItem( "&File", file );
00174    fileNewAction->addTo( file );
00175    fileOpenAction->addTo( file );
00176    fileSaveAction->addTo( file );
00177    fileSaveAsAction->addTo( file );
00178    file->insertSeparator();
00179    filePrintAction->addTo( file );
00180    file->insertSeparator();
00181    fileCloseAction->addTo( file );
00182    fileQuitAction->addTo( file );
00183 
00184    // add a help menu
00185 
00186    Q3PopupMenu * help = new Q3PopupMenu( this );
00187    menuBar()->insertSeparator();
00188    menuBar()->insertItem( "&Help", help );
00189    help->insertItem( "&About", this, SLOT(about()), Qt::Key_F1 );
00190    help->insertItem( "About &Qt", this, SLOT(aboutQt()) );
00191    help->insertSeparator();
00192    help->insertItem( "What's &This", this, SLOT(whatsThis()), Qt::SHIFT+Qt::Key_F1 );
00193 
00194    // create and define the ROOT Canvas central widget
00195    tab = new QTabWidget(this);
00196    tab->show();
00197    setCentralWidget( tab );
00198 
00199    Q3MainWindow *win1 = new Q3MainWindow( 0, "tab1 main window", Qt::WDestructiveClose );
00200    Q3MainWindow *win2 = new Q3MainWindow( 0, "tab2 main window", Qt::WDestructiveClose );
00201    aCanvas = new TQRootCanvas(this, win1,"Qt&Root");
00202    aCanvas2 = new TQRootCanvas(this, win2,"Qt&Root");
00203 
00204    win1->setCentralWidget(aCanvas);
00205    win2->setCentralWidget(aCanvas2);
00206 
00207 
00208    tab->addTab(win1,"page1");
00209    tab->addTab(win2,"page2");
00210 
00211    win1->show();
00212    win2->show();
00213 
00214    // with no QTabWidget
00215    //    aCanvas = new TQRootCanvas(this,"Qt&Root");
00216    //    setCentralWidget( aCanvas );
00217    resize( 450, 500 );
00218 
00219    // put here some ROOT Specifics ...
00220    if (aCanvas->GetCanvas()) {
00221 
00222       aCanvas->GetCanvas()->Resize();
00223       aCanvas->GetCanvas()->SetFillColor(40);
00224       aCanvas->GetCanvas()->cd();
00225 
00226       pad1 = new TPad("pad1","The pad with the function",0.05,0.50,0.95,0.95,21);
00227       pad1->Draw();
00228       pad1->cd();
00229       pad1->SetGridx();
00230       pad1->SetGridy();
00231       pad1->GetFrame()->SetFillColor(42);
00232       pad1->GetFrame()->SetBorderMode(-1);
00233       pad1->GetFrame()->SetBorderSize(5);
00234 
00235       histo= new TH1F("hppx","Gaussian distribution",100,-4,4);
00236       histo->SetFillColor(0);
00237       histo->Draw();
00238 
00239       aCanvas->GetCanvas()->cd();
00240       pad2 = new TPad("pad2","The pad with the histogram",0.05,0.05,0.95,0.45,21);
00241       pad2->Draw();
00242       pad2->cd();
00243 
00244       form1 = new TFormula("form1","abs(sin(x)/x)");
00245       sqroot = new TF1("sqroot","x*gaus(0) + [3]*form1",0,10);
00246       sqroot->SetParameters(10,4,1,20);
00247       sqroot->SetLineColor(4);
00248       sqroot->SetLineWidth(6);
00249       sqroot->Draw();
00250    } // ! aCAnvas
00251 
00252    if (aCanvas2) {
00253 
00254       TCanvas *c1 = aCanvas2->GetCanvas();
00255       c1->Resize();
00256       c1->SetFillColor(42);
00257       c1->GetFrame()->SetFillColor(21);
00258       c1->GetFrame()->SetBorderSize(12);
00259       c1->cd();
00260 
00261       //graph example
00262       const Int_t n = 20;
00263       Double_t x[n], y[n];
00264       for (Int_t i=0;i<n;i++) {
00265          x[i] = i*0.1;
00266          y[i] = 10*TMath::Sin(x[i]+0.2);
00267          //  printf(" i %i %f %f \n",i,x[i],y[i]);
00268       }
00269       TGraph* gr = new TGraph(n,x,y);
00270       gr->SetLineColor(2);
00271       gr->SetLineWidth(4);
00272       gr->SetMarkerColor(4);
00273       gr->SetMarkerStyle(21);
00274       gr->SetTitle("a simple graph");
00275       gr->Draw("ACP");
00276 
00277       gr->GetHistogram()->SetXTitle("X title");
00278       gr->GetHistogram()->SetYTitle("Y title");
00279       c1->Modified();
00280       c1->Update();
00281     } //!aCanvas2
00282 }
00283 
00284 ApplicationWindow::~ApplicationWindow()
00285 {
00286    qDebug(" ~ApplicationWindow() \n");
00287    if (aCanvas)  delete aCanvas;
00288    if (printer) delete printer;
00289    if (histo) delete histo;
00290 }
00291 
00292 void ApplicationWindow::clear_histo()
00293 {
00294    // clear histo and update
00295    if (histo) {
00296       histo->Reset();
00297       histo->Draw();
00298       gROOT->GetSelectedPad()->Modified();
00299       gROOT->GetSelectedPad()->Update();
00300   }
00301 }
00302 
00303 void ApplicationWindow::execute()
00304 {
00305    //fill histograms and update for monitoring
00306    if ( histo ) {
00307       aCanvas->GetCanvas()->cd();
00308       pad1->cd();
00309       float px,py;
00310       const int kUPDATE = 1000;
00311       //internal event loop
00312       for (Int_t i = 0; i < 25000; i++) {
00313          gRandom->Rannor(px,py);
00314          float random = gRandom->Rndm(1);
00315          histo->Fill(px);
00316          if (i && (i%kUPDATE) == 0) {
00317             if (i == kUPDATE) histo->Draw("same");
00318             aCanvas->GetCanvas()->Modified();
00319             aCanvas->GetCanvas()->Update();
00320          }
00321       }
00322       histo->Draw("same");
00323       aCanvas->GetCanvas()->Modified();
00324       aCanvas->GetCanvas()->Update();
00325    }
00326 }
00327 
00328 void ApplicationWindow::newDoc()
00329 {
00330    ApplicationWindow *ed = new ApplicationWindow;
00331    ed->show();
00332 }
00333 
00334 void ApplicationWindow::load()
00335 {
00336    QString fn = Q3FileDialog::getOpenFileName(QString::null, QString::null, this);
00337    if ( !fn.isEmpty() )
00338       load( fn );
00339    else
00340       statusBar()->message( "Loading aborted", 2000 );
00341 }
00342 
00343 void ApplicationWindow::load( const char *fileName )
00344 {
00345 }
00346 
00347 void ApplicationWindow::save()
00348 {
00349 }
00350 
00351 void ApplicationWindow::saveAs()
00352 {
00353 }
00354 
00355 void ApplicationWindow::print()
00356 {
00357 }
00358 
00359 void ApplicationWindow::closeEvent( QCloseEvent* ce )
00360 {
00361    int testvar =  QMessageBox::information( 0, "Qt Application Example",
00362                                             "Do you want to close QtRoot? "
00363                                             "", "Save", "Cancel", "Close",
00364                                             0, 1 );
00365    switch (testvar) {
00366       case 0: // here we should save
00367          // data
00368          save();
00369          ce->accept();
00370          break;
00371       case 1:
00372       default: // just for sanity
00373          ce->ignore();
00374          break;
00375       case 2: // Here i close all windows
00376          // do now an explicit release of Histogram's
00377          // child windows
00378          TList *lc = (TList*)gROOT->GetListOfCanvases();
00379          TObject *fitpanel  =  lc->FindObject("R__fitpanel");
00380          TObject *drawpanel =  lc->FindObject("R__drawpanelhist");
00381          if (fitpanel) {
00382             qDebug("detecting fitpanel %x \n",fitpanel);
00383             delete fitpanel;
00384          }
00385          if (drawpanel) {
00386             qDebug("detecting drawpanel %x \n",drawpanel);
00387             delete drawpanel;
00388          }
00389          ce->accept();
00390          break;
00391    }
00392 }
00393 
00394 void ApplicationWindow::about()
00395 {
00396    QMessageBox::about( this, "Qt&ROOT Application Example",
00397                        "This example demonstrates simple use of "
00398                        "QMainWindow,\nQMenuBar and QToolBar.");
00399 }
00400 
00401 void ApplicationWindow::aboutQt()
00402 {
00403    QMessageBox::aboutQt( this, "Qt Application Example" );
00404 }

Generated on Tue Jul 5 14:22:21 2011 for ROOT_528-00b_version by  doxygen 1.5.1