source: trunk/geant4/interfaces/basic/src/G4UIQt.cc@ 631

Last change on this file since 631 was 630, checked in by garnier, 18 years ago

maj a jour par rapport au repository de geant4

  • Property svn:mime-type set to text/cpp
File size: 36.8 KB
Line 
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.12 2007/11/15 18:24:27 lgarnier Exp $
28// GEANT4 tag $Name: $
29//
30// L. Garnier
31
32#define GEANT4_QT_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 <qapplication.h>
49#include <qlineedit.h>
50#include <qwidget.h>
51#include <qmenubar.h>
52#include <qlayout.h>
53#include <qpushbutton.h>
54#include <qlabel.h>
55#include <qsplitter.h>
56#include <qscrollbar.h>
57#include <qdialog.h>
58#include <qevent.h>
59#include <qtextedit.h>
60#include <qsignalmapper.h>
61
62#include <qmainwindow.h>
63#if QT_VERSION >= 0x040000
64#include <qmenu.h>
65#include <qlistwidget.h>
66#include <qtreewidget.h>
67#else
68#include <qaction.h>
69#include <qheader.h>
70#include <qlistview.h>
71#include <qpopupmenu.h>
72#endif
73
74
75
76#include <stdlib.h>
77
78// Pourquoi Static et non variables de classe ?
79static G4bool exitSession = true;
80static G4bool exitPause = true;
81
82/** Build a Qt window with a menubar, output area and promt area<br>
83<pre>
84 +-----------------------+
85 |exit menu| |
86 | |
87 | +-------------------+ |
88 | | | |
89 | | Output area | |
90 | | | |
91 | +-------------------+ |
92 | | clear | |
93 | +-------------------+ |
94 | | promt history | |
95 | +-------------------+ |
96 | +-------------------+ |
97 | |> promt area | |
98 | +-------------------+ |
99 +-----------------------+
100</pre>
101*/
102G4UIQt::G4UIQt (
103 int argc
104,char** argv
105)
106 :fHelpDialog(NULL)
107{
108#ifdef GEANT4_QT_DEBUG
109 printf("G4UIQt::Initialise %d %s\n",argc,argv[0]);
110#endif
111 G4Qt* interactorManager = G4Qt::getInstance (argc,argv,(char*)"Qt");
112 G4UImanager* UI = G4UImanager::GetUIpointer();
113 if(UI!=NULL) UI->SetSession(this);
114
115 fMainWindow = new QMainWindow();
116
117#ifdef GEANT4_QT_DEBUG
118 printf("G4UIQt::Initialise after main window creation\n");
119#endif
120#if QT_VERSION < 0x040000
121 fMainWindow->setCaption( tr( "G4UI Session" ));
122 fMainWindow->resize(800,600);
123 fMainWindow->move(50,100);
124#else
125 fMainWindow->setWindowTitle( tr("G4UI Session") );
126 fMainWindow->resize(800,600);
127 fMainWindow->move(QPoint(50,100));
128#endif
129
130 QSplitter *splitter = new QSplitter(Qt::Vertical,fMainWindow);
131
132 // Set layouts
133
134#if QT_VERSION < 0x040000
135
136 QWidget* topWidget = new QWidget(splitter);
137 QWidget* bottomWidget = new QWidget(splitter);
138
139 QVBoxLayout *layoutTop = new QVBoxLayout(topWidget);
140 QVBoxLayout *layoutBottom = new QVBoxLayout(bottomWidget);
141#else
142 QWidget* topWidget = new QWidget();
143 QWidget* bottomWidget = new QWidget();
144
145 QVBoxLayout *layoutTop = new QVBoxLayout;
146 QVBoxLayout *layoutBottom = new QVBoxLayout;
147#endif
148
149 // fill them
150
151 fTextArea = new QTextEdit(topWidget);
152#ifdef GEANT4_QT_DEBUG
153 printf("G4UIQt:: end\n");
154#endif
155#ifdef GEANT4_QT_DEBUG
156 printf("G4UIQt::PushButton 1\n");
157#endif
158 QPushButton *clearButton = new QPushButton("clear",topWidget);
159#ifdef GEANT4_QT_DEBUG
160 printf("G4UIQt::end1\n");
161#endif
162 connect(clearButton, SIGNAL(clicked()), SLOT(ClearButtonCallback()));
163
164#if QT_VERSION < 0x040000
165 fCommandHistoryArea = new QListView(bottomWidget);
166
167 fCommandHistoryArea->setSorting (-1, FALSE);
168 fCommandHistoryArea->setSelectionMode(QListView::Single);
169 fCommandHistoryArea->addColumn("");
170 fCommandHistoryArea->header()->hide();
171 connect(fCommandHistoryArea, SIGNAL(selectionChanged()), SLOT(CommandHistoryCallback()));
172#else
173 fCommandHistoryArea = new QListWidget();
174 fCommandHistoryArea->setSelectionMode(QAbstractItemView::SingleSelection);
175 connect(fCommandHistoryArea, SIGNAL(itemSelectionChanged()), SLOT(CommandHistoryCallback()));
176#endif
177
178 fCommandHistoryArea->installEventFilter(this);
179 fCommandLabel = new QLabel("",bottomWidget);
180
181 fCommandArea = new QLineEdit(bottomWidget);
182 fCommandArea->installEventFilter(this);
183#ifdef GEANT4_QT_DEBUG
184 printf("G4UIQt:: 1\n");
185#endif
186#if QT_VERSION < 0x040000
187 fCommandArea->setActiveWindow();
188#else
189 fCommandArea->activateWindow();
190#endif
191 connect(fCommandArea, SIGNAL(returnPressed()), SLOT(CommandEnteredCallback()));
192#ifdef GEANT4_QT_DEBUG
193 printf("G4UIQt:: 1 connect returnPressed\n");
194#endif
195#if QT_VERSION < 0x040000
196 fCommandArea->setFocusPolicy ( QWidget::StrongFocus );
197 fCommandArea->setFocus();
198#else
199 fCommandArea->setFocusPolicy ( Qt::StrongFocus );
200 fCommandArea->setFocus(Qt::TabFocusReason);
201#endif
202 fTextArea->setReadOnly(true);
203
204
205#ifdef GEANT4_QT_DEBUG
206 printf("G4UIQt:: 2\n");
207#endif
208
209#ifdef GEANT4_QT_DEBUG
210 printf("G4UIQt:: 3\n");
211#endif
212
213
214 layoutTop->addWidget(fTextArea);
215 layoutTop->addWidget(clearButton);
216
217#if QT_VERSION >= 0x040000
218 topWidget->setLayout(layoutTop);
219#endif
220
221#ifdef GEANT4_QT_DEBUG
222 printf("G4UIQt:: 4\n");
223#endif
224 layoutBottom->addWidget(fCommandHistoryArea);
225 layoutBottom->addWidget(fCommandLabel);
226 layoutBottom->addWidget(fCommandArea);
227#if QT_VERSION >= 0x040000
228
229 bottomWidget->setLayout(layoutBottom);
230 splitter->addWidget(topWidget);
231 splitter->addWidget(bottomWidget);
232#endif
233
234
235#ifdef GEANT4_QT_DEBUG
236 printf("G4UIQt:: 5\n");
237#endif
238 fMainWindow->setCentralWidget(splitter);
239
240#if QT_VERSION < 0x040000
241
242 // Add a quit subMenu
243 QPopupMenu *fileMenu = new QPopupMenu( fMainWindow);
244 fileMenu->insertItem( "&Quitter", this, SLOT(ExitSession()), CTRL+Key_Q );
245 fMainWindow->menuBar()->insertItem( QString("&File"), fileMenu );
246
247 // Add a Help menu
248 QPopupMenu *helpMenu = new QPopupMenu( fMainWindow );
249 helpMenu->insertItem( "&Show Help", this, SLOT(ShowHelpCallback()), CTRL+Key_H );
250 fMainWindow->menuBar()->insertItem( QString("&Help"), helpMenu );
251
252#ifdef GEANT4_QT_DEBUG
253 printf("G4UIQt:: 6\n");
254#endif
255
256#else
257
258 // Add a quit subMenu
259 QMenu *fileMenu = fMainWindow->menuBar()->addMenu("File");
260 fileMenu->addAction("Quitter", this, SLOT(ExitSession()));
261
262 // Add a Help menu
263 QMenu *helpMenu = fMainWindow->menuBar()->addMenu("Help");
264 helpMenu->addAction("Show Help", this, SLOT(ShowHelpCallback()));
265#endif
266#ifdef GEANT4_QT_DEBUG
267 printf("G4UIQt:: 7\n");
268#endif
269
270 // Set the splitter size. The fTextArea sould be 2/3 on the fMainWindow
271#if QT_VERSION < 0x040000
272 QValueList<int> vals = splitter->sizes();
273#else
274 QList<int> vals = splitter->sizes();
275#endif
276 if(vals.size()==2) {
277 vals[0] = (splitter->orientation()==Qt::Vertical ? splitter->height() : splitter->width())*3/4;
278 vals[1] = (splitter->orientation()==Qt::Vertical ? splitter->height() : splitter->width())*1/4;
279 splitter->setSizes(vals);
280 }
281#ifdef GEANT4_QT_DEBUG
282 printf("G4UIQt:: 8\n");
283#endif
284
285 if(UI!=NULL) UI->SetCoutDestination(this); // TO KEEP
286}
287
288
289
290G4UIQt::~G4UIQt(
291)
292{
293 G4UImanager* UI = G4UImanager::GetUIpointer(); // TO KEEP
294 if(UI!=NULL) { // TO KEEP
295 UI->SetSession(NULL); // TO KEEP
296 UI->SetCoutDestination(NULL); // TO KEEP
297 }
298
299 if (fMainWindow!=NULL)
300 delete fMainWindow;
301}
302
303
304
305/** Start the Qt main loop
306*/
307G4UIsession* G4UIQt::SessionStart (
308)
309{
310
311 G4Qt* interactorManager = G4Qt::getInstance ();
312
313#if QT_VERSION >= 0x040000
314#if QT_VERSION >= 0x040200
315 fMainWindow->setVisible(true);
316#else
317 fMainWindow->show();
318#endif
319#else
320 fMainWindow->show();
321#endif
322 Prompt("session");
323 exitSession = false;
324
325
326#ifdef GEANT4_QT_DEBUG
327 printf("disable secondary loop\n");
328#endif
329 interactorManager->DisableSecondaryLoop (); // TO KEEP
330 if ((QApplication*)interactorManager->GetMainInteractor())
331 ((QApplication*)interactorManager->GetMainInteractor())->exec();
332
333 // on ne passe pas le dessous ? FIXME ????
334 // je ne pense pas 13/06
335
336 // void* event; // TO KEEP
337 // while((event = interactorManager->GetEvent())!=NULL) { // TO KEEP
338 // interactorManager->DispatchEvent(event); // TO KEEP
339 // if(exitSession==true) break; // TO KEEP
340 // } // TO KEEP
341
342 interactorManager->EnableSecondaryLoop ();
343#ifdef GEANT4_QT_DEBUG
344 printf("enable secondary loop\n");
345#endif
346 return this;
347}
348
349
350/** Display the prompt in the prompt area
351 @param aPrompt : string to display as the promt label
352 //FIXME : probablement inutile puisque le seul a afficher qq chose d'autre
353 que "session" est SecondaryLoop()
354*/
355void G4UIQt::Prompt (
356 G4String aPrompt
357)
358{
359 if (!aPrompt) return;
360
361 fCommandLabel->setText((char*)aPrompt.data());
362}
363
364
365void G4UIQt::SessionTerminate (
366)
367{
368 G4Qt* interactorManager = G4Qt::getInstance ();
369 fMainWindow->close();
370 ((QApplication*)interactorManager->GetMainInteractor())->exit();
371}
372
373
374
375/**
376 Called by intercoms/src/G4UImanager.cc<br>
377 Called by visualization/management/src/G4VisCommands.cc with "EndOfEvent" argument<br>
378 It have to pause the session command terminal.<br>
379 Call SecondaryLoop to wait for exit event<br>
380 @param aState
381 @see : G4VisCommandReviewKeptEvents::SetNewValue
382*/
383void G4UIQt::PauseSessionStart (
384 G4String aState
385)
386{
387 if (!aState) return;
388
389#ifdef GEANT4_QT_DEBUG
390 printf("G4UIQt::PauseSessionStart\n");
391#endif
392 if(aState=="G4_pause> ") { // TO KEEP
393 SecondaryLoop ("Pause, type continue to exit this state"); // TO KEEP
394 } // TO KEEP
395
396 if(aState=="EndOfEvent") { // TO KEEP
397 // Picking with feed back in event data Done here !!!
398 SecondaryLoop ("End of event, type continue to exit this state"); // TO KEEP
399 } // TO KEEP
400}
401
402
403
404/**
405 Begin the secondary loop
406 @param a_prompt : label to display as the prompt label
407 */
408void G4UIQt::SecondaryLoop (
409 G4String aPrompt
410)
411{
412 if (!aPrompt) return;
413
414#ifdef GEANT4_QT_DEBUG
415 printf("G4UIQt::SecondaryLoop\n");
416#endif
417 G4Qt* interactorManager = G4Qt::getInstance (); // TO KEEP ?
418 Prompt(aPrompt); // TO KEEP
419 exitPause = false; // TO KEEP
420 void* event; // TO KEEP
421 while((event = interactorManager->GetEvent())!=NULL) { // TO KEEP
422 interactorManager->DispatchEvent(event); // TO KEEP
423 if(exitPause==true) break; // TO KEEP
424 } // TO KEEP
425 Prompt("session"); // TO KEEP
426}
427
428
429
430/**
431 Receive a cout from Geant4. We have to display it in the cout zone
432 @param aString : label to add in the display area
433 @return 0
434*/
435G4int G4UIQt::ReceiveG4cout (
436 G4String aString
437)
438{
439 if (!aString) return 0;
440 G4Qt* interactorManager = G4Qt::getInstance ();
441 if (!interactorManager) return 0;
442
443#if QT_VERSION < 0x040000
444 fTextArea->append(QString((char*)aString.data()).simplifyWhiteSpace());
445 fTextArea->verticalScrollBar()->setValue(fTextArea->verticalScrollBar()->maxValue());
446#else
447 fTextArea->append(QString((char*)aString.data()).trimmed());
448 fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum());
449#endif
450 // Slow down everything : disababled
451 interactorManager->FlushAndWaitExecution();
452 return 0;
453}
454
455
456/**
457 Receive a cerr from Geant4. We have to display it in the cout zone
458 @param aString : label to add in the display area
459 @return 0
460*/
461G4int G4UIQt::ReceiveG4cerr (
462 G4String aString
463)
464{
465 if (!aString) return 0;
466 G4Qt* interactorManager = G4Qt::getInstance ();
467 if (!interactorManager) return 0;
468
469#if QT_VERSION < 0x040000
470 QColor previousColor = fTextArea->color();
471 fTextArea->setColor(Qt::red);
472 fTextArea->append(QString((char*)aString.data()).simplifyWhiteSpace());
473 fTextArea->setColor(previousColor);
474 fTextArea->verticalScrollBar()->setValue(fTextArea->verticalScrollBar()->maxValue());
475#else
476 QColor previousColor = fTextArea->textColor();
477 fTextArea->setTextColor(Qt::red);
478 fTextArea->append(QString((char*)aString.data()).trimmed());
479 fTextArea->setTextColor(previousColor);
480 fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum());
481#endif
482 interactorManager->FlushAndWaitExecution();
483 return 0;
484}
485
486
487
488/**
489 Add a new menu to the menu bar
490 @param aName name of menu
491 @param aLabel label to display
492 */
493void G4UIQt::AddMenu (
494 const char* aName
495,const char* aLabel
496)
497{
498 if (aName == NULL) return;
499 if (aLabel == NULL) return;
500
501#if QT_VERSION < 0x040000
502 QPopupMenu *fileMenu = new QPopupMenu( fMainWindow);
503 fMainWindow->menuBar()->insertItem( aLabel, fileMenu );
504#else
505 QMenu *fileMenu = new QMenu(aLabel);
506 fMainWindow->menuBar()->insertMenu(fMainWindow->menuBar()->actions().last(),fileMenu);
507#endif
508
509 AddInteractor (aName,(G4Interactor)fileMenu);
510}
511
512
513/**
514 Add a new button to a menu
515 @param aMenu : parent menu
516 @param aLabel : label to display
517 @param aCommand : command to execute as a callback
518 */
519void G4UIQt::AddButton (
520 const char* aMenu
521,const char* aLabel
522,const char* aCommand
523)
524{
525 if(aMenu==NULL) return; // TO KEEP
526 if(aLabel==NULL) return; // TO KEEP
527 if(aCommand==NULL) return; // TO KEEP
528
529#if QT_VERSION < 0x040000
530 QPopupMenu *parent = (QPopupMenu*)GetInteractor(aMenu);
531#else
532 QMenu *parent = (QMenu*)GetInteractor(aMenu);
533#endif
534
535 if(parent==NULL) return;
536
537 QSignalMapper *signalMapper = new QSignalMapper(this);
538#if QT_VERSION < 0x030200
539 QAction *action = new QAction(QString(aLabel),QString(aLabel),QKeySequence::QKeySequence (),signalMapper, SLOT(map()));
540 action->addTo(parent);
541 connect(action,SIGNAL(activated()),signalMapper,SLOT(map()));
542
543#elif QT_VERSION < 0x040000
544 QAction *action = new QAction(QString(aLabel),QKeySequence::QKeySequence (),signalMapper, SLOT(map()));
545 action->addTo(parent);
546 connect(action,SIGNAL(activated()),signalMapper,SLOT(map()));
547
548#else
549 QAction *action = parent->addAction(aLabel, signalMapper, SLOT(map()));
550
551#endif
552 connect(signalMapper, SIGNAL(mapped(const QString &)),this, SLOT(ButtonCallback(const QString&)));
553 signalMapper->setMapping(action, QString(aCommand));
554}
555
556
557
558
559/**
560 Open the help dialog in a separate window.<br>
561 This will be display as a tree widget.<br>
562 Implementation of <b>void G4VBasicShell::TerminalHelp(G4String newCommand)</b>
563
564 @param newCommand : open the tree widget item on this command if is set
565*/
566void G4UIQt::TerminalHelp(
567 G4String newCommand
568)
569{
570 // Create the help dialog
571 if (!fHelpDialog) {
572#if QT_VERSION < 0x040000
573 fHelpDialog = new QDialog(0,0,FALSE,Qt::WStyle_Title | Qt::WStyle_SysMenu | Qt::WStyle_MinMax );
574 QVBoxLayout *vLayout = new QVBoxLayout(fHelpDialog);
575 QSplitter *splitter = new QSplitter(Qt::Horizontal,fHelpDialog);
576#else
577 QVBoxLayout *vLayout = new QVBoxLayout();
578 fHelpDialog = new QDialog(0,Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
579 QSplitter *splitter = new QSplitter(Qt::Horizontal);
580#endif
581 QPushButton *exitButton = new QPushButton("Exit",fHelpDialog);
582 connect(exitButton, SIGNAL(clicked()), fHelpDialog,SLOT(close()));
583
584 // the help tree
585 G4UImanager* UI = G4UImanager::GetUIpointer();
586 if(UI==NULL) return;
587 G4UIcommandTree * treeTop = UI->GetTree();
588
589 // build widget
590#if QT_VERSION < 0x040000
591 fHelpTreeWidget = new QListView(splitter);
592 fHelpTreeWidget->setSelectionMode(QListView::Single);
593 fHelpTreeWidget->setRootIsDecorated(true);
594 fHelpTreeWidget->addColumn("Command");
595 fHelpTreeWidget->addColumn("Description",0);
596 // fHelpTreeWidget->setColumnWidth (1,0);
597 fHelpTreeWidget->header()->setResizeEnabled(FALSE,1);
598 // QList<QListViewItem *> items;
599#else
600 fHelpTreeWidget = new QTreeWidget();
601 fHelpTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
602 fHelpTreeWidget->setColumnCount(2);
603 fHelpTreeWidget->setColumnHidden(1,true);
604 QStringList labels;
605 labels << QString("Command") << QString("Description");
606 fHelpTreeWidget->setHeaderLabels(labels);
607 // QList<QTreeWidgetItem *> items;
608#endif
609
610#if QT_VERSION < 0x040000
611#ifdef GEANT4_QT_DEBUG
612 printf("G4UIQt::QTextEdit 1\n");
613#endif
614 fHelpArea = new QTextEdit(splitter);
615#ifdef GEANT4_QT_DEBUG
616 printf("G4UIQt::end 1\n");
617#endif
618#else
619#ifdef GEANT4_QT_DEBUG
620 printf("G4UIQt::QTextEdit 2\n");
621#endif
622 fHelpArea = new QTextEdit();
623#ifdef GEANT4_QT_DEBUG
624 printf("G4UIQt::end 2\n");
625#endif
626#endif
627 fHelpArea->setReadOnly(true);
628
629 G4int treeSize = treeTop->GetTreeEntry();
630#if QT_VERSION < 0x040000
631 QListViewItem * newItem;
632#else
633 QTreeWidgetItem * newItem;
634#endif
635 for (int a=0;a<treeSize;a++) {
636 // Creating new item
637
638#if QT_VERSION < 0x040000
639 newItem = new QListViewItem(fHelpTreeWidget);
640 newItem->setText(0,QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).simplifyWhiteSpace());
641 newItem->setText(1,QString((char*)(treeTop->GetTree(a+1)->GetTitle()).data()).simplifyWhiteSpace());
642#else
643 //FIXME : Qt 4.2
644 // QStringList stringList;
645 // stringList << QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).trimmed() ;
646 // stringList << QString((char*)(treeTop->GetTree(a+1)->GetTitle()).data()).trimmed() ;
647 // newItem = new QTreeWidgetItem(stringList);
648 // FIXME : Qt 4.0
649 newItem = new QTreeWidgetItem(fHelpTreeWidget);
650 newItem->setText(0,QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).trimmed());
651 newItem->setText(1,QString((char*)(treeTop->GetTree(a+1)->GetTitle()).data()).trimmed());
652#endif
653
654
655 // look for childs
656 CreateChildTree(newItem,treeTop->GetTree(a+1));
657 // items.append(newItem);
658 }
659
660#if QT_VERSION < 0x040000
661 connect(fHelpTreeWidget, SIGNAL(selectionChanged ()),this, SLOT(HelpTreeClicCallback()));
662 connect(fHelpTreeWidget, SIGNAL(doubleClicked (QListViewItem*)),this, SLOT(HelpTreeDoubleClicCallback(QListViewItem*)));
663#else
664 connect(fHelpTreeWidget, SIGNAL(itemSelectionChanged ()),this, SLOT(HelpTreeClicCallback()));
665 connect(fHelpTreeWidget, SIGNAL(itemDoubleClicked (QTreeWidgetItem*,int)),this, SLOT(HelpTreeDoubleClicCallback(QTreeWidgetItem*)));
666#endif
667
668 // Set layouts
669
670#if QT_VERSION >= 0x040000
671 splitter->addWidget(fHelpTreeWidget);
672 splitter->addWidget(fHelpArea);
673#endif
674
675
676#if QT_VERSION >= 0x040000
677 vLayout->addWidget(splitter);
678 vLayout->addWidget(exitButton);
679#else
680 vLayout->add(splitter);
681 vLayout->addWidget(exitButton);
682#endif
683
684 // set the splitter size
685#if QT_VERSION >= 0x040000
686 QList<int> list;
687#else
688 QValueList<int> list;
689#endif
690 list.append( 400 );
691 list.append( 400 );
692 splitter->setSizes(list);
693
694#if QT_VERSION >= 0x040000
695 fHelpDialog->setLayout(vLayout);
696#endif
697
698 }
699
700 // Look for the choosen command "newCommand"
701 size_t i = newCommand.index(" ");
702 G4String targetCom="";
703 if( i != std::string::npos )
704 {
705 G4String newValue = newCommand(i+1,newCommand.length()-(i+1));
706 newValue.strip(G4String::both);
707 targetCom = ModifyToFullPathCommand( newValue );
708 }
709 if (targetCom != "") {
710#if QT_VERSION < 0x040000
711 QListViewItem* findItem = NULL;
712 QListViewItem* tmpItem = fHelpTreeWidget->firstChild();
713 while (tmpItem != 0) {
714 if (!findItem) {
715 findItem = FindTreeItem(tmpItem,QString((char*)targetCom.data()));
716 }
717 tmpItem = tmpItem->nextSibling();
718 }
719#else
720 QTreeWidgetItem* findItem = NULL;
721 for (int a=0;a<fHelpTreeWidget->topLevelItemCount();a++) {
722 if (!findItem) {
723 findItem = FindTreeItem(fHelpTreeWidget->topLevelItem(a),QString((char*)targetCom.data()));
724 }
725 }
726#endif
727
728 if (findItem) {
729
730 //collapsed open item
731#if QT_VERSION < 0x040000
732
733 // FIXME : Has to be checked
734 QListViewItem* tmpItem = fHelpTreeWidget->firstChild();
735 QList<QListViewItem> openItems;
736 while ((tmpItem != 0) || (!openItems.isEmpty())) {
737 if (tmpItem->isOpen() ) {
738 tmpItem->setOpen(false);
739 openItems.append(tmpItem);
740 tmpItem = tmpItem->firstChild();
741 } else {
742 tmpItem = tmpItem->nextSibling();
743 }
744 if (tmpItem == 0) {
745 tmpItem = openItems.take(openItems.count()-1);
746 }
747 }
748#else
749 QList<QTreeWidgetItem *> selected;
750
751 selected = fHelpTreeWidget->selectedItems();
752 if ( selected.count() != 0 ) {
753 QTreeWidgetItem * tmp =selected.at( 0 );
754 while ( tmp) {
755#if QT_VERSION < 0x040202
756 fHelpTreeWidget->setItemExpanded(tmp,false);
757#else
758 tmp->setExpanded(false);
759#endif
760 tmp = tmp->parent();
761 }
762 }
763#endif
764
765 // clear old selection
766 fHelpTreeWidget->clearSelection();
767
768 // set new selection
769#if QT_VERSION >= 0x040000
770#if QT_VERSION < 0x040202
771 fHelpTreeWidget->setItemSelected(findItem,true);
772#else
773 findItem->setSelected(true);
774#endif
775#else
776 findItem->setSelected(true);
777#endif
778
779 // expand parent item
780 while ( findItem) {
781#if QT_VERSION < 0x040000
782 findItem->setOpen(true);
783#else
784#if QT_VERSION < 0x040202
785 fHelpTreeWidget->setItemExpanded(findItem,true);
786#else
787 findItem->setExpanded(true);
788#endif
789#endif
790 findItem = findItem->parent();
791 }
792
793 // Call the update of the right textArea
794 HelpTreeClicCallback();
795 }
796 }
797#if QT_VERSION < 0x040000
798 fHelpDialog->setCaption( tr( "Help on commands" ));
799#else
800 fHelpDialog->setWindowTitle(tr("Help on commands"));
801#endif
802 fHelpDialog->resize(800,600);
803 fHelpDialog->move(QPoint(400,150));
804 fHelpDialog->show();
805 fHelpDialog->raise();
806#if QT_VERSION < 0x040000
807 fHelpDialog->setActiveWindow();
808#else
809 fHelpDialog->activateWindow();
810#endif
811}
812
813
814
815/** Fill the Help Tree Widget
816 @param aParent : parent item to fill
817 @param aCommandTree : commandTree node associate with this part of the Tree
818*/
819#if QT_VERSION < 0x040000
820void G4UIQt::CreateChildTree(
821 QListViewItem *aParent
822,G4UIcommandTree *aCommandTree
823#else
824void G4UIQt::CreateChildTree(
825 QTreeWidgetItem *aParent
826,G4UIcommandTree *aCommandTree
827#endif
828)
829{
830 if (aParent == NULL) return;
831 if (aCommandTree == NULL) return;
832
833
834 // Creating new item
835#if QT_VERSION < 0x040000
836 QListViewItem * newItem;
837#else
838 QTreeWidgetItem * newItem;
839#endif
840
841 // Get the Sub directories
842 for (int a=0;a<aCommandTree->GetTreeEntry();a++) {
843
844#if QT_VERSION < 0x040000
845 newItem = new QListViewItem(aParent);
846 newItem->setText(0,QString((char*)(aCommandTree->GetTree(a+1)->GetPathName()).data()).simplifyWhiteSpace());
847 newItem->setText(1,QString((char*)(aCommandTree->GetTree(a+1)->GetTitle()).data()).simplifyWhiteSpace());
848
849#else
850 newItem = new QTreeWidgetItem(aParent);
851 newItem->setText(0,QString((char*)(aCommandTree->GetTree(a+1)->GetPathName()).data()).trimmed());
852 newItem->setText(1,QString((char*)(aCommandTree->GetTree(a+1)->GetTitle()).data()).trimmed());
853#endif
854
855 CreateChildTree(newItem,aCommandTree->GetTree(a+1));
856 }
857
858
859
860 // Get the Commands
861
862 for (int a=0;a<aCommandTree->GetCommandEntry();a++) {
863
864 QStringList stringList;
865#if QT_VERSION < 0x040000
866 newItem = new QListViewItem(aParent);
867 newItem->setText(0,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).simplifyWhiteSpace());
868 newItem->setText(1,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).simplifyWhiteSpace());
869 newItem->setOpen(false);
870
871#else
872 newItem = new QTreeWidgetItem(aParent);
873 newItem->setText(0,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).trimmed());
874 newItem->setText(1,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).trimmed());
875#if QT_VERSION < 0x040202
876 fHelpTreeWidget->setItemExpanded(newItem,false);
877#else
878 newItem->setExpanded(false);
879#endif
880#endif
881
882 }
883}
884
885
886/** Find a treeItemWidget in the help tree
887 @param aCommand item's String to look for
888 @return item if found, NULL if not
889*/
890#if QT_VERSION < 0x040000
891QListViewItem* G4UIQt::FindTreeItem(
892 QListViewItem *aParent
893#else
894QTreeWidgetItem* G4UIQt::FindTreeItem(
895 QTreeWidgetItem *aParent
896#endif
897,const QString& aCommand
898)
899{
900 if (aParent == NULL) return NULL;
901
902 if (aParent->text(0) == aCommand)
903 return aParent;
904
905#if QT_VERSION < 0x040000
906 QListViewItem * tmp = NULL;
907 QListViewItem* tmpItem = aParent->firstChild();
908 while (tmpItem != 0) {
909 if (!tmp)
910 tmp = FindTreeItem(tmpItem,aCommand);
911 tmpItem = tmpItem->nextSibling();
912 }
913
914#else
915 QTreeWidgetItem * tmp = NULL;
916 for (int a=0;a<aParent->childCount();a++) {
917 if (!tmp)
918 tmp = FindTreeItem(aParent->child(a),aCommand);
919 }
920#endif
921 return tmp;
922}
923
924
925/** Build the command list parameters in a QString<br>
926 Reimplement partialy the G4UIparameter.cc
927 @param aCommand : command to list parameters
928 @see G4UIparameter::List()
929 @see G4UIcommand::List()
930 @return the command list parameters, or "" if nothing
931*/
932QString G4UIQt::GetCommandList (
933 const G4UIcommand *aCommand
934)
935{
936
937 QString txt ="";
938 if (aCommand == NULL)
939 return txt;
940
941 G4String commandPath = aCommand->GetCommandPath();
942 G4String rangeString = aCommand->GetRange();
943 G4int n_guidanceEntry = aCommand->GetGuidanceEntries();
944 G4int n_parameterEntry = aCommand->GetParameterEntries();
945
946 if ((commandPath == "") &&
947 (rangeString == "") &&
948 (n_guidanceEntry == 0) &&
949 (n_parameterEntry == 0)) {
950 return txt;
951 }
952
953 if((commandPath.length()-1)!='/') {
954 txt += "Command " + QString((char*)(commandPath).data()) + "\n";
955 }
956 txt += "Guidance :\n";
957
958 for( G4int i_thGuidance=0; i_thGuidance < n_guidanceEntry; i_thGuidance++ ) {
959 txt += QString((char*)(aCommand->GetGuidanceLine(i_thGuidance)).data()) + "\n";
960 }
961 if( ! rangeString.isNull() ) {
962 txt += " Range of parameters : " + QString((char*)(rangeString).data()) + "\n";
963 }
964 if( n_parameterEntry > 0 ) {
965 G4UIparameter *param;
966
967 // Re-implementation of G4UIparameter.cc
968
969 for( G4int i_thParameter=0; i_thParameter<n_parameterEntry; i_thParameter++ ) {
970 param = aCommand->GetParameter(i_thParameter);
971 txt += "\nParameter : " + QString((char*)(param->GetParameterName()).data()) + "\n";
972 if( ! param->GetParameterGuidance().isNull() )
973 txt += QString((char*)(param->GetParameterGuidance()).data())+ "\n" ;
974 txt += " Parameter type : " + QString(QChar(param->GetParameterType())) + "\n";
975 if(param->IsOmittable()){
976 txt += " Omittable : True\n";
977 } else {
978 txt += " Omittable : False\n";
979 }
980 if( param->GetCurrentAsDefault() ) {
981 txt += " Default value : taken from the current value\n";
982 } else if( ! param->GetDefaultValue().isNull() ) {
983 txt += " Default value : " + QString((char*)(param->GetDefaultValue()).data())+ "\n";
984 }
985 if( ! param->GetParameterRange().isNull() ) {
986 txt += " Parameter range : " + QString((char*)(param->GetParameterRange()).data())+ "\n";
987 }
988 if( ! param->GetParameterCandidates().isNull() ) {
989 txt += " Candidates : " + QString((char*)(param->GetParameterCandidates()).data())+ "\n";
990 }
991 }
992 }
993 return txt;
994}
995
996
997
998/** Implement G4VBasicShell vurtual function
999 */
1000G4bool G4UIQt::GetHelpChoice(
1001 G4int& aInt
1002)
1003{
1004#ifdef GEANT4_QT_DEBUG
1005 printf("G4UIQt::GetHelpChoice SHOULD NEVER GO HERE");
1006#endif
1007 return true;
1008}
1009
1010
1011/** Implement G4VBasicShell vurtual function
1012*/
1013void G4UIQt::ExitHelp(
1014)
1015{
1016#ifdef GEANT4_QT_DEBUG
1017 printf("G4UIQt::ExitHelp SHOULD NEVER GO HERE");
1018#endif
1019}
1020
1021
1022/** Event filter method. Every event from QtApplication goes here.<br/>
1023 We apply a filter only for the Up and Down Arrow press when the QLineEdit<br/>
1024 is active. If this filter match, Up arrow we give the previous command<br/>
1025 and Down arrow will give the next if exist.<br/>
1026 @param obj Emitter of the event
1027 @param event Kind of event
1028*/
1029bool G4UIQt::eventFilter( // Should stay with a minuscule eventFilter because of Qt
1030 QObject *aObj
1031,QEvent *aEvent
1032)
1033{
1034 if (aObj == NULL) return false;
1035 if (aEvent == NULL) return false;
1036
1037 if (aObj == fCommandHistoryArea) {
1038 if (aEvent->type() == QEvent::KeyPress) {
1039 fCommandArea->setFocus();
1040 }
1041 }
1042 if (aObj == fCommandArea) {
1043 if (aEvent->type() == QEvent::KeyPress) {
1044 QKeyEvent *e = static_cast<QKeyEvent*>(aEvent);
1045 if ((e->key() == (Qt::Key_Down)) ||
1046 (e->key() == (Qt::Key_PageDown)) ||
1047 (e->key() == (Qt::Key_Up)) ||
1048 (e->key() == (Qt::Key_PageUp))) {
1049#if QT_VERSION < 0x040000
1050 // count rows...
1051 QListViewItem* tmpItem = fCommandHistoryArea->firstChild();
1052 int selection = -1;
1053 int index = 0;
1054 while (tmpItem != 0) {
1055 if (tmpItem == fCommandHistoryArea->selectedItem()) {
1056 selection = index;
1057 }
1058 index ++;
1059 tmpItem = tmpItem->nextSibling();
1060 }
1061 if (fCommandHistoryArea->childCount()) {
1062 if (selection == -1) {
1063 selection = fCommandHistoryArea->childCount()-1;
1064 } else {
1065 if (e->key() == (Qt::Key_Down)) {
1066 if (selection <(fCommandHistoryArea->childCount()-1))
1067 selection++;
1068 } else if (e->key() == (Qt::Key_PageDown)) {
1069 selection = fCommandHistoryArea->childCount()-1;
1070#else
1071 int selection = fCommandHistoryArea->currentRow();
1072 if (fCommandHistoryArea->count()) {
1073 if (selection == -1) {
1074 selection = fCommandHistoryArea->count()-1;
1075 } else {
1076 if (e->key() == (Qt::Key_Down)) {
1077 if (selection <(fCommandHistoryArea->count()-1))
1078 selection++;
1079 } else if (e->key() == (Qt::Key_PageDown)) {
1080 selection = fCommandHistoryArea->count()-1;
1081#endif
1082 } else if (e->key() == (Qt::Key_Up)) {
1083 if (selection >0)
1084 selection --;
1085 } else if (e->key() == (Qt::Key_PageUp)) {
1086 selection = 0;
1087 }
1088 }
1089 fCommandHistoryArea->clearSelection();
1090#if QT_VERSION < 0x040000
1091 QListViewItem* tmpItem = fCommandHistoryArea->firstChild();
1092 int index = 0;
1093 while (tmpItem != 0) {
1094 if (index == selection) {
1095 tmpItem->setSelected(true);
1096 fCommandHistoryArea->setCurrentItem(tmpItem);
1097 }
1098 index ++;
1099 tmpItem = tmpItem->nextSibling();
1100 }
1101#else
1102#if QT_VERSION < 0x040202
1103 fCommandHistoryArea->setItemSelected(fCommandHistoryArea->item(selection),true);
1104#else
1105 fCommandHistoryArea->item(selection)->setSelected(true);
1106#endif
1107 fCommandHistoryArea->setCurrentItem(fCommandHistoryArea->item(selection));
1108#endif
1109 }
1110 } else if (e->key() == (Qt::Key_Tab)) {
1111#if QT_VERSION < 0x040000
1112 G4String ss = Complete(fCommandArea->text().ascii());
1113#else
1114 G4String ss = Complete(fCommandArea->text().toStdString().c_str());
1115#endif
1116 fCommandArea->setText((char*)(ss.data()));
1117
1118 // do not pass by parent, it will disable widget tab focus !
1119 return true;
1120 }
1121 }
1122 }
1123 // pass the event on to the parent class
1124 return QObject::eventFilter(aObj, aEvent);
1125}
1126
1127
1128
1129
1130/***************************************************************************/
1131//
1132// SLOTS DEFINITIONS
1133//
1134/***************************************************************************/
1135
1136/** Called when user give "help" command.
1137*/
1138void G4UIQt::ShowHelpCallback (
1139)
1140{
1141 TerminalHelp("");
1142}
1143
1144
1145/** Called when user click on clear button. Clear the text Output area
1146*/
1147void G4UIQt::ClearButtonCallback (
1148)
1149{
1150 fTextArea->clear();
1151}
1152
1153/** Called when user exit session
1154*/
1155void G4UIQt::ExitSession (
1156)
1157{
1158 SessionTerminate();
1159}
1160
1161
1162/** Callback call when "click on a menu entry.<br>
1163 Send the associated command to geant4
1164*/
1165void G4UIQt::CommandEnteredCallback (
1166)
1167{
1168#if QT_VERSION < 0x040000
1169 G4String command (fCommandArea->text().ascii());
1170 if (fCommandArea->text().simplifyWhiteSpace() != "") {
1171
1172 QListViewItem *newItem = new QListViewItem(fCommandHistoryArea);
1173 newItem->setText(0,fCommandArea->text());
1174 fCommandHistoryArea->insertItem(newItem);
1175 // now we have to arrange
1176 QListViewItem *temp= fCommandHistoryArea->lastItem();
1177 for (int i=0; i<fCommandHistoryArea->childCount()-1;i++) {
1178 fCommandHistoryArea->takeItem(temp);
1179 fCommandHistoryArea->insertItem(temp);
1180 temp= fCommandHistoryArea->lastItem();
1181 }
1182#else
1183 G4String command (fCommandArea->text().toStdString().c_str());
1184 if (fCommandArea->text().trimmed() != "") {
1185 fCommandHistoryArea->addItem(fCommandArea->text());
1186#endif
1187 fCommandHistoryArea->clearSelection();
1188 fCommandHistoryArea->setCurrentItem(NULL);
1189 fCommandArea->setText("");
1190
1191 G4Qt* interactorManager = G4Qt::getInstance ();
1192 if (interactorManager) {
1193 interactorManager->FlushAndWaitExecution();
1194 }
1195 if (command(0,4) != "help") {
1196 ApplyShellCommand (command,exitSession,exitPause);
1197 } else {
1198 TerminalHelp(command);
1199 }
1200 if(exitSession==true)
1201 SessionTerminate();
1202 }
1203}
1204
1205
1206/** Callback call when "enter" clicked on the command zone.<br>
1207 Send the command to geant4
1208 @param aCommand
1209*/
1210void G4UIQt::ButtonCallback (
1211 const QString& aCommand
1212)
1213{
1214
1215#if QT_VERSION < 0x040000
1216 G4String ss = G4String(aCommand.ascii());
1217#else
1218 G4String ss = G4String(aCommand.toStdString().c_str());
1219#endif
1220 ApplyShellCommand(ss,exitSession,exitPause);
1221 if(exitSession==true)
1222 SessionTerminate();
1223}
1224
1225
1226
1227/** This callback is activated when user selected a item in the help tree
1228*/
1229void G4UIQt::HelpTreeClicCallback (
1230)
1231{
1232#if QT_VERSION < 0x040000
1233 QListViewItem* item = NULL;
1234#else
1235 QTreeWidgetItem* item = NULL;
1236#endif
1237 if (!fHelpTreeWidget)
1238 return ;
1239
1240 if (!fHelpArea)
1241 return;
1242
1243#if QT_VERSION < 0x040000
1244 item =fHelpTreeWidget->selectedItem();
1245#else
1246 QList<QTreeWidgetItem *> list =fHelpTreeWidget->selectedItems();
1247 if (list.isEmpty())
1248 return;
1249 item = list.first();
1250#endif
1251 if (!item)
1252 return;
1253
1254 G4UImanager* UI = G4UImanager::GetUIpointer();
1255 if(UI==NULL) return;
1256 G4UIcommandTree * treeTop = UI->GetTree();
1257#if QT_VERSION < 0x040000
1258 G4UIcommand* command = treeTop->FindPath(item->text (1).ascii());
1259#else
1260 G4UIcommand* command = treeTop->FindPath(item->text (1).toStdString().c_str());
1261#endif
1262 if (command) {
1263#if QT_VERSION >= 0x040000
1264#if QT_VERSION < 0x040200
1265 fHelpArea->clear();
1266 fHelpArea->append(GetCommandList(command));
1267#else
1268 fHelpArea->setText(GetCommandList(command));
1269#endif
1270#else
1271 fHelpArea->setText(GetCommandList(command));
1272#endif
1273 } else {
1274 // this is not a command, this is a sub directory
1275 // We display the Title
1276#if QT_VERSION >= 0x040000
1277#if QT_VERSION < 0x040200
1278 fHelpArea->clear();
1279 fHelpArea->append(item->text (1));
1280#else
1281 fHelpArea->setText(item->text (1));
1282#endif
1283#else
1284 fHelpArea->setText(item->text (1));
1285#endif
1286 }
1287}
1288
1289/** This callback is activated when user double clic on a item in the help tree
1290*/
1291void G4UIQt::HelpTreeDoubleClicCallback (
1292#if QT_VERSION < 0x040000
1293QListViewItem* item
1294#else
1295QTreeWidgetItem* item
1296,int
1297#endif
1298)
1299{
1300 HelpTreeClicCallback();
1301 fCommandArea->setText(item->text (1));
1302}
1303
1304
1305/** Callback called when user select an old command in the command history<br>
1306 Give it to the command area.
1307*/
1308void G4UIQt::CommandHistoryCallback(
1309)
1310{
1311#if QT_VERSION < 0x040000
1312 QListViewItem* item = NULL;
1313#else
1314 QListWidgetItem* item = NULL;
1315#endif
1316 if (!fCommandHistoryArea)
1317 return ;
1318
1319#if QT_VERSION < 0x040000
1320 item =fCommandHistoryArea->selectedItem();
1321#else
1322 QList<QListWidgetItem *> list =fCommandHistoryArea->selectedItems();
1323 if (list.isEmpty())
1324 return;
1325 item = list.first();
1326#endif
1327 if (!item)
1328 return;
1329#if QT_VERSION < 0x040000
1330 fCommandArea->setText(item->text(0));
1331#else
1332 fCommandArea->setText(item->text());
1333#endif
1334
1335}
1336
1337#endif
Note: See TracBrowser for help on using the repository browser.