00001
00002 #include "win.h"
00003
00004 Win::Win( QSplashScreen* splash, QWidget *parent ) : QMainWindow( parent )
00005 {
00006 _closeMessageDialog = new QErrorMessage( this ) ;
00007 _closeMessageDialog->setWindowTitle( "OrK" ) ;
00008
00009
00010 _editModeFlag = _multiWinFlag = FALSE ;
00011 _userPath = "" ;
00012
00013
00014 _mapEditor = 0 ;
00015 _engineEditor = 0 ;
00016 _mapArea = 0 ;
00017
00018
00019 _config = new Config( this ) ;
00020 initClient( 0 ) ;
00021
00022
00023 _maps = new QList <Map*> ;
00024 _rails = new QMultiHash <int, Rail*> ;
00025 _enginesAreas = new QList <EnginesArea*> ;
00026 _engines = new QMultiHash <int, Engine*> ;
00027
00028
00029 connect( _config, SIGNAL( viewMultiWinModeChanged( bool ) )
00030 , this, SLOT( setMultiWinMode( bool ) ) ) ;
00031
00032 connect( _config, SIGNAL( userPathChanged( QString ) ) ,
00033 this, SLOT( setUserPath( QString ) ) ) ;
00034
00035 connect( _config, SIGNAL( railsFileChanged( QString ) ) ,
00036 this, SLOT( setRailsFile( QString ) ) ) ;
00037
00038 connect( _config, SIGNAL( serverAdressChanged( QString ) ) ,
00039 _client, SLOT( setServerAddress( QString ) ) ) ;
00040
00041 connect( _config, SIGNAL( serverPortChanged( quint16 ) ) ,
00042 _client, SLOT( setServerPort( quint16 ) ) ) ;
00043
00044
00045 connect( _client, SIGNAL( infoReceivedRailState( int, bool ) ),
00046 this, SLOT( setRailState( int, bool ) ) ) ;
00047
00048 connect( _client, SIGNAL( infoReceivedEngineState( int, bool, int, bool, bool, bool, bool ) ),
00049 this, SLOT( setEngineState( int, bool, int, bool, bool, bool, bool ) ) ) ;
00050
00051 connect( _client, SIGNAL( infoReceivedPower( bool ) ),
00052 this, SLOT( setPower( bool ) ) ) ;
00053
00054 connect( _client, SIGNAL( errorOccured( QString ) ), statusBar(), SLOT( showMessage( QString ) ) ) ;
00055
00056
00057 splash->showMessage( trUtf8( "Forging user interactions..." ), Qt::AlignBottom ) ;
00058 mkActions() ;
00059 mkMenus() ;
00060 mkToolBars() ;
00061 mkStatusBar() ;
00062
00063
00064 splash->showMessage( trUtf8( "Guessing your dream..." ), Qt::AlignBottom ) ;
00065 readSettings() ;
00066
00067 splash->showMessage( trUtf8( "Building tracks..." ), Qt::AlignBottom ) ;
00068
00069 if( _multiWinFlag )
00070 {
00071
00072 }
00073 else
00074 {
00075 _mapArea = new QMdiArea ;
00076 setCentralWidget( _mapArea ) ;
00077 }
00078
00079
00080
00081 open( windowFilePath() ) ;
00082
00083
00084 QSettings settings( "OrK", "client" ) ;
00085 restoreState( settings.value("state").toByteArray() ) ;
00086 restoreGeometry( settings.value("geometry").toByteArray() );
00087
00088
00089 setWindowIcon( QIcon( ":img/ork.svg" ) ) ;
00090 show() ;
00091
00092 splash->showMessage( trUtf8( "All stuff done." ), Qt::AlignBottom ) ;
00093 }
00094
00095
00096
00097
00098
00099 void Win::initClient( int protocol )
00100 {
00101 switch( protocol )
00102 {
00103
00104 default :
00105 _client = dynamic_cast<VirtualClient*>( new Srcp083Client( this ) ) ;
00106 break ;
00107 }
00108 }
00109
00110
00111 void Win::setRailState( int address, bool state, bool quiet )
00112 {
00113 QMultiHash<int, Rail*>::iterator i = _rails->find( address ) ;
00114 while ( i != _rails->end() && i.key() == address )
00115 {
00116 if ( i.value()->isOpen() != state )
00117 i.value()->setState( state, quiet ) ;
00118 ++i ;
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 }
00138
00139 void Win::setEngineState( int address, bool direction, int speed, bool ligth, bool f2, bool f3, bool f4 )
00140 {
00141 qDebug() << "Set the engine state" << address << direction << speed << ligth << f2 << f3 << f4 ;
00142
00143
00144 }
00145
00146 void Win::reportError( QString error )
00147 {
00148 qDebug() << "ERROR : " << error ;
00149 }
00150
00151
00152
00153
00154
00155
00156 void Win::readSettings()
00157 {
00158 _config->readConfig() ;
00159
00160 QSettings settings( "OrK", "client" ) ;
00161 setWindowFilePath( settings.value( "lastFile", "../models/example.ork" ).toString() ) ;
00162
00163 }
00164
00165 void Win::writeSettings()
00166 {
00167 _config->writeConfig() ;
00168
00169 QSettings settings( "OrK", "client" ) ;
00170
00171 settings.setValue( "lastFile", windowFilePath() != trUtf8("No name") ? windowFilePath() : "" ) ;
00172
00173 settings.setValue( "geometry", saveGeometry() ) ;
00174 settings.setValue( "state", saveState() ) ;
00175 }
00176
00177 void Win::setRailsFile( const QString &railsFile )
00178 {
00179 _railsFile = railsFile ;
00180 _mapRenderer = new QSvgRenderer( _railsFile, this ) ;
00181 }
00182
00183 QString Win::railsFile()
00184 {
00185 return _railsFile ;
00186 }
00187
00188
00189
00190
00191
00192
00193 void Win::clearModel()
00194 {
00195 if ( ! maybeSave() )
00196 return ;
00197
00198
00199 if ( _multiWinFlag )
00200 {
00201
00202 QList<QMainWindow*> maps =
00203 findChildren<QMainWindow*>( QRegExp( "Win Map [0-9]*") ) ;
00204 for( int i = 0; i < maps.size(); ++i )
00205 maps.at( i )->deleteLater() ;
00206
00207
00208 QList<QDockWidget*> enginesAreas =
00209 findChildren<QDockWidget*>( QRegExp( "Win EnginesArea [0-9]*") ) ;
00210 for( int i = 0; i < enginesAreas.size(); ++i )
00211 enginesAreas.at( i )->deleteLater() ;
00212 }
00213 else
00214 {
00215
00216 _mapArea->deleteLater() ;
00217 _mapArea = new QMdiArea( this ) ;
00218 setCentralWidget( _mapArea ) ;
00219
00220
00221 QList<QDockWidget*> enginesAreas =
00222 findChildren<QDockWidget*>( QRegExp( "Dock EnginesArea [0-9]*") ) ;
00223 for( int i = 0; i < enginesAreas.size(); ++i )
00224 enginesAreas.at( i )->deleteLater() ;
00225 }
00226
00227
00228 _mapsMenu->clear() ;
00229 _enginesAreaMenu->clear() ;
00230
00231
00232 _maps = new QList <Map*> ;
00233 _rails = new QMultiHash <int, Rail*> ;
00234 _enginesAreas = new QList <EnginesArea*> ;
00235 _engines = new QMultiHash <int, Engine*> ;
00236
00237
00238
00239 setWindowFilePath( trUtf8("No name") ) ;
00240 setDirty( FALSE ) ;
00241 }
00242
00243 void Win::addMap( Map* map )
00244 {
00245 if ( map->objectName().isEmpty() )
00246 map->setObjectName( QString("Map %1").arg( _maps->count() ) ) ;
00247
00248
00249
00250 _maps->append( map ) ;
00251
00252
00253 QAction *mapAct = new QAction( map->name(), this ) ;
00254 mapAct->setCheckable( TRUE ) ;
00255 mapAct->setChecked( TRUE ) ;
00256
00257 map->addAction( mapAct ) ;
00258
00259
00260 if( _multiWinFlag )
00261 {
00262 QMainWindow *win = new QMainWindow( this ) ;
00263 win->setObjectName( QString("Win %1").arg( map->objectName() ) ) ;
00264
00265 win->setCentralWidget( map ) ;
00266 win->setWindowTitle( map->name() ) ;
00267 win->setWindowIcon( QIcon( ":img/maps.svg" ) ) ;
00268 win->show() ;
00269 connect( mapAct, SIGNAL( triggered( bool ) ),
00270 win, SLOT( setVisible( bool ) ) ) ;
00271 connect( map, SIGNAL( mapSetVisible( bool ) ),
00272 win, SLOT( setVisible( bool ) ) ) ;
00273 }
00274 else
00275 {
00276 QMdiSubWindow* subWin = new QMdiSubWindow( this ) ;
00277 subWin->setObjectName( QString( "SubWin %1").arg( map->objectName() ) ) ;
00278
00279 subWin->setWidget( map ) ;
00280 subWin->setWindowTitle( map->name() ) ;
00281 _mapArea->addSubWindow( subWin ) ;
00282 _mapArea->cascadeSubWindows () ;
00283
00284 subWin->show() ;
00285 connect( mapAct, SIGNAL( triggered( bool ) ),
00286 subWin, SLOT( setVisible( bool ) ) ) ;
00287 connect( map, SIGNAL( mapSetVisible( bool ) ),
00288 subWin, SLOT( setVisible( bool ) ) ) ;
00289 }
00290
00291
00292 connect( mapAct, SIGNAL( triggered( bool ) ), map, SLOT( setVisible( bool ) ) ) ;
00293 connect( map, SIGNAL( mapSetVisible( bool ) ), mapAct, SLOT( setChecked( bool ) ) ) ;
00294 connect( map, SIGNAL( mapClosed() ), this, SLOT( showCloseMessage() ) ) ;
00295 _mapsMenu->addAction( mapAct ) ;
00296
00297 }
00298
00299 void Win::removeMap( Map* map )
00300 {
00301 _maps->removeAll( map );
00302
00303 }
00304
00305 void Win::addEnginesArea( EnginesArea* enginesArea )
00306 {
00307 if ( enginesArea->objectName().isEmpty() )
00308 enginesArea->setObjectName( QString("EnginesArea %1").arg( _enginesAreas->count() ) ) ;
00309
00310
00311
00312 _enginesAreas->append( enginesArea ) ;
00313
00314 QAction *winAct = new QAction( enginesArea->name(), this ) ;
00315 winAct->setCheckable( TRUE ) ;
00316 winAct->setChecked( TRUE ) ;
00317 enginesArea->addAction( winAct );
00318
00319
00320 if ( _multiWinFlag )
00321 {
00322 QMainWindow *win = new QMainWindow( this ) ;
00323 win->setCentralWidget( enginesArea ) ;
00324 win->setObjectName( QString("Win %1").arg( enginesArea->objectName() ) ) ;
00325 win->setWindowTitle( enginesArea->name() ) ;
00326 win->setWindowIcon( QIcon( ":img/engines_areas.svg" ) ) ;
00327 win->show() ;
00328
00329 connect( winAct, SIGNAL( triggered( bool ) ),
00330 win, SLOT( setVisible( bool ) ) ) ;
00331 connect( enginesArea, SIGNAL( areaSetVisible( bool ) ),
00332 win, SLOT( setVisible( bool ) ) ) ;
00333 }
00334 else
00335 {
00336 QDockWidget *dockWidget = new QDockWidget( enginesArea->name(), this ) ;
00337 enginesArea->setParent( dockWidget ) ;
00338 dockWidget->setWidget( enginesArea ) ;
00339 dockWidget->setObjectName( QString( "Dock %1").arg( enginesArea->objectName() ) ) ;
00340 addDockWidget( Qt::RightDockWidgetArea, dockWidget ) ;
00341
00342 connect( winAct, SIGNAL( triggered( bool ) ),
00343 dockWidget, SLOT( setVisible( bool ) ) ) ;
00344 connect( enginesArea, SIGNAL( areaSetVisible( bool ) ),
00345 dockWidget, SLOT( setVisible( bool ) ) ) ;
00346 }
00347
00348 connect( winAct, SIGNAL( triggered( bool ) ), enginesArea, SLOT( setVisible( bool ) ) ) ;
00349 connect( enginesArea, SIGNAL( areaSetVisible( bool ) ), winAct, SLOT( setChecked( bool ) ) ) ;
00350 connect( enginesArea, SIGNAL( areaClosed() ), this, SLOT( showCloseMessage() ) ) ;
00351 connect( enginesArea, SIGNAL( engineDropped() ), this, SLOT( setDirty() ) ) ;
00352 _enginesAreaMenu->addAction( winAct ) ;
00353 }
00354
00355 void Win::removeEnginesArea( EnginesArea* enginesArea )
00356 {
00357 _enginesAreas->removeAll( enginesArea ) ;
00358
00359 }
00360
00361 void Win::addRule( QString trigger, int source, QString action, int target )
00362 {
00363 qDebug() << "New rule added " << trigger << " " << source << " " << action << " " << target ;
00364
00365
00366 }
00367
00368 void Win::addEngine()
00369 {
00370 Engine *engine = new Engine() ;
00371
00372
00373 QList<EnginesArea*> enginesAreas =
00374 findChildren<EnginesArea*>( QRegExp( "EnginesArea [0-9]*") ) ;
00375
00376 if ( enginesAreas.size() == 0 )
00377 return ;
00378
00379 if ( enginesAreas.size() == 1 )
00380 {
00381 enginesAreas.at( 0 )->insertWidget( engine ) ;
00382 }
00383 else
00384 {
00385
00386
00387 QStringList enginesAreasCombo ;
00388 for( int i = 0; i < enginesAreas.size(); ++i )
00389 enginesAreasCombo << enginesAreas.at( i )->name() ;
00390
00391 bool ok;
00392 QString choice = QInputDialog::getItem(this, trUtf8( "Choose the engines area"),
00393 trUtf8("In which engines area do you want to add the new engine ?"), enginesAreasCombo, 0, false, &ok);
00394
00395 if ( ! ok || choice.isEmpty() )
00396 return ;
00397
00398 int i ;
00399 for( i = 0; i < enginesAreas.size(); ++i )
00400 if ( enginesAreas.at( i )->name() == choice )
00401 break ;
00402
00403 enginesAreas.at( i )->insertWidget( engine ) ;
00404 }
00405
00406
00407 if ( _engineEditor != 0 )
00408 connect( engine, SIGNAL( clicked() ), _engineEditor, SLOT( changeEngine() ) ) ;
00409
00410 engineEdit() ;
00411 _engineEditor->parentWidget()->setVisible( TRUE ) ;
00412 engineEditorAct->setChecked( TRUE ) ;
00413 _engineEditor->setEngine( engine ) ;
00414 }
00415
00416 void Win::addEngine( Engine *engine )
00417 {
00418 _engines->insert( engine->address(), engine ) ;
00419 connect( engine, SIGNAL( stateChanged() ), _client, SLOT( setNewEngineState() ) ) ;
00420 }
00421
00422 void Win::removeEngine( Engine* engine )
00423 {
00424 _engines->remove( engine->address(), engine ) ;
00425
00426 }
00427
00428 void Win::addEnginesArea()
00429 {
00430 bool ok;
00431 QString name = QInputDialog::getText( this, trUtf8("New engines area"), trUtf8("Engines area name :"), QLineEdit::Normal, QString(), &ok);
00432
00433 if( ! ok || name.isEmpty() )
00434 return ;
00435
00436 EnginesArea *enginesArea = new EnginesArea() ;
00437 enginesArea->setName( name ) ;
00438
00439 addEnginesArea( enginesArea ) ;
00440 setDirty() ;
00441 }
00442
00443 void Win::addMap()
00444 {
00445 bool ok ;
00446 QString name = QInputDialog::getText( this, trUtf8( "New Map" ), trUtf8( "New map name:" ), QLineEdit::Normal, QString(), &ok ) ;
00447
00448 if( ! ok || name.isEmpty() )
00449 return ;
00450
00451 Map *map = new Map ;
00452 map->setName( name ) ;
00453
00454 addMap( map ) ;
00455 setDirty() ;
00456 }
00457
00458 void Win::addRail( Rail* rail )
00459 {
00460 if ( rail->address() != 0 )
00461 {
00462 _rails->insert( rail->address(), rail ) ;
00463 connect( rail, SIGNAL( clicked() ), _client, SLOT( requestNewRailState() ) ) ;
00464 }
00465 }
00466
00467 void Win::removeRail( Rail* rail )
00468 {
00469 if ( rail->address() != 0 )
00470 _rails->remove( rail->address(), rail );
00471 }
00472
00473 void Win::closeEvent( QCloseEvent *event )
00474 {
00475 if ( _editModeFlag )
00476 editMode( FALSE ) ;
00477
00478 if ( maybeSave() )
00479 {
00480 event->accept();
00481 }
00482 else
00483 event->ignore();
00484
00485 writeSettings() ;
00486 }
00487
00488
00489
00490
00491
00492
00493 void Win::mapEdit()
00494 {
00495
00496 if( _mapEditor == 0 )
00497 {
00498 _mapEditor = new MapEditor() ;
00499
00500 if ( _multiWinFlag )
00501 {
00502 QMainWindow *win = new QMainWindow( this ) ;
00503 _mapEditor->setParent( win ) ;
00504
00505 win->setObjectName( "Win Editor Map" ) ;
00506 win->setCentralWidget( _mapEditor ) ;
00507 win->setWindowTitle( trUtf8("Map editor") ) ;
00508 win->setWindowIcon( QIcon( ":img/map_editor.svg" ) ) ;
00509 win->show() ;
00510
00511 connect( mapEditorAct, SIGNAL( triggered( bool ) ),
00512 win, SLOT( setVisible( bool ) ) ) ;
00513 }
00514 else
00515 {
00516 QDockWidget *dockWidget = new QDockWidget( trUtf8("Map editor"), this ) ;
00517 _mapEditor->setParent( dockWidget ) ;
00518
00519 dockWidget->setObjectName( "Dock Editor Map" ) ;
00520 dockWidget->setWidget( _mapEditor ) ;
00521 addDockWidget( Qt::LeftDockWidgetArea, dockWidget ) ;
00522
00523 connect( mapEditorAct, SIGNAL( triggered( bool ) ),
00524 dockWidget, SLOT( setVisible( bool ) ) ) ;
00525 }
00526 }
00527 }
00528
00529 void Win::engineEdit()
00530 {
00531
00532 if( _engineEditor == 0 )
00533 {
00534 _engineEditor = new EngineEditor() ;
00535 connect( _engineEditor, SIGNAL( engineChanged() ), this, SLOT( setDirty() ) ) ;
00536
00537
00538 QList<Engine*> engines =
00539 findChildren<Engine*>() ;
00540
00541 for( int i = 0; i < engines.size(); ++i )
00542 connect( engines.at( i ), SIGNAL( clicked() ), _engineEditor, SLOT( changeEngine() ) ) ;
00543
00544 if ( _multiWinFlag )
00545 {
00546 QMainWindow *win = new QMainWindow( this ) ;
00547 _engineEditor->setParent( win ) ;
00548
00549 win->setObjectName( "Win Editor Engine" ) ;
00550 win->setCentralWidget( _engineEditor ) ;
00551 win->setWindowTitle( trUtf8("Engine editor") ) ;
00552 win->setWindowIcon( QIcon( ":img/engine_editor.svg" ) ) ;
00553 win->show() ;
00554
00555 connect( engineEditorAct, SIGNAL( triggered( bool ) ),
00556 win, SLOT( setVisible( bool ) ) ) ;
00557 }
00558 else
00559 {
00560 QDockWidget *dockWidget = new QDockWidget( trUtf8("Enigne editor"), this ) ;
00561 _engineEditor->setParent( dockWidget ) ;
00562
00563 dockWidget->setObjectName( "Dock Editor Engine" ) ;
00564 dockWidget->setWidget( _engineEditor ) ;
00565 addDockWidget( Qt::LeftDockWidgetArea, dockWidget ) ;
00566
00567 connect( engineEditorAct, SIGNAL( triggered( bool ) ),
00568 dockWidget, SLOT( setVisible( bool ) ) ) ;
00569 }
00570 }
00571 }
00572
00573 void Win::editMode( bool edition )
00574 {
00575 _editModeFlag = edition ;
00576
00577 if ( ! _editModeFlag )
00578 {
00579 if ( _engineEditor != 0 )
00580 _engineEditor->parentWidget()->setVisible( FALSE ) ;
00581 if ( _mapEditor != 0 )
00582 _mapEditor->parentWidget()->setVisible( FALSE ) ;
00583
00584 engineEditorAct->setChecked( FALSE ) ;
00585 mapEditorAct->setChecked( FALSE ) ;
00586 }
00587
00588
00589 _removeMenu->clear() ;
00590
00591 QList<EnginesArea*> enginesAreas =
00592 findChildren<EnginesArea*>( QRegExp( "EnginesArea [0-9]*" ) ) ;
00593
00594 QMenu *enginesAreasMenu = _removeMenu->addMenu( QIcon( ":img/engines_areas.svg"), trUtf8( "Engines areas") ) ;
00595
00596 for( int i = 0; i < enginesAreas.size(); ++i )
00597 {
00598 QAction *act = new QAction( enginesAreas.at( i )->name(), this ) ;
00599 enginesAreasMenu->addAction( act ) ;
00600
00601 }
00602
00603 QList<Engine*> engines =
00604 findChildren<Engine*>() ;
00605
00606 QMenu *enginesMenu = _removeMenu->addMenu( QIcon( ":img/engines.svg"), trUtf8( "Engines") ) ;
00607
00608 for( int i = 0; i < engines.size(); ++i )
00609 {
00610 QAction *act = new QAction( engines.at( i )->name(), this ) ;
00611 enginesMenu->addAction( act ) ;
00612
00613 }
00614
00615 QList<Map*> maps =
00616 findChildren<Map*>() ;
00617
00618 QMenu *mapsMenu = _removeMenu->addMenu( QIcon( ":img/maps.svg"), trUtf8( "Maps") ) ;
00619
00620 for( int i = 0; i < maps.size(); ++i )
00621 {
00622 QAction *act = new QAction( maps.at( i )->name(), this ) ;
00623 mapsMenu->addAction( act ) ;
00624
00625 }
00626
00627
00628 addMapAct->setEnabled( _editModeFlag ) ;
00629 addLocAct->setEnabled( _editModeFlag ) ;
00630 addLocAreaAct->setEnabled( _editModeFlag ) ;
00631 mapEditorAct->setEnabled( _editModeFlag ) ;
00632 engineEditorAct->setEnabled( _editModeFlag ) ;
00633 _removeMenu->setEnabled( _editModeFlag ) ;
00634 }
00635
00636 void Win::net( bool state )
00637 {
00638 powerAct->setEnabled( state ) ;
00639 }
00640
00641 void Win::setPower( bool state )
00642 {
00643 if ( powerAct->isChecked() != state )
00644 powerAct->toggle() ;
00645 }
00646
00647 void Win::fullscreen()
00648 {
00649 setWindowState( windowState() ^ Qt::WindowFullScreen ) ;
00650 }
00651
00652 void Win::hideMenubar()
00653 {
00654 if( menuBar()->isVisible() )
00655 menuBar()->hide() ;
00656 else
00657 menuBar()->show() ;
00658 }
00659
00660 void Win::zoomIn()
00661 {
00662
00663 if( _multiWinFlag )
00664 {
00665
00666 }
00667 else
00668 {
00669 static_cast<Map*>( _mapArea->activeSubWindow()->widget() )->zoomIn() ;
00670 }
00671 }
00672
00673 void Win::zoomOut()
00674 {
00675
00676 if( _multiWinFlag )
00677 {
00678
00679 }
00680 else
00681 {
00682 static_cast<Map*>( _mapArea->activeSubWindow()->widget() )->zoomOut() ;
00683 }
00684
00685 }
00686
00687 void Win::zoomBestFit()
00688 {
00689
00690 if( _multiWinFlag )
00691 {
00692
00693 }
00694 else
00695 {
00696 static_cast<Map*>( _mapArea->activeSubWindow()->widget() )->fitView() ;
00697 }
00698
00699 }
00700
00701 void Win::zoomOriginal()
00702 {
00703
00704 if( _multiWinFlag )
00705 {
00706
00707 }
00708 else
00709 {
00710 static_cast<Map*>( _mapArea->activeSubWindow()->widget() )->scaleDefault() ;
00711 }
00712
00713 }
00714
00715 void Win::tileView()
00716 {
00717
00718 if( _multiWinFlag )
00719 {
00720
00721 }
00722 else
00723 {
00724 _mapArea->tileSubWindows() ;
00725 }
00726
00727
00728 }
00729
00730 void Win::cascadeView()
00731 {
00732
00733 if( _multiWinFlag )
00734 {
00735
00736 }
00737 else
00738 {
00739 _mapArea->cascadeSubWindows() ;
00740 }
00741
00742 }
00743
00744 void Win::config()
00745 {
00746 _config->show() ;
00747 }
00748
00749 void Win::help()
00750 {
00751
00752 }
00753
00754 void Win::bug()
00755 {
00756
00757 }
00758
00759 void Win::translate()
00760 {
00761
00762 }
00763
00764 void Win::about()
00765 {
00766 QString htmlText = trUtf8("<HTML>"
00767 "<p><b>About open rail Kommander</b></p><p><b>version %1</b></p><p>OrK is an open source railroad models controlling programm.</p><p>OrK is written by Bianco François and Epiney Yvan under the GPL license V.3 in C++ with Qt %2 libraries.</p><p>Check the <a href=\"http://ork.sourceforge.net\">OrK website</a> for more informations.</p></HTML>" ).arg( OrK::Version ).arg( qVersion() ) ;
00768
00769 QMessageBox::about( this, "About orK", htmlText ) ;
00770 }
00771
00772 void Win::mkActions()
00773 {
00774 newAct = new QAction( QIcon( ":img/new.svg" ), trUtf8( "&New model" ), this ) ;
00775 newAct->setShortcut( trUtf8( "Ctrl+N" ) ) ;
00776 addAction( newAct ) ;
00777 connect( newAct, SIGNAL( triggered() ), this, SLOT( clearModel() ) ) ;
00778
00779 openAct = new QAction( QIcon( ":img/open.svg" ), trUtf8( "&Open" ), this ) ;
00780 openAct->setShortcut( trUtf8( "Ctrl+O" ) ) ;
00781 addAction( openAct ) ;
00782 connect( openAct, SIGNAL( triggered() ), this, SLOT( open() ) ) ;
00783
00784 saveAct = new QAction( QIcon( ":img/save.svg" ), trUtf8( "&Save" ), this ) ;
00785 saveAct->setShortcut( trUtf8( "Ctrl+S" ) ) ;
00786 saveAct->setEnabled( FALSE ) ;
00787 addAction( saveAct ) ;
00788 connect( saveAct, SIGNAL( triggered() ), this, SLOT( save() ) ) ;
00789
00790 saveAsAct = new QAction( QIcon( ":img/save_as.svg" ), trUtf8( "Save &as..." ), this ) ;
00791 saveAsAct->setShortcut( trUtf8( "Ctrl+Shift+S" ) ) ;
00792 addAction( saveAsAct ) ;
00793 connect( saveAsAct, SIGNAL( triggered() ), this, SLOT( saveAs() ) ) ;
00794
00795 quitAct = new QAction( QIcon( ":img/quit.svg" ), trUtf8( "&Quit" ), this ) ;
00796 quitAct->setShortcut( trUtf8( "Ctrl+Q" ) ) ;
00797 addAction( quitAct ) ;
00798 connect( quitAct, SIGNAL( triggered() ), this, SLOT( close() ) ) ;
00799
00800 addLocAct = new QAction( QIcon( ":img/add_engine.svg" ), trUtf8( "Add an &engine" ), this ) ;
00801 addLocAct->setShortcut( trUtf8( "Ctrl+Shift+L" ) ) ;
00802 addLocAct->setEnabled( FALSE ) ;
00803 addAction( addLocAct ) ;
00804 connect( addLocAct, SIGNAL( triggered() ), this, SLOT( addEngine() ) ) ;
00805
00806 addLocAreaAct = new QAction( QIcon( ":img/add_engines_area.svg" ), trUtf8( "Add an engines &area" ), this ) ;
00807 addLocAreaAct->setShortcut( trUtf8( "Ctrl+Shift+A" ) ) ;
00808 addLocAreaAct->setEnabled( FALSE ) ;
00809 addAction( addLocAreaAct ) ;
00810 connect( addLocAreaAct, SIGNAL( triggered() ), this, SLOT( addEnginesArea() ) ) ;
00811
00812 addMapAct = new QAction( QIcon( ":img/add_map.svg" ), trUtf8( "Add a &map" ), this ) ;
00813 addMapAct->setShortcut( trUtf8( "Ctrl+Shift+M" ) ) ;
00814 addMapAct->setEnabled( FALSE ) ;
00815 addAction( addMapAct ) ;
00816 connect( addMapAct, SIGNAL( triggered() ), this, SLOT( addMap() ) ) ;
00817
00818
00819 engineEditorAct = new QAction( QIcon( ":img/engine_editor.svg" ), trUtf8( "Show the en&gine editor" ), this ) ;
00820 engineEditorAct->setCheckable( TRUE ) ;
00821 engineEditorAct->setEnabled( FALSE ) ;
00822 addAction( engineEditorAct ) ;
00823 connect( engineEditorAct, SIGNAL( triggered() ), this, SLOT( engineEdit() ) ) ;
00824
00825
00826 mapEditorAct = new QAction( QIcon( ":img/map_editor.svg" ), trUtf8( "Show the map e&ditor" ), this ) ;
00827 mapEditorAct->setCheckable( TRUE ) ;
00828 mapEditorAct->setEnabled( FALSE ) ;
00829 addAction( mapEditorAct ) ;
00830 connect( mapEditorAct, SIGNAL( triggered() ), this, SLOT( mapEdit() ) ) ;
00831
00832
00833 editModeAct = new QAction( QIcon( ":img/edit_mode.svg" ), trUtf8( "Toggle &edit mode" ), this ) ;
00834 editModeAct->setShortcut( trUtf8( "Ctrl+M" ) ) ;
00835 editModeAct->setCheckable( TRUE ) ;
00836 addAction( editModeAct ) ;
00837 connect( editModeAct, SIGNAL( triggered( bool ) ), this, SLOT( editMode( bool ) ) ) ;
00838
00839 connectAct = new QAction( QIcon( ":img/net.svg" ), trUtf8( "&Connect" ), this ) ;
00840 connectAct->setShortcut( trUtf8( "Ctrl+C" ) ) ;
00841 connectAct->setCheckable( TRUE ) ;
00842 addAction( connectAct ) ;
00843 connect( connectAct, SIGNAL( triggered( bool ) ), this, SLOT( net( bool ) ) ) ;
00844 connect( connectAct, SIGNAL( triggered( bool ) ), _client, SLOT( setConnection( bool ) ) ) ;
00845
00846 powerAct = new QAction( QIcon( ":img/power.svg" ), trUtf8( "&Power" ), this ) ;
00847 powerAct->setShortcut( trUtf8( "Ctrl+P" ) ) ;
00848 powerAct->setCheckable( TRUE ) ;
00849 powerAct->setEnabled( FALSE ) ;
00850 addAction( powerAct ) ;
00851 connect( powerAct, SIGNAL( triggered( bool ) ), this, SLOT( setPower( bool ) ) ) ;
00852 connect( powerAct, SIGNAL( triggered( bool ) ), _client, SLOT( setPower( bool ) ) ) ;
00853
00854
00855
00856
00857
00858
00859 fullscreenAct = new QAction( QIcon( ":img/fullscreen.svg" ), trUtf8( "&Fullscreen" ), this ) ;
00860 fullscreenAct->setShortcut( trUtf8( "Ctrl+Shift+F" ) ) ;
00861 fullscreenAct->setCheckable( TRUE ) ;
00862 addAction( fullscreenAct ) ;
00863 connect( fullscreenAct, SIGNAL( triggered() ), this, SLOT( fullscreen() ) ) ;
00864
00865 hideMenubarAct = new QAction( QIcon( ":img/hide_menubar.svg" ), trUtf8( "&Hide Menubar" ), this ) ;
00866 hideMenubarAct->setShortcut( trUtf8( "Ctrl+Shift+H" ) ) ;
00867 hideMenubarAct->setCheckable( TRUE ) ;
00868 addAction( hideMenubarAct ) ;
00869 connect( hideMenubarAct, SIGNAL( triggered() ), this, SLOT( hideMenubar() ) ) ;
00870
00871 zoomInAct = new QAction( QIcon( ":img/zoom_in.svg" ), trUtf8( "Zoom &In" ), this ) ;
00872 zoomInAct->setShortcut( trUtf8( "+" ) ) ;
00873 addAction( zoomInAct ) ;
00874 connect( zoomInAct, SIGNAL( triggered() ), this, SLOT( zoomIn() ) ) ;
00875
00876 zoomOutAct = new QAction( QIcon( ":img/zoom_out.svg" ), trUtf8( "Zoom &Out" ), this ) ;
00877 zoomOutAct->setShortcut( trUtf8( "-" ) ) ;
00878 addAction( zoomOutAct ) ;
00879 connect( zoomOutAct, SIGNAL( triggered() ), this, SLOT( zoomOut() ) ) ;
00880
00881 zoomBestFitAct = new QAction( QIcon( ":img/zoom_best_fit.svg" ), trUtf8( "&Best View" ), this ) ;
00882 zoomBestFitAct->setShortcut( trUtf8( "Ctrl+B" ) ) ;
00883 addAction( zoomBestFitAct ) ;
00884 connect( zoomBestFitAct, SIGNAL( triggered() ), this, SLOT( zoomBestFit() ) ) ;
00885
00886 zoomOriginAct = new QAction( QIcon( ":img/zoom_original.svg" ), trUtf8( "&Original View" ), this ) ;
00887 zoomOriginAct->setShortcut( trUtf8( "Ctrl+O" ) ) ;
00888 addAction( zoomOriginAct ) ;
00889 connect( zoomOriginAct, SIGNAL( triggered() ), this, SLOT( zoomOriginal() ) ) ;
00890
00891 configAct = new QAction( QIcon( ":img/config.svg" ), trUtf8( "&Configure OrK" ), this ) ;
00892 configAct->setShortcut( trUtf8( "Ctrl+K" ) ) ;
00893 addAction( configAct ) ;
00894 connect( configAct, SIGNAL( triggered() ), this, SLOT( config() ) ) ;
00895
00896 helpAct = new QAction( QIcon( ":img/help.svg" ), trUtf8( "&Help" ), this ) ;
00897 helpAct->setShortcut( trUtf8( "F1" ) ) ;
00898 addAction( helpAct ) ;
00899 connect( helpAct, SIGNAL( triggered() ), this, SLOT( help() ) ) ;
00900
00901 bugAct = new QAction( QIcon( ":img/bug.svg" ), trUtf8( "&Bug Repport" ), this ) ;
00902 addAction( bugAct ) ;
00903 connect( bugAct, SIGNAL( triggered() ), this, SLOT( bug() ) ) ;
00904
00905 translateAct = new QAction( QIcon( ":img/translate.svg" ), trUtf8( "&Translate" ), this ) ;
00906 addAction( translateAct );
00907 connect( translateAct, SIGNAL( triggered() ), this, SLOT( translate() ) ) ;
00908
00909 aboutAct = new QAction( QIcon( ":img/ork.svg" ), trUtf8( "About &orK" ), this ) ;
00910 addAction( aboutAct ) ;
00911 connect( aboutAct, SIGNAL( triggered() ), this, SLOT( about() ) ) ;
00912
00913 aboutQtAct = new QAction( QIcon( ":img/qt.svg" ), trUtf8( "About &Qt" ), this ) ;
00914 addAction( aboutQtAct ) ;
00915 connect( aboutQtAct, SIGNAL( triggered() ), qApp, SLOT( aboutQt() ) ) ;
00916
00917 tileViewAct = new QAction( QIcon( ":img/tile_view.svg" ), trUtf8( "&Tile windows" ), this ) ;
00918 tileViewAct->setShortcut( trUtf8( "Ctrl+T" ) ) ;
00919 addAction( tileViewAct ) ;
00920 connect( tileViewAct , SIGNAL( triggered() ), this, SLOT( tileView() ) ) ;
00921
00922 cascadeViewAct = new QAction( QIcon( ":img/cascade_view.svg" ), trUtf8( "&Cascade windows" ), this ) ;
00923 cascadeViewAct->setShortcut( trUtf8( "Ctrl+Shift+T" ) ) ;
00924 addAction( cascadeViewAct ) ;
00925 connect( cascadeViewAct , SIGNAL( triggered() ), this, SLOT( cascadeView() ) ) ;
00926 }
00927
00928 void Win::mkMenus()
00929 {
00930 QMenu *fileMenu = menuBar()->addMenu( trUtf8( "&File" ) ) ;
00931 fileMenu->addAction( newAct ) ;
00932 fileMenu->addSeparator() ;
00933 fileMenu->addAction( openAct ) ;
00934 fileMenu->addSeparator() ;
00935 fileMenu->addAction( saveAct ) ;
00936 fileMenu->addAction( saveAsAct ) ;
00937 fileMenu->addSeparator() ;
00938 fileMenu->addAction( quitAct ) ;
00939
00940 QMenu *editMenu = menuBar()->addMenu( trUtf8( "&Edit" ) ) ;
00941 editMenu->addAction( addLocAct ) ;
00942 editMenu->addAction( addLocAreaAct ) ;
00943 editMenu->addAction( addMapAct ) ;
00944 editMenu->addSeparator() ;
00945 _removeMenu = editMenu->addMenu( QIcon( ":img/remove.svg" ), trUtf8( "&Remove" ) ) ;
00946 _removeMenu->setEnabled( FALSE ) ;
00947 editMenu->addSeparator() ;
00948 editMenu->addAction( engineEditorAct ) ;
00949 editMenu->addAction( mapEditorAct ) ;
00950 editMenu->addSeparator() ;
00951 editMenu->addAction( editModeAct ) ;
00952 editMenu->addSeparator() ;
00953
00954 QMenu *viewMenu = menuBar()->addMenu( trUtf8( "&View" ) ) ;
00955 _mapsMenu = viewMenu->addMenu( QIcon( ":img/maps.svg" ), trUtf8( "&Maps ..." ) ) ;
00956 _enginesAreaMenu = viewMenu->addMenu( QIcon( ":img/engines_areas.svg" ), trUtf8( "&Engines areas ..." ) ) ;
00957 viewMenu->addSeparator() ;
00958 viewMenu->addAction( fullscreenAct ) ;
00959 viewMenu->addAction( hideMenubarAct ) ;
00960 viewMenu->addSeparator() ;
00961 viewMenu->addAction( zoomInAct ) ;
00962 viewMenu->addAction( zoomOutAct ) ;
00963 viewMenu->addAction( zoomBestFitAct ) ;
00964 viewMenu->addAction( zoomOriginAct ) ;
00965 viewMenu->addSeparator() ;
00966 viewMenu->addAction( tileViewAct ) ;
00967 viewMenu->addAction( cascadeViewAct ) ;
00968
00969 QMenu *networkMenu = menuBar()->addMenu( trUtf8( "&Network" ) ) ;
00970 networkMenu->addAction( connectAct ) ;
00971 networkMenu->addAction( powerAct ) ;
00972
00973
00974
00975 QMenu *configMenu = menuBar()->addMenu( trUtf8( "&Configuration" ) ) ;
00976 configMenu->addAction( configAct ) ;
00977
00978 QMenu *helpMenu = menuBar()->addMenu( trUtf8( "&Help" ) ) ;
00979 helpMenu->addAction( helpAct ) ;
00980 helpMenu->addSeparator() ;
00981 helpMenu->addAction( bugAct ) ;
00982 helpMenu->addAction( translateAct ) ;
00983 helpMenu->addSeparator() ;
00984 helpMenu->addAction( aboutAct ) ;
00985 helpMenu->addAction( aboutQtAct ) ;
00986 }
00987
00988 void Win::mkToolBars()
00989 {
00990 QToolBar *fileToolBar = new QToolBar( trUtf8( "File" ) ) ;
00991 fileToolBar->setObjectName( "FileToolBar" ) ;
00992 fileToolBar->addAction( newAct ) ;
00993 fileToolBar->addSeparator() ;
00994 fileToolBar->addAction( openAct ) ;
00995 fileToolBar->addSeparator() ;
00996 fileToolBar->addAction( saveAct ) ;
00997 fileToolBar->addAction( saveAsAct ) ;
00998 addToolBar( Qt::TopToolBarArea, fileToolBar ) ;
00999
01000 QToolBar *editToolBar = new QToolBar( trUtf8( "Edit" ) ) ;
01001 editToolBar->setObjectName( "EditToolBar" ) ;
01002 editToolBar->addAction( addLocAct ) ;
01003 editToolBar->addAction( addLocAreaAct ) ;
01004 editToolBar->addAction( addMapAct ) ;
01005 editToolBar->addSeparator() ;
01006 editToolBar->addAction( engineEditorAct ) ;
01007 editToolBar->addAction( mapEditorAct ) ;
01008 editToolBar->addSeparator() ;
01009 editToolBar->addAction( editModeAct ) ;
01010 addToolBar( Qt::TopToolBarArea, editToolBar ) ;
01011
01012 QToolBar *modelToolBar = new QToolBar( trUtf8( "Model" ) ) ;
01013 modelToolBar->setObjectName( "ModelToolBar" ) ;
01014 modelToolBar->addAction( connectAct ) ;
01015 modelToolBar->addAction( powerAct ) ;
01016 modelToolBar->addSeparator() ;
01017
01018 addToolBar( Qt::TopToolBarArea, modelToolBar ) ;
01019
01020 QToolBar *viewToolBar = new QToolBar( trUtf8( "View" ) ) ;
01021 viewToolBar->setObjectName( "ViewToolBar" ) ;
01022 viewToolBar->addAction( fullscreenAct ) ;
01023 viewToolBar->addAction( hideMenubarAct ) ;
01024 viewToolBar->addSeparator() ;
01025 viewToolBar->addAction( zoomInAct ) ;
01026 viewToolBar->addAction( zoomOutAct ) ;
01027 viewToolBar->addAction( zoomBestFitAct ) ;
01028 viewToolBar->addAction( zoomOriginAct ) ;
01029 viewToolBar->addSeparator() ;
01030 viewToolBar->addAction( tileViewAct ) ;
01031 viewToolBar->addAction( cascadeViewAct );
01032 addToolBar( Qt::TopToolBarArea, viewToolBar ) ;
01033 }
01034
01035 void Win::mkStatusBar()
01036 {
01037 QStatusBar *statBar = statusBar() ;
01038 statBar->setMaximumHeight( 20 ) ;
01039
01040 QLabel *guiState = new QLabel( trUtf8("Command"), this ) ;
01041 QLabel *netState = new QLabel( trUtf8("Disconnected"), this ) ;
01042
01043 statBar->addPermanentWidget( guiState ) ;
01044 statBar->addPermanentWidget( netState ) ;
01045
01046
01047 }
01048
01049 void Win::setMultiWinMode( bool multiWinFlag )
01050 {
01051 if ( _multiWinFlag == multiWinFlag )
01052 return ;
01053
01054 _multiWinFlag = multiWinFlag ;
01055 QWidget *oldParent ;
01056
01057
01058 QList<EnginesArea*> allEnginesAreas =
01059 findChildren<EnginesArea*>( QRegExp( "EnginesArea [0-9]*" ) ) ;
01060
01061 for( int i = 0; i < allEnginesAreas.size(); ++i )
01062 {
01063 oldParent = dynamic_cast<QWidget*>( allEnginesAreas.at( i )->parent() ) ;
01064 addEnginesArea( allEnginesAreas.at( i ) ) ;
01065 oldParent->deleteLater() ;
01066 }
01067
01068
01069
01070
01071
01072 if( !_multiWinFlag )
01073 {
01074 _mapArea = new QMdiArea ;
01075 setCentralWidget( _mapArea ) ;
01076 }
01077
01078 QList<Map*> allMaps = findChildren<Map*>( QRegExp( "Map [0-9]*" ) ) ;
01079
01080 for( int i = 0; i < allMaps.size(); ++i )
01081 {
01082 oldParent = dynamic_cast<QWidget*>( allMaps.at( i )->parent() ) ;
01083 addMap( allMaps.at( i ) ) ;
01084 oldParent->deleteLater() ;
01085 }
01086
01087
01088 if( _multiWinFlag && _mapArea != 0 )
01089 {
01090 _mapArea->deleteLater() ;
01091 _mapArea = 0 ;
01092 }
01093
01094
01095 if ( _editModeFlag )
01096 {
01097 if ( _engineEditor != 0 )
01098 {
01099 _engineEditor->parentWidget()->deleteLater() ;
01100 _engineEditor = 0 ;
01101 engineEdit() ;
01102 }
01103 if ( _mapEditor != 0 )
01104 {
01105 _mapEditor->parentWidget()->deleteLater() ;
01106 _mapEditor = 0 ;
01107 mapEdit() ;
01108 }
01109 }
01110 }
01111
01112 void Win::setUserPath( QString userPath )
01113 {
01114 _userPath = userPath ;
01115 }
01116
01117 void Win::showCloseMessage()
01118 {
01119 _closeMessageDialog->showMessage( trUtf8( "The close event do not delete the map, which stay available in the maps list menu." ) ) ;
01120 }
01121
01122
01123
01124
01125
01126
01127 void Win::setDirty( bool dirty )
01128 {
01129 setWindowModified( dirty ) ;
01130 saveAct->setEnabled( dirty ) ;
01131 }
01132
01133 bool Win::save()
01134 {
01135 bool r ;
01136 if ( windowFilePath().isEmpty() )
01137 r = saveAs() ;
01138 else
01139 r = save( windowFilePath() ) ;
01140
01141 setDirty( !r ) ;
01142 return r ;
01143 }
01144
01145 bool Win::saveAs()
01146 {
01147 QString fileName = QFileDialog::getSaveFileName( this, trUtf8( "Save Model" ), _userPath, trUtf8( "orK Models (*.ork);;All Files (*)" ) ) ;
01148
01149 if( fileName.isEmpty() )
01150 return FALSE ;
01151
01152 setWindowFilePath( fileName ) ;
01153
01154 return save() ;
01155 }
01156
01157 void Win::open()
01158 {
01159 QString fileName = QFileDialog::getOpenFileName( this, tr( "Open Model" ), _userPath, tr( "orK Models (*.ork);;All Files (*)" ) ) ;
01160
01161 if( ! fileName.isEmpty() )
01162 open( fileName ) ;
01163 }
01164
01165 bool Win::maybeSave()
01166 {
01167 if ( isWindowModified() )
01168 {
01169 switch( QMessageBox::warning( this, trUtf8( "OrK" ), trUtf8( "The current model has been modified.\nDo you want to save your changes?" ),QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save) )
01170 {
01171 case QMessageBox::Save :
01172 return save() ;
01173 break ;
01174
01175 case QMessageBox::Discard:
01176 return TRUE ;
01177 break ;
01178
01179 case QMessageBox::Cancel :
01180 return FALSE;
01181 break ;
01182
01183 default :
01184 return TRUE ;
01185 break ;
01186 }
01187 }
01188 else
01189 return TRUE ;
01190 }
01191
01192 void Win::open( QString fileName )
01193 {
01194 if ( ! maybeSave() )
01195 return ;
01196
01197 clearModel() ;
01198
01199 if ( fileName.isEmpty() )
01200 return ;
01201
01202 setWindowFilePath( fileName ) ;
01203
01204 QApplication::setOverrideCursor( QCursor( Qt::BusyCursor ) ) ;
01205
01206 QDomDocument domDoc ;
01207 QFile file( fileName ) ;
01208
01209 if ( ! file.exists() )
01210 {
01211 QMessageBox::warning ( this, trUtf8 ( "OrK open error" ), trUtf8 ( "The file %1 does not exist." ).arg ( QFileInfo( file ).fileName () ) ) ;
01212 QApplication::restoreOverrideCursor() ;
01213 return ;
01214 }
01215
01216 if ( ! file.open( QIODevice::ReadOnly ) )
01217 {
01218 QMessageBox::warning ( this, trUtf8 ( "OrK open error" ), trUtf8 ( "Cannot open %1 file. Check permissions." ).arg ( QFileInfo( file ).fileName () ) ) ;
01219 QApplication::restoreOverrideCursor() ;
01220 return ;
01221 }
01222
01223 if ( ! domDoc.setContent ( file.readAll () ) )
01224 {
01225 QMessageBox::warning ( this, trUtf8 ( "OrK open error" ), trUtf8 ( "Cannot read %1 xml file. Is it a xml file ?" ).arg ( QFileInfo( file ).fileName () ) ) ;
01226 QApplication::restoreOverrideCursor() ;
01227 return ;
01228 }
01229
01230 file.close () ;
01231
01232 if ( domDoc.doctype().name() != "OrKdoc" )
01233 {
01234 QMessageBox::warning ( this, trUtf8 ( "OrK open error" ), trUtf8 ( "The file %1 is not an OrK save file." ).arg ( QFileInfo( file ).fileName () ) ) ;
01235 QApplication::restoreOverrideCursor() ;
01236 return ;
01237 }
01238
01239
01240 QDomElement domElement = domDoc.documentElement().firstChildElement() ;
01241
01242 while( ! domElement.isNull() )
01243 {
01244 Map *map ;
01245 Rail *rail ;
01246 EnginesArea *enginesArea ;
01247 Engine *engine ;
01248
01249 QDomElement itemElement, option, position, engineElement, ruleElement ;
01250 int id, x,y, address, delay, source, target ;
01251 QDomNodeList options, items, engines, rules ;
01252 QString optionKey, optionValue, name, typeId, tooltip, imagePath, trigger, action ;
01253
01254 QString decoder ;
01255
01256 if( domElement.tagName() == "map" )
01257 {
01258 if ( domElement.attribute( "version", "" ) != OrKMap::Version )
01259 qDebug() << "Not valid version. Probable unexpected behaviour" ;
01260
01261 name = domElement.attribute( "name", "" ) ;
01262 id = domElement.attribute( "id", "-1" ).toInt() ;
01263
01264 if ( id == -1 )
01265 qDebug() << "No id or id not valid: error !" ;
01266
01267 map = new Map() ;
01268 map->setName( name ) ;
01269 map->setObjectName( QString("Map %1").arg( id ) ) ;
01270 addMap( map ) ;
01271
01272 items = domElement.elementsByTagName( "item" ) ;
01273
01274 for( uint i = 0; i < items.length(); ++i )
01275 {
01276
01277 address = delay = 0 ;
01278 tooltip = decoder = "";
01279
01280 itemElement = items.item( i ).toElement() ;
01281
01282 typeId = itemElement.attribute( "typeid", "" ) ;
01283 if ( typeId.isEmpty() )
01284 qDebug() << "No typeId attribute: error !" ;
01285
01286 position = itemElement.firstChildElement( "position" ) ;
01287 if ( position.isNull() )
01288 qDebug() << "No position Tag: error !" ;
01289
01290 x = position.attribute( "x", "0" ).toInt() ;
01291 y = position.attribute( "y", "0" ).toInt() ;
01292
01293 options = itemElement.elementsByTagName( "option" ) ;
01294
01295 for ( uint j = 0; j < options.length(); ++j)
01296 {
01297 option = options.item( j ).toElement() ;
01298 optionKey = option.attribute( "name" ) ;
01299 optionValue = option.text() ;
01300
01301 if ( optionKey == "address" )
01302 address = optionValue.toInt() ;
01303 else if ( optionKey == "decoder" )
01304 decoder = optionValue ;
01305 else if ( optionKey == "delay" )
01306 delay = optionValue.toInt() ;
01307 else if ( optionKey == "tooltip" )
01308 tooltip = optionValue ;
01309 else
01310 qDebug() << QString("unknown option %1 with value %2").arg( optionKey ).arg( optionValue ) ;
01311 }
01312
01313 rail = new Rail( _mapRenderer, typeId, address, delay, decoder, tooltip ) ;
01314
01315 map->addItem( rail ) ;
01316 rail->setPos( x * OrKMap::ItemXSize, y * OrKMap::ItemYSize ) ;
01317
01318 addRail( rail ) ;
01319
01320 }
01321
01322 }
01323 else if ( domElement.tagName() == "enginesArea" )
01324 {
01325 if ( domElement.attribute( "version", "" ) != OrKEngine::Version )
01326 qDebug() << "Not valid version. Probable unexpected behaviour" ;
01327
01328 name = domElement.attribute( "name", "" ) ;
01329 id = domElement.attribute( "id", "-1" ).toInt() ;
01330 if ( id == -1 )
01331 qDebug() << "No id or id not valid: error !" ;
01332
01333 enginesArea = new EnginesArea() ;
01334 enginesArea->setName( name ) ;
01335 enginesArea->setObjectName( QString("EnginesArea %1").arg( id ) ) ;
01336 addEnginesArea( enginesArea ) ;
01337
01338 engines = domElement.elementsByTagName( "engine" ) ;
01339
01340 for( uint i = 0; i < engines.length(); ++i )
01341 {
01342
01343 address = 0 ;
01344 imagePath = decoder = "";
01345
01346 engineElement = engines.item( i ).toElement() ;
01347
01348 name = engineElement.attribute( "name", "" ) ;
01349 id = engineElement.attribute( "id", "-1" ).toInt() ;
01350
01351 options = engineElement.elementsByTagName( "option" ) ;
01352
01353 for ( uint j = 0; j < options.length(); ++j)
01354 {
01355
01356 option = options.item( j ).toElement() ;
01357 optionKey = option.attribute( "name" ) ;
01358 optionValue = option.text() ;
01359
01360 if ( optionKey == "address" )
01361 address = optionValue.toInt() ;
01362 else if ( optionKey == "decoder" )
01363 decoder = optionValue ;
01364 else if ( optionKey == "image" )
01365 imagePath = optionValue ;
01366 else
01367 qDebug() << QString("unknown option %1 with value %2").arg( optionKey ).arg( optionValue ) ;
01368 }
01369
01370 engine = new Engine( decoder, address, imagePath, name, enginesArea ) ;
01371 enginesArea->insertWidget( engine, id ) ;
01372
01373 addEngine( engine ) ;
01374
01375 }
01376
01377 }
01378 else if ( domElement.tagName() == "rules" )
01379 {
01380 if ( domElement.attribute( "version", "" ) != OrKRule::Version )
01381 qDebug() << "Not valid version. Probable unexpected behaviour" ;
01382
01383 rules = domElement.elementsByTagName( "rule" ) ;
01384
01385 for( uint i = 0; i < rules.length(); ++i )
01386 {
01387
01388 trigger = action = "";
01389
01390 ruleElement = rules.item( i ).toElement() ;
01391
01392 name = ruleElement.attribute( "name", "" ) ;
01393 source = ruleElement.attribute( "source", "0" ).toInt() ;
01394 target = ruleElement.attribute( "target", "0" ).toInt() ;
01395
01396 options = ruleElement.elementsByTagName( "option" ) ;
01397
01398 for ( uint j = 0; j < options.length(); ++j)
01399 {
01400
01401 option = options.item( j ).toElement() ;
01402 optionKey = option.attribute( "name" ) ;
01403 optionValue = option.text() ;
01404
01405 if ( optionKey == "trigger" )
01406 trigger = optionValue ;
01407 else if ( optionKey == "action" )
01408 action = optionValue ;
01409 else
01410 qDebug() << QString("unknown option %1 with value %2").arg( optionKey ).arg( optionValue ) ;
01411 }
01412
01413 if ( ! trigger.isEmpty() && ! action.isEmpty() )
01414 addRule( trigger, source, action, target ) ;
01415 else
01416 {
01417 addRule( "On", source, "On", target ) ;
01418 addRule( "Off", source, "Off", target ) ;
01419 }
01420
01421 }
01422
01423 }
01424 else
01425 {
01426 qDebug() << "Unknown tag element" ;
01427 }
01428
01429 domElement = domElement.nextSiblingElement() ;
01430
01431 }
01432
01433 setDirty( FALSE ) ;
01434
01435 QApplication::restoreOverrideCursor() ;
01436
01437 }
01438
01439 bool Win::save( QString fileName )
01440 {
01441 QApplication::setOverrideCursor( QCursor( Qt::BusyCursor ) ) ;
01442
01443 QFile file( fileName ) ;
01444
01445 if ( ! file.open( QIODevice::Truncate | QIODevice::WriteOnly | QIODevice::Text ) )
01446 {
01447 QMessageBox::warning ( 0, trUtf8 ( "OrK open error" ), trUtf8 ( "Cannot open %1 file. Check permissions." ).arg ( QFileInfo( file ).fileName () ) ) ;
01448 QApplication::restoreOverrideCursor() ;
01449 return FALSE;
01450 }
01451
01452 QDomDocument doc("OrKdoc") ;
01453
01454 QDomComment info = doc.createComment( trUtf8("This document was generated by OrK version %1").arg( OrK::Version ) ) ;
01455 doc.appendChild( info ) ;
01456
01457 QDomElement root = doc.createElement( "OrKsave" ) ;
01458 doc.appendChild( root ) ;
01459
01460 Map* map ;
01461 for (int i = 0; i < _maps->size(); ++i)
01462 {
01463 map = _maps->at(i) ;
01464
01465 QDomElement mapElement = doc.createElement( "map" ) ;
01466 mapElement.setAttribute( "id" , i ) ;
01467 mapElement.setAttribute( "version" , OrKMap::Version ) ;
01468 mapElement.setAttribute( "name" , map->name() ) ;
01469
01470 QList<QGraphicsItem *> rails = map->items () ;
01471 Rail* r ;
01472
01473 for (int j = 0; j < rails.size(); ++j)
01474 {
01475 r = dynamic_cast<Rail*>( rails.at(j) ) ;
01476 QDomElement railElement = doc.createElement( "item" ) ;
01477 railElement.setAttribute( "typeid", r->type() ) ;
01478
01479 QDomElement position = doc.createElement( "position" ) ;
01480 QPointF pos = r->scenePos() ;
01481
01482 position.setAttribute( "x", round( pos.x() / OrKMap::ItemXSize ) ) ;
01483 position.setAttribute( "y", round( pos.y() / OrKMap::ItemYSize ) ) ;
01484
01485 railElement.appendChild( position ) ;
01486
01487 if ( r->address() != 0 )
01488 {
01489
01490 QDomElement addressElement = doc.createElement( "option" ) ;
01491 addressElement.setAttribute( "name", "address" ) ;
01492 QDomText addressValue = doc.createTextNode( QString().setNum( r->address() ) ) ;
01493 addressElement.appendChild( addressValue ) ;
01494 railElement.appendChild( addressElement ) ;
01495
01496
01497 QDomElement delayElement = doc.createElement("option") ;
01498 delayElement.setAttribute( "name", "delay" ) ;
01499 QDomText delayValue = doc.createTextNode( QString().setNum( r->delay() ) ) ;
01500 delayElement.appendChild( delayValue ) ;
01501 railElement.appendChild( delayElement ) ;
01502
01503
01504 QDomElement decoderElement = doc.createElement("option") ;
01505 decoderElement.setAttribute( "name", "decoder" ) ;
01506 QDomText decoderValue = doc.createTextNode( r->decoder() ) ;
01507 decoderElement.appendChild( decoderValue ) ;
01508 railElement.appendChild( decoderElement ) ;
01509
01510 }
01511
01512 mapElement.appendChild( railElement ) ;
01513
01514 }
01515
01516 root.appendChild( mapElement ) ;
01517
01518 }
01519
01520 EnginesArea* enginesArea ;
01521 for (int i = 0; i < _enginesAreas->size(); ++i)
01522 {
01523 enginesArea = _enginesAreas->at(i) ;
01524
01525 QDomElement enginesAreaElement = doc.createElement( "enginesArea" ) ;
01526 enginesAreaElement.setAttribute( "id" , i ) ;
01527 enginesAreaElement.setAttribute( "version" , OrKEngine::Version ) ;
01528 enginesAreaElement.setAttribute( "name" , enginesArea->name() ) ;
01529
01530 Engine* engine ;
01531 for (int j = 0; j < enginesArea->count(); ++j)
01532 {
01533 engine = dynamic_cast<Engine*>( enginesArea->itemAt(j)->widget() ) ;
01534
01535 if ( ! engine )
01536 continue ;
01537
01538 QDomElement engineElement = doc.createElement( "engine" ) ;
01539 engineElement.setAttribute( "id", j ) ;
01540 engineElement.setAttribute( "name", engine->name() ) ;
01541
01542
01543 QDomElement addressElement = doc.createElement( "option" ) ;
01544 addressElement.setAttribute( "name", "address" ) ;
01545 QDomText addressValue = doc.createTextNode( QString().setNum( engine->address() ) ) ;
01546 addressElement.appendChild( addressValue ) ;
01547 engineElement.appendChild( addressElement ) ;
01548
01549
01550 QDomElement decoderElement = doc.createElement("option") ;
01551 decoderElement.setAttribute( "name", "decoder" ) ;
01552 QDomText decoderValue = doc.createTextNode( engine->decoder() ) ;
01553 decoderElement.appendChild( decoderValue ) ;
01554 engineElement.appendChild( decoderElement ) ;
01555
01556
01557 QDomElement imageElement = doc.createElement("option") ;
01558 imageElement.setAttribute( "name", "image" ) ;
01559 QDomText imageValue = doc.createTextNode( engine->imagePath() ) ;
01560 imageElement.appendChild( imageValue ) ;
01561 engineElement.appendChild( imageElement ) ;
01562
01563
01564 enginesAreaElement.appendChild( engineElement ) ;
01565
01566 }
01567
01568 root.appendChild( enginesAreaElement ) ;
01569
01570 }
01571
01572
01573
01574 file.write( QString( "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n").toUtf8() ) ;
01575 file.write( doc.toByteArray( 4 ) ) ;
01576 file.close() ;
01577
01578 QApplication::restoreOverrideCursor() ;
01579
01580 return TRUE ;
01581 }