Logo ROOT   6.14/05
Reference Guide
TQCanvasMenu.cxx
Go to the documentation of this file.
1 // @(#)root/qtgsi:$Id$
2 // Author: Denis Bertini, M. Al-Turany 01/11/2000
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2006, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "TQCanvasMenu.h"
13 #include "TQRootDialog.h"
14 
15 #include "Riostream.h"
16 
17 #include "TClass.h"
18 #include "TROOT.h"
19 #include "TMethod.h"
20 #include "TMethodCall.h"
21 #include "TMethodArg.h"
22 #include "TCanvas.h"
23 #include "TDataType.h"
24 
26 
27 ////////////////////////////////////////////////////////////////////////////////
28 /// ctor, create the popup menu
29 
30 TQCanvasMenu::TQCanvasMenu(QWidget* wparent, TCanvas *canvas)
31 {
32  fc = canvas;
33  fCurrObj = 0;
34  fParent = wparent;
35  fTabWin = 0;
36  fDialog = 0;
37  fMousePosX = fMousePosY = 0;
38 }
39 
40 ////////////////////////////////////////////////////////////////////////////////
41 /// ctor, create the popup menu
42 
43 TQCanvasMenu::TQCanvasMenu(QWidget* wparent, QWidget *tabWin, TCanvas *canvas)
44 {
45  fc = canvas;
46  fParent = wparent;
47  fTabWin = tabWin;
48  fCurrObj = 0;
49  fDialog = 0;
50  fMousePosX = fMousePosY = 0;
51 }
52 
53 ////////////////////////////////////////////////////////////////////////////////
54 /// dtor
55 
57 {
58 }
59 
60 ////////////////////////////////////////////////////////////////////////////////
61 /// Create title for dialog box retrieving argument values.
62 
64 {
65  static char methodTitle[128];
66 
67  if (object && method)
68  snprintf(methodTitle, 127, "%s::%s", object->ClassName(), method->GetName());
69  else
70  *methodTitle = 0;
71  return methodTitle;
72 
73 }
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 /// Create string describing argument (for use in dialog box).
77 
79 {
80  static Char_t argTitle[128];
81  if (argument) {
82  snprintf(argTitle, 127, "(%s) %s", argument->GetTitle(), argument->GetName());
83  const char *arg_def = argument->GetDefault();
84  if (arg_def && *arg_def) {
85  strncat(argTitle, " [default: ", 127 - strlen(argTitle));
86  strncat(argTitle, arg_def, 127 - strlen(argTitle));
87  strncat(argTitle, "]", 127 - strlen(argTitle));
88  }
89  }
90  else
91  *argTitle = 0;
92 
93  return argTitle;
94 }
95 
96 ////////////////////////////////////////////////////////////////////////////////
97 /// Perform the corresponding selected TObject popup
98 /// in the position defined
99 /// by x, y coordinates (in user coordinate system).
100 /// @param obj (TObject*)
101 /// @param p (QPoint&)
102 
103 void TQCanvasMenu::Popup(TObject *obj, double x, double y, QMouseEvent *e)
104 {
105  TClass *cls = obj->IsA();
106 
107  fCurrObj=obj;
108  fMenu.clear();
109  fMethods.Clear();
110 
111  QString name = cls->GetName();
112  name = name + "::" + obj->GetName();
113 
114  fMenu.addAction(name , this, SLOT(Execute(int)), 0);
115  fMenu.addSeparator();
116 
117  cls->GetMenuItems(&fMethods);
118  for (auto item : fMethods)
119  if (TMethod *method = dynamic_cast<TMethod*>(item))
120  fMenu.addAction(method->GetName(), this, SLOT(Execute(int)), 0);
121 
122  // hold the position where the mouse was clicked
123  fMousePosX = x;
124  fMousePosY = y;
125 
126  // let Qt decide how to draw the popup Menu otherwise we have a problem that
127  // the visible rectangle can get outside the screen (M.T. 03.06.02)
128  fMenu.popup(e->globalPos(), /* QAction* */ nullptr);
129 }
130 
131 ////////////////////////////////////////////////////////////////////////////////
132 /// Slot defined to execute a method from a selected TObject
133 /// using TObject::Execute() function.
134 
136 {
137  if (id < 0) return;
138  QString text="";
139 
140  TVirtualPad *psave = gROOT->GetSelectedPad();
141  TMethod *method=(TMethod *)fMethods.At(id);
142  fc->HandleInput(kButton3Up,gPad->XtoAbsPixel(fMousePosX), gPad->YtoAbsPixel(fMousePosY) );
143  if ( method->GetListOfMethodArgs()->First() ) {
144  Dialog(fCurrObj,method);
145  }
146  else {
147  gROOT->SetFromPopUp(kTRUE);
148  fCurrObj->Execute((char *) method->GetName(), "");
149  }
150  fc->GetPadSave()->Update();
151  fc->GetPadSave()->Modified();
152  gROOT->SetSelectedPad(psave);
153  gROOT->GetSelectedPad()->Update();
154  gROOT->GetSelectedPad()->Modified();
155  fc->Modified();
156  fc->ForceUpdate();
157  gROOT->SetFromPopUp( kFALSE );
158 }
159 
160 ////////////////////////////////////////////////////////////////////////////////
161 /// Create dialog object with OK and Cancel buttons. This dialog
162 /// prompts for the arguments of "method".
163 
164 void TQCanvasMenu::Dialog(TObject* object, TMethod* method)
165 {
166  if (!(object && method)) return;
167  fDialog = new TQRootDialog(fParent, CreateDialogTitle(object, method), object, method);
169  // iterate through all arguments and create apropriate input-data objects:
170  // inputlines, option menus...
171  TMethodArg *argument = 0;
172  TIter next(method->GetListOfMethodArgs());
173  while ((argument = (TMethodArg *) next())) {
174  char *argname = CreateArgumentTitle(argument);
175  const char *type = argument->GetTypeName();
176  TDataType *datatype = gROOT->GetType(type);
177  const char *charstar = "char*";
178  char basictype [32];
179 
180  if (datatype) {
181  strlcpy(basictype, datatype->GetTypeName(),32);
182  }
183  else {
184  if (strncmp(type, "enum", 4) != 0)
185  std::cout << "*** Warning in Dialog(): data type is not basic type, assuming (int)\n";
186  strcpy(basictype, "int");
187  }
188 
189  if (strchr(argname, '*')) {
190  strcat(basictype, "*");
191  type = charstar;
192  }
193 
194  TDataMember *m = argument->GetDataMember();
195  if (m && m->GetterMethod()) {
196  char gettername[256] = "";
197  strlcpy(gettername, m->GetterMethod()->GetMethodName(),256);
198  m->GetterMethod()->Init(object->IsA(), gettername, "");
199  // Get the current value and form it as a text:
200  char val[256];
201  if (!strncmp(basictype, "char*", 5)) {
202  char *tdefval = 0;
203  m->GetterMethod()->Execute(object, "", &tdefval);
204  if (tdefval && strlen(tdefval))
205  strlcpy(val, tdefval, 256);
206  }
207  else if (!strncmp(basictype, "float", 5) ||
208  !strncmp(basictype, "double", 6)) {
209  Double_t ddefval = 0.0;
210  m->GetterMethod()->Execute(object, "", ddefval);
211  snprintf(val, 255, "%g", ddefval);
212  }
213  else if (!strncmp(basictype, "char", 4) ||
214  !strncmp(basictype, "int", 3) ||
215  !strncmp(basictype, "long", 4) ||
216  !strncmp(basictype, "short", 5)) {
217  Long_t ldefval = 0L;
218  m->GetterMethod()->Execute(object, "", ldefval);
219  snprintf(val, 255, "%li", ldefval);
220  }
221  // Find out whether we have options ...
222  TList *opt;
223  if ((opt = m->GetOptions())) {
224  std::cout << "*** Warning in Dialog(): option menu not yet implemented " << opt << std::endl;
225  // should stop dialog
226  return;
227  }
228  else {
229  // we haven't got options - textfield ...
230  fDialog->Add(argname, val, type);
231  }
232  }
233  else { // if m not found ...
234  char val[256] = "";
235  const char *tval = argument->GetDefault();
236  if (tval) strlcpy(val, tval, 256);
237  fDialog->Add(argname, val, type);
238  }
239  } //end while
240 
241  fDialog->Popup();
242 }
double fMousePosX
Definition: TQCanvasMenu.h:61
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
TMethodCall * GetterMethod(TClass *cl=0)
Return a TMethodCall method responsible for getting the value of data member.
TString GetTypeName()
Get basic type of typedef, e,g.
Definition: TDataType.cxx:149
auto * m
Definition: textangle.C:8
TList fMethods
Definition: TQCanvasMenu.h:56
double fMousePosY
Definition: TQCanvasMenu.h:62
All ROOT classes may have RTTI (run time type identification) support added.
Definition: TDataMember.h:31
virtual void Update()=0
char * CreateArgumentTitle(TMethodArg *argument)
Create string describing argument (for use in dialog box).
void SetTCanvas(TCanvas *aCanvas)
Definition: TQRootDialog.h:47
TVirtualPad * GetPadSave() const
Definition: TCanvas.h:145
#define gROOT
Definition: TROOT.h:410
TList * GetOptions() const
Returns list of options - list of TOptionListItems.
Each ROOT method (see TMethod) has a linked list of its arguments.
Definition: TMethodArg.h:31
virtual ~TQCanvasMenu()
dtor
TQCanvasMenu(const TQCanvasMenu &c)
const char * GetTypeName() const
Get type of method argument, e.g.
Definition: TMethodArg.cxx:67
void Add(const char *argname, const char *value, const char *type)
Add widgets for arguments.
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:128
Double_t x[n]
Definition: legend1.C:17
void Popup(TObject *obj, double x, double y, QMouseEvent *e)
Perform the corresponding selected TObject popup in the position defined by x, y coordinates (in user...
void Init(const TFunction *func)
Initialize the method invocation environment based on the TFunction object.
QWidget * fParent
Definition: TQCanvasMenu.h:60
static constexpr double L
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition: TVirtualPad.h:49
QWidget * fTabWin
Definition: TQCanvasMenu.h:60
A doubly linked list.
Definition: TList.h:44
const char * GetMethodName() const
Definition: TMethodCall.h:90
void Execute(int id)
Slot defined to execute a method from a selected TObject using TObject::Execute() function...
virtual TList * GetListOfMethodArgs()
Returns methodarg list and additionally updates fDataMember in TMethod by calling FindDataMember();...
Definition: TMethod.cxx:305
virtual TObject * First() const
Return the first object in the list. Returns 0 when list is empty.
Definition: TList.cxx:655
Basic data type descriptor (datatype information is obtained from CINT).
Definition: TDataType.h:44
virtual void Execute(const char *method, const char *params, Int_t *error=0)
Execute method on this object with the given parameter string, e.g.
Definition: TObject.cxx:277
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:75
virtual TObject * At(Int_t idx) const
Returns the object at position idx. Returns 0 if idx is out of range.
Definition: TList.cxx:354
TObject * fCurrObj
Definition: TQCanvasMenu.h:54
const Bool_t kFALSE
Definition: RtypesCore.h:88
TDataMember * GetDataMember() const
Returns TDataMember pointed by this methodarg.
Definition: TMethodArg.cxx:121
long Long_t
Definition: RtypesCore.h:50
virtual void Modified(Bool_t flag=1)=0
The Canvas class.
Definition: TCanvas.h:31
#define ClassImp(name)
Definition: Rtypes.h:359
TQRootDialog * fDialog
Definition: TQCanvasMenu.h:58
double Double_t
Definition: RtypesCore.h:55
TText * text
int type
Definition: TGX11.cxx:120
Double_t y[n]
Definition: legend1.C:17
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
virtual void Clear(Option_t *option="")
Remove all objects from the list.
Definition: TList.cxx:399
Mother of all ROOT objects.
Definition: TObject.h:37
void ForceUpdate()
Definition: TCanvas.h:137
char Char_t
Definition: RtypesCore.h:29
const char * GetDefault() const
Get default value of method argument.
Definition: TMethodArg.cxx:58
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
TCanvas * fc
Definition: TQCanvasMenu.h:57
#define snprintf
Definition: civetweb.c:1351
#define gPad
Definition: TVirtualPad.h:285
virtual void HandleInput(EEventType button, Int_t x, Int_t y)
Handle Input Events.
Definition: TCanvas.cxx:1168
void Dialog(TObject *obj, TMethod *method)
Create dialog object with OK and Cancel buttons.
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:357
void Popup()
Show the dialog.
const Bool_t kTRUE
Definition: RtypesCore.h:87
void Modified(Bool_t flag=1)
Definition: TPad.h:414
char name[80]
Definition: TGX11.cxx:109
char * CreateDialogTitle(TObject *object, TMethod *method)
Create title for dialog box retrieving argument values.
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48