source: branches/Mars_use_drstimefiles/mbase/MEnv.cc@ 18049

Last change on this file since 18049 was 9345, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 32.2 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz 2/2005 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2009
21!
22!
23\* ======================================================================== */
24
25//////////////////////////////////////////////////////////////////////////////
26//
27// MEnv
28//
29// It is a slightly changed version of TEnv. It logs all resources which are
30// touched, so that you can print all untouched resources by
31// PrintUntouched()
32//
33// A new special resource is available. With the resource "Include"
34// you can include resources from other files, for example
35//
36// Include: file1.rc file2.rc
37//
38// Including can be done recursively. Resources from the included files
39// have lower priority. This allows to write a resource file with your
40// default resources which then can be included in other files overwriting
41// some of the resources.
42//
43// If given paths are not absolute there base is always th elocation of
44// the including file.
45//
46// Class Version 1:
47// ----------------
48// + TString fName
49//
50//////////////////////////////////////////////////////////////////////////////
51#include "MEnv.h"
52
53#include <Gtypes.h>
54#include <TObjString.h>
55#include <TObjArray.h>
56
57#include <TPave.h>
58#include <TAttText.h>
59#include <TAttMarker.h>
60#include <THashList.h> // needed since root v5.10/00 (TEnv::GetTable)
61
62#include "MLog.h"
63#include "MLogManip.h"
64
65#include "MArgs.h"
66#include "MString.h"
67
68ClassImp(MEnv);
69
70using namespace std;
71
72//---------------------------------------------------------------------------
73//
74// (Default) constructor. If the given file cannot be accessed SetRcName("")
75// is called which can then be checked by IsValid()
76//
77// The second argument is the default name. Usually as name of the MEnv
78// the base name of the file is returned. By giving a default name
79// you can overwrite this behavious. This is useful for I/O
80// because it allows to use Read and Write without argument.
81//
82MEnv::MEnv(const char *name, const char *defname) : TEnv(name), fName(defname)
83{
84 fChecked.SetOwner();
85
86 // If TEnv::TEnv has not set fRcName
87 if (!IsValid())
88 return;
89
90 // ExpandPathName (not done by TEnv::TEnv) and read again
91 TString fname(name);
92 gSystem->ExpandPathName(fname);
93
94 // Is file accessible
95 if (gSystem->AccessPathName(fname, kFileExists))
96 fname = "";
97
98 SetRcName(fname);
99
100 // No file found
101 if (fname.IsNull())
102 return;
103
104 // File has been already processed, but ReadInclude is part of a
105 // derived function, i.e. not yet executed.
106 if (GetEntries()>0 || fname==name)
107 {
108 if (ReadInclude()<0)
109 SetRcName("");
110 return;
111 }
112
113 // File not yet processed. Reread file.
114 if (ReadFile(fname, kEnvLocal)<0)
115 SetRcName("");
116}
117
118//---------------------------------------------------------------------------
119//
120// Process an Include directive and read the corresponding contents
121//
122Int_t MEnv::ReadInclude()
123{
124 // Check for "Include" resource
125 const TString incl = GetValue("Include", "");
126 if (incl.IsNull())
127 return 0;
128
129 const char *dir = gSystem->DirName(GetRcName());
130
131 // Tokenize the array into single files divided by a whitespace
132 TObjArray *arr = incl.Tokenize(" ");
133
134 // We have to rebuild the Include array from scratch to get the
135 // correct sorting for a possible rereading.
136 SetValue("Include", "");
137
138 // FIXME: Make sure that recursions don't crash the system!
139
140 for (int i=0; i<arr->GetEntries(); i++)
141 {
142 // Get file name to include
143 TString fenv = (*arr)[i]->GetName();
144
145 // If the is not anabsolute path we prepend the dir-name
146 // of the including file. This allows that includes
147 // do not necessarily need absolute paths and paths are always
148 // relative to the location of th eincluding file.
149 if (!gSystem->IsAbsoluteFileName(fenv))
150 {
151 fenv.Prepend("/");
152 fenv.Prepend(dir);
153 }
154
155 // Read included file and check if its valid
156 const MEnv env(fenv);
157 if (!env.IsValid())
158 {
159 delete arr;
160 return -1;
161 }
162
163 // Add file name before its childs
164 SetValue("+Include", fenv);
165
166 // If it is valid add entries from include without overwriting,
167 // i.e. the included resources have lower priority
168 AddEnv(env, kFALSE);
169
170 // Get a possible child include from env
171 const TString incl2 = const_cast<MEnv&>(env).GetValue("Include", "");
172 if (!incl2.IsNull())
173 SetValue("+Include", incl2);
174 }
175
176 delete arr;
177
178 // Get final compiled resource
179 TString inc = GetValue("Include", "");
180
181 // Remove obsolete whitespaces for convinience
182 inc.ReplaceAll(" ", " ");
183 inc = inc.Strip(TString::kBoth);
184
185 // Set final resource, now as kEnvLocal (previously set as kEnvChanged)
186 SetValue("Include", inc, kEnvLocal);
187
188 // FIXME: Remove douplets in the correct order
189
190 return 0;
191}
192
193//---------------------------------------------------------------------------
194//
195// Read and parse the resource file for a certain level.
196// Returns -1 on case of error, 0 in case of success.
197//
198// Check for an include directive
199//
200Int_t MEnv::ReadFile(const char *fname, EEnvLevel level)
201{
202 // First read the file via TEnv
203 if (TEnv::ReadFile(fname, level)<0)
204 return -1;
205
206 return ReadInclude();
207}
208
209// --------------------------------------------------------------------------
210//
211// Read an object from the current directory. If no name is given
212// the name of this object is used.
213//
214Int_t MEnv::Read(const char *name)
215{
216 const Int_t rc = TEnv::Read(name?name:(const char*)fName);
217 //if (name)
218 // SetName(name);
219 return rc;
220}
221
222//---------------------------------------------------------------------------
223//
224// Make sure that the name used for writing doesn't contain a full path
225//
226const char *MEnv::GetName() const
227{
228 if (!fName.IsNull())
229 return fName;
230
231 const char *pos = strrchr(GetRcName(), '/');
232 return pos>0 ? pos+1 : GetRcName();
233}
234
235//---------------------------------------------------------------------------
236//
237// Return the total number of entries in the table
238//
239Int_t MEnv::GetEntries() const
240{
241 if (!GetTable())
242 return -1;
243
244 return GetTable()->GetEntries();
245}
246
247//---------------------------------------------------------------------------
248//
249// Compile str+post and make sure that in between there is a unique dot.
250//
251TString MEnv::Compile(TString str, const char *post) const
252{
253 if (!str.IsNull() && !str.EndsWith("."))
254 str += ".";
255
256 str += post;
257
258 return str;
259}
260
261//---------------------------------------------------------------------------
262//
263// Get the value from the table and remember the value as checked
264//
265Int_t MEnv::GetValue(const char *name, Int_t dflt)
266{
267 if (!fChecked.FindObject(name))
268 fChecked.Add(new TObjString(name));
269 return TEnv::GetValue(name, dflt);
270}
271
272//---------------------------------------------------------------------------
273//
274// Get the value from the table and remember the value as checked
275//
276Double_t MEnv::GetValue(const char *name, Double_t dflt)
277{
278 if (!fChecked.FindObject(name))
279 fChecked.Add(new TObjString(name));
280 return TEnv::GetValue(name, dflt);
281}
282
283//---------------------------------------------------------------------------
284//
285// Get the value from the table and remember the value as checked
286//
287const char *MEnv::GetValue(const char *name, const char *dflt)
288{
289 if (!fChecked.FindObject(name))
290 fChecked.Add(new TObjString(name));
291 return TEnv::GetValue(name, dflt);
292}
293
294//---------------------------------------------------------------------------
295//
296// TEnv doen't have a streamer --> cannot be cloned
297// --> we have to clone it ourself
298//
299TObject *MEnv::Clone(const char *) const
300{
301 MEnv *env = new MEnv("/dev/null");
302 env->SetRcName(GetRcName());
303 env->AddEnv(*this);
304 return env;
305}
306
307//---------------------------------------------------------------------------
308//
309// Interprete fill style: Hollow, Solid, Hatch, 0%-100%
310// If no text style is detected the value is converted to an integer.
311//
312Int_t MEnv::GetFillStyle(const char *name, Int_t dftl)
313{
314 TString str = GetValue(name, "");
315 str = str.Strip(TString::kBoth);
316 if (str.IsNull())
317 return dftl;
318
319 str.ToLower();
320
321 switch (str.Hash())
322 {
323 case 2374867578U: return 0; // hollow
324 case 764279305U: return 1001; // solid
325 case 1854683492U: return 2001; // hatch
326 }
327
328 return str.EndsWith("%") ? 4000+str.Atoi() : str.Atoi();
329}
330
331//---------------------------------------------------------------------------
332//
333// Interprete line style: Solid, Dashed, Dotted, DashDotted
334// If no line style is detected the value is converted to an integer.
335//
336Int_t MEnv::GetLineStyle(const char *name, Int_t dftl)
337{
338 TString str = GetValue(name, "");
339 str = str.Strip(TString::kBoth);
340 if (str.IsNull())
341 return dftl;
342
343 str.ToLower();
344
345 switch (str.Hash())
346 {
347 case 764279305U: return kSolid;
348 case 241979881U: return kDashed;
349 case 2391642602U: return kDotted;
350 case 1124931659U: return kDashDotted;
351 }
352
353 return str.Atoi();
354}
355
356//---------------------------------------------------------------------------
357//
358// Interprete alignment: Top, Right, Left, Bottom, Center, tr, cc, bl, ...
359// If no text align is detected the value is converted to an integer.
360//
361// eg.
362// Top Right
363// Bottom Center
364// Center
365// tr
366// br
367// cr
368//
369Int_t MEnv::GetAlign(const char *name, Int_t dftl)
370{
371 TString str = GetValue(name, "");
372 str = str.Strip(TString::kBoth);
373 if (str.IsNull())
374 return dftl;
375
376 str.ToLower();
377
378 switch (str.Hash())
379 {
380 case 29746: return 33; // tr
381 case 25379: return 22; // cc
382 case 25132: return 11; // bl
383
384 case 25388: return 12; // cl
385 case 25394: return 32; // cr
386
387 case 29731: return 23; // tc
388 case 25123: return 32; // bc
389
390 case 29740: return 13; // tl
391 case 25138: return 13; // br
392 }
393
394 Int_t align = 0;
395 if (str.Contains("right", TString::kIgnoreCase))
396 align += 3;
397 if (str.Contains("left", TString::kIgnoreCase))
398 align += 1;
399 if (str.Contains("bottom", TString::kIgnoreCase))
400 align += 10;
401 if (str.Contains("top", TString::kIgnoreCase))
402 align += 30;
403
404 if (str.Contains("center", TString::kIgnoreCase))
405 {
406 if (align==0)
407 return 22;
408 if (align/10==0)
409 return align+20;
410 if (align%10==0)
411 return align+2;
412 }
413
414 return align>0 ? align : str.Atoi();
415}
416
417//---------------------------------------------------------------------------
418//
419// Interprete color: Black, White, Red, Green, Blue, Yellow, Magenta,
420// Cyan, Gray1-5, Grey1-5.
421// If no text color is detected the value is converted to an integer.
422//
423// eg.
424// Red
425// Light Red
426// Dark Red
427//
428Int_t MEnv::GetColor(const char *name, Int_t dftl)
429{
430 TString str = GetValue(name, "");
431
432 str = str.Strip(TString::kBoth);
433 if (str.IsNull())
434 return dftl;
435
436 str.ToLower();
437
438 Int_t offset=0;
439 if (str.Contains("dark"))
440 {
441 str.ReplaceAll("dark", "");
442 str = str.Strip(TString::kBoth);
443 offset = 100;
444 }
445 if (str.Contains("light"))
446 {
447 str.ReplaceAll("light", "");
448 str = str.Strip(TString::kBoth);
449 offset = 150;
450 }
451
452 switch (str.Hash())
453 {
454 case 2368543371U: return kWhite+offset;
455 case 1814927399U: return kBlack+offset;
456 case 7496964U: return kRed+offset;
457 case 2897107074U: return kGreen+offset;
458 case 1702194402U: return kBlue+offset;
459 case 2374817882U: return kYellow+offset;
460 case 2894218701U: return kMagenta+offset;
461 case 1851881955U: return kCyan+offset;
462 case 749623518U: return 19; // grey1
463 case 749623517U: return 18; // grey2
464 case 749623516U: return 17; // grey3
465 case 749623515U: return 16; // grey4
466 case 749623514U: return 15; // grey5
467 case 749623513U: return 14; // grey6
468 case 749623512U: return 13; // grey7
469 case 749623511U: return 12; // grey8
470 case 741234910U: return 19; // gray1
471 case 741234909U: return 18; // gray2
472 case 741234908U: return 17; // gray3
473 case 741234907U: return 16; // gray4
474 case 741234906U: return 15; // gray5
475 case 741234905U: return 14; // gray6
476 case 741234904U: return 13; // gray7
477 case 741234903U: return 12; // gray8
478 }
479 return str.Atoi();
480}
481
482//---------------------------------------------------------------------------
483//
484// As possible convert the color col into a text string which can be
485// interpreted by GetColor before setting the resource value
486//
487void MEnv::SetColor(const char *name, Int_t col)
488{
489 TString val;
490
491 if (col>99 && col<101+kCyan)
492 {
493 val = "Dark ";
494 col -= 100;
495 }
496 if (col>150 && col<151+kCyan)
497 {
498 val = "Light ";
499 col -= 150;
500 }
501
502 switch (col)
503 {
504 case kWhite: val += "White"; break;
505 case kBlack: val += "Black"; break;
506 case kRed: val += "Red"; break;
507 case kGreen: val += "Green"; break;
508 case kBlue: val += "Blue"; break;
509 case kYellow: val += "Yellow"; break;
510 case kMagenta: val += "Magenta"; break;
511 case kCyan: val += "Cyan"; break;
512 case 19: val += "Grey1"; break;
513 case 18: val += "Grey2"; break;
514 case 17: val += "Grey3"; break;
515 case 16: val += "Grey4"; break;
516 case 15: val += "Grey5"; break;
517 case 14: val += "Grey6"; break;
518 case 13: val += "Grey7"; break;
519 case 12: val += "Grey8"; break;
520 }
521
522 if (val.IsNull())
523 val += col;
524
525 SetValue(name, val);
526}
527
528//---------------------------------------------------------------------------
529//
530// As possible convert the alignment align into a text string which can be
531// interpreted by GetAlign before setting the resource value
532//
533void MEnv::SetAlign(const char *name, Int_t align)
534{
535 TString val;
536 if (align==22)
537 {
538 SetValue(name, "Center");
539 return;
540 }
541
542 switch (align%10)
543 {
544 case 1: val += "Left"; break;
545 case 2: val += "Center"; break;
546 case 3: val += "Right"; break;
547 }
548
549 switch (align/10)
550 {
551 case 1: val += "Bottom"; break;
552 case 2: val += "Center"; break;
553 case 3: val += "Top"; break;
554 }
555
556 SetValue(name, val);
557}
558
559//---------------------------------------------------------------------------
560//
561// As possible convert the fill style style into a text string which can be
562// interpreted by GetFillStyle before setting the resource value
563//
564void MEnv::SetFillStyle(const char *name, Int_t style)
565{
566 TString val;
567
568 if (style>3999 && style<4101)
569 val = MString::Format("%d%%", style-4000);
570
571 switch (style)
572 {
573 case 0: val = "Hollow"; break;
574 case 1001: val = "Solid"; break;
575 case 2001: val = "Hatch"; break;
576 }
577
578 if (val.IsNull())
579 val += style;
580
581 SetValue(name, val);
582}
583
584//---------------------------------------------------------------------------
585//
586// As possible convert the line style style into a text string which can be
587// interpreted by GetLineStyle before setting the resource value
588//
589void MEnv::SetLineStyle(const char *name, Int_t style)
590{
591 TString val;
592 switch (style)
593 {
594 case kSolid: val = "Solid"; break;
595 case kDashed: val = "Dashed"; break;
596 case kDotted: val = "Dotted"; break;
597 case kDashDotted: val = "DashDotted"; break;
598 }
599
600 if (val.IsNull())
601 val += style;
602
603 SetValue(name, val);
604}
605
606//---------------------------------------------------------------------------
607//
608// As possible convert the marker style style into a text string which can be
609// interpreted by GetLineStyle before setting the resource value
610//
611void MEnv::SetMarkerStyle(const char *name, Int_t style)
612{
613 TString val;
614 switch (style)
615 {
616 case kDot: val = "dot";
617 case kPlus: val = "plus";
618 case kCircle: val = "circle";
619 case kMultiply: val = "multiply";
620 case kFullDotSmall: val = "fulldotsmall";
621 case kFullDotMedium: val = "fulldotmedium";
622 case kFullDotLarge: val = "fulldotlarge";
623// case kOpenTriangleDown: val = "opentriangledown";
624// case kFullCross: val = "fullcross";
625 case kFullCircle: val = "fullcircle";
626 case kFullSquare: val = "fullsquare";
627 case kFullTriangleDown: val = "fulltriangledown";
628 case kOpenCircle: val = "opencircle";
629 case kOpenSquare: val = "opensquare";
630 case kOpenTriangleUp: val = "opentriangleup";
631 case kOpenDiamond: val = "opendiamond";
632 case kOpenCross: val = "opencross";
633 case kFullStar: val = "fullstar";
634 case kOpenStar: val = "openstar";
635 }
636
637 if (val.IsNull())
638 val += style;
639
640 SetValue(name, val);
641}
642
643//---------------------------------------------------------------------------
644//
645// Get the attributed from a TAttLine (if dftl is given use it as default)
646// name.LineColor <see also GetColor>
647// name.LineStyle
648// name.LineWidth
649// For more details on the meaning see TAttLine
650//
651void MEnv::GetAttLine(const char *name, TAttLine &line, TAttLine *dftl)
652{
653 const TString color = Compile(name, "LineColor");
654 const TString style = Compile(name, "LineStyle");
655 const TString width = Compile(name, "LineWidth");
656
657 if (!dftl)
658 dftl = &line;
659
660 const Color_t col = GetColor(color, dftl->GetLineColor());
661 const Style_t sty = GetLineStyle(style, dftl->GetLineStyle());
662 const Style_t wid = GetValue(width, dftl->GetLineWidth());
663
664 line.SetLineColor(col);
665 line.SetLineStyle(sty);
666 line.SetLineWidth(wid);
667}
668
669//---------------------------------------------------------------------------
670//
671// Get the attributed from a TAttText (if dftl is given use it as default)
672// name.TextColor <see also GetColor>
673// name.TextAlign <see also GetAlign>
674// name.TextAngle
675// name.TextFont
676// name.TextSize
677// For more details on the meaning see TAttText
678//
679void MEnv::GetAttText(const char *name, TAttText &text, TAttText *dftl)
680{
681 const TString color = Compile(name, "TextColor");
682 const TString align = Compile(name, "TextAlign");
683 const TString angle = Compile(name, "TextAngle");
684 const TString font = Compile(name, "TextFont");
685 const TString size = Compile(name, "TextSize");
686
687 if (!dftl)
688 dftl = &text;
689
690 const Color_t col = GetColor(color, dftl->GetTextColor());
691 const Short_t ali = GetAlign(align, dftl->GetTextAlign());
692 const Float_t ang = GetValue(angle, dftl->GetTextAngle());
693 const Font_t fon = GetValue(font, dftl->GetTextFont());
694 const Float_t siz = GetValue(size, dftl->GetTextSize());
695
696 text.SetTextColor(col);
697 text.SetTextAlign(ali);
698 text.SetTextAngle(ang);
699 text.SetTextFont(fon);
700 text.SetTextSize(siz);
701}
702
703//---------------------------------------------------------------------------
704//
705// Get the attributed from a TAttFill (if dftl is given use it as default)
706// name.FillColor <see also GetColor>
707// name.FillStyle <see also GetFillStyle>
708// For more details on the meaning see TAttFill
709//
710void MEnv::GetAttFill(const char *name, TAttFill &fill, TAttFill *dftl)
711{
712 const TString color = Compile(name, "FillColor");
713 const TString style = Compile(name, "FillStyle");
714
715 if (!dftl)
716 dftl = &fill;
717
718 const Color_t col = GetColor(color, dftl->GetFillColor());
719 const Style_t sty = GetFillStyle(style, dftl->GetFillStyle());
720
721 fill.SetFillColor(col);
722 fill.SetFillStyle(sty);
723}
724
725//---------------------------------------------------------------------------
726//
727// Get the attributed from a TAttMarker (if dftl is given use it as default)
728// name.MarkerColor <see also GetColor>
729// name.MarkerStyle
730// name.MarkerSize
731// For more details on the meaning see TAttMarker
732//
733void MEnv::GetAttMarker(const char *name, TAttMarker &marker, TAttMarker *dftl)
734{
735 const TString color = Compile(name, "MarkerColor");
736 const TString style = Compile(name, "MarkerStyle");
737 const TString size = Compile(name, "MarkerSize");
738
739 if (!dftl)
740 dftl = &marker;
741
742 const Color_t col = GetColor(color, dftl->GetMarkerColor());
743 const Style_t sty = GetValue(style, dftl->GetMarkerStyle());
744 const Size_t siz = GetValue(size, dftl->GetMarkerSize());
745
746 marker.SetMarkerColor(col);
747 marker.SetMarkerStyle(sty);
748 marker.SetMarkerSize(siz);
749}
750
751//---------------------------------------------------------------------------
752//
753// Get the attributed from a TPave (if dftl is given use it as default)
754// name.CornerRadius
755// name.BorderSize
756// name.Option
757// Also all resources from TAttLine and TAttFill are supported.
758//
759// For your conveinience: If the CornerRadius is greater than 0 "arc" is
760// added to the options. If it is equal or less than 0 "arc" is removed
761// from the options.
762//
763// For more details on the meaning see TPave
764//
765void MEnv::GetAttPave(const char *str, TPave &pave, TPave *dftl)
766{
767 const TString post(str);
768
769 TString name(pave.GetName());
770 if (!name.IsNull() && name!=pave.ClassName())
771 name = Compile(name, post);
772
773 GetAttLine(name, pave, dftl);
774 GetAttFill(name, pave, dftl);
775
776 const TString corner = Compile(name, "CornerRadius");
777 const TString border = Compile(name, "BorderSize");
778 const TString option = Compile(name, "Option");
779
780 if (!dftl)
781 dftl = &pave;
782
783 const Double_t cor = GetValue(corner, dftl->GetCornerRadius());
784 const Int_t bor = GetValue(border, dftl->GetBorderSize());
785
786 pave.SetCornerRadius(cor);
787 pave.SetBorderSize(bor);
788
789 TString opt = GetValue(option, dftl->GetOption());
790 opt.ToLower();
791
792 const Bool_t has = pave.GetCornerRadius()>0;
793
794 if (has && !opt.Contains("arc"))
795 opt += "arc";
796
797 if (!has && opt.Contains("arc"))
798 opt.ReplaceAll("arc", "");
799
800 pave.SetOption(opt);
801
802}
803
804//---------------------------------------------------------------------------
805//
806// Get the attributed for the TObject obj. Use dftl for default attributes
807// if given.
808//
809// There is support for:
810// TPave <see GetAttPave>
811// TAttLine <see GetAttLine>
812// TAttText <see GetAttText>
813// TAttFill <see GetAttFill>
814// TAttMarker <see GetAttMarker>
815//
816void MEnv::GetAttributes(const char *name, TObject *obj, TObject *dftl)
817{
818 //TAttAxis *line = dynamic_cast<TAttAxis*>(obj);
819 //TAtt3D *line = dynamic_cast<TAtt3D*>(obj);
820 //TAttCanvas *line = dynamic_cast<TAttCanvas*>(obj);
821 //TAttFillCanvas *line = dynamic_cast<TAttFillEitor*>(obj);
822 //TAttLineCanvas *line = dynamic_cast<TAttLineCanvas*>(obj);
823 //TAttLineEditor *line = dynamic_cast<TAttLineEditor*>(obj);
824 //TAttMarkerCanvas *line = dynamic_cast<TAttMarkerCanvas*>(obj);
825 //TAttMarkerEditor *line = dynamic_cast<TAttMarkerEditor*>(obj);
826 //TAttPad *line = dynamic_cast<TAttPad*>(obj);
827 //TAttParticle *line = dynamic_cast<TAttParticle*>(obj);
828 //TAttTextCanvas *line = dynamic_cast<TAttTextCanvas*>(obj);
829 //TAttTextEditor *line = dynamic_cast<TAttTextEditor*>(obj);
830
831 TPave *pave = dynamic_cast<TPave*>(obj);
832 TAttLine *line = dynamic_cast<TAttLine*>(obj);
833 TAttText *text = dynamic_cast<TAttText*>(obj);
834 TAttFill *fill = dynamic_cast<TAttFill*>(obj);
835 TAttMarker *mark = dynamic_cast<TAttMarker*>(obj);
836
837 if (pave)
838 {
839 GetAttPave(name, *pave, dynamic_cast<TPave*>(dftl));
840 return;
841 }
842
843 if (line)
844 GetAttLine(name, *line, dynamic_cast<TAttLine*>(dftl));
845 if (text)
846 GetAttText(name, *text, dynamic_cast<TAttText*>(dftl));
847 if (fill)
848 GetAttFill(name, *fill, dynamic_cast<TAttFill*>(dftl));
849 if (mark)
850 GetAttMarker(name, *mark, dynamic_cast<TAttMarker*>(dftl));
851}
852
853//---------------------------------------------------------------------------
854//
855// Set the resources from a TAttLine:
856// name.LineColor <see also SetColor>
857// name.LineStyle
858// name.LineWidth
859//
860void MEnv::SetAttLine(const char *name, const TAttLine &line)
861{
862 const TString color = Compile(name, "LineColor");
863 const TString style = Compile(name, "LineStyle");
864 const TString width = Compile(name, "LineWidth");
865
866 SetColor(color, line.GetLineColor());
867 SetLineStyle(style, line.GetLineStyle());
868 SetValue(width, line.GetLineWidth());
869}
870
871//---------------------------------------------------------------------------
872//
873// Set the resources from a TAttText:
874// name.TextColor <see also SetColor>
875// name.TextAlign <see also SetAlign>
876// name.TextAngle
877// name.TextFont
878// name.TextSize
879//
880void MEnv::SetAttText(const char *name, const TAttText &text)
881{
882 const TString color = Compile(name, "TextColor");
883 const TString align = Compile(name, "TextAlign");
884 const TString angle = Compile(name, "TextAngle");
885 const TString font = Compile(name, "TextFont");
886 const TString size = Compile(name, "TextSize");
887
888 SetColor(color, text.GetTextColor());
889 SetAlign(align, text.GetTextAlign());
890 SetValue(angle, text.GetTextAngle());
891 SetValue(font, text.GetTextFont());
892 SetValue(size, text.GetTextSize());
893}
894
895//---------------------------------------------------------------------------
896//
897// Set the resources from a TAttFill:
898// name.FillColor <see also SetColor>
899// name.FillStyle <see also SetFillStyle>
900//
901void MEnv::SetAttFill(const char *name, const TAttFill &fill)
902{
903 const TString color = Compile(name, "FillColor");
904 const TString style = Compile(name, "FillStyle");
905
906 SetColor(color, fill.GetFillColor());
907 SetFillStyle(style, fill.GetFillStyle());
908}
909
910//---------------------------------------------------------------------------
911//
912// Set the resources from a TAttMarker:
913// name.MarkerColor <see also SetColor>
914// name.MarkerStyle
915// name.MarkerSize
916//
917void MEnv::SetAttMarker(const char *name, const TAttMarker &marker)
918{
919 const TString color = Compile(name, "MarkerColor");
920 const TString style = Compile(name, "MarkerStyle");
921 const TString size = Compile(name, "MarkerSize");
922
923 SetColor(color, marker.GetMarkerColor());
924 SetMarkerStyle(style, marker.GetMarkerStyle());
925 SetValue(size, marker.GetMarkerSize());
926}
927
928//---------------------------------------------------------------------------
929//
930// Set the resources from a TPave:
931// name.CornerRadius
932// name.BorderSize
933// name.Option
934// Also all resources from TAttLine and TAttFill are supported.
935//
936void MEnv::SetAttPave(const char *str, const TPave &pave)
937{
938 const TString name(str);
939
940 SetAttLine(name, pave);
941 SetAttFill(name, pave);
942
943 const TString corner = Compile(name, "CornerRadius");
944 const TString border = Compile(name, "BorderSize");
945 const TString option = Compile(name, "Option");
946
947 SetValue(corner, const_cast<TPave&>(pave).GetCornerRadius());
948 SetValue(border, const_cast<TPave&>(pave).GetBorderSize());
949 SetValue(option, pave.GetOption());
950}
951
952//---------------------------------------------------------------------------
953//
954// Set the attributed for the TObject obj.
955//
956// There is support for:
957// TPave <see SetAttPave>
958// TAttLine <see SetAttLine>
959// TAttText <see SetAttText>
960// TAttFill <see SetAttFill>
961// TAttMarker <see SetAttMarker>
962//
963void MEnv::SetAttributes(const char *name, const TObject *obj)
964{
965 const TPave *pave = dynamic_cast<const TPave*>(obj);
966 const TAttLine *line = dynamic_cast<const TAttLine*>(obj);
967 const TAttText *text = dynamic_cast<const TAttText*>(obj);
968 const TAttFill *fill = dynamic_cast<const TAttFill*>(obj);
969 const TAttMarker *mark = dynamic_cast<const TAttMarker*>(obj);
970
971 if (pave)
972 {
973 SetAttPave(name, *pave);
974 return;
975 }
976
977 if (line)
978 SetAttLine(name, *line);
979 if (text)
980 SetAttText(name, *text);
981 if (fill)
982 SetAttFill(name, *fill);
983 if (mark)
984 SetAttMarker(name, *mark);
985}
986
987//---------------------------------------------------------------------------
988//
989// Add all values from TEnv env the this MEnv. To not overwrite existing
990// values set overwrite to kFALSE
991//
992void MEnv::AddEnv(const TEnv &env, Bool_t overwrite)
993{
994 if (!GetTable() || !env.GetTable())
995 return;
996
997 TIter Next(env.GetTable());
998
999 TEnvRec *er;
1000 while ((er = (TEnvRec*)Next()))
1001 {
1002 if (overwrite || !Defined(er->GetName()))
1003 SetValue(er->GetName(), er->GetValue(), er->GetLevel(), er->GetType());
1004 }
1005}
1006
1007//---------------------------------------------------------------------------
1008//
1009// Check MArgs for all options "--rc=" and remove them. Options should be
1010// given like
1011//
1012// program --rc=Option1:Test1 --rc=Option2.SubOption:Test2
1013//
1014// If all resources could be interpeted corrctly kTRUE is returned. If
1015// there were problems kFALSE is returned.
1016//
1017Bool_t MEnv::TakeEnv(MArgs &arg, Bool_t print, Bool_t overwrite)
1018{
1019 if (!GetTable())
1020 {
1021 gLog << err << "ERROR - MEnv not yet initialized." << endl;
1022 return kFALSE;
1023 }
1024
1025 Bool_t ret = kTRUE;
1026 while (1)
1027 {
1028 const TString rc = arg.GetStringAndRemove("--rc=");
1029 if (rc.IsNull())
1030 break;
1031
1032 const Ssiz_t pos = rc.First(':');
1033 if (pos<0)
1034 {
1035 gLog << warn << "WARNING - Resource '" << rc << "' doesn't contain a colon... ignored." << endl;
1036 ret=kFALSE;
1037 continue;
1038 }
1039 if (pos==0)
1040 {
1041 gLog << warn << "WARNING - Resource '" << rc << "' doesn't contain a name... ignored." << endl;
1042 ret=kFALSE;
1043 continue;
1044 }
1045 if (pos==rc.Length()-1)
1046 {
1047 gLog << warn << "WARNING - Resource '" << rc << "' empty... ignored." << endl;
1048 ret=kFALSE;
1049 continue;
1050 }
1051
1052 const TString name = rc(0, pos);
1053 const TString val = rc(pos+1, rc.Length());
1054
1055 if (print)
1056 gLog << all << "Command line resource '" << name << "' with value '" << val << "'...";
1057
1058 const Bool_t exists = Defined(name);
1059 if (!exists)
1060 {
1061 SetValue(name, val, kEnvLocal);
1062 if (print)
1063 gLog << "set." << endl;
1064 continue;
1065 }
1066
1067 if (overwrite)
1068 {
1069 SetValue(name, "");
1070 SetValue(name, val, kEnvLocal);
1071 if (print)
1072 gLog << "changed." << endl;
1073 continue;
1074 }
1075
1076 if (print)
1077 gLog << "skipped/existing." << endl;
1078 }
1079 return ret;
1080}
1081
1082//---------------------------------------------------------------------------
1083//
1084// Add name and full path to output
1085//
1086void MEnv::PrintEnv(EEnvLevel level) const
1087{
1088 if (!TString(GetRcName()).IsNull())
1089 {
1090 cout << "# Path: " << GetRcName() << endl;
1091 cout << "# File: " << gSystem->BaseName(GetRcName()) << endl;
1092 }
1093 if (!fName.IsNull())
1094 cout << "# Name: " << fName << endl;
1095
1096 TEnv::PrintEnv(level);
1097}
1098
1099//---------------------------------------------------------------------------
1100//
1101// Print resources which have never been touched (for debugging)
1102//
1103void MEnv::PrintUntouched() const
1104{
1105 int i=0;
1106 gLog << inf << flush;
1107
1108 TString sep = "Untouched Resources in ";
1109 sep += GetRcName();
1110 gLog.Separator(sep);
1111 TIter Next(GetTable());
1112 TObject *o=0;
1113
1114 while ((o=Next()))
1115 if (!fChecked.FindObject(o->GetName()))
1116 {
1117 gLog << warn << " - Resource " << o->GetName() << " untouched" << endl;
1118 i++;
1119 }
1120 if (i==0)
1121 gLog << inf << "None." << endl;
1122 else
1123 gLog << inf << i << " resources have not been touched." << endl;
1124}
1125
1126//---------------------------------------------------------------------------
1127//
1128// Return number of resources which have not been touched.
1129//
1130Int_t MEnv::GetNumUntouched() const
1131{
1132 int i=0;
1133 TIter Next(GetTable());
1134 TObject *o=0;
1135 while ((o=Next()))
1136 if (!fChecked.FindObject(o->GetName()))
1137 i++;
1138 return i;
1139}
Note: See TracBrowser for help on using the repository browser.