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

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

import all except CVS

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