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

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

r670@mac-90108: laurentgarnier | 2007-11-26 18:13:28 +0100
mise a jour pour Qt3 linux

  • Property svn:mime-type set to text/cpp
File size: 35.9 KB
RevLine 
[607]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//
[608]27// $Id: G4UIQt.cc,v 1.12 2007/11/15 18:24:27 lgarnier Exp $
[607]28// GEANT4 tag $Name: $
29//
30// L. Garnier
31
[610]32#define GEANT4_QT_DEBUG
[607]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 <qapplication.h>
49#include <qlineedit.h>
50#include <qwidget.h>
51#include <qmenubar.h>
52#include <qlayout.h>
53#include <qpushbutton.h>
54#include <qlabel.h>
55#include <qsplitter.h>
56#include <qscrollbar.h>
57#include <qdialog.h>
58#include <qevent.h>
59#include <qtextedit.h>
60#include <qsignalmapper.h>
61
[608]62#include <qmainwindow.h>
[607]63#if QT_VERSION >= 0x040000
64#include <qmenu.h>
65#include <qlistwidget.h>
66#include <qtreewidget.h>
67#else
68#include <qaction.h>
69#include <qheader.h>
70#include <qlistview.h>
71#include <qpopupmenu.h>
72#endif
73
74
75
76#include <stdlib.h>
77
78// Pourquoi Static et non variables de classe ?
79static G4bool exitSession = true;
80static G4bool exitPause = true;
81
82/** Build a Qt window with a menubar, output area and promt area<br>
83<pre>
84 +-----------------------+
85 |exit menu| |
86 | |
87 | +-------------------+ |
88 | | | |
89 | | Output area | |
90 | | | |
91 | +-------------------+ |
92 | | clear | |
93 | +-------------------+ |
94 | | promt history | |
95 | +-------------------+ |
96 | +-------------------+ |
97 | |> promt area | |
98 | +-------------------+ |
99 +-----------------------+
100</pre>
101*/
102G4UIQt::G4UIQt (
103 int argc
104,char** argv
105)
106 :fHelpDialog(NULL)
107{
[608]108#ifdef GEANT4_QT_DEBUG
[607]109 printf("G4UIQt::Initialise %d %s\n",argc,argv[0]);
[608]110#endif
[607]111 G4Qt* interactorManager = G4Qt::getInstance (argc,argv,(char*)"Qt");
112 G4UImanager* UI = G4UImanager::GetUIpointer();
113 if(UI!=NULL) UI->SetSession(this);
114
115 fMainWindow = new QMainWindow();
116
[608]117#ifdef GEANT4_QT_DEBUG
[607]118 printf("G4UIQt::Initialise after main window creation\n");
[608]119#endif
[607]120#if QT_VERSION < 0x040000
121 fMainWindow->setCaption( tr( "G4UI Session" ));
122 fMainWindow->resize(800,600);
123 fMainWindow->move(50,100);
124#else
125 fMainWindow->setWindowTitle( tr("G4UI Session") );
126 fMainWindow->resize(800,600);
127 fMainWindow->move(QPoint(50,100));
128#endif
129
[617]130 QSplitter *splitter = new QSplitter(Qt::Vertical,fMainWindow);
[611]131
[610]132 // Set layouts
133
134#if QT_VERSION < 0x040000
135
136 QWidget* topWidget = new QWidget(splitter);
137 QWidget* bottomWidget = new QWidget(splitter);
138
139 QVBoxLayout *layoutTop = new QVBoxLayout(topWidget);
140 QVBoxLayout *layoutBottom = new QVBoxLayout(bottomWidget);
141#else
142 QWidget* topWidget = new QWidget();
143 QWidget* bottomWidget = new QWidget();
144
145 QVBoxLayout *layoutTop = new QVBoxLayout;
146 QVBoxLayout *layoutBottom = new QVBoxLayout;
147#endif
148
149 // fill them
150
151 fTextArea = new QTextEdit(topWidget);
152#ifdef GEANT4_QT_DEBUG
153 printf("G4UIQt:: end\n");
154#endif
155#ifdef GEANT4_QT_DEBUG
156 printf("G4UIQt::PushButton 1\n");
157#endif
158 QPushButton *clearButton = new QPushButton("clear",topWidget);
159#ifdef GEANT4_QT_DEBUG
160 printf("G4UIQt::end1\n");
161#endif
[607]162 connect(clearButton, SIGNAL(clicked()), SLOT(ClearButtonCallback()));
163
164#if QT_VERSION < 0x040000
[610]165 fCommandHistoryArea = new QListView(bottomWidget);
[607]166 fCommandHistoryArea->setSelectionMode(QListView::Single);
[608]167 connect(fCommandHistoryArea, SIGNAL(selectionChanged()), SLOT(CommandHistoryCallback()));
[607]168#else
169 fCommandHistoryArea = new QListWidget();
170 fCommandHistoryArea->setSelectionMode(QAbstractItemView::SingleSelection);
[608]171 connect(fCommandHistoryArea, SIGNAL(itemSelectionChanged()), SLOT(CommandHistoryCallback()));
[607]172#endif
173 fCommandHistoryArea->installEventFilter(this);
[610]174 fCommandLabel = new QLabel("",bottomWidget);
[607]175
[610]176 fCommandArea = new QLineEdit(bottomWidget);
[607]177 fCommandArea->installEventFilter(this);
[610]178#ifdef GEANT4_QT_DEBUG
179 printf("G4UIQt:: 1\n");
180#endif
[607]181#if QT_VERSION < 0x040000
182 fCommandArea->setActiveWindow();
183#else
184 fCommandArea->activateWindow();
185#endif
186 connect(fCommandArea, SIGNAL(returnPressed()), SLOT(CommandEnteredCallback()));
187#if QT_VERSION < 0x040000
188 fCommandArea->setFocusPolicy ( QWidget::StrongFocus );
189 fCommandArea->setFocus();
190#else
191 fCommandArea->setFocusPolicy ( Qt::StrongFocus );
192 fCommandArea->setFocus(Qt::TabFocusReason);
193#endif
194 fTextArea->setReadOnly(true);
195
196
[610]197#ifdef GEANT4_QT_DEBUG
198 printf("G4UIQt:: 2\n");
199#endif
[607]200
[610]201#ifdef GEANT4_QT_DEBUG
202 printf("G4UIQt:: 3\n");
[607]203#endif
204
205
206 layoutTop->addWidget(fTextArea);
207 layoutTop->addWidget(clearButton);
208
209#if QT_VERSION >= 0x040000
210 topWidget->setLayout(layoutTop);
211#endif
212
[610]213#ifdef GEANT4_QT_DEBUG
214 printf("G4UIQt:: 4\n");
215#endif
[607]216 layoutBottom->addWidget(fCommandHistoryArea);
217 layoutBottom->addWidget(fCommandLabel);
218 layoutBottom->addWidget(fCommandArea);
219#if QT_VERSION >= 0x040000
220
221 bottomWidget->setLayout(layoutBottom);
222 splitter->addWidget(topWidget);
223 splitter->addWidget(bottomWidget);
224#endif
225
226
[610]227#ifdef GEANT4_QT_DEBUG
228 printf("G4UIQt:: 5\n");
229#endif
[607]230 fMainWindow->setCentralWidget(splitter);
231
232#if QT_VERSION < 0x040000
233
234 // Add a quit subMenu
235 QPopupMenu *fileMenu = new QPopupMenu( fMainWindow);
[608]236 fileMenu->insertItem( "&Quitter", this, SLOT(ExitSession()), CTRL+Key_Q );
[607]237 fMainWindow->menuBar()->insertItem( QString("&File"), fileMenu );
238
239 // Add a Help menu
240 QPopupMenu *helpMenu = new QPopupMenu( fMainWindow );
241 helpMenu->insertItem( "&Show Help", this, SLOT(ShowHelpCallback()), CTRL+Key_H );
242 fMainWindow->menuBar()->insertItem( QString("&Help"), helpMenu );
243
[610]244#ifdef GEANT4_QT_DEBUG
245 printf("G4UIQt:: 6\n");
246#endif
[607]247
248#else
249
250 // Add a quit subMenu
251 QMenu *fileMenu = fMainWindow->menuBar()->addMenu("File");
[608]252 fileMenu->addAction("Quitter", this, SLOT(ExitSession()));
[607]253
254 // Add a Help menu
255 QMenu *helpMenu = fMainWindow->menuBar()->addMenu("Help");
256 helpMenu->addAction("Show Help", this, SLOT(ShowHelpCallback()));
257#endif
[610]258#ifdef GEANT4_QT_DEBUG
259 printf("G4UIQt:: 7\n");
260#endif
[607]261
262 // Set the splitter size. The fTextArea sould be 2/3 on the fMainWindow
263#if QT_VERSION < 0x040000
264 QValueList<int> vals = splitter->sizes();
265#else
266 QList<int> vals = splitter->sizes();
267#endif
268 if(vals.size()==2) {
269 vals[0] = (splitter->orientation()==Qt::Vertical ? splitter->height() : splitter->width())*3/4;
270 vals[1] = (splitter->orientation()==Qt::Vertical ? splitter->height() : splitter->width())*1/4;
271 splitter->setSizes(vals);
272 }
[610]273#ifdef GEANT4_QT_DEBUG
274 printf("G4UIQt:: 8\n");
275#endif
[607]276
277 if(UI!=NULL) UI->SetCoutDestination(this); // TO KEEP
278}
279
280
281
282G4UIQt::~G4UIQt(
283)
284{
285 G4UImanager* UI = G4UImanager::GetUIpointer(); // TO KEEP
286 if(UI!=NULL) { // TO KEEP
287 UI->SetSession(NULL); // TO KEEP
288 UI->SetCoutDestination(NULL); // TO KEEP
289 }
290
291 if (fMainWindow!=NULL)
292 delete fMainWindow;
293}
294
295
296
297/** Start the Qt main loop
298*/
299G4UIsession* G4UIQt::SessionStart (
300)
301{
302
303 G4Qt* interactorManager = G4Qt::getInstance ();
304
305#if QT_VERSION >= 0x040000
[608]306#if QT_VERSION >= 0x040200
[607]307 fMainWindow->setVisible(true);
308#else
309 fMainWindow->show();
310#endif
[608]311#else
312 fMainWindow->show();
313#endif
[607]314 Prompt("session");
315 exitSession = false;
316
317
[608]318#ifdef GEANT4_QT_DEBUG
[607]319 printf("disable secondary loop\n");
[608]320#endif
[607]321 interactorManager->DisableSecondaryLoop (); // TO KEEP
[610]322 if ((QApplication*)interactorManager->GetMainInteractor())
323 ((QApplication*)interactorManager->GetMainInteractor())->exec();
324
[607]325 // on ne passe pas le dessous ? FIXME ????
326 // je ne pense pas 13/06
327
328 // void* event; // TO KEEP
329 // while((event = interactorManager->GetEvent())!=NULL) { // TO KEEP
330 // interactorManager->DispatchEvent(event); // TO KEEP
331 // if(exitSession==true) break; // TO KEEP
332 // } // TO KEEP
333
334 interactorManager->EnableSecondaryLoop ();
[608]335#ifdef GEANT4_QT_DEBUG
[607]336 printf("enable secondary loop\n");
[608]337#endif
[607]338 return this;
339}
340
341
342/** Display the prompt in the prompt area
343 @param aPrompt : string to display as the promt label
344 //FIXME : probablement inutile puisque le seul a afficher qq chose d'autre
345 que "session" est SecondaryLoop()
346*/
347void G4UIQt::Prompt (
348 G4String aPrompt
349)
350{
351 if (!aPrompt) return;
352
353 fCommandLabel->setText((char*)aPrompt.data());
354}
355
356
357void G4UIQt::SessionTerminate (
358)
359{
360 G4Qt* interactorManager = G4Qt::getInstance ();
361 fMainWindow->close();
362 ((QApplication*)interactorManager->GetMainInteractor())->exit();
363}
364
365
366
367/**
368 Called by intercoms/src/G4UImanager.cc<br>
369 Called by visualization/management/src/G4VisCommands.cc with "EndOfEvent" argument<br>
370 It have to pause the session command terminal.<br>
371 Call SecondaryLoop to wait for exit event<br>
372 @param aState
373 @see : G4VisCommandReviewKeptEvents::SetNewValue
374*/
375void G4UIQt::PauseSessionStart (
376 G4String aState
377)
378{
379 if (!aState) return;
380
[608]381#ifdef GEANT4_QT_DEBUG
[607]382 printf("G4UIQt::PauseSessionStart\n");
[608]383#endif
[607]384 if(aState=="G4_pause> ") { // TO KEEP
385 SecondaryLoop ("Pause, type continue to exit this state"); // TO KEEP
386 } // TO KEEP
387
388 if(aState=="EndOfEvent") { // TO KEEP
389 // Picking with feed back in event data Done here !!!
390 SecondaryLoop ("End of event, type continue to exit this state"); // TO KEEP
391 } // TO KEEP
392}
393
394
395
396/**
397 Begin the secondary loop
398 @param a_prompt : label to display as the prompt label
399 */
400void G4UIQt::SecondaryLoop (
401 G4String aPrompt
402)
403{
404 if (!aPrompt) return;
405
[608]406#ifdef GEANT4_QT_DEBUG
[607]407 printf("G4UIQt::SecondaryLoop\n");
[608]408#endif
[607]409 G4Qt* interactorManager = G4Qt::getInstance (); // TO KEEP ?
410 Prompt(aPrompt); // TO KEEP
411 exitPause = false; // TO KEEP
412 void* event; // TO KEEP
413 while((event = interactorManager->GetEvent())!=NULL) { // TO KEEP
414 interactorManager->DispatchEvent(event); // TO KEEP
415 if(exitPause==true) break; // TO KEEP
416 } // TO KEEP
417 Prompt("session"); // TO KEEP
418}
419
420
421
422/**
423 Receive a cout from Geant4. We have to display it in the cout zone
424 @param aString : label to add in the display area
425 @return 0
426*/
427G4int G4UIQt::ReceiveG4cout (
428 G4String aString
429)
430{
431 if (!aString) return 0;
432 G4Qt* interactorManager = G4Qt::getInstance ();
433 if (!interactorManager) return 0;
434
435#if QT_VERSION < 0x040000
436 fTextArea->append(QString((char*)aString.data()).simplifyWhiteSpace());
437 fTextArea->verticalScrollBar()->setValue(fTextArea->verticalScrollBar()->maxValue());
438#else
439 fTextArea->append(QString((char*)aString.data()).trimmed());
440 fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum());
441#endif
442 interactorManager->FlushAndWaitExecution();
443 return 0;
444}
445
446
447/**
448 Receive a cerr from Geant4. We have to display it in the cout zone
449 @param aString : label to add in the display area
450 @return 0
451*/
452G4int G4UIQt::ReceiveG4cerr (
453 G4String aString
454)
455{
456 if (!aString) return 0;
457 G4Qt* interactorManager = G4Qt::getInstance ();
458 if (!interactorManager) return 0;
459
460#if QT_VERSION < 0x040000
461 QColor previousColor = fTextArea->color();
462 fTextArea->setColor(Qt::red);
463 fTextArea->append(QString((char*)aString.data()).simplifyWhiteSpace());
464 fTextArea->setColor(previousColor);
465 fTextArea->verticalScrollBar()->setValue(fTextArea->verticalScrollBar()->maxValue());
466#else
467 QColor previousColor = fTextArea->textColor();
468 fTextArea->setTextColor(Qt::red);
469 fTextArea->append(QString((char*)aString.data()).trimmed());
470 fTextArea->setTextColor(previousColor);
471 fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum());
472#endif
473 interactorManager->FlushAndWaitExecution();
474 return 0;
475}
476
477
478
479/**
480 Add a new menu to the menu bar
481 @param aName name of menu
482 @param aLabel label to display
483 */
484void G4UIQt::AddMenu (
485 const char* aName
486,const char* aLabel
487)
488{
[617]489#ifdef GEANT4_QT_DEBUG
490 printf("G4UIQt::AddMenu 1\n");
491#endif
[607]492 if (aName == NULL) return;
493 if (aLabel == NULL) return;
494
[617]495#ifdef GEANT4_QT_DEBUG
496 printf("G4UIQt::AddMenu 2 %s %s\n",aName,aLabel);
497#endif
[607]498#if QT_VERSION < 0x040000
499 QPopupMenu *fileMenu = new QPopupMenu( fMainWindow);
500 fMainWindow->menuBar()->insertItem( aLabel, fileMenu );
501#else
502 QMenu *fileMenu = new QMenu(aLabel);
503 fMainWindow->menuBar()->insertMenu(fMainWindow->menuBar()->actions().last(),fileMenu);
504#endif
505
506 AddInteractor (aName,(G4Interactor)fileMenu);
507}
508
509
510/**
511 Add a new button to a menu
512 @param aMenu : parent menu
513 @param aLabel : label to display
514 @param aCommand : command to execute as a callback
515 */
516void G4UIQt::AddButton (
517 const char* aMenu
518,const char* aLabel
519,const char* aCommand
520)
521{
522 if(aMenu==NULL) return; // TO KEEP
523 if(aLabel==NULL) return; // TO KEEP
524 if(aCommand==NULL) return; // TO KEEP
525
526#if QT_VERSION < 0x040000
527 QPopupMenu *parent = (QPopupMenu*)GetInteractor(aMenu);
528#else
529 QMenu *parent = (QMenu*)GetInteractor(aMenu);
530#endif
531
532 if(parent==NULL) return;
533
534 QSignalMapper *signalMapper = new QSignalMapper(this);
535#if QT_VERSION < 0x040000
[617]536#if QT_VERSION < 0x030200
537 QAction *action = new QAction(QString(aLabel),QString(aLabel),QKeySequence::QKeySequence (),signalMapper, SLOT(map()));
538#else
[607]539 QAction *action = new QAction(QString(aLabel),QKeySequence::QKeySequence (),signalMapper, SLOT(map()));
[617]540#endif
[607]541 action->addTo(parent);
542#else
543 QAction *action = parent->addAction(aLabel, signalMapper, SLOT(map()));
544#endif
545 signalMapper->setMapping(action, QString(aCommand));
546 connect(signalMapper, SIGNAL(mapped(const QString &)),this, SLOT(ButtonCallback(const QString&)));
547}
548
549
550
551
552/**
553 Open the help dialog in a separate window.<br>
554 This will be display as a tree widget.<br>
555 Implementation of <b>void G4VBasicShell::TerminalHelp(G4String newCommand)</b>
556
557 @param newCommand : open the tree widget item on this command if is set
558*/
559void G4UIQt::TerminalHelp(
560 G4String newCommand
561)
562{
563 // Create the help dialog
564 if (!fHelpDialog) {
565#if QT_VERSION < 0x040000
566 fHelpDialog = new QDialog(fMainWindow,0,FALSE,Qt::WStyle_Title | Qt::WStyle_SysMenu | Qt::WStyle_MinMax );
[617]567 QVBoxLayout *vLayout = new QVBoxLayout(fHelpDialog);
568 QWidget* global = new QWidget(fHelpDialog);
[607]569#else
[617]570 QVBoxLayout *vLayout = new QVBoxLayout();
[607]571 fHelpDialog = new QDialog(fMainWindow,Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
572#endif
[617]573 QSplitter *splitter = new QSplitter(Qt::Horizontal,fHelpDialog);
574
[607]575 QPushButton *exitButton = new QPushButton("Exit",fMainWindow);
576 connect(exitButton, SIGNAL(clicked()), fHelpDialog,SLOT(close()));
577
578 // the help tree
579 G4UImanager* UI = G4UImanager::GetUIpointer();
580 if(UI==NULL) return;
581 G4UIcommandTree * treeTop = UI->GetTree();
582
583 // build widget
584#if QT_VERSION < 0x040000
585 fHelpTreeWidget = new QListView(splitter);
586 fHelpTreeWidget->setSelectionMode(QListView::Single);
587 fHelpTreeWidget->addColumn("Command");
588 fHelpTreeWidget->addColumn("Description");
[617]589 fHelpTreeWidget->setColumnWidth (1,0);
[607]590 fHelpTreeWidget->header()->setResizeEnabled(FALSE,1);
591 // QList<QListViewItem *> items;
592#else
593 fHelpTreeWidget = new QTreeWidget();
594 fHelpTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
595 fHelpTreeWidget->setColumnCount(2);
596 fHelpTreeWidget->setColumnHidden(1,true);
597 QStringList labels;
598 labels << QString("Command") << QString("Description");
599 fHelpTreeWidget->setHeaderLabels(labels);
600 // QList<QTreeWidgetItem *> items;
601#endif
602
603#if QT_VERSION < 0x040000
[610]604#ifdef GEANT4_QT_DEBUG
605 printf("G4UIQt::QTextEdit 1\n");
606#endif
[607]607 fHelpArea = new QTextEdit(splitter);
[610]608#ifdef GEANT4_QT_DEBUG
609 printf("G4UIQt::end 1\n");
610#endif
[607]611#else
[610]612#ifdef GEANT4_QT_DEBUG
613 printf("G4UIQt::QTextEdit 2\n");
614#endif
[607]615 fHelpArea = new QTextEdit();
[610]616#ifdef GEANT4_QT_DEBUG
617 printf("G4UIQt::end 2\n");
[607]618#endif
[610]619#endif
[607]620 fHelpArea->setReadOnly(true);
621
622 G4int treeSize = treeTop->GetTreeEntry();
623#if QT_VERSION < 0x040000
624 QListViewItem * newItem;
625#else
626 QTreeWidgetItem * newItem;
627#endif
628 for (int a=0;a<treeSize;a++) {
629 // Creating new item
630
631#if QT_VERSION < 0x040000
632 newItem = new QListViewItem(fHelpTreeWidget);
633 newItem->setText(0,QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).simplifyWhiteSpace());
634 newItem->setText(1,QString((char*)(treeTop->GetTree(a+1)->GetTitle()).data()).simplifyWhiteSpace());
635#else
636 //FIXME : Qt 4.2
637 // QStringList stringList;
638 // stringList << QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).trimmed() ;
639 // stringList << QString((char*)(treeTop->GetTree(a+1)->GetTitle()).data()).trimmed() ;
640 // newItem = new QTreeWidgetItem(stringList);
641 // FIXME : Qt 4.0
642 newItem = new QTreeWidgetItem(fHelpTreeWidget);
643 newItem->setText(0,QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).trimmed());
644 newItem->setText(1,QString((char*)(treeTop->GetTree(a+1)->GetTitle()).data()).trimmed());
645#endif
646
647
648 // look for childs
649 CreateChildTree(newItem,treeTop->GetTree(a+1));
650 // items.append(newItem);
651 }
652
653 connect(fHelpTreeWidget, SIGNAL(itemSelectionChanged ()),this, SLOT(HelpTreeClicCallback()));
654 connect(fHelpTreeWidget, SIGNAL(itemDoubleClicked (QTreeWidgetItem*,int)),this, SLOT(HelpTreeDoubleClicCallback(QTreeWidgetItem*,int)));
655
656 // Set layouts
657
[617]658#if QT_VERSION >= 0x040000
[607]659 splitter->addWidget(fHelpTreeWidget);
660 splitter->addWidget(fHelpArea);
661#endif
662
663
[617]664#if QT_VERSION < 0x040000
665 vLayout->addWidget(global);
666#else
[607]667 vLayout->addWidget(splitter);
[617]668#endif
[607]669 vLayout->addWidget(exitButton);
670#if QT_VERSION >= 0x040000
671 fHelpDialog->setLayout(vLayout);
672#endif
673
674 }
675
676 // Look for the choosen command "newCommand"
677 size_t i = newCommand.index(" ");
678 G4String targetCom="";
679 if( i != std::string::npos )
680 {
681 G4String newValue = newCommand(i+1,newCommand.length()-(i+1));
682 newValue.strip(G4String::both);
683 targetCom = ModifyToFullPathCommand( newValue );
684 }
685 if (targetCom != "") {
686#if QT_VERSION < 0x040000
687 QListViewItem* findItem = NULL;
688 QListViewItem* tmpItem = fHelpTreeWidget->firstChild();
689 while (tmpItem != 0) {
690 if (!findItem) {
691 findItem = FindTreeItem(tmpItem,QString((char*)targetCom.data()));
692 }
693 tmpItem = tmpItem->nextSibling();
694 }
695#else
696 QTreeWidgetItem* findItem = NULL;
697 for (int a=0;a<fHelpTreeWidget->topLevelItemCount();a++) {
698 if (!findItem) {
699 findItem = FindTreeItem(fHelpTreeWidget->topLevelItem(a),QString((char*)targetCom.data()));
700 }
701 }
702#endif
703
704 if (findItem) {
705
706 //collapsed open item
707#if QT_VERSION < 0x040000
708
709 // FIXME : Has to be checked
710 QListViewItem* tmpItem = fHelpTreeWidget->firstChild();
711 QList<QListViewItem> openItems;
712 while ((tmpItem != 0) || (!openItems.isEmpty())) {
713 if (tmpItem->isOpen() ) {
714 tmpItem->setOpen(false);
715 openItems.append(tmpItem);
716 tmpItem = tmpItem->firstChild();
717 } else {
718 tmpItem = tmpItem->nextSibling();
719 }
720 if (tmpItem == 0) {
721 tmpItem = openItems.take(openItems.count()-1);
722 }
723 }
724#else
725 QList<QTreeWidgetItem *> selected;
726
727 selected = fHelpTreeWidget->selectedItems();
728 if ( selected.count() != 0 ) {
729 QTreeWidgetItem * tmp =selected.at( 0 );
730 while ( tmp) {
731#if QT_VERSION < 0x040202
732 fHelpTreeWidget->setItemExpanded(tmp,false);
733#else
734 tmp->setExpanded(false);
735#endif
736 tmp = tmp->parent();
737 }
738 }
739#endif
740
741 // clear old selection
742 fHelpTreeWidget->clearSelection();
743
744 // set new selection
745#if QT_VERSION >= 0x040000
746#if QT_VERSION < 0x040202
747 fHelpTreeWidget->setItemSelected(findItem,true);
748#else
749 findItem->setSelected(true);
750#endif
751#else
752 findItem->setSelected(true);
753#endif
754
755 // expand parent item
756 while ( findItem) {
757#if QT_VERSION < 0x040000
758 findItem->setOpen(true);
759#else
760#if QT_VERSION < 0x040202
761 fHelpTreeWidget->setItemExpanded(findItem,true);
762#else
763 findItem->setExpanded(true);
764#endif
765#endif
766 findItem = findItem->parent();
767 }
768
769 // Call the update of the right textArea
770 HelpTreeClicCallback();
771 }
772 }
773#if QT_VERSION < 0x040000
774 fHelpDialog->setCaption( tr( "Help on commands" ));
775#else
776 fHelpDialog->setWindowTitle(tr("Help on commands"));
777#endif
778 fHelpDialog->resize(800,600);
779 fHelpDialog->move(QPoint(400,150));
780 fHelpDialog->show();
781 fHelpDialog->raise();
782#if QT_VERSION < 0x040000
783 fHelpDialog->setActiveWindow();
784#else
785 fHelpDialog->activateWindow();
786#endif
787}
788
789
790
791/** Fill the Help Tree Widget
792 @param aParent : parent item to fill
793 @param aCommandTree : commandTree node associate with this part of the Tree
794*/
795#if QT_VERSION < 0x040000
796void G4UIQt::CreateChildTree(
797 QListViewItem *aParent
798,G4UIcommandTree *aCommandTree
799#else
800void G4UIQt::CreateChildTree(
801 QTreeWidgetItem *aParent
802,G4UIcommandTree *aCommandTree
803#endif
804)
805{
806 if (aParent == NULL) return;
807 if (aCommandTree == NULL) return;
808
809
810 // Creating new item
811#if QT_VERSION < 0x040000
812 QListViewItem * newItem;
813#else
814 QTreeWidgetItem * newItem;
815#endif
816
817 // Get the Sub directories
818 for (int a=0;a<aCommandTree->GetTreeEntry();a++) {
819
820#if QT_VERSION < 0x040000
821 newItem = new QListViewItem(aParent);
822 newItem->setText(0,QString((char*)(aCommandTree->GetTree(a+1)->GetPathName()).data()).simplifyWhiteSpace());
823 newItem->setText(1,QString((char*)(aCommandTree->GetTree(a+1)->GetTitle()).data()).simplifyWhiteSpace());
824
825#else
826 newItem = new QTreeWidgetItem(aParent);
827 newItem->setText(0,QString((char*)(aCommandTree->GetTree(a+1)->GetPathName()).data()).trimmed());
828 newItem->setText(1,QString((char*)(aCommandTree->GetTree(a+1)->GetTitle()).data()).trimmed());
829#endif
830
831 CreateChildTree(newItem,aCommandTree->GetTree(a+1));
832 }
833
834
835
836 // Get the Commands
837
838 for (int a=0;a<aCommandTree->GetCommandEntry();a++) {
839
840 QStringList stringList;
841#if QT_VERSION < 0x040000
842 newItem = new QListViewItem(aParent);
843 newItem->setText(0,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).simplifyWhiteSpace());
844 newItem->setText(1,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).simplifyWhiteSpace());
845 newItem->setOpen(false);
846
847#else
848 newItem = new QTreeWidgetItem(aParent);
849 newItem->setText(0,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).trimmed());
850 newItem->setText(1,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).trimmed());
851#if QT_VERSION < 0x040202
852 fHelpTreeWidget->setItemExpanded(newItem,false);
853#else
854 newItem->setExpanded(false);
855#endif
856#endif
857
858 }
859}
860
861
862/** Find a treeItemWidget in the help tree
863 @param aCommand item's String to look for
864 @return item if found, NULL if not
865*/
866#if QT_VERSION < 0x040000
867QListViewItem* G4UIQt::FindTreeItem(
868 QListViewItem *aParent
869#else
870QTreeWidgetItem* G4UIQt::FindTreeItem(
871 QTreeWidgetItem *aParent
872#endif
873,const QString& aCommand
874)
875{
876 if (aParent == NULL) return NULL;
877
878 if (aParent->text(0) == aCommand)
879 return aParent;
880
881#if QT_VERSION < 0x040000
882 QListViewItem * tmp = NULL;
883 QListViewItem* tmpItem = aParent->firstChild();
884 while (tmpItem != 0) {
885 if (!tmp)
886 tmp = FindTreeItem(tmpItem,aCommand);
887 tmpItem = tmpItem->nextSibling();
888 }
889
890#else
891 QTreeWidgetItem * tmp = NULL;
892 for (int a=0;a<aParent->childCount();a++) {
893 if (!tmp)
894 tmp = FindTreeItem(aParent->child(a),aCommand);
895 }
896#endif
897 return tmp;
898}
899
900
901/** Build the command list parameters in a QString<br>
902 Reimplement partialy the G4UIparameter.cc
903 @param aCommand : command to list parameters
904 @see G4UIparameter::List()
905 @see G4UIcommand::List()
906 @return the command list parameters, or "" if nothing
907*/
908QString G4UIQt::GetCommandList (
909 const G4UIcommand *aCommand
910)
911{
912
913 QString txt ="";
914 if (aCommand == NULL)
915 return txt;
916
917 G4String commandPath = aCommand->GetCommandPath();
918 G4String rangeString = aCommand->GetRange();
919 G4int n_guidanceEntry = aCommand->GetGuidanceEntries();
920 G4int n_parameterEntry = aCommand->GetParameterEntries();
921
922 if ((commandPath == "") &&
923 (rangeString == "") &&
924 (n_guidanceEntry == 0) &&
925 (n_parameterEntry == 0)) {
926 return txt;
927 }
928
929 if((commandPath.length()-1)!='/') {
930 txt += "Command " + QString((char*)(commandPath).data()) + "\n";
931 }
932 txt += "Guidance :\n";
933
934 for( G4int i_thGuidance=0; i_thGuidance < n_guidanceEntry; i_thGuidance++ ) {
935 txt += QString((char*)(aCommand->GetGuidanceLine(i_thGuidance)).data()) + "\n";
936 }
937 if( ! rangeString.isNull() ) {
938 txt += " Range of parameters : " + QString((char*)(rangeString).data()) + "\n";
939 }
940 if( n_parameterEntry > 0 ) {
941 G4UIparameter *param;
942
943 // Re-implementation of G4UIparameter.cc
944
945 for( G4int i_thParameter=0; i_thParameter<n_parameterEntry; i_thParameter++ ) {
946 param = aCommand->GetParameter(i_thParameter);
947 txt += "\nParameter : " + QString((char*)(param->GetParameterName()).data()) + "\n";
948 if( ! param->GetParameterGuidance().isNull() )
949 txt += QString((char*)(param->GetParameterGuidance()).data())+ "\n" ;
950 txt += " Parameter type : " + QString(QChar(param->GetParameterType())) + "\n";
951 if(param->IsOmittable()){
952 txt += " Omittable : True\n";
953 } else {
954 txt += " Omittable : False\n";
955 }
956 if( param->GetCurrentAsDefault() ) {
957 txt += " Default value : taken from the current value\n";
958 } else if( ! param->GetDefaultValue().isNull() ) {
959 txt += " Default value : " + QString((char*)(param->GetDefaultValue()).data())+ "\n";
960 }
961 if( ! param->GetParameterRange().isNull() ) {
962 txt += " Parameter range : " + QString((char*)(param->GetParameterRange()).data())+ "\n";
963 }
964 if( ! param->GetParameterCandidates().isNull() ) {
965 txt += " Candidates : " + QString((char*)(param->GetParameterCandidates()).data())+ "\n";
966 }
967 }
968 }
969 return txt;
970}
971
972
973
974/** Implement G4VBasicShell vurtual function
975 */
976G4bool G4UIQt::GetHelpChoice(
977 G4int& aInt
978)
979{
[608]980#ifdef GEANT4_QT_DEBUG
[607]981 printf("G4UIQt::GetHelpChoice SHOULD NEVER GO HERE");
[608]982#endif
[607]983 return true;
984}
985
986
987/** Implement G4VBasicShell vurtual function
988*/
989void G4UIQt::ExitHelp(
990)
991{
[608]992#ifdef GEANT4_QT_DEBUG
[607]993 printf("G4UIQt::ExitHelp SHOULD NEVER GO HERE");
[608]994#endif
[607]995}
996
997
998/** Event filter method. Every event from QtApplication goes here.<br/>
999 We apply a filter only for the Up and Down Arrow press when the QLineEdit<br/>
1000 is active. If this filter match, Up arrow we give the previous command<br/>
1001 and Down arrow will give the next if exist.<br/>
1002 @param obj Emitter of the event
1003 @param event Kind of event
1004*/
1005bool G4UIQt::eventFilter( // Should stay with a minuscule eventFilter because of Qt
1006 QObject *aObj
1007,QEvent *aEvent
1008)
1009{
1010 if (aObj == NULL) return false;
1011 if (aEvent == NULL) return false;
1012
1013 if (aObj == fCommandHistoryArea) {
1014 if (aEvent->type() == QEvent::KeyPress) {
1015 fCommandArea->setFocus();
1016 }
1017 }
1018 if (aObj == fCommandArea) {
1019 if (aEvent->type() == QEvent::KeyPress) {
1020 QKeyEvent *e = static_cast<QKeyEvent*>(aEvent);
1021 if ((e->key() == (Qt::Key_Down)) ||
1022 (e->key() == (Qt::Key_PageDown)) ||
1023 (e->key() == (Qt::Key_Up)) ||
1024 (e->key() == (Qt::Key_PageUp))) {
1025#if QT_VERSION < 0x040000
1026 // count rows...
1027 QListViewItem* tmpItem = fCommandHistoryArea->firstChild();
1028 int selection = -1;
1029 int index = 0;
1030 while (tmpItem != 0) {
1031 if (tmpItem == fCommandHistoryArea->selectedItem()) {
1032 selection = index;
1033 }
1034 index ++;
1035 tmpItem = tmpItem->nextSibling();
1036 }
1037 if (fCommandHistoryArea->childCount()) {
1038 if (selection == -1) {
1039 selection = fCommandHistoryArea->childCount()-1;
1040 } else {
1041 if (e->key() == (Qt::Key_Down)) {
1042 if (selection <(fCommandHistoryArea->childCount()-1))
1043 selection++;
1044 } else if (e->key() == (Qt::Key_PageDown)) {
1045 selection = fCommandHistoryArea->childCount()-1;
1046#else
1047 int selection = fCommandHistoryArea->currentRow();
1048 if (fCommandHistoryArea->count()) {
1049 if (selection == -1) {
1050 selection = fCommandHistoryArea->count()-1;
1051 } else {
1052 if (e->key() == (Qt::Key_Down)) {
1053 if (selection <(fCommandHistoryArea->count()-1))
1054 selection++;
1055 } else if (e->key() == (Qt::Key_PageDown)) {
1056 selection = fCommandHistoryArea->count()-1;
1057#endif
1058 } else if (e->key() == (Qt::Key_Up)) {
1059 if (selection >0)
1060 selection --;
1061 } else if (e->key() == (Qt::Key_PageUp)) {
1062 selection = 0;
1063 }
1064 }
1065 fCommandHistoryArea->clearSelection();
1066#if QT_VERSION < 0x040000
1067 QListViewItem* tmpItem = fCommandHistoryArea->firstChild();
1068 int index = 0;
1069 while (tmpItem != 0) {
1070 if (index == selection) {
1071 tmpItem->setSelected(true);
1072 fCommandHistoryArea->setCurrentItem(tmpItem);
1073 }
1074 index ++;
1075 tmpItem = tmpItem->nextSibling();
1076 }
1077#else
1078#if QT_VERSION < 0x040202
1079 fCommandHistoryArea->setItemSelected(fCommandHistoryArea->item(selection),true);
1080#else
1081 fCommandHistoryArea->item(selection)->setSelected(true);
1082#endif
1083 fCommandHistoryArea->setCurrentItem(fCommandHistoryArea->item(selection));
1084#endif
1085 }
1086 } else if (e->key() == (Qt::Key_Tab)) {
1087#if QT_VERSION < 0x040000
1088 G4String ss = Complete(fCommandArea->text().ascii());
1089#else
1090 G4String ss = Complete(fCommandArea->text().toStdString().c_str());
1091#endif
1092 fCommandArea->setText((char*)(ss.data()));
1093
1094 // do not pass by parent, it will disable widget tab focus !
1095 return true;
1096 }
1097 }
1098 }
1099 // pass the event on to the parent class
1100 return QObject::eventFilter(aObj, aEvent);
1101}
1102
1103
1104
1105
1106/***************************************************************************/
1107//
1108// SLOTS DEFINITIONS
1109//
1110/***************************************************************************/
1111
1112/** Called when user give "help" command.
1113*/
1114void G4UIQt::ShowHelpCallback (
1115)
1116{
1117 TerminalHelp("");
1118}
1119
1120
1121/** Called when user click on clear button. Clear the text Output area
1122*/
1123void G4UIQt::ClearButtonCallback (
1124)
1125{
1126 fTextArea->clear();
1127}
1128
[608]1129/** Called when user exit session
1130*/
1131void G4UIQt::ExitSession (
1132)
1133{
1134 SessionTerminate();
1135}
[607]1136
[608]1137
[607]1138/** Callback call when "click on a menu entry.<br>
1139 Send the associated command to geant4
1140*/
1141void G4UIQt::CommandEnteredCallback (
1142)
1143{
1144#if QT_VERSION < 0x040000
1145 G4String command (fCommandArea->text().ascii());
1146 if (fCommandArea->text().simplifyWhiteSpace() != "") {
1147
1148 QListViewItem *newItem = new QListViewItem(fCommandHistoryArea);
1149 newItem->setText(0,fCommandArea->text());
1150#else
1151 G4String command (fCommandArea->text().toStdString().c_str());
1152 if (fCommandArea->text().trimmed() != "") {
1153 fCommandHistoryArea->addItem(fCommandArea->text());
1154#endif
1155 fCommandHistoryArea->clearSelection();
1156 fCommandHistoryArea->setCurrentItem(NULL);
1157 fCommandArea->setText("");
1158
1159 G4Qt* interactorManager = G4Qt::getInstance ();
1160 if (interactorManager) {
1161 interactorManager->FlushAndWaitExecution();
1162 }
1163 if (command(0,4) != "help") {
1164 ApplyShellCommand (command,exitSession,exitPause);
1165 } else {
1166 TerminalHelp(command);
1167 }
[608]1168#ifdef GEANT4_QT_DEBUG
[607]1169 printf("after \n");
[608]1170#endif
[607]1171 if(exitSession==true)
1172 SessionTerminate();
1173 }
1174}
1175
1176
1177/** Callback call when "enter" clicked on the command zone.<br>
1178 Send the command to geant4
1179 @param aCommand
1180*/
1181void G4UIQt::ButtonCallback (
1182 const QString& aCommand
1183)
1184{
1185#if QT_VERSION < 0x040000
1186 G4String ss = G4String(aCommand.ascii());
1187#else
1188 G4String ss = G4String(aCommand.toStdString().c_str());
1189#endif
1190 ApplyShellCommand(ss,exitSession,exitPause);
1191 if(exitSession==true)
1192 SessionTerminate();
1193}
1194
1195
1196
1197/** This callback is activated when user selected a item in the help tree
1198*/
1199void G4UIQt::HelpTreeClicCallback (
1200)
1201{
[617]1202 // printf("G4UIQt::HelpTreeClicCallback");
[607]1203#if QT_VERSION < 0x040000
1204 QListViewItem* item = NULL;
1205#else
1206 QTreeWidgetItem* item = NULL;
1207#endif
1208 if (!fHelpTreeWidget)
1209 return ;
1210
1211 if (!fHelpArea)
1212 return;
1213
1214#if QT_VERSION < 0x040000
1215 item =fHelpTreeWidget->selectedItem();
1216#else
1217 QList<QTreeWidgetItem *> list =fHelpTreeWidget->selectedItems();
1218 if (list.isEmpty())
1219 return;
1220 item = list.first();
1221#endif
1222 if (!item)
1223 return;
1224
1225 G4UImanager* UI = G4UImanager::GetUIpointer();
1226 if(UI==NULL) return;
1227 G4UIcommandTree * treeTop = UI->GetTree();
1228#if QT_VERSION < 0x040000
1229 G4UIcommand* command = treeTop->FindPath(item->text (1).ascii());
1230#else
1231 G4UIcommand* command = treeTop->FindPath(item->text (1).toStdString().c_str());
1232#endif
1233 if (command) {
1234#if QT_VERSION >= 0x040000
1235#if QT_VERSION < 0x040200
1236 fHelpArea->clear();
1237 fHelpArea->append(GetCommandList(command));
1238#else
1239 fHelpArea->setText(GetCommandList(command));
1240#endif
1241#else
1242 fHelpArea->setText(GetCommandList(command));
1243#endif
1244 } else {
1245 // this is not a command, this is a sub directory
1246 // We display the Title
1247#if QT_VERSION >= 0x040000
1248#if QT_VERSION < 0x040200
1249 fHelpArea->clear();
1250 fHelpArea->append(item->text (1));
1251#else
1252 fHelpArea->setText(item->text (1));
1253#endif
1254#else
1255 fHelpArea->setText(item->text (1));
1256#endif
1257 }
1258}
1259
1260/** This callback is activated when user double clic on a item in the help tree
1261*/
1262void G4UIQt::HelpTreeDoubleClicCallback (
1263#if QT_VERSION < 0x040000
1264QListViewItem* item
1265#else
1266QTreeWidgetItem* item
1267#endif
[617]1268 ,int
[607]1269)
1270{
[608]1271#ifdef GEANT4_QT_DEBUG
[607]1272 printf("G4UIQt::HelpTreeDoubleClicCallback");
[608]1273#endif
[607]1274 HelpTreeClicCallback();
1275 fCommandArea->setText(item->text (1));
1276}
1277
1278
1279/** Callback called when user select an old command in the command history<br>
1280 Give it to the command area.
1281*/
1282void G4UIQt::CommandHistoryCallback(
1283)
1284{
1285#if QT_VERSION < 0x040000
1286 QListViewItem* item = NULL;
1287#else
1288 QListWidgetItem* item = NULL;
1289#endif
1290 if (!fCommandHistoryArea)
1291 return ;
1292
1293
1294#if QT_VERSION < 0x040000
1295 item =fHelpTreeWidget->selectedItem();
1296#else
1297 QList<QListWidgetItem *> list =fCommandHistoryArea->selectedItems();
1298 if (list.isEmpty())
1299 return;
1300 item = list.first();
1301#endif
1302 if (!item)
1303 return;
1304#if QT_VERSION < 0x040000
1305 fCommandArea->setText(item->text(0));
1306#else
1307 fCommandArea->setText(item->text());
1308#endif
1309
1310}
1311
1312#endif
Note: See TracBrowser for help on using the repository browser.