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

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

r804@wl-72126: garnier | 2008-04-22 11:41:10 +0200
modif mineures

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