source: trunk/geant4/interfaces/basic/src/G4UIQt.cc @ 624

Last change on this file since 624 was 624, checked in by garnier, 17 years ago

corrections de bugs

  • Property svn:mime-type set to text/cpp
File size: 36.9 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.12 2007/11/15 18:24:27 lgarnier Exp $
28// GEANT4 tag $Name:  $
29//
30// L. Garnier
31
32#define GEANT4_QT_DEBUG
33
34#ifdef G4UI_BUILD_QT_SESSION
35
36#include "G4Types.hh"
37
38#include <string.h>
39
40#include "G4UIQt.hh"
41#include "G4UImanager.hh"
42#include "G4StateManager.hh"
43#include "G4UIcommandTree.hh"
44#include "G4UIcommandStatus.hh"
45
46#include "G4Qt.hh"
47
48#include <qapplication.h>
49#include <qlineedit.h>
50#include <qwidget.h>
51#include <qmenubar.h>
52#include <qlayout.h>
53#include <qpushbutton.h>
54#include <qlabel.h>
55#include <qsplitter.h>
56#include <qscrollbar.h>
57#include <qdialog.h>
58#include <qevent.h>
59#include <qtextedit.h>
60#include <qsignalmapper.h>
61
62#include <qmainwindow.h>
63#if QT_VERSION >= 0x040000
64#include <qmenu.h>
65#include <qlistwidget.h>
66#include <qtreewidget.h>
67#else
68#include <qaction.h>
69#include <qheader.h>
70#include <qlistview.h>
71#include <qpopupmenu.h>
72#endif
73
74
75
76#include <stdlib.h>
77
78// Pourquoi Static et non  variables de classe ?
79static G4bool exitSession = true;
80static G4bool exitPause = true;
81
82/**   Build a Qt window with a menubar, output area and promt area<br>
83<pre>
84   +-----------------------+
85   |exit menu|             |
86   |                       |
87   | +-------------------+ |
88   | |                   | |
89   | |  Output area      | |
90   | |                   | |
91   | +-------------------+ |
92   |      | clear |        |
93   | +-------------------+ |
94   | |  promt history    | |
95   | +-------------------+ |
96   | +-------------------+ |
97   | |> promt area       | |
98   | +-------------------+ |
99   +-----------------------+
100</pre>
101*/
102G4UIQt::G4UIQt (
103 int argc
104,char** argv
105)
106  :fHelpDialog(NULL)
107{
108#ifdef GEANT4_QT_DEBUG
109  printf("G4UIQt::Initialise %d %s\n",argc,argv[0]);
110#endif
111  G4Qt* interactorManager = G4Qt::getInstance (argc,argv,(char*)"Qt");
112  G4UImanager* UI = G4UImanager::GetUIpointer();
113  if(UI!=NULL) UI->SetSession(this);
114
115  fMainWindow = new QMainWindow();
116
117#ifdef GEANT4_QT_DEBUG
118  printf("G4UIQt::Initialise after main window creation\n");
119#endif
120#if QT_VERSION < 0x040000
121  fMainWindow->setCaption( tr( "G4UI Session" ));
122  fMainWindow->resize(800,600);
123  fMainWindow->move(50,100);
124#else
125  fMainWindow->setWindowTitle( tr("G4UI Session") );
126  fMainWindow->resize(800,600);
127  fMainWindow->move(QPoint(50,100));
128#endif
129
130  QSplitter *splitter = new QSplitter(Qt::Vertical,fMainWindow);
131
132  // Set layouts
133
134#if QT_VERSION < 0x040000
135
136  QWidget* topWidget = new QWidget(splitter);
137  QWidget* bottomWidget = new QWidget(splitter);
138
139  QVBoxLayout *layoutTop = new QVBoxLayout(topWidget);
140  QVBoxLayout *layoutBottom = new QVBoxLayout(bottomWidget);
141#else
142  QWidget* topWidget = new QWidget();
143  QWidget* bottomWidget = new QWidget();
144
145  QVBoxLayout *layoutTop = new QVBoxLayout;
146  QVBoxLayout *layoutBottom = new QVBoxLayout;
147#endif
148
149  // fill them
150
151  fTextArea = new QTextEdit(topWidget);
152#ifdef GEANT4_QT_DEBUG
153  printf("G4UIQt:: end\n");
154#endif
155#ifdef GEANT4_QT_DEBUG
156  printf("G4UIQt::PushButton 1\n");
157#endif
158  QPushButton *clearButton = new QPushButton("clear",topWidget);
159#ifdef GEANT4_QT_DEBUG
160  printf("G4UIQt::end1\n");
161#endif
162  connect(clearButton, SIGNAL(clicked()), SLOT(ClearButtonCallback()));
163
164#if QT_VERSION < 0x040000
165  fCommandHistoryArea = new QListView(bottomWidget);
166
167  fCommandHistoryArea->setSorting (-1, FALSE);
168  fCommandHistoryArea->setSelectionMode(QListView::Single);
169  fCommandHistoryArea->addColumn("");
170  fCommandHistoryArea->header()->hide();
171  connect(fCommandHistoryArea, SIGNAL(selectionChanged()), SLOT(CommandHistoryCallback()));
172#else
173  fCommandHistoryArea = new QListWidget();
174  fCommandHistoryArea->setSelectionMode(QAbstractItemView::SingleSelection);
175  connect(fCommandHistoryArea, SIGNAL(itemSelectionChanged()), SLOT(CommandHistoryCallback()));
176#endif
177
178  fCommandHistoryArea->installEventFilter(this);
179  fCommandLabel = new QLabel("",bottomWidget);
180
181  fCommandArea = new QLineEdit(bottomWidget);
182  fCommandArea->installEventFilter(this);
183#ifdef GEANT4_QT_DEBUG
184  printf("G4UIQt::   1\n");
185#endif
186#if QT_VERSION < 0x040000
187  fCommandArea->setActiveWindow();
188#else
189  fCommandArea->activateWindow();
190#endif
191  connect(fCommandArea, SIGNAL(returnPressed()), SLOT(CommandEnteredCallback()));
192#ifdef GEANT4_QT_DEBUG
193  printf("G4UIQt::   1 connect returnPressed\n");
194#endif
195#if QT_VERSION < 0x040000
196  fCommandArea->setFocusPolicy ( QWidget::StrongFocus );
197  fCommandArea->setFocus();
198#else
199  fCommandArea->setFocusPolicy ( Qt::StrongFocus );
200  fCommandArea->setFocus(Qt::TabFocusReason);
201#endif
202  fTextArea->setReadOnly(true);
203
204
205#ifdef GEANT4_QT_DEBUG
206  printf("G4UIQt::   2\n");
207#endif
208
209#ifdef GEANT4_QT_DEBUG
210  printf("G4UIQt::   3\n");
211#endif
212
213
214  layoutTop->addWidget(fTextArea);
215  layoutTop->addWidget(clearButton);
216
217#if QT_VERSION >= 0x040000
218  topWidget->setLayout(layoutTop);
219#endif
220
221#ifdef GEANT4_QT_DEBUG
222  printf("G4UIQt::   4\n");
223#endif
224  layoutBottom->addWidget(fCommandHistoryArea);
225  layoutBottom->addWidget(fCommandLabel);
226  layoutBottom->addWidget(fCommandArea);
227#if QT_VERSION >= 0x040000
228
229  bottomWidget->setLayout(layoutBottom);
230  splitter->addWidget(topWidget);
231  splitter->addWidget(bottomWidget);
232#endif
233
234
235#ifdef GEANT4_QT_DEBUG
236  printf("G4UIQt::   5\n");
237#endif
238  fMainWindow->setCentralWidget(splitter);
239  ((QApplication*)interactorManager->GetMainInteractor())->setMainWidget(fMainWindow);
240
241#if QT_VERSION < 0x040000
242
243  // Add a quit subMenu
244  QPopupMenu *fileMenu = new QPopupMenu( fMainWindow);
245  fileMenu->insertItem( "&Quitter",  this, SLOT(ExitSession()), CTRL+Key_Q );
246  fMainWindow->menuBar()->insertItem( QString("&File"), fileMenu );
247
248  // Add a Help menu
249  QPopupMenu *helpMenu = new QPopupMenu( fMainWindow );
250  helpMenu->insertItem( "&Show Help",  this, SLOT(ShowHelpCallback()), CTRL+Key_H );
251  fMainWindow->menuBar()->insertItem( QString("&Help"), helpMenu );
252
253#ifdef GEANT4_QT_DEBUG
254  printf("G4UIQt::   6\n");
255#endif
256
257#else
258
259  // Add a quit subMenu
260  QMenu *fileMenu = fMainWindow->menuBar()->addMenu("File");
261  fileMenu->addAction("Quitter", this, SLOT(ExitSession()));
262
263  // Add a Help menu
264  QMenu *helpMenu = fMainWindow->menuBar()->addMenu("Help");
265  helpMenu->addAction("Show Help", this, SLOT(ShowHelpCallback()));
266#endif
267#ifdef GEANT4_QT_DEBUG
268  printf("G4UIQt::   7\n");
269#endif
270
271  // Set the splitter size. The fTextArea sould be 2/3 on the fMainWindow
272#if QT_VERSION < 0x040000
273  QValueList<int> vals = splitter->sizes();
274#else
275  QList<int> vals = splitter->sizes();
276#endif
277  if(vals.size()==2) {
278    vals[0] = (splitter->orientation()==Qt::Vertical ? splitter->height() : splitter->width())*3/4;
279    vals[1] = (splitter->orientation()==Qt::Vertical ? splitter->height() : splitter->width())*1/4;
280    splitter->setSizes(vals);
281  }
282#ifdef GEANT4_QT_DEBUG
283  printf("G4UIQt::   8\n");
284#endif
285
286  if(UI!=NULL) UI->SetCoutDestination(this);  // TO KEEP
287}
288
289
290
291G4UIQt::~G4UIQt(
292)
293{
294  G4UImanager* UI = G4UImanager::GetUIpointer();  // TO KEEP
295  if(UI!=NULL) {  // TO KEEP
296    UI->SetSession(NULL);  // TO KEEP
297    UI->SetCoutDestination(NULL);  // TO KEEP
298  }
299 
300  if (fMainWindow!=NULL)
301    delete fMainWindow;
302}
303
304
305
306/**   Start the Qt main loop
307*/
308G4UIsession* G4UIQt::SessionStart (
309)
310{
311
312  G4Qt* interactorManager = G4Qt::getInstance ();
313
314#if QT_VERSION >= 0x040000
315#if QT_VERSION >= 0x040200
316  fMainWindow->setVisible(true);
317#else
318  fMainWindow->show();
319#endif
320#else
321  fMainWindow->show();
322#endif
323  Prompt("session");
324  exitSession = false;
325
326
327#ifdef GEANT4_QT_DEBUG
328  printf("disable secondary loop\n");
329#endif
330  interactorManager->DisableSecondaryLoop (); // TO KEEP
331  if ((QApplication*)interactorManager->GetMainInteractor())
332    ((QApplication*)interactorManager->GetMainInteractor())->exec();
333
334  // on ne passe pas le dessous ? FIXME ????
335  // je ne pense pas 13/06
336
337  //   void* event; // TO KEEP
338  //   while((event = interactorManager->GetEvent())!=NULL) {  // TO KEEP
339  //     interactorManager->DispatchEvent(event); // TO KEEP
340  //     if(exitSession==true) break; // TO KEEP
341  //   } // TO KEEP
342
343  interactorManager->EnableSecondaryLoop ();
344#ifdef GEANT4_QT_DEBUG
345  printf("enable secondary loop\n");
346#endif
347  return this;
348}
349
350
351/**   Display the prompt in the prompt area
352   @param aPrompt : string to display as the promt label
353   //FIXME : probablement inutile puisque le seul a afficher qq chose d'autre
354   que "session" est SecondaryLoop()
355*/
356void G4UIQt::Prompt (
357 G4String aPrompt
358)
359{
360  if (!aPrompt) return;
361
362  fCommandLabel->setText((char*)aPrompt.data());
363}
364
365
366void G4UIQt::SessionTerminate (
367)
368{
369  G4Qt* interactorManager = G4Qt::getInstance ();
370  fMainWindow->close();
371  ((QApplication*)interactorManager->GetMainInteractor())->exit();
372}
373
374
375
376/**
377   Called by intercoms/src/G4UImanager.cc<br>
378   Called by visualization/management/src/G4VisCommands.cc with "EndOfEvent" argument<br>
379   It have to pause the session command terminal.<br>
380   Call SecondaryLoop to wait for exit event<br>
381   @param aState
382   @see : G4VisCommandReviewKeptEvents::SetNewValue
383*/
384void G4UIQt::PauseSessionStart (
385 G4String aState
386)
387{
388  if (!aState) return;
389
390#ifdef GEANT4_QT_DEBUG
391  printf("G4UIQt::PauseSessionStart\n");
392#endif
393  if(aState=="G4_pause> ") {  // TO KEEP
394    SecondaryLoop ("Pause, type continue to exit this state"); // TO KEEP
395  } // TO KEEP
396
397  if(aState=="EndOfEvent") { // TO KEEP
398    // Picking with feed back in event data Done here !!!
399    SecondaryLoop ("End of event, type continue to exit this state"); // TO KEEP
400  } // TO KEEP
401}
402
403
404
405/**
406   Begin the secondary loop
407   @param a_prompt : label to display as the prompt label
408 */
409void G4UIQt::SecondaryLoop (
410 G4String aPrompt
411)
412{
413  if (!aPrompt) return;
414
415#ifdef GEANT4_QT_DEBUG
416  printf("G4UIQt::SecondaryLoop\n");
417#endif
418  G4Qt* interactorManager = G4Qt::getInstance (); // TO KEEP ?
419  Prompt(aPrompt); // TO KEEP
420  exitPause = false; // TO KEEP
421  void* event; // TO KEEP
422  while((event = interactorManager->GetEvent())!=NULL) {  // TO KEEP
423    interactorManager->DispatchEvent(event); // TO KEEP
424    if(exitPause==true) break; // TO KEEP
425  } // TO KEEP
426  Prompt("session"); // TO KEEP
427}
428
429
430
431/**
432   Receive a cout from Geant4. We have to display it in the cout zone
433   @param aString : label to add in the display area
434   @return 0
435*/
436G4int G4UIQt::ReceiveG4cout (
437 G4String aString
438)
439{
440  if (!aString) return 0;
441  G4Qt* interactorManager = G4Qt::getInstance ();
442  if (!interactorManager) return 0;
443 
444#if QT_VERSION < 0x040000
445  fTextArea->append(QString((char*)aString.data()).simplifyWhiteSpace());
446  fTextArea->verticalScrollBar()->setValue(fTextArea->verticalScrollBar()->maxValue());
447#else
448  fTextArea->append(QString((char*)aString.data()).trimmed());
449  fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum());
450#endif
451  // Slow down everything : disababled
452  interactorManager->FlushAndWaitExecution();
453  return 0;
454}
455
456
457/**
458   Receive a cerr from Geant4. We have to display it in the cout zone
459   @param aString : label to add in the display area
460   @return 0
461*/
462G4int G4UIQt::ReceiveG4cerr (
463 G4String aString
464)
465{
466  if (!aString) return 0;
467  G4Qt* interactorManager = G4Qt::getInstance ();
468  if (!interactorManager) return 0;
469
470#if QT_VERSION < 0x040000
471  QColor previousColor = fTextArea->color();
472  fTextArea->setColor(Qt::red);
473  fTextArea->append(QString((char*)aString.data()).simplifyWhiteSpace());
474  fTextArea->setColor(previousColor);
475  fTextArea->verticalScrollBar()->setValue(fTextArea->verticalScrollBar()->maxValue());
476#else
477  QColor previousColor = fTextArea->textColor();
478  fTextArea->setTextColor(Qt::red);
479  fTextArea->append(QString((char*)aString.data()).trimmed());
480  fTextArea->setTextColor(previousColor);
481  fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum());
482#endif
483  interactorManager->FlushAndWaitExecution();
484  return 0;
485}
486
487
488
489/**
490   Add a new menu to the menu bar
491   @param aName name of menu
492   @param aLabel label to display
493 */
494void G4UIQt::AddMenu (
495 const char* aName
496,const char* aLabel
497)
498{
499  if (aName == NULL) return;
500  if (aLabel == NULL) return;
501
502#if QT_VERSION < 0x040000
503  QPopupMenu *fileMenu = new QPopupMenu( fMainWindow);
504  fMainWindow->menuBar()->insertItem( aLabel, fileMenu );
505#else
506  QMenu *fileMenu = new QMenu(aLabel);
507  fMainWindow->menuBar()->insertMenu(fMainWindow->menuBar()->actions().last(),fileMenu);
508#endif
509
510  AddInteractor (aName,(G4Interactor)fileMenu);
511}
512
513
514/**
515   Add a new button to a menu
516   @param aMenu : parent menu
517   @param aLabel : label to display
518   @param aCommand : command to execute as a callback
519 */
520void G4UIQt::AddButton (
521 const char* aMenu
522,const char* aLabel
523,const char* aCommand
524)
525{
526  if(aMenu==NULL) return; // TO KEEP
527  if(aLabel==NULL) return; // TO KEEP
528  if(aCommand==NULL) return; // TO KEEP
529
530#if QT_VERSION < 0x040000
531  QPopupMenu *parent = (QPopupMenu*)GetInteractor(aMenu);
532#else
533  QMenu *parent = (QMenu*)GetInteractor(aMenu);
534#endif
535
536  if(parent==NULL) return;
537 
538  QSignalMapper *signalMapper = new QSignalMapper(this);
539#if QT_VERSION < 0x030200
540  QAction *action = new QAction(QString(aLabel),QString(aLabel),QKeySequence::QKeySequence (),signalMapper, SLOT(map()));
541  action->addTo(parent);
542 connect(action,SIGNAL(activated()),signalMapper,SLOT(map()));
543
544#elif QT_VERSION < 0x040000
545  QAction *action = new QAction(QString(aLabel),QKeySequence::QKeySequence (),signalMapper, SLOT(map()));
546  action->addTo(parent);
547 connect(action,SIGNAL(activated()),signalMapper,SLOT(map()));
548
549#else
550  QAction *action = parent->addAction(aLabel, signalMapper, SLOT(map()));
551
552#endif
553  connect(signalMapper, SIGNAL(mapped(const QString &)),this, SLOT(ButtonCallback(const QString&)));
554  signalMapper->setMapping(action, QString(aCommand));
555}
556
557
558
559
560/**
561   Open the help dialog in a separate window.<br>
562   This will be display as a tree widget.<br>
563   Implementation of <b>void G4VBasicShell::TerminalHelp(G4String newCommand)</b>
564
565   @param newCommand : open the tree widget item on this command if is set
566*/
567void G4UIQt::TerminalHelp(
568 G4String newCommand
569)
570{
571  // Create the help dialog
572  if (!fHelpDialog) {
573#if QT_VERSION < 0x040000
574    fHelpDialog = new QDialog(0,0,FALSE,Qt::WStyle_Title | Qt::WStyle_SysMenu | Qt::WStyle_MinMax );
575    QVBoxLayout *vLayout = new QVBoxLayout(fHelpDialog);
576    QSplitter *splitter = new QSplitter(Qt::Horizontal,fHelpDialog);
577#else
578    QVBoxLayout *vLayout = new QVBoxLayout();
579    fHelpDialog = new QDialog(fMainWindow,Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
580    QSplitter *splitter = new QSplitter(Qt::Horizontal);
581#endif
582    QPushButton *exitButton = new QPushButton("Exit",fHelpDialog);
583    connect(exitButton, SIGNAL(clicked()), fHelpDialog,SLOT(close()));
584
585    // the help tree
586    G4UImanager* UI = G4UImanager::GetUIpointer();
587    if(UI==NULL) return;
588    G4UIcommandTree * treeTop = UI->GetTree();
589
590    // build widget
591#if QT_VERSION < 0x040000
592    fHelpTreeWidget = new QListView(splitter);
593    fHelpTreeWidget->setSelectionMode(QListView::Single);
594    fHelpTreeWidget->setRootIsDecorated(true);
595    fHelpTreeWidget->addColumn("Command");
596    fHelpTreeWidget->addColumn("Description",0);
597    //    fHelpTreeWidget->setColumnWidth (1,0);
598    fHelpTreeWidget->header()->setResizeEnabled(FALSE,1);
599    //    QList<QListViewItem *> items;
600#else
601    fHelpTreeWidget = new QTreeWidget();
602    fHelpTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
603    fHelpTreeWidget->setColumnCount(2);
604    fHelpTreeWidget->setColumnHidden(1,true);
605    QStringList labels;
606    labels << QString("Command") << QString("Description");
607    fHelpTreeWidget->setHeaderLabels(labels);
608    //    QList<QTreeWidgetItem *> items;
609#endif
610
611#if QT_VERSION < 0x040000
612#ifdef GEANT4_QT_DEBUG
613  printf("G4UIQt::QTextEdit 1\n");
614#endif
615    fHelpArea = new QTextEdit(splitter);
616#ifdef GEANT4_QT_DEBUG
617  printf("G4UIQt::end 1\n");
618#endif
619#else
620#ifdef GEANT4_QT_DEBUG
621  printf("G4UIQt::QTextEdit 2\n");
622#endif
623    fHelpArea = new QTextEdit();
624#ifdef GEANT4_QT_DEBUG
625  printf("G4UIQt::end 2\n");
626#endif
627#endif
628    fHelpArea->setReadOnly(true);
629
630    G4int treeSize = treeTop->GetTreeEntry();
631#if QT_VERSION < 0x040000
632    QListViewItem * newItem;
633#else
634    QTreeWidgetItem * newItem;
635#endif
636    for (int a=0;a<treeSize;a++) {
637      // Creating new item
638
639#if QT_VERSION < 0x040000
640      newItem = new QListViewItem(fHelpTreeWidget);
641      newItem->setText(0,QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).simplifyWhiteSpace());
642      newItem->setText(1,QString((char*)(treeTop->GetTree(a+1)->GetTitle()).data()).simplifyWhiteSpace());
643#else
644                                                                                                   //FIXME : Qt 4.2
645                                                                                                   //      QStringList stringList;
646                                                                                                   //      stringList << QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).trimmed()  ;
647                                                                                                   //      stringList << QString((char*)(treeTop->GetTree(a+1)->GetTitle()).data()).trimmed()  ;
648                                                                                                   //      newItem = new QTreeWidgetItem(stringList);
649                                                                                                   // FIXME : Qt 4.0
650      newItem = new QTreeWidgetItem(fHelpTreeWidget);
651      newItem->setText(0,QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).trimmed());
652      newItem->setText(1,QString((char*)(treeTop->GetTree(a+1)->GetTitle()).data()).trimmed());
653#endif
654
655
656      // look for childs
657      CreateChildTree(newItem,treeTop->GetTree(a+1));
658      //      items.append(newItem);
659    }
660
661#if QT_VERSION < 0x040000
662    connect(fHelpTreeWidget, SIGNAL(selectionChanged ()),this, SLOT(HelpTreeClicCallback())); 
663    connect(fHelpTreeWidget, SIGNAL(doubleClicked (QListViewItem*)),this, SLOT(HelpTreeDoubleClicCallback(QListViewItem*)));
664#else
665    connect(fHelpTreeWidget, SIGNAL(itemSelectionChanged ()),this, SLOT(HelpTreeClicCallback())); 
666    connect(fHelpTreeWidget, SIGNAL(itemDoubleClicked (QTreeWidgetItem*,int)),this, SLOT(HelpTreeDoubleClicCallback(QTreeWidgetItem*))); 
667#endif
668
669    // Set layouts
670
671#if QT_VERSION >= 0x040000
672    splitter->addWidget(fHelpTreeWidget);
673    splitter->addWidget(fHelpArea);
674#endif
675
676
677#if QT_VERSION >= 0x040000
678    vLayout->addWidget(splitter);
679    vLayout->addWidget(exitButton);
680#else
681    vLayout->add(splitter);
682    vLayout->addWidget(exitButton);
683#endif
684
685    // set the splitter size
686#if QT_VERSION >= 0x040000
687    QList<int> list;
688#else
689    QValueList<int> list;
690#endif
691    list.append( 400 );
692    list.append( 400 );
693    splitter->setSizes(list);
694
695#if QT_VERSION >= 0x040000
696    fHelpDialog->setLayout(vLayout);
697#endif
698
699  }
700
701  // Look for the choosen command "newCommand"
702  size_t i = newCommand.index(" ");
703  G4String targetCom="";
704  if( i != std::string::npos )
705    {
706      G4String newValue = newCommand(i+1,newCommand.length()-(i+1));
707      newValue.strip(G4String::both);
708      targetCom = ModifyToFullPathCommand( newValue );
709    }
710  if (targetCom != "") {
711#if QT_VERSION < 0x040000
712    QListViewItem* findItem = NULL;
713    QListViewItem* tmpItem = fHelpTreeWidget->firstChild();
714    while (tmpItem != 0) {
715      if (!findItem) {
716        findItem = FindTreeItem(tmpItem,QString((char*)targetCom.data()));
717      }
718      tmpItem = tmpItem->nextSibling();
719    }
720#else
721    QTreeWidgetItem* findItem = NULL;
722    for (int a=0;a<fHelpTreeWidget->topLevelItemCount();a++) {
723      if (!findItem) {
724        findItem = FindTreeItem(fHelpTreeWidget->topLevelItem(a),QString((char*)targetCom.data()));
725      }
726    }
727#endif
728   
729    if (findItem) {     
730     
731      //collapsed open item
732#if QT_VERSION < 0x040000
733
734      // FIXME : Has to be checked
735      QListViewItem* tmpItem = fHelpTreeWidget->firstChild();
736      QList<QListViewItem> openItems;
737      while ((tmpItem != 0) || (!openItems.isEmpty())) {
738        if (tmpItem->isOpen() ) {
739          tmpItem->setOpen(false);
740          openItems.append(tmpItem);
741          tmpItem = tmpItem->firstChild();
742        } else {
743          tmpItem = tmpItem->nextSibling();
744        }
745        if (tmpItem == 0) {
746          tmpItem = openItems.take(openItems.count()-1);
747        }
748      }
749#else
750      QList<QTreeWidgetItem *> selected;
751
752      selected = fHelpTreeWidget->selectedItems();
753      if ( selected.count() != 0 ) {
754        QTreeWidgetItem * tmp =selected.at( 0 );
755        while ( tmp) {
756#if QT_VERSION < 0x040202
757              fHelpTreeWidget->setItemExpanded(tmp,false);
758#else
759          tmp->setExpanded(false);
760#endif
761          tmp = tmp->parent();
762        }
763      }
764#endif
765     
766      // clear old selection
767      fHelpTreeWidget->clearSelection();
768
769      // set new selection
770#if QT_VERSION >= 0x040000
771#if QT_VERSION < 0x040202
772      fHelpTreeWidget->setItemSelected(findItem,true);
773#else
774      findItem->setSelected(true);
775#endif     
776#else
777      findItem->setSelected(true);
778#endif
779     
780      // expand parent item
781      while ( findItem) {
782#if QT_VERSION < 0x040000
783        findItem->setOpen(true);
784#else
785#if QT_VERSION < 0x040202
786            fHelpTreeWidget->setItemExpanded(findItem,true);
787#else
788        findItem->setExpanded(true);
789#endif
790#endif
791        findItem = findItem->parent();
792      }
793
794      // Call the update of the right textArea
795      HelpTreeClicCallback();
796    }
797  }
798#if QT_VERSION < 0x040000
799  fHelpDialog->setCaption( tr( "Help on commands" ));
800#else
801  fHelpDialog->setWindowTitle(tr("Help on commands"));
802#endif
803  fHelpDialog->resize(800,600);
804  fHelpDialog->move(QPoint(400,150));
805  fHelpDialog->show();
806  fHelpDialog->raise();
807#if QT_VERSION < 0x040000
808  fHelpDialog->setActiveWindow();
809#else
810  fHelpDialog->activateWindow();
811#endif
812}
813
814
815
816/**   Fill the Help Tree Widget
817   @param aParent : parent item to fill
818   @param aCommandTree : commandTree node associate with this part of the Tree
819*/
820#if QT_VERSION < 0x040000
821void G4UIQt::CreateChildTree(
822 QListViewItem *aParent
823,G4UIcommandTree *aCommandTree
824#else
825void G4UIQt::CreateChildTree(
826 QTreeWidgetItem *aParent
827,G4UIcommandTree *aCommandTree
828#endif
829)
830{
831  if (aParent == NULL) return;
832  if (aCommandTree == NULL) return;
833
834
835  // Creating new item
836#if QT_VERSION < 0x040000
837  QListViewItem * newItem;
838#else
839  QTreeWidgetItem * newItem;
840#endif
841
842  // Get the Sub directories
843  for (int a=0;a<aCommandTree->GetTreeEntry();a++) {
844
845#if QT_VERSION < 0x040000
846      newItem = new QListViewItem(aParent);
847      newItem->setText(0,QString((char*)(aCommandTree->GetTree(a+1)->GetPathName()).data()).simplifyWhiteSpace());
848      newItem->setText(1,QString((char*)(aCommandTree->GetTree(a+1)->GetTitle()).data()).simplifyWhiteSpace());
849
850#else
851    newItem = new QTreeWidgetItem(aParent);
852    newItem->setText(0,QString((char*)(aCommandTree->GetTree(a+1)->GetPathName()).data()).trimmed());
853    newItem->setText(1,QString((char*)(aCommandTree->GetTree(a+1)->GetTitle()).data()).trimmed());
854#endif
855
856    CreateChildTree(newItem,aCommandTree->GetTree(a+1));
857  }
858
859
860
861  // Get the Commands
862
863  for (int a=0;a<aCommandTree->GetCommandEntry();a++) {
864   
865    QStringList stringList;
866#if QT_VERSION < 0x040000
867    newItem = new QListViewItem(aParent);
868    newItem->setText(0,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).simplifyWhiteSpace());
869    newItem->setText(1,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).simplifyWhiteSpace());
870    newItem->setOpen(false);
871
872#else
873    newItem = new QTreeWidgetItem(aParent);
874    newItem->setText(0,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).trimmed());
875    newItem->setText(1,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).trimmed());
876#if QT_VERSION < 0x040202
877        fHelpTreeWidget->setItemExpanded(newItem,false);
878#else
879    newItem->setExpanded(false);
880#endif
881#endif
882     
883  }
884}
885
886
887/** Find a treeItemWidget in the help tree
888    @param aCommand item's String to look for
889    @return item if found, NULL if not
890*/
891#if QT_VERSION < 0x040000
892QListViewItem* G4UIQt::FindTreeItem(
893 QListViewItem *aParent
894#else
895QTreeWidgetItem* G4UIQt::FindTreeItem(
896 QTreeWidgetItem *aParent
897#endif
898,const QString& aCommand
899)
900{
901  if (aParent == NULL) return NULL;
902
903  if (aParent->text(0) == aCommand)
904    return aParent;
905 
906#if QT_VERSION < 0x040000
907  QListViewItem * tmp = NULL;
908  QListViewItem* tmpItem = aParent->firstChild();
909    while (tmpItem != 0) {
910      if (!tmp)
911        tmp = FindTreeItem(tmpItem,aCommand);
912      tmpItem = tmpItem->nextSibling();
913    }
914
915#else
916  QTreeWidgetItem * tmp = NULL;
917  for (int a=0;a<aParent->childCount();a++) {
918    if (!tmp)
919      tmp = FindTreeItem(aParent->child(a),aCommand);
920  }
921#endif
922  return tmp;
923}
924
925
926/**   Build the command list parameters in a QString<br>
927   Reimplement partialy the G4UIparameter.cc
928   @param aCommand : command to list parameters
929   @see G4UIparameter::List()
930   @see G4UIcommand::List()
931   @return the command list parameters, or "" if nothing
932*/
933QString G4UIQt::GetCommandList (
934 const G4UIcommand *aCommand
935)
936{
937
938  QString txt ="";
939  if (aCommand == NULL)
940    return txt;
941
942  G4String commandPath = aCommand->GetCommandPath();
943  G4String rangeString = aCommand->GetRange();
944  G4int n_guidanceEntry = aCommand->GetGuidanceEntries();
945  G4int n_parameterEntry = aCommand->GetParameterEntries();
946 
947  if ((commandPath == "") &&
948      (rangeString == "") &&
949      (n_guidanceEntry == 0) &&
950      (n_parameterEntry == 0)) {
951    return txt;
952  }
953
954  if((commandPath.length()-1)!='/') {
955    txt += "Command " + QString((char*)(commandPath).data()) + "\n";
956  }
957  txt += "Guidance :\n";
958 
959  for( G4int i_thGuidance=0; i_thGuidance < n_guidanceEntry; i_thGuidance++ ) {
960    txt += QString((char*)(aCommand->GetGuidanceLine(i_thGuidance)).data()) + "\n";
961  }
962  if( ! rangeString.isNull() ) {
963    txt += " Range of parameters : " + QString((char*)(rangeString).data()) + "\n";
964  }
965  if( n_parameterEntry > 0 ) {
966    G4UIparameter *param;
967   
968    // Re-implementation of G4UIparameter.cc
969   
970    for( G4int i_thParameter=0; i_thParameter<n_parameterEntry; i_thParameter++ ) {
971      param = aCommand->GetParameter(i_thParameter);
972      txt += "\nParameter : " + QString((char*)(param->GetParameterName()).data()) + "\n";
973      if( ! param->GetParameterGuidance().isNull() )
974        txt += QString((char*)(param->GetParameterGuidance()).data())+ "\n" ;
975      txt += " Parameter type  : " + QString(QChar(param->GetParameterType())) + "\n";
976      if(param->IsOmittable()){
977        txt += " Omittable       : True\n";
978      } else {
979        txt += " Omittable       : False\n";
980      }
981      if( param->GetCurrentAsDefault() ) {
982        txt += " Default value   : taken from the current value\n";
983      } else if( ! param->GetDefaultValue().isNull() ) {
984        txt += " Default value   : " + QString((char*)(param->GetDefaultValue()).data())+ "\n";
985      }
986      if( ! param->GetParameterRange().isNull() ) {
987        txt += " Parameter range : " + QString((char*)(param->GetParameterRange()).data())+ "\n";
988      }
989      if( ! param->GetParameterCandidates().isNull() ) {
990        txt += " Candidates      : " + QString((char*)(param->GetParameterCandidates()).data())+ "\n";
991      }
992    }
993  }
994  return txt;
995}
996
997
998
999/**  Implement G4VBasicShell vurtual function
1000 */
1001G4bool G4UIQt::GetHelpChoice(
1002 G4int& aInt
1003)
1004{
1005#ifdef GEANT4_QT_DEBUG
1006  printf("G4UIQt::GetHelpChoice SHOULD NEVER GO HERE");
1007#endif
1008  return true;
1009}
1010
1011
1012/**   Implement G4VBasicShell vurtual function
1013*/
1014void G4UIQt::ExitHelp(
1015)
1016{
1017#ifdef GEANT4_QT_DEBUG
1018  printf("G4UIQt::ExitHelp SHOULD NEVER GO HERE");
1019#endif
1020}
1021
1022
1023/**   Event filter method. Every event from QtApplication goes here.<br/>
1024   We apply a filter only for the Up and Down Arrow press when the QLineEdit<br/>
1025   is active. If this filter match, Up arrow we give the previous command<br/>
1026   and Down arrow will give the next if exist.<br/>
1027   @param obj Emitter of the event
1028   @param event Kind of event
1029*/
1030bool G4UIQt::eventFilter( // Should stay with a minuscule eventFilter because of Qt
1031 QObject *aObj
1032,QEvent *aEvent
1033)
1034{
1035  if (aObj == NULL) return false;
1036  if (aEvent == NULL) return false;
1037
1038  if (aObj == fCommandHistoryArea) {
1039    if (aEvent->type() == QEvent::KeyPress) {
1040      fCommandArea->setFocus();
1041    }
1042  }
1043  if (aObj == fCommandArea) {
1044    if (aEvent->type() == QEvent::KeyPress) {
1045      QKeyEvent *e = static_cast<QKeyEvent*>(aEvent);
1046      if ((e->key() == (Qt::Key_Down)) ||
1047          (e->key() == (Qt::Key_PageDown)) ||
1048          (e->key() == (Qt::Key_Up)) ||
1049          (e->key() == (Qt::Key_PageUp))) {
1050#if QT_VERSION < 0x040000
1051        // count rows...
1052        QListViewItem* tmpItem = fCommandHistoryArea->firstChild();
1053        int selection = -1;
1054        int index = 0;
1055        while (tmpItem != 0) {
1056          if (tmpItem == fCommandHistoryArea->selectedItem()) {
1057            selection = index;
1058          }
1059          index ++;
1060          tmpItem = tmpItem->nextSibling();
1061        }
1062        if (fCommandHistoryArea->childCount()) {
1063          if (selection == -1) {
1064            selection = fCommandHistoryArea->childCount()-1;
1065          } else {
1066            if (e->key() == (Qt::Key_Down)) {
1067              if (selection <(fCommandHistoryArea->childCount()-1))
1068                selection++;
1069            } else if (e->key() == (Qt::Key_PageDown)) {
1070              selection = fCommandHistoryArea->childCount()-1;
1071#else
1072        int selection = fCommandHistoryArea->currentRow();
1073        if (fCommandHistoryArea->count()) {
1074          if (selection == -1) {
1075            selection = fCommandHistoryArea->count()-1;
1076          } else {
1077            if (e->key() == (Qt::Key_Down)) {
1078              if (selection <(fCommandHistoryArea->count()-1))
1079                selection++;
1080            } else if (e->key() == (Qt::Key_PageDown)) {
1081              selection = fCommandHistoryArea->count()-1;
1082#endif
1083            } else if (e->key() == (Qt::Key_Up)) {
1084              if (selection >0)
1085                selection --;
1086            } else if (e->key() == (Qt::Key_PageUp)) {
1087              selection = 0;
1088            }
1089          }
1090          fCommandHistoryArea->clearSelection();
1091#if QT_VERSION < 0x040000
1092          QListViewItem* tmpItem = fCommandHistoryArea->firstChild();
1093          int index = 0;
1094          while (tmpItem != 0) {
1095            if (index == selection) {
1096              tmpItem->setSelected(true);
1097              fCommandHistoryArea->setCurrentItem(tmpItem);
1098          }
1099          index ++;
1100          tmpItem = tmpItem->nextSibling();
1101        }
1102#else
1103#if QT_VERSION < 0x040202
1104          fCommandHistoryArea->setItemSelected(fCommandHistoryArea->item(selection),true);
1105#else
1106          fCommandHistoryArea->item(selection)->setSelected(true);
1107#endif     
1108          fCommandHistoryArea->setCurrentItem(fCommandHistoryArea->item(selection));
1109#endif
1110        }
1111      } else if (e->key() == (Qt::Key_Tab)) {
1112#if QT_VERSION < 0x040000
1113        G4String ss = Complete(fCommandArea->text().ascii());
1114#else
1115        G4String ss = Complete(fCommandArea->text().toStdString().c_str());
1116#endif
1117        fCommandArea->setText((char*)(ss.data()));
1118
1119        // do not pass by parent, it will disable widget tab focus !
1120        return true;
1121      }
1122    }
1123  }
1124  // pass the event on to the parent class
1125  return QObject::eventFilter(aObj, aEvent);
1126}
1127
1128
1129
1130
1131/***************************************************************************/
1132//
1133//             SLOTS DEFINITIONS
1134//
1135/***************************************************************************/
1136
1137/**   Called when user give "help" command.
1138*/
1139void G4UIQt::ShowHelpCallback (
1140)
1141{
1142  TerminalHelp("");
1143}
1144
1145
1146/**   Called when user click on clear button. Clear the text Output area
1147*/
1148void G4UIQt::ClearButtonCallback (
1149)
1150{
1151  fTextArea->clear();
1152}
1153
1154/**   Called when user exit session
1155*/
1156void G4UIQt::ExitSession (
1157)
1158{
1159  SessionTerminate();
1160}
1161
1162
1163/**   Callback call when "click on a menu entry.<br>
1164   Send the associated command to geant4
1165*/
1166void G4UIQt::CommandEnteredCallback (
1167)
1168{
1169#if QT_VERSION < 0x040000
1170  G4String command (fCommandArea->text().ascii());
1171  if (fCommandArea->text().simplifyWhiteSpace() != "") {
1172
1173    QListViewItem *newItem = new QListViewItem(fCommandHistoryArea);
1174    newItem->setText(0,fCommandArea->text());
1175    fCommandHistoryArea->insertItem(newItem);
1176    // now we have to arrange
1177    QListViewItem *temp= fCommandHistoryArea->lastItem();
1178    for (int i=0; i<fCommandHistoryArea->childCount()-1;i++) {
1179      fCommandHistoryArea->takeItem(temp);
1180      fCommandHistoryArea->insertItem(temp);
1181      temp= fCommandHistoryArea->lastItem();
1182    }
1183#else
1184  G4String command (fCommandArea->text().toStdString().c_str());
1185  if (fCommandArea->text().trimmed() != "") {
1186    fCommandHistoryArea->addItem(fCommandArea->text());
1187#endif
1188    fCommandHistoryArea->clearSelection();
1189    fCommandHistoryArea->setCurrentItem(NULL);
1190    fCommandArea->setText("");
1191
1192    G4Qt* interactorManager = G4Qt::getInstance ();
1193    if (interactorManager) {
1194      interactorManager->FlushAndWaitExecution();
1195    }
1196    if (command(0,4) != "help") {
1197      ApplyShellCommand (command,exitSession,exitPause);
1198    } else {
1199      TerminalHelp(command);
1200    }
1201    if(exitSession==true)
1202      SessionTerminate();
1203  }
1204}
1205
1206
1207/**   Callback call when "enter" clicked on the command zone.<br>
1208   Send the command to geant4
1209   @param aCommand
1210*/
1211void G4UIQt::ButtonCallback (
1212 const QString& aCommand
1213)
1214{
1215
1216#if QT_VERSION < 0x040000
1217  G4String ss = G4String(aCommand.ascii());
1218#else
1219  G4String ss = G4String(aCommand.toStdString().c_str());
1220#endif
1221  ApplyShellCommand(ss,exitSession,exitPause);
1222  if(exitSession==true)
1223    SessionTerminate();
1224}
1225
1226
1227
1228/**   This callback is activated when user selected a item in the help tree
1229*/
1230void G4UIQt::HelpTreeClicCallback (
1231)
1232{
1233#if QT_VERSION < 0x040000
1234  QListViewItem* item =  NULL;
1235#else
1236  QTreeWidgetItem* item =  NULL;
1237#endif
1238  if (!fHelpTreeWidget)
1239    return ;
1240
1241  if (!fHelpArea)
1242    return;
1243 
1244#if QT_VERSION < 0x040000
1245  item =fHelpTreeWidget->selectedItem();
1246#else
1247  QList<QTreeWidgetItem *> list =fHelpTreeWidget->selectedItems();
1248  if (list.isEmpty())
1249    return;
1250  item = list.first();
1251#endif
1252  if (!item)
1253    return;
1254 
1255  G4UImanager* UI = G4UImanager::GetUIpointer();
1256  if(UI==NULL) return;
1257  G4UIcommandTree * treeTop = UI->GetTree();
1258#if QT_VERSION < 0x040000
1259  G4UIcommand* command = treeTop->FindPath(item->text (1).ascii());
1260#else
1261  G4UIcommand* command = treeTop->FindPath(item->text (1).toStdString().c_str());
1262#endif
1263  if (command) {
1264#if QT_VERSION >= 0x040000
1265#if QT_VERSION < 0x040200
1266    fHelpArea->clear();
1267    fHelpArea->append(GetCommandList(command));
1268#else
1269    fHelpArea->setText(GetCommandList(command));
1270#endif
1271#else
1272    fHelpArea->setText(GetCommandList(command));
1273#endif
1274  } else {
1275    // this is not a command, this is a sub directory
1276    // We display the Title
1277#if QT_VERSION >= 0x040000
1278#if QT_VERSION < 0x040200
1279    fHelpArea->clear();
1280    fHelpArea->append(item->text (1));
1281#else
1282    fHelpArea->setText(item->text (1));
1283#endif
1284#else
1285    fHelpArea->setText(item->text (1));
1286#endif
1287  }
1288}
1289
1290/**   This callback is activated when user double clic on a item in the help tree
1291*/
1292void G4UIQt::HelpTreeDoubleClicCallback (
1293#if QT_VERSION < 0x040000
1294QListViewItem* item
1295#else
1296QTreeWidgetItem* item
1297,int
1298#endif
1299)
1300{
1301  HelpTreeClicCallback();
1302  fCommandArea->setText(item->text (1));
1303}
1304
1305
1306/**   Callback called when user select an old command in the command history<br>
1307   Give it to the command area.
1308*/
1309void G4UIQt::CommandHistoryCallback(
1310)
1311{
1312#if QT_VERSION < 0x040000
1313  QListViewItem* item =  NULL;
1314#else
1315  QListWidgetItem* item =  NULL;
1316#endif
1317  if (!fCommandHistoryArea)
1318    return ;
1319 
1320#if QT_VERSION < 0x040000
1321  item =fCommandHistoryArea->selectedItem();
1322#else
1323  QList<QListWidgetItem *> list =fCommandHistoryArea->selectedItems();
1324  if (list.isEmpty())
1325    return;
1326  item = list.first();
1327#endif
1328  if (!item)
1329    return;
1330#if QT_VERSION < 0x040000
1331  fCommandArea->setText(item->text(0));
1332#else
1333  fCommandArea->setText(item->text());
1334#endif
1335
1336}
1337
1338#endif
Note: See TracBrowser for help on using the repository browser.