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

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

r567@mac-90108: laurentgarnier | 2007-05-30 15:55:34 +0200
G4Qt

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