]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONPainterMatrixFrame.cxx
Compilation on Windows/Cygwin. Corrected dependences
[u/mrichter/AliRoot.git] / MUON / AliMUONPainterMatrixFrame.cxx
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 "AliMUONPainterMatrixFrame.h"
19
20 #include "AliMUONPainterColorSlider.h"
21 #include "AliMUONPainterMatrix.h"
22 #include "AliMUONPainterGroup.h"
23 #include "AliMUONPainterHighlighter.h"
24 #include "AliMUONPainterInterfaceHelper.h"
25 #include "AliMUONPainterPlotSelector.h"
26 #include "AliMUONPainterRegistry.h"
27 #include "AliMUONVTrackerData.h"
28 #include "AliMUONVPainter.h"
29 #include "AliLog.h"
30 #include <Riostream.h>
31 #include <TCanvas.h>
32 #include <TEnv.h>
33 #include <TGButton.h>
34 #include <TGButtonGroup.h>
35 #include <TGFrame.h>
36 #include <TGListBox.h>
37 #include <TObjArray.h>
38 #include <TObjString.h>
39 #include <TRootEmbeddedCanvas.h>
40 #include <TString.h>
41 #include <cassert>
42 #include <float.h>
43
44 /// \class AliMUONPainterMatrixFrame
45 ///
46 /// A widget to draw a painter matrix, and the corresponding interface
47 /// to select what to outline or paint, and which part of the painter
48 /// is responding to mouse events
49 ///
50 /// \author Laurent Aphecetche, Subatech
51
52 ///\cond CLASSIMP
53 ClassImp(AliMUONPainterMatrixFrame)
54 ///\endcond
55
56 //_____________________________________________________________________________
57 AliMUONPainterMatrixFrame::AliMUONPainterMatrixFrame(const TGWindow* window, 
58                                                    UInt_t w, UInt_t h)
59 : TGCompositeFrame(window,w,h,kVerticalFrame|kDoubleBorder),
60   fPainterMatrix(0x0),
61   fView(0x0),
62   fInterface(0x0),
63   fResponderButtons(0x0),
64   fOutlineButtons(0x0),
65   fPlotSelector(0x0),
66   fPainterHighlighter(new AliMUONPainterHighlighter),
67   fCanvasWidth(0),
68   fCanvasHeight(0),
69   fMainFrame(0x0),
70   fColorSlider(0x0)
71 {
72     /// ctor
73     const Int_t kBorderSize = 10;
74
75     UInt_t wi = w - kBorderSize*0;
76     UInt_t hi = h - kBorderSize*1;
77     
78     fCanvasWidth = wi;
79     fCanvasHeight = (UInt_t)(hi*0.75);
80     
81     fMainFrame = new TGHorizontalFrame(this,fCanvasWidth,hi);
82     
83     const Int_t kColorWidth = 100;
84     
85     fColorSlider = new AliMUONPainterColorSlider(fMainFrame,kColorWidth,fCanvasHeight);
86     
87     fView = new TRootEmbeddedCanvas("ec",fMainFrame,fCanvasWidth-kColorWidth,fCanvasHeight,kChildFrame);
88     
89     fInterface = new TGHorizontalFrame(this,fCanvasWidth);
90     
91     fMainFrame->AddFrame(fView, new TGLayoutHints(kLHintsLeft));
92     fMainFrame->AddFrame(fColorSlider,new TGLayoutHints(kLHintsTop|kLHintsRight|kLHintsExpandX|kLHintsCenterY,kBorderSize/2));
93
94     AliMUONPainterInterfaceHelper::SetBackgroundColor("MatrixFrame.ColorSlider",*fColorSlider);
95     
96     fResponderButtons = new TGButtonGroup(fInterface,"Responder");
97     
98     fOutlineButtons = new TGButtonGroup(fInterface,"Outline");
99     
100     fInterface->AddFrame(fResponderButtons);
101     fInterface->AddFrame(fOutlineButtons);
102
103     fPlotSelector = 
104       new AliMUONPainterPlotSelector(fInterface);//,wi,interfaceHeight);
105     
106     fInterface->AddFrame(fPlotSelector);//,new TGLayoutHints(kLHintsRight|kLHintsExpandX));
107
108     fOutlineButtons->Show();
109     fResponderButtons->Show();
110         
111     AddFrame(fMainFrame,new TGLayoutHints(kLHintsExpandX|kLHintsTop,
112                                          0,0,0,0));
113
114     AddFrame(fInterface,new TGLayoutHints(kLHintsExpandX|kLHintsBottom,
115              0,0,kBorderSize,0));
116
117
118     // Set the connections
119     
120     fPlotSelector->Connect("DataSourceWasChanged(const char*,AliMUONVTrackerData*,Int_t)",                                
121                                  "AliMUONPainterMatrixFrame",
122                                  this,
123                                  "DataSourceWasChanged(const char*,AliMUONVTrackerData*,Int_t)");
124     
125     fColorSlider->Connect("DataRangeWasChanged(Double_t*)",
126                           "AliMUONPainterMatrixFrame",
127                          this,
128                          "DataRangeWasChanged(Double_t*)");
129
130     fColorSlider->Connect("DataRangeAutoRequested()",
131                           "AliMUONPainterMatrixFrame",
132                          this,
133                          "DataRangeAutoRequested()");
134     
135     // Set the colors (mainly for debugging frame layout)
136
137     AliMUONPainterInterfaceHelper::SetBackgroundColor("MatrixFrame.Main",*this);
138     
139     fMainFrame->HideFrame(fColorSlider);
140     
141     fMainFrame->Resize();
142 }
143
144 //_____________________________________________________________________________
145 AliMUONPainterMatrixFrame::~AliMUONPainterMatrixFrame()
146 {
147   /// dtor
148   delete fPainterHighlighter;
149   AliError("Please write a decent dtor for this class !");
150 }
151
152 //_____________________________________________________________________________
153 void
154 AliMUONPainterMatrixFrame::ChangeTitle(const TString& title)
155 {
156   /// Change title
157   
158   TitleHasChanged(title.Data());
159 }
160
161 //_____________________________________________________________________________
162 void
163 AliMUONPainterMatrixFrame::ChangeTitle(AliMUONVPainter* painter, 
164                                        const char* basename,
165                                        Double_t x, Double_t y)
166 {
167   /// Change the title according to painter
168   
169   TString name;
170   
171   if (painter) 
172   {
173
174     if ( basename ) name = basename;
175     else name = painter->PathName();
176     
177     AliMUONVPainter* master = painter->Master();
178     
179     AliMUONPainterGroup* group = master->PlotterGroup();
180
181     AliDebug(1,Form("Painter is %s plotterGroup is %x %s",
182                     painter->PathName().Data(),
183                     group,
184                     ( group ? group->Type() : "")));
185     
186     
187     if ( group && group->Data() ) 
188     {
189       name += "\n";
190       name += painter->Describe(*(group->Data()),group->DataIndex(),x,y);
191     }
192   }
193   else
194   {
195     name = fPainterMatrix->Name();
196   }
197   
198   TitleHasChanged(name.Data());
199 }
200
201 //_____________________________________________________________________________
202 void
203 AliMUONPainterMatrixFrame::Clear(Option_t*)
204 {
205   /// Clear the view(s)
206   
207   fPainterMatrix = 0x0;
208
209   AliMUONPainterInterfaceHelper::ClearButtons(*fOutlineButtons);
210   AliMUONPainterInterfaceHelper::ClearButtons(*fResponderButtons);
211   
212   fView->GetCanvas()->SetEditable(kTRUE);
213   fView->GetCanvas()->Clear();
214   fView->GetCanvas()->Modified();
215   fView->GetCanvas()->Update();
216   fView->GetCanvas()->SetEditable(kFALSE);
217
218   Layout();  
219 }
220
221 //_____________________________________________________________________________
222 void 
223 AliMUONPainterMatrixFrame::CreateButtons()
224 {
225   /// Create the interface buttons
226   
227   AliDebug(1,"");
228   
229 //  AliMUONVPainter* painter = fPainterMatrix->Painter(0);
230   
231   /// create buttons    
232   TObjArray types;
233   
234   fPainterMatrix->GetTypes(types);
235   
236   TIter nextType(&types);
237   TObjString* str;
238   
239   while ( ( str = static_cast<TObjString*>(nextType()) ) )
240   {
241     AliMUONPainterInterfaceHelper::AddRadioButton(*fResponderButtons,str->String());
242     AliMUONPainterInterfaceHelper::AddCheckButton(*fOutlineButtons,str->String());
243   }
244   
245   fOutlineButtons->Connect("Clicked(Int_t)","AliMUONPainterMatrixFrame",
246                         this,"OutlineButtonWasClicked(Int_t)");
247   
248   fResponderButtons->Connect("Clicked(Int_t)","AliMUONPainterMatrixFrame",
249                              this,"ResponderButtonWasClicked(Int_t)");        
250 }
251
252 //_____________________________________________________________________________
253 void 
254 AliMUONPainterMatrixFrame::DataRangeAutoRequested()
255 {
256   /// Get there when the "Auto" button below the color slider is clicked,
257   /// to compute the data range actually painted.
258   
259   Double_t dataMin, dataMax;
260
261   AliDebug(1,"");
262   
263   fPainterMatrix->ComputeDataRange();
264
265   fPainterMatrix->GetDataRange(dataMin,dataMax);
266   
267   AliDebug(1,Form("dataMin,Max for SetRange=%e,%e",dataMin,dataMax));
268   
269   Bool_t emit(kTRUE);
270   
271   fColorSlider->SetRange(dataMin,dataMax,emit);
272   
273   Update();
274 }
275
276 //_____________________________________________________________________________
277 void 
278 AliMUONPainterMatrixFrame::DataRangeWasChanged(Double_t* range)
279 {
280   /// Get there when the data range is changed
281   
282   AliDebug(1,Form("range=%e,%e",range[0],range[1]));
283
284   fPainterMatrix->SetDataRange(range[0],range[1]);
285   
286   Update();
287 }
288
289 //_____________________________________________________________________________
290 void
291 AliMUONPainterMatrixFrame::DataSourceWasChanged(const char* type,
292                                                AliMUONVTrackerData* data,
293                                                Int_t indexInData)
294 {
295   /// Update what to plot
296  
297   TString pattern(type);
298
299   AliDebug(1,Form("type=%s data=%s index=%d",type,
300                   (data ? data->GetName() : "null"),indexInData));
301     
302   AliMUONVTrackerData* d = data;
303   
304   if ( !d || !data || indexInData < 0 || pattern == "" )
305   {
306     pattern = "*";
307     d = 0;
308     indexInData = -1;
309   }
310   
311   fPainterMatrix->SetData(pattern,d,indexInData);
312     
313   Update();
314   
315   ChangeTitle(fPainterMatrix->Painter(0));
316 }
317
318 //_____________________________________________________________________________
319 void
320 AliMUONPainterMatrixFrame::EventInfo(Int_t event, Int_t px ,Int_t py, TObject* object)
321 {
322   /// Used to detect entering/leaving a given painter 
323   
324   if (!gPad || !object) return;
325   
326 //  cout << "EventInfo : event " << event << " px " << px << " py " << py
327 //    << " object " << object << " " << object->GetName() << endl;
328 // 
329   if ( event == 7 ) 
330   {
331     if ( object->InheritsFrom("AliMUONVPainter") )
332     {
333       AliMUONVPainter* p = static_cast<AliMUONVPainter*>(object);
334       p->ExecuteEvent(7,px,py);
335       return;
336     }      
337   }
338   
339   if ( event == kMouseLeave )
340   {
341     if ( object->InheritsFrom("AliMUONVPainter") )
342     {
343       AliMUONVPainter* p = static_cast<AliMUONVPainter*>(object);
344       MouseLeave(p);
345       fPainterHighlighter->SetPainter(0x0);
346       gPad->Modified();
347       gPad->Update();
348     }
349   }
350
351   if ( event == kMouseEnter )
352   {
353     if ( object->InheritsFrom("AliMUONVPainter") )
354     {
355       AliMUONVPainter* painter = static_cast<AliMUONVPainter*>(object);
356       if ( painter->IsResponder() && !painter->HandleMouseMotion() )
357       {
358         MouseEnter(static_cast<AliMUONVPainter*>(object));
359         fPainterHighlighter->SetPainter(painter);
360         gPad->Modified();
361         gPad->Update();
362       }
363       else if ( !painter->HandleMouseMotion() )
364       {
365         MouseEnter(static_cast<AliMUONVPainter*>(object)); 
366       }
367     }
368   }
369   
370    if ( event == kMouseMotion ) 
371   {
372     if ( object->InheritsFrom("AliMUONVPainter") )
373     {
374       AliMUONVPainter* painter = static_cast<AliMUONVPainter*>(object);
375
376       if ( painter->HandleMouseMotion() && painter->IsResponder() )
377       {
378         Double_t pos[2];
379         TVirtualPad* padsave = gPad;
380         painter->Pad()->cd();
381         painter->PixelToPad(px,py,pos[0],pos[1]);
382         MouseMotion(static_cast<AliMUONVPainter*>(object),pos);
383         fPainterHighlighter->SetPainter(painter,pos[0],pos[1]);
384         gPad->Modified();
385         gPad->Update();
386         gPad = padsave;
387       }
388     }    
389   }
390 }
391
392 //_____________________________________________________________________________
393 void
394 AliMUONPainterMatrixFrame::MouseEnter(AliMUONVPainter* painter)
395 {
396   /// Emit a signal to notify that mouse pointer is entering a given painter
397
398   AliDebug(1,Form("painter=%x %s",painter,painter->PathName().Data()));
399   
400   ChangeTitle(painter);
401
402   Long_t params[] = { (Long_t)painter };
403   
404   Emit("MouseEnter(AliMUONVPainter*)",params);  
405 }
406
407
408 //_____________________________________________________________________________
409 void
410 AliMUONPainterMatrixFrame::MouseLeave(AliMUONVPainter* painter)
411 {
412   /// Emit a signal to notify that mouse pointer is leaving a given painter
413   
414   ChangeTitle(fPainterMatrix->Name());
415
416   Long_t params[] = { (Long_t)painter };
417   
418   Emit("MouseLeave(AliMUONVPainter*)",params);
419 }
420
421 //_____________________________________________________________________________
422 void
423 AliMUONPainterMatrixFrame::MouseMotion(AliMUONVPainter* painter, Double_t* position)
424 {
425   /// Emit a signal to notify that mouse pointer is moving within a given painter
426   
427   ChangeTitle(painter,painter->NameAtPosition(position[0],position[1]),
428               position[0],position[1]);
429   
430   Long_t params[] = { (Long_t)painter, (Long_t)position };
431   
432   Emit("MouseMotion(AliMUONVPainter*,Double_t*)",params);
433 }
434
435
436 //_____________________________________________________________________________
437 void
438 AliMUONPainterMatrixFrame::ResponderButtonWasClicked(Int_t id)
439 {
440   /// One responder button was clicked
441   
442   TGTextButton* button = static_cast<TGTextButton*>(fResponderButtons->GetButton(id));
443   TString pattern = button->GetString();
444   
445 //  AliInfo(Form("id=%d button=%d %s",id,button->IsOn(),pattern.Data()));
446
447   assert(button->IsOn()==1);
448   
449   fPainterMatrix->SetResponder(pattern.Data());
450 }
451
452 //_____________________________________________________________________________
453 void
454 AliMUONPainterMatrixFrame::OutlineButtonWasClicked(Int_t id)
455 {
456   /// One outline button was clicked
457   
458   TGTextButton* button = static_cast<TGTextButton*>(fOutlineButtons->GetButton(id));
459   TString pattern = button->GetString();
460   
461   fPainterMatrix->SetOutlined(pattern.Data(),button->IsOn());
462   
463   ViewModified();
464   fView->GetCanvas()->Update();
465     
466   // Update the interface (e.g. list of possible responders can have 
467   // changed due to visibility change)
468   UpdateInterface(kFALSE);
469 }
470
471 //_____________________________________________________________________________
472 void 
473 AliMUONPainterMatrixFrame::SaveAs(const char* filename, Option_t* option) const
474 {
475   /// Save painter matrix (in the sense of "print") in filename
476   fView->GetCanvas()->SaveAs(filename,option);
477 }
478
479 //_____________________________________________________________________________
480 void
481 AliMUONPainterMatrixFrame::TitleHasChanged(const char* title)
482 {
483   /// Emit the TitleHasChanged signal
484   
485   Long_t params[] = { (Long_t)title };
486   Emit("TitleHasChanged(const char*)",params);
487 }
488
489
490 //_____________________________________________________________________________
491 void
492 AliMUONPainterMatrixFrame::Update()
493 {
494   /// Force update of all canvases
495
496   UpdateDataRange();
497   
498   fView->GetCanvas()->SetEditable(kTRUE);
499   
500   Bool_t colorSlider = ( fPainterMatrix->Data() != 0x0 );
501   
502   ViewModified();
503
504   fView->GetCanvas()->SetEditable(kFALSE);
505   
506   AliDebug(1,Form("colorSlider=%d",colorSlider));
507   
508   if ( colorSlider )
509   {
510     fMainFrame->ShowFrame(fColorSlider);
511   }
512   else
513   {
514     fMainFrame->HideFrame(fColorSlider);
515   }
516   
517   fMainFrame->Layout();
518   Layout();
519 }
520
521 //_____________________________________________________________________________
522 void
523 AliMUONPainterMatrixFrame::UpdateDataRange()
524 {
525   /// Update the data range
526   
527   Double_t min, max;
528
529   fPainterMatrix->GetDataRange(min,max);
530
531   AliDebug(1,Form("min %e max %e",min,max));
532
533   if ( min > max ) 
534   {
535     fPainterMatrix->ComputeDataRange();
536     fPainterMatrix->GetDataRange(min,max);
537   }
538
539   fColorSlider->SetRange(min,max,kFALSE);
540 }
541
542 //_____________________________________________________________________________
543 void 
544 AliMUONPainterMatrixFrame::UpdateInterface(Bool_t fromScratch)
545 {
546   /// Update the full interface
547   
548   if ( fromScratch || fOutlineButtons->GetCount() == 0 ) 
549   {
550     CreateButtons();
551   }
552   
553   AliMUONPainterInterfaceHelper::Unselect(*fResponderButtons,"*");
554   AliMUONPainterInterfaceHelper::Unselect(*fOutlineButtons,"*");
555   
556   AliMUONVPainter* painter = fPainterMatrix->Painter(0);
557   
558   TObjArray types;
559   types.SetOwner(kTRUE);
560   
561   fPainterMatrix->GetTypes(types);
562   
563   // update button states
564   TIter next(&types);
565   TObjString* otype;
566   
567   TString theResponder;
568   
569   while ( ( otype = static_cast<TObjString*>(next()) ) )
570   {    
571     AliMUONPainterGroup* group = painter->Group(otype->String());
572     
573     if ( group && group->IsOutlined() ) 
574     {
575       AliMUONPainterInterfaceHelper::Select(*fOutlineButtons,otype->String().Data());
576     }    
577   }
578   
579   if ( painter ) 
580   {
581     AliMUONPainterGroup* responderGroup = painter->ResponderGroup();
582   
583     if (responderGroup)
584     {
585       AliMUONPainterInterfaceHelper::Select(*fResponderButtons,responderGroup->Type());
586     }
587   }
588   
589   // update data source view
590   
591   fPlotSelector->Update(*fPainterMatrix);
592     
593   fResponderButtons->Show();
594   fOutlineButtons->Show();
595
596   Layout();
597 }
598
599 //_____________________________________________________________________________
600 void 
601 AliMUONPainterMatrixFrame::Use(AliMUONPainterMatrix* group)
602 {
603   /// Change the matrix used
604   
605   Clear();
606   
607   fPainterMatrix = group;
608   
609   fView->GetCanvas()->SetEditable(kTRUE);
610   
611   fView->GetCanvas()->Divide(fPainterMatrix->Nx(),fPainterMatrix->Ny());
612   
613   for ( Int_t i = 0; i < fPainterMatrix->Size(); ++i ) 
614   {
615     AliMUONVPainter* painter = fPainterMatrix->Painter(i);
616     fView->GetCanvas()->cd(i+1);
617     painter->Draw("R");
618     fPainterHighlighter->SetPainter(0x0);
619     fPainterHighlighter->Draw();
620   }  
621
622   Update();
623   
624   UpdateInterface(kTRUE);
625   
626   ChangeTitle(fPainterMatrix->Name());
627   
628   fView->GetCanvas()->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)",
629                               "AliMUONPainterMatrixFrame",this, 
630                               "EventInfo(Int_t,Int_t,Int_t,TObject*)"); 
631   
632 }
633
634 //_____________________________________________________________________________
635 void 
636 AliMUONPainterMatrixFrame::ViewModified()
637 {
638   /// Update our canvas
639   
640   for ( Int_t i = 0; i < fPainterMatrix->Size(); ++i ) 
641   {
642     fView->GetCanvas()->GetPad(i+1)->Modified();
643   }
644   fView->GetCanvas()->Modified();
645   fView->GetCanvas()->Update();
646 }
647
648