TMainDisplayWindow.cxx

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <cstdlib>
00003 
00004 #include <TGFileDialog.h>
00005 #include "TMainDisplayWindow.hxx"
00006 #include <TObject.h>
00007 
00008 ClassImp(TMainDisplayWindow)
00009 static int fDefaultWidth = 1200;
00010 static int fDefaultHeight = 800;
00011 
00012 TMainDisplayWindow::TMainDisplayWindow(const TGWindow *p,UInt_t w,UInt_t h, bool isOffline)
00013   
00014 {
00015   fIsOffline = isOffline;
00016   fProcessingPaused = false;
00017   fNumberSkipEventButton = 0;
00018 
00019   // Create a main frame
00020   fMain = new TGMainFrame(p,w,h);
00021 
00022   fTab = new TGTab(fMain);
00023   fTab->Resize(fTab->GetDefaultSize());
00024   fMain->AddFrame(fTab, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
00025   fTab->MoveResize(2,2,300,300);
00026 
00027   // _______________________________________________________________________________________
00028   // Set up buttons for bottom of mainframe.
00029 
00030   // Create a horizontal frame widget with buttons
00031   fHframe = new TGHorizontalFrame(fMain,200,40);
00032 
00033 
00034   // Set different options for bottom, depending on if using offline or online.
00035   if(fIsOffline){
00036 
00037     fNextButton = new TGTextButton(fHframe,"&Next");
00038     fHframe->AddFrame(fNextButton, new TGLayoutHints(kLHintsCenterX,5,5,3,4));
00039       
00040   }else{
00041 
00042     fNumberSkipEventButton = new TGNumberEntry(fHframe, 0, 9,999, TGNumberFormat::kNESInteger,
00043                                   TGNumberFormat::kNEANonNegative, 
00044                                   TGNumberFormat::kNELLimitMinMax,
00045                                   1, 100000);
00046     fNumberSkipEventButton->SetIntNumber(5);
00047     fHframe->AddFrame(fNumberSkipEventButton, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 5));
00048 
00049     TGLabel *labelEv = new TGLabel(fHframe, "Plot every Xth events");
00050     fHframe->AddFrame(labelEv, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 5));
00051 
00052   }
00053 
00054 
00055   // Add the histogram reset button.
00056   fResetButton = new TGTextButton(fHframe,"&Reset Histograms");
00057   fHframe->AddFrame(fResetButton, new TGLayoutHints(kLHintsCenterX,5,5,3,4));
00058 
00059 
00060   // Add buttons to save current pad or current canvas.
00061   fSavePadButton = new TGTextButton(fHframe,"&Save Active Pad");
00062   fSavePadButton->Connect("Clicked()", "TMainDisplayWindow", this, "SavePadButtonAction()");
00063   fHframe->AddFrame(fSavePadButton, new TGLayoutHints(kLHintsCenterX,5,5,3,4));
00064   
00065   fSaveCanvasButton = new TGTextButton(fHframe,"&Save Canvas");
00066   fSaveCanvasButton->Connect("Clicked()", "TMainDisplayWindow", this, "SaveCanvasButtonAction()");
00067   fHframe->AddFrame(fSaveCanvasButton, new TGLayoutHints(kLHintsCenterX,5,5,3,4));
00068 
00069   // Disable the save buttons if online
00070   if(!fIsOffline){
00071     fSavePadButton->SetEnabled(false);
00072     fSaveCanvasButton->SetEnabled(false);
00073   }
00074   
00075 
00076   if(!fIsOffline){
00077     fPauseButton = new TGTextButton(fHframe,"&Pause");
00078     fHframe->AddFrame(fPauseButton, new TGLayoutHints(kLHintsCenterX,5,5,3,4));
00079     fPauseButton->Connect("Clicked()", "TMainDisplayWindow", this, "PauseResumeButtonAction()");
00080   }
00081 
00082 
00083   fQuitButton = new TGTextButton(fHframe,"&Quit");
00084   fHframe->AddFrame(fQuitButton, new TGLayoutHints(kLHintsCenterX,5,5,3,4));
00085 
00086 
00087   fMain->AddFrame(fHframe, new TGLayoutHints(kLHintsNormal,2,2,2,2));
00088 
00089 }
00090 
00091 
00092 
00093 /// This resizing is still not working right! 
00094 /// You can easily get a situation where resizing doesn't work.  Argh.
00095 void TMainDisplayWindow::ResetSize(){  
00096 
00097   // Resize each part of the tab.
00098   for(int itab = 0; itab < fTab->GetNumberOfTabs() ; itab++){    
00099 
00100     TGCompositeFrame* frame = fTab->GetTabContainer(itab);
00101 
00102     // This is awkward.  Each composite frame actually has a bunch of elements.
00103     // Try to resize all the elements that are TGFrameElements. 
00104     // This seems to work if the added frame is a TRootEmbeddedCanvas; not sure 
00105     // otherwise.
00106     TList* mylist = frame->GetList();
00107     
00108     TIter next(mylist);
00109     while (TObject *obj = next()){
00110 
00111       
00112       TGFrameElement *frame_element = dynamic_cast<TGFrameElement*>(obj);
00113 
00114       if(frame_element){
00115         frame_element->fFrame->Resize((frame_element->fFrame->GetDefaultSize().fWidth)
00116                                       *(fMain->GetSize().fWidth)/(fMain->GetDefaultSize().fWidth),
00117                                       (frame_element->fFrame->GetDefaultSize().fHeight)
00118                                       *(fMain->GetSize().fHeight)/(fMain->GetDefaultSize().fHeight));
00119 
00120 
00121         TGTab *this_tab = dynamic_cast<TGTab*>(frame_element->fFrame);
00122         if(this_tab){
00123           // Resize each part of the tab.
00124           for(int jtab = 0; jtab < this_tab->GetNumberOfTabs() ; jtab++){    
00125             
00126             TGCompositeFrame* frame2 = this_tab->GetTabContainer(jtab);
00127             
00128             // This is awkward.  Each composite frame actually has a bunch of elements.
00129             // Try to resize all the elements that are TGFrameElements. 
00130             // This seems to work if the added frame is a TRootEmbeddedCanvas; not sure 
00131             // otherwise.
00132             TList* mylist2 = frame2->GetList();
00133             
00134             TIter next2(mylist2);
00135             while (TObject *obj2 = next2()){
00136 
00137             
00138               TGFrameElement *frame_element = dynamic_cast<TGFrameElement*>(obj2);
00139               
00140               if(frame_element){
00141                 frame_element->fFrame->Resize((frame_element->fFrame->GetDefaultSize().fWidth)
00142                                               *(fMain->GetSize().fWidth)/(fMain->GetDefaultSize().fWidth),
00143                                               (frame_element->fFrame->GetDefaultSize().fHeight)
00144                                               *(fMain->GetSize().fHeight)/(fMain->GetDefaultSize().fHeight));
00145               }
00146             }
00147           }
00148 
00149         }
00150         
00151 
00152       }else
00153         std::cout << "TMainDisplayWindow::ResetSize::  Dynamic cast of obj to TGFrameElement failed." << std::endl;
00154 
00155 
00156     }
00157    
00158 
00159   }
00160 
00161 }
00162  
00163 TGTab* TMainDisplayWindow::GetSubTab(int index){
00164 
00165   TGCompositeFrame* frame = fTab->GetTabContainer(index);
00166   
00167   TList* mylist = frame->GetList();
00168   
00169   TIter next(mylist);
00170   while (TObject *obj = next()){    
00171     TGFrameElement *frame_element = dynamic_cast<TGFrameElement*>(obj);
00172      
00173     if(frame_element){
00174       TGTab *this_tab = dynamic_cast<TGTab*>(frame_element->fFrame);
00175       if(this_tab)   return this_tab;       
00176     }
00177   }
00178   
00179   return 0;
00180   
00181 }
00182 
00183 std::pair<int,int> TMainDisplayWindow::AddSingleTab(std::string name, TGTab * tab, int mainTabIndex){
00184 
00185 
00186   // Add a new tab element, of type TGCompositeFrame
00187   TGCompositeFrame *compositeFrame;
00188   if(tab==0)
00189     compositeFrame = fTab->AddTab(name.c_str());
00190   else
00191     compositeFrame = tab->AddTab(name.c_str());
00192   compositeFrame->SetLayoutManager(new TGVerticalLayout(compositeFrame));
00193 
00194   // Add a canvas within that composite frame.
00195   char cname[500];
00196   std::pair<int,int> index;
00197   if(tab ==0){
00198     sprintf(cname,"Canvas_%i",fTab->GetNumberOfTabs() - 1);
00199     index.first = fTab->GetNumberOfTabs() - 1;
00200     index.second = - 1;
00201   }else{
00202     sprintf(cname,"Canvas_%i_%i",mainTabIndex,tab->GetNumberOfTabs() - 1);
00203     //sprintf(cname,"Canvas_%i_%i",fTab->GetNumberOfTabs() - 1,tab->GetNumberOfTabs() - 1);
00204     index.first = mainTabIndex; // fTab->GetNumberOfTabs() - 1;
00205     index.second = tab->GetNumberOfTabs() - 1;
00206   }
00207   TRootEmbeddedCanvas *embed_canvas  = new TRootEmbeddedCanvas(cname, compositeFrame,fDefaultWidth,fDefaultHeight);
00208   compositeFrame->AddFrame(embed_canvas, new TGLayoutHints(kLHintsTop | kLHintsExpandX,5,5,5,0));
00209 
00210   return index;
00211 
00212 }
00213 
00214 std::pair<int,int> TMainDisplayWindow::AddCanvas(std::string subtabname, std::string tabname){
00215 
00216   // If we want to add tab to main tab-list, tabname is empty
00217   if(tabname.empty()){
00218     std::cout << "Adding new canvas in tab named '" << subtabname << "'" << std::endl;
00219     return AddSingleTab(subtabname);
00220   }
00221 
00222   // Otherwise we try to find an existing tab with that name.
00223   bool foundTab = false;
00224   TGTab *subtab = 0;
00225   int mainTabIndex = 0;
00226   for(int itab = 0; itab < fTab->GetNumberOfTabs() ; itab++){    
00227         
00228     if(GetTabName(fTab,itab).compare(tabname) == 0){
00229       subtab = GetSubTab(itab);    
00230       mainTabIndex = itab;
00231     }
00232   }
00233   
00234   if(subtab){
00235     std::cout << "Adding new canvas in sub-tab named '" << subtabname << "' of tab " << tabname << std::endl;    
00236     return AddSingleTab(subtabname,subtab,mainTabIndex);
00237   }
00238 
00239   // We didn't find an existing sub-tab; create it.
00240   TGCompositeFrame *compositeFrame = fTab->AddTab(tabname.c_str());
00241   compositeFrame->SetLayoutManager(new TGVerticalLayout(compositeFrame));
00242   subtab = new TGTab(compositeFrame);
00243   compositeFrame->AddFrame(subtab, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
00244 
00245   std::cout << "Creating new tab named '"<< tabname <<"'. "
00246             << "Creating a new canvas in sub-tab named '" << subtabname << "'" << std::endl;    
00247   return AddSingleTab(subtabname,subtab,fTab->GetNumberOfTabs()-1);
00248   
00249  
00250 }
00251 
00252 std::pair<int,int> TMainDisplayWindow::GetCurrentTabIndex(){
00253   std::pair<int,int> tmp;
00254   tmp.first = fTab->GetCurrent();
00255   TGTab *subtab = GetSubTab(fTab->GetCurrent());
00256   if(subtab)
00257     tmp.second = subtab->GetCurrent();
00258   else
00259     tmp.second = -1;
00260 
00261   return tmp;
00262 
00263 }
00264 
00265 
00266 void TMainDisplayWindow::BuildWindow(){
00267 
00268   fMain->MapSubwindows();
00269   // Initialize the layout algorithm
00270   fMain->Resize(fMain->GetDefaultSize());
00271   // Map main frame
00272   fMain->MapWindow();
00273 
00274 
00275 }
00276 
00277 /// Pull the embedded canvas out of the compositeframe.
00278 TRootEmbeddedCanvas* GetTRootEmbeddedCanvasFromTGCompositeFrame(TGCompositeFrame* frame){
00279 
00280   if(!frame) return 0;
00281   
00282   TList* mylist = frame->GetList();
00283   
00284   TIter next(mylist);
00285   while (TObject *obj = next()){
00286     
00287     TGFrameElement *frame_element = dynamic_cast<TGFrameElement*>(obj);
00288     
00289     if(frame_element){
00290       
00291       TGFrame* frame = frame_element->fFrame;
00292       TRootEmbeddedCanvas *embed_canvas = dynamic_cast<TRootEmbeddedCanvas*>(frame);
00293 
00294       if(embed_canvas){
00295         return embed_canvas;
00296       }
00297     }    
00298   }
00299   
00300   return 0;
00301 
00302 }
00303 
00304 TGCompositeFrame* TMainDisplayWindow::GetCompositeFrame(std::pair<int,int> index){
00305 
00306   TGTab *subtab = GetSubTab(index.first);
00307   TGCompositeFrame* frame;
00308   if(subtab)
00309     frame = subtab->GetTabContainer(index.second);
00310   else
00311     frame = fTab->GetTabContainer(index.first);
00312   return frame;
00313 
00314 }
00315 
00316 TGCompositeFrame* TMainDisplayWindow::GetCurrentCompositeFrame(){
00317   TGTab *subtab = GetSubTab(fTab->GetCurrent());
00318   TGCompositeFrame* frame;
00319   if(subtab)
00320     frame = subtab->GetTabContainer(subtab->GetCurrent());
00321   else
00322     frame = fTab->GetTabContainer(fTab->GetCurrent());
00323   return frame;
00324 }
00325 
00326 TRootEmbeddedCanvas* TMainDisplayWindow::GetCurrentEmbeddedCanvas(){
00327 
00328   TGCompositeFrame* frame = GetCurrentCompositeFrame();
00329   return GetTRootEmbeddedCanvasFromTGCompositeFrame(frame);
00330 
00331 }
00332 
00333 
00334 TRootEmbeddedCanvas* TMainDisplayWindow::GetEmbeddedCanvas(const char *name){
00335 
00336   
00337   // Complicated.  Need to loop over all possible tabs and sub-tabs, looking for
00338   // first match.
00339   for(int itab = 0; itab < fTab->GetNumberOfTabs() ; itab++){    
00340 
00341     if(GetTabName(fTab, itab).compare(name) == 0){
00342       TGCompositeFrame* frame = fTab->GetTabContainer(itab);
00343       return GetTRootEmbeddedCanvasFromTGCompositeFrame(frame);
00344     }
00345 
00346     TGTab *subtab = 0;
00347     if(subtab = GetSubTab(itab)){
00348       for (int isubtab = 0; isubtab < subtab->GetNumberOfTabs() ; isubtab++){    
00349     
00350         //std::cout << itab << " " << isubtab << " Name! " << GetTabName(subtab, isubtab) << std::endl;
00351         if(GetTabName(subtab, isubtab).compare(name) == 0){
00352           TGCompositeFrame* frame = subtab->GetTabContainer(isubtab);
00353           return GetTRootEmbeddedCanvasFromTGCompositeFrame(frame);
00354         }       
00355       }
00356     }
00357   }
00358 
00359   return 0;
00360 
00361 
00362 }
00363 
00364 
00365 
00366 
00367 
00368 TCanvas* TMainDisplayWindow::GetCanvas(const char *name){
00369   TRootEmbeddedCanvas* embed = GetEmbeddedCanvas(name);
00370   if(embed) return embed->GetCanvas();
00371   
00372   return 0;
00373 }
00374 
00375  
00376 TGTab* TMainDisplayWindow::GetTab(std::pair<int,int> tabindex){
00377 
00378   TGTab *tab = 0;
00379   if(tabindex.second == -1)
00380     tab = fTab;
00381   else{
00382     tab = GetSubTab(tabindex.first);
00383   }
00384 
00385   if(!tab) return 0;  
00386   return tab;
00387   
00388 }
00389 std::string TMainDisplayWindow::GetTabName(TGTab *tab, int index){
00390 
00391   TIter nextTab(tab->GetList());
00392   nextTab();  // Skip the first element.  This is the overall TGCompositeFrame, not the tabbed one.
00393 
00394   // Get the index for the current canvas
00395   if(index < 0)
00396     index = tab->GetCurrent();
00397   TGFrameElement *el;
00398   TGTabElement *tabel = 0;
00399   TGCompositeFrame *comp = 0;
00400   // Loop over the number of tabs, until we find the right one.
00401   for(int i = 0; i <= index; i++){
00402     el = (TGFrameElement *) nextTab();
00403     tabel  = (TGTabElement *) el->fFrame;
00404     el   = (TGFrameElement *) nextTab();
00405     comp = (TGCompositeFrame *) el->fFrame;
00406   }
00407   if(tabel){
00408     return std::string(*tabel->GetText());
00409   }
00410 
00411   return std::string("not found");
00412 }
00413 
00414 std::string TMainDisplayWindow::GetCurrentTabName(){
00415  
00416   std::pair<int,int> tabindex = GetCurrentTabIndex();
00417   TGTab *tab = GetTab(tabindex);
00418   if(!tab || tab->GetNumberOfTabs() == 0){
00419     return std::string("not found");
00420   }
00421   return GetTabName(tab);
00422 
00423 
00424 }
00425 
00426 
00427 TMainDisplayWindow::~TMainDisplayWindow() {
00428 
00429   delete fMain;
00430   delete fTab;  
00431 
00432 }
00433 
00434 
00435 
00436 const char *filetypes[] = { "GIF files",    "*.gif",
00437                             "JPG files",     "*.jpg",
00438                             "PNG files",     "*.png",
00439                             "EPS files",   "*.eps",
00440                             0,               0 };
00441 
00442 void TMainDisplayWindow::SavePadButtonAction(){
00443   static TString dir(".");
00444   TGFileInfo fi;
00445   fi.fFileTypes = filetypes;
00446   fi.fIniDir    = StrDup(dir);
00447   new TGFileDialog(gClient->GetRoot(), fMain, kFDSave, &fi);
00448   printf("Open file: %s (dir: %s)\n", fi.fFilename,
00449          fi.fIniDir);
00450 
00451   // Resize pad while saving; to get good image resolution.fTab
00452   double xl = gPad->GetXlowNDC(), xh = gPad->GetXlowNDC() + gPad->GetWNDC();
00453   double yl = gPad->GetYlowNDC(), yh = gPad->GetYlowNDC() + gPad->GetHNDC();
00454 
00455   gPad->SetPad(0,0,1.0,1.0);
00456   gPad->SaveAs(fi.fFilename);
00457   gPad->SetPad(xl,yl,xh,yh);
00458   gPad->Update();
00459   
00460 }
00461 
00462 void TMainDisplayWindow::SaveCanvasButtonAction(){
00463   
00464   static TString dir(".");
00465   TGFileInfo fi;
00466   fi.fFileTypes = filetypes;
00467   fi.fIniDir    = StrDup(dir);
00468   new TGFileDialog(gClient->GetRoot(), fMain, kFDSave, &fi);
00469   printf("Open file: %s (dir: %s)\n", fi.fFilename,
00470          fi.fIniDir);
00471 
00472   gPad->GetCanvas()->SaveAs(fi.fFilename);
00473 
00474 }
00475 
00476 void TMainDisplayWindow::PauseResumeButtonAction(){
00477 
00478   if(fProcessingPaused){
00479     fProcessingPaused = false;
00480     fPauseButton->SetText("Pause");
00481     fSavePadButton->SetEnabled(false);
00482     fSaveCanvasButton->SetEnabled(false);
00483   }else{
00484     fProcessingPaused = true;
00485     fPauseButton->SetText("Resume");
00486     fSavePadButton->SetEnabled(true);
00487     fSaveCanvasButton->SetEnabled(true);
00488   }
00489 }
00490 
00491 
00492 int TMainDisplayWindow::GetNumberSubTabs(int i){
00493 
00494 
00495   TGCompositeFrame* frame = fTab->GetTabContainer(i);
00496   
00497   // This is awkward.  Each composite frame actually has a bunch of elements,
00498   // one of which might be a TGTab
00499   TList* mylist = frame->GetList();
00500   
00501   TIter next(mylist);
00502   
00503   while (TObject *obj = next()){    
00504     TGFrameElement *frame_element = dynamic_cast<TGFrameElement*>(obj);    
00505     if(frame_element){         
00506       TGTab *this_tab = dynamic_cast<TGTab*>(frame_element->fFrame);
00507       if(this_tab){        
00508         return this_tab->GetNumberOfTabs();
00509       }
00510     }
00511   }
00512 
00513   // didn't find sub-tab, so return 0
00514   return 0;
00515 }

Generated on 12 Feb 2016 for ROOT Analyzer by  doxygen 1.6.1