TQCanvasMenu.cxx

Go to the documentation of this file.
00001 // @(#)root/qtgsi:$Id: TQCanvasMenu.cxx 35527 2010-09-21 12:27:01Z brun $
00002 // Author: Denis Bertini, M. Al-Turany  01/11/2000
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2006, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #include "Riostream.h"
00013 #include "qevent.h"
00014 #include "qdialog.h"
00015 #include "qpushbutton.h"
00016 #include "qlabel.h"
00017 #include "qpainter.h"
00018 #if  (QT_VERSION > 0x039999) // Added by cholm@nbi.dk - for Qt 4
00019 # include "qmenu.h"
00020 # include "q3popupmenu.h"
00021 typedef Q3PopupMenu QPopupMenu;
00022 #else
00023 # include "qpopupmenu.h"
00024 #endif
00025 
00026 
00027 #include "TQCanvasMenu.h"
00028 #include "TClass.h"
00029 #include "TROOT.h"
00030 #include "TMethod.h"
00031 #include "TMethodCall.h"
00032 #include "TMethodArg.h"
00033 #include "TCanvas.h"
00034 #include "TDataType.h"
00035 #include "TQRootDialog.h"
00036 
00037 ClassImp(TQCanvasMenu)
00038 
00039 //______________________________________________________________________________
00040 TQCanvasMenu::TQCanvasMenu(QWidget* parent, TCanvas *canvas)
00041 {
00042    // ctor, create the popup menu
00043 
00044    fc       = canvas;
00045    fPopup   = new QPopupMenu;
00046    fCurrObj = 0;
00047    fParent  = parent;
00048    fTabWin  = 0;
00049    fDialog  = 0;
00050    fMousePosX = fMousePosY = 0;
00051 }
00052 
00053 //______________________________________________________________________________
00054 TQCanvasMenu::TQCanvasMenu(QWidget* parent, QWidget *tabWin, TCanvas *canvas)
00055 {
00056    // ctor, create the popup menu
00057 
00058    fc       = canvas;
00059    fPopup   = new QPopupMenu;
00060    fParent  = parent;
00061    fTabWin  = tabWin;
00062    fCurrObj = 0;
00063    fDialog  = 0;
00064    fMousePosX = fMousePosY = 0;
00065 }
00066 
00067 //______________________________________________________________________________
00068 TQCanvasMenu::~TQCanvasMenu()
00069 {
00070    // dtor
00071 
00072    if (fPopup) delete fPopup;
00073 }
00074 
00075 //______________________________________________________________________________
00076 char *TQCanvasMenu::CreateDialogTitle( TObject *object, TMethod *method )
00077 {
00078    // Create title for dialog box retrieving argument values.
00079 
00080    static char methodTitle[128];
00081 
00082    if (object && method)
00083       snprintf(methodTitle, 127, "%s::%s", object->ClassName(), method->GetName());
00084    else
00085       *methodTitle = 0;
00086    return methodTitle;
00087 
00088 }
00089 
00090 //______________________________________________________________________________
00091 char *TQCanvasMenu::CreateArgumentTitle(TMethodArg *argument)
00092 {
00093    // Create string describing argument (for use in dialog box).
00094 
00095    static Char_t argTitle[128];
00096    if (argument) {
00097       snprintf(argTitle, 127, "(%s)  %s", argument->GetTitle(), argument->GetName());
00098       const char *arg_def = argument->GetDefault();
00099       if (arg_def && *arg_def) {
00100          strncat(argTitle, "  [default: ", 127 - strlen(argTitle));
00101          strncat(argTitle, arg_def, 127 - strlen(argTitle));
00102          strncat(argTitle, "]", 127 - strlen(argTitle));
00103       }
00104    }
00105    else
00106       *argTitle = 0;
00107 
00108    return argTitle;
00109 }
00110 
00111 //______________________________________________________________________________
00112 void TQCanvasMenu::Popup(TObject *obj, double x, double y, QMouseEvent *e)
00113 {
00114    // Perform the corresponding selected TObject  popup
00115    // in the position defined
00116    // by x, y coordinates (in user coordinate system).
00117    // @param obj (TObject*)
00118    // @param p (QPoint&)
00119 
00120    TClass *klass=obj->IsA();
00121    Int_t curId=-1;
00122 
00123    fCurrObj=obj;
00124    fPopup->clear();
00125    fMethods.Clear();
00126 
00127    QString buffer=klass->GetName();
00128    buffer+="::";
00129    buffer+=obj->GetName();
00130    fPopup->insertItem(buffer, this, SLOT( Execute(int) ), 0,curId); curId++;
00131    klass->GetMenuItems(&fMethods);
00132    fPopup->insertSeparator();
00133    TIter iter(&fMethods);
00134    TMethod *method=0;
00135    while ( (method = dynamic_cast<TMethod*>(iter())) != 0) {
00136       buffer=method->GetName();
00137       fPopup->insertItem(buffer, this, SLOT( Execute(int) ), 0,curId);
00138       curId++;
00139    }
00140    // hold the position where the mouse was clicked
00141    fMousePosX= x;
00142    fMousePosY= y;
00143 
00144    // let Qt decide how to draw the popup Menu otherwise we have a problem that
00145    // the visible rectangle can get outside the screen (M.T. 03.06.02)
00146    fPopup->popup(e->globalPos(), 0);
00147 
00148 }
00149 
00150 //______________________________________________________________________________
00151 void TQCanvasMenu::Execute(int id)
00152 {
00153    // Slot defined to execute a method from a selected TObject
00154    // using TObject::Execute() function.
00155 
00156    QString text="";
00157 
00158    TVirtualPad  *psave = gROOT->GetSelectedPad();
00159    TMethod *method=(TMethod *)fMethods.At(id);
00160    fc->HandleInput(kButton3Up,gPad->XtoAbsPixel(fMousePosX), gPad->YtoAbsPixel(fMousePosY) );
00161    if (  method->GetListOfMethodArgs()->First() ) {
00162       Dialog(fCurrObj,method);
00163    }
00164    else {
00165       gROOT->SetFromPopUp(kTRUE);
00166       fCurrObj->Execute((char *) method->GetName(), "");
00167    }
00168    fc->GetPadSave()->Update();
00169    fc->GetPadSave()->Modified();
00170    gROOT->SetSelectedPad(psave);
00171    gROOT->GetSelectedPad()->Update();
00172    gROOT->GetSelectedPad()->Modified();
00173    fc->Modified();
00174    fc->ForceUpdate();
00175    gROOT->SetFromPopUp( kFALSE );
00176 }
00177 
00178 //______________________________________________________________________________
00179 void TQCanvasMenu::Dialog(TObject* object, TMethod* method)
00180 {
00181    // Create dialog object with OK and Cancel buttons. This dialog
00182    // prompts for the arguments of "method".
00183 
00184    if (!(object && method)) return;
00185    fDialog = new TQRootDialog(fParent,CreateDialogTitle(object, method),0,object ,method);
00186    fDialog->SetTCanvas(fc);
00187    // iterate through all arguments and create apropriate input-data objects:
00188    // inputlines, option menus...
00189    TMethodArg *argument = 0;
00190    TIter next(method->GetListOfMethodArgs());
00191    while ((argument = (TMethodArg *) next())) {
00192       char       *argname    = CreateArgumentTitle(argument);
00193       const char *type       = argument->GetTypeName();
00194       TDataType    *datatype   = gROOT->GetType(type);
00195       const char *charstar   = "char*";
00196       char        basictype [32];
00197 
00198       if (datatype) {
00199          strlcpy(basictype, datatype->GetTypeName(),32);
00200       }
00201       else {
00202          if (strncmp(type, "enum", 4) != 0)
00203          cout << "*** Warning in Dialog(): data type is not basic type, assuming (int)\n";
00204          strcpy(basictype, "int");
00205       }
00206 
00207       if (strchr(argname, '*')) {
00208          strcat(basictype, "*");
00209          type = charstar;
00210       }
00211 
00212       TDataMember *m = argument->GetDataMember();
00213       if (m && m->GetterMethod()) {
00214          char gettername[256] = "";
00215          strlcpy(gettername, m->GetterMethod()->GetMethodName(),256);
00216          m->GetterMethod()->Init(object->IsA(), gettername, "");
00217          // Get the current value and form it as a text:
00218          char val[256];
00219          if (!strncmp(basictype, "char*", 5)) {
00220             char *tdefval;
00221             m->GetterMethod()->Execute(object, "", &tdefval);
00222             strlcpy(val, tdefval, 256);
00223          }
00224          else if (!strncmp(basictype, "float", 5) ||
00225             !strncmp(basictype, "double", 6)) {
00226             Double_t ddefval;
00227             m->GetterMethod()->Execute(object, "", ddefval);
00228             snprintf(val, 255, "%g", ddefval);
00229          }
00230          else if (!strncmp(basictype, "char", 4) ||
00231              !strncmp(basictype, "int", 3)  ||
00232              !strncmp(basictype, "long", 4) ||
00233              !strncmp(basictype, "short", 5)) {
00234             Long_t ldefval;
00235             m->GetterMethod()->Execute(object, "", ldefval);
00236             snprintf(val, 255, "%li", ldefval);
00237          }
00238          // Find out whether we have options ...
00239          TList *opt;
00240          if ((opt = m->GetOptions())) {
00241             cout << "*** Warning in Dialog(): option menu not yet implemented " << opt << endl;
00242             // should stop dialog
00243             return;
00244          }
00245          else {
00246             // we haven't got options - textfield ...
00247             fDialog->Add(argname, val, type);
00248          }
00249       }
00250       else {    // if m not found ...
00251          char val[256] = "";
00252          const char *tval = argument->GetDefault();
00253          if (tval) strlcpy(val, tval, 256);
00254          fDialog->Add(argname, val, type);
00255       }
00256    } //end while
00257 
00258    fDialog->Popup();
00259 }

Generated on Tue Jul 5 14:22:20 2011 for ROOT_528-00b_version by  doxygen 1.5.1