Logo ROOT   6.12/07
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 "Riostream.h"
13 #include "qevent.h"
14 #include "qdialog.h"
15 #include "qpushbutton.h"
16 #include "qlabel.h"
17 #include "qpainter.h"
18 #if (QT_VERSION > 0x039999) // Added by cholm@nbi.dk - for Qt 4
19 # ifndef QT3_SUPPORT
20 # define QT3_SUPPORT
21 # endif
22 # include "q3popupmenu.h"
23 #else
24 # include "qpopupmenu.h"
25 #endif
26 
27 
28 #include "TQCanvasMenu.h"
29 #include "TClass.h"
30 #include "TROOT.h"
31 #include "TMethod.h"
32 #include "TMethodCall.h"
33 #include "TMethodArg.h"
34 #include "TCanvas.h"
35 #include "TDataType.h"
36 #include "TQRootDialog.h"
37 
39 
40 ////////////////////////////////////////////////////////////////////////////////
41 /// ctor, create the popup menu
42 
43 TQCanvasMenu::TQCanvasMenu(QWidget* parent, TCanvas *canvas)
44 {
45  fc = canvas;
46  fPopup = new QPopupMenu;
47  fCurrObj = 0;
48  fParent = parent;
49  fTabWin = 0;
50  fDialog = 0;
51  fMousePosX = fMousePosY = 0;
52 }
53 
54 ////////////////////////////////////////////////////////////////////////////////
55 /// ctor, create the popup menu
56 
57 TQCanvasMenu::TQCanvasMenu(QWidget* parent, QWidget *tabWin, TCanvas *canvas)
58 {
59  fc = canvas;
60  fPopup = new QPopupMenu;
61  fParent = parent;
62  fTabWin = tabWin;
63  fCurrObj = 0;
64  fDialog = 0;
65  fMousePosX = fMousePosY = 0;
66 }
67 
68 ////////////////////////////////////////////////////////////////////////////////
69 /// dtor
70 
72 {
73  if (fPopup) delete fPopup;
74 }
75 
76 ////////////////////////////////////////////////////////////////////////////////
77 /// Create title for dialog box retrieving argument values.
78 
80 {
81  static char methodTitle[128];
82 
83  if (object && method)
84  snprintf(methodTitle, 127, "%s::%s", object->ClassName(), method->GetName());
85  else
86  *methodTitle = 0;
87  return methodTitle;
88 
89 }
90 
91 ////////////////////////////////////////////////////////////////////////////////
92 /// Create string describing argument (for use in dialog box).
93 
95 {
96  static Char_t argTitle[128];
97  if (argument) {
98  snprintf(argTitle, 127, "(%s) %s", argument->GetTitle(), argument->GetName());
99  const char *arg_def = argument->GetDefault();
100  if (arg_def && *arg_def) {
101  strncat(argTitle, " [default: ", 127 - strlen(argTitle));
102  strncat(argTitle, arg_def, 127 - strlen(argTitle));
103  strncat(argTitle, "]", 127 - strlen(argTitle));
104  }
105  }
106  else
107  *argTitle = 0;
108 
109  return argTitle;
110 }
111 
112 ////////////////////////////////////////////////////////////////////////////////
113 /// Perform the corresponding selected TObject popup
114 /// in the position defined
115 /// by x, y coordinates (in user coordinate system).
116 /// @param obj (TObject*)
117 /// @param p (QPoint&)
118 
119 void TQCanvasMenu::Popup(TObject *obj, double x, double y, QMouseEvent *e)
120 {
121  TClass *klass=obj->IsA();
122  Int_t curId=-1;
123 
124  fCurrObj=obj;
125  fPopup->clear();
126  fMethods.Clear();
127 
128  QString buffer=klass->GetName();
129  buffer+="::";
130  buffer+=obj->GetName();
131  fPopup->insertItem(buffer, this, SLOT( Execute(int) ), 0,curId); curId++;
132  klass->GetMenuItems(&fMethods);
133  fPopup->insertSeparator();
134  TIter iter(&fMethods);
135  TMethod *method=0;
136  while ( (method = dynamic_cast<TMethod*>(iter())) != 0) {
137  buffer=method->GetName();
138  fPopup->insertItem(buffer, this, SLOT( Execute(int) ), 0,curId);
139  curId++;
140  }
141  // hold the position where the mouse was clicked
142  fMousePosX= x;
143  fMousePosY= y;
144 
145  // let Qt decide how to draw the popup Menu otherwise we have a problem that
146  // the visible rectangle can get outside the screen (M.T. 03.06.02)
147  fPopup->popup(e->globalPos(), 0);
148 
149 }
150 
151 ////////////////////////////////////////////////////////////////////////////////
152 /// Slot defined to execute a method from a selected TObject
153 /// using TObject::Execute() function.
154 
156 {
157  if (id < 0) return;
158  QString text="";
159 
160  TVirtualPad *psave = gROOT->GetSelectedPad();
161  TMethod *method=(TMethod *)fMethods.At(id);
162  fc->HandleInput(kButton3Up,gPad->XtoAbsPixel(fMousePosX), gPad->YtoAbsPixel(fMousePosY) );
163  if ( method->GetListOfMethodArgs()->First() ) {
164  Dialog(fCurrObj,method);
165  }
166  else {
167  gROOT->SetFromPopUp(kTRUE);
168  fCurrObj->Execute((char *) method->GetName(), "");
169  }
170  fc->GetPadSave()->Update();
171  fc->GetPadSave()->Modified();
172  gROOT->SetSelectedPad(psave);
173  gROOT->GetSelectedPad()->Update();
174  gROOT->GetSelectedPad()->Modified();
175  fc->Modified();
176  fc->ForceUpdate();
177  gROOT->SetFromPopUp( kFALSE );
178 }
179 
180 ////////////////////////////////////////////////////////////////////////////////
181 /// Create dialog object with OK and Cancel buttons. This dialog
182 /// prompts for the arguments of "method".
183 
184 void TQCanvasMenu::Dialog(TObject* object, TMethod* method)
185 {
186  if (!(object && method)) return;
187  fDialog = new TQRootDialog(fParent,CreateDialogTitle(object, method),0,object ,method);
189  // iterate through all arguments and create apropriate input-data objects:
190  // inputlines, option menus...
191  TMethodArg *argument = 0;
192  TIter next(method->GetListOfMethodArgs());
193  while ((argument = (TMethodArg *) next())) {
194  char *argname = CreateArgumentTitle(argument);
195  const char *type = argument->GetTypeName();
196  TDataType *datatype = gROOT->GetType(type);
197  const char *charstar = "char*";
198  char basictype [32];
199 
200  if (datatype) {
201  strlcpy(basictype, datatype->GetTypeName(),32);
202  }
203  else {
204  if (strncmp(type, "enum", 4) != 0)
205  std::cout << "*** Warning in Dialog(): data type is not basic type, assuming (int)\n";
206  strcpy(basictype, "int");
207  }
208 
209  if (strchr(argname, '*')) {
210  strcat(basictype, "*");
211  type = charstar;
212  }
213 
214  TDataMember *m = argument->GetDataMember();
215  if (m && m->GetterMethod()) {
216  char gettername[256] = "";
217  strlcpy(gettername, m->GetterMethod()->GetMethodName(),256);
218  m->GetterMethod()->Init(object->IsA(), gettername, "");
219  // Get the current value and form it as a text:
220  char val[256];
221  if (!strncmp(basictype, "char*", 5)) {
222  char *tdefval = 0;
223  m->GetterMethod()->Execute(object, "", &tdefval);
224  if (tdefval && strlen(tdefval))
225  strlcpy(val, tdefval, 256);
226  }
227  else if (!strncmp(basictype, "float", 5) ||
228  !strncmp(basictype, "double", 6)) {
229  Double_t ddefval = 0.0;
230  m->GetterMethod()->Execute(object, "", ddefval);
231  snprintf(val, 255, "%g", ddefval);
232  }
233  else if (!strncmp(basictype, "char", 4) ||
234  !strncmp(basictype, "int", 3) ||
235  !strncmp(basictype, "long", 4) ||
236  !strncmp(basictype, "short", 5)) {
237  Long_t ldefval = 0L;
238  m->GetterMethod()->Execute(object, "", ldefval);
239  snprintf(val, 255, "%li", ldefval);
240  }
241  // Find out whether we have options ...
242  TList *opt;
243  if ((opt = m->GetOptions())) {
244  std::cout << "*** Warning in Dialog(): option menu not yet implemented " << opt << std::endl;
245  // should stop dialog
246  return;
247  }
248  else {
249  // we haven't got options - textfield ...
250  fDialog->Add(argname, val, type);
251  }
252  }
253  else { // if m not found ...
254  char val[256] = "";
255  const char *tval = argument->GetDefault();
256  if (tval) strlcpy(val, tval, 256);
257  fDialog->Add(argname, val, type);
258  }
259  } //end while
260 
261  fDialog->Popup();
262 }
double fMousePosX
Definition: TQCanvasMenu.h:76
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:71
double fMousePosY
Definition: TQCanvasMenu.h:77
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:72
TVirtualPad * GetPadSave() const
Definition: TCanvas.h:145
#define gROOT
Definition: TROOT.h:402
TList * GetOptions() const
Returns list of options - list of TOptionListItems.
int Int_t
Definition: RtypesCore.h:41
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:75
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:75
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:69
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:73
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:72
#define snprintf
Definition: civetweb.c:822
#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
QPopupMenu * fPopup
Definition: TQCanvasMenu.h:70
void Modified(Bool_t flag=1)
Definition: TPad.h:414
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