00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifdef G4UI_BUILD_QT_SESSION
00035
00036 #include "G4Types.hh"
00037
00038 #include <string.h>
00039
00040 #include "G4UIQt.hh"
00041 #include "G4UImanager.hh"
00042 #include "G4StateManager.hh"
00043 #include "G4UIcommandTree.hh"
00044 #include "G4UIcommandStatus.hh"
00045
00046 #include "G4Qt.hh"
00047
00048 #include <QtGui/qapplication.h>
00049 #include <QtGui/qwidget.h>
00050 #include <QtGui/qmenu.h>
00051 #include <QtGui/qmenubar.h>
00052 #include <QtGui/qboxlayout.h>
00053 #include <QtGui/qpushbutton.h>
00054 #include <QtGui/qlabel.h>
00055 #include <QtGui/qsplitter.h>
00056 #include <QtGui/qscrollbar.h>
00057 #include <QtGui/qdialog.h>
00058 #include <QtGui/qevent.h>
00059
00060 #include <stdlib.h>
00061
00062
00063 static G4bool exitSession = true;
00064 static G4bool exitPause = true;
00065
00087 G4UIQt::G4UIQt (
00088 int argc,
00089 char** argv
00090 )
00091 :fHelpDialog(NULL)
00092 {
00093 G4Qt* interactorManager = G4Qt::getInstance ();
00094 G4UImanager* UI = G4UImanager::GetUIpointer();
00095 if(UI!=NULL) UI->SetSession(this);
00096
00097 fMainWindow = new QMainWindow();
00098 fMainWindow->setWindowTitle( "G4UI Session" );
00099 fMainWindow->resize(800,600);
00100 fMainWindow->move(QPoint(200,100));
00101
00102 QSplitter *splitter = new QSplitter(Qt::Vertical);
00103 fTextArea = new QTextEdit();
00104 QPushButton *clearButton = new QPushButton("clear");
00105 connect(clearButton, SIGNAL(clicked()), SLOT(clearButtonCallback()));
00106
00107 fCommandHistoryArea = new QListWidget();
00108 fCommandHistoryArea->setSelectionMode(QAbstractItemView::SingleSelection);
00109 connect(fCommandHistoryArea, SIGNAL(itemSelectionChanged()), SLOT(commandHistoryCallback()));
00110 fCommandHistoryArea->installEventFilter(this);
00111 fCommandLabel = new QLabel();
00112
00113 fCommandArea = new QLineEdit();
00114 fCommandArea->installEventFilter(this);
00115 fCommandArea->activateWindow();
00116 connect(fCommandArea, SIGNAL(returnPressed()), SLOT(commandEnteredCallback()));
00117
00118
00119 fTextArea->setReadOnly(true);
00120
00121
00122
00123
00124 QWidget* topWidget = new QWidget();
00125 QVBoxLayout *layoutTop = new QVBoxLayout;
00126
00127 QWidget* bottomWidget = new QWidget();
00128 QVBoxLayout *layoutBottom = new QVBoxLayout;
00129
00130
00131 layoutTop->addWidget(fTextArea);
00132 layoutTop->addWidget(clearButton);
00133 topWidget->setLayout(layoutTop);
00134
00135 layoutBottom->addWidget(fCommandHistoryArea);
00136 layoutBottom->addWidget(fCommandLabel);
00137 layoutBottom->addWidget(fCommandArea);
00138 bottomWidget->setLayout(layoutBottom);
00139
00140
00141 splitter->addWidget(topWidget);
00142 splitter->addWidget(bottomWidget);
00143 fMainWindow->setCentralWidget(splitter);
00144
00145
00146 QMenu *fileMenu = fMainWindow->menuBar()->addMenu("File");
00147 fileMenu->addAction("Quitter", fMainWindow, SLOT(close()));
00148
00149
00150 QMenu *helpMenu = fMainWindow->menuBar()->addMenu("Help");
00151 helpMenu->addAction("Show Help", this, SLOT(showHelpCallback()));
00152
00153
00154 QList<int> vals = splitter->sizes();
00155 if(vals.size()==2) {
00156 vals[0] = (splitter->orientation()==Qt::Vertical ? splitter->height() : splitter->width())*3/4;
00157 vals[1] = (splitter->orientation()==Qt::Vertical ? splitter->height() : splitter->width())*1/4;
00158 splitter->setSizes(vals);
00159 }
00160
00161
00162 if(UI!=NULL) UI->SetCoutDestination(this);
00163 }
00164
00165
00166
00167 G4UIQt::~G4UIQt(
00168 )
00169 {
00170 G4UImanager* UI = G4UImanager::GetUIpointer();
00171 if(UI!=NULL) {
00172 UI->SetSession(NULL);
00173 UI->SetCoutDestination(NULL);
00174 }
00175
00176
00177 if (fMainWindow!=NULL)
00178 delete fMainWindow;
00179 }
00180
00181
00182
00183
00187 G4UIsession* G4UIQt::SessionStart (
00188 )
00189 {
00190
00191 G4Qt* interactorManager = G4Qt::getInstance ();
00192 fMainWindow->show();
00193 Prompt("session");
00194 exitSession = false;
00195
00196
00197 printf("disable secondary loop\n");
00198 interactorManager->DisableSecondaryLoop ();
00199 ((QApplication*)interactorManager->GetMainInteractor())->exec();
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 interactorManager->EnableSecondaryLoop ();
00210 printf("enable secondary loop\n");
00211 return this;
00212 }
00213
00214
00221 void G4UIQt::Prompt (
00222 G4String aPrompt
00223 )
00224 {
00225 fCommandLabel->setText((char*)aPrompt.data());
00226 }
00227
00228
00229 void G4UIQt::SessionTerminate (
00230 )
00231 {
00232 G4Qt* interactorManager = G4Qt::getInstance ();
00233 fMainWindow->close();
00234 ((QApplication*)interactorManager->GetMainInteractor())->exit();
00235 }
00236
00237
00238
00247 void G4UIQt::PauseSessionStart (
00248 G4String aState
00249 )
00250 {
00251 printf("G4UIQt::PauseSessionStart\n");
00252 if(aState=="G4_pause> ") {
00253 SecondaryLoop ("Pause, type continue to exit this state");
00254 }
00255
00256 if(aState=="EndOfEvent") {
00257
00258 SecondaryLoop ("End of event, type continue to exit this state");
00259 }
00260 }
00261
00262
00263
00268 void G4UIQt::SecondaryLoop (
00269 G4String aPrompt
00270 )
00271 {
00272 printf("G4UIQt::SecondaryLoop\n");
00273 G4Qt* interactorManager = G4Qt::getInstance ();
00274 Prompt(aPrompt);
00275 exitPause = false;
00276 void* event;
00277 while((event = interactorManager->GetEvent())!=NULL) {
00278 interactorManager->DispatchEvent(event);
00279 if(exitPause==true) break;
00280 }
00281 Prompt("session");
00282 }
00283
00284
00285
00290 G4int G4UIQt::ReceiveG4cout (
00291 G4String aString
00292 )
00293 {
00294 fTextArea->append(QString((char*)aString.data()).trimmed());
00295 fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum());
00296 return 0;
00297 }
00298
00299
00304 G4int G4UIQt::ReceiveG4cerr (
00305 G4String aString
00306 )
00307 {
00308 QColor previousColor = fTextArea->textColor();
00309 fTextArea->setTextColor(Qt::red);
00310 fTextArea->append(QString((char*)aString.data()).trimmed());
00311 fTextArea->setTextColor(previousColor);
00312 fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum());
00313 return 0;
00314 }
00315
00316
00317
00323 void G4UIQt::AddMenu (
00324 const char* aName
00325 ,const char* aLabel
00326 )
00327 {
00328 QMenu *fileMenu = new QMenu(aLabel);
00329 fMainWindow->menuBar()->insertMenu(fMainWindow->menuBar()->actions().last(),fileMenu);
00330 AddInteractor (aName,(G4Interactor)fileMenu);
00331 }
00332
00333
00340 void G4UIQt::AddButton (
00341 const char* aMenu
00342 ,const char* aLabel
00343 ,const char* aCommand
00344 )
00345 {
00346 if(aMenu==NULL) return;
00347 if(aLabel==NULL) return;
00348 if(aCommand==NULL) return;
00349 QMenu *parent = (QMenu*)GetInteractor(aMenu);
00350 if(parent==NULL) return;
00351
00352 signalMapper = new QSignalMapper(this);
00353 QAction *action = parent->addAction(aLabel, signalMapper, SLOT(map()));
00354 signalMapper->setMapping(action, QString(aCommand));
00355 connect(signalMapper, SIGNAL(mapped(const QString &)),this, SLOT(buttonCallback(const QString&)));
00356 }
00357
00358
00359
00360
00368 void G4UIQt::TerminalHelp(
00369 G4String newCommand
00370 )
00371 {
00372 if (!fHelpDialog) {
00373 fHelpDialog = new QDialog;
00374
00375 QSplitter *splitter = new QSplitter(Qt::Horizontal);
00376 fHelpArea = new QTextEdit();
00377 QPushButton *exitButton = new QPushButton("Exit");
00378 connect(exitButton, SIGNAL(clicked()), fHelpDialog,SLOT(close()));
00379 fHelpArea->setReadOnly(true);
00380
00381
00382 G4UImanager* UI = G4UImanager::GetUIpointer();
00383 if(UI==NULL) return;
00384 G4UIcommandTree * treeTop = UI->GetTree();
00385
00386
00387 fHelpTreeWidget = new QTreeWidget();
00388 fHelpTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
00389 fHelpTreeWidget->setColumnCount(2);
00390 fHelpTreeWidget->setColumnHidden(1,true);
00391 QStringList labels;
00392 labels << QString("Command") << QString("Description");
00393 fHelpTreeWidget->setHeaderLabels(labels);
00394
00395 QList<QTreeWidgetItem *> items;
00396 G4int treeSize = treeTop->GetTreeEntry();
00397 QTreeWidgetItem * newItem;
00398 for (int a=0;a<treeSize;a++) {
00399
00400 QStringList stringList;
00401 stringList << QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).trimmed() ;
00402 stringList << QString((char*)(treeTop->GetTree(a+1)->GetTitle()).data()).trimmed() ;
00403
00404 newItem = new QTreeWidgetItem(stringList);
00405
00406
00407 CreateChildTree(newItem,treeTop->GetTree(a+1));
00408 items.append(newItem);
00409 }
00410 fHelpTreeWidget->insertTopLevelItems(0, items);
00411
00412
00413
00414
00415 connect(fHelpTreeWidget, SIGNAL(itemSelectionChanged ()),this, SLOT(helpTreeCallback()));
00416
00417
00418
00419 QVBoxLayout *vLayout = new QVBoxLayout;
00420
00421 splitter->addWidget(fHelpTreeWidget);
00422 splitter->addWidget(fHelpArea);
00423
00424 vLayout->addWidget(splitter);
00425 vLayout->addWidget(exitButton);
00426 fHelpDialog->setLayout(vLayout);
00427
00428 }
00429
00430
00431 size_t i = newCommand.index(" ");
00432 G4String targetCom="";
00433 if( i != std::string::npos )
00434 {
00435 G4String newValue = newCommand(i+1,newCommand.length()-(i+1));
00436 newValue.strip(G4String::both);
00437 targetCom = ModifyToFullPathCommand( newValue );
00438 printf("test : av:%s-- ap:%s--\n",((char*)newValue.data()),((char*)targetCom.data()));
00439 }
00440 if (targetCom != "") {
00441 QList<QTreeWidgetItem *> list = fHelpTreeWidget->findItems(QString(((char*)targetCom.data())),Qt::MatchFixedString,0);
00442 for (int a=0;a<13;a++) {
00443 printf("verif.... =%s= +%s+\n",fHelpTreeWidget->topLevelItem(a)->text(0).toStdString().c_str(),((char*)targetCom.data()));
00444 }
00445
00446 if (!list.isEmpty()) {
00447 if (list.first()->childCount() >0)
00448 list.first()->setExpanded(true);
00449
00450
00451 QList<QTreeWidgetItem *> selected;
00452 selected = fHelpTreeWidget->selectedItems();
00453 if ( selected.count() != 0 ) {
00454 fHelpTreeWidget->collapseItem (selected.at( 0 ) );
00455 }
00456
00457
00458 fHelpTreeWidget->clearSelection();
00459 list.first()->setSelected(true);
00460
00461
00462 helpTreeCallback();
00463 }
00464 }
00465 fHelpDialog->setWindowTitle("Help on commands");
00466 fHelpDialog->resize(800,600);
00467 fHelpDialog->move(QPoint(400,150));
00468 fHelpDialog->show();
00469 fHelpDialog->raise();
00470 fHelpDialog->activateWindow();
00471 }
00472
00473
00474
00480 void G4UIQt::CreateChildTree(
00481 QTreeWidgetItem *aParent
00482 ,G4UIcommandTree *aCommandTree
00483 )
00484 {
00485
00486
00487 QTreeWidgetItem * newItem;
00488
00489
00490
00491 for (int a=0;a<aCommandTree->GetTreeEntry();a++) {
00492
00493 QStringList stringList;
00494 stringList << QString((char*)(aCommandTree->GetTree(a+1)->GetPathName()).data()).trimmed() ;
00495 stringList << QString((char*)(aCommandTree->GetTree(a+1)->GetTitle()).data()).trimmed() ;
00496 newItem = new QTreeWidgetItem(stringList);
00497
00498 CreateChildTree(newItem,aCommandTree->GetTree(a+1));
00499 aParent->addChild(newItem);
00500 }
00501
00502
00503
00504
00505
00506 for (int a=0;a<aCommandTree->GetCommandEntry();a++) {
00507
00508 QStringList stringList;
00509 stringList << QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).trimmed() ;
00510 stringList << QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).trimmed() ;
00511 newItem = new QTreeWidgetItem(stringList);
00512
00513 aParent->addChild(newItem);
00514 newItem->setExpanded(false);
00515 }
00516 }
00517
00518
00526 QString G4UIQt::GetCommandList (
00527 G4UIcommand *aCommand
00528 )
00529 {
00530
00531 QString txt;
00532 G4String commandPath = aCommand->GetCommandPath();
00533 G4String rangeString = aCommand->GetRange();
00534
00535 if((commandPath.length()-1)!='/')
00536 {
00537 txt += "Command " + QString((char*)(commandPath).data()) + "\n";
00538 }
00539 txt += "Guidance :\n";
00540 G4int n_guidanceEntry = aCommand->GetGuidanceEntries();
00541
00542 for( G4int i_thGuidance=0; i_thGuidance < n_guidanceEntry; i_thGuidance++ )
00543 { txt += QString((char*)(aCommand->GetGuidanceLine(i_thGuidance)).data()) + "\n"; }
00544 if( ! rangeString.isNull() )
00545 { txt += " Range of parameters : " + QString((char*)(rangeString).data()) + "\n"; }
00546 G4int n_parameterEntry = aCommand->GetParameterEntries();
00547 if( n_parameterEntry > 0 )
00548 {
00549 G4UIparameter *param;
00550
00551
00552
00553 for( G4int i_thParameter=0; i_thParameter<n_parameterEntry; i_thParameter++ )
00554 { param = aCommand->GetParameter(i_thParameter);
00555 txt += "\nParameter : " + QString((char*)(param->GetParameterName()).data()) + "\n";
00556 if( ! param->GetParameterGuidance().isNull() )
00557 txt += QString((char*)(param->GetParameterGuidance()).data())+ "\n" ;
00558 txt += " Parameter type : " + QString(param->GetParameterType())+ "\n";
00559 if(param->IsOmittable())
00560 { txt += " Omittable : True\n"; }
00561 else
00562 { txt += " Omittable : False\n"; }
00563 if( param->GetCurrentAsDefault() )
00564 { txt += " Default value : taken from the current value\n"; }
00565 else if( ! param->GetDefaultValue().isNull() )
00566 { txt += " Default value : " + QString((char*)(param->GetDefaultValue()).data())+ "\n"; }
00567 if( ! param->GetParameterRange().isNull() )
00568 txt += " Parameter range : " + QString((char*)(param->GetParameterRange()).data())+ "\n";
00569 if( ! param->GetParameterCandidates().isNull() )
00570 txt += " Candidates : " + QString((char*)(param->GetParameterCandidates()).data())+ "\n";
00571 }
00572 }
00573 return txt;
00574 }
00575
00576
00577
00581 G4bool G4UIQt::GetHelpChoice(
00582 G4int& aInt
00583 )
00584 {
00585 printf("G4UIQt::GetHelpChoice SHOULD NEVER GO HERE");
00586 return true;
00587 }
00588
00589
00593 void G4UIQt::ExitHelp(
00594 )
00595 {
00596 printf("G4UIQt::ExitHelp SHOULD NEVER GO HERE");
00597 }
00598
00599
00608 bool G4UIQt::eventFilter(
00609 QObject *obj
00610 ,QEvent *event
00611 )
00612 {
00613 if (obj == fCommandHistoryArea) {
00614 if (event->type() == QEvent::KeyPress) {
00615 fCommandArea->setFocus();
00616 }
00617 }
00618 if (obj == fCommandArea) {
00619 if (event->type() == QEvent::KeyPress) {
00620 QKeyEvent *e = static_cast<QKeyEvent*>(event);
00621 if ((e->key() == (Qt::Key_Down)) ||
00622 (e->key() == (Qt::Key_PageDown)) ||
00623 (e->key() == (Qt::Key_Up)) ||
00624 (e->key() == (Qt::Key_PageUp))) {
00625 int selection = fCommandHistoryArea->currentRow();
00626 for (int a=0;a<fCommandHistoryArea->count();a++) {
00627
00628 }
00629 if (fCommandHistoryArea->count()) {
00630 if (selection == -1) {
00631 selection = fCommandHistoryArea->count()-1;
00632 }
00633 if (e->key() == (Qt::Key_Down)) {
00634 if (selection <(fCommandHistoryArea->count()-1))
00635 selection++;
00636 } else if (e->key() == (Qt::Key_PageDown)) {
00637 selection = fCommandHistoryArea->count()-1;
00638 } else if (e->key() == (Qt::Key_Up)) {
00639 if (selection >0)
00640 selection --;
00641 } else if (e->key() == (Qt::Key_PageUp)) {
00642 selection = 0;
00643 }
00644 fCommandHistoryArea->clearSelection();
00645 fCommandHistoryArea->item(selection)->setSelected(true);
00646 fCommandHistoryArea->setCurrentItem(fCommandHistoryArea->item(selection));
00647 }
00648 }
00649 }
00650 }
00651
00652 return QObject::eventFilter(obj, event);
00653 }
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00667 void G4UIQt::showHelpCallback (
00668 )
00669 {
00670 TerminalHelp("");
00671 }
00672
00673
00677 void G4UIQt::clearButtonCallback (
00678 )
00679 {
00680 fTextArea->clear();
00681 }
00682
00683
00688 void G4UIQt::commandEnteredCallback (
00689 )
00690 {
00691 G4String command (fCommandArea->text().toStdString().c_str());
00692 if (fCommandArea->text().trimmed() != "") {
00693 fCommandHistoryArea->addItem(fCommandArea->text());
00694 fCommandHistoryArea->clearSelection();
00695 fCommandHistoryArea->item(fCommandHistoryArea->count()-1)->setSelected(true);
00696 fCommandHistoryArea->setCurrentItem(fCommandHistoryArea->item(fCommandHistoryArea->count()-1));
00697
00698 if (command(0,4) != "help") {
00699 ApplyShellCommand (command,exitSession,exitPause);
00700 } else {
00701 TerminalHelp(command);
00702 }
00703 if(exitSession==true)
00704 SessionTerminate();
00705 }
00706 fCommandArea->setText("");
00707 }
00708
00709
00715 void G4UIQt::buttonCallback (
00716 const QString& aCommand
00717 )
00718 {
00719 G4String ss = G4String(aCommand.toStdString().c_str());
00720 printf ("debug : execute:\n-%s- %d %d \n",ss.data(),exitSession,exitPause);
00721 ApplyShellCommand(ss,exitSession,exitPause);
00722 if(exitSession==true)
00723 SessionTerminate();
00724 }
00725
00726
00727
00731 void G4UIQt::helpTreeCallback (
00732 )
00733 {
00734
00735 QTreeWidgetItem* item = NULL;
00736 if (!fHelpTreeWidget)
00737 return ;
00738
00739 if (!fHelpArea)
00740 return;
00741
00742 QList<QTreeWidgetItem *> list =fHelpTreeWidget->selectedItems();
00743 if (list.isEmpty())
00744 return;
00745 item = list.first();
00746 if (!item)
00747 return;
00748
00749 G4UImanager* UI = G4UImanager::GetUIpointer();
00750 if(UI==NULL) return;
00751 G4UIcommandTree * treeTop = UI->GetTree();
00752 G4UIcommand* command = treeTop->FindPath(item->text (1).toStdString().c_str());
00753 if (command) {
00754 fHelpArea->setText(GetCommandList(command));
00755 } else {
00756
00757
00758 fHelpArea->setText(item->text (1).toStdString().c_str());
00759 }
00760 }
00761
00762
00767 void G4UIQt::commandHistoryCallback(
00768 )
00769 {
00770 QListWidgetItem* item = NULL;
00771 if (!fCommandHistoryArea)
00772 return ;
00773
00774
00775 QList<QListWidgetItem *> list =fCommandHistoryArea->selectedItems();
00776 if (list.isEmpty())
00777 return;
00778 item = list.first();
00779 if (!item)
00780 return;
00781 fCommandArea->setText(item->text());
00782
00783 }
00784
00785 #endif