StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StCheckQtEnv.cxx
1 //*-- Author : Valery Fine(fine@bnl.gov) 27/10/2006
2 //
3 // $Id: StCheckQtEnv.cxx,v 1.10 2010/02/26 01:23:06 fine Exp $
4 // This class sets the Qt/Root environment "on fly" and
5 // generates the correct ROOT resource ".rootrc" file
6 // also
7 
8 #include "StCheckQtEnv.h"
9 #include "TSystem.h"
10 #include "TApplication.h"
11 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,00,0)
12 #include "TPRegexp.h"
13 #endif
14 #include "TEnv.h"
15 namespace {
16 class InitGraph {
17  public:
18  InitGraph() {
19  gEnv->SetValue("Gui.Backend", "qt");
20  gEnv->SetValue("Gui.Factory", "qtgui");
21  gApplication->NeedGraphicsLibs();
22  gApplication->InitializeGraphics();
23  }
24 };
25 }
26 
27 //__________________________________________________________________
28 FILE *StCheckQtEnv::OpeFileName(const char *fileNamePrototype)
29 {
30  // Open the file by the given file name
31  return fopen( (const char*)GetNewFileName(fileNamePrototype),"w");
32 }
33 //__________________________________________________________________
34 TString StCheckQtEnv::GetNewFileName(const char *fileNamePrototype)
35 {
36  // Find the filename for the given "fileNamePrototype"
37  TString fileName = fileNamePrototype;
38  gSystem->ExpandPathName(fileName);
39  // Find the file extenstion if any
40  Ssiz_t fileExtension = fileName.Last('.');
41  TString fileNameHold = fileName;
42  if ((fileExtension==0) || (fileExtension == -1)) {
43  fileExtension = fileName.Length();
44  }
45  Int_t counter = 0;
46  while (gSystem->AccessPathName(fileName.Data())==0) {
47  fileName = fileNameHold;
48  fileName.Insert(fileExtension,Form(".%d",counter++));
49  }
50  return fileName;
51 }
52 
53 //__________________________________________________________________
54 //Int_t StCheckQtEnv::SetRootResource(FILE *file, const char *plugin,
55 // const char *lib, const char *full,Bool_t append)
56 Int_t StCheckQtEnv::SetRootResource(FILE *file, const char *plugin,
57  const char *lib, const char *full,Bool_t append)
58 {
59  fprintf(stderr," Testing the plugin <%s> from the lib = <%s>\n", plugin, lib);
60  Int_t success = 0;
61  TString fullName;
62  TString fullValue;
63  if (full) {
64 // fprintf(stderr," full = %s", full);
65  fullName = Form("lib%s",lib);
66  fullValue = Form(full,lib);
67  } else {
68  fullValue = lib;
69  }
70 
71  if ( !full || gSystem->DynamicPathName(fullName.Data(),kTRUE) ) {
72  // Check plugin
73  success = 1;
74  TString currentPlugin = gEnv->GetValue(plugin,"none");
75 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,00,0)
76  TPRegexp exp(Form("\\b%s\\b",lib));
77 #else
78  TString exp(Form(" %s ",lib));
79 #endif
80  if ( !currentPlugin.Contains(exp) )
81  {
82  TString p = "+";
83  if (append) {
84  p +=plugin;
85  plugin = p.Data();
86  // fprintf(stderr, "Appending %s\n", plugin);
87  }
88  gEnv->SetValue(plugin,fullValue);
89  if (file) {
90  fputs(Form("%s %s\n",plugin,fullValue.Data()), file);
91  }
92  } else {
93  // fprintf(stderr, "Found %s with <%s>\n", currentPlugin.Data(), Form("\\s+?%s\\s+?",lib));
94  }
95  }
96  return success;
97 }
98 
99 //__________________________________________________________________
100 Long_t StCheckQtEnv::SetQtEnv(bool checkCoin) {
101  //------------------
102  // Check GED library
103  // Return value:
104  // -1 - Wrong Qt env. need SysAdmit to fix it
105  // 0 - Correct env.
106  // > 0 - Wrong env. The user should fix his/her env.
107  //------------------
108  static InitGraph init_graph;
109  const char *plugins[] = {
110  "Plugin.TVirtualX"
111  , "GQt"
112  , " qt TGQt %s \"TGQt(const char*,const char*)\""
113 
114  , "Gui.Backend", "qt", 0
115 
116  , "Plugin.TGuiFactory"
117  , "QtRootGui"
118  , " qtgui TQtGUIFactory %s \"TQtGUIFactory()\""
119 
120  , "Gui.Factory", "qtgui", 0
121 
122  , "Gui.Factory", "qt" , 0
123 
124  , "Plugin.TVirtualPadEditor"
125  , "QtGed"
126  , " Ged TQtGedEditor %s \"TQtGedEditor(TCanvas*)\""
127 
128  , "Plugin.TVirtualViewer3D"
129  , "RQTGL"
130  , " ogl TQtRootViewer3D %s \"TQtRootViewer3D(TVirtualPad*)\""
131 
132  , "Plugin.TVirtualViewer3D"
133  , "RQIVTGL"
134  , " oiv TQtRootCoinViewer3D %s \"TQtRootCoinViewer3D(TVirtualPad*)\""
135  , "", "", ""
136  };
137  Int_t iPlugin = 0;
138  Long_t c = 0;
139  TString fileName = GetNewFileName("rootrc");
140  FILE *f = fopen((const char *)fileName, "w");
141  if (f) {
142  // Check Qt-layer
143  if ((c+=SetRootResource(f,plugins[iPlugin],plugins[iPlugin+1],plugins[iPlugin+2]))) {
144  iPlugin+=3;
145  c+=SetRootResource(f,plugins[iPlugin],plugins[iPlugin+1],plugins[iPlugin+2]);
146  iPlugin+=3;
147  // Check Qt-extension
148  if ((c+=SetRootResource(f,plugins[iPlugin],plugins[iPlugin+1],plugins[iPlugin+2]))) {
149  iPlugin+=3;
150  if ((c+=SetRootResource(f,plugins[iPlugin],plugins[iPlugin+1],plugins[iPlugin+2]))) {
151  // iPlugin+=3;
152  // Set Qt extension
153  // skip Qt-layer setting
154  iPlugin+=3;
155  } else {
156  // Set Qt-layer
157  iPlugin+=3;
158  c+=SetRootResource(f,plugins[iPlugin],plugins[iPlugin+1],plugins[iPlugin+2]);
159  }
160 
161  // Check QtGed
162  iPlugin+=3;
163  c+=SetRootResource(f,plugins[iPlugin],plugins[iPlugin+1],plugins[iPlugin+2]);
164  // Check QtGL
165  iPlugin+=3;
166 
167  if ((c+=SetRootResource(f,plugins[iPlugin],plugins[iPlugin+1],plugins[iPlugin+2]))) {
168  // Check Open Inventor
169  Long_t savedC = c;
170 
171  if (checkCoin) {
172  if (gSystem->DynamicPathName("libCoin",kTRUE)) {
173  iPlugin+=3;
174  c+=SetRootResource(f,plugins[iPlugin],plugins[iPlugin+1],plugins[iPlugin+2],kTRUE);
175  } else {
176  fprintf(stderr," ----------------------------------------------------------\n");
177  fprintf(stderr," ATTENTION : \n");
178  fprintf(stderr,"\"Coin3d\" shared libraries has not beed detected\n");
179  fprintf(stderr," Please, run:\n\n");
180  fprintf(stderr,"===== \"source $STAR/QtRoot/qtgl/qtcoin/setup.csh\" =====\n\n");
181  fprintf(stderr," script to activated the advanced (recommended) Coin3D env.\n");
182  fprintf(stderr," ----------------------------------------------------------\n");
183  fprintf(stderr," and re-start your application\n");
184  fprintf(stderr," ----------------------------------------------------------\n");
185  c = savedC; // no libCoin. we still can use it
186  gSystem->Sleep(5000);
187  } }
188  } } }
189 
190  fclose(f);
191  } else {
192  c = 1;
193  }
194  if (c == 0) {
195  fprintf(stderr," No shared library to activate the Qt-layer has been detected.\n");
196  fprintf(stderr," Please talk to your SysAdmin\n");
197  gSystem->Unlink(fileName.Data());
198  c = -1;
199  } else {
200  Long_t id; Long_t size; Long_t flags; Long_t modtime;
201  gSystem->GetPathInfo(fileName.Data(), &id, &size, &flags, &modtime);
202  c = size;
203  if (size == 0) {
204  fprintf(stderr," ----------------------------------------------------------\n");
205  fprintf(stderr," The correct Qt/Root env has been detected.\n");
206  fprintf(stderr," ----------------------------------------------------------\n");
207  gSystem->Unlink(fileName.Data());
208  } else {
209  fprintf(stderr," ----------------------------------------------------------\n");
210  fprintf(stderr," ATTENTION : \n");
211  fprintf(stderr," The new version of ROOT resource file has been created: <%s>.\n",fileName.Data());
212  if (gSystem->AccessPathName(".rootrc")) {
213  fprintf(stderr," To active the Qt-layer - create a symlink:\n");
214  fprintf(stderr,"============ \"ln -s %s .rootrc \" ====================\n", fileName.Data());
215  } else {
216  fprintf(stderr," To active the Qt-layer - **merge** the existen \".rootrc\" file with \"%s\" \n", fileName.Data());
217  }
218  fprintf(stderr," and re-start your application\n");
219  fprintf(stderr," ----------------------------------------------------------\n");
220  }
221  }
222  return c;
223  // gEnv->Print();
224  // gEnv->SaveLevel(kEnvLocal);
225 }