LCOV - code coverage report
Current view: top level - MUON/MUONgraphics - AliMUONPainterInterfaceHelper.cxx (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1 114 0.9 %
Date: 2016-06-14 17:26:59 Functions: 1 19 5.3 %

          Line data    Source code
       1             : /**************************************************************************
       2             : * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
       3             : *                                                                        *
       4             : * Author: The ALICE Off-line Project.                                    *
       5             : * Contributors are mentioned in the code where appropriate.              *
       6             : *                                                                        *
       7             : * Permission to use, copy, modify and distribute this software and its   *
       8             : * documentation strictly for non-commercial purposes is hereby granted   *
       9             : * without fee, provided that the above copyright notice appears in all   *
      10             : * copies and that both the copyright notice and this permission notice   *
      11             : * appear in the supporting documentation. The authors make no claims     *
      12             : * about the suitability of this software for any purpose. It is          *
      13             : * provided "as is" without express or implied warranty.                  *
      14             : **************************************************************************/
      15             : 
      16             : // $Id$
      17             : 
      18             : #include "AliMUONPainterInterfaceHelper.h"
      19             : 
      20             : ///\class AliMUONPainterInterfaceHelper
      21             : ///
      22             : /// Helper class to work with TGButtonGroup
      23             : ///
      24             : /// This class only works if the buttons in the TGButtonGroup have contiguous
      25             : /// ids, and if those ids start at ButtonStartingId().
      26             : /// Not bullet-proof, I admit, but this is the only way I've found with
      27             : /// the current TGButtonGroup implementation which does not allow to loop
      28             : /// easily on all buttons.
      29             : ///
      30             : // Author Laurent Aphecetche, Subatech
      31             : 
      32             : #include "AliMUONPainterEnv.h"
      33             : #include "AliMUONPainterHelper.h"
      34             : #include "AliLog.h"
      35             : #include <Riostream.h>
      36             : #include <TClass.h>
      37             : #include <TGButtonGroup.h>
      38             : #include <TGButton.h>
      39             : #include <TGClient.h>
      40             : #include <TObjArray.h>
      41             : #include <TObjString.h>
      42             : #include <TString.h>
      43             : #include <RVersion.h>
      44             : #include <cassert>
      45             : 
      46             : using std::cout;
      47             : using std::endl;
      48             : ///\cond CLASSIMP
      49          12 : ClassImp(AliMUONPainterInterfaceHelper)
      50             : ///\endcond
      51             : 
      52             : //_____________________________________________________________________________
      53           0 : AliMUONPainterInterfaceHelper::AliMUONPainterInterfaceHelper() : TObject()
      54           0 : {
      55             :   /// ctor
      56           0 : }
      57             : 
      58             : //_____________________________________________________________________________
      59             : AliMUONPainterInterfaceHelper::~AliMUONPainterInterfaceHelper()
      60           0 : {
      61             :   /// dtor
      62           0 : }
      63             : 
      64             : //_____________________________________________________________________________
      65             : void
      66             : AliMUONPainterInterfaceHelper::AddRadioButton(TGButtonGroup& bg,
      67             :                                               const TString& name,
      68             :                                               void* userData,
      69             :                                               Bool_t select)
      70             : {
      71             :   /// Add a radio button to a group
      72           0 :   Int_t n = bg.GetCount();
      73             :   
      74           0 :   TGButton* button = new TGRadioButton(&bg,
      75           0 :                                        name.Data(),
      76           0 :                                        n+ButtonStartingId());
      77           0 :   button->SetUserData(userData);
      78           0 :   button->SetOn(select,kFALSE);  
      79           0 : }
      80             : 
      81             : //_____________________________________________________________________________
      82             : void
      83             : AliMUONPainterInterfaceHelper::AddCheckButton(TGButtonGroup& bg,
      84             :                                               const TString& name,
      85             :                                               void* userData,
      86             :                                               Bool_t select)
      87             : {
      88             :   /// Add a check button to a group
      89           0 :   Int_t n = bg.GetCount();
      90             :   
      91           0 :   TGButton* button = new TGCheckButton(&bg,
      92           0 :                                        name.Data(),
      93           0 :                                        n+ButtonStartingId());
      94           0 :   button->SetUserData(userData);
      95           0 :   button->SetOn(select,kFALSE);
      96           0 : }
      97             : 
      98             : //_____________________________________________________________________________
      99             : void 
     100             : AliMUONPainterInterfaceHelper::ClearButtons(TGButtonGroup& bg)
     101             : {
     102             :   /// Remove all buttons from group.
     103             :   /// Bear in mind that you're thus disconnecting the group from
     104             :   /// any signals it might have. So you must re-connect them after
     105             :   /// a call to this method, if you want to.
     106             :   
     107           0 :   while ( bg.GetCount() > 0 )
     108             :   {
     109           0 :     TGTextButton* button = (TGTextButton*)(bg.GetButton(bg.GetCount()));
     110           0 :     if ( !button ) 
     111             :     {
     112           0 :       AliFatalClass(Form("Got a null button for bg.Count()=%d",bg.GetCount()));
     113           0 :     }
     114           0 :     bg.Remove(button);
     115             : #if ROOT_VERSION_CODE <= ROOT_VERSION(5,16,0)
     116             :     button->DestroyWindow();
     117             : #endif
     118           0 :     delete button;
     119             :   }
     120           0 : }
     121             : 
     122             : //_____________________________________________________________________________
     123             : void
     124             : AliMUONPainterInterfaceHelper::Copy(const TGButtonGroup& src, TGButtonGroup& dest)
     125             : {
     126             :   /// Copy a button group into another one
     127           0 :   ClearButtons(dest);
     128             :   
     129           0 :   dest.SetTitle(src.GetTitle());
     130             :   
     131           0 :   if ( &src != &dest )
     132             :   {
     133           0 :     for ( Int_t i = ButtonStartingId(); i < ButtonStartingId() + src.GetCount(); ++i )
     134             :     {
     135           0 :       TGTextButton* tb = static_cast<TGTextButton*>(src.GetButton(i));
     136           0 :       TGButton* button = new TGRadioButton(&dest,tb->GetTitle(),tb->WidgetId());
     137           0 :       assert(tb->WidgetId()==i);
     138           0 :       button->SetUserData(tb->GetUserData());
     139           0 :       button->SetOn(tb->IsOn(),kFALSE);
     140             :     }
     141           0 :   }
     142           0 : }
     143             : 
     144             : //_____________________________________________________________________________
     145             : void 
     146             : AliMUONPainterInterfaceHelper::Dump(const TGButtonGroup& bg)
     147             : {
     148             :   /// Printout
     149           0 :   cout << Form("ButtonGroup %s %s",bg.GetName(),bg.GetTitle()) << endl;
     150             :   
     151           0 :   for ( Int_t i = ButtonStartingId(); i < bg.GetCount() + ButtonStartingId(); ++i ) 
     152             :   {
     153           0 :     TGTextButton* button = static_cast<TGTextButton*>(bg.GetButton(i));
     154           0 :     if ( button ) 
     155             :     {
     156           0 :       cout << Form("i %2d button %s id %lu wid %d ON %d",
     157           0 :                  i,button->GetTitle(),button->GetId(),
     158           0 :                  button->WidgetId(),
     159           0 :                  button->IsOn()) << endl;
     160           0 :     }
     161             :     else
     162             :     {
     163           0 :       cout << Form("i %2d button = 0x0",i) << endl;
     164             :     }
     165             :   }
     166           0 : }
     167             : 
     168             : //_____________________________________________________________________________
     169             : TGButton* 
     170             : AliMUONPainterInterfaceHelper::FindButtonByName(const TGButtonGroup& bg, 
     171             :                                                 const TString& name)
     172             : {
     173             :   /// Find a button by name
     174             :   
     175           0 :   for ( Int_t i = ButtonStartingId(); i < ButtonStartingId() + bg.GetCount(); ++i )
     176             :   {
     177           0 :     TGTextButton* button = static_cast<TGTextButton*>(bg.GetButton(i));
     178           0 :     if (!button)
     179             :     {
     180           0 :       AliErrorClass(Form("(%s) : Something wrong",name.Data()));
     181           0 :       Dump(bg);
     182           0 :     }
     183             :     else
     184             :     {
     185           0 :       if ( name == button->GetTitle() )
     186             :       {
     187           0 :         return button;
     188             :       }
     189             :     }
     190           0 :   }
     191           0 :   return 0x0;
     192           0 : }
     193             : 
     194             : //_____________________________________________________________________________
     195             : TGButton* 
     196             : AliMUONPainterInterfaceHelper::FindButtonByUserData(const TGButtonGroup& bg, 
     197             :                                                     const void* userData)
     198             : {
     199             :   /// Find a button by userData
     200             :   
     201           0 :   for ( Int_t i = ButtonStartingId(); i < ButtonStartingId() + bg.GetCount(); ++i )
     202             :   {
     203           0 :     TGButton* button = bg.GetButton(i);
     204           0 :     if ( button->GetUserData() == userData )
     205             :     {
     206           0 :       return button;
     207             :     }
     208           0 :   }
     209           0 :   return 0x0;
     210           0 : }
     211             : 
     212             : //_____________________________________________________________________________
     213             : TGButton* 
     214             : AliMUONPainterInterfaceHelper::FindDownButton(const TGButtonGroup& bg)
     215             : {
     216             :   /// Find which button is down (in a radio group)
     217             :   
     218           0 :   for ( Int_t i = ButtonStartingId(); i < ButtonStartingId() + bg.GetCount(); ++i )
     219             :   {
     220           0 :     TGButton* button = bg.GetButton(i);
     221           0 :     if ( button->IsOn() ) 
     222             :     {
     223           0 :       return button;
     224             :     }
     225           0 :   }
     226           0 :   return 0x0;
     227           0 : }
     228             : 
     229             : 
     230             : //_____________________________________________________________________________
     231             : void 
     232             : AliMUONPainterInterfaceHelper::SetBackgroundColor(const char* resourceBaseName, 
     233             :                                                   TGWindow& window)
     234             : {
     235             :   /// Set the background color of the window
     236           0 :   TString rs(Form("%s.BackgroundColor",resourceBaseName));
     237           0 :   TString colorName = AliMUONPainterHelper::Instance()->Env()->String(rs.Data(),"#c0c0c0");
     238             : 
     239           0 :   Pixel_t color;
     240           0 :   Bool_t ok = gClient->GetColorByName(colorName, color);
     241           0 :   if ( ok ) 
     242             :   {
     243           0 :     window.SetBackgroundColor(color);
     244             :   }
     245           0 : }
     246             : 
     247             : //_____________________________________________________________________________
     248             : void 
     249             : AliMUONPainterInterfaceHelper::SetState(TGButtonGroup& bg, Bool_t state)
     250             : {
     251             :   /// should not be needed with root > 5.16/00 as there's a TGButtonGroup::SetState
     252             :   /// method now...
     253             : 
     254             : #if ROOT_VERSION_CODE > ROOT_VERSION(5,16,0)
     255           0 :   bg.SetState(state);
     256             : #else
     257             :   for ( Int_t i = ButtonStartingId(); i < ButtonStartingId() + bg.GetCount(); ++i ) 
     258             :   {
     259             :     TGTextButton* button = (TGTextButton*)(bg.GetButton(i));
     260             :     if ( state ) 
     261             :     {
     262             :       button->SetState(kButtonUp);
     263             :     }
     264             :     else
     265             :     {
     266             :       button->SetState(kButtonDisabled);
     267             :     }
     268             :   }
     269             : #endif  
     270           0 : }
     271             : 
     272             : //_____________________________________________________________________________
     273             : void 
     274             : AliMUONPainterInterfaceHelper::Select(TGButtonGroup& bg, 
     275             :                                       const TString& buttonName,
     276             :                                       Bool_t emit)
     277             : {
     278             :   /// Select which button should be on
     279             :   
     280           0 :   for ( Int_t i = ButtonStartingId(); i < ButtonStartingId() + bg.GetCount(); ++i ) 
     281             :   {
     282           0 :     TGTextButton* button = (TGTextButton*)(bg.GetButton(i));
     283           0 :     if ( buttonName == button->GetTitle() || buttonName == "*" ) 
     284             :     {
     285           0 :       if ( emit ) 
     286             :       {
     287           0 :         button->Clicked();
     288           0 :       }
     289             :       else
     290             :       {        
     291           0 :         button->SetOn(kTRUE);
     292             :       }
     293             :     }
     294             :   }  
     295           0 : }
     296             : 
     297             : //_____________________________________________________________________________
     298             : void 
     299             : AliMUONPainterInterfaceHelper::Unselect(TGButtonGroup& bg, 
     300             :                                         const TString& buttonName,
     301             :                                         Bool_t emit)
     302             : {
     303             :   /// Unselect a given button
     304             :   
     305           0 :   for ( Int_t i = ButtonStartingId(); i < ButtonStartingId() + bg.GetCount(); ++i ) 
     306             :   {
     307           0 :     TGTextButton* button = (TGTextButton*)(bg.GetButton(i));
     308           0 :     if ( buttonName == button->GetTitle() || buttonName == "*" ) 
     309             :     {
     310           0 :       if ( emit ) 
     311             :       {
     312           0 :         button->Released();
     313           0 :       }
     314             :       else
     315             :       {        
     316           0 :         button->SetOn(kFALSE);
     317             :       }
     318             :     }
     319             :   }  
     320           0 : }
     321             : 
     322             : //_____________________________________________________________________________
     323             : void
     324             : AliMUONPainterInterfaceHelper::RemoveButton(TGButtonGroup& bg,
     325             :                                             const TGButton* button)
     326             : {
     327             :   /// Remove a button
     328             :   
     329             :   // need to redo it from scratch in order not the leave holes in the
     330             :   // id numbering
     331             :   
     332           0 :   TGButtonGroup bgtmp(bg.GetParent(),bg.GetTitle());
     333             :   
     334           0 :   Int_t id(ButtonStartingId());
     335             :   
     336           0 :   for ( Int_t i = ButtonStartingId(); i < ButtonStartingId() + bg.GetCount(); ++i ) 
     337             :   {
     338           0 :     TGTextButton* b = static_cast<TGTextButton*>(bg.GetButton(i));
     339             : 
     340           0 :     if ( b != button ) 
     341             :     {
     342           0 :       TGButton* bc = new TGRadioButton(&bgtmp,b->GetTitle(),id);
     343           0 :       ++id;
     344           0 :       bc->SetUserData(b->GetUserData());
     345           0 :       bc->SetOn(b->IsOn());
     346           0 :     }    
     347             :   }    
     348             : 
     349           0 :   ClearButtons(bg);
     350             : 
     351           0 :   Copy(bgtmp,bg);
     352             :   
     353           0 :   bg.Show();
     354           0 : }

Generated by: LCOV version 1.11