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

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

r611@mac-90108: laurentgarnier | 2007-06-13 12:27:43 +0200
resolution du ticket #55

  • Property svn:mime-type set to text/cpp
File size: 16.3 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();
97  if(UI!=NULL) UI->SetSession(this);
98
99  fMainWindow = new QMainWindow();
100  fMainWindow->setWindowTitle( "G4UI Session" );
101  fMainWindow->resize(800,600);
102  fMainWindow->move(QPoint(300,100));
103
104  QSplitter *splitter = new QSplitter(Qt::Vertical);
105  fTextArea = new QTextEdit();
106  QPushButton *clearButton = new QPushButton("clear");
107  connect(clearButton, SIGNAL(clicked()), SLOT(clearButtonCallback()));
108
109  fCommandHistoryArea = new QTextEdit();
110  fCommandLabel = new QLabel();
111
112  fCommandArea = new QLineEdit();
113  fCommandArea->activateWindow();
114  connect(fCommandArea, SIGNAL(returnPressed()), SLOT(commandEnteredCallback()));
115  fCommandArea->setFocusPolicy ( Qt::StrongFocus );
116  fCommandArea->setFocus(Qt::TabFocusReason);
117  fTextArea->setReadOnly(true);
118  fCommandHistoryArea->setReadOnly(true);
119
120 
121
122  // Set layouts
123  QVBoxLayout *layoutSplitter = new QVBoxLayout;
124
125  QWidget* topWidget = new QWidget();
126  QVBoxLayout *layoutTop = new QVBoxLayout;
127
128  QWidget* bottomWidget = new QWidget();
129  QVBoxLayout *layoutBottom = new QVBoxLayout;
130
131
132  layoutTop->addWidget(fTextArea);
133  layoutTop->addWidget(clearButton);
134  topWidget->setLayout(layoutTop);
135
136  layoutBottom->addWidget(fCommandHistoryArea);
137  layoutBottom->addWidget(fCommandLabel);
138  layoutBottom->addWidget(fCommandArea);
139  bottomWidget->setLayout(layoutBottom);
140
141
142  layoutSplitter->addWidget(topWidget);
143  layoutSplitter->addWidget(bottomWidget);
144  splitter->setLayout(layoutSplitter);
145
146  fMainWindow->setCentralWidget(splitter);
147
148  QMenu *fileMenu = fMainWindow->menuBar()->addMenu("File");
149  fileMenu->addAction("Quitter", fMainWindow, SLOT(close()));
150
151  // Set the splitter size. The fTextArea sould be 2/3 on the fMainWindow
152  QList<int> vals = splitter->sizes();
153  if(vals.size()==2) {
154    vals[0] = (splitter->orientation()==Qt::Vertical ? splitter->height() : splitter->width())*3/4;
155    vals[1] = (splitter->orientation()==Qt::Vertical ? splitter->height() : splitter->width())*1/4;
156    splitter->setSizes(vals);
157  }
158
159
160  if(UI!=NULL) UI->SetCoutDestination(this);  // TO KEEP
161}
162/***************************************************************************/
163G4UIQt::~G4UIQt(
164)
165/***************************************************************************/
166/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
167{
168  G4UImanager* UI = G4UImanager::GetUIpointer();  // TO KEEP
169  if(UI!=NULL) {  // TO KEEP
170    UI->SetSession(NULL);  // TO KEEP
171    UI->SetCoutDestination(NULL);  // TO KEEP
172  }
173
174 
175  if (fMainWindow!=NULL)
176    delete fMainWindow;
177}
178/***************************************************************************/
179/*
180    Start the Qt main loop
181 */
182G4UIsession* G4UIQt::SessionStart (
183)
184/***************************************************************************/
185/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
186{
187
188  G4Qt* interactorManager = G4Qt::getInstance ();
189  fMainWindow->show();
190  Prompt("session");
191  exitSession = false;
192
193
194  printf("disable secondary loop\n");
195  interactorManager->DisableSecondaryLoop (); // TO KEEP
196  ((QApplication*)interactorManager->GetMainInteractor())->exec();
197  // on ne passe pas le dessous ? FIXME ????
198
199
200
201
202
203
204//   void* event; // TO KEEP
205//   while((event = interactorManager->GetEvent())!=NULL) {  // TO KEEP
206//     interactorManager->DispatchEvent(event); // TO KEEP
207//     if(exitSession==true) break; // TO KEEP
208//   } // TO KEEP
209   interactorManager->EnableSecondaryLoop (); // TO KEEP
210  printf("enable secondary loop\n");
211  return this; // TO KEEP
212}
213/***************************************************************************/
214
215/**
216  Display the prompt in the prompt area
217 */
218void G4UIQt::Prompt (
219 G4String aPrompt
220)
221/***************************************************************************/
222/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
223{
224  fCommandLabel->setText((char*)aPrompt.data());
225}
226/***************************************************************************/
227void G4UIQt::SessionTerminate (
228)
229/***************************************************************************/
230/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
231{
232  G4Qt* interactorManager = G4Qt::getInstance ();
233  fMainWindow->close();
234  ((QApplication*)interactorManager->GetMainInteractor())->exit();
235}
236/***************************************************************************/
237void G4UIQt::PauseSessionStart (
238 G4String a_state
239)
240/***************************************************************************/
241/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
242{
243  printf("G4UIQt::PauseSessionStart\n");
244  if(a_state=="G4_pause> ") {  // TO KEEP
245    SecondaryLoop ("Pause, type continue to exit this state"); // TO KEEP
246  } // TO KEEP
247
248  if(a_state=="EndOfEvent") { // TO KEEP
249    // Picking with feed back in event data Done here !!!
250    SecondaryLoop ("End of event, type continue to exit this state"); // TO KEEP
251  } // TO KEEP
252}
253/***************************************************************************/
254void G4UIQt::SecondaryLoop (
255 G4String a_prompt
256)
257/***************************************************************************/
258/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
259{
260  printf("G4UIQt::SecondaryLoop\n");
261  G4Qt* interactorManager = G4Qt::getInstance (); // TO KEEP ?
262  Prompt(a_prompt); // TO KEEP
263  exitPause = false; // TO KEEP
264  void* event; // TO KEEP
265  while((event = interactorManager->GetEvent())!=NULL) {  // TO KEEP
266    interactorManager->DispatchEvent(event); // TO KEEP
267    if(exitPause==true) break; // TO KEEP
268  } // TO KEEP
269  Prompt("session"); // TO KEEP
270}
271/***************************************************************************/
272/**
273  Receive a cout from Geant4. We have to display it in the cout zone
274 */
275G4int G4UIQt::ReceiveG4cout (
276 G4String a_string
277)
278/***************************************************************************/
279/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
280{
281  fTextArea->append(QString((char*)a_string.data()).trimmed());
282  fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum());
283  return 0;
284}
285/***************************************************************************/
286/**
287  Receive a cerr from Geant4. We have to display it in the cout zone
288 */
289G4int G4UIQt::ReceiveG4cerr (
290 G4String a_string
291)
292/***************************************************************************/
293/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
294{
295  QColor previousColor = fTextArea->textColor();
296  fTextArea->setTextColor(Qt::red);
297  fTextArea->append(QString((char*)a_string.data()).trimmed());
298  fTextArea->setTextColor(previousColor);
299  fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum());
300  return 0;
301}
302/***************************************************************************/
303G4bool G4UIQt::GetHelpChoice(
304 G4int& aInt
305)
306/***************************************************************************/
307/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
308{
309  printf("G4UIQt::GetHelpChoice\n");
310 
311  fHelp = true; // TO KEEP
312//   // SecondaryLoop : // TO KEEP
313  G4Qt* interactorManager = G4Qt::getInstance (); // TO KEEP ?
314  Prompt("Help"); // TO KEEP
315  exitHelp = false; // TO KEEP
316  void* event; // TO KEEP
317  while((event = interactorManager->GetEvent())!=NULL) {  // TO KEEP
318    interactorManager->DispatchEvent(event); // TO KEEP
319    if(exitHelp==true) break; // TO KEEP
320  } // TO KEEP
321  Prompt("session"); // TO KEEP
322  // // TO KEEP
323  if(fHelp==false) return false; // TO KEEP
324  aInt = fHelpChoice; // TO KEEP
325  fHelp = false; // TO KEEP
326  return true; // TO KEEP
327}
328/***************************************************************************/
329void G4UIQt::ExitHelp(
330)
331/***************************************************************************/
332/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
333{
334  printf("G4UIQt::ExitHelp\n");
335}
336
337/***************************************************************************/
338void G4UIQt::AddMenu (
339 const char* a_name
340,const char* a_label
341)
342/***************************************************************************/
343/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
344{
345  printf("G4UIQt::AddMenu %s %s\n",a_name,a_label);
346
347  QMenu *fileMenu = fMainWindow->menuBar()->addMenu(a_label);
348  AddInteractor (a_name,(G4Interactor)fileMenu);
349
350  //  QMenu *menu = new QMenu("test");//a_label);
351  //  fMainWindow->menuBar()->addMenu(menu);
352
353//   if(menuBar==NULL) return;
354//   if(a_name==NULL) return;
355//   if(a_label==NULL) return;
356//   XtManageChild (menuBar);
357//   // Pulldown menu :
358//   Widget widget;
359//   widget = XmCreatePulldownMenu (menuBar,(char*)a_name,NULL,0);
360//   AddInteractor (a_name,(G4Interactor)widget);
361//   // Cascade button :
362//   Arg args[2];
363//   XmString cps = XmStringLtoRCreate((char*)a_label,XmSTRING_DEFAULT_CHARSET);
364//   XtSetArg (args[0],XmNlabelString,cps);
365//   XtSetArg (args[1],XmNsubMenuId,widget);
366//   widget = XmCreateCascadeButton (menuBar,(char*)a_name,args,2);
367//   XmStringFree (cps);
368//   XtManageChild (widget);
369//   ExecuteChangeSizeFunction(form);
370}
371/***************************************************************************/
372void G4UIQt::AddButton (
373 const char* a_menu
374,const char* a_label
375,const char* a_command
376)
377/***************************************************************************/
378/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
379{
380  if(a_menu==NULL) return; // TO KEEP
381  if(a_label==NULL) return; // TO KEEP
382  if(a_command==NULL) return; // TO KEEP
383  QMenu *parent = (QMenu*)GetInteractor(a_menu);
384  if(parent==NULL) return;
385 
386  signalMapper = new QSignalMapper(this);
387  QAction *action = parent->addAction(a_label, signalMapper, SLOT(map()));
388  signalMapper->setMapping(action, QString(a_command));
389  connect(signalMapper, SIGNAL(mapped(const QString &)),this, SLOT(buttonCallback(const QString&)));
390 
391  //  std::string slot = SLOT(buttonCallback(std::string));   
392  printf("G4UIQt::AddButton %s %s %s\n",a_menu,a_label,a_command);
393
394//   Widget widget = XmCreatePushButton(parent,(char*)a_label,NULL,0);
395//   XtManageChild (widget);
396//   XtAddCallback (widget,XmNactivateCallback,ButtonCallback,(XtPointer)this);
397//   commands[action] = a_command;
398}
399
400
401// /***************************************************************************/
402//G4String G4UIQt::GetCommand (
403//  QAction *a_widget
404//)
405// /***************************************************************************/
406// /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
407// {
408//   return commands[a_widget];
409// }
410/***************************************************************************/
411/***************************************************************************/
412/***************************************************************************/
413
414/**
415  Callback call when "enter" clicked on the command zone.
416  Send the command to geant4
417 */
418void G4UIQt::buttonCallback (
419  const QString& a_command
420)
421/***************************************************************************/
422/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
423{
424  if(fHelp==true) return; // Disabled when in help.
425  G4String ss = G4String(a_command.toStdString().c_str());
426  printf ("debug : execute:\n-%s- %d %d \n",ss.data(),exitSession,exitPause);
427  ApplyShellCommand(ss,exitSession,exitPause);
428  if(exitSession==true)
429    SessionTerminate();
430}
431
432/**
433  Callback call when "click on a menu entry.
434  Send the associated command to geant4
435 */
436void G4UIQt::commandEnteredCallback (
437)
438/***************************************************************************/
439/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
440{
441  printf ("debug : callback\n");
442  G4String command (fCommandArea->text().toStdString().c_str());
443  if (fCommandArea->text().toStdString().c_str() != "") {
444    fCommandHistoryArea->append(fCommandArea->text());
445    if(fHelp==true) {
446      exitHelp = true;
447      fHelp = ConvertStringToInt(command.data(),fHelpChoice);
448    } else {
449      ApplyShellCommand (command,exitSession,exitPause);
450      if(exitSession==true)
451        SessionTerminate();
452    }
453  }
454  fCommandArea->setText("");
455}
456
457
458/***************************************************************************/
459void G4UIQt::clearButtonCallback (
460)
461/***************************************************************************/
462/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
463{
464  fTextArea->clear();
465}
466
467//////////////////////////////////////////////////////////////////////////////
468G4bool ConvertStringToInt(
469 const char* aString
470,int& aInt
471)
472//////////////////////////////////////////////////////////////////////////////
473//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
474{
475  aInt = 0; // TO KEEP
476  if(aString==NULL) return false; // TO KEEP
477  char* s; // TO KEEP
478  long value = strtol(aString,&s,10); // TO KEEP
479  if(s==aString) return false; // TO KEEP
480  aInt = value; // TO KEEP
481  return true; // TO KEEP
482}
483
484#endif
Note: See TracBrowser for help on using the repository browser.