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

Last change on this file since 881 was 876, checked in by garnier, 17 years ago

G4Qt : Fix some problems. See History file

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