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

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

debut de correction du ticket #98

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