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

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

r587@mac-90108: laurentgarnier | 2007-06-07 17:22:03 +0200
on avance

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