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

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

append qt3 fix

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