Logo ROOT   6.08/07
Reference Guide
qtroot.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright ( C ) 2000 denis Bertini. All rights reserved.
3 *****************************************************************************/
4 
5 #include "qtroot.h"
6 
7 #if QT_VERSION >= 0x40000
8 //Added by qt3to4:
9 #include <QCloseEvent>
10 #include "q3toolbar.h"
11 #include "q3filedialog.h"
12 #include "q3strlist.h"
13 #include "q3popupmenu.h"
14 #include "q3intdict.h"
15 #include "q3action.h"
16 #include "q3toolbar.h"
17 #include "qnamespace.h"
18 #include "q3filedialog.h"
19 #include "q3whatsthis.h"
20 #include "Q3MimeSourceFactory"
21 #else
22 #include "qfiledialog.h"
23 #include "qstrlist.h"
24 #include "qpopupmenu.h"
25 #include "qintdict.h"
26 #include "qaction.h"
27 #include "qtoolbar.h"
28 #include "qnamespace.h"
29 #include "qfiledialog.h"
30 #include "qwhatsthis.h"
31 #include "qkeycode.h"
32 typedef QToolBar Q3ToolBar;
33 typedef QPopupMenu Q3PopupMenu;
34 typedef QAction Q3Action;
35 typedef QWhatsThis Q3WhatsThis;
36 typedef QFileDialog Q3FileDialog;
37 typedef QMimeSourceFactory Q3MimeSourceFactory;
38 #endif
39 
40 #include "stdlib.h"
41 #include "qevent.h"
42 #include "qpainter.h"
43 #include "qprinter.h"
44 #include "qtoolbutton.h"
45 #include "qspinbox.h"
46 #include "qtooltip.h"
47 #include "qrect.h"
48 #include "qpoint.h"
49 #include "qcolordialog.h"
50 #include "qcursor.h"
51 #include "qimage.h"
52 #include "qpushbutton.h"
53 #include "Riostream.h"
54 using namespace std;
55 #include "qdial.h"
56 #include "qapplication.h"
57 #include "qimage.h"
58 #include "qpixmap.h"
59 #include "qtoolbutton.h"
60 #include "qmenubar.h"
61 #include "qfile.h"
62 #include "qstatusbar.h"
63 #include "qmessagebox.h"
64 #include "qdialog.h"
65 #include "qlabel.h"
66 
67 
68 #include "filesave.xpm"
69 #include "fileopen.xpm"
70 #include "fileprint.xpm"
71 #include "qtbuttonsupdate.xpm"
72 #include "qtbuttonsclear.xpm"
73 
74 #include "TPad.h"
75 #include "TList.h"
76 #include "TObject.h"
77 #include "TROOT.h"
78 #include "TApplication.h"
79 #include "TString.h"
80 #include "TH1.h"
81 #include "TList.h"
82 #include "TIterator.h"
83 #include "TMethod.h"
84 #include "TCanvas.h"
85 #include "TDataType.h"
86 #include "TMethodCall.h"
87 #include "TPad.h"
88 #include "TObjArray.h"
89 #include "TIterator.h"
90 #include "TRandom.h"
91 #include "TFrame.h"
92 #include "TGraph.h"
93 #include "TMath.h"
94 
95 // global menus
96 
97 const char * fileOpenText = "<img source=\"fileopen\"> "
98 "Click this button to open a <em>new file</em>. <br><br>"
99 "You can also select the <b>Open command</b> from the File menu.";
100 const char * fileSaveText = "Click this button to save the file you are "
101 "editing. You will be prompted for a file name.\n\n"
102 "You can also select the Save command from the File menu.\n\n"
103 "Note that implementing this function is left as an exercise for the reader.";
104 const char * filePrintText = "Click this button to print the file you "
105 "are editing.\n\n"
106 "You can also select the Print command from the File menu.";
107 const char* updateHisto = " update histograms ";
108 const char* clearHisto = " clear histograms ";
109 
110 
111 //---------------------------------------------------------------------------
112 // Qt &Root user Application window example
113 //---------------------------------------------------------------------------
114 
115 
117  : Q3MainWindow( 0, "example application main window", Qt::WDestructiveClose )
118 {
119  // create a printer
120  printer = new QPrinter;
121  // create user interface actions
122 
123  Q3Action *fileNewAction = new Q3Action( "New", "&New", Qt::CTRL+Qt::Key_N, this, "new" );
124 
125  connect( fileNewAction, SIGNAL( activated() ) , this, SLOT( newDoc() ) );
126 
127  Q3Action *fileOpenAction = new Q3Action( "Open File", QPixmap( fileopen ), "&Open", Qt::CTRL+Qt::Key_O, this, "open" );
128  connect( fileOpenAction, SIGNAL( activated() ) , this, SLOT( load() ) );
129  Q3MimeSourceFactory::defaultFactory()->setPixmap( "fileopen", QPixmap( fileopen ) );
130  fileOpenAction->setWhatsThis( fileOpenText );
131 
132  Q3Action *fileSaveAction = new Q3Action( "Save File", QPixmap( filesave ), "&Save", Qt::CTRL+Qt::Key_S, this, "save" );
133  connect( fileSaveAction, SIGNAL( activated() ) , this, SLOT( save() ) );
134  fileSaveAction->setWhatsThis( fileSaveText );
135 
136  Q3Action *fileSaveAsAction = new Q3Action( "Save File As", "Save &as", 0, this, "save as" );
137  connect( fileSaveAsAction, SIGNAL( activated() ) , this, SLOT( saveAs() ) );
138  fileSaveAsAction->setWhatsThis( fileSaveText );
139 
140  Q3Action *filePrintAction = new Q3Action( "Print File", QPixmap( fileprint ), "&Print", Qt::CTRL+Qt::Key_P, this, "print" );
141  connect( filePrintAction, SIGNAL( activated() ) , this, SLOT( print() ) );
142  filePrintAction->setWhatsThis( filePrintText );
143 
144  Q3Action *fileCloseAction = new Q3Action( "Close", "&Close", Qt::CTRL+Qt::Key_W, this, "close" );
145  connect( fileCloseAction, SIGNAL( activated() ) , this, SLOT( close() ) );
146 
147  Q3Action *fileQuitAction = new Q3Action( "Quit", "&Quit", Qt::CTRL+Qt::Key_Q, this, "quit" );
148  connect( fileQuitAction, SIGNAL( activated() ) , qApp, SLOT( quit() ) );
149 
150  // create button for histo handling
151  Q3Action *Update_histo = new Q3Action("Update Histo",QPixmap("qtbuttonsupdate.xpm"),"&Update", Qt::CTRL+Qt::Key_0, this, "update");
152  connect( Update_histo, SIGNAL( activated() ) , this, SLOT( execute() ) );
153  Q3MimeSourceFactory::defaultFactory()->setPixmap( "update", QPixmap("qtbuttonsupdate.xpm" ) );
154  Update_histo->setWhatsThis( updateHisto );
155 
156  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() ) );
157  Q3MimeSourceFactory::defaultFactory()->setPixmap( "clear", QPixmap("qtbuttonsclear.xpm" ) );
158  clear_histo->setWhatsThis( clearHisto );
159 
160  // populate a tool bar with some actions
161 
162  Q3ToolBar* fileTools = new Q3ToolBar( this, "file operations" );
163  fileTools->setLabel( tr( "File Operations" ) );
164  fileOpenAction->addTo( fileTools );
165  fileSaveAction->addTo( fileTools );
166  filePrintAction->addTo( fileTools );
167  Update_histo->addTo ( fileTools );
168  clear_histo->addTo ( fileTools );
169  (void)Q3WhatsThis::whatsThisButton( fileTools );
170  // popuplate a menu with all actions
171 
172  Q3PopupMenu * file = new Q3PopupMenu( this );
173  menuBar()->insertItem( "&File", file );
174  fileNewAction->addTo( file );
175  fileOpenAction->addTo( file );
176  fileSaveAction->addTo( file );
177  fileSaveAsAction->addTo( file );
178  file->insertSeparator();
179  filePrintAction->addTo( file );
180  file->insertSeparator();
181  fileCloseAction->addTo( file );
182  fileQuitAction->addTo( file );
183 
184  // add a help menu
185 
186  Q3PopupMenu * help = new Q3PopupMenu( this );
187  menuBar()->insertSeparator();
188  menuBar()->insertItem( "&Help", help );
189  help->insertItem( "&About", this, SLOT(about()), Qt::Key_F1 );
190  help->insertItem( "About &Qt", this, SLOT(aboutQt()) );
191  help->insertSeparator();
192  help->insertItem( "What's &This", this, SLOT(whatsThis()), Qt::SHIFT+Qt::Key_F1 );
193 
194  // create and define the ROOT Canvas central widget
195  tab = new QTabWidget(this);
196  tab->show();
197  setCentralWidget( tab );
198 
199  Q3MainWindow *win1 = new Q3MainWindow( 0, "tab1 main window", Qt::WDestructiveClose );
200  Q3MainWindow *win2 = new Q3MainWindow( 0, "tab2 main window", Qt::WDestructiveClose );
201  aCanvas = new TQRootCanvas(this, win1,"Qt&Root");
202  aCanvas2 = new TQRootCanvas(this, win2,"Qt&Root");
203 
204  win1->setCentralWidget(aCanvas);
205  win2->setCentralWidget(aCanvas2);
206 
207 
208  tab->addTab(win1,"page1");
209  tab->addTab(win2,"page2");
210 
211  win1->show();
212  win2->show();
213 
214  // with no QTabWidget
215  // aCanvas = new TQRootCanvas(this,"Qt&Root");
216  // setCentralWidget( aCanvas );
217  resize( 450, 500 );
218 
219  // put here some ROOT Specifics ...
220  if (aCanvas->GetCanvas()) {
221 
222  aCanvas->GetCanvas()->Resize();
224  aCanvas->GetCanvas()->cd();
225 
226  pad1 = new TPad("pad1","The pad with the function",0.05,0.50,0.95,0.95,21);
227  pad1->Draw();
228  pad1->cd();
229  pad1->SetGridx();
230  pad1->SetGridy();
231  pad1->GetFrame()->SetFillColor(42);
232  pad1->GetFrame()->SetBorderMode(-1);
233  pad1->GetFrame()->SetBorderSize(5);
234 
235  histo= new TH1F("hppx","Gaussian distribution",100,-4,4);
236  histo->SetFillColor(0);
237  histo->Draw();
238 
239  aCanvas->GetCanvas()->cd();
240  pad2 = new TPad("pad2","The pad with the histogram",0.05,0.05,0.95,0.45,21);
241  pad2->Draw();
242  pad2->cd();
243 
244  form1 = new TFormula("form1","abs(sin(x)/x)");
245  sqroot = new TF1("sqroot","x*gaus(0) + [3]*form1",0,10);
246  sqroot->SetParameters(10,4,1,20);
247  sqroot->SetLineColor(4);
248  sqroot->SetLineWidth(6);
249  sqroot->Draw();
250  } // ! aCAnvas
251 
252  if (aCanvas2) {
253 
255  c1->Resize();
256  c1->SetFillColor(42);
257  c1->GetFrame()->SetFillColor(21);
258  c1->GetFrame()->SetBorderSize(12);
259  c1->cd();
260 
261  //graph example
262  const Int_t n = 20;
263  Double_t x[n], y[n];
264  for (Int_t i=0;i<n;i++) {
265  x[i] = i*0.1;
266  y[i] = 10*TMath::Sin(x[i]+0.2);
267  // printf(" i %i %f %f \n",i,x[i],y[i]);
268  }
269  TGraph* gr = new TGraph(n,x,y);
270  gr->SetLineColor(2);
271  gr->SetLineWidth(4);
272  gr->SetMarkerColor(4);
273  gr->SetMarkerStyle(21);
274  gr->SetTitle("a simple graph");
275  gr->Draw("ACP");
276 
277  gr->GetHistogram()->SetXTitle("X title");
278  gr->GetHistogram()->SetYTitle("Y title");
279  c1->Modified();
280  c1->Update();
281  } //!aCanvas2
282 }
283 
285 {
286  qDebug(" ~ApplicationWindow() \n");
287  if (aCanvas) delete aCanvas;
288  if (printer) delete printer;
289  if (histo) delete histo;
290 }
291 
293 {
294  // clear histo and update
295  if (histo) {
296  histo->Reset();
297  histo->Draw();
298  gROOT->GetSelectedPad()->Modified();
299  gROOT->GetSelectedPad()->Update();
300  }
301 }
302 
304 {
305  //fill histograms and update for monitoring
306  if ( histo ) {
307  aCanvas->GetCanvas()->cd();
308  pad1->cd();
309  float px,py;
310  const int kUPDATE = 1000;
311  //internal event loop
312  for (Int_t i = 0; i < 25000; i++) {
313  gRandom->Rannor(px,py);
314  float random = gRandom->Rndm(1);
315  histo->Fill(px);
316  if (i && (i%kUPDATE) == 0) {
317  if (i == kUPDATE) histo->Draw("same");
318  aCanvas->GetCanvas()->Modified();
319  aCanvas->GetCanvas()->Update();
320  }
321  }
322  histo->Draw("same");
323  aCanvas->GetCanvas()->Modified();
324  aCanvas->GetCanvas()->Update();
325  }
326 }
327 
329 {
331  ed->show();
332 }
333 
335 {
336  QString fn = Q3FileDialog::getOpenFileName(QString::null, QString::null, this);
337  if ( !fn.isEmpty() )
338  load( fn );
339  else
340  statusBar()->message( "Loading aborted", 2000 );
341 }
342 
343 void ApplicationWindow::load( const char *fileName )
344 {
345 }
346 
348 {
349 }
350 
352 {
353 }
354 
356 {
357 }
358 
359 void ApplicationWindow::closeEvent( QCloseEvent* ce )
360 {
361  int testvar = QMessageBox::information( 0, "Qt Application Example",
362  "Do you want to close QtRoot? "
363  "", "Save", "Cancel", "Close",
364  0, 1 );
365  switch (testvar) {
366  case 0: // here we should save
367  // data
368  save();
369  ce->accept();
370  break;
371  case 1:
372  default: // just for sanity
373  ce->ignore();
374  break;
375  case 2: // Here i close all windows
376  // do now an explicit release of Histogram's
377  // child windows
378  TList *lc = (TList*)gROOT->GetListOfCanvases();
379  TObject *fitpanel = lc->FindObject("R__fitpanel");
380  TObject *drawpanel = lc->FindObject("R__drawpanelhist");
381  if (fitpanel) {
382  qDebug("detecting fitpanel %x \n",fitpanel);
383  delete fitpanel;
384  }
385  if (drawpanel) {
386  qDebug("detecting drawpanel %x \n",drawpanel);
387  delete drawpanel;
388  }
389  ce->accept();
390  break;
391  }
392 }
393 
395 {
396  QMessageBox::about( this, "Qt&ROOT Application Example",
397  "This example demonstrates simple use of "
398  "QMainWindow,\nQMenuBar and QToolBar.");
399 }
400 
402 {
403  QMessageBox::aboutQt( this, "Qt Application Example" );
404 }
QAction Q3Action
Definition: qtroot.cpp:34
virtual void SetGridx(Int_t value=1)
Definition: TPad.h:319
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition: TAttLine.h:49
void aboutQt()
online help about this Qt based Application
Definition: qtroot.cpp:401
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3125
void clear_histo()
Main slot to reinitialize the objects.
Definition: qtroot.cpp:292
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:439
virtual void Rannor(Float_t &a, Float_t &b)
Return 2 numbers distributed following a gaussian with mean=0 and sigma=1.
Definition: TRandom.cxx:460
void load()
IO Loading:
Definition: qtroot.cpp:334
void print()
print results in a specified format
Definition: qtroot.cpp:355
virtual void SetBorderMode(Short_t bordermode)
Definition: TWbox.h:53
return c1
Definition: legend1.C:41
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:302
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
#define gROOT
Definition: TROOT.h:364
void execute()
Main slot action upon ROOT classes.
Definition: qtroot.cpp:303
int Int_t
Definition: RtypesCore.h:41
virtual void SetYTitle(const char *title)
Definition: TH1.h:414
QFileDialog Q3FileDialog
Definition: qtroot.cpp:36
virtual void Draw(Option_t *option="")
Draw this function with its current attributes.
Definition: TF1.cxx:1086
const char * filePrintText
Definition: qtroot.cpp:104
virtual void SetTitle(const char *title="")
Set graph title.
Definition: TGraph.cxx:2176
void about()
online help about Qt&Root Main Application
Definition: qtroot.cpp:394
STL namespace.
TQRootCanvas * aCanvas
Definition: qtroot.h:158
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:747
TQRootCanvas * aCanvas2
Definition: qtroot.h:158
virtual void Reset(Option_t *option="")
Reset.
Definition: TH1.cxx:9207
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
Definition: TList.cxx:497
null_t< F > null()
TVirtualPad * cd(Int_t subpadnumber=0)
Set Current pad.
Definition: TPad.cxx:526
Double_t x[n]
Definition: legend1.C:17
virtual void Resize(Option_t *option="")
Recompute canvas parameters following a X11 Resize.
Definition: TCanvas.cxx:1533
TFrame * GetFrame()
Get frame.
Definition: TPad.cxx:2746
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition: TAttMarker.h:43
QPrinter * printer
Definition: qtroot.h:157
TCanvas * GetCanvas()
Definition: TQRootCanvas.h:73
virtual void SetBorderSize(Short_t bordersize)
Definition: TWbox.h:54
virtual void Draw(Option_t *option="")
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1208
A doubly linked list.
Definition: TList.h:47
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:512
const char * fileOpenText
Definition: qtroot.cpp:97
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:46
Main Qt&Root Application Window:
Definition: qtroot.h:72
void save()
Saving results function:
Definition: qtroot.cpp:347
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:42
QMainWindow Q3MainWindow
Definition: qtroot.h:21
The F O R M U L A class.
Definition: TFormula.h:89
void newDoc()
New incoming functionality documentation.
Definition: qtroot.cpp:328
The most important graphics class in the ROOT system.
Definition: TPad.h:37
void closeEvent(QCloseEvent *ce)
Definition: qtroot.cpp:359
TH1F * histo
Definition: qtroot.h:159
QWhatsThis Q3WhatsThis
Definition: qtroot.cpp:35
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:45
QTabWidget * tab
Definition: qtroot.h:163
R__EXTERN TRandom * gRandom
Definition: TRandom.h:66
TGraphErrors * gr
Definition: legend1.C:25
TFormula * form1
Definition: qtroot.h:161
#define SHIFT
Definition: gifencode.c:16
The Canvas class.
Definition: TCanvas.h:41
const char * fileSaveText
Definition: qtroot.cpp:100
TH1F * GetHistogram() const
Returns a pointer to the histogram used to draw the axis Takes into account the two following cases...
Definition: TGraph.cxx:1466
double Double_t
Definition: RtypesCore.h:55
virtual void SetGridy(Int_t value=1)
Definition: TPad.h:320
QMimeSourceFactory Q3MimeSourceFactory
Definition: qtroot.cpp:37
Double_t y[n]
Definition: legend1.C:17
Mother of all ROOT objects.
Definition: TObject.h:37
typedef void((*Func_t)())
QPopupMenu Q3PopupMenu
Definition: qtroot.cpp:33
virtual void SetXTitle(const char *title)
Definition: TH1.h:413
const char * clearHisto
Definition: qtroot.cpp:108
void saveAs()
saving pictures, in different supported formats
Definition: qtroot.cpp:351
Definition: file.py:1
1-Dim function class
Definition: TF1.h:149
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:53
Double_t Sin(Double_t)
Definition: TMath.h:421
QToolBar Q3ToolBar
Definition: qtroot.cpp:32
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2183
const char * updateHisto
Definition: qtroot.cpp:107
const Int_t n
Definition: legend1.C:16
void Modified(Bool_t flag=1)
Definition: TPad.h:399