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

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

control A et control E

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