GEMC  1.8
Geant4 Monte-Carlo Framework
g4dialog.cc
Go to the documentation of this file.
1 // %%%%%%%%%%
2 // Qt headers
3 // %%%%%%%%%%
4 #include <QtGui>
5 
6 
7 // %%%%%%%%%%%%%
8 // gemc headers
9 // %%%%%%%%%%%%%
10 #include "g4dialog.h"
11 
12 // %%%%%%%%%%
13 // G4 headers
14 // %%%%%%%%%%
15 #include "G4UIcommandTree.hh"
16 
17 g4dialog::g4dialog(QWidget *parent, gemc_opts *Opts) : QWidget(parent)
18 {
19  gemcOpt = Opts;
20  UImanager = G4UImanager::GetUIpointer();
21 
22 
23  // Layout:
24  //
25  // + +-------------------+ +
26  // | | | | |
27  // | | Tree | Help | |
28  // | | | | |
29  // | +-------------------+ |
30  // | +-------------------+ |
31  // | | | |
32  // | | promt history | |
33  // | +-------------------+ |
34  // | +-------------------+ |
35  // | | > promt area | |
36  // | +-------------------+ |
37  // +-----------------------+
38 
39 
40  // Vertical Splitter - Top and Bottom layouts
41  QSplitter *splitter = new QSplitter(Qt::Vertical);
42 
43 
44  // %%%%%%%%%%
45  // top layout
46  // %%%%%%%%%%
47  QWidget *topWidget = new QWidget(splitter);
48  QSplitter *treesplitter = new QSplitter(Qt::Horizontal);
49 
50 
51  // Tree + Help VBOX assembly
52  QVBoxLayout *helpLayout = new QVBoxLayout(treesplitter);
53 
54  // Left: The help tree
55  fHelpTreeWidget = new QTreeWidget();
56  fHelpTreeWidget = CreateHelpTree();
57  if(fHelpTreeWidget)
58  helpLayout->addWidget(fHelpTreeWidget);
59 
60 
61  // Right: the help on individual commands
62  fHelpArea = new QTextEdit(treesplitter);
63  fHelpArea->setReadOnly(true);
64 
65 
66  // treesplitter size
67  QList<int> tlist;
68  tlist.append( 400 );
69  tlist.append( 400 );
70  treesplitter->setSizes(tlist);
71 
72 
73  QVBoxLayout *layoutTop = new QVBoxLayout(topWidget);
74  layoutTop->addWidget(treesplitter);
75 
76 
77  // %%%%%%%%%%%%%
78  // bottom layout
79  // %%%%%%%%%%%%%
80  QWidget *bottomWidget = new QWidget(splitter);
81 
82  // history area
83  fCommandHistoryArea = new QListWidget();
84  fCommandHistoryArea->setSelectionMode(QAbstractItemView::SingleSelection);
85  fCommandHistoryArea->installEventFilter(this);
86  connect(fCommandHistoryArea, SIGNAL(itemSelectionChanged()), SLOT(CommandHistoryCallback()));
87 
88 
89  // manual command
90  // QLabel *fCommandLabel = new QLabel("Press Enter to Execute Command:");
91  fCommandArea = new QLineEdit();
92  fCommandArea->installEventFilter(this);
93  fCommandArea->activateWindow();
94  fCommandArea->setFocusPolicy ( Qt::StrongFocus );
95  fCommandArea->setFocus(Qt::TabFocusReason);
96  connect(fCommandArea, SIGNAL(returnPressed()), SLOT(CommandEnteredCallback()));
97 
98  // putting all together
99  QVBoxLayout *layoutBottom = new QVBoxLayout(bottomWidget);
100  layoutBottom->addWidget(new QLabel("History:"));
101  layoutBottom->addWidget(fCommandHistoryArea);
102  layoutBottom->addWidget(new QLabel("Press Enter to Execute Command:"));
103  layoutBottom->addWidget(fCommandArea);
104 
105 
106  // splitter size
107  QList<int> list;
108  list.append( 500 );
109  list.append( 300 );
110  splitter->setSizes(list);
111 
112 
113 
114  // %%%%%%%%%%%
115  // all layouts
116  // %%%%%%%%%%%
117  QVBoxLayout *mainLayout = new QVBoxLayout;
118  mainLayout->addWidget(splitter);
119  setLayout(mainLayout);
120 
121 }
122 
123 
124 
125 
127 {
128  string hd_msg = gemcOpt->args["LOG_MSG"].args ;
129  double VERB = gemcOpt->args["GEO_VERBOSITY"].arg ;
130  if(VERB>2)
131  cout << hd_msg << " g4 Dialog Widget Deleted." << endl;
132 
133 }
134 
135 
136 // execute history item
137 void g4dialog::CommandHistoryCallback()
138 {
139  QListWidgetItem* item = NULL;
140  if (!fCommandHistoryArea)
141  return ;
142 
143 
144 
145  QList<QListWidgetItem *> list = fCommandHistoryArea->selectedItems();
146  if(list.isEmpty())
147  return;
148 
149  item = list.first();
150  if(!item)
151  return;
152 
153  fCommandArea->setText(item->text());
154 
155 }
156 
157 
158 
159 // execute G4 manual command
160 void g4dialog::CommandEnteredCallback()
161 {
162 
163  if(fCommandArea->text().trimmed() != "")
164  {
165  fCommandHistoryArea->addItem(fCommandArea->text());
166 
167  UImanager->ApplyCommand(fCommandArea->text().toStdString().c_str());
168 
169  fCommandHistoryArea->clearSelection();
170  fCommandHistoryArea->setCurrentItem(NULL);
171  fCommandArea->setText("");
172  }
173 }
174 
175 
176 
177 
178 QTreeWidget* g4dialog::CreateHelpTree()
179 {
180  if(UImanager==NULL) return NULL;
181  G4UIcommandTree *treeTop = UImanager->GetTree();
182 
183 
184  // build widget
185  fHelpTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
186 
187  QStringList labels;
188  labels << QString("Command");
189  fHelpTreeWidget->setHeaderLabels(labels);
190 
191  G4int treeSize = treeTop->GetTreeEntry();
192  QTreeWidgetItem * newItem;
193 
194  for (int a=0;a<treeSize;a++)
195  {
196  // Creating new item
197  newItem = new QTreeWidgetItem(fHelpTreeWidget);
198  newItem->setText(0, QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).trimmed());
199 
200  // look for childs
201  CreateChildTree(newItem, treeTop->GetTree(a+1));
202  }
203 
204 
205  connect(fHelpTreeWidget, SIGNAL(itemSelectionChanged() ), this, SLOT(HelpTreeClicCallback() ) );
206  connect(fHelpTreeWidget, SIGNAL(itemDoubleClicked (QTreeWidgetItem*, int) ), this, SLOT(HelpTreeDoubleClicCallback()));
207 
208  return fHelpTreeWidget;
209 }
210 
211 
212 
213 
214 void g4dialog::CreateChildTree(QTreeWidgetItem *aParent,G4UIcommandTree *aCommandTree)
215 {
216  if (aParent == NULL) return;
217  if (aCommandTree == NULL) return;
218 
219 
220  // Creating new item
221  QTreeWidgetItem * newItem;
222 
223  // Get the Sub directories
224  for (int a=0;a<aCommandTree->GetTreeEntry();a++)
225  {
226 
227  newItem = new QTreeWidgetItem(aParent);
228  newItem->setText(0,QString((char*)(aCommandTree->GetTree(a+1)->GetPathName()).data()).trimmed());
229 
230  CreateChildTree(newItem,aCommandTree->GetTree(a+1));
231  }
232 
233  // Get the Commands
234  for (int a=0;a<aCommandTree->GetCommandEntry();a++)
235  {
236  QStringList stringList;
237  newItem = new QTreeWidgetItem(aParent);
238  newItem->setText(0, QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).trimmed());
239  newItem->setExpanded(false);
240 
241  }
242 }
243 
244 
245 // displays help on the right help area
246 void g4dialog::HelpTreeClicCallback()
247 {
248  QTreeWidgetItem* item = NULL;
249  if(!fHelpTreeWidget || !fHelpArea)
250  return;
251 
252  QList<QTreeWidgetItem *> list = fHelpTreeWidget->selectedItems();
253  if(list.isEmpty())
254  return;
255 
256  item = list.first();
257  if(!item)
258  return;
259 
260 
261  if(UImanager==NULL) return;
262  G4UIcommandTree *treeTop = UImanager->GetTree();
263 
264  string itemText = item->text(0).toStdString();
265  G4UIcommand* command = treeTop->FindPath(itemText.c_str());
266 
267  // if it's a valid command, display the help
268  if(command)
269  {
270  fHelpArea->setText(GetCommandList(command));
271  }
272  else
273  {
274  // this is a command
275  G4UIcommandTree* path = treeTop->FindCommandTree(itemText.c_str());
276  if(path)
277  {
278  // this is not a command, this is a sub directory
279  // We display the Title
280  fHelpArea->setText(path->GetTitle().data());
281  }
282  }
283 }
284 
285 
286 QString g4dialog::GetCommandList (const G4UIcommand *aCommand)
287 {
288 
289  QString txt ="";
290  if (aCommand == NULL)
291  return txt;
292 
293  G4String commandPath = aCommand->GetCommandPath();
294  G4String rangeString = aCommand->GetRange();
295  G4int n_guidanceEntry = aCommand->GetGuidanceEntries();
296  G4int n_parameterEntry = aCommand->GetParameterEntries();
297 
298  if ((commandPath == "") &&
299  (rangeString == "") &&
300  (n_guidanceEntry == 0) &&
301  (n_parameterEntry == 0)) {
302  return txt;
303  }
304 
305  if((commandPath.length()-1)!='/')
306  {
307  txt += "Command " + QString((char*)(commandPath).data()) + "\n";
308  }
309  txt += "Guidance :\n";
310 
311  for( G4int i_thGuidance=0; i_thGuidance < n_guidanceEntry; i_thGuidance++ )
312  {
313  txt += QString((char*)(aCommand->GetGuidanceLine(i_thGuidance)).data()) + "\n";
314  }
315  if( ! rangeString.isNull() )
316  {
317  txt += " Range of parameters : " + QString((char*)(rangeString).data()) + "\n";
318  }
319  if( n_parameterEntry > 0 )
320  {
321  G4UIparameter *param;
322 
323  // Re-implementation of G4UIparameter.cc
324  for( G4int i_thParameter=0; i_thParameter<n_parameterEntry; i_thParameter++ )
325  {
326  param = aCommand->GetParameter(i_thParameter);
327  txt += "\nParameter : " + QString((char*)(param->GetParameterName()).data()) + "\n";
328  if( ! param->GetParameterGuidance().isNull() )
329  txt += QString((char*)(param->GetParameterGuidance()).data())+ "\n" ;
330  txt += " Parameter type : " + QString(QChar(param->GetParameterType())) + "\n";
331  if(param->IsOmittable())
332  {
333  txt += " Omittable : True\n";
334  }
335  else
336  {
337  txt += " Omittable : False\n";
338  }
339  if( param->GetCurrentAsDefault() )
340  {
341  txt += " Default value : taken from the current value\n";
342  }
343  else if( ! param->GetDefaultValue().isNull() )
344  {
345  txt += " Default value : " + QString((char*)(param->GetDefaultValue()).data())+ "\n";
346  }
347  if( ! param->GetParameterRange().isNull() )
348  {
349  txt += " Parameter range : " + QString((char*)(param->GetParameterRange()).data())+ "\n";
350  }
351  if( ! param->GetParameterCandidates().isNull() )
352  {
353  txt += " Candidates : " + QString((char*)(param->GetParameterCandidates()).data())+ "\n";
354  }
355  }
356  }
357  return txt;
358 }
359 
360 
361 
362 void g4dialog::HelpTreeDoubleClicCallback()
363 {
364  HelpTreeClicCallback();
365 
366  QTreeWidgetItem* item = NULL;
367 
368  if(!fHelpTreeWidget || !fHelpArea)
369  return ;
370 
371  QList<QTreeWidgetItem *> list = fHelpTreeWidget->selectedItems();
372  if(list.isEmpty())
373  return;
374 
375  item = list.first();
376  if(!item)
377  return;
378 
379 
380  fCommandArea->clear();
381  fCommandArea->setText(item->text(0));
382 }
383 
384 
385 
386 
387 
388 
389 
390 
391 
gemc_opts * gemcOpt
Definition: g4dialog.h:43
g4dialog(QWidget *parent, gemc_opts *)
Definition: g4dialog.cc:17
map< string, opts > args
Options map.
Definition: usage.h:68
~g4dialog()
Definition: g4dialog.cc:126
G4UImanager * UImanager
Definition: g4dialog.h:44