source: trunk/source/interfaces/basic/src/G4UIQt.cc @ 861

Last change on this file since 861 was 861, checked in by garnier, 16 years ago

G4cout problem fixed. See #24

  • Property svn:mime-type set to text/cpp
File size: 44.1 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.18 2008/10/02 08:50:39 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  // Check if already define in external app QMainWindow
116  bool found = false;
117#if QT_VERSION < 0x040000
118  // theses lines does nothing exept this one "GLWindow = new QDialog(0..."
119  // but if I comment them, it doesn't work...
120  QWidgetList  *list = QApplication::allWidgets();
121  QWidgetListIt it( *list );         // iterate over the widgets
122  QWidget * widget;
123  while ( (widget=it.current()) != 0 ) {  // for each widget...
124    ++it;
125    if ((found== false) && (widget->inherits("QMainWindow"))) {
126      found = true;
127    }
128  }
129  delete list;                      // delete the list, not the widgets
130#else
131  foreach (QWidget *widget, QApplication::allWidgets()) {
132    if ((found== false) && (widget->inherits("QMainWindow"))) {
133      found = true;
134    }
135  }
136#endif
137
138  if (found) {
139    G4cout        << "G4UIQt : Found an external App with a QMainWindow already defined. Aborted" << G4endl;
140    return ;
141  }
142  fMainWindow = new QMainWindow();
143
144#ifdef GEANT4_QT_DEBUG
145  printf("G4UIQt::Initialise after main window creation\n");
146#endif
147#if QT_VERSION < 0x040000
148  fMainWindow->setCaption( tr( "G4UI Session" ));
149  fMainWindow->resize(900,600);
150  fMainWindow->move(50,100);
151#else
152  fMainWindow->setWindowTitle( tr("G4UI Session") );
153  fMainWindow->resize(900,600);
154  fMainWindow->move(QPoint(50,100));
155#endif
156
157  QSplitter *splitter = new QSplitter(Qt::Vertical,fMainWindow);
158
159  // Set layouts
160
161  QWidget* topWidget = new QWidget(splitter);
162  QWidget* bottomWidget = new QWidget(splitter);
163
164  QVBoxLayout *layoutTop = new QVBoxLayout(topWidget);
165  QVBoxLayout *layoutBottom = new QVBoxLayout(bottomWidget);
166
167  // fill them
168
169  fTextArea = new QTextEdit(topWidget);
170  QPushButton *clearButton = new QPushButton("clear",topWidget);
171  connect(clearButton, SIGNAL(clicked()), SLOT(ClearButtonCallback()));
172
173#if QT_VERSION < 0x040000
174
175  fCommandHistoryArea = new QListView(bottomWidget);
176  fCommandHistoryArea->setSorting (-1, FALSE);
177  fCommandHistoryArea->setSelectionMode(QListView::Single);
178  fCommandHistoryArea->addColumn("");
179  fCommandHistoryArea->header()->hide();
180  connect(fCommandHistoryArea, SIGNAL(selectionChanged()), SLOT(CommandHistoryCallback()));
181#else
182  fCommandHistoryArea = new QListWidget();
183  fCommandHistoryArea->setSelectionMode(QAbstractItemView::SingleSelection);
184  connect(fCommandHistoryArea, SIGNAL(itemSelectionChanged()), SLOT(CommandHistoryCallback()));
185#endif
186  fCommandHistoryArea->installEventFilter(this);
187  fCommandLabel = new QLabel("",bottomWidget);
188
189  fCommandArea = new QLineEdit(bottomWidget);
190  fCommandArea->installEventFilter(this);
191#if QT_VERSION < 0x040000
192  fCommandArea->setActiveWindow();
193#else
194  fCommandArea->activateWindow();
195#endif
196  connect(fCommandArea, SIGNAL(returnPressed()), SLOT(CommandEnteredCallback()));
197#if QT_VERSION < 0x040000
198  fCommandArea->setFocusPolicy ( QWidget::StrongFocus );
199  fCommandArea->setFocus();
200#else
201  fCommandArea->setFocusPolicy ( Qt::StrongFocus );
202  fCommandArea->setFocus(Qt::TabFocusReason);
203#endif
204  fTextArea->setReadOnly(true);
205
206
207#ifdef GEANT4_QT_DEBUG
208  printf("G4UIQt::   2\n");
209#endif
210
211
212
213  layoutTop->addWidget(fTextArea);
214  layoutTop->addWidget(clearButton);
215
216#if QT_VERSION >= 0x040000
217  topWidget->setLayout(layoutTop);
218#endif
219
220  layoutBottom->addWidget(fCommandHistoryArea);
221  layoutBottom->addWidget(fCommandLabel);
222  layoutBottom->addWidget(fCommandArea);
223#if QT_VERSION >= 0x040000
224
225  bottomWidget->setLayout(layoutBottom);
226  splitter->addWidget(topWidget);
227  splitter->addWidget(bottomWidget);
228#endif
229
230
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
246#else
247
248  // Add a quit subMenu
249  QMenu *fileMenu = fMainWindow->menuBar()->addMenu("File");
250  fileMenu->addAction("Quitter", this, SLOT(ExitSession()));
251
252  // Add a Help menu
253  QMenu *helpMenu = fMainWindow->menuBar()->addMenu("Help");
254  helpMenu->addAction("Show Help", this, SLOT(ShowHelpCallback()));
255#endif
256
257  // Set the splitter size. The fTextArea sould be 2/3 on the fMainWindow
258#if QT_VERSION < 0x040000
259  QValueList<int> vals = splitter->sizes();
260#else
261  QList<int> vals = splitter->sizes();
262#endif
263  if(vals.size()==2) {
264    vals[0] = (splitter->orientation()==Qt::Vertical ? splitter->height() : splitter->width())*3/4;
265    vals[1] = (splitter->orientation()==Qt::Vertical ? splitter->height() : splitter->width())*1/4;
266    splitter->setSizes(vals);
267  }
268
269  if(UI!=NULL) UI->SetCoutDestination(this);  // TO KEEP
270}
271
272
273
274G4UIQt::~G4UIQt(
275)
276{
277  G4UImanager* UI = G4UImanager::GetUIpointer();  // TO KEEP
278  if(UI!=NULL) {  // TO KEEP
279    UI->SetSession(NULL);  // TO KEEP
280    UI->SetCoutDestination(NULL);  // TO KEEP
281  }
282 
283  if (fMainWindow!=NULL)
284    delete fMainWindow;
285}
286
287
288
289/**   Start the Qt main loop
290*/
291G4UIsession* G4UIQt::SessionStart (
292)
293{
294
295  G4Qt* interactorManager = G4Qt::getInstance ();
296
297#if QT_VERSION >= 0x040000
298#if QT_VERSION >= 0x040200
299  fMainWindow->setVisible(true);
300#else
301  fMainWindow->show();
302#endif
303#else
304  fMainWindow->show();
305#endif
306  Prompt("session");
307  exitSession = false;
308
309
310#ifdef GEANT4_QT_DEBUG
311  printf("disable secondary loop\n");
312#endif
313  interactorManager->DisableSecondaryLoop (); // TO KEEP
314  if ((QApplication*)interactorManager->GetMainInteractor())
315    ((QApplication*)interactorManager->GetMainInteractor())->exec();
316
317  // on ne passe pas le dessous ? FIXME ????
318  // je ne pense pas 13/06
319
320  //   void* event; // TO KEEP
321  //   while((event = interactorManager->GetEvent())!=NULL) {  // TO KEEP
322  //     interactorManager->DispatchEvent(event); // TO KEEP
323  //     if(exitSession==true) break; // TO KEEP
324  //   } // TO KEEP
325
326  interactorManager->EnableSecondaryLoop ();
327#ifdef GEANT4_QT_DEBUG
328  printf("enable secondary loop\n");
329#endif
330  return this;
331}
332
333
334/**   Display the prompt in the prompt area
335   @param aPrompt : string to display as the promt label
336   //FIXME : probablement inutile puisque le seul a afficher qq chose d'autre
337   que "session" est SecondaryLoop()
338*/
339void G4UIQt::Prompt (
340 G4String aPrompt
341)
342{
343  if (!aPrompt) return;
344
345  fCommandLabel->setText((char*)aPrompt.data());
346}
347
348
349void G4UIQt::SessionTerminate (
350)
351{
352  G4Qt* interactorManager = G4Qt::getInstance ();
353  fMainWindow->close();
354  ((QApplication*)interactorManager->GetMainInteractor())->exit();
355}
356
357
358
359/**
360   Called by intercoms/src/G4UImanager.cc<br>
361   Called by visualization/management/src/G4VisCommands.cc with "EndOfEvent" argument<br>
362   It have to pause the session command terminal.<br>
363   Call SecondaryLoop to wait for exit event<br>
364   @param aState
365   @see : G4VisCommandReviewKeptEvents::SetNewValue
366*/
367void G4UIQt::PauseSessionStart (
368 G4String aState
369)
370{
371  if (!aState) return;
372
373#ifdef GEANT4_QT_DEBUG
374  printf("G4UIQt::PauseSessionStart\n");
375#endif
376  if(aState=="G4_pause> ") {  // TO KEEP
377    SecondaryLoop ("Pause, type continue to exit this state"); // TO KEEP
378  } // TO KEEP
379
380  if(aState=="EndOfEvent") { // TO KEEP
381    // Picking with feed back in event data Done here !!!
382    SecondaryLoop ("End of event, type continue to exit this state"); // TO KEEP
383  } // TO KEEP
384}
385
386
387
388/**
389   Begin the secondary loop
390   @param a_prompt : label to display as the prompt label
391 */
392void G4UIQt::SecondaryLoop (
393 G4String aPrompt
394)
395{
396  if (!aPrompt) return;
397
398#ifdef GEANT4_QT_DEBUG
399  printf("G4UIQt::SecondaryLoop\n");
400#endif
401  G4Qt* interactorManager = G4Qt::getInstance (); // TO KEEP ?
402  Prompt(aPrompt); // TO KEEP
403  exitPause = false; // TO KEEP
404  void* event; // TO KEEP
405  while((event = interactorManager->GetEvent())!=NULL) {  // TO KEEP
406    interactorManager->DispatchEvent(event); // TO KEEP
407    if(exitPause==true) break; // TO KEEP
408  } // TO KEEP
409  Prompt("session"); // TO KEEP
410}
411
412
413
414/**
415   Receive a cout from Geant4. We have to display it in the cout zone
416   @param aString : label to add in the display area
417   @return 0
418*/
419G4int G4UIQt::ReceiveG4cout (
420 G4String aString
421)
422{
423  if (!aString) return 0;
424  G4Qt* interactorManager = G4Qt::getInstance ();
425  if (!interactorManager) return 0;
426 
427#if QT_VERSION < 0x040000
428  fTextArea->append(QString((char*)aString.data()).simplifyWhiteSpace());
429  fTextArea->verticalScrollBar()->setValue(fTextArea->verticalScrollBar()->maxValue());
430#else
431  fTextArea->append(QString((char*)aString.data()).trimmed());
432  fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum());
433#endif
434  fTextArea->repaint();
435  //  interactorManager->FlushAndWaitExecution();
436  return 0;
437}
438
439
440/**
441   Receive a cerr from Geant4. We have to display it in the cout zone
442   @param aString : label to add in the display area
443   @return 0
444*/
445G4int G4UIQt::ReceiveG4cerr (
446 G4String aString
447)
448{
449  if (!aString) return 0;
450  G4Qt* interactorManager = G4Qt::getInstance ();
451  if (!interactorManager) return 0;
452
453#if QT_VERSION < 0x040000
454  QColor previousColor = fTextArea->color();
455  fTextArea->setColor(Qt::red);
456  fTextArea->append(QString((char*)aString.data()).simplifyWhiteSpace());
457  fTextArea->setColor(previousColor);
458  fTextArea->verticalScrollBar()->setValue(fTextArea->verticalScrollBar()->maxValue());
459#else
460  QColor previousColor = fTextArea->textColor();
461  fTextArea->setTextColor(Qt::red);
462  fTextArea->append(QString((char*)aString.data()).trimmed());
463  fTextArea->setTextColor(previousColor);
464  fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum());
465#endif
466  fTextArea->repaint();
467  //  interactorManager->FlushAndWaitExecution();
468  return 0;
469}
470
471
472
473/**
474   Add a new menu to the menu bar
475   @param aName name of menu
476   @param aLabel label to display
477 */
478void G4UIQt::AddMenu (
479 const char* aName
480,const char* aLabel
481)
482{
483  if (aName == NULL) return;
484  if (aLabel == NULL) return;
485
486#if QT_VERSION < 0x040000
487  QPopupMenu *fileMenu = new QPopupMenu( fMainWindow);
488  fMainWindow->menuBar()->insertItem( aLabel, fileMenu );
489#else
490  QMenu *fileMenu = new QMenu(aLabel);
491  fMainWindow->menuBar()->insertMenu(fMainWindow->menuBar()->actions().last(),fileMenu);
492#endif
493
494  AddInteractor (aName,(G4Interactor)fileMenu);
495}
496
497
498/**
499   Add a new button to a menu
500   @param aMenu : parent menu
501   @param aLabel : label to display
502   @param aCommand : command to execute as a callback
503 */
504void G4UIQt::AddButton (
505 const char* aMenu
506,const char* aLabel
507,const char* aCommand
508)
509{
510  if(aMenu==NULL) return; // TO KEEP
511  if(aLabel==NULL) return; // TO KEEP
512  if(aCommand==NULL) return; // TO KEEP
513
514#if QT_VERSION < 0x040000
515  QPopupMenu *parent = (QPopupMenu*)GetInteractor(aMenu);
516#else
517  QMenu *parent = (QMenu*)GetInteractor(aMenu);
518#endif
519
520  if(parent==NULL) return;
521 
522  QSignalMapper *signalMapper = new QSignalMapper(this);
523#if QT_VERSION < 0x030200
524  QAction *action = new QAction(QString(aLabel),QString(aLabel),QKeySequence::QKeySequence (),signalMapper, SLOT(map()));
525  action->addTo(parent);
526 connect(action,SIGNAL(activated()),signalMapper,SLOT(map()));
527
528#elif QT_VERSION < 0x040000
529  QAction *action = new QAction(QString(aLabel),QKeySequence::QKeySequence (),signalMapper, SLOT(map()));
530  action->addTo(parent);
531 connect(action,SIGNAL(activated()),signalMapper,SLOT(map()));
532
533#else
534  QAction *action = parent->addAction(aLabel, signalMapper, SLOT(map()));
535
536#endif
537  connect(signalMapper, SIGNAL(mapped(const QString &)),this, SLOT(ButtonCallback(const QString&)));
538  signalMapper->setMapping(action, QString(aCommand));
539}
540
541
542
543
544/**
545   Open the help dialog in a separate window.<br>
546   This will be display as a tree widget.<br>
547   Implementation of <b>void G4VBasicShell::TerminalHelp(G4String newCommand)</b>
548
549   @param newCommand : open the tree widget item on this command if is set
550*/
551void G4UIQt::TerminalHelp(
552 G4String newCommand
553)
554{
555  // Create the help dialog
556  if (!fHelpDialog) {
557#if QT_VERSION < 0x040000
558    fHelpDialog = new QDialog(0,0,FALSE,Qt::WStyle_Title | Qt::WStyle_SysMenu | Qt::WStyle_MinMax );
559#else
560    fHelpDialog = new QDialog(0,Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
561#endif
562    QVBoxLayout *vLayout = new QVBoxLayout(fHelpDialog);
563    QWidget *helpWidget = new QWidget(fHelpDialog);
564    QSplitter *splitter = new QSplitter(Qt::Horizontal,fHelpDialog);
565    QPushButton *exitButton = new QPushButton("Exit",fHelpDialog);
566    exitButton->setAutoDefault(false);
567    connect(exitButton, SIGNAL(clicked()), fHelpDialog,SLOT(close()));
568   
569    QHBoxLayout *helpLayout = new QHBoxLayout(helpWidget);
570#if QT_VERSION < 0x040000
571    helpLayout->add(new QLabel("Search :",helpWidget));
572#else
573    helpLayout->addWidget(new QLabel("Search :",helpWidget));
574#endif
575    helpLine = new QLineEdit(fHelpDialog);
576#if QT_VERSION < 0x040000
577    helpLayout->add(helpLine);
578    connect( helpLine, SIGNAL( returnPressed () ), this, SLOT( lookForHelpStringCallback() ) );
579#else
580    helpLayout->addWidget(helpLine);
581    connect( helpLine, SIGNAL( editingFinished () ), this, SLOT( lookForHelpStringCallback() ) );
582#endif
583
584    // the help tree
585#if QT_VERSION < 0x040000
586    fHelpTreeWidget = new QListView(splitter);
587#else
588    fHelpTreeWidget = new QTreeWidget();
589#endif
590    fHelpTreeWidget = CreateHelpTree();
591
592    fHelpArea = new QTextEdit(splitter);
593    fHelpArea->setReadOnly(true);
594
595    // Set layouts
596
597#if QT_VERSION >= 0x040000
598    if (fHelpTreeWidget)
599      splitter->addWidget(fHelpTreeWidget);
600    splitter->addWidget(fHelpArea);
601#endif
602
603
604#if QT_VERSION >= 0x040000
605    vLayout->addWidget(helpWidget);
606    vLayout->addWidget(splitter,1);
607    vLayout->addWidget(exitButton);
608#else
609    vLayout->addWidget(helpWidget);
610    vLayout->addWidget(splitter,1);
611    vLayout->addWidget(exitButton);
612#endif
613
614    // set the splitter size
615#if QT_VERSION >= 0x040000
616    QList<int> list;
617#else
618    QValueList<int> list;
619#endif
620    list.append( 400 );
621    list.append( 400 );
622    splitter->setSizes(list);
623
624#if QT_VERSION >= 0x040000
625    fHelpDialog->setLayout(vLayout);
626#endif
627
628  }
629
630  ActivateCommand(newCommand);
631
632#if QT_VERSION < 0x040000
633  fHelpDialog->setCaption( tr( "Help on commands" ));
634#else
635  fHelpDialog->setWindowTitle(tr("Help on commands"));
636#endif
637  fHelpDialog->resize(800,600);
638  fHelpDialog->move(QPoint(400,150));
639  fHelpDialog->show();
640  fHelpDialog->raise();
641#if QT_VERSION < 0x040000
642  fHelpDialog->setActiveWindow();
643#else
644  fHelpDialog->activateWindow();
645#endif
646}
647
648
649void G4UIQt::ActivateCommand(
650 G4String newCommand
651)
652{
653  if (!fHelpTreeWidget) {
654    return;
655  }
656  // Look for the choosen command "newCommand"
657  size_t i = newCommand.index(" ");
658  G4String targetCom="";
659  if( i != std::string::npos )
660    {
661      G4String newValue = newCommand(i+1,newCommand.length()-(i+1));
662      newValue.strip(G4String::both);
663      targetCom = ModifyToFullPathCommand( newValue );
664    }
665  if (targetCom != "") {
666#if QT_VERSION < 0x040000
667    QListViewItem* findItem = NULL;
668    QListViewItem* tmpItem = fHelpTreeWidget->firstChild();
669    while (tmpItem != 0) {
670      if (!findItem) {
671        findItem = FindTreeItem(tmpItem,QString((char*)targetCom.data()));
672      }
673      tmpItem = tmpItem->nextSibling();
674    }
675#else
676    QTreeWidgetItem* findItem = NULL;
677    for (int a=0;a<fHelpTreeWidget->topLevelItemCount();a++) {
678      if (!findItem) {
679        findItem = FindTreeItem(fHelpTreeWidget->topLevelItem(a),QString((char*)targetCom.data()));
680      }
681    }
682#endif
683   
684    if (findItem) {     
685     
686      //collapsed open item
687#if QT_VERSION < 0x040000
688
689      // FIXME : Has to be checked
690      QListViewItem* tmpItem = fHelpTreeWidget->firstChild();
691      QList<QListViewItem> openItems;
692      while ((tmpItem != 0) || (!openItems.isEmpty())) {
693        if (tmpItem->isOpen() ) {
694          tmpItem->setOpen(false);
695          openItems.append(tmpItem);
696          tmpItem = tmpItem->firstChild();
697        } else {
698          tmpItem = tmpItem->nextSibling();
699        }
700        if (tmpItem == 0) {
701          tmpItem = openItems.take(openItems.count()-1);
702        }
703      }
704#else
705      QList<QTreeWidgetItem *> selected;
706
707      selected = fHelpTreeWidget->selectedItems();
708      if ( selected.count() != 0 ) {
709        QTreeWidgetItem * tmp =selected.at( 0 );
710        while ( tmp) {
711#if QT_VERSION < 0x040202
712              fHelpTreeWidget->setItemExpanded(tmp,false);
713#else
714          tmp->setExpanded(false);
715#endif
716          tmp = tmp->parent();
717        }
718      }
719#endif
720     
721      // clear old selection
722      fHelpTreeWidget->clearSelection();
723
724      // set new selection
725#if QT_VERSION >= 0x040000
726#if QT_VERSION < 0x040202
727      fHelpTreeWidget->setItemSelected(findItem,true);
728#else
729      findItem->setSelected(true);
730#endif     
731#else
732      findItem->setSelected(true);
733#endif
734     
735      // expand parent item
736      while ( findItem) {
737#if QT_VERSION < 0x040000
738        findItem->setOpen(true);
739#else
740#if QT_VERSION < 0x040202
741            fHelpTreeWidget->setItemExpanded(findItem,true);
742#else
743        findItem->setExpanded(true);
744#endif
745#endif
746        findItem = findItem->parent();
747      }
748
749      // Call the update of the right textArea
750      HelpTreeClicCallback();
751    }
752  }
753}
754
755
756
757/**
758   Create the help tree widget
759   @param parent : parent of tree widget
760   @return the widget containing the tree or NULL if it could not have beeen created
761 */
762
763#if QT_VERSION < 0x040000
764QListView * G4UIQt::CreateHelpTree()
765#else
766QTreeWidget * G4UIQt::CreateHelpTree()
767#endif
768{
769  G4UImanager* UI = G4UImanager::GetUIpointer();
770  if(UI==NULL) return NULL;
771  G4UIcommandTree * treeTop = UI->GetTree();
772
773
774  // build widget
775#if QT_VERSION < 0x040000
776  fHelpTreeWidget->setSelectionMode(QListView::Single);
777  fHelpTreeWidget->setRootIsDecorated(true);
778  fHelpTreeWidget->addColumn("Command");
779  fHelpTreeWidget->header()->setResizeEnabled(FALSE,1);
780#else
781  fHelpTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
782  QStringList labels;
783  labels << QString("Command");
784  fHelpTreeWidget->setHeaderLabels(labels);
785#endif
786
787  G4int treeSize = treeTop->GetTreeEntry();
788#if QT_VERSION < 0x040000
789  QListViewItem * newItem;
790#else
791  QTreeWidgetItem * newItem;
792#endif
793  for (int a=0;a<treeSize;a++) {
794    // Creating new item
795
796#if QT_VERSION < 0x040000
797    newItem = new QListViewItem(fHelpTreeWidget);
798    newItem->setText(0,QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).simplifyWhiteSpace());
799#else
800    newItem = new QTreeWidgetItem(fHelpTreeWidget);
801    newItem->setText(0,QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).trimmed());
802#endif
803
804
805    // look for childs
806    CreateChildTree(newItem,treeTop->GetTree(a+1));
807    //      items.append(newItem);
808  }
809
810
811#if QT_VERSION < 0x040000
812  connect(fHelpTreeWidget, SIGNAL(selectionChanged ()),this, SLOT(HelpTreeClicCallback())); 
813  connect(fHelpTreeWidget, SIGNAL(doubleClicked (QListViewItem*)),this, SLOT(HelpTreeDoubleClicCallback()));
814#else
815  connect(fHelpTreeWidget, SIGNAL(itemSelectionChanged ()),this, SLOT(HelpTreeClicCallback())); 
816  connect(fHelpTreeWidget, SIGNAL(itemDoubleClicked (QTreeWidgetItem*,int)),this, SLOT(HelpTreeDoubleClicCallback())); 
817#endif
818
819  return fHelpTreeWidget;
820}
821
822
823
824/**   Fill the Help Tree Widget
825   @param aParent : parent item to fill
826   @param aCommandTree : commandTree node associate with this part of the Tree
827*/
828#if QT_VERSION < 0x040000
829void G4UIQt::CreateChildTree(
830 QListViewItem *aParent
831,G4UIcommandTree *aCommandTree
832#else
833void G4UIQt::CreateChildTree(
834 QTreeWidgetItem *aParent
835,G4UIcommandTree *aCommandTree
836#endif
837)
838{
839  if (aParent == NULL) return;
840  if (aCommandTree == NULL) return;
841
842
843  // Creating new item
844#if QT_VERSION < 0x040000
845  QListViewItem * newItem;
846#else
847  QTreeWidgetItem * newItem;
848#endif
849
850  // Get the Sub directories
851  for (int a=0;a<aCommandTree->GetTreeEntry();a++) {
852
853#if QT_VERSION < 0x040000
854      newItem = new QListViewItem(aParent);
855      newItem->setText(0,QString((char*)(aCommandTree->GetTree(a+1)->GetPathName()).data()).simplifyWhiteSpace());
856
857#else
858    newItem = new QTreeWidgetItem(aParent);
859    newItem->setText(0,QString((char*)(aCommandTree->GetTree(a+1)->GetPathName()).data()).trimmed());
860#endif
861
862    CreateChildTree(newItem,aCommandTree->GetTree(a+1));
863  }
864
865
866
867  // Get the Commands
868
869  for (int a=0;a<aCommandTree->GetCommandEntry();a++) {
870   
871    QStringList stringList;
872#if QT_VERSION < 0x040000
873    newItem = new QListViewItem(aParent);
874    newItem->setText(0,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).simplifyWhiteSpace());
875    newItem->setOpen(false);
876
877#else
878    newItem = new QTreeWidgetItem(aParent);
879    newItem->setText(0,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).trimmed());
880#if QT_VERSION < 0x040202
881        fHelpTreeWidget->setItemExpanded(newItem,false);
882#else
883    newItem->setExpanded(false);
884#endif
885#endif
886     
887  }
888}
889
890
891/** Find a treeItemWidget in the help tree
892    @param aCommand item's String to look for
893    @return item if found, NULL if not
894*/
895#if QT_VERSION < 0x040000
896QListViewItem* G4UIQt::FindTreeItem(
897 QListViewItem *aParent
898#else
899QTreeWidgetItem* G4UIQt::FindTreeItem(
900 QTreeWidgetItem *aParent
901#endif
902,const QString& aCommand
903)
904{
905  if (aParent == NULL) return NULL;
906
907  if (aParent->text(0) == aCommand)
908    return aParent;
909 
910#if QT_VERSION < 0x040000
911  QListViewItem * tmp = NULL;
912  QListViewItem* tmpItem = aParent->firstChild();
913    while (tmpItem != 0) {
914      if (!tmp)
915        tmp = FindTreeItem(tmpItem,aCommand);
916      tmpItem = tmpItem->nextSibling();
917    }
918
919#else
920  QTreeWidgetItem * tmp = NULL;
921  for (int a=0;a<aParent->childCount();a++) {
922    if (!tmp)
923      tmp = FindTreeItem(aParent->child(a),aCommand);
924  }
925#endif
926  return tmp;
927}
928
929
930/**   Build the command list parameters in a QString<br>
931   Reimplement partialy the G4UIparameter.cc
932   @param aCommand : command to list parameters
933   @see G4UIparameter::List()
934   @see G4UIcommand::List()
935   @return the command list parameters, or "" if nothing
936*/
937QString G4UIQt::GetCommandList (
938 const G4UIcommand *aCommand
939)
940{
941
942  QString txt ="";
943  if (aCommand == NULL)
944    return txt;
945
946  G4String commandPath = aCommand->GetCommandPath();
947  G4String rangeString = aCommand->GetRange();
948  G4int n_guidanceEntry = aCommand->GetGuidanceEntries();
949  G4int n_parameterEntry = aCommand->GetParameterEntries();
950 
951  if ((commandPath == "") &&
952      (rangeString == "") &&
953      (n_guidanceEntry == 0) &&
954      (n_parameterEntry == 0)) {
955    return txt;
956  }
957
958  if((commandPath.length()-1)!='/') {
959    txt += "Command " + QString((char*)(commandPath).data()) + "\n";
960  }
961  txt += "Guidance :\n";
962 
963  for( G4int i_thGuidance=0; i_thGuidance < n_guidanceEntry; i_thGuidance++ ) {
964    txt += QString((char*)(aCommand->GetGuidanceLine(i_thGuidance)).data()) + "\n";
965  }
966  if( ! rangeString.isNull() ) {
967    txt += " Range of parameters : " + QString((char*)(rangeString).data()) + "\n";
968  }
969  if( n_parameterEntry > 0 ) {
970    G4UIparameter *param;
971   
972    // Re-implementation of G4UIparameter.cc
973   
974    for( G4int i_thParameter=0; i_thParameter<n_parameterEntry; i_thParameter++ ) {
975      param = aCommand->GetParameter(i_thParameter);
976      txt += "\nParameter : " + QString((char*)(param->GetParameterName()).data()) + "\n";
977      if( ! param->GetParameterGuidance().isNull() )
978        txt += QString((char*)(param->GetParameterGuidance()).data())+ "\n" ;
979      txt += " Parameter type  : " + QString(QChar(param->GetParameterType())) + "\n";
980      if(param->IsOmittable()){
981        txt += " Omittable       : True\n";
982      } else {
983        txt += " Omittable       : False\n";
984      }
985      if( param->GetCurrentAsDefault() ) {
986        txt += " Default value   : taken from the current value\n";
987      } else if( ! param->GetDefaultValue().isNull() ) {
988        txt += " Default value   : " + QString((char*)(param->GetDefaultValue()).data())+ "\n";
989      }
990      if( ! param->GetParameterRange().isNull() ) {
991        txt += " Parameter range : " + QString((char*)(param->GetParameterRange()).data())+ "\n";
992      }
993      if( ! param->GetParameterCandidates().isNull() ) {
994        txt += " Candidates      : " + QString((char*)(param->GetParameterCandidates()).data())+ "\n";
995      }
996    }
997  }
998  return txt;
999}
1000
1001
1002
1003/**  Implement G4VBasicShell vurtual function
1004 */
1005G4bool G4UIQt::GetHelpChoice(
1006 G4int& aInt
1007)
1008{
1009#ifdef GEANT4_QT_DEBUG
1010  printf("G4UIQt::GetHelpChoice SHOULD NEVER GO HERE");
1011#endif
1012  return true;
1013}
1014
1015
1016/**   Implement G4VBasicShell vurtual function
1017*/
1018void G4UIQt::ExitHelp(
1019)
1020{
1021#ifdef GEANT4_QT_DEBUG
1022  printf("G4UIQt::ExitHelp SHOULD NEVER GO HERE");
1023#endif
1024}
1025
1026
1027/**   Event filter method. Every event from QtApplication goes here.<br/>
1028   We apply a filter only for the Up and Down Arrow press when the QLineEdit<br/>
1029   is active. If this filter match, Up arrow we give the previous command<br/>
1030   and Down arrow will give the next if exist.<br/>
1031   @param obj Emitter of the event
1032   @param event Kind of event
1033*/
1034bool G4UIQt::eventFilter( // Should stay with a minuscule eventFilter because of Qt
1035 QObject *aObj
1036,QEvent *aEvent
1037)
1038{
1039  if (aObj == NULL) return false;
1040  if (aEvent == NULL) return false;
1041
1042  if (aObj == fCommandHistoryArea) {
1043    if (aEvent->type() == QEvent::KeyPress) {
1044      fCommandArea->setFocus();
1045    }
1046  }
1047  if (aObj == fCommandArea) {
1048    if (aEvent->type() == QEvent::KeyPress) {
1049      QKeyEvent *e = static_cast<QKeyEvent*>(aEvent);
1050      if ((e->key() == (Qt::Key_Down)) ||
1051          (e->key() == (Qt::Key_PageDown)) ||
1052          (e->key() == (Qt::Key_Up)) ||
1053          (e->key() == (Qt::Key_PageUp))) {
1054#if QT_VERSION < 0x040000
1055        // count rows...
1056        QListViewItem* tmpItem = fCommandHistoryArea->firstChild();
1057        int selection = -1;
1058        int index = 0;
1059        while (tmpItem != 0) {
1060          if (tmpItem == fCommandHistoryArea->selectedItem()) {
1061            selection = index;
1062          }
1063          index ++;
1064          tmpItem = tmpItem->nextSibling();
1065        }
1066        if (fCommandHistoryArea->childCount()) {
1067          if (selection == -1) {
1068            selection = fCommandHistoryArea->childCount()-1;
1069          } else {
1070            if (e->key() == (Qt::Key_Down)) {
1071              if (selection <(fCommandHistoryArea->childCount()-1))
1072                selection++;
1073            } else if (e->key() == (Qt::Key_PageDown)) {
1074              selection = fCommandHistoryArea->childCount()-1;
1075#else
1076        int selection = fCommandHistoryArea->currentRow();
1077        if (fCommandHistoryArea->count()) {
1078          if (selection == -1) {
1079            selection = fCommandHistoryArea->count()-1;
1080          } else {
1081            if (e->key() == (Qt::Key_Down)) {
1082              if (selection <(fCommandHistoryArea->count()-1))
1083                selection++;
1084            } else if (e->key() == (Qt::Key_PageDown)) {
1085              selection = fCommandHistoryArea->count()-1;
1086#endif
1087            } else if (e->key() == (Qt::Key_Up)) {
1088              if (selection >0)
1089                selection --;
1090            } else if (e->key() == (Qt::Key_PageUp)) {
1091              selection = 0;
1092            }
1093          }
1094          fCommandHistoryArea->clearSelection();
1095#if QT_VERSION < 0x040000
1096          QListViewItem* tmpItem = fCommandHistoryArea->firstChild();
1097          int index = 0;
1098          while (tmpItem != 0) {
1099            if (index == selection) {
1100              tmpItem->setSelected(true);
1101              fCommandHistoryArea->setCurrentItem(tmpItem);
1102          }
1103          index ++;
1104          tmpItem = tmpItem->nextSibling();
1105        }
1106#else
1107#if QT_VERSION < 0x040202
1108          fCommandHistoryArea->setItemSelected(fCommandHistoryArea->item(selection),true);
1109#else
1110          fCommandHistoryArea->item(selection)->setSelected(true);
1111#endif     
1112          fCommandHistoryArea->setCurrentItem(fCommandHistoryArea->item(selection));
1113#endif
1114        }
1115      } else if (e->key() == (Qt::Key_Tab)) {
1116#if QT_VERSION < 0x040000
1117        G4String ss = Complete(fCommandArea->text().ascii());
1118#else
1119        G4String ss = Complete(fCommandArea->text().toStdString().c_str());
1120#endif
1121        fCommandArea->setText((char*)(ss.data()));
1122
1123        // do not pass by parent, it will disable widget tab focus !
1124        return true;
1125      }
1126    }
1127  }
1128  // pass the event on to the parent class
1129  return QObject::eventFilter(aObj, aEvent);
1130}
1131
1132
1133
1134
1135/***************************************************************************/
1136//
1137//             SLOTS DEFINITIONS
1138//
1139/***************************************************************************/
1140
1141/**   Called when user give "help" command.
1142*/
1143void G4UIQt::ShowHelpCallback (
1144)
1145{
1146  TerminalHelp("");
1147}
1148
1149
1150/**   Called when user click on clear button. Clear the text Output area
1151*/
1152void G4UIQt::ClearButtonCallback (
1153)
1154{
1155  fTextArea->clear();
1156}
1157
1158/**   Called when user exit session
1159*/
1160void G4UIQt::ExitSession (
1161)
1162{
1163  SessionTerminate();
1164}
1165
1166
1167/**   Callback call when "click on a menu entry.<br>
1168   Send the associated command to geant4
1169*/
1170void G4UIQt::CommandEnteredCallback (
1171)
1172{
1173#if QT_VERSION < 0x040000
1174  G4String command (fCommandArea->text().ascii());
1175  if (fCommandArea->text().simplifyWhiteSpace() != "") {
1176
1177    QListViewItem *newItem = new QListViewItem(fCommandHistoryArea);
1178    newItem->setText(0,fCommandArea->text());
1179    fCommandHistoryArea->insertItem(newItem);
1180    // now we have to arrange
1181    QListViewItem *temp= fCommandHistoryArea->lastItem();
1182    for (int i=0; i<fCommandHistoryArea->childCount()-1;i++) {
1183      fCommandHistoryArea->takeItem(temp);
1184      fCommandHistoryArea->insertItem(temp);
1185      temp= fCommandHistoryArea->lastItem();
1186    }
1187#else
1188  G4String command (fCommandArea->text().toStdString().c_str());
1189  if (fCommandArea->text().trimmed() != "") {
1190    fCommandHistoryArea->addItem(fCommandArea->text());
1191#endif
1192    fCommandHistoryArea->clearSelection();
1193    fCommandHistoryArea->setCurrentItem(NULL);
1194    fCommandArea->setText("");
1195
1196    G4Qt* interactorManager = G4Qt::getInstance ();
1197    if (interactorManager) {
1198      interactorManager->FlushAndWaitExecution();
1199    }
1200    if (command(0,4) != "help") {
1201      ApplyShellCommand (command,exitSession,exitPause);
1202    } else {
1203      TerminalHelp(command);
1204    }
1205    if(exitSession==true)
1206      SessionTerminate();
1207  }
1208}
1209
1210
1211/**   Callback call when "enter" clicked on the command zone.<br>
1212   Send the command to geant4
1213   @param aCommand
1214*/
1215void G4UIQt::ButtonCallback (
1216 const QString& aCommand
1217)
1218{
1219#if QT_VERSION < 0x040000
1220  G4String ss = G4String(aCommand.ascii());
1221#else
1222  G4String ss = G4String(aCommand.toStdString().c_str());
1223#endif
1224  ApplyShellCommand(ss,exitSession,exitPause);
1225  if(exitSession==true)
1226    SessionTerminate();
1227}
1228
1229
1230
1231/**   This callback is activated when user selected a item in the help tree
1232*/
1233void G4UIQt::HelpTreeClicCallback (
1234)
1235{
1236#if QT_VERSION < 0x040000
1237  QListViewItem* item =  NULL;
1238#else
1239  QTreeWidgetItem* item =  NULL;
1240#endif
1241  if (!fHelpTreeWidget)
1242    return ;
1243
1244  if (!fHelpArea)
1245    return;
1246 
1247#if QT_VERSION < 0x040000
1248  item =fHelpTreeWidget->selectedItem();
1249#else
1250  QList<QTreeWidgetItem *> list =fHelpTreeWidget->selectedItems();
1251  if (list.isEmpty())
1252    return;
1253  item = list.first();
1254#endif
1255  if (!item)
1256    return;
1257 
1258  G4UImanager* UI = G4UImanager::GetUIpointer();
1259  if(UI==NULL) return;
1260  G4UIcommandTree * treeTop = UI->GetTree();
1261
1262
1263
1264  std::string itemText;
1265#if QT_VERSION < 0x040000
1266  itemText = std::string(item->text(0).ascii());
1267#else
1268  itemText = std::string(item->text(0).toStdString());
1269#endif
1270
1271  G4UIcommand* command = treeTop->FindPath(itemText.c_str());
1272
1273  if (command) {
1274#if QT_VERSION >= 0x040000
1275#if QT_VERSION < 0x040200
1276    fHelpArea->clear();
1277    fHelpArea->append(GetCommandList(command));
1278#else
1279    fHelpArea->setText(GetCommandList(command));
1280#endif
1281#else
1282    fHelpArea->setText(GetCommandList(command));
1283#endif
1284  } else {  // this is a command
1285    G4UIcommandTree* path = treeTop->FindCommandTree(itemText.c_str());
1286    if ( path) {
1287      // this is not a command, this is a sub directory
1288      // We display the Title
1289#if QT_VERSION >= 0x040000
1290#if QT_VERSION < 0x040200
1291      fHelpArea->clear();
1292      fHelpArea->append(path->GetTitle().data());
1293#else
1294      fHelpArea->setText(path->GetTitle().data());
1295#endif
1296#else
1297      fHelpArea->setText(path->GetTitle().data());
1298#endif
1299    }
1300  }
1301}
1302 
1303/**   This callback is activated when user double clic on a item in the help tree
1304*/
1305void G4UIQt::HelpTreeDoubleClicCallback (
1306)
1307{
1308  HelpTreeClicCallback();
1309
1310#if QT_VERSION < 0x040000
1311  QListViewItem* item =  NULL;
1312#else
1313  QTreeWidgetItem* item =  NULL;
1314#endif
1315  if (!fHelpTreeWidget)
1316    return ;
1317
1318  if (!fHelpArea)
1319    return;
1320 
1321#if QT_VERSION < 0x040000
1322  item =fHelpTreeWidget->selectedItem();
1323#else
1324  QList<QTreeWidgetItem *> list =fHelpTreeWidget->selectedItems();
1325  if (list.isEmpty())
1326    return;
1327  item = list.first();
1328#endif
1329  if (!item)
1330    return;
1331
1332  fCommandArea->clear();
1333  fCommandArea->setText(item->text(0));
1334}
1335
1336
1337/**   Callback called when user select an old command in the command history<br>
1338   Give it to the command area.
1339*/
1340void G4UIQt::CommandHistoryCallback(
1341)
1342{
1343#if QT_VERSION < 0x040000
1344  QListViewItem* item =  NULL;
1345#else
1346  QListWidgetItem* item =  NULL;
1347#endif
1348  if (!fCommandHistoryArea)
1349    return ;
1350
1351 
1352#if QT_VERSION < 0x040000
1353  item =fCommandHistoryArea->selectedItem();
1354#else
1355  QList<QListWidgetItem *> list =fCommandHistoryArea->selectedItems();
1356  if (list.isEmpty())
1357    return;
1358  item = list.first();
1359#endif
1360  if (!item)
1361    return;
1362#if QT_VERSION < 0x040000
1363  fCommandArea->setText(item->text(0));
1364#else
1365  fCommandArea->setText(item->text());
1366#endif
1367
1368}
1369
1370
1371/**   Callback called when user give a new string to look for<br>
1372   Display a list of matching commands descriptions. If no string is set,
1373   will display the complete help tree
1374*/
1375void G4UIQt::lookForHelpStringCallback(
1376)
1377{
1378#if QT_VERSION < 0x040200
1379  fHelpArea->clear();
1380#else
1381  fHelpArea->setText("");
1382#endif
1383  if (helpLine->text() =="") {
1384    // clear old help tree
1385    fHelpTreeWidget->clear();
1386#if QT_VERSION < 0x040000
1387    fHelpTreeWidget->removeColumn(1);
1388    fHelpTreeWidget->removeColumn(0);
1389#endif
1390    CreateHelpTree();
1391    return;
1392  }
1393
1394  // the help tree
1395  G4UImanager* UI = G4UImanager::GetUIpointer();
1396  if(UI==NULL) return;
1397  G4UIcommandTree * treeTop = UI->GetTree();
1398 
1399  G4int treeSize = treeTop->GetTreeEntry();
1400
1401  // clear old help tree
1402  fHelpTreeWidget->clear();
1403#if QT_VERSION < 0x040000
1404  fHelpTreeWidget->removeColumn(1);
1405  fHelpTreeWidget->removeColumn(0);
1406#endif
1407
1408  // look for new items
1409
1410  int tmp = 0;
1411#if QT_VERSION < 0x040000
1412  int multFactor = 1000; // factor special for having double keys in Qt3
1413  int doubleKeyAdd = 0;  // decay for doubleKeys in Qt3
1414#endif
1415
1416  QMap<int,QString> commandResultMap;
1417  QMap<int,QString> commandChildResultMap;
1418
1419  for (int a=0;a<treeSize;a++) {
1420    G4UIcommand* command = treeTop->FindPath(treeTop->GetTree(a+1)->GetPathName().data());
1421#if QT_VERSION > 0x040000
1422    tmp = GetCommandList (command).count(helpLine->text(),Qt::CaseInsensitive);
1423#else
1424    tmp = GetCommandList (command).contains(helpLine->text(),false);
1425#endif
1426    if (tmp >0) {
1427#if QT_VERSION > 0x040000
1428      commandResultMap.insertMulti(tmp,QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()));
1429#else // tricky thing for Qt3...
1430      doubleKeyAdd = 0;
1431      while (commandResultMap.find( tmp*multFactor+doubleKeyAdd) != commandResultMap.end()) {
1432        doubleKeyAdd ++;
1433      }
1434      commandResultMap.insert( tmp*multFactor+doubleKeyAdd,QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()) );
1435#endif
1436    }
1437    // look for childs
1438    commandChildResultMap = LookForHelpStringInChildTree(treeTop->GetTree(a+1),helpLine->text());
1439    // insert new childs
1440    if (!commandChildResultMap.empty()) {
1441#if QT_VERSION > 0x040000
1442      QMap<int,QString>::const_iterator i = commandChildResultMap.constBegin();
1443      while (i != commandChildResultMap.constEnd()) {
1444        commandResultMap.insertMulti(i.key(),i.value());
1445#else // tricky thing for Qt3...
1446      QMap<int,QString>::const_iterator i = commandChildResultMap.begin();
1447      while (i != commandChildResultMap.end()) {
1448        doubleKeyAdd = 0;
1449        while (commandResultMap.find( i.key()*multFactor+doubleKeyAdd) != commandResultMap.end()) {
1450          doubleKeyAdd ++;
1451        }
1452        commandResultMap.insert(i.key()*multFactor+doubleKeyAdd,i.data());
1453#endif
1454        i++;
1455      }
1456      commandChildResultMap.clear();
1457    }
1458  }
1459
1460  // build new help tree
1461#if QT_VERSION < 0x040000
1462  fHelpTreeWidget->setSelectionMode(QListView::Single);
1463  fHelpTreeWidget->setRootIsDecorated(true);
1464  fHelpTreeWidget->addColumn("Command");
1465  fHelpTreeWidget->addColumn("Match");
1466  //  fHelpTreeWidget->header()->setResizeEnabled(FALSE,1);
1467#else
1468  fHelpTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
1469  fHelpTreeWidget->setColumnCount(2);
1470  QStringList labels;
1471  labels << QString("Command") << QString("Match");
1472  fHelpTreeWidget->setHeaderLabels(labels);
1473#endif
1474
1475  if (commandResultMap.empty()) {
1476#if QT_VERSION < 0x040200
1477    fHelpArea->clear();
1478    fHelpArea->append("No match found");
1479#else
1480    fHelpArea->setText("No match found");
1481#endif
1482    return;
1483  }
1484
1485#if QT_VERSION > 0x040000
1486  QMap<int,QString>::const_iterator i = commandResultMap.constEnd();
1487#else
1488  QMap<int,QString>::const_iterator i = commandResultMap.end();
1489#endif
1490  i--;
1491  // 10 maximum progress values
1492  float multValue = 10.0/(float)(i.key());
1493  QString progressChar = "|";
1494  QString progressStr = "|";
1495
1496#if QT_VERSION < 0x040000
1497  QListViewItem * newItem;
1498#else
1499  QTreeWidgetItem * newItem;
1500#endif
1501  bool end = false;
1502  while (!end) {
1503#if QT_VERSION > 0x040000
1504    if (i == commandResultMap.constBegin()) {
1505#else
1506    if (i == commandResultMap.begin()) {
1507#endif
1508      end = true;
1509    }
1510    for(int a=0;a<int(i.key()*multValue);a++) {
1511      progressStr += progressChar;
1512    }
1513#if QT_VERSION < 0x040000
1514    newItem = new QListViewItem(fHelpTreeWidget);
1515    newItem->setText(0,i.data().simplifyWhiteSpace());
1516#else
1517    newItem = new QTreeWidgetItem(fHelpTreeWidget);
1518    newItem->setText(0,i.value().trimmed());
1519#endif
1520    newItem->setText(1,progressStr);
1521   
1522#if QT_VERSION >= 0x040200
1523    newItem->setForeground ( 1, QBrush(Qt::blue) );
1524#endif
1525    progressStr = "|";
1526    i--;
1527  }
1528  // FIXME :  to be checked on Qt3
1529#if QT_VERSION < 0x040000
1530  fHelpTreeWidget->setColumnWidthMode (1,QListView::Maximum);
1531  fHelpTreeWidget->setSorting(1,false);
1532#else
1533  fHelpTreeWidget->resizeColumnToContents (0);
1534  fHelpTreeWidget->sortItems(1,Qt::DescendingOrder);
1535  //  fHelpTreeWidget->setColumnWidth(1,10);//resizeColumnToContents (1);
1536#endif
1537}
1538
1539
1540
1541
1542QMap<int,QString> G4UIQt::LookForHelpStringInChildTree(
1543 G4UIcommandTree *aCommandTree
1544,const QString & text
1545 )
1546{
1547  QMap<int,QString> commandResultMap;
1548  if (aCommandTree == NULL) return commandResultMap;
1549 
1550#if QT_VERSION < 0x040000
1551  int multFactor = 1000; // factor special for having double keys in Qt3
1552  int doubleKeyAdd = 0;  // decay for doubleKeys in Qt3
1553#endif
1554
1555  // Get the Sub directories
1556  int tmp = 0;
1557  QMap<int,QString> commandChildResultMap;
1558 
1559  for (int a=0;a<aCommandTree->GetTreeEntry();a++) {
1560    const G4UIcommand* command = aCommandTree->GetGuidance();
1561#if QT_VERSION > 0x040000
1562    tmp = GetCommandList (command).count(text,Qt::CaseInsensitive);
1563#else
1564    tmp = GetCommandList (command).contains(text,false);
1565#endif
1566    if (tmp >0) {
1567#if QT_VERSION > 0x040000
1568      commandResultMap.insertMulti(tmp,QString((char*)(aCommandTree->GetTree(a+1)->GetPathName()).data()));
1569#else // tricky thing for Qt3...
1570      doubleKeyAdd = 0;
1571      while (commandResultMap.find( tmp*multFactor+doubleKeyAdd) != commandResultMap.end()) {
1572        doubleKeyAdd ++;
1573      }
1574      commandResultMap.insert(tmp*multFactor+doubleKeyAdd,QString((char*)(aCommandTree->GetTree(a+1)->GetPathName()).data()));
1575#endif
1576    }
1577    // look for childs
1578    commandChildResultMap = LookForHelpStringInChildTree(aCommandTree->GetTree(a+1),text);
1579   
1580    if (!commandChildResultMap.empty()) {
1581      // insert new childs
1582#if QT_VERSION > 0x040000
1583      QMap<int,QString>::const_iterator i = commandChildResultMap.constBegin();
1584      while (i != commandChildResultMap.constEnd()) {
1585        commandResultMap.insertMulti(i.key(),i.value());
1586#else // tricky thing for Qt3...
1587      QMap<int,QString>::const_iterator i = commandChildResultMap.begin();
1588      while (i != commandChildResultMap.end()) {
1589        doubleKeyAdd = 0;
1590        while (commandResultMap.find( i.key()*multFactor+doubleKeyAdd) != commandResultMap.end()) {
1591          doubleKeyAdd ++;
1592        }
1593        commandResultMap.insert(i.key()*multFactor+doubleKeyAdd,i.data());
1594#endif
1595        i++;
1596      }
1597      commandChildResultMap.clear();
1598    }
1599  }
1600  // Get the Commands
1601 
1602  for (int a=0;a<aCommandTree->GetCommandEntry();a++) {
1603    const G4UIcommand* command = aCommandTree->GetCommand(a+1);
1604#if QT_VERSION > 0x040000
1605    tmp = GetCommandList (command).count(text,Qt::CaseInsensitive);
1606#else
1607    tmp = GetCommandList (command).contains(text,false);
1608#endif
1609    if (tmp >0) {
1610#if QT_VERSION > 0x040000
1611      commandResultMap.insertMulti(tmp,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()));
1612#else // tricky thing for Qt3...
1613      doubleKeyAdd = 0;
1614      while (commandResultMap.find( tmp*multFactor+doubleKeyAdd) != commandResultMap.end()) {
1615        doubleKeyAdd ++;
1616      }
1617      commandResultMap.insert(tmp*multFactor+doubleKeyAdd,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()));
1618#endif
1619#ifdef GEANT4_QT_DEBUG
1620#endif
1621    }
1622   
1623  }
1624  return commandResultMap;
1625}
1626#endif
Note: See TracBrowser for help on using the repository browser.