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

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

modifs pour Qt3

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