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