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

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

Passage sur Windows Qt4 OK

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