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

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

r616@mac-90108: laurentgarnier | 2007-06-13 17:38:01 +0200
debut de l aide sur une nouvealle fenetre

  • Property svn:mime-type set to text/cpp
File size: 19.5 KB
Line 
1// TODO !
2
3//
4// ********************************************************************
5// * License and Disclaimer                                           *
6// *                                                                  *
7// * The  Geant4 software  is  copyright of the Copyright Holders  of *
8// * the Geant4 Collaboration.  It is provided  under  the terms  and *
9// * conditions of the Geant4 Software License,  included in the file *
10// * LICENSE and available at  http://cern.ch/geant4/license .  These *
11// * include a list of copyright holders.                             *
12// *                                                                  *
13// * Neither the authors of this software system, nor their employing *
14// * institutes,nor the agencies providing financial support for this *
15// * work  make  any representation or  warranty, express or implied, *
16// * regarding  this  software system or assume any liability for its *
17// * use.  Please see the license in the file  LICENSE  and URL above *
18// * for the full disclaimer and the limitation of liability.         *
19// *                                                                  *
20// * This  code  implementation is the result of  the  scientific and *
21// * technical work of the GEANT4 collaboration.                      *
22// * By using,  copying,  modifying or  distributing the software (or *
23// * any work based  on the software)  you  agree  to acknowledge its *
24// * use  in  resulting  scientific  publications,  and indicate your *
25// * acceptance of all terms of the Geant4 Software license.          *
26// ********************************************************************
27//
28//
29// $Id: G4UIQt.cc,v 1.14 2007/05/29 11:09:49 $
30// GEANT4 tag $Name: geant4-08-01 $
31//
32// L. Garnier
33
34//#define DEBUG
35
36#ifdef G4UI_BUILD_QT_SESSION
37
38#include "G4Types.hh"
39
40#include <string.h>
41
42#include "G4UIQt.hh"
43#include "G4UImanager.hh"
44#include "G4StateManager.hh"
45#include "G4UIcommandTree.hh"
46#include "G4UIcommandStatus.hh"
47
48#include "G4Qt.hh"
49
50#include <QtGui/qapplication.h>
51#include <QtGui/qwidget.h>
52#include <QtGui/qmenu.h>
53#include <QtGui/qmenubar.h>
54#include <QtGui/qboxlayout.h>
55#include <QtGui/qpushbutton.h>
56#include <QtGui/qlabel.h>
57#include <QtGui/qsplitter.h>
58#include <QtGui/qscrollbar.h>
59#include <QtGui/qtreewidget.h>
60#include <QtGui/qdialog.h>
61
62#include <stdlib.h>
63
64static G4bool ConvertStringToInt(const char*,int&);
65
66static G4bool exitSession = true;
67static G4bool exitPause = true;
68static G4bool exitHelp = true;
69/***************************************************************************/
70/**
71 Build a Qt window with a menubar, output area and promt area
72      +-----------------------+
73      |exit menu|             |
74      |                       |
75      | +-------------------+ |
76      | |                   | |
77      | |  Output area      | |
78      | |                   | |
79      | +-------------------+ |
80      |     | clear |         |
81      | +-------------------+ |
82      | |  promt history    | |
83      | +-------------------+ |
84      | +-------------------+ |
85      | |> promt area       | |
86      | +-------------------+ |
87      +-----------------------+
88*/
89
90G4UIQt::G4UIQt (
91 int argc,
92 char** argv
93)
94/***************************************************************************/
95/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
96{
97  G4Qt* interactorManager = G4Qt::getInstance ();
98  G4UImanager* UI = G4UImanager::GetUIpointer();
99  if(UI!=NULL) UI->SetSession(this);
100
101  fMainWindow = new QMainWindow();
102  fMainWindow->setWindowTitle( "G4UI Session" );
103  fMainWindow->resize(800,600);
104  fMainWindow->move(QPoint(300,100));
105
106  QSplitter *splitter = new QSplitter(Qt::Vertical);
107  fTextArea = new QTextEdit();
108  QPushButton *clearButton = new QPushButton("clear");
109  connect(clearButton, SIGNAL(clicked()), SLOT(clearButtonCallback()));
110
111  fCommandHistoryArea = new QTextEdit();
112  fCommandLabel = new QLabel();
113
114  fCommandArea = new QLineEdit();
115  fCommandArea->activateWindow();
116  connect(fCommandArea, SIGNAL(returnPressed()), SLOT(commandEnteredCallback()));
117  fCommandArea->setFocusPolicy ( Qt::StrongFocus );
118  fCommandArea->setFocus(Qt::TabFocusReason);
119  fTextArea->setReadOnly(true);
120  fCommandHistoryArea->setReadOnly(true);
121
122 
123
124  // Set layouts
125  QVBoxLayout *layoutSplitter = new QVBoxLayout;
126
127  QWidget* topWidget = new QWidget();
128  QVBoxLayout *layoutTop = new QVBoxLayout;
129
130  QWidget* bottomWidget = new QWidget();
131  QVBoxLayout *layoutBottom = new QVBoxLayout;
132
133
134  layoutTop->addWidget(fTextArea);
135  layoutTop->addWidget(clearButton);
136  topWidget->setLayout(layoutTop);
137
138  layoutBottom->addWidget(fCommandHistoryArea);
139  layoutBottom->addWidget(fCommandLabel);
140  layoutBottom->addWidget(fCommandArea);
141  bottomWidget->setLayout(layoutBottom);
142
143
144  layoutSplitter->addWidget(topWidget);
145  layoutSplitter->addWidget(bottomWidget);
146  splitter->setLayout(layoutSplitter);
147
148  fMainWindow->setCentralWidget(splitter);
149
150  QMenu *fileMenu = fMainWindow->menuBar()->addMenu("File");
151  fileMenu->addAction("Quitter", fMainWindow, SLOT(close()));
152
153  // Set the splitter size. The fTextArea sould be 2/3 on the fMainWindow
154  QList<int> vals = splitter->sizes();
155  if(vals.size()==2) {
156    vals[0] = (splitter->orientation()==Qt::Vertical ? splitter->height() : splitter->width())*3/4;
157    vals[1] = (splitter->orientation()==Qt::Vertical ? splitter->height() : splitter->width())*1/4;
158    splitter->setSizes(vals);
159  }
160
161
162  if(UI!=NULL) UI->SetCoutDestination(this);  // TO KEEP
163}
164/***************************************************************************/
165G4UIQt::~G4UIQt(
166)
167/***************************************************************************/
168/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
169{
170  G4UImanager* UI = G4UImanager::GetUIpointer();  // TO KEEP
171  if(UI!=NULL) {  // TO KEEP
172    UI->SetSession(NULL);  // TO KEEP
173    UI->SetCoutDestination(NULL);  // TO KEEP
174  }
175
176 
177  if (fMainWindow!=NULL)
178    delete fMainWindow;
179}
180/***************************************************************************/
181/*
182    Start the Qt main loop
183 */
184G4UIsession* G4UIQt::SessionStart (
185)
186/***************************************************************************/
187/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
188{
189
190  G4Qt* interactorManager = G4Qt::getInstance ();
191  fMainWindow->show();
192  Prompt("session");
193  exitSession = false;
194
195
196  printf("disable secondary loop\n");
197  interactorManager->DisableSecondaryLoop (); // TO KEEP
198  ((QApplication*)interactorManager->GetMainInteractor())->exec();
199  // on ne passe pas le dessous ? FIXME ????
200  // je ne pense pas 13/06
201
202//   void* event; // TO KEEP
203//   while((event = interactorManager->GetEvent())!=NULL) {  // TO KEEP
204//     interactorManager->DispatchEvent(event); // TO KEEP
205//     if(exitSession==true) break; // TO KEEP
206//   } // TO KEEP
207
208  interactorManager->EnableSecondaryLoop ();
209  printf("enable secondary loop\n");
210  return this;
211}
212/***************************************************************************/
213
214/**
215  Display the prompt in the prompt area
216 */
217void G4UIQt::Prompt (
218 G4String aPrompt
219)
220/***************************************************************************/
221/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
222{
223  fCommandLabel->setText((char*)aPrompt.data());
224}
225/***************************************************************************/
226void G4UIQt::SessionTerminate (
227)
228/***************************************************************************/
229/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
230{
231  G4Qt* interactorManager = G4Qt::getInstance ();
232  fMainWindow->close();
233  ((QApplication*)interactorManager->GetMainInteractor())->exit();
234}
235/***************************************************************************/
236void G4UIQt::PauseSessionStart (
237 G4String a_state
238)
239/***************************************************************************/
240/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
241{
242  printf("G4UIQt::PauseSessionStart\n");
243  if(a_state=="G4_pause> ") {  // TO KEEP
244    SecondaryLoop ("Pause, type continue to exit this state"); // TO KEEP
245  } // TO KEEP
246
247  if(a_state=="EndOfEvent") { // TO KEEP
248    // Picking with feed back in event data Done here !!!
249    SecondaryLoop ("End of event, type continue to exit this state"); // TO KEEP
250  } // TO KEEP
251}
252/***************************************************************************/
253void G4UIQt::SecondaryLoop (
254 G4String a_prompt
255)
256/***************************************************************************/
257/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
258{
259  printf("G4UIQt::SecondaryLoop\n");
260  G4Qt* interactorManager = G4Qt::getInstance (); // TO KEEP ?
261  Prompt(a_prompt); // TO KEEP
262  exitPause = false; // TO KEEP
263  void* event; // TO KEEP
264  while((event = interactorManager->GetEvent())!=NULL) {  // TO KEEP
265    interactorManager->DispatchEvent(event); // TO KEEP
266    if(exitPause==true) break; // TO KEEP
267  } // TO KEEP
268  Prompt("session"); // TO KEEP
269}
270/***************************************************************************/
271/**
272  Receive a cout from Geant4. We have to display it in the cout zone
273 */
274G4int G4UIQt::ReceiveG4cout (
275 G4String a_string
276)
277/***************************************************************************/
278/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
279{
280  fTextArea->append(QString((char*)a_string.data()).trimmed());
281  fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum());
282  return 0;
283}
284/***************************************************************************/
285/**
286  Receive a cerr from Geant4. We have to display it in the cout zone
287 */
288G4int G4UIQt::ReceiveG4cerr (
289 G4String a_string
290)
291/***************************************************************************/
292/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
293{
294  QColor previousColor = fTextArea->textColor();
295  fTextArea->setTextColor(Qt::red);
296  fTextArea->append(QString((char*)a_string.data()).trimmed());
297  fTextArea->setTextColor(previousColor);
298  fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum());
299  return 0;
300}
301/***************************************************************************/
302G4bool G4UIQt::GetHelpChoice(
303 G4int& aInt
304)
305/***************************************************************************/
306/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
307{
308  printf("G4UIQt::GetHelpChoice\n");
309 
310  fHelp = true; // TO KEEP
311//   // SecondaryLoop : // TO KEEP
312  G4Qt* interactorManager = G4Qt::getInstance (); // TO KEEP ?
313  Prompt("Help"); // TO KEEP
314  exitHelp = false; // TO KEEP
315  void* event; // TO KEEP
316  while((event = interactorManager->GetEvent())!=NULL) {  // TO KEEP
317    interactorManager->DispatchEvent(event); // TO KEEP
318    if(exitHelp==true) break; // TO KEEP
319  } // TO KEEP
320  Prompt("session"); // TO KEEP
321  // // TO KEEP
322  if(fHelp==false) return false; // TO KEEP
323  aInt = fHelpChoice; // TO KEEP
324  fHelp = false; // TO KEEP
325  return true; // TO KEEP
326}
327/***************************************************************************/
328void G4UIQt::ExitHelp(
329)
330/***************************************************************************/
331/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
332{
333  printf("G4UIQt::ExitHelp\n");
334}
335
336/***************************************************************************/
337void G4UIQt::AddMenu (
338 const char* a_name
339,const char* a_label
340)
341/***************************************************************************/
342/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
343{
344  printf("G4UIQt::AddMenu %s %s\n",a_name,a_label);
345
346  QMenu *fileMenu = fMainWindow->menuBar()->addMenu(a_label);
347  AddInteractor (a_name,(G4Interactor)fileMenu);
348
349  //  QMenu *menu = new QMenu("test");//a_label);
350  //  fMainWindow->menuBar()->addMenu(menu);
351
352//   if(menuBar==NULL) return;
353//   if(a_name==NULL) return;
354//   if(a_label==NULL) return;
355//   XtManageChild (menuBar);
356//   // Pulldown menu :
357//   Widget widget;
358//   widget = XmCreatePulldownMenu (menuBar,(char*)a_name,NULL,0);
359//   AddInteractor (a_name,(G4Interactor)widget);
360//   // Cascade button :
361//   Arg args[2];
362//   XmString cps = XmStringLtoRCreate((char*)a_label,XmSTRING_DEFAULT_CHARSET);
363//   XtSetArg (args[0],XmNlabelString,cps);
364//   XtSetArg (args[1],XmNsubMenuId,widget);
365//   widget = XmCreateCascadeButton (menuBar,(char*)a_name,args,2);
366//   XmStringFree (cps);
367//   XtManageChild (widget);
368//   ExecuteChangeSizeFunction(form);
369}
370/***************************************************************************/
371void G4UIQt::AddButton (
372 const char* a_menu
373,const char* a_label
374,const char* a_command
375)
376/***************************************************************************/
377/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
378{
379  if(a_menu==NULL) return; // TO KEEP
380  if(a_label==NULL) return; // TO KEEP
381  if(a_command==NULL) return; // TO KEEP
382  QMenu *parent = (QMenu*)GetInteractor(a_menu);
383  if(parent==NULL) return;
384 
385  signalMapper = new QSignalMapper(this);
386  QAction *action = parent->addAction(a_label, signalMapper, SLOT(map()));
387  signalMapper->setMapping(action, QString(a_command));
388  connect(signalMapper, SIGNAL(mapped(const QString &)),this, SLOT(buttonCallback(const QString&)));
389 
390  //  std::string slot = SLOT(buttonCallback(std::string));   
391  printf("G4UIQt::AddButton %s %s %s\n",a_menu,a_label,a_command);
392
393//   Widget widget = XmCreatePushButton(parent,(char*)a_label,NULL,0);
394//   XtManageChild (widget);
395//   XtAddCallback (widget,XmNactivateCallback,ButtonCallback,(XtPointer)this);
396//   commands[action] = a_command;
397}
398
399
400// /***************************************************************************/
401//G4String G4UIQt::GetCommand (
402//  QAction *a_widget
403//)
404// /***************************************************************************/
405// /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
406// {
407//   return commands[a_widget];
408// }
409/***************************************************************************/
410/***************************************************************************/
411/***************************************************************************/
412
413/**
414  Callback call when "enter" clicked on the command zone.
415  Send the command to geant4
416 */
417void G4UIQt::buttonCallback (
418  const QString& a_command
419)
420/***************************************************************************/
421/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
422{
423  if(fHelp==true) return; // Disabled when in help.
424  G4String ss = G4String(a_command.toStdString().c_str());
425  printf ("debug : execute:\n-%s- %d %d \n",ss.data(),exitSession,exitPause);
426  ApplyShellCommand(ss,exitSession,exitPause);
427  if(exitSession==true)
428    SessionTerminate();
429}
430
431/**
432  Callback call when "click on a menu entry.
433  Send the associated command to geant4
434 */
435void G4UIQt::commandEnteredCallback (
436)
437/***************************************************************************/
438/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
439{
440  printf ("debug : callback\n");
441  G4String command (fCommandArea->text().toStdString().c_str());
442  if (fCommandArea->text().toStdString().c_str() != "") {
443    fCommandHistoryArea->append(fCommandArea->text());
444    if(fHelp==true) {
445  printf ("ne doit plus passer ici\n");
446      exitHelp = true;
447      fHelp = ConvertStringToInt(command.data(),fHelpChoice);
448    } else {
449      if (command(0,4) != "help") {
450        ApplyShellCommand (command,exitSession,exitPause);
451      } else {
452        printf ("terminal help\n");
453        TerminalHelp(command);
454      }
455      if(exitSession==true)
456        SessionTerminate();
457    }
458  }
459  fCommandArea->setText("");
460}
461
462
463/***************************************************************************/
464void G4UIQt::clearButtonCallback (
465)
466/***************************************************************************/
467/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
468{
469  fTextArea->clear();
470}
471
472//////////////////////////////////////////////////////////////////////////////
473G4bool ConvertStringToInt(
474 const char* aString
475,int& aInt
476)
477//////////////////////////////////////////////////////////////////////////////
478//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
479{
480  aInt = 0; // TO KEEP
481  if(aString==NULL) return false; // TO KEEP
482  char* s; // TO KEEP
483  long value = strtol(aString,&s,10); // TO KEEP
484  if(s==aString) return false; // TO KEEP
485  aInt = value; // TO KEEP
486  return true; // TO KEEP
487}
488
489#endif
490
491void G4UIQt::TerminalHelp(G4String newCommand)
492{
493  QDialog *helpDialog = new QDialog;
494
495  QSplitter *splitter = new QSplitter(Qt::Horizontal);
496  QTextEdit *textArea = new QTextEdit();
497  QPushButton *exitButton = new QPushButton("Exit");
498  QTreeWidget *treeWidget = new QTreeWidget();
499  connect(exitButton, SIGNAL(clicked()), helpDialog,SLOT(close()));
500
501  textArea->setReadOnly(true);
502
503  // Set layouts
504  QHBoxLayout *splitterLayout = new QHBoxLayout;
505
506  QWidget* topWidget = new QWidget();
507  QVBoxLayout *vLayout = new QVBoxLayout;
508
509  splitterLayout->addWidget(treeWidget);
510  splitterLayout->addWidget(textArea);
511  splitter->setLayout(splitterLayout);
512
513  vLayout->addWidget(splitter);
514  vLayout->addWidget(exitButton);
515  helpDialog->setLayout(vLayout);
516
517
518  helpDialog->show();
519  helpDialog->raise();
520  helpDialog->activateWindow();
521  ////////////////
522
523  printf ("G4UIQt::TerminalHelp \n");
524//   G4UImanager* UI = G4UImanager::GetUIpointer();
525//   if(UI==NULL) return;
526//   G4UIcommandTree * treeTop = UI->GetTree();
527//   size_t i = newCommand.index(" ");
528//   if( i != std::string::npos )
529//   {
530//     G4String newValue = newCommand(i+1,newCommand.length()-(i+1));
531//     newValue.strip(G4String::both);
532//     G4String targetCom = ModifyToFullPathCommand( newValue );
533//     G4UIcommand* theCommand = treeTop->FindPath( targetCom );
534//     if( theCommand != NULL )
535//     {
536//       theCommand->List();
537//       return;
538//     }
539//     else
540//     {
541//       G4cout << "Command <" << newValue << " is not found." << G4endl;
542//       return;
543//     }
544//   }
545
546//   G4UIcommandTree * floor[10];
547//   floor[0] = treeTop;
548//   G4int iFloor = 0;
549//   size_t prefixIndex = 1;
550//   G4String prefix = GetCurrentWorkingDirectory();
551//   while( prefixIndex < prefix.length()-1 )
552//   {
553//     size_t ii = prefix.index("/",prefixIndex);
554//     floor[iFloor+1] =
555//       floor[iFloor]->GetTree(G4String(prefix(0,ii+1)));
556//     prefixIndex = ii+1;
557//     iFloor++;
558//   }
559//   floor[iFloor]->ListCurrentWithNum();
560//   // 1998 Oct 2 non-number input
561//   while(1){
562//    //G4cout << G4endl << "Type the number ( 0:end, -n:n level back ) : "<<std::flush;
563//     G4cout << G4endl << "Type the number ( 0:end, -n:n level back ) : "<<G4endl;
564//     G4int i;
565//     if(!GetHelpChoice(i)){
566//       G4cout << G4endl << "Not a number, once more" << G4endl;
567//       continue;
568//     } else if( i < 0 ){
569//       iFloor += i;
570//       if( iFloor < 0 ) iFloor = 0;
571//       floor[iFloor]->ListCurrentWithNum();
572//       continue;
573//     } else if(i == 0) {
574//       break;
575//     } else if( i > 0 ) {
576//       G4int n_tree = floor[iFloor]->GetTreeEntry();
577//       if( i > n_tree )
578//       {
579//         if( i <= n_tree + floor[iFloor]->GetCommandEntry() )
580//         {
581//           floor[iFloor]->GetCommand(i-n_tree)->List();
582//         }
583//       }
584//       else
585//       {
586//         floor[iFloor+1] = floor[iFloor]->GetTree(i);
587//         iFloor++;
588//         floor[iFloor]->ListCurrentWithNum();
589//       }
590//     }
591//   }
592  G4cout << "Exit from HELP." << G4endl << G4endl;
593  //G4cout << G4endl;
594  ExitHelp();
595}
596
597
598
599
600
601
602
603
604
605
606
607
Note: See TracBrowser for help on using the repository browser.