source: trunk/source/interfaces/basic/src/G4UIQt.cc@ 1197

Last change on this file since 1197 was 1184, checked in by garnier, 16 years ago

modif dans CVS

  • Property svn:mime-type set to text/cpp
File size: 47.8 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.28 2009/11/05 14:34:18 lgarnier Exp $
28// GEANT4 tag $Name: $
29//
30// L. Garnier
31
32#ifdef G4UI_BUILD_QT_SESSION
33
34#include "G4Types.hh"
35
36#include <string.h>
37
38#include "G4UIQt.hh"
39#include "G4UImanager.hh"
40#include "G4StateManager.hh"
41#include "G4UIcommandTree.hh"
42#include "G4UIcommandStatus.hh"
43
44#include "G4Qt.hh"
45
46#include <qapplication.h>
47#include <qlineedit.h>
48#include <qwidget.h>
49#include <qmenubar.h>
50#include <qlayout.h>
51#include <qpushbutton.h>
52#include <qlabel.h>
53#include <qsplitter.h>
54#include <qscrollbar.h>
55#include <qdialog.h>
56#include <qevent.h>
57#include <qtextedit.h>
58#include <qsignalmapper.h>
59#include <qtabwidget.h>
60
61#include <qmainwindow.h>
62#if QT_VERSION >= 0x040000
63#include <qmenu.h>
64#include <qlistwidget.h>
65#include <qtreewidget.h>
66#else
67#include <qaction.h>
68#include <qheader.h>
69#include <qlistview.h>
70#include <qpopupmenu.h>
71#include <qwidgetlist.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,fTabWidget(NULL)
108,fCoutText("Output")
109{
110 G4Qt* interactorManager = G4Qt::getInstance (argc,argv,(char*)"Qt");
111 if (!(QApplication*)interactorManager->GetMainInteractor()) {
112 G4cout << "G4UIQt : Unable to init Qt. Aborted" << G4endl;
113 }
114
115 G4UImanager* UI = G4UImanager::GetUIpointer();
116 if(UI!=NULL) UI->SetSession(this);
117
118 // Check if already define in external app QMainWindow
119 bool found = false;
120#if QT_VERSION < 0x040000
121 // theses lines does nothing exept this one "GLWindow = new QDialog(0..."
122 // but if I comment them, it doesn't work...
123 QWidgetList *list = QApplication::allWidgets();
124 QWidgetListIt it( *list ); // iterate over the widgets
125 QWidget * widget;
126 while ( (widget=it.current()) != 0 ) { // for each widget...
127 ++it;
128 if ((found== false) && (widget->inherits("QMainWindow"))) {
129 found = true;
130 }
131 }
132 delete list; // delete the list, not the widgets
133#else
134 foreach (QWidget *widget, QApplication::allWidgets()) {
135 if ((found== false) && (widget->inherits("QMainWindow"))) {
136 found = true;
137 }
138 }
139#endif
140
141 if (found) {
142 G4cout << "G4UIQt : Found an external App with a QMainWindow already defined. Aborted" << G4endl;
143 return ;
144 }
145 fMainWindow = new QMainWindow();
146
147#ifdef G4DEBUG_INTERFACES_BASIC
148 printf("G4UIQt::Initialise after main window creation UImanager:%d G4UIQt(this) : %d+++++++++++\n",UI,this);
149#endif
150#if QT_VERSION < 0x040000
151 fMainWindow->setCaption( tr( "G4UI Session" ));
152 fMainWindow->resize(900,600);
153 fMainWindow->move(50,100);
154#else
155 fMainWindow->setWindowTitle( tr("G4UI Session") );
156 fMainWindow->resize(900,600);
157 fMainWindow->move(QPoint(50,100));
158#endif
159
160#ifdef G4DEBUG_INTERFACES_BASIC
161 printf("G4UIQt::G4UIQt :: 1\n");
162#endif
163 QWidget *mainWidget = new QWidget(fMainWindow);
164#if QT_VERSION < 0x040000
165 QSplitter *visHistorySplitter = new QSplitter(Qt::Vertical,fMainWindow);
166 QSplitter *historyCommandLineSplitter = new QSplitter(Qt::Vertical,fMainWindow);
167#else
168 QSplitter *visHistorySplitter = new QSplitter(Qt::Vertical,mainWidget);
169 QSplitter *historyCommandLineSplitter = new QSplitter(Qt::Vertical,mainWidget);
170#endif
171
172#ifdef G4DEBUG_INTERFACES_BASIC
173 printf("G4UIQt::G4UIQt :: 2\n");
174#endif
175 // Set layouts
176
177 fTabWidget = new QTabWidget;
178 connect(fTabWidget, SIGNAL(currentChanged ( int ) ), SLOT(UpdateTabWidget(int)));
179
180 visWidget = new QWidget(visHistorySplitter);
181 QWidget* coutWidget = new QWidget(fTabWidget);
182 QWidget* historyWidget = new QWidget(historyCommandLineSplitter);
183 QWidget* commandLineWidget = new QWidget(historyCommandLineSplitter);
184
185 // QVBoxLayout *layoutVis = new QVBoxLayout(visWidget);
186 QVBoxLayout *layoutCout = new QVBoxLayout(coutWidget);
187 QVBoxLayout *layoutHistory = new QVBoxLayout(historyWidget);
188 QVBoxLayout *layoutCommandLine = new QVBoxLayout(commandLineWidget);
189
190 // fill them
191
192 fTabWidget->addTab(coutWidget,fCoutText);
193
194 fTextArea = new QTextEdit(coutWidget);
195 QPushButton *clearButton = new QPushButton("clear",coutWidget);
196 connect(clearButton, SIGNAL(clicked()), SLOT(ClearButtonCallback()));
197
198#if QT_VERSION < 0x040000
199
200 fCommandHistoryArea = new QListView(historyWidget);
201 fCommandHistoryArea->setSorting (-1, FALSE);
202 fCommandHistoryArea->setSelectionMode(QListView::Single);
203 fCommandHistoryArea->addColumn("");
204 fCommandHistoryArea->header()->hide();
205 connect(fCommandHistoryArea, SIGNAL(selectionChanged()), SLOT(CommandHistoryCallback()));
206#else
207 fCommandHistoryArea = new QListWidget();
208 fCommandHistoryArea->setSelectionMode(QAbstractItemView::SingleSelection);
209 connect(fCommandHistoryArea, SIGNAL(itemSelectionChanged()), SLOT(CommandHistoryCallback()));
210#endif
211 fCommandHistoryArea->installEventFilter(this);
212 fCommandLabel = new QLabel("",commandLineWidget);
213
214#ifdef G4DEBUG_INTERFACES_BASIC
215 printf("G4UIQt::G4UIQt :: 3\n");
216#endif
217 fCommandArea = new QLineEdit(commandLineWidget);
218 fCommandArea->installEventFilter(this);
219#if QT_VERSION < 0x040000
220 fCommandArea->setActiveWindow();
221#else
222 fCommandArea->activateWindow();
223#endif
224 connect(fCommandArea, SIGNAL(returnPressed()), SLOT(CommandEnteredCallback()));
225#if QT_VERSION < 0x040000
226 fCommandArea->setFocusPolicy ( QWidget::StrongFocus );
227 fCommandArea->setFocus();
228#else
229 fCommandArea->setFocusPolicy ( Qt::StrongFocus );
230 fCommandArea->setFocus(Qt::TabFocusReason);
231#endif
232 fTextArea->setReadOnly(true);
233
234 // layoutVis->addWidget(visWidget);
235
236 layoutCout->addWidget(fTextArea);
237 layoutCout->addWidget(clearButton);
238
239#ifdef G4DEBUG_INTERFACES_BASIC
240 printf("G4UIQt::G4UIQt :: 4\n");
241#endif
242#if QT_VERSION >= 0x040000
243 // topWidget->setLayout(layoutTop);
244#endif
245
246 layoutHistory->addWidget(fCommandHistoryArea);
247 layoutCommandLine->addWidget(fCommandLabel);
248 layoutCommandLine->addWidget(fCommandArea);
249#if QT_VERSION >= 0x040000
250
251 // bottomWidget->setLayout(layoutBottom);
252 QVBoxLayout *mainLayout = new QVBoxLayout(mainWidget);
253
254 // the splitter for History/CommandLine
255 historyCommandLineSplitter->addWidget(historyWidget);
256 historyCommandLineSplitter->addWidget(commandLineWidget);
257
258 // The splitter for Vis/Cout
259 visHistorySplitter->addWidget(visWidget);
260 visHistorySplitter->addWidget(historyCommandLineSplitter);
261
262 mainLayout->addWidget(visHistorySplitter);
263
264#endif
265#ifdef G4DEBUG_INTERFACES_BASIC
266 printf("G4UIQt::G4UIQt :: 5\n");
267#endif
268
269#if QT_VERSION >= 0x040000
270 fMainWindow->setCentralWidget(mainWidget);
271#else
272 fMainWindow->setCentralWidget(visHistorySplitter);
273#endif
274
275#if QT_VERSION < 0x040000
276
277 // Add a quit subMenu
278 QPopupMenu *fileMenu = new QPopupMenu( fMainWindow);
279 fileMenu->insertItem( "&Quit", this, SLOT(ExitSession()), CTRL+Key_Q );
280 fMainWindow->menuBar()->insertItem( QString("&File"), fileMenu );
281
282 // Add a Help menu
283 QPopupMenu *helpMenu = new QPopupMenu( fMainWindow );
284 helpMenu->insertItem( "&Show Help", this, SLOT(ShowHelpCallback()), CTRL+Key_H );
285 fMainWindow->menuBar()->insertItem( QString("&Help"), helpMenu );
286
287
288#else
289
290 // Add a quit subMenu
291 QMenu *fileMenu = fMainWindow->menuBar()->addMenu("File");
292 fileMenu->addAction("Quit", this, SLOT(ExitSession()));
293
294 // Add a Help menu
295 QMenu *helpMenu = fMainWindow->menuBar()->addMenu("Help");
296 helpMenu->addAction("Show Help", this, SLOT(ShowHelpCallback()));
297#endif
298
299 AddInteractor ("file",(G4Interactor)fileMenu);
300 AddInteractor ("help",(G4Interactor)helpMenu);
301#ifdef G4DEBUG_INTERFACES_BASIC
302 printf("G4UIQt::G4UIQt :: 6\n");
303#endif
304
305 // Set the splitter size. The fTextArea sould be 2/3 on the fMainWindow
306#if QT_VERSION < 0x040000
307 // QValueList<int> vals = splitter->sizes();
308#else
309 // QList<int> vals = splitter->sizes();
310#endif
311// if(vals.size()==2) {
312// vals[0] = (splitter->orientation()==Qt::Vertical ? splitter->height() : splitter->width())*3/4;
313// vals[1] = (splitter->orientation()==Qt::Vertical ? splitter->height() : splitter->width())*1/4;
314// splitter->setSizes(vals);
315// }
316
317 if(UI!=NULL) UI->SetCoutDestination(this); // TO KEEP
318
319 interactorManager->SetG4UI(this);
320
321#ifdef G4DEBUG_INTERFACES_BASIC
322 printf("G4UIQt::G4UIQt END\n");
323#endif
324}
325
326
327
328G4UIQt::~G4UIQt(
329)
330{
331#ifdef G4DEBUG_INTERFACES_BASIC
332 printf("G4UIQt::~G4UIQt Delete\n");
333#endif
334 G4UImanager* UI = G4UImanager::GetUIpointer(); // TO KEEP
335 if(UI!=NULL) { // TO KEEP
336 UI->SetSession(NULL); // TO KEEP
337 UI->SetCoutDestination(NULL); // TO KEEP
338 }
339
340 if (fMainWindow!=NULL) {
341#ifdef G4DEBUG_INTERFACES_BASIC
342 printf("G4UIQt::~G4UIQt DELETE fMainWindow\n");
343#endif
344 delete fMainWindow;
345 }
346}
347
348
349/** Add a new tab widget.
350 Create the tab if it was not done
351*/
352void G4UIQt::AddTabVisWidget(
353 QWidget* aWidget
354,QString name
355,int sizeX
356,int sizeY
357)
358{
359 if (fTabWidget != NULL) {
360 if (!aWidget) return;
361
362#if QT_VERSION < 0x040000
363 aWidget->reparent(visWidget,0,QPoint(0,0));
364#else
365 aWidget->setParent(visWidget);
366#endif
367
368 fTabWidget->addTab(aWidget,name);
369 QHBoxLayout *mainLayout = new QHBoxLayout(visWidget);
370 mainLayout->setMargin(0);
371 mainLayout->setSpacing(0);
372 mainLayout->addWidget(fTabWidget);
373
374 // Set the size
375#ifdef G4DEBUG_INTERFACES_BASIC
376 printf("G4UIQt::AddTabWidget size:%d %d\n",sizeX,sizeY);
377#endif
378 // resize(sizeX,sizeY);
379 }
380}
381
382
383void G4UIQt::UpdateTabWidget(int tabNumber) {
384 QString text = fTabWidget->tabText (tabNumber);
385 if ( text != fCoutText) {
386
387 // Then we have to do /vis/viewer/select on the selelected tab
388 QString paramSelect = QString("/vis/viewer/select ")+text;
389 QString paramFlush = QString("/vis/viewer/flush ")+text;
390 G4UImanager* UI = G4UImanager::GetUIpointer();
391 if(UI!=NULL) {
392 UI->ApplyCommand(paramSelect.toStdString().c_str());
393 UI->ApplyCommand(paramFlush.toStdString().c_str());
394 }
395 fTabWidget->currentWidget()->repaint();
396 }
397}
398
399
400
401/** Start the Qt main loop
402*/
403G4UIsession* G4UIQt::SessionStart (
404)
405{
406
407 G4Qt* interactorManager = G4Qt::getInstance ();
408
409#if QT_VERSION >= 0x040000
410#if QT_VERSION >= 0x040200
411 fMainWindow->setVisible(true);
412#else
413 fMainWindow->show();
414#endif
415#else
416 fMainWindow->show();
417#endif
418 Prompt("session");
419 exitSession = false;
420
421
422 interactorManager->DisableSecondaryLoop (); // TO KEEP
423 if ((QApplication*)interactorManager->GetMainInteractor())
424 ((QApplication*)interactorManager->GetMainInteractor())->exec();
425
426 // on ne passe pas le dessous ? FIXME ????
427 // je ne pense pas 13/06
428
429 // void* event; // TO KEEP
430 // while((event = interactorManager->GetEvent())!=NULL) { // TO KEEP
431 // interactorManager->DispatchEvent(event); // TO KEEP
432 // if(exitSession==true) break; // TO KEEP
433 // } // TO KEEP
434
435 interactorManager->EnableSecondaryLoop ();
436 return this;
437}
438
439
440/** Display the prompt in the prompt area
441 @param aPrompt : string to display as the promt label
442 //FIXME : probablement inutile puisque le seul a afficher qq chose d'autre
443 que "session" est SecondaryLoop()
444*/
445void G4UIQt::Prompt (
446 G4String aPrompt
447)
448{
449 if (!aPrompt) return;
450
451 fCommandLabel->setText((char*)aPrompt.data());
452}
453
454
455
456void G4UIQt::SessionTerminate (
457)
458{
459 G4Qt* interactorManager = G4Qt::getInstance ();
460 fMainWindow->close();
461 ((QApplication*)interactorManager->GetMainInteractor())->exit();
462}
463
464
465
466/**
467 Called by intercoms/src/G4UImanager.cc<br>
468 Called by visualization/management/src/G4VisCommands.cc with "EndOfEvent" argument<br>
469 It have to pause the session command terminal.<br>
470 Call SecondaryLoop to wait for exit event<br>
471 @param aState
472 @see : G4VisCommandReviewKeptEvents::SetNewValue
473*/
474void G4UIQt::PauseSessionStart (
475 G4String aState
476)
477{
478 if (!aState) return;
479
480 if(aState=="G4_pause> ") { // TO KEEP
481 SecondaryLoop ("Pause, type continue to exit this state"); // TO KEEP
482 } // TO KEEP
483
484 if(aState=="EndOfEvent") { // TO KEEP
485 // Picking with feed back in event data Done here !!!
486 SecondaryLoop ("End of event, type continue to exit this state"); // TO KEEP
487 } // TO KEEP
488}
489
490
491
492/**
493 Begin the secondary loop
494 @param a_prompt : label to display as the prompt label
495 */
496void G4UIQt::SecondaryLoop (
497 G4String aPrompt
498)
499{
500 if (!aPrompt) return;
501
502 G4Qt* interactorManager = G4Qt::getInstance (); // TO KEEP ?
503 Prompt(aPrompt); // TO KEEP
504 exitPause = false; // TO KEEP
505 void* event; // TO KEEP
506 while((event = interactorManager->GetEvent())!=NULL) { // TO KEEP
507 interactorManager->DispatchEvent(event); // TO KEEP
508 if(exitPause==true) break; // TO KEEP
509 } // TO KEEP
510 Prompt("session"); // TO KEEP
511}
512
513
514
515/**
516 Receive a cout from Geant4. We have to display it in the cout zone
517 @param aString : label to add in the display area
518 @return 0
519*/
520G4int G4UIQt::ReceiveG4cout (
521 G4String aString
522)
523{
524 if (!aString) return 0;
525 G4Qt* interactorManager = G4Qt::getInstance ();
526 if (!interactorManager) return 0;
527
528#if QT_VERSION < 0x040000
529 fTextArea->append(QString((char*)aString.data()).simplifyWhiteSpace());
530 fTextArea->verticalScrollBar()->setValue(fTextArea->verticalScrollBar()->maxValue());
531#else
532 fTextArea->append(QString((char*)aString.data()).trimmed());
533 fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum());
534#endif
535 fTextArea->repaint();
536 // interactorManager->FlushAndWaitExecution();
537 return 0;
538}
539
540
541/**
542 Receive a cerr from Geant4. We have to display it in the cout zone
543 @param aString : label to add in the display area
544 @return 0
545*/
546G4int G4UIQt::ReceiveG4cerr (
547 G4String aString
548)
549{
550 if (!aString) return 0;
551 G4Qt* interactorManager = G4Qt::getInstance ();
552 if (!interactorManager) return 0;
553
554#if QT_VERSION < 0x040000
555 QColor previousColor = fTextArea->color();
556 fTextArea->setColor(Qt::red);
557 fTextArea->append(QString((char*)aString.data()).simplifyWhiteSpace());
558 fTextArea->setColor(previousColor);
559 fTextArea->verticalScrollBar()->setValue(fTextArea->verticalScrollBar()->maxValue());
560#else
561 QColor previousColor = fTextArea->textColor();
562 fTextArea->setTextColor(Qt::red);
563 fTextArea->append(QString((char*)aString.data()).trimmed());
564 fTextArea->setTextColor(previousColor);
565 fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum());
566#endif
567 fTextArea->repaint();
568 // interactorManager->FlushAndWaitExecution();
569 return 0;
570}
571
572
573
574/**
575 Add a new menu to the menu bar
576 @param aName name of menu
577 @param aLabel label to display
578 */
579void G4UIQt::AddMenu (
580 const char* aName
581,const char* aLabel
582)
583{
584 if (aName == NULL) return;
585 if (aLabel == NULL) return;
586
587#if QT_VERSION < 0x040000
588 QPopupMenu *fileMenu = new QPopupMenu( fMainWindow);
589 fMainWindow->menuBar()->insertItem( aLabel, fileMenu );
590#else
591 QMenu *fileMenu = new QMenu(aLabel);
592 fMainWindow->menuBar()->insertMenu(fMainWindow->menuBar()->actions().last(),fileMenu);
593#endif
594
595 AddInteractor (aName,(G4Interactor)fileMenu);
596}
597
598
599/**
600 Add a new button to a menu
601 @param aMenu : parent menu
602 @param aLabel : label to display
603 @param aCommand : command to execute as a callback
604 */
605void G4UIQt::AddButton (
606 const char* aMenu
607,const char* aLabel
608,const char* aCommand
609)
610{
611 if(aMenu==NULL) return; // TO KEEP
612 if(aLabel==NULL) return; // TO KEEP
613 if(aCommand==NULL) return; // TO KEEP
614
615#if QT_VERSION < 0x040000
616 QPopupMenu *parent = (QPopupMenu*)GetInteractor(aMenu);
617#else
618 QMenu *parent = (QMenu*)GetInteractor(aMenu);
619#endif
620
621 if(parent==NULL) return;
622
623 QSignalMapper *signalMapper = new QSignalMapper(this);
624#if QT_VERSION < 0x030200
625 QAction *action = new QAction(QString(aLabel),QString(aLabel),QKeySequence::QKeySequence (),signalMapper, SLOT(map()));
626 action->addTo(parent);
627 connect(action,SIGNAL(activated()),signalMapper,SLOT(map()));
628
629#elif QT_VERSION < 0x040000
630 QAction *action = new QAction(QString(aLabel),QKeySequence::QKeySequence (),signalMapper, SLOT(map()));
631 action->addTo(parent);
632 connect(action,SIGNAL(activated()),signalMapper,SLOT(map()));
633
634#else
635 QAction *action = parent->addAction(aLabel, signalMapper, SLOT(map()));
636
637#endif
638 connect(signalMapper, SIGNAL(mapped(const QString &)),this, SLOT(ButtonCallback(const QString&)));
639 signalMapper->setMapping(action, QString(aCommand));
640}
641
642
643
644
645/**
646 Open the help dialog in a separate window.<br>
647 This will be display as a tree widget.<br>
648 Implementation of <b>void G4VBasicShell::TerminalHelp(G4String newCommand)</b>
649
650 @param newCommand : open the tree widget item on this command if is set
651*/
652void G4UIQt::TerminalHelp(
653 G4String newCommand
654)
655{
656 // Create the help dialog
657 if (!fHelpDialog) {
658#if QT_VERSION < 0x040000
659 fHelpDialog = new QDialog(0,0,FALSE,Qt::WStyle_Title | Qt::WStyle_SysMenu | Qt::WStyle_MinMax );
660#else
661 fHelpDialog = new QDialog(0,Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
662#endif
663 QVBoxLayout *vLayout = new QVBoxLayout(fHelpDialog);
664 QWidget *helpWidget = new QWidget(fHelpDialog);
665 QSplitter *splitter = new QSplitter(Qt::Horizontal,fHelpDialog);
666 QPushButton *exitButton = new QPushButton("Exit",fHelpDialog);
667 exitButton->setAutoDefault(false);
668 connect(exitButton, SIGNAL(clicked()), fHelpDialog,SLOT(close()));
669
670 QHBoxLayout *helpLayout = new QHBoxLayout(helpWidget);
671#if QT_VERSION < 0x040000
672 helpLayout->add(new QLabel("Search :",helpWidget));
673#else
674 helpLayout->addWidget(new QLabel("Search :",helpWidget));
675#endif
676 helpLine = new QLineEdit(fHelpDialog);
677#if QT_VERSION < 0x040000
678 helpLayout->add(helpLine);
679 connect( helpLine, SIGNAL( returnPressed () ), this, SLOT( lookForHelpStringCallback() ) );
680#else
681 helpLayout->addWidget(helpLine);
682 connect( helpLine, SIGNAL( editingFinished () ), this, SLOT( lookForHelpStringCallback() ) );
683#endif
684
685 // the help tree
686#if QT_VERSION < 0x040000
687 fHelpTreeWidget = new QListView(splitter);
688#else
689 fHelpTreeWidget = new QTreeWidget();
690#endif
691 fHelpTreeWidget = CreateHelpTree();
692
693 fHelpArea = new QTextEdit(splitter);
694 fHelpArea->setReadOnly(true);
695
696 // Set layouts
697
698#if QT_VERSION >= 0x040000
699 if (fHelpTreeWidget)
700 splitter->addWidget(fHelpTreeWidget);
701 splitter->addWidget(fHelpArea);
702#endif
703
704
705#if QT_VERSION >= 0x040000
706 vLayout->addWidget(helpWidget);
707 vLayout->addWidget(splitter,1);
708 vLayout->addWidget(exitButton);
709#else
710 vLayout->addWidget(helpWidget);
711 vLayout->addWidget(splitter,1);
712 vLayout->addWidget(exitButton);
713#endif
714
715 // set the splitter size
716#if QT_VERSION >= 0x040000
717 QList<int> list;
718#else
719 QValueList<int> list;
720#endif
721 list.append( 400 );
722 list.append( 400 );
723 splitter->setSizes(list);
724
725#if QT_VERSION >= 0x040000
726 fHelpDialog->setLayout(vLayout);
727#endif
728
729 }
730
731 ActivateCommand(newCommand);
732
733#if QT_VERSION < 0x040000
734 fHelpDialog->setCaption( tr( "Help on commands" ));
735#else
736 fHelpDialog->setWindowTitle(tr("Help on commands"));
737#endif
738 fHelpDialog->resize(800,600);
739 fHelpDialog->move(QPoint(400,150));
740 fHelpDialog->show();
741 fHelpDialog->raise();
742#if QT_VERSION < 0x040000
743 fHelpDialog->setActiveWindow();
744#else
745 fHelpDialog->activateWindow();
746#endif
747}
748
749
750void G4UIQt::ActivateCommand(
751 G4String newCommand
752)
753{
754 if (!fHelpTreeWidget) {
755 return;
756 }
757 // Look for the choosen command "newCommand"
758 size_t i = newCommand.index(" ");
759 G4String targetCom="";
760 if( i != std::string::npos )
761 {
762 G4String newValue = newCommand(i+1,newCommand.length()-(i+1));
763 newValue.strip(G4String::both);
764 targetCom = ModifyToFullPathCommand( newValue );
765 }
766 if (targetCom != "") {
767#if QT_VERSION < 0x040000
768 QListViewItem* findItem = NULL;
769 QListViewItem* tmpItem = fHelpTreeWidget->firstChild();
770 while (tmpItem != 0) {
771 if (!findItem) {
772 findItem = FindTreeItem(tmpItem,QString((char*)targetCom.data()));
773 }
774 tmpItem = tmpItem->nextSibling();
775 }
776#else
777 QTreeWidgetItem* findItem = NULL;
778 for (int a=0;a<fHelpTreeWidget->topLevelItemCount();a++) {
779 if (!findItem) {
780 findItem = FindTreeItem(fHelpTreeWidget->topLevelItem(a),QString((char*)targetCom.data()));
781 }
782 }
783#endif
784
785 if (findItem) {
786
787 //collapsed open item
788#if QT_VERSION < 0x040000
789
790 // FIXME : Has to be checked
791 QListViewItem* tmpItem = fHelpTreeWidget->firstChild();
792 QList<QListViewItem> openItems;
793 while ((tmpItem != 0) || (!openItems.isEmpty())) {
794 if (tmpItem->isOpen() ) {
795 tmpItem->setOpen(false);
796 openItems.append(tmpItem);
797 tmpItem = tmpItem->firstChild();
798 } else {
799 tmpItem = tmpItem->nextSibling();
800 }
801 if (tmpItem == 0) {
802 tmpItem = openItems.take(openItems.count()-1);
803 }
804 }
805#else
806 QList<QTreeWidgetItem *> selected;
807
808 selected = fHelpTreeWidget->selectedItems();
809 if ( selected.count() != 0 ) {
810 QTreeWidgetItem * tmp =selected.at( 0 );
811 while ( tmp) {
812#if QT_VERSION < 0x040202
813 fHelpTreeWidget->setItemExpanded(tmp,false);
814#else
815 tmp->setExpanded(false);
816#endif
817 tmp = tmp->parent();
818 }
819 }
820#endif
821
822 // clear old selection
823 fHelpTreeWidget->clearSelection();
824
825 // set new selection
826#if QT_VERSION >= 0x040000
827#if QT_VERSION < 0x040202
828 fHelpTreeWidget->setItemSelected(findItem,true);
829#else
830 findItem->setSelected(true);
831#endif
832#else
833 findItem->setSelected(true);
834#endif
835
836 // expand parent item
837 while ( findItem) {
838#if QT_VERSION < 0x040000
839 findItem->setOpen(true);
840#else
841#if QT_VERSION < 0x040202
842 fHelpTreeWidget->setItemExpanded(findItem,true);
843#else
844 findItem->setExpanded(true);
845#endif
846#endif
847 findItem = findItem->parent();
848 }
849
850 // Call the update of the right textArea
851 HelpTreeClicCallback();
852 }
853 }
854}
855
856
857
858/**
859 Create the help tree widget
860 @param parent : parent of tree widget
861 @return the widget containing the tree or NULL if it could not have beeen created
862 */
863
864#if QT_VERSION < 0x040000
865QListView * G4UIQt::CreateHelpTree()
866#else
867QTreeWidget * G4UIQt::CreateHelpTree()
868#endif
869{
870 G4UImanager* UI = G4UImanager::GetUIpointer();
871 if(UI==NULL) return NULL;
872 G4UIcommandTree * treeTop = UI->GetTree();
873
874
875 // build widget
876#if QT_VERSION < 0x040000
877 fHelpTreeWidget->setSelectionMode(QListView::Single);
878 fHelpTreeWidget->setRootIsDecorated(true);
879 fHelpTreeWidget->addColumn("Command");
880 fHelpTreeWidget->header()->setResizeEnabled(FALSE,1);
881#else
882 fHelpTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
883 QStringList labels;
884 labels << QString("Command");
885 fHelpTreeWidget->setHeaderLabels(labels);
886#endif
887
888 G4int treeSize = treeTop->GetTreeEntry();
889#if QT_VERSION < 0x040000
890 QListViewItem * newItem;
891#else
892 QTreeWidgetItem * newItem;
893#endif
894 for (int a=0;a<treeSize;a++) {
895 // Creating new item
896
897#if QT_VERSION < 0x040000
898 newItem = new QListViewItem(fHelpTreeWidget);
899 newItem->setText(0,QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).simplifyWhiteSpace());
900#else
901 newItem = new QTreeWidgetItem(fHelpTreeWidget);
902 newItem->setText(0,QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).trimmed());
903#endif
904
905
906 // look for childs
907 CreateChildTree(newItem,treeTop->GetTree(a+1));
908 // items.append(newItem);
909 }
910
911
912#if QT_VERSION < 0x040000
913 connect(fHelpTreeWidget, SIGNAL(selectionChanged ()),this, SLOT(HelpTreeClicCallback()));
914 connect(fHelpTreeWidget, SIGNAL(doubleClicked (QListViewItem*)),this, SLOT(HelpTreeDoubleClicCallback()));
915#else
916 connect(fHelpTreeWidget, SIGNAL(itemSelectionChanged ()),this, SLOT(HelpTreeClicCallback()));
917 connect(fHelpTreeWidget, SIGNAL(itemDoubleClicked (QTreeWidgetItem*,int)),this, SLOT(HelpTreeDoubleClicCallback()));
918#endif
919
920 return fHelpTreeWidget;
921}
922
923
924
925/** Fill the Help Tree Widget
926 @param aParent : parent item to fill
927 @param aCommandTree : commandTree node associate with this part of the Tree
928*/
929#if QT_VERSION < 0x040000
930void G4UIQt::CreateChildTree(
931 QListViewItem *aParent
932,G4UIcommandTree *aCommandTree
933#else
934void G4UIQt::CreateChildTree(
935 QTreeWidgetItem *aParent
936,G4UIcommandTree *aCommandTree
937#endif
938)
939{
940 if (aParent == NULL) return;
941 if (aCommandTree == NULL) return;
942
943
944 // Creating new item
945#if QT_VERSION < 0x040000
946 QListViewItem * newItem;
947#else
948 QTreeWidgetItem * newItem;
949#endif
950
951 // Get the Sub directories
952 for (int a=0;a<aCommandTree->GetTreeEntry();a++) {
953
954#if QT_VERSION < 0x040000
955 newItem = new QListViewItem(aParent);
956 newItem->setText(0,QString((char*)(aCommandTree->GetTree(a+1)->GetPathName()).data()).simplifyWhiteSpace());
957
958#else
959 newItem = new QTreeWidgetItem(aParent);
960 newItem->setText(0,QString((char*)(aCommandTree->GetTree(a+1)->GetPathName()).data()).trimmed());
961#endif
962
963 CreateChildTree(newItem,aCommandTree->GetTree(a+1));
964 }
965
966
967
968 // Get the Commands
969
970 for (int a=0;a<aCommandTree->GetCommandEntry();a++) {
971
972 QStringList stringList;
973#if QT_VERSION < 0x040000
974 newItem = new QListViewItem(aParent);
975 newItem->setText(0,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).simplifyWhiteSpace());
976 newItem->setOpen(false);
977
978#else
979 newItem = new QTreeWidgetItem(aParent);
980 newItem->setText(0,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).trimmed());
981#if QT_VERSION < 0x040202
982 fHelpTreeWidget->setItemExpanded(newItem,false);
983#else
984 newItem->setExpanded(false);
985#endif
986#endif
987
988 }
989}
990
991
992/** Find a treeItemWidget in the help tree
993 @param aCommand item's String to look for
994 @return item if found, NULL if not
995*/
996#if QT_VERSION < 0x040000
997QListViewItem* G4UIQt::FindTreeItem(
998 QListViewItem *aParent
999#else
1000QTreeWidgetItem* G4UIQt::FindTreeItem(
1001 QTreeWidgetItem *aParent
1002#endif
1003,const QString& aCommand
1004)
1005{
1006 if (aParent == NULL) return NULL;
1007
1008 if (aParent->text(0) == aCommand)
1009 return aParent;
1010
1011#if QT_VERSION < 0x040000
1012 QListViewItem * tmp = NULL;
1013 QListViewItem* tmpItem = aParent->firstChild();
1014 while (tmpItem != 0) {
1015 if (!tmp)
1016 tmp = FindTreeItem(tmpItem,aCommand);
1017 tmpItem = tmpItem->nextSibling();
1018 }
1019
1020#else
1021 QTreeWidgetItem * tmp = NULL;
1022 for (int a=0;a<aParent->childCount();a++) {
1023 if (!tmp)
1024 tmp = FindTreeItem(aParent->child(a),aCommand);
1025 }
1026#endif
1027 return tmp;
1028}
1029
1030
1031
1032/** Build the command list parameters in a QString<br>
1033 Reimplement partialy the G4UIparameter.cc
1034 @param aCommand : command to list parameters
1035 @see G4UIparameter::List()
1036 @see G4UIcommand::List()
1037 @return the command list parameters, or "" if nothing
1038*/
1039QString G4UIQt::GetCommandList (
1040 const G4UIcommand *aCommand
1041)
1042{
1043
1044 QString txt ="";
1045 if (aCommand == NULL)
1046 return txt;
1047
1048 G4String commandPath = aCommand->GetCommandPath();
1049 G4String rangeString = aCommand->GetRange();
1050 G4int n_guidanceEntry = aCommand->GetGuidanceEntries();
1051 G4int n_parameterEntry = aCommand->GetParameterEntries();
1052
1053 if ((commandPath == "") &&
1054 (rangeString == "") &&
1055 (n_guidanceEntry == 0) &&
1056 (n_parameterEntry == 0)) {
1057 return txt;
1058 }
1059
1060 if((commandPath.length()-1)!='/') {
1061 txt += "Command " + QString((char*)(commandPath).data()) + "\n";
1062 }
1063 txt += "Guidance :\n";
1064
1065 for( G4int i_thGuidance=0; i_thGuidance < n_guidanceEntry; i_thGuidance++ ) {
1066 txt += QString((char*)(aCommand->GetGuidanceLine(i_thGuidance)).data()) + "\n";
1067 }
1068 if( ! rangeString.isNull() ) {
1069 txt += " Range of parameters : " + QString((char*)(rangeString).data()) + "\n";
1070 }
1071 if( n_parameterEntry > 0 ) {
1072 G4UIparameter *param;
1073
1074 // Re-implementation of G4UIparameter.cc
1075
1076 for( G4int i_thParameter=0; i_thParameter<n_parameterEntry; i_thParameter++ ) {
1077 param = aCommand->GetParameter(i_thParameter);
1078 txt += "\nParameter : " + QString((char*)(param->GetParameterName()).data()) + "\n";
1079 if( ! param->GetParameterGuidance().isNull() )
1080 txt += QString((char*)(param->GetParameterGuidance()).data())+ "\n" ;
1081 txt += " Parameter type : " + QString(QChar(param->GetParameterType())) + "\n";
1082 if(param->IsOmittable()){
1083 txt += " Omittable : True\n";
1084 } else {
1085 txt += " Omittable : False\n";
1086 }
1087 if( param->GetCurrentAsDefault() ) {
1088 txt += " Default value : taken from the current value\n";
1089 } else if( ! param->GetDefaultValue().isNull() ) {
1090 txt += " Default value : " + QString((char*)(param->GetDefaultValue()).data())+ "\n";
1091 }
1092 if( ! param->GetParameterRange().isNull() ) {
1093 txt += " Parameter range : " + QString((char*)(param->GetParameterRange()).data())+ "\n";
1094 }
1095 if( ! param->GetParameterCandidates().isNull() ) {
1096 txt += " Candidates : " + QString((char*)(param->GetParameterCandidates()).data())+ "\n";
1097 }
1098 }
1099 }
1100 return txt;
1101}
1102
1103
1104
1105/** Implement G4VBasicShell vurtual function
1106 */
1107G4bool G4UIQt::GetHelpChoice(
1108 G4int&
1109)
1110{
1111#ifdef G4DEBUG_INTERFACES_BASIC
1112 printf("G4UIQt::GetHelpChoice SHOULD NEVER GO HERE");
1113#endif
1114 return true;
1115}
1116
1117
1118/** Implement G4VBasicShell vurtual function
1119*/
1120void G4UIQt::ExitHelp(
1121)
1122{
1123#ifdef G4DEBUG_INTERFACES_BASIC
1124 printf("G4UIQt::ExitHelp SHOULD NEVER GO HERE");
1125#endif
1126}
1127
1128
1129/** Event filter method. Every event from QtApplication goes here.<br/>
1130 We apply a filter only for the Up and Down Arrow press when the QLineEdit<br/>
1131 is active. If this filter match, Up arrow we give the previous command<br/>
1132 and Down arrow will give the next if exist.<br/>
1133 @param obj Emitter of the event
1134 @param event Kind of event
1135*/
1136bool G4UIQt::eventFilter( // Should stay with a minuscule eventFilter because of Qt
1137 QObject *aObj
1138,QEvent *aEvent
1139)
1140{
1141 if (aObj == NULL) return false;
1142 if (aEvent == NULL) return false;
1143
1144 if (aObj == fCommandHistoryArea) {
1145 if (aEvent->type() == QEvent::KeyPress) {
1146 fCommandArea->setFocus();
1147 }
1148 }
1149 if (aObj == fCommandArea) {
1150 if (aEvent->type() == QEvent::KeyPress) {
1151 QKeyEvent *e = static_cast<QKeyEvent*>(aEvent);
1152 if ((e->key() == (Qt::Key_Down)) ||
1153 (e->key() == (Qt::Key_PageDown)) ||
1154 (e->key() == (Qt::Key_Up)) ||
1155 (e->key() == (Qt::Key_PageUp))) {
1156#if QT_VERSION < 0x040000
1157 // count rows...
1158 QListViewItem* tmpItem = fCommandHistoryArea->firstChild();
1159 int selection = -1;
1160 int index = 0;
1161 while (tmpItem != 0) {
1162 if (tmpItem == fCommandHistoryArea->selectedItem()) {
1163 selection = index;
1164 }
1165 index ++;
1166 tmpItem = tmpItem->nextSibling();
1167 }
1168 if (fCommandHistoryArea->childCount()) {
1169 if (selection == -1) {
1170 selection = fCommandHistoryArea->childCount()-1;
1171 } else {
1172 if (e->key() == (Qt::Key_Down)) {
1173 if (selection <(fCommandHistoryArea->childCount()-1))
1174 selection++;
1175 } else if (e->key() == (Qt::Key_PageDown)) {
1176 selection = fCommandHistoryArea->childCount()-1;
1177#else
1178 int selection = fCommandHistoryArea->currentRow();
1179 if (fCommandHistoryArea->count()) {
1180 if (selection == -1) {
1181 selection = fCommandHistoryArea->count()-1;
1182 } else {
1183 if (e->key() == (Qt::Key_Down)) {
1184 if (selection <(fCommandHistoryArea->count()-1))
1185 selection++;
1186 } else if (e->key() == (Qt::Key_PageDown)) {
1187 selection = fCommandHistoryArea->count()-1;
1188#endif
1189 } else if (e->key() == (Qt::Key_Up)) {
1190 if (selection >0)
1191 selection --;
1192 } else if (e->key() == (Qt::Key_PageUp)) {
1193 selection = 0;
1194 }
1195 }
1196 fCommandHistoryArea->clearSelection();
1197#if QT_VERSION < 0x040000
1198 QListViewItem* tmpItem = fCommandHistoryArea->firstChild();
1199 int index = 0;
1200 while (tmpItem != 0) {
1201 if (index == selection) {
1202 tmpItem->setSelected(true);
1203 fCommandHistoryArea->setCurrentItem(tmpItem);
1204 }
1205 index ++;
1206 tmpItem = tmpItem->nextSibling();
1207 }
1208#else
1209#if QT_VERSION < 0x040202
1210 fCommandHistoryArea->setItemSelected(fCommandHistoryArea->item(selection),true);
1211#else
1212 fCommandHistoryArea->item(selection)->setSelected(true);
1213#endif
1214 fCommandHistoryArea->setCurrentItem(fCommandHistoryArea->item(selection));
1215#endif
1216 }
1217 } else if (e->key() == (Qt::Key_Tab)) {
1218#if QT_VERSION < 0x040000
1219 G4String ss = Complete(fCommandArea->text().ascii());
1220#else
1221 G4String ss = Complete(fCommandArea->text().toStdString().c_str());
1222#endif
1223 fCommandArea->setText((char*)(ss.data()));
1224
1225 // do not pass by parent, it will disable widget tab focus !
1226 return true;
1227#if QT_VERSION >= 0x040000
1228 // L.Garnier : MetaModifier is CTRL for MAC, but I don't want to put a MAC
1229 // specific #ifdef
1230 } else if (((e->modifiers () == Qt::ControlModifier) || (e->modifiers () == Qt::MetaModifier)) && (e->key() == Qt::Key_A)) {
1231 fCommandArea->home(false);
1232 return true;
1233 } else if (((e->modifiers () == Qt::ControlModifier) || (e->modifiers () == Qt::MetaModifier)) && (e->key() == Qt::Key_E)) {
1234 fCommandArea->end(false);
1235 return true;
1236#endif
1237 }
1238 }
1239 }
1240 // pass the event on to the parent class
1241 return QObject::eventFilter(aObj, aEvent);
1242}
1243
1244
1245
1246
1247/***************************************************************************/
1248//
1249// SLOTS DEFINITIONS
1250//
1251/***************************************************************************/
1252
1253/** Called when user give "help" command.
1254*/
1255void G4UIQt::ShowHelpCallback (
1256)
1257{
1258 TerminalHelp("");
1259}
1260
1261
1262/** Called when user click on clear button. Clear the text Output area
1263*/
1264void G4UIQt::ClearButtonCallback (
1265)
1266{
1267 fTextArea->clear();
1268}
1269
1270/** Called when user exit session
1271*/
1272void G4UIQt::ExitSession (
1273)
1274{
1275 SessionTerminate();
1276}
1277
1278
1279/** Callback call when "click on a menu entry.<br>
1280 Send the associated command to geant4
1281*/
1282void G4UIQt::CommandEnteredCallback (
1283)
1284{
1285#if QT_VERSION < 0x040000
1286 G4String command (fCommandArea->text().ascii());
1287 if (fCommandArea->text().simplifyWhiteSpace() != "") {
1288
1289 QListViewItem *newItem = new QListViewItem(fCommandHistoryArea);
1290 newItem->setText(0,fCommandArea->text());
1291 fCommandHistoryArea->insertItem(newItem);
1292 // now we have to arrange
1293 QListViewItem *temp= fCommandHistoryArea->lastItem();
1294 for (int i=0; i<fCommandHistoryArea->childCount()-1;i++) {
1295 fCommandHistoryArea->takeItem(temp);
1296 fCommandHistoryArea->insertItem(temp);
1297 temp= fCommandHistoryArea->lastItem();
1298 }
1299#else
1300 G4String command (fCommandArea->text().toStdString().c_str());
1301 if (fCommandArea->text().trimmed() != "") {
1302 fCommandHistoryArea->addItem(fCommandArea->text());
1303#endif
1304 fCommandHistoryArea->clearSelection();
1305 fCommandHistoryArea->setCurrentItem(NULL);
1306 fCommandArea->setText("");
1307
1308 G4Qt* interactorManager = G4Qt::getInstance ();
1309 if (interactorManager) {
1310 interactorManager->FlushAndWaitExecution();
1311 }
1312 if (command(0,4) != "help") {
1313 ApplyShellCommand (command,exitSession,exitPause);
1314 } else {
1315 TerminalHelp(command);
1316 }
1317 if(exitSession==true)
1318 SessionTerminate();
1319 }
1320}
1321
1322
1323/** Callback call when "enter" clicked on the command zone.<br>
1324 Send the command to geant4
1325 @param aCommand
1326*/
1327void G4UIQt::ButtonCallback (
1328 const QString& aCommand
1329)
1330{
1331#if QT_VERSION < 0x040000
1332 G4String ss = G4String(aCommand.ascii());
1333#else
1334 G4String ss = G4String(aCommand.toStdString().c_str());
1335#endif
1336 ApplyShellCommand(ss,exitSession,exitPause);
1337 if(exitSession==true)
1338 SessionTerminate();
1339}
1340
1341
1342
1343/** This callback is activated when user selected a item in the help tree
1344*/
1345void G4UIQt::HelpTreeClicCallback (
1346)
1347{
1348#if QT_VERSION < 0x040000
1349 QListViewItem* item = NULL;
1350#else
1351 QTreeWidgetItem* item = NULL;
1352#endif
1353 if (!fHelpTreeWidget)
1354 return ;
1355
1356 if (!fHelpArea)
1357 return;
1358
1359#if QT_VERSION < 0x040000
1360 item =fHelpTreeWidget->selectedItem();
1361#else
1362 QList<QTreeWidgetItem *> list =fHelpTreeWidget->selectedItems();
1363 if (list.isEmpty())
1364 return;
1365 item = list.first();
1366#endif
1367 if (!item)
1368 return;
1369
1370 G4UImanager* UI = G4UImanager::GetUIpointer();
1371 if(UI==NULL) return;
1372 G4UIcommandTree * treeTop = UI->GetTree();
1373
1374
1375
1376 std::string itemText;
1377#if QT_VERSION < 0x040000
1378 itemText = std::string(item->text(0).ascii());
1379#else
1380 itemText = std::string(item->text(0).toStdString());
1381#endif
1382
1383 G4UIcommand* command = treeTop->FindPath(itemText.c_str());
1384
1385 if (command) {
1386#if QT_VERSION >= 0x040000
1387#if QT_VERSION < 0x040200
1388 fHelpArea->clear();
1389 fHelpArea->append(GetCommandList(command));
1390#else
1391 fHelpArea->setText(GetCommandList(command));
1392#endif
1393#else
1394 fHelpArea->setText(GetCommandList(command));
1395#endif
1396 } else { // this is a command
1397 G4UIcommandTree* path = treeTop->FindCommandTree(itemText.c_str());
1398 if ( path) {
1399 // this is not a command, this is a sub directory
1400 // We display the Title
1401#if QT_VERSION >= 0x040000
1402#if QT_VERSION < 0x040200
1403 fHelpArea->clear();
1404 fHelpArea->append(path->GetTitle().data());
1405#else
1406 fHelpArea->setText(path->GetTitle().data());
1407#endif
1408#else
1409 fHelpArea->setText(path->GetTitle().data());
1410#endif
1411 }
1412 }
1413}
1414
1415/** This callback is activated when user double clic on a item in the help tree
1416*/
1417void G4UIQt::HelpTreeDoubleClicCallback (
1418)
1419{
1420 HelpTreeClicCallback();
1421
1422#if QT_VERSION < 0x040000
1423 QListViewItem* item = NULL;
1424#else
1425 QTreeWidgetItem* item = NULL;
1426#endif
1427 if (!fHelpTreeWidget)
1428 return ;
1429
1430 if (!fHelpArea)
1431 return;
1432
1433#if QT_VERSION < 0x040000
1434 item =fHelpTreeWidget->selectedItem();
1435#else
1436 QList<QTreeWidgetItem *> list =fHelpTreeWidget->selectedItems();
1437 if (list.isEmpty())
1438 return;
1439 item = list.first();
1440#endif
1441 if (!item)
1442 return;
1443
1444 fCommandArea->clear();
1445 fCommandArea->setText(item->text(0));
1446}
1447
1448
1449/** Callback called when user select an old command in the command history<br>
1450 Give it to the command area.
1451*/
1452void G4UIQt::CommandHistoryCallback(
1453)
1454{
1455#if QT_VERSION < 0x040000
1456 QListViewItem* item = NULL;
1457#else
1458 QListWidgetItem* item = NULL;
1459#endif
1460 if (!fCommandHistoryArea)
1461 return ;
1462
1463
1464#if QT_VERSION < 0x040000
1465 item =fCommandHistoryArea->selectedItem();
1466#else
1467 QList<QListWidgetItem *> list =fCommandHistoryArea->selectedItems();
1468 if (list.isEmpty())
1469 return;
1470 item = list.first();
1471#endif
1472 if (!item)
1473 return;
1474#if QT_VERSION < 0x040000
1475 fCommandArea->setText(item->text(0));
1476#else
1477 fCommandArea->setText(item->text());
1478#endif
1479
1480}
1481
1482
1483/** Callback called when user give a new string to look for<br>
1484 Display a list of matching commands descriptions. If no string is set,
1485 will display the complete help tree
1486*/
1487void G4UIQt::lookForHelpStringCallback(
1488)
1489{
1490#if QT_VERSION < 0x040200
1491 fHelpArea->clear();
1492#else
1493 fHelpArea->setText("");
1494#endif
1495 if (helpLine->text() =="") {
1496 // clear old help tree
1497 fHelpTreeWidget->clear();
1498#if QT_VERSION < 0x040000
1499 fHelpTreeWidget->removeColumn(1);
1500 fHelpTreeWidget->removeColumn(0);
1501#endif
1502 CreateHelpTree();
1503 return;
1504 }
1505
1506 // the help tree
1507 G4UImanager* UI = G4UImanager::GetUIpointer();
1508 if(UI==NULL) return;
1509 G4UIcommandTree * treeTop = UI->GetTree();
1510
1511 G4int treeSize = treeTop->GetTreeEntry();
1512
1513 // clear old help tree
1514 fHelpTreeWidget->clear();
1515#if QT_VERSION < 0x040000
1516 fHelpTreeWidget->removeColumn(1);
1517 fHelpTreeWidget->removeColumn(0);
1518#endif
1519
1520 // look for new items
1521
1522 int tmp = 0;
1523#if QT_VERSION < 0x040000
1524 int multFactor = 1000; // factor special for having double keys in Qt3
1525 int doubleKeyAdd = 0; // decay for doubleKeys in Qt3
1526#endif
1527
1528 QMap<int,QString> commandResultMap;
1529 QMap<int,QString> commandChildResultMap;
1530
1531 for (int a=0;a<treeSize;a++) {
1532 G4UIcommand* command = treeTop->FindPath(treeTop->GetTree(a+1)->GetPathName().data());
1533#if QT_VERSION > 0x040000
1534 tmp = GetCommandList (command).count(helpLine->text(),Qt::CaseInsensitive);
1535#else
1536 tmp = GetCommandList (command).contains(helpLine->text(),false);
1537#endif
1538 if (tmp >0) {
1539#if QT_VERSION > 0x040000
1540 commandResultMap.insertMulti(tmp,QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()));
1541#else // tricky thing for Qt3...
1542 doubleKeyAdd = 0;
1543 while (commandResultMap.find( tmp*multFactor+doubleKeyAdd) != commandResultMap.end()) {
1544 doubleKeyAdd ++;
1545 }
1546 commandResultMap.insert( tmp*multFactor+doubleKeyAdd,QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()) );
1547#endif
1548 }
1549 // look for childs
1550 commandChildResultMap = LookForHelpStringInChildTree(treeTop->GetTree(a+1),helpLine->text());
1551 // insert new childs
1552 if (!commandChildResultMap.empty()) {
1553#if QT_VERSION > 0x040000
1554 QMap<int,QString>::const_iterator i = commandChildResultMap.constBegin();
1555 while (i != commandChildResultMap.constEnd()) {
1556 commandResultMap.insertMulti(i.key(),i.value());
1557#else // tricky thing for Qt3...
1558 QMap<int,QString>::const_iterator i = commandChildResultMap.begin();
1559 while (i != commandChildResultMap.end()) {
1560 doubleKeyAdd = 0;
1561 while (commandResultMap.find( i.key()*multFactor+doubleKeyAdd) != commandResultMap.end()) {
1562 doubleKeyAdd ++;
1563 }
1564 commandResultMap.insert(i.key()*multFactor+doubleKeyAdd,i.data());
1565#endif
1566 i++;
1567 }
1568 commandChildResultMap.clear();
1569 }
1570 }
1571
1572 // build new help tree
1573#if QT_VERSION < 0x040000
1574 fHelpTreeWidget->setSelectionMode(QListView::Single);
1575 fHelpTreeWidget->setRootIsDecorated(true);
1576 fHelpTreeWidget->addColumn("Command");
1577 fHelpTreeWidget->addColumn("Match");
1578 // fHelpTreeWidget->header()->setResizeEnabled(FALSE,1);
1579#else
1580 fHelpTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
1581 fHelpTreeWidget->setColumnCount(2);
1582 QStringList labels;
1583 labels << QString("Command") << QString("Match");
1584 fHelpTreeWidget->setHeaderLabels(labels);
1585#endif
1586
1587 if (commandResultMap.empty()) {
1588#if QT_VERSION < 0x040200
1589 fHelpArea->clear();
1590 fHelpArea->append("No match found");
1591#else
1592 fHelpArea->setText("No match found");
1593#endif
1594 return;
1595 }
1596
1597#if QT_VERSION > 0x040000
1598 QMap<int,QString>::const_iterator i = commandResultMap.constEnd();
1599#else
1600 QMap<int,QString>::const_iterator i = commandResultMap.end();
1601#endif
1602 i--;
1603 // 10 maximum progress values
1604 float multValue = 10.0/(float)(i.key());
1605 QString progressChar = "|";
1606 QString progressStr = "|";
1607
1608#if QT_VERSION < 0x040000
1609 QListViewItem * newItem;
1610#else
1611 QTreeWidgetItem * newItem;
1612#endif
1613 bool end = false;
1614 while (!end) {
1615#if QT_VERSION > 0x040000
1616 if (i == commandResultMap.constBegin()) {
1617#else
1618 if (i == commandResultMap.begin()) {
1619#endif
1620 end = true;
1621 }
1622 for(int a=0;a<int(i.key()*multValue);a++) {
1623 progressStr += progressChar;
1624 }
1625#if QT_VERSION < 0x040000
1626 newItem = new QListViewItem(fHelpTreeWidget);
1627 newItem->setText(0,i.data().simplifyWhiteSpace());
1628#else
1629 newItem = new QTreeWidgetItem(fHelpTreeWidget);
1630 newItem->setText(0,i.value().trimmed());
1631#endif
1632 newItem->setText(1,progressStr);
1633
1634#if QT_VERSION >= 0x040200
1635 newItem->setForeground ( 1, QBrush(Qt::blue) );
1636#endif
1637 progressStr = "|";
1638 i--;
1639 }
1640 // FIXME : to be checked on Qt3
1641#if QT_VERSION < 0x040000
1642 fHelpTreeWidget->setColumnWidthMode (1,QListView::Maximum);
1643 fHelpTreeWidget->setSorting(1,false);
1644#else
1645 fHelpTreeWidget->resizeColumnToContents (0);
1646 fHelpTreeWidget->sortItems(1,Qt::DescendingOrder);
1647 // fHelpTreeWidget->setColumnWidth(1,10);//resizeColumnToContents (1);
1648#endif
1649}
1650
1651
1652
1653
1654QMap<int,QString> G4UIQt::LookForHelpStringInChildTree(
1655 G4UIcommandTree *aCommandTree
1656,const QString & text
1657 )
1658{
1659 QMap<int,QString> commandResultMap;
1660 if (aCommandTree == NULL) return commandResultMap;
1661
1662#if QT_VERSION < 0x040000
1663 int multFactor = 1000; // factor special for having double keys in Qt3
1664 int doubleKeyAdd = 0; // decay for doubleKeys in Qt3
1665#endif
1666
1667 // Get the Sub directories
1668 int tmp = 0;
1669 QMap<int,QString> commandChildResultMap;
1670
1671 for (int a=0;a<aCommandTree->GetTreeEntry();a++) {
1672 const G4UIcommand* command = aCommandTree->GetGuidance();
1673#if QT_VERSION > 0x040000
1674 tmp = GetCommandList (command).count(text,Qt::CaseInsensitive);
1675#else
1676 tmp = GetCommandList (command).contains(text,false);
1677#endif
1678 if (tmp >0) {
1679#if QT_VERSION > 0x040000
1680 commandResultMap.insertMulti(tmp,QString((char*)(aCommandTree->GetTree(a+1)->GetPathName()).data()));
1681#else // tricky thing for Qt3...
1682 doubleKeyAdd = 0;
1683 while (commandResultMap.find( tmp*multFactor+doubleKeyAdd) != commandResultMap.end()) {
1684 doubleKeyAdd ++;
1685 }
1686 commandResultMap.insert(tmp*multFactor+doubleKeyAdd,QString((char*)(aCommandTree->GetTree(a+1)->GetPathName()).data()));
1687#endif
1688 }
1689 // look for childs
1690 commandChildResultMap = LookForHelpStringInChildTree(aCommandTree->GetTree(a+1),text);
1691
1692 if (!commandChildResultMap.empty()) {
1693 // insert new childs
1694#if QT_VERSION > 0x040000
1695 QMap<int,QString>::const_iterator i = commandChildResultMap.constBegin();
1696 while (i != commandChildResultMap.constEnd()) {
1697 commandResultMap.insertMulti(i.key(),i.value());
1698#else // tricky thing for Qt3...
1699 QMap<int,QString>::const_iterator i = commandChildResultMap.begin();
1700 while (i != commandChildResultMap.end()) {
1701 doubleKeyAdd = 0;
1702 while (commandResultMap.find( i.key()*multFactor+doubleKeyAdd) != commandResultMap.end()) {
1703 doubleKeyAdd ++;
1704 }
1705 commandResultMap.insert(i.key()*multFactor+doubleKeyAdd,i.data());
1706#endif
1707 i++;
1708 }
1709 commandChildResultMap.clear();
1710 }
1711 }
1712 // Get the Commands
1713
1714 for (int a=0;a<aCommandTree->GetCommandEntry();a++) {
1715 const G4UIcommand* command = aCommandTree->GetCommand(a+1);
1716#if QT_VERSION > 0x040000
1717 tmp = GetCommandList (command).count(text,Qt::CaseInsensitive);
1718#else
1719 tmp = GetCommandList (command).contains(text,false);
1720#endif
1721 if (tmp >0) {
1722#if QT_VERSION > 0x040000
1723 commandResultMap.insertMulti(tmp,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()));
1724#else // tricky thing for Qt3...
1725 doubleKeyAdd = 0;
1726 while (commandResultMap.find( tmp*multFactor+doubleKeyAdd) != commandResultMap.end()) {
1727 doubleKeyAdd ++;
1728 }
1729 commandResultMap.insert(tmp*multFactor+doubleKeyAdd,QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()));
1730#endif
1731 }
1732
1733 }
1734 return commandResultMap;
1735}
1736#endif
Note: See TracBrowser for help on using the repository browser.