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

Last change on this file since 1132 was 1118, checked in by garnier, 15 years ago

en test

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