Logo ROOT   6.12/07
Reference Guide
TMenuItem.cxx
Go to the documentation of this file.
1 /// \file TMenuItem.cxx
2 /// \ingroup Base ROOT7
3 /// \author Sergey Linev
4 /// \date 2017-07-18
5 /// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
6 /// is welcome!
7 
8 /*************************************************************************
9  * Copyright (C) 1995-2017, Rene Brun and Fons Rademakers. *
10  * All rights reserved. *
11  * *
12  * For the licensing terms see $ROOTSYS/LICENSE. *
13  * For the list of contributors see $ROOTSYS/README/CREDITS. *
14  *************************************************************************/
15 
16 #include "ROOT/TMenuItem.hxx"
17 
18 #include "TROOT.h"
19 #include "TClass.h"
20 #include "TList.h"
21 #include "TMethod.h"
22 #include "TMethodArg.h"
23 #include "TMethodCall.h"
24 #include "TBufferJSON.h"
25 
27 {
28  for (unsigned n = 0; n < fItems.size(); ++n) delete fItems[n];
29 
30  fItems.clear();
31 }
32 
34 {
35  Cleanup();
36 
37  TList *lst = new TList;
38  cl->GetMenuItems(lst);
39 
40  TIter iter(lst);
41  TMethod *m = 0;
42 
43  while ((m = (TMethod *)iter()) != 0) {
44 
45  if (m->IsMenuItem() == kMenuToggle) {
46  TString getter;
47  if (m->Getter() && strlen(m->Getter()) > 0) {
48  getter = m->Getter();
49  } else if (strncmp(m->GetName(), "Set", 3) == 0) {
50  getter = TString(m->GetName())(3, strlen(m->GetName()) - 3);
51  if (cl->GetMethodAllAny(TString("Has") + getter))
52  getter = TString("Has") + getter;
53  else if (cl->GetMethodAllAny(TString("Get") + getter))
54  getter = TString("Get") + getter;
55  else if (cl->GetMethodAllAny(TString("Is") + getter))
56  getter = TString("Is") + getter;
57  else
58  getter = "";
59  }
60 
61  if ((getter.Length() > 0) && cl->GetMethodAllAny(getter)) {
62  // execute getter method to get current state of toggle item
63 
64  TMethodCall *call = new TMethodCall(cl, getter, "");
65 
66  if (call->ReturnType() == TMethodCall::kLong) {
67  Long_t l(0);
68  call->Execute(obj, l);
69 
70  AddChkMenuItem(m->GetName(), m->GetTitle(), l != 0, Form("%s(%s)", m->GetName(), (l != 0) ? "0" : "1"));
71 
72  } else {
73  // Error("CheckModifiedFlag", "Cannot get toggle value with getter %s", getter.Data());
74  }
75 
76  delete call;
77  }
78  } else {
79  TList *args = m->GetListOfMethodArgs();
80 
81  if (!args || (args->GetSize() == 0)) {
82  AddMenuItem(m->GetName(), m->GetTitle(), Form("%s()", m->GetName()));
83  } else {
85  item->SetExec(Form("%s()", m->GetName()));
86 
87  TIter args_iter(args);
88  TMethodArg *arg = 0;
89 
90  while ((arg = dynamic_cast<TMethodArg *>(args_iter())) != 0) {
91  Detail::TMenuArgument menu_arg(arg->GetName(), arg->GetTitle(), arg->GetFullTypeName());
92  if (arg->GetDefault()) menu_arg.SetDefault(arg->GetDefault());
93  item->AddArg(menu_arg);
94  }
95 
96  Add(item);
97  }
98  }
99  }
100 
101  delete lst;
102 }
103 
105 {
106  TClass *cl = gROOT->GetClass("std::vector<ROOT::Experimental::Detail::TMenuItem*>");
107 
108  // FIXME: got problem with std::list<TMenuItem>, can be generic TBufferJSON
110 
111  // printf("Got JSON %s\n", res.Data());
112 
113  return res.Data();
114 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
auto * m
Definition: textangle.C:8
void SetDefault(const std::string &dflt)
Definition: TMenuItem.hxx:98
virtual const char * Getter() const
Definition: TMethod.h:59
#define gROOT
Definition: TROOT.h:402
static const EReturnType kLong
Definition: TMethodCall.h:43
Basic string class.
Definition: TString.h:125
Each ROOT method (see TMethod) has a linked list of its arguments.
Definition: TMethodArg.h:31
const char * GetFullTypeName() const
Get full type description of method argument, e.g.: "class TDirectory*".
Definition: TMethodArg.cxx:75
TMethod * GetMethodAllAny(const char *method)
Return pointer to method without looking at parameters.
Definition: TClass.cxx:4205
void SetExec(const std::string &exec)
Set execution string with all required arguments, which will be executed when menu item is selected...
Definition: TMenuItem.hxx:51
void AddChkMenuItem(const std::string &name, const std::string &title, bool checked, const std::string &toggle)
Definition: TMenuItem.hxx:139
Method or function calling interface.
Definition: TMethodCall.h:37
void AddMenuItem(const std::string &name, const std::string &title, const std::string &exec)
Definition: TMenuItem.hxx:132
A doubly linked list.
Definition: TList.h:44
virtual TList * GetListOfMethodArgs()
Returns methodarg list and additionally updates fDataMember in TMethod by calling FindDataMember();...
Definition: TMethod.cxx:305
char * Form(const char *fmt,...)
Ssiz_t Length() const
Definition: TString.h:386
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:75
void PopulateObjectMenu(void *obj, TClass *cl)
Definition: TMenuItem.cxx:33
std::vector< Detail::TMenuItem * > fItems
list of items in the menu
Definition: TMenuItem.hxx:123
long Long_t
Definition: RtypesCore.h:50
static TString ConvertToJSON(const TObject *obj, Int_t compact=0, const char *member_name=0)
Converts object, inherited from TObject class, to JSON string Lower digit of compact parameter define...
void Add(Detail::TMenuItem *item)
Definition: TMenuItem.hxx:130
EMenuItemKind IsMenuItem() const
Definition: TMethod.h:56
const char * GetDefault() const
Get default value of method argument.
Definition: TMethodArg.cxx:58
auto * l
Definition: textangle.C:4
void Execute(const char *, const char *, int *=0)
Execute method on this object with the given parameter string, e.g.
Definition: TMethodCall.h:64
Each ROOT class (see TClass) has a linked list of methods.
Definition: TMethod.h:38
void GetMenuItems(TList *listitems)
Returns list of methods accessible by context menu.
Definition: TClass.cxx:3728
virtual Int_t GetSize() const
Definition: TCollection.h:180
void AddArg(const TMenuArgument &arg)
Definition: TMenuItem.hxx:114
const Int_t n
Definition: legend1.C:16
EReturnType ReturnType()
Returns the return type of the method.
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
const char * Data() const
Definition: TString.h:345