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

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

Erreur pour Qt4 corrigee

  • Property svn:mime-type set to text/cpp
File size: 35.4 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);
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->installEventFilter(this);
174 fCommandLabel = new QLabel("",bottomWidget);
175
176 fCommandArea = new QLineEdit(bottomWidget);
177 fCommandArea->installEventFilter(this);
178#ifdef GEANT4_QT_DEBUG
179 printf("G4UIQt:: 1\n");
180#endif
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
197#ifdef GEANT4_QT_DEBUG
198 printf("G4UIQt:: 2\n");
199#endif
200
201#ifdef GEANT4_QT_DEBUG
202 printf("G4UIQt:: 3\n");
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
213#ifdef GEANT4_QT_DEBUG
214 printf("G4UIQt:: 4\n");
215#endif
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
227#ifdef GEANT4_QT_DEBUG
228 printf("G4UIQt:: 5\n");
229#endif
230 fMainWindow->setCentralWidget(splitter);
231
232#if QT_VERSION < 0x040000
233
234 // Add a quit subMenu
235 QPopupMenu *fileMenu = new QPopupMenu( fMainWindow);
236 fileMenu->insertItem( "&Quitter", this, SLOT(ExitSession()), CTRL+Key_Q );
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
244#ifdef GEANT4_QT_DEBUG
245 printf("G4UIQt:: 6\n");
246#endif
247
248#else
249
250 // Add a quit subMenu
251 QMenu *fileMenu = fMainWindow->menuBar()->addMenu("File");
252 fileMenu->addAction("Quitter", this, SLOT(ExitSession()));
253
254 // Add a Help menu
255 QMenu *helpMenu = fMainWindow->menuBar()->addMenu("Help");
256 helpMenu->addAction("Show Help", this, SLOT(ShowHelpCallback()));
257#endif
258#ifdef GEANT4_QT_DEBUG
259 printf("G4UIQt:: 7\n");
260#endif
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 }
273#ifdef GEANT4_QT_DEBUG
274 printf("G4UIQt:: 8\n");
275#endif
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
306#if QT_VERSION >= 0x040200
307 fMainWindow->setVisible(true);
308#else
309 fMainWindow->show();
310#endif
311#else
312 fMainWindow->show();
313#endif
314 Prompt("session");
315 exitSession = false;
316
317
318#ifdef GEANT4_QT_DEBUG
319 printf("disable secondary loop\n");
320#endif
321 interactorManager->DisableSecondaryLoop (); // TO KEEP
322 if ((QApplication*)interactorManager->GetMainInteractor())
323 ((QApplication*)interactorManager->GetMainInteractor())->exec();
324
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 ();
335#ifdef GEANT4_QT_DEBUG
336 printf("enable secondary loop\n");
337#endif
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
381#ifdef GEANT4_QT_DEBUG
382 printf("G4UIQt::PauseSessionStart\n");
383#endif
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
406#ifdef GEANT4_QT_DEBUG
407 printf("G4UIQt::SecondaryLoop\n");
408#endif
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{
489 if (aName == NULL) return;
490 if (aLabel == NULL) return;
491
492#if QT_VERSION < 0x040000
493 QPopupMenu *fileMenu = new QPopupMenu( fMainWindow);
494 fMainWindow->menuBar()->insertItem( aLabel, fileMenu );
495#else
496 QMenu *fileMenu = new QMenu(aLabel);
497 fMainWindow->menuBar()->insertMenu(fMainWindow->menuBar()->actions().last(),fileMenu);
498#endif
499
500 AddInteractor (aName,(G4Interactor)fileMenu);
501}
502
503
504/**
505 Add a new button to a menu
506 @param aMenu : parent menu
507 @param aLabel : label to display
508 @param aCommand : command to execute as a callback
509 */
510void G4UIQt::AddButton (
511 const char* aMenu
512,const char* aLabel
513,const char* aCommand
514)
515{
516 if(aMenu==NULL) return; // TO KEEP
517 if(aLabel==NULL) return; // TO KEEP
518 if(aCommand==NULL) return; // TO KEEP
519
520#if QT_VERSION < 0x040000
521 QPopupMenu *parent = (QPopupMenu*)GetInteractor(aMenu);
522#else
523 QMenu *parent = (QMenu*)GetInteractor(aMenu);
524#endif
525
526 if(parent==NULL) return;
527
528 QSignalMapper *signalMapper = new QSignalMapper(this);
529#if QT_VERSION < 0x040000
530 QAction *action = new QAction(QString(aLabel),QKeySequence::QKeySequence (),signalMapper, SLOT(map()));
531 action->addTo(parent);
532#else
533 QAction *action = parent->addAction(aLabel, signalMapper, SLOT(map()));
534#endif
535 signalMapper->setMapping(action, QString(aCommand));
536 connect(signalMapper, SIGNAL(mapped(const QString &)),this, SLOT(ButtonCallback(const QString&)));
537}
538
539
540
541
542/**
543 Open the help dialog in a separate window.<br>
544 This will be display as a tree widget.<br>
545 Implementation of <b>void G4VBasicShell::TerminalHelp(G4String newCommand)</b>
546
547 @param newCommand : open the tree widget item on this command if is set
548*/
549void G4UIQt::TerminalHelp(
550 G4String newCommand
551)
552{
553 // Create the help dialog
554 if (!fHelpDialog) {
555#if QT_VERSION < 0x040000
556 fHelpDialog = new QDialog(fMainWindow,0,FALSE,Qt::WStyle_Title | Qt::WStyle_SysMenu | Qt::WStyle_MinMax );
557#else
558 fHelpDialog = new QDialog(fMainWindow,Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
559#endif
560 QSplitter *splitter = new QSplitter(Qt::Horizontal);
561 QPushButton *exitButton = new QPushButton("Exit",fMainWindow);
562 connect(exitButton, SIGNAL(clicked()), fHelpDialog,SLOT(close()));
563
564 // the help tree
565 G4UImanager* UI = G4UImanager::GetUIpointer();
566 if(UI==NULL) return;
567 G4UIcommandTree * treeTop = UI->GetTree();
568
569 // build widget
570#if QT_VERSION < 0x040000
571 fHelpTreeWidget = new QListView(splitter);
572 fHelpTreeWidget->setSelectionMode(QListView::Single);
573 fHelpTreeWidget->addColumn("Command");
574 fHelpTreeWidget->addColumn("Description");
575 fHelpTreeWidget->hideColumn(1);
576 fHelpTreeWidget->header()->setResizeEnabled(FALSE,1);
577 // QList<QListViewItem *> items;
578#else
579 fHelpTreeWidget = new QTreeWidget();
580 fHelpTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
581 fHelpTreeWidget->setColumnCount(2);
582 fHelpTreeWidget->setColumnHidden(1,true);
583 QStringList labels;
584 labels << QString("Command") << QString("Description");
585 fHelpTreeWidget->setHeaderLabels(labels);
586 // QList<QTreeWidgetItem *> items;
587#endif
588
589#if QT_VERSION < 0x040000
590#ifdef GEANT4_QT_DEBUG
591 printf("G4UIQt::QTextEdit 1\n");
592#endif
593 fHelpArea = new QTextEdit(splitter);
594#ifdef GEANT4_QT_DEBUG
595 printf("G4UIQt::end 1\n");
596#endif
597#else
598#ifdef GEANT4_QT_DEBUG
599 printf("G4UIQt::QTextEdit 2\n");
600#endif
601 fHelpArea = new QTextEdit();
602#ifdef GEANT4_QT_DEBUG
603 printf("G4UIQt::end 2\n");
604#endif
605#endif
606 fHelpArea->setReadOnly(true);
607
608 G4int treeSize = treeTop->GetTreeEntry();
609#if QT_VERSION < 0x040000
610 QListViewItem * newItem;
611#else
612 QTreeWidgetItem * newItem;
613#endif
614 for (int a=0;a<treeSize;a++) {
615 // Creating new item
616
617#if QT_VERSION < 0x040000
618 newItem = new QListViewItem(fHelpTreeWidget);
619 newItem->setText(0,QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).simplifyWhiteSpace());
620 newItem->setText(1,QString((char*)(treeTop->GetTree(a+1)->GetTitle()).data()).simplifyWhiteSpace());
621#else
622 //FIXME : Qt 4.2
623 // QStringList stringList;
624 // stringList << QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).trimmed() ;
625 // stringList << QString((char*)(treeTop->GetTree(a+1)->GetTitle()).data()).trimmed() ;
626 // newItem = new QTreeWidgetItem(stringList);
627 // FIXME : Qt 4.0
628 newItem = new QTreeWidgetItem(fHelpTreeWidget);
629 newItem->setText(0,QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).trimmed());
630 newItem->setText(1,QString((char*)(treeTop->GetTree(a+1)->GetTitle()).data()).trimmed());
631#endif
632
633
634 // look for childs
635 CreateChildTree(newItem,treeTop->GetTree(a+1));
636 // items.append(newItem);
637 }
638
639 connect(fHelpTreeWidget, SIGNAL(itemSelectionChanged ()),this, SLOT(HelpTreeClicCallback()));
640 connect(fHelpTreeWidget, SIGNAL(itemDoubleClicked (QTreeWidgetItem*,int)),this, SLOT(HelpTreeDoubleClicCallback(QTreeWidgetItem*,int)));
641
642 // Set layouts
643
644#if QT_VERSION < 0x040000
645 QVBoxLayout *vLayout = new QVBoxLayout(fHelpDialog);
646#else
647 QVBoxLayout *vLayout = new QVBoxLayout;
648 splitter->addWidget(fHelpTreeWidget);
649 splitter->addWidget(fHelpArea);
650#endif
651
652
653 vLayout->addWidget(splitter);
654 vLayout->addWidget(exitButton);
655#if QT_VERSION >= 0x040000
656 fHelpDialog->setLayout(vLayout);
657#endif
658
659 }
660
661 // Look for the choosen command "newCommand"
662 size_t i = newCommand.index(" ");
663 G4String targetCom="";
664 if( i != std::string::npos )
665 {
666 G4String newValue = newCommand(i+1,newCommand.length()-(i+1));
667 newValue.strip(G4String::both);
668 targetCom = ModifyToFullPathCommand( newValue );
669 }
670 if (targetCom != "") {
671#if QT_VERSION < 0x040000
672 QListViewItem* findItem = NULL;
673 QListViewItem* tmpItem = fHelpTreeWidget->firstChild();
674 while (tmpItem != 0) {
675 if (!findItem) {
676 findItem = FindTreeItem(tmpItem,QString((char*)targetCom.data()));
677 }
678 tmpItem = tmpItem->nextSibling();
679 }
680#else
681 QTreeWidgetItem* findItem = NULL;
682 for (int a=0;a<fHelpTreeWidget->topLevelItemCount();a++) {
683 if (!findItem) {
684 findItem = FindTreeItem(fHelpTreeWidget->topLevelItem(a),QString((char*)targetCom.data()));
685 }
686 }
687#endif
688
689 if (findItem) {
690
691 //collapsed open item
692#if QT_VERSION < 0x040000
693
694 // FIXME : Has to be checked
695 QListViewItem* tmpItem = fHelpTreeWidget->firstChild();
696 QList<QListViewItem> openItems;
697 while ((tmpItem != 0) || (!openItems.isEmpty())) {
698 if (tmpItem->isOpen() ) {
699 tmpItem->setOpen(false);
700 openItems.append(tmpItem);
701 tmpItem = tmpItem->firstChild();
702 } else {
703 tmpItem = tmpItem->nextSibling();
704 }
705 if (tmpItem == 0) {
706 tmpItem = openItems.take(openItems.count()-1);
707 }
708 }
709#else
710 QList<QTreeWidgetItem *> selected;
711
712 selected = fHelpTreeWidget->selectedItems();
713 if ( selected.count() != 0 ) {
714 QTreeWidgetItem * tmp =selected.at( 0 );
715 while ( tmp) {
716#if QT_VERSION < 0x040202
717 fHelpTreeWidget->setItemExpanded(tmp,false);
718#else
719 tmp->setExpanded(false);
720#endif
721 tmp = tmp->parent();
722 }
723 }
724#endif
725
726 // clear old selection
727 fHelpTreeWidget->clearSelection();
728
729 // set new selection
730#if QT_VERSION >= 0x040000
731#if QT_VERSION < 0x040202
732 fHelpTreeWidget->setItemSelected(findItem,true);
733#else
734 findItem->setSelected(true);
735#endif
736#else
737 findItem->setSelected(true);
738#endif
739
740 // expand parent item
741 while ( findItem) {
742#if QT_VERSION < 0x040000
743 findItem->setOpen(true);
744#else
745#if QT_VERSION < 0x040202
746 fHelpTreeWidget->setItemExpanded(findItem,true);
747#else
748 findItem->setExpanded(true);
749#endif
750#endif
751 findItem = findItem->parent();
752 }
753
754 // Call the update of the right textArea
755 HelpTreeClicCallback();
756 }
757 }
758#if QT_VERSION < 0x040000
759 fHelpDialog->setCaption( tr( "Help on commands" ));
760#else
761 fHelpDialog->setWindowTitle(tr("Help on commands"));
762#endif
763 fHelpDialog->resize(800,600);
764 fHelpDialog->move(QPoint(400,150));
765 fHelpDialog->show();
766 fHelpDialog->raise();
767#if QT_VERSION < 0x040000
768 fHelpDialog->setActiveWindow();
769#else
770 fHelpDialog->activateWindow();
771#endif
772}
773
774
775
776/** Fill the Help Tree Widget
777 @param aParent : parent item to fill
778 @param aCommandTree : commandTree node associate with this part of the Tree
779*/
780#if QT_VERSION < 0x040000
781void G4UIQt::CreateChildTree(
782 QListViewItem *aParent
783,G4UIcommandTree *aCommandTree
784#else
785void G4UIQt::CreateChildTree(
786 QTreeWidgetItem *aParent
787,G4UIcommandTree *aCommandTree
788#endif
789)
790{
791 if (aParent == NULL) return;
792 if (aCommandTree == NULL) return;
793
794
795 // Creating new item
796#if QT_VERSION < 0x040000
797 QListViewItem * newItem;
798#else
799 QTreeWidgetItem * newItem;
800#endif
801
802 // Get the Sub directories
803 for (int a=0;a<aCommandTree->GetTreeEntry();a++) {
804
805#if QT_VERSION < 0x040000
806 newItem = new QListViewItem(aParent);
807 newItem->setText(0,QString((char*)(aCommandTree->GetTree(a+1)->GetPathName()).data()).simplifyWhiteSpace());
808 newItem->setText(1,QString((char*)(aCommandTree->GetTree(a+1)->GetTitle()).data()).simplifyWhiteSpace());
809
810#else
811 newItem = new QTreeWidgetItem(aParent);
812 newItem->setText(0,QString((char*)(aCommandTree->GetTree(a+1)->GetPathName()).data()).trimmed());
813 newItem->setText(1,QString((char*)(aCommandTree->GetTree(a+1)->GetTitle()).data()).trimmed());
814#endif
815
816 CreateChildTree(newItem,aCommandTree->GetTree(a+1));
817 }
818
819
820
821 // Get the Commands
822
823 for (int a=0;a<aCommandTree->GetCommandEntry();a++) {
824
825 QStringList stringList;
826#if QT_VERSION < 0x040000
827 newItem = new QListViewItem(aParent);
828 newItem->setText(0,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).simplifyWhiteSpace());
829 newItem->setText(1,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).simplifyWhiteSpace());
830 newItem->setOpen(false);
831
832#else
833 newItem = new QTreeWidgetItem(aParent);
834 newItem->setText(0,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).trimmed());
835 newItem->setText(1,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).trimmed());
836#if QT_VERSION < 0x040202
837 fHelpTreeWidget->setItemExpanded(newItem,false);
838#else
839 newItem->setExpanded(false);
840#endif
841#endif
842
843 }
844}
845
846
847/** Find a treeItemWidget in the help tree
848 @param aCommand item's String to look for
849 @return item if found, NULL if not
850*/
851#if QT_VERSION < 0x040000
852QListViewItem* G4UIQt::FindTreeItem(
853 QListViewItem *aParent
854#else
855QTreeWidgetItem* G4UIQt::FindTreeItem(
856 QTreeWidgetItem *aParent
857#endif
858,const QString& aCommand
859)
860{
861 if (aParent == NULL) return NULL;
862
863 if (aParent->text(0) == aCommand)
864 return aParent;
865
866#if QT_VERSION < 0x040000
867 QListViewItem * tmp = NULL;
868 QListViewItem* tmpItem = aParent->firstChild();
869 while (tmpItem != 0) {
870 if (!tmp)
871 tmp = FindTreeItem(tmpItem,aCommand);
872 tmpItem = tmpItem->nextSibling();
873 }
874
875#else
876 QTreeWidgetItem * tmp = NULL;
877 for (int a=0;a<aParent->childCount();a++) {
878 if (!tmp)
879 tmp = FindTreeItem(aParent->child(a),aCommand);
880 }
881#endif
882 return tmp;
883}
884
885
886/** Build the command list parameters in a QString<br>
887 Reimplement partialy the G4UIparameter.cc
888 @param aCommand : command to list parameters
889 @see G4UIparameter::List()
890 @see G4UIcommand::List()
891 @return the command list parameters, or "" if nothing
892*/
893QString G4UIQt::GetCommandList (
894 const G4UIcommand *aCommand
895)
896{
897
898 QString txt ="";
899 if (aCommand == NULL)
900 return txt;
901
902 G4String commandPath = aCommand->GetCommandPath();
903 G4String rangeString = aCommand->GetRange();
904 G4int n_guidanceEntry = aCommand->GetGuidanceEntries();
905 G4int n_parameterEntry = aCommand->GetParameterEntries();
906
907 if ((commandPath == "") &&
908 (rangeString == "") &&
909 (n_guidanceEntry == 0) &&
910 (n_parameterEntry == 0)) {
911 return txt;
912 }
913
914 if((commandPath.length()-1)!='/') {
915 txt += "Command " + QString((char*)(commandPath).data()) + "\n";
916 }
917 txt += "Guidance :\n";
918
919 for( G4int i_thGuidance=0; i_thGuidance < n_guidanceEntry; i_thGuidance++ ) {
920 txt += QString((char*)(aCommand->GetGuidanceLine(i_thGuidance)).data()) + "\n";
921 }
922 if( ! rangeString.isNull() ) {
923 txt += " Range of parameters : " + QString((char*)(rangeString).data()) + "\n";
924 }
925 if( n_parameterEntry > 0 ) {
926 G4UIparameter *param;
927
928 // Re-implementation of G4UIparameter.cc
929
930 for( G4int i_thParameter=0; i_thParameter<n_parameterEntry; i_thParameter++ ) {
931 param = aCommand->GetParameter(i_thParameter);
932 txt += "\nParameter : " + QString((char*)(param->GetParameterName()).data()) + "\n";
933 if( ! param->GetParameterGuidance().isNull() )
934 txt += QString((char*)(param->GetParameterGuidance()).data())+ "\n" ;
935 txt += " Parameter type : " + QString(QChar(param->GetParameterType())) + "\n";
936 if(param->IsOmittable()){
937 txt += " Omittable : True\n";
938 } else {
939 txt += " Omittable : False\n";
940 }
941 if( param->GetCurrentAsDefault() ) {
942 txt += " Default value : taken from the current value\n";
943 } else if( ! param->GetDefaultValue().isNull() ) {
944 txt += " Default value : " + QString((char*)(param->GetDefaultValue()).data())+ "\n";
945 }
946 if( ! param->GetParameterRange().isNull() ) {
947 txt += " Parameter range : " + QString((char*)(param->GetParameterRange()).data())+ "\n";
948 }
949 if( ! param->GetParameterCandidates().isNull() ) {
950 txt += " Candidates : " + QString((char*)(param->GetParameterCandidates()).data())+ "\n";
951 }
952 }
953 }
954 return txt;
955}
956
957
958
959/** Implement G4VBasicShell vurtual function
960 */
961G4bool G4UIQt::GetHelpChoice(
962 G4int& aInt
963)
964{
965#ifdef GEANT4_QT_DEBUG
966 printf("G4UIQt::GetHelpChoice SHOULD NEVER GO HERE");
967#endif
968 return true;
969}
970
971
972/** Implement G4VBasicShell vurtual function
973*/
974void G4UIQt::ExitHelp(
975)
976{
977#ifdef GEANT4_QT_DEBUG
978 printf("G4UIQt::ExitHelp SHOULD NEVER GO HERE");
979#endif
980}
981
982
983/** Event filter method. Every event from QtApplication goes here.<br/>
984 We apply a filter only for the Up and Down Arrow press when the QLineEdit<br/>
985 is active. If this filter match, Up arrow we give the previous command<br/>
986 and Down arrow will give the next if exist.<br/>
987 @param obj Emitter of the event
988 @param event Kind of event
989*/
990bool G4UIQt::eventFilter( // Should stay with a minuscule eventFilter because of Qt
991 QObject *aObj
992,QEvent *aEvent
993)
994{
995 if (aObj == NULL) return false;
996 if (aEvent == NULL) return false;
997
998 if (aObj == fCommandHistoryArea) {
999 if (aEvent->type() == QEvent::KeyPress) {
1000 fCommandArea->setFocus();
1001 }
1002 }
1003 if (aObj == fCommandArea) {
1004 if (aEvent->type() == QEvent::KeyPress) {
1005 QKeyEvent *e = static_cast<QKeyEvent*>(aEvent);
1006 if ((e->key() == (Qt::Key_Down)) ||
1007 (e->key() == (Qt::Key_PageDown)) ||
1008 (e->key() == (Qt::Key_Up)) ||
1009 (e->key() == (Qt::Key_PageUp))) {
1010#if QT_VERSION < 0x040000
1011 // count rows...
1012 QListViewItem* tmpItem = fCommandHistoryArea->firstChild();
1013 int selection = -1;
1014 int index = 0;
1015 while (tmpItem != 0) {
1016 if (tmpItem == fCommandHistoryArea->selectedItem()) {
1017 selection = index;
1018 }
1019 index ++;
1020 tmpItem = tmpItem->nextSibling();
1021 }
1022 if (fCommandHistoryArea->childCount()) {
1023 if (selection == -1) {
1024 selection = fCommandHistoryArea->childCount()-1;
1025 } else {
1026 if (e->key() == (Qt::Key_Down)) {
1027 if (selection <(fCommandHistoryArea->childCount()-1))
1028 selection++;
1029 } else if (e->key() == (Qt::Key_PageDown)) {
1030 selection = fCommandHistoryArea->childCount()-1;
1031#else
1032 int selection = fCommandHistoryArea->currentRow();
1033 if (fCommandHistoryArea->count()) {
1034 if (selection == -1) {
1035 selection = fCommandHistoryArea->count()-1;
1036 } else {
1037 if (e->key() == (Qt::Key_Down)) {
1038 if (selection <(fCommandHistoryArea->count()-1))
1039 selection++;
1040 } else if (e->key() == (Qt::Key_PageDown)) {
1041 selection = fCommandHistoryArea->count()-1;
1042#endif
1043 } else if (e->key() == (Qt::Key_Up)) {
1044 if (selection >0)
1045 selection --;
1046 } else if (e->key() == (Qt::Key_PageUp)) {
1047 selection = 0;
1048 }
1049 }
1050 fCommandHistoryArea->clearSelection();
1051#if QT_VERSION < 0x040000
1052 QListViewItem* tmpItem = fCommandHistoryArea->firstChild();
1053 int index = 0;
1054 while (tmpItem != 0) {
1055 if (index == selection) {
1056 tmpItem->setSelected(true);
1057 fCommandHistoryArea->setCurrentItem(tmpItem);
1058 }
1059 index ++;
1060 tmpItem = tmpItem->nextSibling();
1061 }
1062#else
1063#if QT_VERSION < 0x040202
1064 fCommandHistoryArea->setItemSelected(fCommandHistoryArea->item(selection),true);
1065#else
1066 fCommandHistoryArea->item(selection)->setSelected(true);
1067#endif
1068 fCommandHistoryArea->setCurrentItem(fCommandHistoryArea->item(selection));
1069#endif
1070 }
1071 } else if (e->key() == (Qt::Key_Tab)) {
1072#if QT_VERSION < 0x040000
1073 G4String ss = Complete(fCommandArea->text().ascii());
1074#else
1075 G4String ss = Complete(fCommandArea->text().toStdString().c_str());
1076#endif
1077 fCommandArea->setText((char*)(ss.data()));
1078
1079 // do not pass by parent, it will disable widget tab focus !
1080 return true;
1081 }
1082 }
1083 }
1084 // pass the event on to the parent class
1085 return QObject::eventFilter(aObj, aEvent);
1086}
1087
1088
1089
1090
1091/***************************************************************************/
1092//
1093// SLOTS DEFINITIONS
1094//
1095/***************************************************************************/
1096
1097/** Called when user give "help" command.
1098*/
1099void G4UIQt::ShowHelpCallback (
1100)
1101{
1102 TerminalHelp("");
1103}
1104
1105
1106/** Called when user click on clear button. Clear the text Output area
1107*/
1108void G4UIQt::ClearButtonCallback (
1109)
1110{
1111 fTextArea->clear();
1112}
1113
1114/** Called when user exit session
1115*/
1116void G4UIQt::ExitSession (
1117)
1118{
1119 SessionTerminate();
1120}
1121
1122
1123/** Callback call when "click on a menu entry.<br>
1124 Send the associated command to geant4
1125*/
1126void G4UIQt::CommandEnteredCallback (
1127)
1128{
1129#if QT_VERSION < 0x040000
1130 G4String command (fCommandArea->text().ascii());
1131 if (fCommandArea->text().simplifyWhiteSpace() != "") {
1132
1133 QListViewItem *newItem = new QListViewItem(fCommandHistoryArea);
1134 newItem->setText(0,fCommandArea->text());
1135#else
1136 G4String command (fCommandArea->text().toStdString().c_str());
1137 if (fCommandArea->text().trimmed() != "") {
1138 fCommandHistoryArea->addItem(fCommandArea->text());
1139#endif
1140 fCommandHistoryArea->clearSelection();
1141 fCommandHistoryArea->setCurrentItem(NULL);
1142 fCommandArea->setText("");
1143
1144 G4Qt* interactorManager = G4Qt::getInstance ();
1145 if (interactorManager) {
1146 interactorManager->FlushAndWaitExecution();
1147 }
1148 if (command(0,4) != "help") {
1149 ApplyShellCommand (command,exitSession,exitPause);
1150 } else {
1151 TerminalHelp(command);
1152 }
1153#ifdef GEANT4_QT_DEBUG
1154 printf("after \n");
1155#endif
1156 if(exitSession==true)
1157 SessionTerminate();
1158 }
1159}
1160
1161
1162/** Callback call when "enter" clicked on the command zone.<br>
1163 Send the command to geant4
1164 @param aCommand
1165*/
1166void G4UIQt::ButtonCallback (
1167 const QString& aCommand
1168)
1169{
1170#if QT_VERSION < 0x040000
1171 G4String ss = G4String(aCommand.ascii());
1172#else
1173 G4String ss = G4String(aCommand.toStdString().c_str());
1174#endif
1175 ApplyShellCommand(ss,exitSession,exitPause);
1176 if(exitSession==true)
1177 SessionTerminate();
1178}
1179
1180
1181
1182/** This callback is activated when user selected a item in the help tree
1183*/
1184void G4UIQt::HelpTreeClicCallback (
1185)
1186{
1187 // printf("G4UIQt::HelpTreeClicCallback");
1188#if QT_VERSION < 0x040000
1189 QListViewItem* item = NULL;
1190#else
1191 QTreeWidgetItem* item = NULL;
1192#endif
1193 if (!fHelpTreeWidget)
1194 return ;
1195
1196 if (!fHelpArea)
1197 return;
1198
1199#if QT_VERSION < 0x040000
1200 item =fHelpTreeWidget->selectedItem();
1201#else
1202 QList<QTreeWidgetItem *> list =fHelpTreeWidget->selectedItems();
1203 if (list.isEmpty())
1204 return;
1205 item = list.first();
1206#endif
1207 if (!item)
1208 return;
1209
1210 G4UImanager* UI = G4UImanager::GetUIpointer();
1211 if(UI==NULL) return;
1212 G4UIcommandTree * treeTop = UI->GetTree();
1213#if QT_VERSION < 0x040000
1214 G4UIcommand* command = treeTop->FindPath(item->text (1).ascii());
1215#else
1216 G4UIcommand* command = treeTop->FindPath(item->text (1).toStdString().c_str());
1217#endif
1218 if (command) {
1219#if QT_VERSION >= 0x040000
1220#if QT_VERSION < 0x040200
1221 fHelpArea->clear();
1222 fHelpArea->append(GetCommandList(command));
1223#else
1224 fHelpArea->setText(GetCommandList(command));
1225#endif
1226#else
1227 fHelpArea->setText(GetCommandList(command));
1228#endif
1229 } else {
1230 // this is not a command, this is a sub directory
1231 // We display the Title
1232#if QT_VERSION >= 0x040000
1233#if QT_VERSION < 0x040200
1234 fHelpArea->clear();
1235 fHelpArea->append(item->text (1));
1236#else
1237 fHelpArea->setText(item->text (1));
1238#endif
1239#else
1240 fHelpArea->setText(item->text (1));
1241#endif
1242 }
1243}
1244
1245/** This callback is activated when user double clic on a item in the help tree
1246*/
1247void G4UIQt::HelpTreeDoubleClicCallback (
1248#if QT_VERSION < 0x040000
1249QListViewItem* item
1250#else
1251QTreeWidgetItem* item
1252#endif
1253 ,int nb
1254)
1255{
1256#ifdef GEANT4_QT_DEBUG
1257 printf("G4UIQt::HelpTreeDoubleClicCallback");
1258#endif
1259 HelpTreeClicCallback();
1260 fCommandArea->setText(item->text (1));
1261}
1262
1263
1264/** Callback called when user select an old command in the command history<br>
1265 Give it to the command area.
1266*/
1267void G4UIQt::CommandHistoryCallback(
1268)
1269{
1270#if QT_VERSION < 0x040000
1271 QListViewItem* item = NULL;
1272#else
1273 QListWidgetItem* item = NULL;
1274#endif
1275 if (!fCommandHistoryArea)
1276 return ;
1277
1278
1279#if QT_VERSION < 0x040000
1280 item =fHelpTreeWidget->selectedItem();
1281#else
1282 QList<QListWidgetItem *> list =fCommandHistoryArea->selectedItems();
1283 if (list.isEmpty())
1284 return;
1285 item = list.first();
1286#endif
1287 if (!item)
1288 return;
1289#if QT_VERSION < 0x040000
1290 fCommandArea->setText(item->text(0));
1291#else
1292 fCommandArea->setText(item->text());
1293#endif
1294
1295}
1296
1297#endif
Note: See TracBrowser for help on using the repository browser.