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

Last change on this file since 1248 was 1248, checked in by garnier, 14 years ago

test de helpTree update

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