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

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

debug visu

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