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

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

update geant4.9.3 tag

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