1 | // |
---|
2 | // ******************************************************************** |
---|
3 | // * License and Disclaimer * |
---|
4 | // * * |
---|
5 | // * The Geant4 software is copyright of the Copyright Holders of * |
---|
6 | // * the Geant4 Collaboration. It is provided under the terms and * |
---|
7 | // * conditions of the Geant4 Software License, included in the file * |
---|
8 | // * LICENSE and available at http://cern.ch/geant4/license . These * |
---|
9 | // * include a list of copyright holders. * |
---|
10 | // * * |
---|
11 | // * Neither the authors of this software system, nor their employing * |
---|
12 | // * institutes,nor the agencies providing financial support for this * |
---|
13 | // * work make any representation or warranty, express or implied, * |
---|
14 | // * regarding this software system or assume any liability for its * |
---|
15 | // * use. Please see the license in the file LICENSE and URL above * |
---|
16 | // * for the full disclaimer and the limitation of liability. * |
---|
17 | // * * |
---|
18 | // * This code implementation is the result of the scientific and * |
---|
19 | // * technical work of the GEANT4 collaboration. * |
---|
20 | // * By using, copying, modifying or distributing the software (or * |
---|
21 | // * any work based on the software) you agree to acknowledge its * |
---|
22 | // * use in resulting scientific publications, and indicate your * |
---|
23 | // * acceptance of all terms of the Geant4 Software license. * |
---|
24 | // ******************************************************************** |
---|
25 | // |
---|
26 | // |
---|
27 | // $Id: G4UIQt.cc,v 1.14 2007/05/29 11:09:49 $ |
---|
28 | // GEANT4 tag $Name: geant4-08-01 $ |
---|
29 | // |
---|
30 | // L. Garnier |
---|
31 | |
---|
32 | //#define DEBUG |
---|
33 | |
---|
34 | #ifdef G4UI_BUILD_QT_SESSION |
---|
35 | |
---|
36 | #include "G4Types.hh" |
---|
37 | |
---|
38 | #include <string.h> |
---|
39 | |
---|
40 | #include "G4UIQt.hh" |
---|
41 | #include "G4UImanager.hh" |
---|
42 | #include "G4StateManager.hh" |
---|
43 | #include "G4UIcommandTree.hh" |
---|
44 | #include "G4UIcommandStatus.hh" |
---|
45 | |
---|
46 | #include "G4Qt.hh" |
---|
47 | |
---|
48 | #include <QtGui/qapplication.h> |
---|
49 | #include <QtGui/qwidget.h> |
---|
50 | #include <QtGui/qmenu.h> |
---|
51 | #include <QtGui/qmenubar.h> |
---|
52 | #include <QtGui/qboxlayout.h> |
---|
53 | #include <QtGui/qpushbutton.h> |
---|
54 | #include <QtGui/qlabel.h> |
---|
55 | #include <QtGui/qsplitter.h> |
---|
56 | #include <QtGui/qscrollbar.h> |
---|
57 | #include <QtGui/qdialog.h> |
---|
58 | #include <QtGui/qevent.h> |
---|
59 | |
---|
60 | #include <stdlib.h> |
---|
61 | |
---|
62 | // Pourquoi Static et non variables de classe ? |
---|
63 | static G4bool exitSession = true; |
---|
64 | static G4bool exitPause = true; |
---|
65 | |
---|
66 | /** Build a Qt window with a menubar, output area and promt area<br> |
---|
67 | <pre> |
---|
68 | +-----------------------+ |
---|
69 | |exit menu| | |
---|
70 | | | |
---|
71 | | +-------------------+ | |
---|
72 | | | | | |
---|
73 | | | Output area | | |
---|
74 | | | | | |
---|
75 | | +-------------------+ | |
---|
76 | | | clear | | |
---|
77 | | +-------------------+ | |
---|
78 | | | promt history | | |
---|
79 | | +-------------------+ | |
---|
80 | | +-------------------+ | |
---|
81 | | |> promt area | | |
---|
82 | | +-------------------+ | |
---|
83 | +-----------------------+ |
---|
84 | </pre> |
---|
85 | */ |
---|
86 | G4UIQt::G4UIQt ( |
---|
87 | int argc |
---|
88 | ,char** argv |
---|
89 | ) |
---|
90 | :fHelpDialog(NULL) |
---|
91 | { |
---|
92 | G4Qt* interactorManager = G4Qt::getInstance (); |
---|
93 | G4UImanager* UI = G4UImanager::GetUIpointer(); |
---|
94 | if(UI!=NULL) UI->SetSession(this); |
---|
95 | |
---|
96 | fMainWindow = new QMainWindow(); |
---|
97 | fMainWindow->setWindowTitle( "G4UI Session" ); |
---|
98 | fMainWindow->resize(800,600); |
---|
99 | fMainWindow->move(QPoint(50,100)); |
---|
100 | |
---|
101 | QSplitter *splitter = new QSplitter(Qt::Vertical); |
---|
102 | fTextArea = new QTextEdit(); |
---|
103 | QPushButton *clearButton = new QPushButton("clear"); |
---|
104 | connect(clearButton, SIGNAL(clicked()), SLOT(ClearButtonCallback())); |
---|
105 | |
---|
106 | fCommandHistoryArea = new QListWidget(); |
---|
107 | fCommandHistoryArea->setSelectionMode(QAbstractItemView::SingleSelection); |
---|
108 | connect(fCommandHistoryArea, SIGNAL(itemSelectionChanged()), SLOT(CommandHistoryCallback())); |
---|
109 | fCommandHistoryArea->installEventFilter(this); |
---|
110 | fCommandLabel = new QLabel(); |
---|
111 | |
---|
112 | fCommandArea = new QLineEdit(); |
---|
113 | fCommandArea->installEventFilter(this); |
---|
114 | fCommandArea->activateWindow(); |
---|
115 | connect(fCommandArea, SIGNAL(returnPressed()), SLOT(CommandEnteredCallback())); |
---|
116 | // fCommandArea->setFocusPolicy ( Qt::StrongFocus ); |
---|
117 | // fCommandArea->setFocus(Qt::TabFocusReason); |
---|
118 | fTextArea->setReadOnly(true); |
---|
119 | |
---|
120 | |
---|
121 | // Set layouts |
---|
122 | |
---|
123 | QWidget* topWidget = new QWidget(); |
---|
124 | QVBoxLayout *layoutTop = new QVBoxLayout; |
---|
125 | |
---|
126 | QWidget* bottomWidget = new QWidget(); |
---|
127 | QVBoxLayout *layoutBottom = new QVBoxLayout; |
---|
128 | |
---|
129 | |
---|
130 | layoutTop->addWidget(fTextArea); |
---|
131 | layoutTop->addWidget(clearButton); |
---|
132 | topWidget->setLayout(layoutTop); |
---|
133 | |
---|
134 | layoutBottom->addWidget(fCommandHistoryArea); |
---|
135 | layoutBottom->addWidget(fCommandLabel); |
---|
136 | layoutBottom->addWidget(fCommandArea); |
---|
137 | bottomWidget->setLayout(layoutBottom); |
---|
138 | |
---|
139 | |
---|
140 | splitter->addWidget(topWidget); |
---|
141 | splitter->addWidget(bottomWidget); |
---|
142 | fMainWindow->setCentralWidget(splitter); |
---|
143 | |
---|
144 | // Add a quit subMenu |
---|
145 | QMenu *fileMenu = fMainWindow->menuBar()->addMenu("File"); |
---|
146 | fileMenu->addAction("Quitter", fMainWindow, SLOT(close())); |
---|
147 | |
---|
148 | // Add a Help menu |
---|
149 | QMenu *helpMenu = fMainWindow->menuBar()->addMenu("Help"); |
---|
150 | helpMenu->addAction("Show Help", this, SLOT(ShowHelpCallback())); |
---|
151 | |
---|
152 | // Set the splitter size. The fTextArea sould be 2/3 on the fMainWindow |
---|
153 | QList<int> vals = splitter->sizes(); |
---|
154 | if(vals.size()==2) { |
---|
155 | vals[0] = (splitter->orientation()==Qt::Vertical ? splitter->height() : splitter->width())*3/4; |
---|
156 | vals[1] = (splitter->orientation()==Qt::Vertical ? splitter->height() : splitter->width())*1/4; |
---|
157 | splitter->setSizes(vals); |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | if(UI!=NULL) UI->SetCoutDestination(this); // TO KEEP |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | |
---|
166 | G4UIQt::~G4UIQt( |
---|
167 | ) |
---|
168 | { |
---|
169 | G4UImanager* UI = G4UImanager::GetUIpointer(); // TO KEEP |
---|
170 | if(UI!=NULL) { // TO KEEP |
---|
171 | UI->SetSession(NULL); // TO KEEP |
---|
172 | UI->SetCoutDestination(NULL); // TO KEEP |
---|
173 | } |
---|
174 | |
---|
175 | if (fMainWindow!=NULL) |
---|
176 | delete fMainWindow; |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | |
---|
181 | /** Start the Qt main loop |
---|
182 | */ |
---|
183 | G4UIsession* G4UIQt::SessionStart ( |
---|
184 | ) |
---|
185 | { |
---|
186 | |
---|
187 | G4Qt* interactorManager = G4Qt::getInstance (); |
---|
188 | fMainWindow->show(); |
---|
189 | Prompt("session"); |
---|
190 | exitSession = false; |
---|
191 | |
---|
192 | |
---|
193 | printf("disable secondary loop\n"); |
---|
194 | interactorManager->DisableSecondaryLoop (); // TO KEEP |
---|
195 | ((QApplication*)interactorManager->GetMainInteractor())->exec(); |
---|
196 | // on ne passe pas le dessous ? FIXME ???? |
---|
197 | // je ne pense pas 13/06 |
---|
198 | |
---|
199 | // void* event; // TO KEEP |
---|
200 | // while((event = interactorManager->GetEvent())!=NULL) { // TO KEEP |
---|
201 | // interactorManager->DispatchEvent(event); // TO KEEP |
---|
202 | // if(exitSession==true) break; // TO KEEP |
---|
203 | // } // TO KEEP |
---|
204 | |
---|
205 | interactorManager->EnableSecondaryLoop (); |
---|
206 | printf("enable secondary loop\n"); |
---|
207 | return this; |
---|
208 | } |
---|
209 | |
---|
210 | |
---|
211 | /** Display the prompt in the prompt area |
---|
212 | @param aPrompt : string to display as the promt label |
---|
213 | //FIXME : probablement inutile puisque le seul a afficher qq chose d'autre |
---|
214 | que "session" est SecondaryLoop() |
---|
215 | */ |
---|
216 | void G4UIQt::Prompt ( |
---|
217 | G4String aPrompt |
---|
218 | ) |
---|
219 | { |
---|
220 | if (!aPrompt) return; |
---|
221 | |
---|
222 | fCommandLabel->setText((char*)aPrompt.data()); |
---|
223 | } |
---|
224 | |
---|
225 | |
---|
226 | void G4UIQt::SessionTerminate ( |
---|
227 | ) |
---|
228 | { |
---|
229 | G4Qt* interactorManager = G4Qt::getInstance (); |
---|
230 | fMainWindow->close(); |
---|
231 | ((QApplication*)interactorManager->GetMainInteractor())->exit(); |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | |
---|
236 | /** |
---|
237 | Called by intercoms/src/G4UImanager.cc<br> |
---|
238 | Called by visualization/management/src/G4VisCommands.cc with "EndOfEvent" argument<br> |
---|
239 | It have to pause the session command terminal.<br> |
---|
240 | Call SecondaryLoop to wait for exit event<br> |
---|
241 | @param aState |
---|
242 | @see : G4VisCommandReviewKeptEvents::SetNewValue |
---|
243 | */ |
---|
244 | void G4UIQt::PauseSessionStart ( |
---|
245 | G4String aState |
---|
246 | ) |
---|
247 | { |
---|
248 | if (!aState) return; |
---|
249 | |
---|
250 | printf("G4UIQt::PauseSessionStart\n"); |
---|
251 | if(aState=="G4_pause> ") { // TO KEEP |
---|
252 | SecondaryLoop ("Pause, type continue to exit this state"); // TO KEEP |
---|
253 | } // TO KEEP |
---|
254 | |
---|
255 | if(aState=="EndOfEvent") { // TO KEEP |
---|
256 | // Picking with feed back in event data Done here !!! |
---|
257 | SecondaryLoop ("End of event, type continue to exit this state"); // TO KEEP |
---|
258 | } // TO KEEP |
---|
259 | } |
---|
260 | |
---|
261 | |
---|
262 | |
---|
263 | /** |
---|
264 | Begin the secondary loop |
---|
265 | @param a_prompt : label to display as the prompt label |
---|
266 | */ |
---|
267 | void G4UIQt::SecondaryLoop ( |
---|
268 | G4String aPrompt |
---|
269 | ) |
---|
270 | { |
---|
271 | if (!aPrompt) return; |
---|
272 | |
---|
273 | printf("G4UIQt::SecondaryLoop\n"); |
---|
274 | G4Qt* interactorManager = G4Qt::getInstance (); // TO KEEP ? |
---|
275 | Prompt(aPrompt); // TO KEEP |
---|
276 | exitPause = false; // TO KEEP |
---|
277 | void* event; // TO KEEP |
---|
278 | while((event = interactorManager->GetEvent())!=NULL) { // TO KEEP |
---|
279 | interactorManager->DispatchEvent(event); // TO KEEP |
---|
280 | if(exitPause==true) break; // TO KEEP |
---|
281 | } // TO KEEP |
---|
282 | Prompt("session"); // TO KEEP |
---|
283 | } |
---|
284 | |
---|
285 | |
---|
286 | |
---|
287 | /** |
---|
288 | Receive a cout from Geant4. We have to display it in the cout zone |
---|
289 | @param aString : label to add in the display area |
---|
290 | @return 0 |
---|
291 | */ |
---|
292 | G4int G4UIQt::ReceiveG4cout ( |
---|
293 | G4String aString |
---|
294 | ) |
---|
295 | { |
---|
296 | if (!aString) return 0; |
---|
297 | printf(" **************** G4 Cout : %s \n",(char*)aString.data()); |
---|
298 | fTextArea->append(QString((char*)aString.data()).trimmed()); |
---|
299 | fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum()); |
---|
300 | return 0; |
---|
301 | } |
---|
302 | |
---|
303 | |
---|
304 | /** |
---|
305 | Receive a cerr from Geant4. We have to display it in the cout zone |
---|
306 | @param aString : label to add in the display area |
---|
307 | @return 0 |
---|
308 | */ |
---|
309 | G4int G4UIQt::ReceiveG4cerr ( |
---|
310 | G4String aString |
---|
311 | ) |
---|
312 | { |
---|
313 | if (!aString) return 0; |
---|
314 | |
---|
315 | QColor previousColor = fTextArea->textColor(); |
---|
316 | fTextArea->setTextColor(Qt::red); |
---|
317 | fTextArea->append(QString((char*)aString.data()).trimmed()); |
---|
318 | fTextArea->setTextColor(previousColor); |
---|
319 | fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum()); |
---|
320 | return 0; |
---|
321 | } |
---|
322 | |
---|
323 | |
---|
324 | |
---|
325 | /** |
---|
326 | Add a new menu to the menu bar |
---|
327 | @param aName name of menu |
---|
328 | @param aLabel label to display |
---|
329 | */ |
---|
330 | void G4UIQt::AddMenu ( |
---|
331 | const char* aName |
---|
332 | ,const char* aLabel |
---|
333 | ) |
---|
334 | { |
---|
335 | if (aName == NULL) return; |
---|
336 | if (aLabel == NULL) return; |
---|
337 | |
---|
338 | QMenu *fileMenu = new QMenu(aLabel); |
---|
339 | fMainWindow->menuBar()->insertMenu(fMainWindow->menuBar()->actions().last(),fileMenu); |
---|
340 | AddInteractor (aName,(G4Interactor)fileMenu); |
---|
341 | } |
---|
342 | |
---|
343 | |
---|
344 | /** |
---|
345 | Add a new button to a menu |
---|
346 | @param aMenu : parent menu |
---|
347 | @param aLabel : label to display |
---|
348 | @param aCommand : command to execute as a callback |
---|
349 | */ |
---|
350 | void G4UIQt::AddButton ( |
---|
351 | const char* aMenu |
---|
352 | ,const char* aLabel |
---|
353 | ,const char* aCommand |
---|
354 | ) |
---|
355 | { |
---|
356 | if(aMenu==NULL) return; // TO KEEP |
---|
357 | if(aLabel==NULL) return; // TO KEEP |
---|
358 | if(aCommand==NULL) return; // TO KEEP |
---|
359 | |
---|
360 | QMenu *parent = (QMenu*)GetInteractor(aMenu); |
---|
361 | if(parent==NULL) return; |
---|
362 | |
---|
363 | QSignalMapper *signalMapper = new QSignalMapper(this); |
---|
364 | QAction *action = parent->addAction(aLabel, signalMapper, SLOT(map())); |
---|
365 | signalMapper->setMapping(action, QString(aCommand)); |
---|
366 | connect(signalMapper, SIGNAL(mapped(const QString &)),this, SLOT(ButtonCallback(const QString&))); |
---|
367 | } |
---|
368 | |
---|
369 | |
---|
370 | |
---|
371 | |
---|
372 | /** |
---|
373 | Open the help dialog in a separate window.<br> |
---|
374 | This will be display as a tree widget.<br> |
---|
375 | Implementation of <b>void G4VBasicShell::TerminalHelp(G4String newCommand)</b> |
---|
376 | |
---|
377 | @param newCommand : open the tree widget item on this command if is set |
---|
378 | */ |
---|
379 | void G4UIQt::TerminalHelp( |
---|
380 | G4String newCommand |
---|
381 | ) |
---|
382 | { |
---|
383 | // Create the help dialog |
---|
384 | if (!fHelpDialog) { |
---|
385 | fHelpDialog = new QDialog; |
---|
386 | |
---|
387 | QSplitter *splitter = new QSplitter(Qt::Horizontal); |
---|
388 | fHelpArea = new QTextEdit(); |
---|
389 | QPushButton *exitButton = new QPushButton("Exit"); |
---|
390 | connect(exitButton, SIGNAL(clicked()), fHelpDialog,SLOT(close())); |
---|
391 | fHelpArea->setReadOnly(true); |
---|
392 | |
---|
393 | // the help tree |
---|
394 | G4UImanager* UI = G4UImanager::GetUIpointer(); |
---|
395 | if(UI==NULL) return; |
---|
396 | G4UIcommandTree * treeTop = UI->GetTree(); |
---|
397 | |
---|
398 | // build widget |
---|
399 | fHelpTreeWidget = new QTreeWidget(); |
---|
400 | fHelpTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection); |
---|
401 | fHelpTreeWidget->setColumnCount(2); |
---|
402 | fHelpTreeWidget->setColumnHidden(1,true); |
---|
403 | QStringList labels; |
---|
404 | labels << QString("Command") << QString("Description"); |
---|
405 | fHelpTreeWidget->setHeaderLabels(labels); |
---|
406 | |
---|
407 | QList<QTreeWidgetItem *> items; |
---|
408 | G4int treeSize = treeTop->GetTreeEntry(); |
---|
409 | QTreeWidgetItem * newItem; |
---|
410 | for (int a=0;a<treeSize;a++) { |
---|
411 | // Creating new item |
---|
412 | QStringList stringList; |
---|
413 | stringList << QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).trimmed() ; |
---|
414 | stringList << QString((char*)(treeTop->GetTree(a+1)->GetTitle()).data()).trimmed() ; |
---|
415 | |
---|
416 | newItem = new QTreeWidgetItem(stringList); |
---|
417 | |
---|
418 | // look for childs |
---|
419 | CreateChildTree(newItem,treeTop->GetTree(a+1)); |
---|
420 | items.append(newItem); |
---|
421 | } |
---|
422 | fHelpTreeWidget->insertTopLevelItems(0, items); |
---|
423 | |
---|
424 | connect(fHelpTreeWidget, SIGNAL(itemSelectionChanged ()),this, SLOT(HelpTreeClicCallback())); |
---|
425 | connect(fHelpTreeWidget, SIGNAL(itemDoubleClicked ()),this, SLOT(HelpTreeDoubleClicCallback())); |
---|
426 | |
---|
427 | // Set layouts |
---|
428 | |
---|
429 | QVBoxLayout *vLayout = new QVBoxLayout; |
---|
430 | |
---|
431 | splitter->addWidget(fHelpTreeWidget); |
---|
432 | splitter->addWidget(fHelpArea); |
---|
433 | |
---|
434 | vLayout->addWidget(splitter); |
---|
435 | vLayout->addWidget(exitButton); |
---|
436 | fHelpDialog->setLayout(vLayout); |
---|
437 | |
---|
438 | } |
---|
439 | |
---|
440 | // Look for the choosen command "newCommand" |
---|
441 | size_t i = newCommand.index(" "); |
---|
442 | G4String targetCom=""; |
---|
443 | if( i != std::string::npos ) |
---|
444 | { |
---|
445 | G4String newValue = newCommand(i+1,newCommand.length()-(i+1)); |
---|
446 | newValue.strip(G4String::both); |
---|
447 | targetCom = ModifyToFullPathCommand( newValue ); |
---|
448 | } |
---|
449 | if (targetCom != "") { |
---|
450 | QTreeWidgetItem* findItem = NULL; |
---|
451 | for (int a=0;a<fHelpTreeWidget->topLevelItemCount();a++) { |
---|
452 | if (!findItem) { |
---|
453 | findItem = FindTreeItem(fHelpTreeWidget->topLevelItem(a),QString((char*)targetCom.data())); |
---|
454 | } |
---|
455 | } |
---|
456 | |
---|
457 | if (findItem) { |
---|
458 | |
---|
459 | //collapsed open item |
---|
460 | QList<QTreeWidgetItem *> selected; |
---|
461 | selected = fHelpTreeWidget->selectedItems(); |
---|
462 | if ( selected.count() != 0 ) { |
---|
463 | QTreeWidgetItem * tmp =selected.at( 0 ); |
---|
464 | while ( tmp) { |
---|
465 | tmp->setExpanded(false); |
---|
466 | tmp = tmp->parent(); |
---|
467 | } |
---|
468 | } |
---|
469 | |
---|
470 | // clear old selection |
---|
471 | fHelpTreeWidget->clearSelection(); |
---|
472 | |
---|
473 | // set new selection |
---|
474 | findItem->setSelected(true); |
---|
475 | |
---|
476 | // expand parent item |
---|
477 | while ( findItem) { |
---|
478 | findItem->setExpanded(true); |
---|
479 | findItem = findItem->parent(); |
---|
480 | } |
---|
481 | |
---|
482 | // Call the update of the right textArea |
---|
483 | HelpTreeClicCallback(); |
---|
484 | } |
---|
485 | } |
---|
486 | fHelpDialog->setWindowTitle("Help on commands"); |
---|
487 | fHelpDialog->resize(800,600); |
---|
488 | fHelpDialog->move(QPoint(400,150)); |
---|
489 | fHelpDialog->show(); |
---|
490 | fHelpDialog->raise(); |
---|
491 | fHelpDialog->activateWindow(); |
---|
492 | } |
---|
493 | |
---|
494 | |
---|
495 | |
---|
496 | /** Fill the Help Tree Widget |
---|
497 | @param aParent : parent item to fill |
---|
498 | @param aCommandTree : commandTree node associate with this part of the Tree |
---|
499 | */ |
---|
500 | void G4UIQt::CreateChildTree( |
---|
501 | QTreeWidgetItem *aParent |
---|
502 | ,G4UIcommandTree *aCommandTree |
---|
503 | ) |
---|
504 | { |
---|
505 | if (aParent == NULL) return; |
---|
506 | if (aCommandTree == NULL) return; |
---|
507 | |
---|
508 | |
---|
509 | // Creating new item |
---|
510 | QTreeWidgetItem * newItem; |
---|
511 | |
---|
512 | // Get the Sub directories |
---|
513 | for (int a=0;a<aCommandTree->GetTreeEntry();a++) { |
---|
514 | |
---|
515 | QStringList stringList; |
---|
516 | stringList << QString((char*)(aCommandTree->GetTree(a+1)->GetPathName()).data()).trimmed() ; |
---|
517 | stringList << QString((char*)(aCommandTree->GetTree(a+1)->GetTitle()).data()).trimmed() ; |
---|
518 | newItem = new QTreeWidgetItem(stringList); |
---|
519 | |
---|
520 | CreateChildTree(newItem,aCommandTree->GetTree(a+1)); |
---|
521 | aParent->addChild(newItem); |
---|
522 | } |
---|
523 | |
---|
524 | |
---|
525 | |
---|
526 | // Get the Commands |
---|
527 | |
---|
528 | for (int a=0;a<aCommandTree->GetCommandEntry();a++) { |
---|
529 | |
---|
530 | QStringList stringList; |
---|
531 | stringList << QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).trimmed() ; |
---|
532 | stringList << QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).trimmed() ; |
---|
533 | newItem = new QTreeWidgetItem(stringList); |
---|
534 | |
---|
535 | aParent->addChild(newItem); |
---|
536 | newItem->setExpanded(false); |
---|
537 | } |
---|
538 | } |
---|
539 | |
---|
540 | |
---|
541 | /** Find a treeItemWidget in the help tree |
---|
542 | @param aCommand item's String to look for |
---|
543 | @return item if found, NULL if not |
---|
544 | */ |
---|
545 | QTreeWidgetItem* G4UIQt::FindTreeItem( |
---|
546 | QTreeWidgetItem *aParent |
---|
547 | ,const QString& aCommand |
---|
548 | ) |
---|
549 | { |
---|
550 | if (aParent == NULL) return NULL; |
---|
551 | |
---|
552 | if (aParent->text(0) == aCommand) |
---|
553 | return aParent; |
---|
554 | |
---|
555 | QTreeWidgetItem * tmp = NULL; |
---|
556 | for (int a=0;a<aParent->childCount();a++) { |
---|
557 | if (!tmp) |
---|
558 | tmp = FindTreeItem(aParent->child(a),aCommand); |
---|
559 | } |
---|
560 | return tmp; |
---|
561 | } |
---|
562 | |
---|
563 | |
---|
564 | /** Build the command list parameters in a QString<br> |
---|
565 | Reimplement partialy the G4UIparameter.cc |
---|
566 | @param aCommand : command to list parameters |
---|
567 | @see G4UIparameter::List() |
---|
568 | @see G4UIcommand::List() |
---|
569 | @return the command list parameters, or "" if nothing |
---|
570 | */ |
---|
571 | QString G4UIQt::GetCommandList ( |
---|
572 | const G4UIcommand *aCommand |
---|
573 | ) |
---|
574 | { |
---|
575 | |
---|
576 | QString txt =""; |
---|
577 | if (aCommand == NULL) |
---|
578 | return txt; |
---|
579 | |
---|
580 | G4String commandPath = aCommand->GetCommandPath(); |
---|
581 | G4String rangeString = aCommand->GetRange(); |
---|
582 | G4int n_guidanceEntry = aCommand->GetGuidanceEntries(); |
---|
583 | G4int n_parameterEntry = aCommand->GetParameterEntries(); |
---|
584 | |
---|
585 | if ((commandPath == "") && |
---|
586 | (rangeString == "") && |
---|
587 | (n_guidanceEntry == 0) && |
---|
588 | (n_parameterEntry == 0)) { |
---|
589 | return txt; |
---|
590 | } |
---|
591 | |
---|
592 | if((commandPath.length()-1)!='/') { |
---|
593 | txt += "Command " + QString((char*)(commandPath).data()) + "\n"; |
---|
594 | } |
---|
595 | txt += "Guidance :\n"; |
---|
596 | |
---|
597 | for( G4int i_thGuidance=0; i_thGuidance < n_guidanceEntry; i_thGuidance++ ) { |
---|
598 | txt += QString((char*)(aCommand->GetGuidanceLine(i_thGuidance)).data()) + "\n"; |
---|
599 | } |
---|
600 | if( ! rangeString.isNull() ) { |
---|
601 | txt += " Range of parameters : " + QString((char*)(rangeString).data()) + "\n"; |
---|
602 | } |
---|
603 | if( n_parameterEntry > 0 ) { |
---|
604 | G4UIparameter *param; |
---|
605 | |
---|
606 | // Re-implementation of G4UIparameter.cc |
---|
607 | |
---|
608 | for( G4int i_thParameter=0; i_thParameter<n_parameterEntry; i_thParameter++ ) { |
---|
609 | param = aCommand->GetParameter(i_thParameter); |
---|
610 | txt += "\nParameter : " + QString((char*)(param->GetParameterName()).data()) + "\n"; |
---|
611 | if( ! param->GetParameterGuidance().isNull() ) |
---|
612 | txt += QString((char*)(param->GetParameterGuidance()).data())+ "\n" ; |
---|
613 | txt += " Parameter type : " + QString(param->GetParameterType())+ "\n"; |
---|
614 | if(param->IsOmittable()){ |
---|
615 | txt += " Omittable : True\n"; |
---|
616 | } else { |
---|
617 | txt += " Omittable : False\n"; |
---|
618 | } |
---|
619 | if( param->GetCurrentAsDefault() ) { |
---|
620 | txt += " Default value : taken from the current value\n"; |
---|
621 | } else if( ! param->GetDefaultValue().isNull() ) { |
---|
622 | txt += " Default value : " + QString((char*)(param->GetDefaultValue()).data())+ "\n"; |
---|
623 | } |
---|
624 | if( ! param->GetParameterRange().isNull() ) { |
---|
625 | txt += " Parameter range : " + QString((char*)(param->GetParameterRange()).data())+ "\n"; |
---|
626 | } |
---|
627 | if( ! param->GetParameterCandidates().isNull() ) { |
---|
628 | txt += " Candidates : " + QString((char*)(param->GetParameterCandidates()).data())+ "\n"; |
---|
629 | } |
---|
630 | } |
---|
631 | } |
---|
632 | return txt; |
---|
633 | } |
---|
634 | |
---|
635 | |
---|
636 | |
---|
637 | /** Implement G4VBasicShell vurtual function |
---|
638 | */ |
---|
639 | G4bool G4UIQt::GetHelpChoice( |
---|
640 | G4int& aInt |
---|
641 | ) |
---|
642 | { |
---|
643 | printf("G4UIQt::GetHelpChoice SHOULD NEVER GO HERE"); |
---|
644 | return true; |
---|
645 | } |
---|
646 | |
---|
647 | |
---|
648 | /** Implement G4VBasicShell vurtual function |
---|
649 | */ |
---|
650 | void G4UIQt::ExitHelp( |
---|
651 | ) |
---|
652 | { |
---|
653 | printf("G4UIQt::ExitHelp SHOULD NEVER GO HERE"); |
---|
654 | } |
---|
655 | |
---|
656 | |
---|
657 | /** Event filter method. Every event from QtApplication goes here.<br/> |
---|
658 | We apply a filter only for the Up and Down Arrow press when the QLineEdit<br/> |
---|
659 | is active. If this filter match, Up arrow we give the previous command<br/> |
---|
660 | and Down arrow will give the next if exist.<br/> |
---|
661 | @param obj Emitter of the event |
---|
662 | @param event Kind of event |
---|
663 | */ |
---|
664 | bool G4UIQt::eventFilter( // Should stay with a minuscule eventFilter because of Qt |
---|
665 | QObject *aObj |
---|
666 | ,QEvent *aEvent |
---|
667 | ) |
---|
668 | { |
---|
669 | if (aObj == NULL) return false; |
---|
670 | if (aEvent == NULL) return false; |
---|
671 | |
---|
672 | if (aObj == fCommandHistoryArea) { |
---|
673 | if (aEvent->type() == QEvent::KeyPress) { |
---|
674 | fCommandArea->setFocus(); |
---|
675 | } |
---|
676 | } |
---|
677 | if (aObj == fCommandArea) { |
---|
678 | if (aEvent->type() == QEvent::KeyPress) { |
---|
679 | QKeyEvent *e = static_cast<QKeyEvent*>(aEvent); |
---|
680 | if ((e->key() == (Qt::Key_Down)) || |
---|
681 | (e->key() == (Qt::Key_PageDown)) || |
---|
682 | (e->key() == (Qt::Key_Up)) || |
---|
683 | (e->key() == (Qt::Key_PageUp))) { |
---|
684 | int selection = fCommandHistoryArea->currentRow(); |
---|
685 | if (fCommandHistoryArea->count()) { |
---|
686 | if (selection == -1) { |
---|
687 | selection = fCommandHistoryArea->count()-1; |
---|
688 | } else { |
---|
689 | if (e->key() == (Qt::Key_Down)) { |
---|
690 | if (selection <(fCommandHistoryArea->count()-1)) |
---|
691 | selection++; |
---|
692 | } else if (e->key() == (Qt::Key_PageDown)) { |
---|
693 | selection = fCommandHistoryArea->count()-1; |
---|
694 | } else if (e->key() == (Qt::Key_Up)) { |
---|
695 | if (selection >0) |
---|
696 | selection --; |
---|
697 | } else if (e->key() == (Qt::Key_PageUp)) { |
---|
698 | selection = 0; |
---|
699 | } |
---|
700 | } |
---|
701 | fCommandHistoryArea->clearSelection(); |
---|
702 | fCommandHistoryArea->item(selection)->setSelected(true); |
---|
703 | fCommandHistoryArea->setCurrentItem(fCommandHistoryArea->item(selection)); |
---|
704 | } |
---|
705 | } else if (e->key() == (Qt::Key_Tab)) { |
---|
706 | G4String ss = Complete(fCommandArea->text().toStdString().c_str()); |
---|
707 | fCommandArea->setText((char*)(ss.data())); |
---|
708 | |
---|
709 | // do not pass by parent, it will disable widget tab focus ! |
---|
710 | return true; |
---|
711 | } |
---|
712 | } |
---|
713 | } |
---|
714 | // pass the event on to the parent class |
---|
715 | return QObject::eventFilter(aObj, aEvent); |
---|
716 | } |
---|
717 | |
---|
718 | |
---|
719 | |
---|
720 | |
---|
721 | /***************************************************************************/ |
---|
722 | // |
---|
723 | // SLOTS DEFINITIONS |
---|
724 | // |
---|
725 | /***************************************************************************/ |
---|
726 | |
---|
727 | /** Called when user give "help" command. |
---|
728 | */ |
---|
729 | void G4UIQt::ShowHelpCallback ( |
---|
730 | ) |
---|
731 | { |
---|
732 | TerminalHelp(""); |
---|
733 | } |
---|
734 | |
---|
735 | |
---|
736 | /** Called when user click on clear button. Clear the text Output area |
---|
737 | */ |
---|
738 | void G4UIQt::ClearButtonCallback ( |
---|
739 | ) |
---|
740 | { |
---|
741 | fTextArea->clear(); |
---|
742 | } |
---|
743 | |
---|
744 | |
---|
745 | /** Callback call when "click on a menu entry.<br> |
---|
746 | Send the associated command to geant4 |
---|
747 | */ |
---|
748 | void G4UIQt::CommandEnteredCallback ( |
---|
749 | ) |
---|
750 | { |
---|
751 | G4String command (fCommandArea->text().toStdString().c_str()); |
---|
752 | if (fCommandArea->text().trimmed() != "") { |
---|
753 | fCommandHistoryArea->addItem(fCommandArea->text()); |
---|
754 | fCommandHistoryArea->clearSelection(); |
---|
755 | fCommandHistoryArea->item(fCommandHistoryArea->count()-1)->setSelected(true); |
---|
756 | fCommandHistoryArea->setCurrentItem(fCommandHistoryArea->item(fCommandHistoryArea->count()-1)); |
---|
757 | |
---|
758 | if (command(0,4) != "help") { |
---|
759 | ApplyShellCommand (command,exitSession,exitPause); |
---|
760 | } else { |
---|
761 | TerminalHelp(command); |
---|
762 | } |
---|
763 | if(exitSession==true) |
---|
764 | SessionTerminate(); |
---|
765 | } |
---|
766 | fCommandArea->setText(""); |
---|
767 | } |
---|
768 | |
---|
769 | |
---|
770 | /** Callback call when "enter" clicked on the command zone.<br> |
---|
771 | Send the command to geant4 |
---|
772 | @param aCommand |
---|
773 | */ |
---|
774 | void G4UIQt::ButtonCallback ( |
---|
775 | const QString& aCommand |
---|
776 | ) |
---|
777 | { |
---|
778 | G4String ss = G4String(aCommand.toStdString().c_str()); |
---|
779 | ApplyShellCommand(ss,exitSession,exitPause); |
---|
780 | if(exitSession==true) |
---|
781 | SessionTerminate(); |
---|
782 | } |
---|
783 | |
---|
784 | |
---|
785 | |
---|
786 | /** This callback is activated when user selected a item in the help tree |
---|
787 | */ |
---|
788 | void G4UIQt::HelpTreeClicCallback ( |
---|
789 | ) |
---|
790 | { |
---|
791 | QTreeWidgetItem* item = NULL; |
---|
792 | if (!fHelpTreeWidget) |
---|
793 | return ; |
---|
794 | |
---|
795 | if (!fHelpArea) |
---|
796 | return; |
---|
797 | |
---|
798 | QList<QTreeWidgetItem *> list =fHelpTreeWidget->selectedItems(); |
---|
799 | if (list.isEmpty()) |
---|
800 | return; |
---|
801 | item = list.first(); |
---|
802 | if (!item) |
---|
803 | return; |
---|
804 | |
---|
805 | G4UImanager* UI = G4UImanager::GetUIpointer(); |
---|
806 | if(UI==NULL) return; |
---|
807 | G4UIcommandTree * treeTop = UI->GetTree(); |
---|
808 | G4UIcommand* command = treeTop->FindPath(item->text (1).toStdString().c_str()); |
---|
809 | if (command) { |
---|
810 | fHelpArea->setText(GetCommandList(command)); |
---|
811 | } else { |
---|
812 | // this is not a command, this is a sub directory |
---|
813 | // We display the Title |
---|
814 | fHelpArea->setText(item->text (1).toStdString().c_str()); |
---|
815 | } |
---|
816 | } |
---|
817 | |
---|
818 | /** This callback is activated when user double clic on a item in the help tree |
---|
819 | */ |
---|
820 | void G4UIQt::HelpTreeDoubleClicCallback ( |
---|
821 | ) |
---|
822 | { |
---|
823 | // update the helpArea |
---|
824 | HelpTreeClicCallback(); |
---|
825 | |
---|
826 | QTreeWidgetItem* item = NULL; |
---|
827 | if (!fHelpTreeWidget) |
---|
828 | return ; |
---|
829 | |
---|
830 | if (!fHelpArea) |
---|
831 | return; |
---|
832 | |
---|
833 | QList<QTreeWidgetItem *> list =fHelpTreeWidget->selectedItems(); |
---|
834 | if (list.isEmpty()) |
---|
835 | return; |
---|
836 | item = list.first(); |
---|
837 | if (!item) |
---|
838 | return; |
---|
839 | |
---|
840 | fCommandArea->setText(item->text (1)); |
---|
841 | } |
---|
842 | |
---|
843 | |
---|
844 | /** Callback called when user select an old command in the command history<br> |
---|
845 | Give it to the command area. |
---|
846 | */ |
---|
847 | void G4UIQt::CommandHistoryCallback( |
---|
848 | ) |
---|
849 | { |
---|
850 | QListWidgetItem* item = NULL; |
---|
851 | if (!fCommandHistoryArea) |
---|
852 | return ; |
---|
853 | |
---|
854 | |
---|
855 | QList<QListWidgetItem *> list =fCommandHistoryArea->selectedItems(); |
---|
856 | if (list.isEmpty()) |
---|
857 | return; |
---|
858 | item = list.first(); |
---|
859 | if (!item) |
---|
860 | return; |
---|
861 | fCommandArea->setText(item->text()); |
---|
862 | |
---|
863 | } |
---|
864 | |
---|
865 | #endif |
---|