StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SetQtEnv.C
1 //*-- Author : Valery Fine(fine@bnl.gov) 27/10/2006
2 //
3 // $Id: SetQtEnv.C,v 1.4 2006/10/30 20:38:01 fine Exp $
4 // This macro sets the Qt/Root environment "on fly" and
5 // generates the correct ROOT resource ".rootrc" file
6 // also
7 //__________________________________________________________________
8 FILE *OpeFileName(const char *fileNamePrototype)
9 {
10  // Open the file by the giverm file name
11  return fopen( (const char*)GetNewFileName(fileNamePrototype),"w");
12 }
13 //__________________________________________________________________
14 TString GetNewFileName(const char *fileNamePrototype)
15 {
16  // Find the filename for the given "fileNamePrototype"
17  TString fileName = fileNamePrototype;
18  gSystem->ExpandPathName(fileName);
19  // Find the file extenstion if any
20  Ssiz_t fileExtension = fileName.Last('.');
21  TString fileNameHold = fileName;
22  if ((fileExtension==0) || (fileExtension == -1)) {
23  fileExtension = fileName.Length();
24  }
25  Int_t counter = 0;
26  while (gSystem->AccessPathName(fileName.Data())==0) {
27  fileName = fileNameHold;
28  fileName.Insert(fileExtension,Form(".%d",counter++));
29  }
30  return fileName;
31 }
32 
33 //__________________________________________________________________
34 Int_t SetRootResource(FILE *file, const char *plugin,
35  const char *lib,
36  const char *full=0,Bool_t append=kFALSE)
37 {
38  fprintf(stderr," Testing the plugin <%s> from the lib = <%s>\n", plugin, lib);
39  Int_t success = 0;
40  TString fullName;
41  TString fullValue;
42  if (full) {
43 // fprintf(stderr," full = %s", full);
44  fullName = Form("lib%s",lib);
45  fullValue = Form(full,lib);
46  } else {
47  fullValue = lib;
48  }
49  fprintf(stderr," ------- \n");
50 
51 
52  if ( !full || gSystem->DynamicPathName(fullName.Data(),kTRUE) ) {
53  // Check plugin
54  success = 1;
55  TString currentPlugin = gEnv->GetValue(plugin,"none");
56  TPRegexp exp(Form("\\b%s\\b",lib));
57  if ( !currentPlugin.Contains(exp) )
58  {
59  TString p = "+";
60  if (append) {
61  p +=plugin;
62  plugin = p.Data();
63  // fprintf(stderr, "Appending %s\n", plugin);
64  }
65  gEnv->SetValue(plugin,fullValue);
66  if (file) {
67  fputs(Form("%s %s\n",plugin,fullValue.Data()), file);
68  }
69  } else {
70  // fprintf(stderr, "Found %s with <%s>\n", currentPlugin.Data(), Form("\\s+?%s\\s+?",lib));
71  }
72  }
73  return success;
74 }
75 
76 //__________________________________________________________________
77 void SetQtEnv() {
78  //------------------
79  // Check GED library
80  //------------------
81  const char *plugins[] = {
82  "Plugin.TVirtualX"
83  , "GQt"
84  , " qt TGQt %s \"TGQt(const char*,const char*)\""
85 
86  , "Gui.Backend", "qt", 0
87 
88  , "Plugin.TGuiFactory"
89  , "QtRootGui"
90  , " qtgui TQtGUIFactory %s \"TQtGUIFactory()\""
91 
92  , "Gui.Factory", "qtgui", 0
93 
94  , "Gui.Factory", "qt" , 0
95 
96  , "Plugin.TVirtualPadEditor"
97  , "QtGed"
98  , " Ged TQtGedEditor %s \"TQtGedEditor(TCanvas*)\""
99 
100  , "Plugin.TVirtualViewer3D"
101  , "RQTGL"
102  , " ogl TQtRootViewer3D %s \"TQtRootViewer3D(TVirtualPad*)\""
103 
104  , "Plugin.TVirtualViewer3D"
105  , "RQIVTGL"
106  , " oiv TQtRootCoinViewer3D %s \"TQtRootCoinViewer3D(TVirtualPad*)\""
107  , "", "", ""
108  };
109  Int_t iPlugin = 0;
110  Int_t c = 0;
111  TString fileName = GetNewFileName("rootrc");
112  FILE *f = fopen((const char *)fileName, "w");
113  // Check Qt-layer
114  if (c+=SetRootResource(f,plugins[iPlugin++],plugins[iPlugin++],plugins[iPlugin++])) {
115  c+=SetRootResource(f,plugins[iPlugin++],plugins[iPlugin++],plugins[iPlugin++]);
116  // Check Qt-extension
117  if (c+=SetRootResource(f,plugins[iPlugin++],plugins[iPlugin++],plugins[iPlugin++])) {
118  if (c+=SetRootResource(f,plugins[iPlugin++],plugins[iPlugin++],plugins[iPlugin++])) {
119  // Set Qt extension
120  // skip Qt-layer setting
121  iPlugin++;iPlugin++;iPlugin++;
122  } else {
123  // Set Qt-layer
124  c+=SetRootResource(f,plugins[iPlugin++],plugins[iPlugin++],plugins[iPlugin++]);
125  }
126 
127  // Check QtGed
128  c+=SetRootResource(f,plugins[iPlugin++],plugins[iPlugin++],plugins[iPlugin++]);
129 
130  // Check QtGL
131  if (c+=SetRootResource(f,plugins[iPlugin++],plugins[iPlugin++],plugins[iPlugin++])) {
132  // Check Open Inventor
133  if (gSystem->DynamicPathName("libCoin",kTRUE)) {
134  c+=SetRootResource(f,plugins[iPlugin++],plugins[iPlugin++],plugins[iPlugin++],kTRUE);
135  } else {
136  fprintf( stderr, "\"Coin3d\" shared libraies has not beed detected\n");
137  fprintf( stderr, "Please, run \"source $STAR/QtRoot/qtgl/qtcoin/setup.csh\" script to set the Coin3D env.\n");
138  } } } }
139 
140  fclose(f);
141  if (c == 0) {
142  fprintf(stderr," No shared library to activate the Qt-layer has been detected.\n Please talk to your SysAdmin\n");
143  gSystem->Unlink(fileName.Data());
144  } else {
145  Long_t id; Long_t size; Long_t flags; Long_t modtime;
146  gSystem->GetPathInfo(fileName.Data(), &id, &size, &flags, &modtime);
147  if (size == 0) {
148  fprintf(stderr," ----------------------------------------------------------\n");
149  fprintf(stderr," The correct Qt/Root env has been detected.\n");
150  fprintf(stderr," ----------------------------------------------------------\n");
151  gSystem->Unlink(fileName.Data());
152  } else {
153  fprintf(stderr," ----------------------------------------------------------\n");
154  fprintf(stderr," The new version of ROOT resource file has been created: <%s>.\n",fileName.Data());
155  if (gSystem->AccessPathName(".rootrc")) {
156  fprintf(stderr," To active the Qt-layer - create a symlink \"ln -s %s .rootrc \" \n", fileName.Data());
157  } else {
158  fprintf(stderr," To active the Qt-layer - merge the existen \".rootrc\" file with \"%s\" \n", fileName.Data());
159  }
160  fprintf(stderr," and re-start your application\n");
161  fprintf(stderr," ----------------------------------------------------------\n");
162  }
163  }
164  // gEnv->Print();
165  // gEnv->SaveLevel(kEnvLocal);
166 }