Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

tqcanvasmenu.cpp

Go to the documentation of this file.
00001 //-------------------------------------------------------------
00002 //        Go4 Release Package v3.04-01 (build 30401)
00003 //                      28-November-2008
00004 //---------------------------------------------------------------
00005 //   The GSI Online Offline Object Oriented (Go4) Project
00006 //   Experiment Data Processing at EE department, GSI
00007 //---------------------------------------------------------------
00008 //
00009 //Copyright (C) 2000- Gesellschaft f. Schwerionenforschung, GSI
00010 //                    Planckstr. 1, 64291 Darmstadt, Germany
00011 //Contact:            http://go4.gsi.de
00012 //----------------------------------------------------------------
00013 //This software can be used under the license agreements as stated
00014 //in Go4License.txt file which is part of the distribution.
00015 //----------------------------------------------------------------
00016 /****************************************************************************
00017 ** Copyright ( C ) 2000 denis Bertini.  All rights reserved.
00018 *****************************************************************************/
00019 
00020 #include "tqcanvasmenu.h"
00021 
00022 #include "Riostream.h"
00023 
00024 #include "qpopupmenu.h"
00025 #include "qinputdialog.h"
00026 #include "qcolordialog.h"
00027 
00028 #include "TH1.h"
00029 #include "TPad.h"
00030 #include "TCanvas.h"
00031 #include "TROOT.h"
00032 #include "TClass.h"
00033 #include "TDataType.h"
00034 #include "TDataMember.h"
00035 #include "TMethod.h"
00036 #include "TMethodCall.h"
00037 #include "TMethodArg.h"
00038 #include "TColor.h"
00039 #include "TLatex.h"
00040 
00041 #include "lockguard.h"
00042 #include "tqrootdialog.h"
00043 
00044 TQCanvasMenu::TQCanvasMenu(QWidget* parent, TCanvas *canvas)
00045 {
00046     c = canvas;
00047     fPopup = new QPopupMenu;
00048     fCurrObj = 0;
00049     fParent= parent;
00050     fTabWin = 0;
00051 }
00052 
00053 TQCanvasMenu::TQCanvasMenu(QWidget* parent, QWidget *tabWin, TCanvas *canvas)
00054 {
00055    c=canvas;
00056    fPopup = new QPopupMenu;
00057    fParent = parent;
00058    fTabWin = tabWin;
00059    fCurrObj=0;
00060 }
00061 
00062 TQCanvasMenu::~TQCanvasMenu()
00063 {
00064    if(fPopup) delete fPopup;
00065 }
00066 
00067 char* TQCanvasMenu::createDialogTitle( TObject *object, TMethod *method )
00068 {
00069  // Create title for dialog box retrieving argument values.
00070    Qtrootlockguard threadlock;
00071    static char methodTitle[128];
00072 
00073   if (object && method)
00074     snprintf(methodTitle, 127, "%s::%s", object->ClassName(), method->GetName());
00075   else
00076     *methodTitle = 0;
00077 
00078   return methodTitle;
00079 
00080 }
00081 
00082 char* TQCanvasMenu::createArgumentTitle(TMethodArg *argument)
00083 {
00084   // Create string describing argument (for use in dialog box).
00085    Qtrootlockguard threadlock;
00086    static Char_t argTitle[128];
00087   if (argument) {
00088     snprintf(argTitle, 127, "(%s)  %s", argument->GetTitle(), argument->GetName());
00089     if (argument->GetDefault() && *(argument->GetDefault())) {
00090       strcat(argTitle, "  [default: ");
00091       strcat(argTitle, argument->GetDefault());
00092       strcat(argTitle, "]");
00093     }
00094   } else
00095     *argTitle = 0;
00096 
00097   return argTitle;
00098 }
00099 
00100 
00101 void TQCanvasMenu::popup(TObject *obj, double x, double y, QMouseEvent *e)
00102 {
00103    Qtrootlockguard threadlock;
00104    TClass *klass=obj->IsA();
00105    int curId=-1;
00106 
00107    fCurrObj=obj;
00108    fPopup->clear();
00109    fMethods.Clear();
00110 
00111    QString buffer=klass->GetName();
00112    buffer+="::";
00113    buffer+=obj->GetName();
00114    fPopup->insertItem(buffer, this, SLOT( execute(int) ), 0,curId); curId++;
00115    klass->GetMenuItems(&fMethods);
00116    fPopup->insertSeparator();
00117 
00118    if(!klass->InheritsFrom(TLatex::Class())) {
00119        fPopup->insertItem("Insert Latex", this, SLOT(execute(int)), 0, 100 );
00120        fPopup->insertSeparator();
00121    }
00122 
00123    if(klass->InheritsFrom(TH1::Class())) {
00124       fPopup->insertItem("Qt Hist Line Color ", this, SLOT(execute(int)), 0, 101 );
00125       fPopup->insertItem("Qt Hist Fill Color ", this, SLOT(execute(int)), 0, 102 );
00126       fPopup->insertSeparator();
00127    }
00128 
00129    TIter iter(&fMethods);
00130    TMethod *method=0;
00131    while ( (method = dynamic_cast<TMethod*>(iter())) != 0) {
00132       buffer=method->GetName();
00133       fPopup->insertItem(buffer, this, SLOT( execute(int) ), 0,curId);
00134        curId++;
00135    }
00136 
00137    MousePosX= x;  // hold the position where the mouse was clicked
00138    MousePosY= y;
00139 
00140    fPopup->popup(e->globalPos(), 0); /* let Qt decide how to draw the popup Menu otherwise we have a problem that
00141                     *   the visible rectangle can get outside the screen (M. Al-Turany 03.06.02)
00142                     */
00143 }
00144 
00145 void TQCanvasMenu::execute(int id)
00146 {
00147    Qtrootlockguard threadlock;
00148    QString text("");
00149    bool ok = FALSE;
00150    if (id >=100) {
00151       switch (id){
00152          case 100: {
00153             TLatex *fxLatex = new TLatex();
00154             text = QInputDialog::getText(tr( "Qt Root" ),
00155                                          tr( "Please enter your text" ),
00156                                          QLineEdit::Normal, QString::null, &ok);
00157             //if (ok && !text.isEmpty())
00158             fxLatex->DrawLatex(MousePosX, MousePosY, text.latin1());
00159             emit MenuCommandExecuted(fxLatex, "DrawLatex");
00160 
00161             break;
00162         }
00163         case 101:
00164            ChangeHisLineColor();
00165            break;
00166         case 102 :
00167            ChangeHisFillColor();
00168            break;
00169       }
00170       gROOT->GetSelectedPad()->Update();
00171       gROOT->GetSelectedPad()->Modified();
00172       c->Modified();
00173       c->ForceUpdate();
00174       gROOT->SetFromPopUp( kFALSE );
00175    } else
00176    if (!(id < 0)) {
00177 
00178       // save global to Pad before calling TObject::Execute()
00179 
00180       TVirtualPad  *psave = gROOT->GetSelectedPad();
00181       TMethod *method= (TMethod *)fMethods.At(id);
00182 
00184       c->HandleInput(kButton3Up,gPad->XtoAbsPixel(MousePosX), gPad->YtoAbsPixel(MousePosY) );
00185 
00186       // change current dir that all new histograms appear here
00187       gROOT->cd();
00188 
00189       if (method->GetListOfMethodArgs()->First())
00190          dialog(fCurrObj, method);
00191       else {
00192          gROOT->SetFromPopUp(kTRUE);
00193          fCurrObj->Execute(method->GetName(), "");
00194 
00195          if (fCurrObj->TestBit(TObject::kNotDeleted)) {
00196             emit MenuCommandExecuted(fCurrObj, method->GetName());
00197          } else
00198             fCurrObj = 0;
00199       }
00200       c->GetPadSave()->Update();
00201       c->GetPadSave()->Modified();
00202       gROOT->SetSelectedPad(psave);
00204       //gPad->GetName(), gROOT->GetSelectedPad()->GetName(), c->GetName());
00205 
00206       gROOT->GetSelectedPad()->Update();
00207       gROOT->GetSelectedPad()->Modified();
00208       c->Modified();
00209       c->ForceUpdate();
00210       gROOT->SetFromPopUp(kFALSE);
00211     }
00212 }
00213 
00214 void TQCanvasMenu::EmitMenuCommandExecuted(TObject* obj, const char* cmdname)
00215 {
00216    emit MenuCommandExecuted(obj, cmdname);
00217 }
00218 
00219 void TQCanvasMenu::dialog(TObject* object, TMethod* method)
00220 {
00221    Qtrootlockguard threadlock;
00222   // Create dialog object with OK and Cancel buttons. This dialog
00223    // prompts for the arguments of "method".
00224 
00225    if (!(object && method)) return;
00226 
00227    TQRootDialog* fDialog = new TQRootDialog(fParent, createDialogTitle(object, method),0,object,method);
00228 
00229    fDialog->setTCanvas(c);
00230   // iterate through all arguments and create apropriate input-data objects:
00231   // inputlines, option menus...
00232    TMethodArg *argument = 0;
00233    TIter next(method->GetListOfMethodArgs());
00234 
00235    while ((argument = (TMethodArg *) next())) {
00236      Text_t       *argname    = createArgumentTitle(argument);
00237      const Text_t *type       = argument->GetTypeName();
00238      TDataType    *datatype   = gROOT->GetType(type);
00239      const Text_t *charstar   = "char*";
00240      Text_t        basictype [32];
00241 
00242      if (datatype) {
00243        strncpy(basictype, datatype->GetTypeName(),31);
00244      } else {
00245        if (strncmp(type, "enum", 4) != 0)
00246     cout << "*** Warning in Dialog(): data type is not basic type, assuming (int)\n";
00247        strcpy(basictype, "int");
00248       }
00249 
00250      if (strchr(argname, '*')) {
00251        strcat(basictype, "*");
00252        type = charstar;
00253      }
00254 
00255       TDataMember *m = argument->GetDataMember();
00256       if (m && m->GetterMethod()) {
00257 
00258    Text_t gettername[256] = "";
00259    strncpy(gettername, m->GetterMethod()->GetMethodName(),255);
00260    m->GetterMethod()->Init(object->IsA(), gettername, "");
00261 
00262    // Get the current value and form it as a text:
00263 
00264    Text_t val[256];
00265 
00266    if (!strncmp(basictype, "char*", 5)) {
00267       Text_t *tdefval;
00268       m->GetterMethod()->Execute(object, "", &tdefval);
00269       strncpy(val, tdefval, 255);
00270    } else if (!strncmp(basictype, "float", 5) ||
00271          !strncmp(basictype, "double", 6)) {
00272       Double_t ddefval;
00273       m->GetterMethod()->Execute(object, "", ddefval);
00274       snprintf(val, 255, "%g", ddefval);
00275    } else if (!strncmp(basictype, "char", 4) ||
00276                     !strncmp(basictype, "int", 3)  ||
00277          !strncmp(basictype, "long", 4) ||
00278          !strncmp(basictype, "short", 5)) {
00279       Long_t ldefval;
00280       m->GetterMethod()->Execute(object, "", ldefval);
00281             snprintf(val, 255, "%li", ldefval);
00282    }
00283 
00284    // Find out whether we have options ...
00285 
00286    TList *opt;
00287    if ((opt = m->GetOptions())) {
00288      cout << "*** Warning in Dialog(): option menu not yet implemented " << opt << endl;
00289      // should stop dialog
00290      return;
00291 #if 0
00292      TMotifOptionMenu *o= new TMotifOptionMenu(argname);
00293      TIter nextopt(opt);
00294      TOptionListItem *it = 0;
00295      while ((it = (TOptionListItem*) nextopt())) {
00296        Text_t *name  = it->fOptName;
00297        Text_t *label = it->fOptLabel;
00298                Long_t value  = it->fValue;
00299                if (value != -9999) {
00300        Text_t val[256];
00301        snprintf(val, 255, "%li", value);
00302        o->AddItem(name, val);
00303                }else
00304        o->AddItem(name, label);
00305      }
00306      o->SetData(val);
00307      fDialog->Add(o);
00308 #endif
00309    } else {
00310      // we haven't got options - textfield ...
00311             fDialog->add(argname, val, type);
00312    }
00313 
00314    } else {    // if m not found ...
00315       char val[256] = "";
00316       const char *tval = argument->GetDefault();
00317       if (tval) strncpy(val, tval, 255);
00318       fDialog->add(argname, val, type);
00319 
00320    }
00321    }
00322 
00323    fDialog->SetCanMenu(this);
00324 
00325    fDialog->popup();
00326 }
00327 
00328 
00329 void TQCanvasMenu::ChangeHisLineColor()
00330 {
00331    TH1 *fxTH1 = (TH1*) fCurrObj;
00332    QColor col = QColorDialog::getColor();
00333    if ( col.isValid() ) {
00334       short int C_new =  TColor::GetColor(col.red(), col.green(), col.blue());
00335       if (fxTH1!=0) {
00336          fxTH1->SetLineColor(C_new);
00337          emit MenuCommandExecuted(fxTH1,"SetLineColor");
00338       }
00339    }
00340 }
00341 
00342 void TQCanvasMenu::ChangeHisFillColor()
00343 {
00344    TH1 *fxTH1 = (TH1*) fCurrObj;
00345    QColor col = QColorDialog::getColor();
00346    if ( col.isValid() ) {
00347      short int C_new =  TColor::GetColor(col.red(), col.green(), col.blue());
00348      if (fxTH1!=0) {
00349          fxTH1->SetFillColor(C_new);
00350          emit MenuCommandExecuted(fxTH1,"SetFillColor");
00351      }
00352   }
00353 }
00354 
00355 
00356 
00357 
00358 
00359 
00360 
00361 
00362 
00363 
00364 
00365 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Fri Nov 28 12:59:33 2008 for Go4-v3.04-1 by  doxygen 1.4.2