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

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

Correction du ticket #99 et #85

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