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

Last change on this file since 621 was 620, checked in by garnier, 18 years ago

correction du ticket #92

  • 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 interactorManager->FlushAndWaitExecution();
451 return 0;
452}
453
454
455/**
456 Receive a cerr from Geant4. We have to display it in the cout zone
457 @param aString : label to add in the display area
458 @return 0
459*/
460G4int G4UIQt::ReceiveG4cerr (
461 G4String aString
462)
463{
464 if (!aString) return 0;
465 G4Qt* interactorManager = G4Qt::getInstance ();
466 if (!interactorManager) return 0;
467
468#if QT_VERSION < 0x040000
469 QColor previousColor = fTextArea->color();
470 fTextArea->setColor(Qt::red);
471 fTextArea->append(QString((char*)aString.data()).simplifyWhiteSpace());
472 fTextArea->setColor(previousColor);
473 fTextArea->verticalScrollBar()->setValue(fTextArea->verticalScrollBar()->maxValue());
474#else
475 QColor previousColor = fTextArea->textColor();
476 fTextArea->setTextColor(Qt::red);
477 fTextArea->append(QString((char*)aString.data()).trimmed());
478 fTextArea->setTextColor(previousColor);
479 fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum());
480#endif
481 interactorManager->FlushAndWaitExecution();
482 return 0;
483}
484
485
486
487/**
488 Add a new menu to the menu bar
489 @param aName name of menu
490 @param aLabel label to display
491 */
492void G4UIQt::AddMenu (
493 const char* aName
494,const char* aLabel
495)
496{
497 if (aName == NULL) return;
498 if (aLabel == NULL) return;
499
500#if QT_VERSION < 0x040000
501 QPopupMenu *fileMenu = new QPopupMenu( fMainWindow);
502 fMainWindow->menuBar()->insertItem( aLabel, fileMenu );
503#else
504 QMenu *fileMenu = new QMenu(aLabel);
505 fMainWindow->menuBar()->insertMenu(fMainWindow->menuBar()->actions().last(),fileMenu);
506#endif
507
508 AddInteractor (aName,(G4Interactor)fileMenu);
509}
510
511
512/**
513 Add a new button to a menu
514 @param aMenu : parent menu
515 @param aLabel : label to display
516 @param aCommand : command to execute as a callback
517 */
518void G4UIQt::AddButton (
519 const char* aMenu
520,const char* aLabel
521,const char* aCommand
522)
523{
524 if(aMenu==NULL) return; // TO KEEP
525 if(aLabel==NULL) return; // TO KEEP
526 if(aCommand==NULL) return; // TO KEEP
527
528#if QT_VERSION < 0x040000
529 QPopupMenu *parent = (QPopupMenu*)GetInteractor(aMenu);
530#else
531 QMenu *parent = (QMenu*)GetInteractor(aMenu);
532#endif
533
534 if(parent==NULL) return;
535
536 QSignalMapper *signalMapper = new QSignalMapper(this);
537#if QT_VERSION < 0x030200
538 QAction *action = new QAction(QString(aLabel),QString(aLabel),QKeySequence::QKeySequence (),signalMapper, SLOT(map()));
539 action->addTo(parent);
540 connect(action,SIGNAL(activated()),signalMapper,SLOT(map()));
541
542#elif QT_VERSION < 0x040000
543 QAction *action = new QAction(QString(aLabel),QKeySequence::QKeySequence (),signalMapper, SLOT(map()));
544 action->addTo(parent);
545 connect(action,SIGNAL(clicked()),signalMapper,SLOT(map()));
546
547#else
548 QAction *action = parent->addAction(aLabel, signalMapper, SLOT(map()));
549
550#endif
551 connect(signalMapper, SIGNAL(mapped(const QString &)),this, SLOT(ButtonCallback(const QString&)));
552 signalMapper->setMapping(action, QString(aCommand));
553}
554
555
556
557
558/**
559 Open the help dialog in a separate window.<br>
560 This will be display as a tree widget.<br>
561 Implementation of <b>void G4VBasicShell::TerminalHelp(G4String newCommand)</b>
562
563 @param newCommand : open the tree widget item on this command if is set
564*/
565void G4UIQt::TerminalHelp(
566 G4String newCommand
567)
568{
569 // Create the help dialog
570 if (!fHelpDialog) {
571#if QT_VERSION < 0x040000
572 fHelpDialog = new QDialog(fMainWindow,0,FALSE,Qt::WStyle_Title | Qt::WStyle_SysMenu | Qt::WStyle_MinMax );
573 QVBoxLayout *vLayout = new QVBoxLayout(fHelpDialog);
574 QSplitter *splitter = new QSplitter(Qt::Horizontal,fHelpDialog);
575#else
576 QVBoxLayout *vLayout = new QVBoxLayout();
577 fHelpDialog = new QDialog(fMainWindow,Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
578 QSplitter *splitter = new QSplitter(Qt::Horizontal);
579#endif
580 QPushButton *exitButton = new QPushButton("Exit",fHelpDialog);
581 connect(exitButton, SIGNAL(clicked()), fHelpDialog,SLOT(close()));
582
583 // the help tree
584 G4UImanager* UI = G4UImanager::GetUIpointer();
585 if(UI==NULL) return;
586 G4UIcommandTree * treeTop = UI->GetTree();
587
588 // build widget
589#if QT_VERSION < 0x040000
590 fHelpTreeWidget = new QListView(splitter);
591 fHelpTreeWidget->setSelectionMode(QListView::Single);
592 fHelpTreeWidget->setRootIsDecorated(true);
593 fHelpTreeWidget->addColumn("Command");
594 fHelpTreeWidget->addColumn("Description",0);
595 // fHelpTreeWidget->setColumnWidth (1,0);
596 fHelpTreeWidget->header()->setResizeEnabled(FALSE,1);
597 // QList<QListViewItem *> items;
598#else
599 fHelpTreeWidget = new QTreeWidget();
600 fHelpTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
601 fHelpTreeWidget->setColumnCount(2);
602 fHelpTreeWidget->setColumnHidden(1,true);
603 QStringList labels;
604 labels << QString("Command") << QString("Description");
605 fHelpTreeWidget->setHeaderLabels(labels);
606 // QList<QTreeWidgetItem *> items;
607#endif
608
609#if QT_VERSION < 0x040000
610#ifdef GEANT4_QT_DEBUG
611 printf("G4UIQt::QTextEdit 1\n");
612#endif
613 fHelpArea = new QTextEdit(splitter);
614#ifdef GEANT4_QT_DEBUG
615 printf("G4UIQt::end 1\n");
616#endif
617#else
618#ifdef GEANT4_QT_DEBUG
619 printf("G4UIQt::QTextEdit 2\n");
620#endif
621 fHelpArea = new QTextEdit();
622#ifdef GEANT4_QT_DEBUG
623 printf("G4UIQt::end 2\n");
624#endif
625#endif
626 fHelpArea->setReadOnly(true);
627
628 G4int treeSize = treeTop->GetTreeEntry();
629#if QT_VERSION < 0x040000
630 QListViewItem * newItem;
631#else
632 QTreeWidgetItem * newItem;
633#endif
634 for (int a=0;a<treeSize;a++) {
635 // Creating new item
636
637#if QT_VERSION < 0x040000
638 newItem = new QListViewItem(fHelpTreeWidget);
639 newItem->setText(0,QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).simplifyWhiteSpace());
640 newItem->setText(1,QString((char*)(treeTop->GetTree(a+1)->GetTitle()).data()).simplifyWhiteSpace());
641#else
642 //FIXME : Qt 4.2
643 // QStringList stringList;
644 // stringList << QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).trimmed() ;
645 // stringList << QString((char*)(treeTop->GetTree(a+1)->GetTitle()).data()).trimmed() ;
646 // newItem = new QTreeWidgetItem(stringList);
647 // FIXME : Qt 4.0
648 newItem = new QTreeWidgetItem(fHelpTreeWidget);
649 newItem->setText(0,QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).trimmed());
650 newItem->setText(1,QString((char*)(treeTop->GetTree(a+1)->GetTitle()).data()).trimmed());
651#endif
652
653
654 // look for childs
655 CreateChildTree(newItem,treeTop->GetTree(a+1));
656 // items.append(newItem);
657 }
658
659#if QT_VERSION < 0x040000
660 connect(fHelpTreeWidget, SIGNAL(selectionChanged ()),this, SLOT(HelpTreeClicCallback()));
661 connect(fHelpTreeWidget, SIGNAL(doubleClicked (QListViewItem*)),this, SLOT(HelpTreeDoubleClicCallback(QListViewItem*)));
662#else
663 connect(fHelpTreeWidget, SIGNAL(itemSelectionChanged ()),this, SLOT(HelpTreeClicCallback()));
664 connect(fHelpTreeWidget, SIGNAL(itemDoubleClicked (QTreeWidgetItem*,int)),this, SLOT(HelpTreeDoubleClicCallback(QTreeWidgetItem*)));
665#endif
666
667 // Set layouts
668
669#if QT_VERSION >= 0x040000
670 splitter->addWidget(fHelpTreeWidget);
671 splitter->addWidget(fHelpArea);
672#endif
673
674
675#if QT_VERSION >= 0x040000
676 vLayout->addWidget(splitter);
677 vLayout->addWidget(exitButton);
678#else
679 vLayout->add(splitter);
680 vLayout->addWidget(exitButton);
681#endif
682
683 // set the splitter size
684#if QT_VERSION >= 0x040000
685 QList<int> list;
686#else
687 QValueList<int> list;
688#endif
689 list.append( 400 );
690 list.append( 400 );
691 splitter->setSizes(list);
692
693#if QT_VERSION >= 0x040000
694 fHelpDialog->setLayout(vLayout);
695#endif
696
697 }
698
699 // Look for the choosen command "newCommand"
700 size_t i = newCommand.index(" ");
701 G4String targetCom="";
702 if( i != std::string::npos )
703 {
704 G4String newValue = newCommand(i+1,newCommand.length()-(i+1));
705 newValue.strip(G4String::both);
706 targetCom = ModifyToFullPathCommand( newValue );
707 }
708 if (targetCom != "") {
709#if QT_VERSION < 0x040000
710 QListViewItem* findItem = NULL;
711 QListViewItem* tmpItem = fHelpTreeWidget->firstChild();
712 while (tmpItem != 0) {
713 if (!findItem) {
714 findItem = FindTreeItem(tmpItem,QString((char*)targetCom.data()));
715 }
716 tmpItem = tmpItem->nextSibling();
717 }
718#else
719 QTreeWidgetItem* findItem = NULL;
720 for (int a=0;a<fHelpTreeWidget->topLevelItemCount();a++) {
721 if (!findItem) {
722 findItem = FindTreeItem(fHelpTreeWidget->topLevelItem(a),QString((char*)targetCom.data()));
723 }
724 }
725#endif
726
727 if (findItem) {
728
729 //collapsed open item
730#if QT_VERSION < 0x040000
731
732 // FIXME : Has to be checked
733 QListViewItem* tmpItem = fHelpTreeWidget->firstChild();
734 QList<QListViewItem> openItems;
735 while ((tmpItem != 0) || (!openItems.isEmpty())) {
736 if (tmpItem->isOpen() ) {
737 tmpItem->setOpen(false);
738 openItems.append(tmpItem);
739 tmpItem = tmpItem->firstChild();
740 } else {
741 tmpItem = tmpItem->nextSibling();
742 }
743 if (tmpItem == 0) {
744 tmpItem = openItems.take(openItems.count()-1);
745 }
746 }
747#else
748 QList<QTreeWidgetItem *> selected;
749
750 selected = fHelpTreeWidget->selectedItems();
751 if ( selected.count() != 0 ) {
752 QTreeWidgetItem * tmp =selected.at( 0 );
753 while ( tmp) {
754#if QT_VERSION < 0x040202
755 fHelpTreeWidget->setItemExpanded(tmp,false);
756#else
757 tmp->setExpanded(false);
758#endif
759 tmp = tmp->parent();
760 }
761 }
762#endif
763
764 // clear old selection
765 fHelpTreeWidget->clearSelection();
766
767 // set new selection
768#if QT_VERSION >= 0x040000
769#if QT_VERSION < 0x040202
770 fHelpTreeWidget->setItemSelected(findItem,true);
771#else
772 findItem->setSelected(true);
773#endif
774#else
775 findItem->setSelected(true);
776#endif
777
778 // expand parent item
779 while ( findItem) {
780#if QT_VERSION < 0x040000
781 findItem->setOpen(true);
782#else
783#if QT_VERSION < 0x040202
784 fHelpTreeWidget->setItemExpanded(findItem,true);
785#else
786 findItem->setExpanded(true);
787#endif
788#endif
789 findItem = findItem->parent();
790 }
791
792 // Call the update of the right textArea
793 HelpTreeClicCallback();
794 }
795 }
796#if QT_VERSION < 0x040000
797 fHelpDialog->setCaption( tr( "Help on commands" ));
798#else
799 fHelpDialog->setWindowTitle(tr("Help on commands"));
800#endif
801 fHelpDialog->resize(800,600);
802 fHelpDialog->move(QPoint(400,150));
803 fHelpDialog->show();
804 fHelpDialog->raise();
805#if QT_VERSION < 0x040000
806 fHelpDialog->setActiveWindow();
807#else
808 fHelpDialog->activateWindow();
809#endif
810}
811
812
813
814/** Fill the Help Tree Widget
815 @param aParent : parent item to fill
816 @param aCommandTree : commandTree node associate with this part of the Tree
817*/
818#if QT_VERSION < 0x040000
819void G4UIQt::CreateChildTree(
820 QListViewItem *aParent
821,G4UIcommandTree *aCommandTree
822#else
823void G4UIQt::CreateChildTree(
824 QTreeWidgetItem *aParent
825,G4UIcommandTree *aCommandTree
826#endif
827)
828{
829 if (aParent == NULL) return;
830 if (aCommandTree == NULL) return;
831
832
833 // Creating new item
834#if QT_VERSION < 0x040000
835 QListViewItem * newItem;
836#else
837 QTreeWidgetItem * newItem;
838#endif
839
840 // Get the Sub directories
841 for (int a=0;a<aCommandTree->GetTreeEntry();a++) {
842
843#if QT_VERSION < 0x040000
844 newItem = new QListViewItem(aParent);
845 newItem->setText(0,QString((char*)(aCommandTree->GetTree(a+1)->GetPathName()).data()).simplifyWhiteSpace());
846 newItem->setText(1,QString((char*)(aCommandTree->GetTree(a+1)->GetTitle()).data()).simplifyWhiteSpace());
847
848#else
849 newItem = new QTreeWidgetItem(aParent);
850 newItem->setText(0,QString((char*)(aCommandTree->GetTree(a+1)->GetPathName()).data()).trimmed());
851 newItem->setText(1,QString((char*)(aCommandTree->GetTree(a+1)->GetTitle()).data()).trimmed());
852#endif
853
854 CreateChildTree(newItem,aCommandTree->GetTree(a+1));
855 }
856
857
858
859 // Get the Commands
860
861 for (int a=0;a<aCommandTree->GetCommandEntry();a++) {
862
863 QStringList stringList;
864#if QT_VERSION < 0x040000
865 newItem = new QListViewItem(aParent);
866 newItem->setText(0,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).simplifyWhiteSpace());
867 newItem->setText(1,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).simplifyWhiteSpace());
868 newItem->setOpen(false);
869
870#else
871 newItem = new QTreeWidgetItem(aParent);
872 newItem->setText(0,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).trimmed());
873 newItem->setText(1,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).trimmed());
874#if QT_VERSION < 0x040202
875 fHelpTreeWidget->setItemExpanded(newItem,false);
876#else
877 newItem->setExpanded(false);
878#endif
879#endif
880
881 }
882}
883
884
885/** Find a treeItemWidget in the help tree
886 @param aCommand item's String to look for
887 @return item if found, NULL if not
888*/
889#if QT_VERSION < 0x040000
890QListViewItem* G4UIQt::FindTreeItem(
891 QListViewItem *aParent
892#else
893QTreeWidgetItem* G4UIQt::FindTreeItem(
894 QTreeWidgetItem *aParent
895#endif
896,const QString& aCommand
897)
898{
899 if (aParent == NULL) return NULL;
900
901 if (aParent->text(0) == aCommand)
902 return aParent;
903
904#if QT_VERSION < 0x040000
905 QListViewItem * tmp = NULL;
906 QListViewItem* tmpItem = aParent->firstChild();
907 while (tmpItem != 0) {
908 if (!tmp)
909 tmp = FindTreeItem(tmpItem,aCommand);
910 tmpItem = tmpItem->nextSibling();
911 }
912
913#else
914 QTreeWidgetItem * tmp = NULL;
915 for (int a=0;a<aParent->childCount();a++) {
916 if (!tmp)
917 tmp = FindTreeItem(aParent->child(a),aCommand);
918 }
919#endif
920 return tmp;
921}
922
923
924/** Build the command list parameters in a QString<br>
925 Reimplement partialy the G4UIparameter.cc
926 @param aCommand : command to list parameters
927 @see G4UIparameter::List()
928 @see G4UIcommand::List()
929 @return the command list parameters, or "" if nothing
930*/
931QString G4UIQt::GetCommandList (
932 const G4UIcommand *aCommand
933)
934{
935
936 QString txt ="";
937 if (aCommand == NULL)
938 return txt;
939
940 G4String commandPath = aCommand->GetCommandPath();
941 G4String rangeString = aCommand->GetRange();
942 G4int n_guidanceEntry = aCommand->GetGuidanceEntries();
943 G4int n_parameterEntry = aCommand->GetParameterEntries();
944
945 if ((commandPath == "") &&
946 (rangeString == "") &&
947 (n_guidanceEntry == 0) &&
948 (n_parameterEntry == 0)) {
949 return txt;
950 }
951
952 if((commandPath.length()-1)!='/') {
953 txt += "Command " + QString((char*)(commandPath).data()) + "\n";
954 }
955 txt += "Guidance :\n";
956
957 for( G4int i_thGuidance=0; i_thGuidance < n_guidanceEntry; i_thGuidance++ ) {
958 txt += QString((char*)(aCommand->GetGuidanceLine(i_thGuidance)).data()) + "\n";
959 }
960 if( ! rangeString.isNull() ) {
961 txt += " Range of parameters : " + QString((char*)(rangeString).data()) + "\n";
962 }
963 if( n_parameterEntry > 0 ) {
964 G4UIparameter *param;
965
966 // Re-implementation of G4UIparameter.cc
967
968 for( G4int i_thParameter=0; i_thParameter<n_parameterEntry; i_thParameter++ ) {
969 param = aCommand->GetParameter(i_thParameter);
970 txt += "\nParameter : " + QString((char*)(param->GetParameterName()).data()) + "\n";
971 if( ! param->GetParameterGuidance().isNull() )
972 txt += QString((char*)(param->GetParameterGuidance()).data())+ "\n" ;
973 txt += " Parameter type : " + QString(QChar(param->GetParameterType())) + "\n";
974 if(param->IsOmittable()){
975 txt += " Omittable : True\n";
976 } else {
977 txt += " Omittable : False\n";
978 }
979 if( param->GetCurrentAsDefault() ) {
980 txt += " Default value : taken from the current value\n";
981 } else if( ! param->GetDefaultValue().isNull() ) {
982 txt += " Default value : " + QString((char*)(param->GetDefaultValue()).data())+ "\n";
983 }
984 if( ! param->GetParameterRange().isNull() ) {
985 txt += " Parameter range : " + QString((char*)(param->GetParameterRange()).data())+ "\n";
986 }
987 if( ! param->GetParameterCandidates().isNull() ) {
988 txt += " Candidates : " + QString((char*)(param->GetParameterCandidates()).data())+ "\n";
989 }
990 }
991 }
992 return txt;
993}
994
995
996
997/** Implement G4VBasicShell vurtual function
998 */
999G4bool G4UIQt::GetHelpChoice(
1000 G4int& aInt
1001)
1002{
1003#ifdef GEANT4_QT_DEBUG
1004 printf("G4UIQt::GetHelpChoice SHOULD NEVER GO HERE");
1005#endif
1006 return true;
1007}
1008
1009
1010/** Implement G4VBasicShell vurtual function
1011*/
1012void G4UIQt::ExitHelp(
1013)
1014{
1015#ifdef GEANT4_QT_DEBUG
1016 printf("G4UIQt::ExitHelp SHOULD NEVER GO HERE");
1017#endif
1018}
1019
1020
1021/** Event filter method. Every event from QtApplication goes here.<br/>
1022 We apply a filter only for the Up and Down Arrow press when the QLineEdit<br/>
1023 is active. If this filter match, Up arrow we give the previous command<br/>
1024 and Down arrow will give the next if exist.<br/>
1025 @param obj Emitter of the event
1026 @param event Kind of event
1027*/
1028bool G4UIQt::eventFilter( // Should stay with a minuscule eventFilter because of Qt
1029 QObject *aObj
1030,QEvent *aEvent
1031)
1032{
1033 if (aObj == NULL) return false;
1034 if (aEvent == NULL) return false;
1035
1036 if (aObj == fCommandHistoryArea) {
1037 if (aEvent->type() == QEvent::KeyPress) {
1038 fCommandArea->setFocus();
1039 }
1040 }
1041 if (aObj == fCommandArea) {
1042 if (aEvent->type() == QEvent::KeyPress) {
1043 QKeyEvent *e = static_cast<QKeyEvent*>(aEvent);
1044 if ((e->key() == (Qt::Key_Down)) ||
1045 (e->key() == (Qt::Key_PageDown)) ||
1046 (e->key() == (Qt::Key_Up)) ||
1047 (e->key() == (Qt::Key_PageUp))) {
1048#if QT_VERSION < 0x040000
1049 // count rows...
1050 QListViewItem* tmpItem = fCommandHistoryArea->firstChild();
1051 int selection = -1;
1052 int index = 0;
1053 while (tmpItem != 0) {
1054 if (tmpItem == fCommandHistoryArea->selectedItem()) {
1055 selection = index;
1056 }
1057 index ++;
1058 tmpItem = tmpItem->nextSibling();
1059 }
1060 if (fCommandHistoryArea->childCount()) {
1061 if (selection == -1) {
1062 selection = fCommandHistoryArea->childCount()-1;
1063 } else {
1064 if (e->key() == (Qt::Key_Down)) {
1065 if (selection <(fCommandHistoryArea->childCount()-1))
1066 selection++;
1067 } else if (e->key() == (Qt::Key_PageDown)) {
1068 selection = fCommandHistoryArea->childCount()-1;
1069#else
1070 int selection = fCommandHistoryArea->currentRow();
1071 if (fCommandHistoryArea->count()) {
1072 if (selection == -1) {
1073 selection = fCommandHistoryArea->count()-1;
1074 } else {
1075 if (e->key() == (Qt::Key_Down)) {
1076 if (selection <(fCommandHistoryArea->count()-1))
1077 selection++;
1078 } else if (e->key() == (Qt::Key_PageDown)) {
1079 selection = fCommandHistoryArea->count()-1;
1080#endif
1081 } else if (e->key() == (Qt::Key_Up)) {
1082 if (selection >0)
1083 selection --;
1084 } else if (e->key() == (Qt::Key_PageUp)) {
1085 selection = 0;
1086 }
1087 }
1088 fCommandHistoryArea->clearSelection();
1089#if QT_VERSION < 0x040000
1090 QListViewItem* tmpItem = fCommandHistoryArea->firstChild();
1091 int index = 0;
1092 while (tmpItem != 0) {
1093 if (index == selection) {
1094 tmpItem->setSelected(true);
1095 fCommandHistoryArea->setCurrentItem(tmpItem);
1096 }
1097 index ++;
1098 tmpItem = tmpItem->nextSibling();
1099 }
1100#else
1101#if QT_VERSION < 0x040202
1102 fCommandHistoryArea->setItemSelected(fCommandHistoryArea->item(selection),true);
1103#else
1104 fCommandHistoryArea->item(selection)->setSelected(true);
1105#endif
1106 fCommandHistoryArea->setCurrentItem(fCommandHistoryArea->item(selection));
1107#endif
1108 }
1109 } else if (e->key() == (Qt::Key_Tab)) {
1110#if QT_VERSION < 0x040000
1111 G4String ss = Complete(fCommandArea->text().ascii());
1112#else
1113 G4String ss = Complete(fCommandArea->text().toStdString().c_str());
1114#endif
1115 fCommandArea->setText((char*)(ss.data()));
1116
1117 // do not pass by parent, it will disable widget tab focus !
1118 return true;
1119 }
1120 }
1121 }
1122 // pass the event on to the parent class
1123 return QObject::eventFilter(aObj, aEvent);
1124}
1125
1126
1127
1128
1129/***************************************************************************/
1130//
1131// SLOTS DEFINITIONS
1132//
1133/***************************************************************************/
1134
1135/** Called when user give "help" command.
1136*/
1137void G4UIQt::ShowHelpCallback (
1138)
1139{
1140 TerminalHelp("");
1141}
1142
1143
1144/** Called when user click on clear button. Clear the text Output area
1145*/
1146void G4UIQt::ClearButtonCallback (
1147)
1148{
1149 fTextArea->clear();
1150}
1151
1152/** Called when user exit session
1153*/
1154void G4UIQt::ExitSession (
1155)
1156{
1157 SessionTerminate();
1158}
1159
1160
1161/** Callback call when "click on a menu entry.<br>
1162 Send the associated command to geant4
1163*/
1164void G4UIQt::CommandEnteredCallback (
1165)
1166{
1167#if QT_VERSION < 0x040000
1168 G4String command (fCommandArea->text().ascii());
1169 if (fCommandArea->text().simplifyWhiteSpace() != "") {
1170
1171 QListViewItem *newItem = new QListViewItem(fCommandHistoryArea);
1172 newItem->setText(0,fCommandArea->text());
1173 fCommandHistoryArea->insertItem(newItem);
1174 // now we have to arrange
1175 QListViewItem *temp= fCommandHistoryArea->lastItem();
1176 for (int i=0; i<fCommandHistoryArea->childCount()-1;i++) {
1177 fCommandHistoryArea->takeItem(temp);
1178 fCommandHistoryArea->insertItem(temp);
1179 temp= fCommandHistoryArea->lastItem();
1180 }
1181#else
1182 G4String command (fCommandArea->text().toStdString().c_str());
1183 if (fCommandArea->text().trimmed() != "") {
1184 fCommandHistoryArea->addItem(fCommandArea->text());
1185#endif
1186 fCommandHistoryArea->clearSelection();
1187 fCommandHistoryArea->setCurrentItem(NULL);
1188 fCommandArea->setText("");
1189
1190 G4Qt* interactorManager = G4Qt::getInstance ();
1191 if (interactorManager) {
1192 interactorManager->FlushAndWaitExecution();
1193 }
1194 if (command(0,4) != "help") {
1195 ApplyShellCommand (command,exitSession,exitPause);
1196 } else {
1197 TerminalHelp(command);
1198 }
1199 if(exitSession==true)
1200 SessionTerminate();
1201 }
1202}
1203
1204
1205/** Callback call when "enter" clicked on the command zone.<br>
1206 Send the command to geant4
1207 @param aCommand
1208*/
1209void G4UIQt::ButtonCallback (
1210 const QString& aCommand
1211)
1212{
1213
1214#if QT_VERSION < 0x040000
1215 G4String ss = G4String(aCommand.ascii());
1216#else
1217 G4String ss = G4String(aCommand.toStdString().c_str());
1218#endif
1219 ApplyShellCommand(ss,exitSession,exitPause);
1220 if(exitSession==true)
1221 SessionTerminate();
1222}
1223
1224
1225
1226/** This callback is activated when user selected a item in the help tree
1227*/
1228void G4UIQt::HelpTreeClicCallback (
1229)
1230{
1231#if QT_VERSION < 0x040000
1232 QListViewItem* item = NULL;
1233#else
1234 QTreeWidgetItem* item = NULL;
1235#endif
1236 if (!fHelpTreeWidget)
1237 return ;
1238
1239 if (!fHelpArea)
1240 return;
1241
1242#if QT_VERSION < 0x040000
1243 item =fHelpTreeWidget->selectedItem();
1244#else
1245 QList<QTreeWidgetItem *> list =fHelpTreeWidget->selectedItems();
1246 if (list.isEmpty())
1247 return;
1248 item = list.first();
1249#endif
1250 if (!item)
1251 return;
1252
1253 G4UImanager* UI = G4UImanager::GetUIpointer();
1254 if(UI==NULL) return;
1255 G4UIcommandTree * treeTop = UI->GetTree();
1256#if QT_VERSION < 0x040000
1257 G4UIcommand* command = treeTop->FindPath(item->text (1).ascii());
1258#else
1259 G4UIcommand* command = treeTop->FindPath(item->text (1).toStdString().c_str());
1260#endif
1261 if (command) {
1262#if QT_VERSION >= 0x040000
1263#if QT_VERSION < 0x040200
1264 fHelpArea->clear();
1265 fHelpArea->append(GetCommandList(command));
1266#else
1267 fHelpArea->setText(GetCommandList(command));
1268#endif
1269#else
1270 fHelpArea->setText(GetCommandList(command));
1271#endif
1272 } else {
1273 // this is not a command, this is a sub directory
1274 // We display the Title
1275#if QT_VERSION >= 0x040000
1276#if QT_VERSION < 0x040200
1277 fHelpArea->clear();
1278 fHelpArea->append(item->text (1));
1279#else
1280 fHelpArea->setText(item->text (1));
1281#endif
1282#else
1283 fHelpArea->setText(item->text (1));
1284#endif
1285 }
1286}
1287
1288/** This callback is activated when user double clic on a item in the help tree
1289*/
1290void G4UIQt::HelpTreeDoubleClicCallback (
1291#if QT_VERSION < 0x040000
1292QListViewItem* item
1293#else
1294QTreeWidgetItem* item
1295,int
1296#endif
1297)
1298{
1299 HelpTreeClicCallback();
1300 fCommandArea->setText(item->text (1));
1301}
1302
1303
1304/** Callback called when user select an old command in the command history<br>
1305 Give it to the command area.
1306*/
1307void G4UIQt::CommandHistoryCallback(
1308)
1309{
1310#if QT_VERSION < 0x040000
1311 QListViewItem* item = NULL;
1312#else
1313 QListWidgetItem* item = NULL;
1314#endif
1315 if (!fCommandHistoryArea)
1316 return ;
1317
1318#if QT_VERSION < 0x040000
1319 item =fCommandHistoryArea->selectedItem();
1320#else
1321 QList<QListWidgetItem *> list =fCommandHistoryArea->selectedItems();
1322 if (list.isEmpty())
1323 return;
1324 item = list.first();
1325#endif
1326 if (!item)
1327 return;
1328#if QT_VERSION < 0x040000
1329 fCommandArea->setText(item->text(0));
1330#else
1331 fCommandArea->setText(item->text());
1332#endif
1333
1334}
1335
1336#endif
Note: See TracBrowser for help on using the repository browser.