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

Last change on this file since 503 was 503, checked in by garnier, 18 years ago

r609@mac-90108: laurentgarnier | 2007-06-12 11:33:35 +0200
pb sur les menus, voici une version bien bancale

  • Property svn:mime-type set to text/cpp
File size: 18.1 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
146 QMenu *fileMenu = fMainWindow->menuBar()->addMenu(tr("&File"));
147 fileMenu->addAction("repaint", fMainWindow, SLOT(repaint()));
148 fileMenu->addAction("Quitter", fMainWindow, SLOT(close()));
149 fileMenu->addAction("Hide", fMainWindow, SLOT(hide()));
150
151// QMainWindow top = (QMainWindow)interactorManager->GetMainInteractor();
152
153// if(getenv("XENVIRONMENT")==NULL) {
154// XrmDatabase database = XrmGetDatabase(XtDisplay(top));
155// if(database!=NULL) {
156// XrmPutLineResource(&database,"*topShadowColor:white");
157// XrmPutLineResource(&database,"*bottomShadowColor:black");
158// XrmPutLineResource(&database,"*foreground:black");
159// XrmPutLineResource(&database,"*background:lightgrey");
160// XrmPutLineResource(&database,"*borderColor:lightgrey");
161// XrmPutLineResource(&database,"*fontList:-*-helvetica-bold-r-*-*-*-120-*-*-*-*-iso8859-1");
162// XrmPutLineResource(&database,"*text.background:white");
163// XrmPutLineResource(&database,"*text.fontList:*courier*-r-*--14-*");
164// XrmPutLineResource(&database,"*text.maxLength:8000");
165// }
166// }
167
168// Arg args[9];
169// XtSetArg(args[0],XmNkeyboardFocusPolicy,XmPOINTER); // For completion.
170// shell = XtAppCreateShell ("G4UIXm","G4UIXm",
171// topLevelShellWidgetClass,XtDisplay(top),
172// args,1);
173// form = XmCreateForm (shell,(char*)"form",NULL,0);
174// XtManageChild (form);
175
176// XtSetArg(args[0],XmNtopAttachment ,XmATTACH_FORM);
177// XtSetArg(args[1],XmNleftAttachment ,XmATTACH_FORM);
178// XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
179// menuBar = XmCreateMenuBar (form,(char*)"menuBar",args,3);
180
181// XtSetArg(args[0],XmNtopAttachment ,XmATTACH_NONE);
182// XtSetArg(args[1],XmNleftAttachment ,XmATTACH_FORM);
183// XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
184// XtSetArg(args[3],XmNbottomAttachment ,XmATTACH_FORM);
185// command = XmCreateCommand (form,(char*)"command",args,4);
186// XtManageChild (command);
187
188// XtSetArg(args[0],XmNtopAttachment ,XmATTACH_NONE);
189// XtSetArg(args[1],XmNleftAttachment ,XmATTACH_FORM);
190// XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
191// XtSetArg(args[3],XmNbottomAttachment,XmATTACH_WIDGET);
192// XtSetArg(args[4],XmNbottomWidget ,command);
193// XmString cps = XmStringLtoRCreate((char*)"Clear",XmSTRING_DEFAULT_CHARSET);
194// XtSetArg (args[5],XmNlabelString,cps);
195// Widget clearButton = XmCreatePushButton(form,(char*)"clearButton",args,6);
196// XmStringFree (cps);
197// XtManageChild (clearButton);
198
199// XtSetArg(args[0],XmNtopAttachment ,XmATTACH_WIDGET);
200// XtSetArg(args[1],XmNtopWidget ,menuBar);
201// XtSetArg(args[2],XmNleftAttachment ,XmATTACH_FORM);
202// XtSetArg(args[3],XmNrightAttachment ,XmATTACH_FORM);
203// XtSetArg(args[4],XmNbottomAttachment,XmATTACH_WIDGET);
204// XtSetArg(args[5],XmNbottomWidget ,clearButton);
205// XtSetArg(args[6],XmNeditMode ,XmMULTI_LINE_EDIT);
206// XtSetArg(args[7],XmNrows ,12);
207// XtSetArg(args[8],XmNcolumns ,80);
208// text = XmCreateSGcrolledText (form,(char*)"text",args,9);
209// XtManageChild (text);
210
211// XtAddCallback(clearButton,XmNactivateCallback,
212// clearButtonCallback,(XtPointer)text);
213// XtAddCallback(command,XmNcommandEnteredCallback,
214// commandEnteredCallback,(XtPointer)this);
215
216// Widget commandText = XmCommandGetChild(command,XmDIALOG_COMMAND_TEXT);
217// XtAddEventHandler(commandText,KeyPressMask,False,keyHandler,(XtPointer)this);
218
219// XtRealizeWidget(shell);
220// XtMapWidget(shell);
221
222 if(UI!=NULL) UI->SetCoutDestination(this); // TO KEEP
223}
224/***************************************************************************/
225G4UIQt::~G4UIQt(
226)
227/***************************************************************************/
228/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
229{
230 G4UImanager* UI = G4UImanager::GetUIpointer(); // TO KEEP
231 if(UI!=NULL) { // TO KEEP
232 UI->SetSession(NULL); // TO KEEP
233 UI->SetCoutDestination(NULL); // TO KEEP
234 }
235
236
237 if (fMainWindow!=NULL)
238 delete fMainWindow;
239}
240/***************************************************************************/
241/*
242 Start the Qt main loop
243 */
244G4UIsession* G4UIQt::SessionStart (
245)
246/***************************************************************************/
247/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
248{
249
250 G4Qt* interactorManager = G4Qt::getInstance ();
251 fMainWindow->show();
252 Prompt("session");
253 exitSession = false;
254
255
256 printf("disable secondary loop\n");
257 interactorManager->DisableSecondaryLoop (); // TO KEEP
258 ((QApplication*)interactorManager->GetMainInteractor())->exec();
259 // on ne passe pas le dessous ? FIXME ????
260
261
262
263
264
265
266// void* event; // TO KEEP
267// while((event = interactorManager->GetEvent())!=NULL) { // TO KEEP
268// interactorManager->DispatchEvent(event); // TO KEEP
269// if(exitSession==true) break; // TO KEEP
270// } // TO KEEP
271 interactorManager->EnableSecondaryLoop (); // TO KEEP
272 printf("enable secondary loop\n");
273 return this; // TO KEEP
274}
275/***************************************************************************/
276
277/**
278 Display the prompt in the prompt area
279 */
280void G4UIQt::Prompt (
281 G4String aPrompt
282)
283/***************************************************************************/
284/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
285{
286 fCommandLabel->setText((char*)aPrompt.data());
287}
288/***************************************************************************/
289void G4UIQt::SessionTerminate (
290)
291/***************************************************************************/
292/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
293{
294}
295/***************************************************************************/
296void G4UIQt::PauseSessionStart (
297 G4String a_state
298)
299/***************************************************************************/
300/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
301{
302 printf("G4UIQt::PauseSessionStart\n");
303 if(a_state=="G4_pause> ") { // TO KEEP
304 SecondaryLoop ("Pause, type continue to exit this state"); // TO KEEP
305 } // TO KEEP
306
307 if(a_state=="EndOfEvent") { // TO KEEP
308 // Picking with feed back in event data Done here !!!
309 SecondaryLoop ("End of event, type continue to exit this state"); // TO KEEP
310 } // TO KEEP
311}
312/***************************************************************************/
313void G4UIQt::SecondaryLoop (
314 G4String a_prompt
315)
316/***************************************************************************/
317/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
318{
319 printf("G4UIQt::SecondaryLoop\n");
320 G4Qt* interactorManager = G4Qt::getInstance (); // TO KEEP ?
321 Prompt(a_prompt); // TO KEEP
322 exitPause = false; // TO KEEP
323 void* event; // TO KEEP
324 while((event = interactorManager->GetEvent())!=NULL) { // TO KEEP
325 interactorManager->DispatchEvent(event); // TO KEEP
326 if(exitPause==true) break; // TO KEEP
327 } // TO KEEP
328 Prompt("session"); // TO KEEP
329}
330/***************************************************************************/
331/**
332 Receive a cout from Geant4. We have to display it in the cout zone
333 */
334G4int G4UIQt::ReceiveG4cout (
335 G4String a_string
336)
337/***************************************************************************/
338/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
339{
340 fTextArea->append(QString((char*)a_string.data()).trimmed());
341 fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum());
342 return 0;
343}
344/***************************************************************************/
345/**
346 Receive a cerr from Geant4. We have to display it in the cout zone
347 */
348G4int G4UIQt::ReceiveG4cerr (
349 G4String a_string
350)
351/***************************************************************************/
352/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
353{
354 QColor previousColor = fTextArea->textColor();
355 fTextArea->setTextColor(Qt::red);
356 fTextArea->append(QString((char*)a_string.data()).trimmed());
357 fTextArea->setTextColor(previousColor);
358 fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum());
359 return 0;
360}
361/***************************************************************************/
362G4bool G4UIQt::GetHelpChoice(
363 G4int& aInt
364)
365/***************************************************************************/
366/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
367{
368 printf("G4UIQt::GetHelpChoice\n");
369
370 fHelp = true; // TO KEEP
371// // SecondaryLoop : // TO KEEP
372 G4Qt* interactorManager = G4Qt::getInstance (); // TO KEEP ?
373 Prompt("Help"); // TO KEEP
374 exitHelp = false; // TO KEEP
375 void* event; // TO KEEP
376 while((event = interactorManager->GetEvent())!=NULL) { // TO KEEP
377 interactorManager->DispatchEvent(event); // TO KEEP
378 if(exitHelp==true) break; // TO KEEP
379 } // TO KEEP
380 Prompt("session"); // TO KEEP
381 // // TO KEEP
382 if(fHelp==false) return false; // TO KEEP
383 aInt = fHelpChoice; // TO KEEP
384 fHelp = false; // TO KEEP
385 return true; // TO KEEP
386}
387/***************************************************************************/
388void G4UIQt::ExitHelp(
389)
390/***************************************************************************/
391/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
392{
393 printf("G4UIQt::ExitHelp\n");
394}
395
396/***************************************************************************/
397void G4UIQt::AddMenu (
398 const char* a_name
399,const char* a_label
400)
401/***************************************************************************/
402/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
403{
404 printf("G4UIQt::AddMenu %s %s\n",a_name,a_label);
405 QString *name = new QString(a_name);
406 QString *label = new QString(a_label);
407 menuName.push_back(name);
408 menuLabel.push_back(label);
409
410 // QMenu *menu = new QMenu("test");//a_label);
411 // fMainWindow->menuBar()->addMenu(menu);
412
413// if(menuBar==NULL) return;
414// if(a_name==NULL) return;
415// if(a_label==NULL) return;
416// XtManageChild (menuBar);
417// // Pulldown menu :
418// Widget widget;
419// widget = XmCreatePulldownMenu (menuBar,(char*)a_name,NULL,0);
420// AddInteractor (a_name,(G4Interactor)widget);
421// // Cascade button :
422// Arg args[2];
423// XmString cps = XmStringLtoRCreate((char*)a_label,XmSTRING_DEFAULT_CHARSET);
424// XtSetArg (args[0],XmNlabelString,cps);
425// XtSetArg (args[1],XmNsubMenuId,widget);
426// widget = XmCreateCascadeButton (menuBar,(char*)a_name,args,2);
427// XmStringFree (cps);
428// XtManageChild (widget);
429// ExecuteChangeSizeFunction(form);
430}
431/***************************************************************************/
432void G4UIQt::AddButton (
433 const char* a_menu
434,const char* a_label
435,const char* a_command
436)
437/***************************************************************************/
438/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
439{
440 printf("G4UIQt::AddButton %s %s %s\n",a_menu,a_label,a_command);
441// if(a_menu==NULL) return; // TO KEEP
442// if(a_label==NULL) return; // TO KEEP
443// if(a_command==NULL) return; // TO KEEP
444// Widget parent = (Widget)GetInteractor(a_menu);
445// if(parent==NULL) return;
446// Widget widget = XmCreatePushButton(parent,(char*)a_label,NULL,0);
447// XtManageChild (widget);
448// XtAddCallback (widget,XmNactivateCallback,ButtonCallback,(XtPointer)this);
449// commands[widget] = a_command;
450}
451// /***************************************************************************/
452// G4String G4UIXm::GetCommand (
453// Widget a_widget
454// )
455// /***************************************************************************/
456// /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
457// {
458// return commands[a_widget];
459// }
460/***************************************************************************/
461/***************************************************************************/
462/***************************************************************************/
463
464/**
465 Callback call when "enter" clicked on the command zone.
466 Send the command to geant4
467 */
468// TODO !
469void G4UIQt::commandEnteredCallback (
470)
471/***************************************************************************/
472/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
473{
474 G4String command (fCommandArea->text().toStdString().c_str());
475 if (fCommandArea->text().toStdString().c_str() != "") {
476 fCommandHistoryArea->append(fCommandArea->text());
477 if(fHelp==true) {
478 exitHelp = true;
479 fHelp = ConvertStringToInt(command.data(),fHelpChoice);
480 } else {
481 ApplyShellCommand (command,exitSession,exitPause);
482 }
483 }
484 fCommandArea->setText("");
485}
486
487 /***************************************************************************/
488void G4UIQt::clearButtonCallback (
489)
490/***************************************************************************/
491/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
492{
493 fTextArea->clear();
494}
495
496//////////////////////////////////////////////////////////////////////////////
497G4bool ConvertStringToInt(
498 const char* aString
499,int& aInt
500)
501//////////////////////////////////////////////////////////////////////////////
502//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
503{
504 aInt = 0; // TO KEEP
505 if(aString==NULL) return false; // TO KEEP
506 char* s; // TO KEEP
507 long value = strtol(aString,&s,10); // TO KEEP
508 if(s==aString) return false; // TO KEEP
509 aInt = value; // TO KEEP
510 return true; // TO KEEP
511}
512
513#endif
Note: See TracBrowser for help on using the repository browser.