[481] | 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 | // |
---|
[593] | 27 | // $Id: G4UIQt.cc,v 1.3 2007/11/08 17:00:09 lgarnier Exp $ |
---|
| 28 | // GEANT4 tag $Name: $ |
---|
[481] | 29 | // |
---|
[484] | 30 | // L. Garnier |
---|
[481] | 31 | |
---|
| 32 | //#define DEBUG |
---|
| 33 | |
---|
| 34 | #ifdef G4UI_BUILD_QT_SESSION |
---|
| 35 | |
---|
| 36 | #include "G4Types.hh" |
---|
| 37 | |
---|
| 38 | #include <string.h> |
---|
| 39 | |
---|
| 40 | #include "G4UIQt.hh" |
---|
| 41 | #include "G4UImanager.hh" |
---|
| 42 | #include "G4StateManager.hh" |
---|
| 43 | #include "G4UIcommandTree.hh" |
---|
| 44 | #include "G4UIcommandStatus.hh" |
---|
| 45 | |
---|
| 46 | #include "G4Qt.hh" |
---|
| 47 | |
---|
[593] | 48 | #include <QtGui/QApplication.h> |
---|
| 49 | #include <QtGui/QMainWindow.h> |
---|
| 50 | #include <QtGui/QLineEdit.h> |
---|
| 51 | #include <QtGui/QWidget.h> |
---|
| 52 | #include <QtGui/QMenu.h> |
---|
| 53 | #include <QtGui/QMenuBar.h> |
---|
| 54 | #include <QtGui/QBoxLayout.h> |
---|
| 55 | #include <QtGui/QPushButton.h> |
---|
| 56 | #include <QtGui/QLabel.h> |
---|
| 57 | #include <QtGui/QSplitter.h> |
---|
| 58 | #include <QtGui/QScrollBar.h> |
---|
| 59 | #include <QtGui/QDialog.h> |
---|
| 60 | #include <QtGui/QEvent.h> |
---|
| 61 | #include <QtGui/QListWidget.h> |
---|
| 62 | #include <QtGui/QTextEdit.h> |
---|
| 63 | #include <QtGui/QTreeWidget.h> |
---|
| 64 | #include <QtCore/QSignalMapper.h> |
---|
[481] | 65 | |
---|
[593] | 66 | |
---|
[481] | 67 | #include <stdlib.h> |
---|
| 68 | |
---|
[513] | 69 | // Pourquoi Static et non variables de classe ? |
---|
[481] | 70 | static G4bool exitSession = true; |
---|
| 71 | static G4bool exitPause = true; |
---|
[524] | 72 | |
---|
| 73 | /** Build a Qt window with a menubar, output area and promt area<br> |
---|
| 74 | <pre> |
---|
| 75 | +-----------------------+ |
---|
| 76 | |exit menu| | |
---|
| 77 | | | |
---|
| 78 | | +-------------------+ | |
---|
| 79 | | | | | |
---|
| 80 | | | Output area | | |
---|
| 81 | | | | | |
---|
| 82 | | +-------------------+ | |
---|
| 83 | | | clear | | |
---|
| 84 | | +-------------------+ | |
---|
| 85 | | | promt history | | |
---|
| 86 | | +-------------------+ | |
---|
| 87 | | +-------------------+ | |
---|
| 88 | | |> promt area | | |
---|
| 89 | | +-------------------+ | |
---|
| 90 | +-----------------------+ |
---|
| 91 | </pre> |
---|
[481] | 92 | */ |
---|
| 93 | G4UIQt::G4UIQt ( |
---|
[527] | 94 | int argc |
---|
| 95 | ,char** argv |
---|
| 96 | ) |
---|
[513] | 97 | :fHelpDialog(NULL) |
---|
[481] | 98 | { |
---|
[496] | 99 | G4Qt* interactorManager = G4Qt::getInstance (); |
---|
[505] | 100 | G4UImanager* UI = G4UImanager::GetUIpointer(); |
---|
| 101 | if(UI!=NULL) UI->SetSession(this); |
---|
[481] | 102 | |
---|
[494] | 103 | fMainWindow = new QMainWindow(); |
---|
| 104 | fMainWindow->setWindowTitle( "G4UI Session" ); |
---|
[500] | 105 | fMainWindow->resize(800,600); |
---|
[571] | 106 | fMainWindow->move(QPoint(50,100)); |
---|
[496] | 107 | |
---|
[498] | 108 | QSplitter *splitter = new QSplitter(Qt::Vertical); |
---|
| 109 | fTextArea = new QTextEdit(); |
---|
[490] | 110 | QPushButton *clearButton = new QPushButton("clear"); |
---|
[526] | 111 | connect(clearButton, SIGNAL(clicked()), SLOT(ClearButtonCallback())); |
---|
[498] | 112 | |
---|
[519] | 113 | fCommandHistoryArea = new QListWidget(); |
---|
| 114 | fCommandHistoryArea->setSelectionMode(QAbstractItemView::SingleSelection); |
---|
[526] | 115 | connect(fCommandHistoryArea, SIGNAL(itemSelectionChanged()), SLOT(CommandHistoryCallback())); |
---|
[523] | 116 | fCommandHistoryArea->installEventFilter(this); |
---|
[498] | 117 | fCommandLabel = new QLabel(); |
---|
| 118 | |
---|
[518] | 119 | fCommandArea = new QLineEdit(); |
---|
| 120 | fCommandArea->installEventFilter(this); |
---|
[500] | 121 | fCommandArea->activateWindow(); |
---|
[526] | 122 | connect(fCommandArea, SIGNAL(returnPressed()), SLOT(CommandEnteredCallback())); |
---|
[578] | 123 | fCommandArea->setFocusPolicy ( Qt::StrongFocus ); |
---|
| 124 | fCommandArea->setFocus(Qt::TabFocusReason); |
---|
[498] | 125 | fTextArea->setReadOnly(true); |
---|
| 126 | |
---|
[505] | 127 | |
---|
| 128 | // Set layouts |
---|
[497] | 129 | |
---|
[498] | 130 | QWidget* topWidget = new QWidget(); |
---|
| 131 | QVBoxLayout *layoutTop = new QVBoxLayout; |
---|
[481] | 132 | |
---|
[498] | 133 | QWidget* bottomWidget = new QWidget(); |
---|
| 134 | QVBoxLayout *layoutBottom = new QVBoxLayout; |
---|
[494] | 135 | |
---|
[490] | 136 | |
---|
[498] | 137 | layoutTop->addWidget(fTextArea); |
---|
| 138 | layoutTop->addWidget(clearButton); |
---|
| 139 | topWidget->setLayout(layoutTop); |
---|
[494] | 140 | |
---|
[498] | 141 | layoutBottom->addWidget(fCommandHistoryArea); |
---|
| 142 | layoutBottom->addWidget(fCommandLabel); |
---|
| 143 | layoutBottom->addWidget(fCommandArea); |
---|
| 144 | bottomWidget->setLayout(layoutBottom); |
---|
[490] | 145 | |
---|
[495] | 146 | |
---|
[522] | 147 | splitter->addWidget(topWidget); |
---|
| 148 | splitter->addWidget(bottomWidget); |
---|
[498] | 149 | fMainWindow->setCentralWidget(splitter); |
---|
[495] | 150 | |
---|
[513] | 151 | // Add a quit subMenu |
---|
[504] | 152 | QMenu *fileMenu = fMainWindow->menuBar()->addMenu("File"); |
---|
[503] | 153 | fileMenu->addAction("Quitter", fMainWindow, SLOT(close())); |
---|
[496] | 154 | |
---|
[513] | 155 | // Add a Help menu |
---|
| 156 | QMenu *helpMenu = fMainWindow->menuBar()->addMenu("Help"); |
---|
[526] | 157 | helpMenu->addAction("Show Help", this, SLOT(ShowHelpCallback())); |
---|
[513] | 158 | |
---|
[505] | 159 | // Set the splitter size. The fTextArea sould be 2/3 on the fMainWindow |
---|
| 160 | QList<int> vals = splitter->sizes(); |
---|
| 161 | if(vals.size()==2) { |
---|
| 162 | vals[0] = (splitter->orientation()==Qt::Vertical ? splitter->height() : splitter->width())*3/4; |
---|
| 163 | vals[1] = (splitter->orientation()==Qt::Vertical ? splitter->height() : splitter->width())*1/4; |
---|
| 164 | splitter->setSizes(vals); |
---|
| 165 | } |
---|
[481] | 166 | |
---|
| 167 | |
---|
[484] | 168 | if(UI!=NULL) UI->SetCoutDestination(this); // TO KEEP |
---|
[481] | 169 | } |
---|
[524] | 170 | |
---|
| 171 | |
---|
| 172 | |
---|
[481] | 173 | G4UIQt::~G4UIQt( |
---|
| 174 | ) |
---|
| 175 | { |
---|
[484] | 176 | G4UImanager* UI = G4UImanager::GetUIpointer(); // TO KEEP |
---|
| 177 | if(UI!=NULL) { // TO KEEP |
---|
| 178 | UI->SetSession(NULL); // TO KEEP |
---|
| 179 | UI->SetCoutDestination(NULL); // TO KEEP |
---|
[481] | 180 | } |
---|
[484] | 181 | |
---|
[494] | 182 | if (fMainWindow!=NULL) |
---|
| 183 | delete fMainWindow; |
---|
[481] | 184 | } |
---|
[524] | 185 | |
---|
| 186 | |
---|
| 187 | |
---|
| 188 | /** Start the Qt main loop |
---|
| 189 | */ |
---|
[481] | 190 | G4UIsession* G4UIQt::SessionStart ( |
---|
| 191 | ) |
---|
| 192 | { |
---|
[484] | 193 | |
---|
[496] | 194 | G4Qt* interactorManager = G4Qt::getInstance (); |
---|
[494] | 195 | fMainWindow->show(); |
---|
| 196 | Prompt("session"); |
---|
| 197 | exitSession = false; |
---|
[481] | 198 | |
---|
[496] | 199 | |
---|
| 200 | printf("disable secondary loop\n"); |
---|
| 201 | interactorManager->DisableSecondaryLoop (); // TO KEEP |
---|
| 202 | ((QApplication*)interactorManager->GetMainInteractor())->exec(); |
---|
| 203 | // on ne passe pas le dessous ? FIXME ???? |
---|
[506] | 204 | // je ne pense pas 13/06 |
---|
[496] | 205 | |
---|
[524] | 206 | // void* event; // TO KEEP |
---|
| 207 | // while((event = interactorManager->GetEvent())!=NULL) { // TO KEEP |
---|
| 208 | // interactorManager->DispatchEvent(event); // TO KEEP |
---|
| 209 | // if(exitSession==true) break; // TO KEEP |
---|
| 210 | // } // TO KEEP |
---|
[506] | 211 | |
---|
| 212 | interactorManager->EnableSecondaryLoop (); |
---|
[496] | 213 | printf("enable secondary loop\n"); |
---|
[506] | 214 | return this; |
---|
[481] | 215 | } |
---|
[494] | 216 | |
---|
[524] | 217 | |
---|
| 218 | /** Display the prompt in the prompt area |
---|
| 219 | @param aPrompt : string to display as the promt label |
---|
| 220 | //FIXME : probablement inutile puisque le seul a afficher qq chose d'autre |
---|
| 221 | que "session" est SecondaryLoop() |
---|
| 222 | */ |
---|
[481] | 223 | void G4UIQt::Prompt ( |
---|
| 224 | G4String aPrompt |
---|
| 225 | ) |
---|
| 226 | { |
---|
[527] | 227 | if (!aPrompt) return; |
---|
[526] | 228 | |
---|
[500] | 229 | fCommandLabel->setText((char*)aPrompt.data()); |
---|
[481] | 230 | } |
---|
[524] | 231 | |
---|
| 232 | |
---|
[481] | 233 | void G4UIQt::SessionTerminate ( |
---|
| 234 | ) |
---|
| 235 | { |
---|
[505] | 236 | G4Qt* interactorManager = G4Qt::getInstance (); |
---|
| 237 | fMainWindow->close(); |
---|
| 238 | ((QApplication*)interactorManager->GetMainInteractor())->exit(); |
---|
[481] | 239 | } |
---|
[524] | 240 | |
---|
| 241 | |
---|
| 242 | |
---|
| 243 | /** |
---|
| 244 | Called by intercoms/src/G4UImanager.cc<br> |
---|
| 245 | Called by visualization/management/src/G4VisCommands.cc with "EndOfEvent" argument<br> |
---|
| 246 | It have to pause the session command terminal.<br> |
---|
| 247 | Call SecondaryLoop to wait for exit event<br> |
---|
| 248 | @param aState |
---|
| 249 | @see : G4VisCommandReviewKeptEvents::SetNewValue |
---|
| 250 | */ |
---|
[481] | 251 | void G4UIQt::PauseSessionStart ( |
---|
[524] | 252 | G4String aState |
---|
[481] | 253 | ) |
---|
| 254 | { |
---|
[527] | 255 | if (!aState) return; |
---|
[526] | 256 | |
---|
[501] | 257 | printf("G4UIQt::PauseSessionStart\n"); |
---|
[524] | 258 | if(aState=="G4_pause> ") { // TO KEEP |
---|
[484] | 259 | SecondaryLoop ("Pause, type continue to exit this state"); // TO KEEP |
---|
| 260 | } // TO KEEP |
---|
[481] | 261 | |
---|
[524] | 262 | if(aState=="EndOfEvent") { // TO KEEP |
---|
[481] | 263 | // Picking with feed back in event data Done here !!! |
---|
[484] | 264 | SecondaryLoop ("End of event, type continue to exit this state"); // TO KEEP |
---|
| 265 | } // TO KEEP |
---|
[481] | 266 | } |
---|
[524] | 267 | |
---|
| 268 | |
---|
| 269 | |
---|
| 270 | /** |
---|
| 271 | Begin the secondary loop |
---|
| 272 | @param a_prompt : label to display as the prompt label |
---|
| 273 | */ |
---|
[484] | 274 | void G4UIQt::SecondaryLoop ( |
---|
[524] | 275 | G4String aPrompt |
---|
[481] | 276 | ) |
---|
| 277 | { |
---|
[527] | 278 | if (!aPrompt) return; |
---|
[526] | 279 | |
---|
[501] | 280 | printf("G4UIQt::SecondaryLoop\n"); |
---|
[484] | 281 | G4Qt* interactorManager = G4Qt::getInstance (); // TO KEEP ? |
---|
[524] | 282 | Prompt(aPrompt); // TO KEEP |
---|
[484] | 283 | exitPause = false; // TO KEEP |
---|
| 284 | void* event; // TO KEEP |
---|
| 285 | while((event = interactorManager->GetEvent())!=NULL) { // TO KEEP |
---|
| 286 | interactorManager->DispatchEvent(event); // TO KEEP |
---|
| 287 | if(exitPause==true) break; // TO KEEP |
---|
| 288 | } // TO KEEP |
---|
| 289 | Prompt("session"); // TO KEEP |
---|
[481] | 290 | } |
---|
[524] | 291 | |
---|
| 292 | |
---|
| 293 | |
---|
[481] | 294 | /** |
---|
[524] | 295 | Receive a cout from Geant4. We have to display it in the cout zone |
---|
| 296 | @param aString : label to add in the display area |
---|
[526] | 297 | @return 0 |
---|
[524] | 298 | */ |
---|
[484] | 299 | G4int G4UIQt::ReceiveG4cout ( |
---|
[524] | 300 | G4String aString |
---|
[481] | 301 | ) |
---|
| 302 | { |
---|
[527] | 303 | if (!aString) return 0; |
---|
[578] | 304 | G4Qt* interactorManager = G4Qt::getInstance (); |
---|
| 305 | if (!interactorManager) return 0; |
---|
| 306 | |
---|
[579] | 307 | // printf(" **************** G4 Cout : %s\n",(char*)aString.data()); |
---|
[575] | 308 | fTextArea->append(QString((char*)aString.data()).trimmed()); |
---|
| 309 | fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum()); |
---|
[578] | 310 | interactorManager->FlushAndWaitExecution(); |
---|
[495] | 311 | return 0; |
---|
[481] | 312 | } |
---|
[524] | 313 | |
---|
| 314 | |
---|
[481] | 315 | /** |
---|
[524] | 316 | Receive a cerr from Geant4. We have to display it in the cout zone |
---|
| 317 | @param aString : label to add in the display area |
---|
[526] | 318 | @return 0 |
---|
[524] | 319 | */ |
---|
[484] | 320 | G4int G4UIQt::ReceiveG4cerr ( |
---|
[524] | 321 | G4String aString |
---|
[481] | 322 | ) |
---|
| 323 | { |
---|
[527] | 324 | if (!aString) return 0; |
---|
[578] | 325 | G4Qt* interactorManager = G4Qt::getInstance (); |
---|
| 326 | if (!interactorManager) return 0; |
---|
[526] | 327 | |
---|
[500] | 328 | QColor previousColor = fTextArea->textColor(); |
---|
| 329 | fTextArea->setTextColor(Qt::red); |
---|
[524] | 330 | fTextArea->append(QString((char*)aString.data()).trimmed()); |
---|
[500] | 331 | fTextArea->setTextColor(previousColor); |
---|
[501] | 332 | fTextArea->verticalScrollBar()->setSliderPosition(fTextArea->verticalScrollBar()->maximum()); |
---|
[578] | 333 | interactorManager->FlushAndWaitExecution(); |
---|
[495] | 334 | return 0; |
---|
[481] | 335 | } |
---|
[501] | 336 | |
---|
[524] | 337 | |
---|
| 338 | |
---|
| 339 | /** |
---|
| 340 | Add a new menu to the menu bar |
---|
| 341 | @param aName name of menu |
---|
| 342 | @param aLabel label to display |
---|
| 343 | */ |
---|
[484] | 344 | void G4UIQt::AddMenu ( |
---|
[524] | 345 | const char* aName |
---|
| 346 | ,const char* aLabel |
---|
[481] | 347 | ) |
---|
| 348 | { |
---|
[526] | 349 | if (aName == NULL) return; |
---|
| 350 | if (aLabel == NULL) return; |
---|
| 351 | |
---|
[524] | 352 | QMenu *fileMenu = new QMenu(aLabel); |
---|
[513] | 353 | fMainWindow->menuBar()->insertMenu(fMainWindow->menuBar()->actions().last(),fileMenu); |
---|
[524] | 354 | AddInteractor (aName,(G4Interactor)fileMenu); |
---|
[481] | 355 | } |
---|
[524] | 356 | |
---|
| 357 | |
---|
| 358 | /** |
---|
| 359 | Add a new button to a menu |
---|
| 360 | @param aMenu : parent menu |
---|
| 361 | @param aLabel : label to display |
---|
| 362 | @param aCommand : command to execute as a callback |
---|
| 363 | */ |
---|
[484] | 364 | void G4UIQt::AddButton ( |
---|
[524] | 365 | const char* aMenu |
---|
| 366 | ,const char* aLabel |
---|
| 367 | ,const char* aCommand |
---|
[481] | 368 | ) |
---|
| 369 | { |
---|
[524] | 370 | if(aMenu==NULL) return; // TO KEEP |
---|
| 371 | if(aLabel==NULL) return; // TO KEEP |
---|
| 372 | if(aCommand==NULL) return; // TO KEEP |
---|
[526] | 373 | |
---|
[524] | 374 | QMenu *parent = (QMenu*)GetInteractor(aMenu); |
---|
[504] | 375 | if(parent==NULL) return; |
---|
| 376 | |
---|
[526] | 377 | QSignalMapper *signalMapper = new QSignalMapper(this); |
---|
[524] | 378 | QAction *action = parent->addAction(aLabel, signalMapper, SLOT(map())); |
---|
| 379 | signalMapper->setMapping(action, QString(aCommand)); |
---|
[526] | 380 | connect(signalMapper, SIGNAL(mapped(const QString &)),this, SLOT(ButtonCallback(const QString&))); |
---|
[491] | 381 | } |
---|
[504] | 382 | |
---|
| 383 | |
---|
| 384 | |
---|
[513] | 385 | |
---|
| 386 | /** |
---|
[524] | 387 | Open the help dialog in a separate window.<br> |
---|
| 388 | This will be display as a tree widget.<br> |
---|
| 389 | Implementation of <b>void G4VBasicShell::TerminalHelp(G4String newCommand)</b> |
---|
[506] | 390 | |
---|
[524] | 391 | @param newCommand : open the tree widget item on this command if is set |
---|
| 392 | */ |
---|
| 393 | void G4UIQt::TerminalHelp( |
---|
| 394 | G4String newCommand |
---|
| 395 | ) |
---|
[506] | 396 | { |
---|
[524] | 397 | // Create the help dialog |
---|
[514] | 398 | if (!fHelpDialog) { |
---|
[579] | 399 | fHelpDialog = new QDialog(fMainWindow,Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint); |
---|
[514] | 400 | QSplitter *splitter = new QSplitter(Qt::Horizontal); |
---|
| 401 | fHelpArea = new QTextEdit(); |
---|
| 402 | QPushButton *exitButton = new QPushButton("Exit"); |
---|
| 403 | connect(exitButton, SIGNAL(clicked()), fHelpDialog,SLOT(close())); |
---|
| 404 | fHelpArea->setReadOnly(true); |
---|
[506] | 405 | |
---|
[514] | 406 | // the help tree |
---|
| 407 | G4UImanager* UI = G4UImanager::GetUIpointer(); |
---|
| 408 | if(UI==NULL) return; |
---|
| 409 | G4UIcommandTree * treeTop = UI->GetTree(); |
---|
[507] | 410 | |
---|
[514] | 411 | // build widget |
---|
| 412 | fHelpTreeWidget = new QTreeWidget(); |
---|
[515] | 413 | fHelpTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection); |
---|
[514] | 414 | fHelpTreeWidget->setColumnCount(2); |
---|
| 415 | fHelpTreeWidget->setColumnHidden(1,true); |
---|
| 416 | QStringList labels; |
---|
| 417 | labels << QString("Command") << QString("Description"); |
---|
| 418 | fHelpTreeWidget->setHeaderLabels(labels); |
---|
| 419 | |
---|
| 420 | QList<QTreeWidgetItem *> items; |
---|
| 421 | G4int treeSize = treeTop->GetTreeEntry(); |
---|
| 422 | QTreeWidgetItem * newItem; |
---|
| 423 | for (int a=0;a<treeSize;a++) { |
---|
| 424 | // Creating new item |
---|
| 425 | QStringList stringList; |
---|
| 426 | stringList << QString((char*)(treeTop->GetTree(a+1)->GetPathName()).data()).trimmed() ; |
---|
| 427 | stringList << QString((char*)(treeTop->GetTree(a+1)->GetTitle()).data()).trimmed() ; |
---|
| 428 | |
---|
| 429 | newItem = new QTreeWidgetItem(stringList); |
---|
| 430 | |
---|
| 431 | // look for childs |
---|
| 432 | CreateChildTree(newItem,treeTop->GetTree(a+1)); |
---|
| 433 | items.append(newItem); |
---|
| 434 | } |
---|
| 435 | fHelpTreeWidget->insertTopLevelItems(0, items); |
---|
| 436 | |
---|
[575] | 437 | connect(fHelpTreeWidget, SIGNAL(itemSelectionChanged ()),this, SLOT(HelpTreeClicCallback())); |
---|
[577] | 438 | connect(fHelpTreeWidget, SIGNAL(itemDoubleClicked (QTreeWidgetItem*,int)),this, SLOT(HelpTreeDoubleClicCallback(QTreeWidgetItem*,int))); |
---|
[514] | 439 | |
---|
| 440 | // Set layouts |
---|
| 441 | |
---|
| 442 | QVBoxLayout *vLayout = new QVBoxLayout; |
---|
| 443 | |
---|
[522] | 444 | splitter->addWidget(fHelpTreeWidget); |
---|
| 445 | splitter->addWidget(fHelpArea); |
---|
[514] | 446 | |
---|
| 447 | vLayout->addWidget(splitter); |
---|
| 448 | vLayout->addWidget(exitButton); |
---|
| 449 | fHelpDialog->setLayout(vLayout); |
---|
| 450 | |
---|
| 451 | } |
---|
| 452 | |
---|
[513] | 453 | // Look for the choosen command "newCommand" |
---|
| 454 | size_t i = newCommand.index(" "); |
---|
[514] | 455 | G4String targetCom=""; |
---|
[513] | 456 | if( i != std::string::npos ) |
---|
[524] | 457 | { |
---|
| 458 | G4String newValue = newCommand(i+1,newCommand.length()-(i+1)); |
---|
| 459 | newValue.strip(G4String::both); |
---|
| 460 | targetCom = ModifyToFullPathCommand( newValue ); |
---|
| 461 | } |
---|
[514] | 462 | if (targetCom != "") { |
---|
[524] | 463 | QTreeWidgetItem* findItem = NULL; |
---|
| 464 | for (int a=0;a<fHelpTreeWidget->topLevelItemCount();a++) { |
---|
| 465 | if (!findItem) { |
---|
| 466 | findItem = FindTreeItem(fHelpTreeWidget->topLevelItem(a),QString((char*)targetCom.data())); |
---|
| 467 | } |
---|
[514] | 468 | } |
---|
[525] | 469 | |
---|
[524] | 470 | if (findItem) { |
---|
[525] | 471 | |
---|
[515] | 472 | //collapsed open item |
---|
| 473 | QList<QTreeWidgetItem *> selected; |
---|
| 474 | selected = fHelpTreeWidget->selectedItems(); |
---|
| 475 | if ( selected.count() != 0 ) { |
---|
[524] | 476 | QTreeWidgetItem * tmp =selected.at( 0 ); |
---|
| 477 | while ( tmp) { |
---|
| 478 | tmp->setExpanded(false); |
---|
[525] | 479 | tmp = tmp->parent(); |
---|
[524] | 480 | } |
---|
[514] | 481 | } |
---|
[515] | 482 | |
---|
[525] | 483 | // clear old selection |
---|
| 484 | fHelpTreeWidget->clearSelection(); |
---|
| 485 | |
---|
[524] | 486 | // set new selection |
---|
| 487 | findItem->setSelected(true); |
---|
[515] | 488 | |
---|
[524] | 489 | // expand parent item |
---|
| 490 | while ( findItem) { |
---|
| 491 | findItem->setExpanded(true); |
---|
| 492 | findItem = findItem->parent(); |
---|
| 493 | } |
---|
| 494 | |
---|
[514] | 495 | // Call the update of the right textArea |
---|
[575] | 496 | HelpTreeClicCallback(); |
---|
[514] | 497 | } |
---|
[524] | 498 | } |
---|
[515] | 499 | fHelpDialog->setWindowTitle("Help on commands"); |
---|
[513] | 500 | fHelpDialog->resize(800,600); |
---|
| 501 | fHelpDialog->move(QPoint(400,150)); |
---|
| 502 | fHelpDialog->show(); |
---|
| 503 | fHelpDialog->raise(); |
---|
| 504 | fHelpDialog->activateWindow(); |
---|
[514] | 505 | } |
---|
[506] | 506 | |
---|
[507] | 507 | |
---|
[506] | 508 | |
---|
[526] | 509 | /** Fill the Help Tree Widget |
---|
[524] | 510 | @param aParent : parent item to fill |
---|
| 511 | @param aCommandTree : commandTree node associate with this part of the Tree |
---|
| 512 | */ |
---|
| 513 | void G4UIQt::CreateChildTree( |
---|
| 514 | QTreeWidgetItem *aParent |
---|
| 515 | ,G4UIcommandTree *aCommandTree |
---|
| 516 | ) |
---|
| 517 | { |
---|
[526] | 518 | if (aParent == NULL) return; |
---|
| 519 | if (aCommandTree == NULL) return; |
---|
[514] | 520 | |
---|
[526] | 521 | |
---|
[507] | 522 | // Creating new item |
---|
| 523 | QTreeWidgetItem * newItem; |
---|
[506] | 524 | |
---|
[508] | 525 | // Get the Sub directories |
---|
[524] | 526 | for (int a=0;a<aCommandTree->GetTreeEntry();a++) { |
---|
[526] | 527 | |
---|
[507] | 528 | QStringList stringList; |
---|
[524] | 529 | stringList << QString((char*)(aCommandTree->GetTree(a+1)->GetPathName()).data()).trimmed() ; |
---|
| 530 | stringList << QString((char*)(aCommandTree->GetTree(a+1)->GetTitle()).data()).trimmed() ; |
---|
[507] | 531 | newItem = new QTreeWidgetItem(stringList); |
---|
[508] | 532 | |
---|
[524] | 533 | CreateChildTree(newItem,aCommandTree->GetTree(a+1)); |
---|
| 534 | aParent->addChild(newItem); |
---|
[507] | 535 | } |
---|
[506] | 536 | |
---|
[508] | 537 | |
---|
| 538 | |
---|
| 539 | // Get the Commands |
---|
| 540 | |
---|
[524] | 541 | for (int a=0;a<aCommandTree->GetCommandEntry();a++) { |
---|
[508] | 542 | |
---|
| 543 | QStringList stringList; |
---|
[524] | 544 | stringList << QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).trimmed() ; |
---|
| 545 | stringList << QString((char*)(aCommandTree->GetCommand(a+1)->GetCommandPath()).data()).trimmed() ; |
---|
[508] | 546 | newItem = new QTreeWidgetItem(stringList); |
---|
[509] | 547 | |
---|
[524] | 548 | aParent->addChild(newItem); |
---|
[514] | 549 | newItem->setExpanded(false); |
---|
[508] | 550 | } |
---|
[507] | 551 | } |
---|
[509] | 552 | |
---|
[512] | 553 | |
---|
[526] | 554 | /** Find a treeItemWidget in the help tree |
---|
| 555 | @param aCommand item's String to look for |
---|
| 556 | @return item if found, NULL if not |
---|
[524] | 557 | */ |
---|
| 558 | QTreeWidgetItem* G4UIQt::FindTreeItem( |
---|
| 559 | QTreeWidgetItem *aParent |
---|
[527] | 560 | ,const QString& aCommand |
---|
[524] | 561 | ) |
---|
| 562 | { |
---|
[526] | 563 | if (aParent == NULL) return NULL; |
---|
| 564 | |
---|
[524] | 565 | if (aParent->text(0) == aCommand) |
---|
| 566 | return aParent; |
---|
| 567 | |
---|
| 568 | QTreeWidgetItem * tmp = NULL; |
---|
| 569 | for (int a=0;a<aParent->childCount();a++) { |
---|
| 570 | if (!tmp) |
---|
| 571 | tmp = FindTreeItem(aParent->child(a),aCommand); |
---|
| 572 | } |
---|
| 573 | return tmp; |
---|
| 574 | } |
---|
[511] | 575 | |
---|
[524] | 576 | |
---|
[527] | 577 | /** Build the command list parameters in a QString<br> |
---|
[524] | 578 | Reimplement partialy the G4UIparameter.cc |
---|
| 579 | @param aCommand : command to list parameters |
---|
| 580 | @see G4UIparameter::List() |
---|
[526] | 581 | @see G4UIcommand::List() |
---|
| 582 | @return the command list parameters, or "" if nothing |
---|
[524] | 583 | */ |
---|
[509] | 584 | QString G4UIQt::GetCommandList ( |
---|
[527] | 585 | const G4UIcommand *aCommand |
---|
[509] | 586 | ) |
---|
| 587 | { |
---|
[510] | 588 | |
---|
[526] | 589 | QString txt =""; |
---|
| 590 | if (aCommand == NULL) |
---|
| 591 | return txt; |
---|
| 592 | |
---|
[524] | 593 | G4String commandPath = aCommand->GetCommandPath(); |
---|
| 594 | G4String rangeString = aCommand->GetRange(); |
---|
| 595 | G4int n_guidanceEntry = aCommand->GetGuidanceEntries(); |
---|
| 596 | G4int n_parameterEntry = aCommand->GetParameterEntries(); |
---|
[526] | 597 | |
---|
| 598 | if ((commandPath == "") && |
---|
| 599 | (rangeString == "") && |
---|
| 600 | (n_guidanceEntry == 0) && |
---|
| 601 | (n_parameterEntry == 0)) { |
---|
| 602 | return txt; |
---|
| 603 | } |
---|
[511] | 604 | |
---|
[526] | 605 | if((commandPath.length()-1)!='/') { |
---|
| 606 | txt += "Command " + QString((char*)(commandPath).data()) + "\n"; |
---|
| 607 | } |
---|
| 608 | txt += "Guidance :\n"; |
---|
| 609 | |
---|
| 610 | for( G4int i_thGuidance=0; i_thGuidance < n_guidanceEntry; i_thGuidance++ ) { |
---|
| 611 | txt += QString((char*)(aCommand->GetGuidanceLine(i_thGuidance)).data()) + "\n"; |
---|
| 612 | } |
---|
| 613 | if( ! rangeString.isNull() ) { |
---|
| 614 | txt += " Range of parameters : " + QString((char*)(rangeString).data()) + "\n"; |
---|
| 615 | } |
---|
| 616 | if( n_parameterEntry > 0 ) { |
---|
| 617 | G4UIparameter *param; |
---|
| 618 | |
---|
| 619 | // Re-implementation of G4UIparameter.cc |
---|
| 620 | |
---|
| 621 | for( G4int i_thParameter=0; i_thParameter<n_parameterEntry; i_thParameter++ ) { |
---|
| 622 | param = aCommand->GetParameter(i_thParameter); |
---|
| 623 | txt += "\nParameter : " + QString((char*)(param->GetParameterName()).data()) + "\n"; |
---|
| 624 | if( ! param->GetParameterGuidance().isNull() ) |
---|
| 625 | txt += QString((char*)(param->GetParameterGuidance()).data())+ "\n" ; |
---|
| 626 | txt += " Parameter type : " + QString(param->GetParameterType())+ "\n"; |
---|
| 627 | if(param->IsOmittable()){ |
---|
| 628 | txt += " Omittable : True\n"; |
---|
| 629 | } else { |
---|
| 630 | txt += " Omittable : False\n"; |
---|
| 631 | } |
---|
| 632 | if( param->GetCurrentAsDefault() ) { |
---|
| 633 | txt += " Default value : taken from the current value\n"; |
---|
| 634 | } else if( ! param->GetDefaultValue().isNull() ) { |
---|
| 635 | txt += " Default value : " + QString((char*)(param->GetDefaultValue()).data())+ "\n"; |
---|
| 636 | } |
---|
| 637 | if( ! param->GetParameterRange().isNull() ) { |
---|
| 638 | txt += " Parameter range : " + QString((char*)(param->GetParameterRange()).data())+ "\n"; |
---|
| 639 | } |
---|
| 640 | if( ! param->GetParameterCandidates().isNull() ) { |
---|
| 641 | txt += " Candidates : " + QString((char*)(param->GetParameterCandidates()).data())+ "\n"; |
---|
| 642 | } |
---|
[511] | 643 | } |
---|
[526] | 644 | } |
---|
[509] | 645 | return txt; |
---|
| 646 | } |
---|
[513] | 647 | |
---|
[514] | 648 | |
---|
| 649 | |
---|
[527] | 650 | /** Implement G4VBasicShell vurtual function |
---|
[524] | 651 | */ |
---|
[517] | 652 | G4bool G4UIQt::GetHelpChoice( |
---|
| 653 | G4int& aInt |
---|
| 654 | ) |
---|
| 655 | { |
---|
| 656 | printf("G4UIQt::GetHelpChoice SHOULD NEVER GO HERE"); |
---|
[519] | 657 | return true; |
---|
[517] | 658 | } |
---|
[519] | 659 | |
---|
[524] | 660 | |
---|
[527] | 661 | /** Implement G4VBasicShell vurtual function |
---|
[524] | 662 | */ |
---|
[517] | 663 | void G4UIQt::ExitHelp( |
---|
| 664 | ) |
---|
| 665 | { |
---|
| 666 | printf("G4UIQt::ExitHelp SHOULD NEVER GO HERE"); |
---|
| 667 | } |
---|
[514] | 668 | |
---|
| 669 | |
---|
[527] | 670 | /** Event filter method. Every event from QtApplication goes here.<br/> |
---|
[524] | 671 | We apply a filter only for the Up and Down Arrow press when the QLineEdit<br/> |
---|
| 672 | is active. If this filter match, Up arrow we give the previous command<br/> |
---|
| 673 | and Down arrow will give the next if exist.<br/> |
---|
| 674 | @param obj Emitter of the event |
---|
| 675 | @param event Kind of event |
---|
[518] | 676 | */ |
---|
[528] | 677 | bool G4UIQt::eventFilter( // Should stay with a minuscule eventFilter because of Qt |
---|
[526] | 678 | QObject *aObj |
---|
| 679 | ,QEvent *aEvent |
---|
| 680 | ) |
---|
[518] | 681 | { |
---|
[526] | 682 | if (aObj == NULL) return false; |
---|
| 683 | if (aEvent == NULL) return false; |
---|
| 684 | |
---|
| 685 | if (aObj == fCommandHistoryArea) { |
---|
| 686 | if (aEvent->type() == QEvent::KeyPress) { |
---|
[523] | 687 | fCommandArea->setFocus(); |
---|
| 688 | } |
---|
| 689 | } |
---|
[526] | 690 | if (aObj == fCommandArea) { |
---|
| 691 | if (aEvent->type() == QEvent::KeyPress) { |
---|
| 692 | QKeyEvent *e = static_cast<QKeyEvent*>(aEvent); |
---|
[519] | 693 | if ((e->key() == (Qt::Key_Down)) || |
---|
| 694 | (e->key() == (Qt::Key_PageDown)) || |
---|
| 695 | (e->key() == (Qt::Key_Up)) || |
---|
| 696 | (e->key() == (Qt::Key_PageUp))) { |
---|
| 697 | int selection = fCommandHistoryArea->currentRow(); |
---|
| 698 | if (fCommandHistoryArea->count()) { |
---|
| 699 | if (selection == -1) { |
---|
| 700 | selection = fCommandHistoryArea->count()-1; |
---|
[528] | 701 | } else { |
---|
| 702 | if (e->key() == (Qt::Key_Down)) { |
---|
| 703 | if (selection <(fCommandHistoryArea->count()-1)) |
---|
| 704 | selection++; |
---|
| 705 | } else if (e->key() == (Qt::Key_PageDown)) { |
---|
| 706 | selection = fCommandHistoryArea->count()-1; |
---|
| 707 | } else if (e->key() == (Qt::Key_Up)) { |
---|
| 708 | if (selection >0) |
---|
| 709 | selection --; |
---|
| 710 | } else if (e->key() == (Qt::Key_PageUp)) { |
---|
| 711 | selection = 0; |
---|
| 712 | } |
---|
[519] | 713 | } |
---|
| 714 | fCommandHistoryArea->clearSelection(); |
---|
| 715 | fCommandHistoryArea->item(selection)->setSelected(true); |
---|
[520] | 716 | fCommandHistoryArea->setCurrentItem(fCommandHistoryArea->item(selection)); |
---|
[519] | 717 | } |
---|
[538] | 718 | } else if (e->key() == (Qt::Key_Tab)) { |
---|
| 719 | G4String ss = Complete(fCommandArea->text().toStdString().c_str()); |
---|
| 720 | fCommandArea->setText((char*)(ss.data())); |
---|
| 721 | |
---|
| 722 | // do not pass by parent, it will disable widget tab focus ! |
---|
| 723 | return true; |
---|
[518] | 724 | } |
---|
| 725 | } |
---|
| 726 | } |
---|
| 727 | // pass the event on to the parent class |
---|
[526] | 728 | return QObject::eventFilter(aObj, aEvent); |
---|
[518] | 729 | } |
---|
| 730 | |
---|
| 731 | |
---|
| 732 | |
---|
| 733 | |
---|
| 734 | /***************************************************************************/ |
---|
[514] | 735 | // |
---|
| 736 | // SLOTS DEFINITIONS |
---|
| 737 | // |
---|
| 738 | /***************************************************************************/ |
---|
| 739 | |
---|
[527] | 740 | /** Called when user give "help" command. |
---|
[524] | 741 | */ |
---|
[526] | 742 | void G4UIQt::ShowHelpCallback ( |
---|
[514] | 743 | ) |
---|
| 744 | { |
---|
[513] | 745 | TerminalHelp(""); |
---|
| 746 | } |
---|
| 747 | |
---|
[524] | 748 | |
---|
[527] | 749 | /** Called when user click on clear button. Clear the text Output area |
---|
[524] | 750 | */ |
---|
[526] | 751 | void G4UIQt::ClearButtonCallback ( |
---|
[514] | 752 | ) |
---|
| 753 | { |
---|
| 754 | fTextArea->clear(); |
---|
| 755 | } |
---|
| 756 | |
---|
| 757 | |
---|
[527] | 758 | /** Callback call when "click on a menu entry.<br> |
---|
[524] | 759 | Send the associated command to geant4 |
---|
| 760 | */ |
---|
[526] | 761 | void G4UIQt::CommandEnteredCallback ( |
---|
[514] | 762 | ) |
---|
| 763 | { |
---|
| 764 | G4String command (fCommandArea->text().toStdString().c_str()); |
---|
[522] | 765 | if (fCommandArea->text().trimmed() != "") { |
---|
[519] | 766 | fCommandHistoryArea->addItem(fCommandArea->text()); |
---|
[520] | 767 | fCommandHistoryArea->clearSelection(); |
---|
[578] | 768 | fCommandHistoryArea->setCurrentItem(NULL); |
---|
| 769 | fCommandArea->setText(""); |
---|
[514] | 770 | |
---|
[578] | 771 | G4Qt* interactorManager = G4Qt::getInstance (); |
---|
| 772 | if (interactorManager) { |
---|
| 773 | interactorManager->FlushAndWaitExecution(); |
---|
| 774 | } |
---|
[514] | 775 | if (command(0,4) != "help") { |
---|
[524] | 776 | ApplyShellCommand (command,exitSession,exitPause); |
---|
[514] | 777 | } else { |
---|
| 778 | TerminalHelp(command); |
---|
| 779 | } |
---|
[578] | 780 | printf("after \n"); |
---|
[514] | 781 | if(exitSession==true) |
---|
| 782 | SessionTerminate(); |
---|
| 783 | } |
---|
| 784 | } |
---|
| 785 | |
---|
[524] | 786 | |
---|
[527] | 787 | /** Callback call when "enter" clicked on the command zone.<br> |
---|
[524] | 788 | Send the command to geant4 |
---|
| 789 | @param aCommand |
---|
| 790 | */ |
---|
[526] | 791 | void G4UIQt::ButtonCallback ( |
---|
[524] | 792 | const QString& aCommand |
---|
[514] | 793 | ) |
---|
| 794 | { |
---|
[524] | 795 | G4String ss = G4String(aCommand.toStdString().c_str()); |
---|
[514] | 796 | ApplyShellCommand(ss,exitSession,exitPause); |
---|
| 797 | if(exitSession==true) |
---|
| 798 | SessionTerminate(); |
---|
| 799 | } |
---|
[524] | 800 | |
---|
| 801 | |
---|
| 802 | |
---|
[527] | 803 | /** This callback is activated when user selected a item in the help tree |
---|
[524] | 804 | */ |
---|
[575] | 805 | void G4UIQt::HelpTreeClicCallback ( |
---|
[514] | 806 | ) |
---|
| 807 | { |
---|
[577] | 808 | printf("G4UIQt::HelpTreeClicCallback"); |
---|
[514] | 809 | QTreeWidgetItem* item = NULL; |
---|
| 810 | if (!fHelpTreeWidget) |
---|
| 811 | return ; |
---|
| 812 | |
---|
| 813 | if (!fHelpArea) |
---|
| 814 | return; |
---|
| 815 | |
---|
[515] | 816 | QList<QTreeWidgetItem *> list =fHelpTreeWidget->selectedItems(); |
---|
| 817 | if (list.isEmpty()) |
---|
| 818 | return; |
---|
| 819 | item = list.first(); |
---|
[514] | 820 | if (!item) |
---|
| 821 | return; |
---|
| 822 | |
---|
| 823 | G4UImanager* UI = G4UImanager::GetUIpointer(); |
---|
| 824 | if(UI==NULL) return; |
---|
| 825 | G4UIcommandTree * treeTop = UI->GetTree(); |
---|
| 826 | G4UIcommand* command = treeTop->FindPath(item->text (1).toStdString().c_str()); |
---|
| 827 | if (command) { |
---|
| 828 | fHelpArea->setText(GetCommandList(command)); |
---|
| 829 | } else { |
---|
| 830 | // this is not a command, this is a sub directory |
---|
| 831 | // We display the Title |
---|
| 832 | fHelpArea->setText(item->text (1).toStdString().c_str()); |
---|
| 833 | } |
---|
| 834 | } |
---|
| 835 | |
---|
[575] | 836 | /** This callback is activated when user double clic on a item in the help tree |
---|
| 837 | */ |
---|
| 838 | void G4UIQt::HelpTreeDoubleClicCallback ( |
---|
[577] | 839 | QTreeWidgetItem* item |
---|
| 840 | ,int nb |
---|
[575] | 841 | ) |
---|
| 842 | { |
---|
[577] | 843 | printf("G4UIQt::HelpTreeDoubleClicCallback"); |
---|
[575] | 844 | HelpTreeClicCallback(); |
---|
| 845 | fCommandArea->setText(item->text (1)); |
---|
| 846 | } |
---|
| 847 | |
---|
| 848 | |
---|
[527] | 849 | /** Callback called when user select an old command in the command history<br> |
---|
[524] | 850 | Give it to the command area. |
---|
| 851 | */ |
---|
[526] | 852 | void G4UIQt::CommandHistoryCallback( |
---|
[519] | 853 | ) |
---|
| 854 | { |
---|
| 855 | QListWidgetItem* item = NULL; |
---|
| 856 | if (!fCommandHistoryArea) |
---|
| 857 | return ; |
---|
| 858 | |
---|
| 859 | |
---|
| 860 | QList<QListWidgetItem *> list =fCommandHistoryArea->selectedItems(); |
---|
| 861 | if (list.isEmpty()) |
---|
| 862 | return; |
---|
| 863 | item = list.first(); |
---|
| 864 | if (!item) |
---|
| 865 | return; |
---|
| 866 | fCommandArea->setText(item->text()); |
---|
| 867 | |
---|
| 868 | } |
---|
| 869 | |
---|
[513] | 870 | #endif |
---|