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

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

premiers essai nouvelle version de G4UIQt

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