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

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

r610@mac-90108: laurentgarnier | 2007-06-13 10:50:13 +0200
les menus sont ok, reste le exit qui n agit pas

  • Property svn:mime-type set to text/cpp
File size: 18.8 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
60#include <stdlib.h>
61
62static G4bool ConvertStringToInt(const char*,int&);
63
64static G4bool exitSession = true;
65static G4bool exitPause = true;
66static G4bool exitHelp = true;
67/***************************************************************************/
68/**
69 Build a Qt window with a menubar, output area and promt area
70      +-----------------------+
71      |exit menu|             |
72      |                       |
73      | +-------------------+ |
74      | |                   | |
75      | |  Output area      | |
76      | |                   | |
77      | +-------------------+ |
78      |     | clear |         |
79      | +-------------------+ |
80      | |  promt history    | |
81      | +-------------------+ |
82      | +-------------------+ |
83      | |> promt area       | |
84      | +-------------------+ |
85      +-----------------------+
86*/
87
88G4UIQt::G4UIQt (
89 int argc,
90 char** argv
91)
92/***************************************************************************/
93/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
94{
95  G4Qt* interactorManager = G4Qt::getInstance ();
96  G4UImanager* UI = G4UImanager::GetUIpointer(); // TO KEEP
97  if(UI!=NULL) UI->SetSession(this); // TO KEEP
98
99  fMainWindow = new QMainWindow();
100  fMainWindow->setWindowTitle( "G4UI Session" );
101  fMainWindow->resize(800,600);
102
103  QSplitter *splitter = new QSplitter(Qt::Vertical);
104  fTextArea = new QTextEdit();
105  QPushButton *clearButton = new QPushButton("clear");
106  connect(clearButton, SIGNAL(clicked()), SLOT(clearButtonCallback()));
107
108  fCommandHistoryArea = new QTextEdit();
109  fCommandLabel = new QLabel();
110
111  fCommandArea = new QLineEdit();
112  fCommandArea->activateWindow();
113  connect(fCommandArea, SIGNAL(returnPressed()), SLOT(commandEnteredCallback()));
114  fCommandArea->setFocusPolicy ( Qt::StrongFocus );
115  fCommandArea->setFocus(Qt::TabFocusReason);
116  fTextArea->setReadOnly(true);
117  fCommandHistoryArea->setReadOnly(true);
118
119 
120  QVBoxLayout *layoutSplitter = new QVBoxLayout;
121
122  QWidget* topWidget = new QWidget();
123  QVBoxLayout *layoutTop = new QVBoxLayout;
124
125  QWidget* bottomWidget = new QWidget();
126  QVBoxLayout *layoutBottom = new QVBoxLayout;
127
128
129  layoutTop->addWidget(fTextArea);
130  layoutTop->addWidget(clearButton);
131  topWidget->setLayout(layoutTop);
132
133  layoutBottom->addWidget(fCommandHistoryArea);
134  layoutBottom->addWidget(fCommandLabel);
135  layoutBottom->addWidget(fCommandArea);
136  bottomWidget->setLayout(layoutBottom);
137
138
139  layoutSplitter->addWidget(topWidget);
140  layoutSplitter->addWidget(bottomWidget);
141  splitter->setLayout(layoutSplitter);
142
143  fMainWindow->setCentralWidget(splitter);
144
145  QMenu *fileMenu = fMainWindow->menuBar()->addMenu("File");
146  fileMenu->addAction("Quitter", fMainWindow, SLOT(close()));
147
148//  QMainWindow top = (QMainWindow)interactorManager->GetMainInteractor();
149
150//   if(getenv("XENVIRONMENT")==NULL) {
151//     XrmDatabase database = XrmGetDatabase(XtDisplay(top));
152//     if(database!=NULL) {
153//       XrmPutLineResource(&database,"*topShadowColor:white");
154//       XrmPutLineResource(&database,"*bottomShadowColor:black");
155//       XrmPutLineResource(&database,"*foreground:black");
156//       XrmPutLineResource(&database,"*background:lightgrey");
157//       XrmPutLineResource(&database,"*borderColor:lightgrey");
158//       XrmPutLineResource(&database,"*fontList:-*-helvetica-bold-r-*-*-*-120-*-*-*-*-iso8859-1");
159//       XrmPutLineResource(&database,"*text.background:white");
160//       XrmPutLineResource(&database,"*text.fontList:*courier*-r-*--14-*");
161//       XrmPutLineResource(&database,"*text.maxLength:8000");
162//     }
163//   }
164
165//   Arg args[9];
166//   XtSetArg(args[0],XmNkeyboardFocusPolicy,XmPOINTER); // For completion.
167//   shell = XtAppCreateShell ("G4UIXm","G4UIXm",
168//                          topLevelShellWidgetClass,XtDisplay(top),
169//                          args,1);
170//   form = XmCreateForm (shell,(char*)"form",NULL,0);
171//   XtManageChild (form);
172
173//   XtSetArg(args[0],XmNtopAttachment   ,XmATTACH_FORM);
174//   XtSetArg(args[1],XmNleftAttachment  ,XmATTACH_FORM);
175//   XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
176//   menuBar = XmCreateMenuBar (form,(char*)"menuBar",args,3);
177
178//   XtSetArg(args[0],XmNtopAttachment      ,XmATTACH_NONE);
179//   XtSetArg(args[1],XmNleftAttachment     ,XmATTACH_FORM);
180//   XtSetArg(args[2],XmNrightAttachment    ,XmATTACH_FORM);
181//   XtSetArg(args[3],XmNbottomAttachment   ,XmATTACH_FORM);
182//   command = XmCreateCommand (form,(char*)"command",args,4);
183//   XtManageChild (command);
184
185//   XtSetArg(args[0],XmNtopAttachment   ,XmATTACH_NONE);
186//   XtSetArg(args[1],XmNleftAttachment  ,XmATTACH_FORM);
187//   XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
188//   XtSetArg(args[3],XmNbottomAttachment,XmATTACH_WIDGET);
189//   XtSetArg(args[4],XmNbottomWidget    ,command);
190//   XmString cps = XmStringLtoRCreate((char*)"Clear",XmSTRING_DEFAULT_CHARSET);
191//   XtSetArg (args[5],XmNlabelString,cps);
192//   Widget clearButton = XmCreatePushButton(form,(char*)"clearButton",args,6);
193//   XmStringFree (cps);
194//   XtManageChild (clearButton);
195
196//   XtSetArg(args[0],XmNtopAttachment   ,XmATTACH_WIDGET);
197//   XtSetArg(args[1],XmNtopWidget       ,menuBar);
198//   XtSetArg(args[2],XmNleftAttachment  ,XmATTACH_FORM);
199//   XtSetArg(args[3],XmNrightAttachment ,XmATTACH_FORM);
200//   XtSetArg(args[4],XmNbottomAttachment,XmATTACH_WIDGET);
201//   XtSetArg(args[5],XmNbottomWidget    ,clearButton);
202//   XtSetArg(args[6],XmNeditMode        ,XmMULTI_LINE_EDIT);
203//   XtSetArg(args[7],XmNrows            ,12);
204//   XtSetArg(args[8],XmNcolumns         ,80);
205//   text = XmCreateSGcrolledText (form,(char*)"text",args,9);
206//   XtManageChild (text);
207
208//   XtAddCallback(clearButton,XmNactivateCallback,
209//              clearButtonCallback,(XtPointer)text);
210//   XtAddCallback(command,XmNcommandEnteredCallback,
211//              commandEnteredCallback,(XtPointer)this);
212
213//   Widget commandText = XmCommandGetChild(command,XmDIALOG_COMMAND_TEXT);
214//   XtAddEventHandler(commandText,KeyPressMask,False,keyHandler,(XtPointer)this);
215
216//   XtRealizeWidget(shell);
217//   XtMapWidget(shell);
218
219  if(UI!=NULL) UI->SetCoutDestination(this);  // TO KEEP
220}
221/***************************************************************************/
222G4UIQt::~G4UIQt(
223)
224/***************************************************************************/
225/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
226{
227  G4UImanager* UI = G4UImanager::GetUIpointer();  // TO KEEP
228  if(UI!=NULL) {  // TO KEEP
229    UI->SetSession(NULL);  // TO KEEP
230    UI->SetCoutDestination(NULL);  // TO KEEP
231  }
232
233 
234  if (fMainWindow!=NULL)
235    delete fMainWindow;
236}
237/***************************************************************************/
238/*
239    Start the Qt main loop
240 */
241G4UIsession* G4UIQt::SessionStart (
242)
243/***************************************************************************/
244/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
245{
246
247  G4Qt* interactorManager = G4Qt::getInstance ();
248  fMainWindow->show();
249  Prompt("session");
250  exitSession = false;
251
252
253  printf("disable secondary loop\n");
254  interactorManager->DisableSecondaryLoop (); // TO KEEP
255  ((QApplication*)interactorManager->GetMainInteractor())->exec();
256  // on ne passe pas le dessous ? FIXME ????
257
258
259
260
261
262
263//   void* event; // TO KEEP
264//   while((event = interactorManager->GetEvent())!=NULL) {  // TO KEEP
265//     interactorManager->DispatchEvent(event); // TO KEEP
266//     if(exitSession==true) break; // TO KEEP
267//   } // TO KEEP
268   interactorManager->EnableSecondaryLoop (); // TO KEEP
269  printf("enable secondary loop\n");
270  return this; // TO KEEP
271}
272/***************************************************************************/
273
274/**
275  Display the prompt in the prompt area
276 */
277void G4UIQt::Prompt (
278 G4String aPrompt
279)
280/***************************************************************************/
281/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
282{
283  fCommandLabel->setText((char*)aPrompt.data());
284}
285/***************************************************************************/
286void G4UIQt::SessionTerminate (
287)
288/***************************************************************************/
289/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
290{
291}
292/***************************************************************************/
293void G4UIQt::PauseSessionStart (
294 G4String a_state
295)
296/***************************************************************************/
297/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
298{
299  printf("G4UIQt::PauseSessionStart\n");
300  if(a_state=="G4_pause> ") {  // TO KEEP
301    SecondaryLoop ("Pause, type continue to exit this state"); // TO KEEP
302  } // TO KEEP
303
304  if(a_state=="EndOfEvent") { // TO KEEP
305    // Picking with feed back in event data Done here !!!
306    SecondaryLoop ("End of event, type continue to exit this state"); // TO KEEP
307  } // TO KEEP
308}
309/***************************************************************************/
310void G4UIQt::SecondaryLoop (
311 G4String a_prompt
312)
313/***************************************************************************/
314/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
315{
316  printf("G4UIQt::SecondaryLoop\n");
317  G4Qt* interactorManager = G4Qt::getInstance (); // TO KEEP ?
318  Prompt(a_prompt); // TO KEEP
319  exitPause = false; // TO KEEP
320  void* event; // TO KEEP
321  while((event = interactorManager->GetEvent())!=NULL) {  // TO KEEP
322    interactorManager->DispatchEvent(event); // TO KEEP
323    if(exitPause==true) break; // TO KEEP
324  } // TO KEEP
325  Prompt("session"); // TO KEEP
326}
327/***************************************************************************/
328/**
329  Receive a cout from Geant4. We have to display it in the cout zone
330 */
331G4int G4UIQt::ReceiveG4cout (
332 G4String a_string
333)
334/***************************************************************************/
335/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
336{
337  fTextArea->append(QString((char*)a_string.data()).trimmed());
338  fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum());
339  return 0;
340}
341/***************************************************************************/
342/**
343  Receive a cerr from Geant4. We have to display it in the cout zone
344 */
345G4int G4UIQt::ReceiveG4cerr (
346 G4String a_string
347)
348/***************************************************************************/
349/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
350{
351  QColor previousColor = fTextArea->textColor();
352  fTextArea->setTextColor(Qt::red);
353  fTextArea->append(QString((char*)a_string.data()).trimmed());
354  fTextArea->setTextColor(previousColor);
355  fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum());
356  return 0;
357}
358/***************************************************************************/
359G4bool G4UIQt::GetHelpChoice(
360 G4int& aInt
361)
362/***************************************************************************/
363/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
364{
365  printf("G4UIQt::GetHelpChoice\n");
366 
367  fHelp = true; // TO KEEP
368//   // SecondaryLoop : // TO KEEP
369  G4Qt* interactorManager = G4Qt::getInstance (); // TO KEEP ?
370  Prompt("Help"); // TO KEEP
371  exitHelp = false; // TO KEEP
372  void* event; // TO KEEP
373  while((event = interactorManager->GetEvent())!=NULL) {  // TO KEEP
374    interactorManager->DispatchEvent(event); // TO KEEP
375    if(exitHelp==true) break; // TO KEEP
376  } // TO KEEP
377  Prompt("session"); // TO KEEP
378  // // TO KEEP
379  if(fHelp==false) return false; // TO KEEP
380  aInt = fHelpChoice; // TO KEEP
381  fHelp = false; // TO KEEP
382  return true; // TO KEEP
383}
384/***************************************************************************/
385void G4UIQt::ExitHelp(
386)
387/***************************************************************************/
388/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
389{
390  printf("G4UIQt::ExitHelp\n");
391}
392
393/***************************************************************************/
394void G4UIQt::AddMenu (
395 const char* a_name
396,const char* a_label
397)
398/***************************************************************************/
399/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
400{
401  printf("G4UIQt::AddMenu %s %s\n",a_name,a_label);
402
403  QMenu *fileMenu = fMainWindow->menuBar()->addMenu(a_label);
404  AddInteractor (a_name,(G4Interactor)fileMenu);
405
406  //  QMenu *menu = new QMenu("test");//a_label);
407  //  fMainWindow->menuBar()->addMenu(menu);
408
409//   if(menuBar==NULL) return;
410//   if(a_name==NULL) return;
411//   if(a_label==NULL) return;
412//   XtManageChild (menuBar);
413//   // Pulldown menu :
414//   Widget widget;
415//   widget = XmCreatePulldownMenu (menuBar,(char*)a_name,NULL,0);
416//   AddInteractor (a_name,(G4Interactor)widget);
417//   // Cascade button :
418//   Arg args[2];
419//   XmString cps = XmStringLtoRCreate((char*)a_label,XmSTRING_DEFAULT_CHARSET);
420//   XtSetArg (args[0],XmNlabelString,cps);
421//   XtSetArg (args[1],XmNsubMenuId,widget);
422//   widget = XmCreateCascadeButton (menuBar,(char*)a_name,args,2);
423//   XmStringFree (cps);
424//   XtManageChild (widget);
425//   ExecuteChangeSizeFunction(form);
426}
427/***************************************************************************/
428void G4UIQt::AddButton (
429 const char* a_menu
430,const char* a_label
431,const char* a_command
432)
433/***************************************************************************/
434/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
435{
436  if(a_menu==NULL) return; // TO KEEP
437  if(a_label==NULL) return; // TO KEEP
438  if(a_command==NULL) return; // TO KEEP
439  QMenu *parent = (QMenu*)GetInteractor(a_menu);
440  if(parent==NULL) return;
441 
442  signalMapper = new QSignalMapper(this);
443  QAction *action = parent->addAction(a_label, signalMapper, SLOT(map()));
444  signalMapper->setMapping(action, QString(a_command));
445  connect(signalMapper, SIGNAL(mapped(const QString &)),this, SLOT(buttonCallback(const QString&)));
446 
447  //  std::string slot = SLOT(buttonCallback(std::string));   
448  printf("G4UIQt::AddButton %s %s %s\n",a_menu,a_label,a_command);
449
450//   Widget widget = XmCreatePushButton(parent,(char*)a_label,NULL,0);
451//   XtManageChild (widget);
452//   XtAddCallback (widget,XmNactivateCallback,ButtonCallback,(XtPointer)this);
453//   commands[action] = a_command;
454}
455
456
457// /***************************************************************************/
458//G4String G4UIQt::GetCommand (
459//  QAction *a_widget
460//)
461// /***************************************************************************/
462// /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
463// {
464//   return commands[a_widget];
465// }
466/***************************************************************************/
467/***************************************************************************/
468/***************************************************************************/
469
470/**
471  Callback call when "enter" clicked on the command zone.
472  Send the command to geant4
473 */
474void G4UIQt::buttonCallback (
475  const QString& a_command
476)
477/***************************************************************************/
478/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
479{
480  if(fHelp==true) return; // Disabled when in help.
481  G4String ss = G4String(a_command.toStdString().c_str());
482  printf ("debug : execute:\n%s\n",ss.data());
483  ApplyShellCommand(ss,exitSession,exitPause);
484}
485
486/**
487  Callback call when "click on a menu entry.
488  Send the associated command to geant4
489 */
490void G4UIQt::commandEnteredCallback (
491)
492/***************************************************************************/
493/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
494{
495  G4String command (fCommandArea->text().toStdString().c_str());
496  if (fCommandArea->text().toStdString().c_str() != "") {
497    fCommandHistoryArea->append(fCommandArea->text());
498    if(fHelp==true) {
499      exitHelp = true;
500      fHelp = ConvertStringToInt(command.data(),fHelpChoice);
501    } else {
502      ApplyShellCommand (command,exitSession,exitPause);
503    }
504  }
505  fCommandArea->setText("");}
506}
507
508
509/***************************************************************************/
510void G4UIQt::clearButtonCallback (
511)
512/***************************************************************************/
513/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
514{
515  fTextArea->clear();
516}
517
518//////////////////////////////////////////////////////////////////////////////
519G4bool ConvertStringToInt(
520 const char* aString
521,int& aInt
522)
523//////////////////////////////////////////////////////////////////////////////
524//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
525{
526  aInt = 0; // TO KEEP
527  if(aString==NULL) return false; // TO KEEP
528  char* s; // TO KEEP
529  long value = strtol(aString,&s,10); // TO KEEP
530  if(s==aString) return false; // TO KEEP
531  aInt = value; // TO KEEP
532  return true; // TO KEEP
533}
534
535#endif
Note: See TracBrowser for help on using the repository browser.