Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TObjectDrawable.cxx
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (C) 1995-2017, Rene Brun and Fons Rademakers. *
3 * All rights reserved. *
4 * *
5 * For the licensing terms see $ROOTSYS/LICENSE. *
6 * For the list of contributors see $ROOTSYS/README/CREDITS. *
7 *************************************************************************/
8
10
11#include <ROOT/RDisplayItem.hxx>
12#include <ROOT/RLogger.hxx>
13#include <ROOT/RMenuItems.hxx>
14
15#include "TArrayI.h"
16#include "TColor.h"
17#include "TObjArray.h"
18#include "TObjString.h"
19#include "TROOT.h"
20#include "TStyle.h"
21
22#include <exception>
23#include <sstream>
24#include <iostream>
25
26using namespace ROOT::Experimental;
27
28TObjectDrawable::TObjectDrawable(EKind kind, bool persistent) : RDrawable("tobject"), fKind(kind)
29{
30 if (!persistent) return;
31
32 fOpts = "persistent";
33 fObj = CreateSpecials(kind);
34}
35
36////////////////////////////////////////////////////////////////////
37/// Convert TColor to RGB string for using with SVG
38
40{
41 static TString code;
42
43 if (col->GetAlpha() == 1)
44 code.Form("rgb(%d,%d,%d)", (int) (255*col->GetRed()), (int) (255*col->GetGreen()), (int) (255*col->GetBlue()));
45 else
46 code.Form("rgba(%d,%d,%d,%5.3f)", (int) (255*col->GetRed()), (int) (255*col->GetGreen()), (int) (255*col->GetBlue()), col->GetAlpha());
47
48 return code.Data();
49}
50
51////////////////////////////////////////////////////////////////////
52/// Create instance of requested special object
53
54std::unique_ptr<TObject> TObjectDrawable::CreateSpecials(int kind)
55{
56 switch (kind) {
57 case kColors: {
58 // convert list of colors into strings
59 auto arr = std::make_unique<TObjArray>();
60 arr->SetOwner(kTRUE);
61
62 auto cols = gROOT->GetListOfColors();
63 for (int n = 0; n <= cols->GetLast(); ++n) {
64 auto col = dynamic_cast<TColor *>(cols->At(n));
65 if (!col) continue;
66 auto code = TString::Format("%d=%s", n, GetColorCode(col));
67 arr->Add(new TObjString(code));
68 }
69
70 return arr;
71 }
72 case kStyle: { // create copy of gStyle
73 return std::make_unique<TStyle>(*gStyle);
74 }
75 case kPalette: { // copy color palette
76
77 auto arr = std::make_unique<TObjArray>();
78 arr->SetOwner(kTRUE);
79
80 auto palette = TColor::GetPalette();
81 for (int n = 0; n < palette.GetSize(); ++n) {
82 auto col = gROOT->GetColor(palette[n]);
83 arr->Add(new TObjString(GetColorCode(col)));
84 }
85
86 return arr;
87 }
88 }
89 return nullptr;
90}
91
92
93////////////////////////////////////////////////////////////////////
94/// Create display item which will be delivered to the client
95
96std::unique_ptr<RDisplayItem> TObjectDrawable::Display(const RDisplayContext &ctxt)
97{
98 if (GetVersion() > ctxt.GetLastVersion()) {
99 if ((fKind == kObject) || (fOpts == "persistent"))
100 return std::make_unique<TObjectDisplayItem>(fKind, fObj.get(), fOpts);
101
102 auto specials = CreateSpecials(fKind);
103 return std::make_unique<TObjectDisplayItem>(fKind, specials.release(), fOpts, true);
104 }
105
106 return nullptr;
107}
108
110{
111 // fill context menu items for the ROOT class
112 if (fKind == kObject)
113 items.PopulateObjectMenu(fObj.get(), fObj.get()->IsA());
114}
115
116void TObjectDrawable::Execute(const std::string &exec)
117{
118 if (fKind != kObject) return;
119
120 TObject *obj = fObj.get();
121
122 std::string sub, ex = exec;
123 if (ex.compare(0, 6, "xaxis#") == 0) {
124 ex.erase(0,6);
125 ex.insert(0, "GetXaxis()->");
126 } else if (ex.compare(0, 6, "yaxis#") == 0) {
127 ex.erase(0,6);
128 ex.insert(0, "GetYaxis()->");
129 } else if (ex.compare(0, 6, "zaxis#") == 0) {
130 ex.erase(0,6);
131 ex.insert(0, "GetZaxis()->");
132 }
133
134 std::stringstream cmd;
135 cmd << "((" << obj->ClassName() << "* ) " << std::hex << std::showbase << (size_t)obj << ")->" << ex << ";";
136 std::cout << "TObjectDrawable::Execute Obj " << obj->GetName() << "Cmd " << cmd.str() << std::endl;
137 gROOT->ProcessLine(cmd.str().c_str());
138}
const Bool_t kTRUE
Definition RtypesCore.h:91
#define gROOT
Definition TROOT.h:406
R__EXTERN TStyle * gStyle
Definition TStyle.h:412
Base class for drawable entities: objects that can be painted on a RPad.
Version_t GetVersion() const
List of items for object context menu.
void PopulateObjectMenu(void *obj, TClass *cl)
Fill menu for provided object, using MENU as indicator in method comments.
@ kPalette
list of colors from palette
@ kStyle
instance of TStyle object
Internal::RIOShared< TObject > fObj
The object to be painted.
std::string fOpts
drawing options
std::unique_ptr< TObject > CreateSpecials(int kind)
Create instance of requested special object.
const char * GetColorCode(TColor *col)
Convert TColor to RGB string for using with SVG.
void PopulateMenu(RMenuItems &) final
void Execute(const std::string &) final
std::unique_ptr< RDisplayItem > Display(const RDisplayContext &) override
Create display item which will be delivered to the client.
The color creation and management class.
Definition TColor.h:19
static const TArrayI & GetPalette()
Static function returning the current active palette.
Definition TColor.cxx:1405
Float_t GetRed() const
Definition TColor.h:57
Float_t GetAlpha() const
Definition TColor.h:63
Float_t GetBlue() const
Definition TColor.h:59
Float_t GetGreen() const
Definition TColor.h:58
Collectable string class.
Definition TObjString.h:28
Mother of all ROOT objects.
Definition TObject.h:37
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:359
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:130
Basic string class.
Definition TString.h:136
const char * Data() const
Definition TString.h:369
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2331
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2309
const Int_t n
Definition legend1.C:16
Double_t ex[n]
Definition legend1.C:17