1 #include "STD_Application.h"
3 #include "STD_MDIDesktop.h"
5 #include "STD_CloseDlg.h"
7 #include <SUIT_Tools.h>
8 #include <SUIT_Desktop.h>
9 #include <SUIT_Session.h>
10 #include <SUIT_ViewModel.h>
11 #include <SUIT_Operation.h>
12 #include <SUIT_MessageBox.h>
13 #include <SUIT_ResourceMgr.h>
15 #include <QtxDockAction.h>
16 #include <QtxActionMenuMgr.h>
17 #include <QtxActionToolMgr.h>
18 #include <QtxPopupMenu.h>
22 #include <qpopupmenu.h>
23 #include <qstatusbar.h>
24 #include <qfiledialog.h>
25 #include <qapplication.h>
29 extern "C" STD_EXPORT SUIT_Application* createApplication()
31 return new STD_Application();
34 STD_Application::STD_Application()
36 myEditEnabled( true ),
39 STD_MDIDesktop* desk = new STD_MDIDesktop();
41 connect( desk, SIGNAL( closing( SUIT_Desktop*, QCloseEvent* ) ),
42 this, SLOT( onDesktopClosing( SUIT_Desktop*, QCloseEvent* ) ) );
47 STD_Application::~STD_Application()
51 QString STD_Application::applicationName() const
53 return QString( "StdApplication" );
56 void STD_Application::start()
61 updateCommandsStatus();
62 setEditEnabled( myEditEnabled );
64 SUIT_Application::start();
67 void STD_Application::onDesktopClosing( SUIT_Desktop*, QCloseEvent* e )
69 if ( !isPossibleToClose() )
75 SUIT_Study* study = activeStudy();
78 study->closeDocument();
88 void STD_Application::createActions()
90 SUIT_Desktop* desk = desktop();
91 SUIT_ResourceMgr* resMgr = resourceMgr();
92 if ( !desk || !resMgr )
97 createAction( FileNewId, tr( "TOT_DESK_FILE_NEW" ),
98 resMgr->loadPixmap( "STD", tr( "ICON_FILE_NEW" ) ),
99 tr( "MEN_DESK_FILE_NEW" ), tr( "PRP_DESK_FILE_NEW" ),
100 CTRL+Key_N, desk, false, this, SLOT( onNewDoc() ) );
102 createAction( FileOpenId, tr( "TOT_DESK_FILE_OPEN" ),
103 resMgr->loadPixmap( "STD", tr( "ICON_FILE_OPEN" ) ),
104 tr( "MEN_DESK_FILE_OPEN" ), tr( "PRP_DESK_FILE_OPEN" ),
105 CTRL+Key_O, desk, false, this, SLOT( onOpenDoc() ) );
107 createAction( FileCloseId, tr( "TOT_DESK_FILE_CLOSE" ),
108 resMgr->loadPixmap( "STD", tr( "ICON_FILE_CLOSE" ) ),
109 tr( "MEN_DESK_FILE_CLOSE" ), tr( "PRP_DESK_FILE_CLOSE" ),
110 CTRL+Key_W, desk, false, this, SLOT( onCloseDoc() ) );
112 createAction( FileExitId, tr( "TOT_DESK_FILE_EXIT" ), QIconSet(),
113 tr( "MEN_DESK_FILE_EXIT" ), tr( "PRP_DESK_FILE_EXIT" ),
114 CTRL+Key_Q, desk, false, this, SLOT( onExit() ) );
116 createAction( FileSaveId, tr( "TOT_DESK_FILE_SAVE" ),
117 resMgr->loadPixmap( "STD", tr( "ICON_FILE_SAVE" ) ),
118 tr( "MEN_DESK_FILE_SAVE" ), tr( "PRP_DESK_FILE_SAVE" ),
119 CTRL+Key_S, desk, false, this, SLOT( onSaveDoc() ) );
121 createAction( FileSaveAsId, tr( "TOT_DESK_FILE_SAVEAS" ), QIconSet(),
122 tr( "MEN_DESK_FILE_SAVEAS" ), tr( "PRP_DESK_FILE_SAVEAS" ),
123 0, desk, false, this, SLOT( onSaveAsDoc() ) );
125 createAction( EditCopyId, tr( "TOT_DESK_EDIT_COPY" ),
126 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_COPY" ) ),
127 tr( "MEN_DESK_EDIT_COPY" ), tr( "PRP_DESK_EDIT_COPY" ),
128 CTRL+Key_C, desk, false, this, SLOT( onCopy() ) );
130 createAction( EditPasteId, tr( "TOT_DESK_EDIT_PASTE" ),
131 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_PASTE" ) ),
132 tr( "MEN_DESK_EDIT_PASTE" ), tr( "PRP_DESK_EDIT_PASTE" ),
133 CTRL+Key_V, desk, false, this, SLOT( onPaste() ) );
135 QAction* a = createAction( ViewStatusBarId, tr( "TOT_DESK_VIEW_STATUSBAR" ),
136 QIconSet(), tr( "MEN_DESK_VIEW_STATUSBAR" ),
137 tr( "PRP_DESK_VIEW_STATUSBAR" ), 0, desk, true );
138 a->setOn( desk->statusBar()->isVisibleTo( desk ) );
139 connect( a, SIGNAL( toggled( bool ) ), this, SLOT( onViewStatusBar( bool ) ) );
141 createAction( NewWindowId, tr( "TOT_DESK_NEWWINDOW" ), QIconSet(),
142 tr( "MEN_DESK_NEWWINDOW" ), tr( "PRP_DESK_NEWWINDOW" ), 0, desk );
144 createAction( HelpAboutId, tr( "TOT_DESK_HELP_ABOUT" ), QIconSet(),
145 tr( "MEN_DESK_HELP_ABOUT" ), tr( "PRP_DESK_HELP_ABOUT" ),
146 0, desk, false, this, SLOT( onHelpAbout() ) );
148 //SRN: BugID IPAL9021, add an action "Load"
149 createAction( FileLoadId, tr( "TOT_DESK_FILE_LOAD" ),
150 resMgr->loadPixmap( "STD", tr( "ICON_FILE_OPEN" ) ),
151 tr( "MEN_DESK_FILE_LOAD" ), tr( "PRP_DESK_FILE_LOAD" ),
152 CTRL+Key_L, desk, false, this, SLOT( onLoadDoc() ) );
153 //SRN: BugID IPAL9021: End
155 QtxDockAction* da = new QtxDockAction( tr( "TOT_DOCK_WINDOWS" ), tr( "MEN_DOCK_WINDOWS" ), desk );
156 registerAction( ViewWindowsId, da );
157 da->setAutoPlace( false );
161 int fileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1, -1, 0 );
162 int editMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 10 );
163 int viewMenu = createMenu( tr( "MEN_DESK_VIEW" ), -1, -1, 10 );
164 int helpMenu = createMenu( tr( "MEN_DESK_HELP" ), -1, -1, 1000 );
168 createMenu( FileNewId, fileMenu, 0 );
169 createMenu( FileOpenId, fileMenu, 0 );
170 createMenu( FileLoadId, fileMenu, 0 ); //SRN: BugID IPAL9021, add a menu item "Load"
171 createMenu( FileCloseId, fileMenu, 0 );
172 createMenu( separator(), fileMenu, -1, 0 );
173 createMenu( FileSaveId, fileMenu, 0 );
174 createMenu( FileSaveAsId, fileMenu, 0 );
175 createMenu( separator(), fileMenu, -1, 0 );
177 createMenu( separator(), fileMenu );
178 createMenu( FileExitId, fileMenu );
180 createMenu( EditCopyId, editMenu );
181 createMenu( EditPasteId, editMenu );
182 createMenu( separator(), editMenu );
184 createMenu( ViewWindowsId, viewMenu );
185 createMenu( ViewStatusBarId, viewMenu );
186 createMenu( separator(), viewMenu );
188 createMenu( HelpAboutId, helpMenu );
189 createMenu( separator(), helpMenu );
193 int stdTBar = createTool( tr( "INF_DESK_TOOLBAR_STANDARD" ) );
197 createTool( FileNewId, stdTBar );
198 createTool( FileOpenId, stdTBar );
199 createTool( FileSaveId, stdTBar );
200 createTool( FileCloseId, stdTBar );
201 createTool( separator(), stdTBar );
202 createTool( EditCopyId, stdTBar );
203 createTool( EditPasteId, stdTBar );
207 Opens new application
209 void STD_Application::onNewDoc()
211 if ( !activeStudy() )
214 activeStudy()->createDocument();
215 updateDesktopTitle();
216 updateCommandsStatus();
220 SUIT_Application* aApp = startApplication( 0, 0 );
221 if ( aApp->inherits( "STD_Application" ) )
222 ((STD_Application*)aApp)->onNewDoc();
224 aApp->createEmptyStudy();
225 aApp->activeStudy()->createDocument();
230 void STD_Application::onOpenDoc()
232 // It is preferrable to use OS-specific file dialog box here !!!
233 QString aName = getFileName( true, QString::null, getFileFilter(), QString::null, 0 );
234 if ( aName.isNull() )
240 bool STD_Application::onOpenDoc( const QString& aName )
243 if ( !activeStudy() )
245 // if no study - open in current desktop
246 res = useFile( aName );
250 // if study exists - open in new desktop. Check: is the same file is opened?
251 SUIT_Session* aSession = SUIT_Session::session();
252 QPtrList<SUIT_Application> aAppList = aSession->applications();
253 bool isAlreadyOpen = false;
254 SUIT_Application* aApp = 0;
255 for ( QPtrListIterator<SUIT_Application> it( aAppList ); it.current() && !isAlreadyOpen; ++it )
258 if ( aApp->activeStudy()->studyName() == aName )
259 isAlreadyOpen = true;
261 if ( !isAlreadyOpen )
263 aApp = startApplication( 0, 0 );
265 res = aApp->useFile( aName );
268 aApp->desktop()->setActiveWindow();
273 bool STD_Application::onLoadDoc( const QString& aName )
276 if ( !activeStudy() )
278 // if no study - load in current desktop
279 res = useStudy( aName );
283 // if study exists - load in new desktop. Check: is the same file is loaded?
284 SUIT_Session* aSession = SUIT_Session::session();
285 QPtrList<SUIT_Application> aAppList = aSession->applications();
286 bool isAlreadyOpen = false;
287 SUIT_Application* aApp = 0;
288 for ( QPtrListIterator<SUIT_Application> it( aAppList ); it.current() && !isAlreadyOpen; ++it )
291 if ( aApp->activeStudy()->studyName() == aName )
292 isAlreadyOpen = true;
294 if ( !isAlreadyOpen )
296 aApp = startApplication( 0, 0 );
298 res = aApp->useStudy( aName );
301 aApp->desktop()->setActiveWindow();
306 void STD_Application::beforeCloseDoc( SUIT_Study* )
310 void STD_Application::afterCloseDoc()
314 void STD_Application::onCloseDoc()
316 if ( !isPossibleToClose() )
319 SUIT_Study* study = activeStudy();
321 beforeCloseDoc( study );
324 study->closeDocument();
332 QPtrList<SUIT_Application> apps = SUIT_Session::session()->applications();
333 for ( unsigned i = 0; i < apps.count(); i++ )
334 aNbStudies += apps.at( i )->getNbStudies();
336 // STV: aNbStudies - number of currently existing studies (exclude currently closed)
337 // STV: aNbStudies should be compared with 0.
342 updateDesktopTitle();
343 updateCommandsStatus();
352 bool STD_Application::isPossibleToClose()
356 activeStudy()->abortAllOperations();
357 if ( activeStudy()->isModified() )
359 QString sName = activeStudy()->studyName().stripWhiteSpace();
360 QString msg = sName.isEmpty() ? tr( "INF_DOC_MODIFIED" ) : tr ( "INF_DOCUMENT_MODIFIED" ).arg( sName );
362 //SRN: BugID: IPAL9021: Begin
363 STD_CloseDlg dlg(desktop());
367 if ( activeStudy()->isSaved() )
369 else if ( !onSaveAsDoc() )
380 //SRN: BugID: IPAL9021: End
386 void STD_Application::onSaveDoc()
388 if ( !activeStudy() )
392 if ( activeStudy()->isSaved() )
394 isOk = activeStudy()->saveDocument();
396 SUIT_MessageBox::error1( desktop(), tr( "TIT_FILE_SAVEAS" ),
397 tr( "MSG_CANT_SAVE" ).arg( activeStudy()->studyName() ), tr( "BUT_OK" ) );
401 updateCommandsStatus();
406 bool STD_Application::onSaveAsDoc()
408 SUIT_Study* study = activeStudy();
412 QString aName = getFileName( false, study->studyName(), getFileFilter(), QString::null, 0 );
414 if ( aName.isNull() )
416 bool isOk = study->saveDocumentAs( aName );
418 updateDesktopTitle();
419 updateCommandsStatus();
424 void STD_Application::onExit()
426 SUIT_Session::session()->closeSession();
429 void STD_Application::onCopy()
433 void STD_Application::onPaste()
437 void STD_Application::setEditEnabled( bool theEnable )
439 myEditEnabled = theEnable;
441 QtxActionMenuMgr* mMgr = desktop()->menuMgr();
442 QtxActionToolMgr* tMgr = desktop()->toolMgr();
444 for ( int i = EditCopyId; i <= EditPasteId; i++ )
446 mMgr->setShown( i, myEditEnabled );
447 tMgr->setShown( i, myEditEnabled );
451 bool STD_Application::useFile(const QString& theFileName)
453 bool res = SUIT_Application::useFile(theFileName);
454 updateDesktopTitle();
455 updateCommandsStatus();
459 void STD_Application::updateDesktopTitle()
461 QString aTitle = applicationName();
462 QString aVer = applicationVersion();
463 if ( !aVer.isEmpty() )
464 aTitle += QString( " " ) + aVer;
468 QString sName = SUIT_Tools::file( activeStudy()->studyName().stripWhiteSpace(), false );
469 if ( !sName.isEmpty() )
470 aTitle += QString( " - [%1]" ).arg( sName );
473 desktop()->setCaption( aTitle );
476 void STD_Application::updateCommandsStatus()
478 bool aHasStudy = activeStudy() != 0;
479 bool aIsNeedToSave = false;
481 aIsNeedToSave = !activeStudy()->isSaved() || activeStudy()->isModified();
483 if ( action( FileSaveId ) )
484 action( FileSaveId )->setEnabled( aIsNeedToSave );
485 if ( action( FileSaveAsId ) )
486 action( FileSaveAsId )->setEnabled( aHasStudy );
487 if ( action( FileCloseId ) )
488 action( FileCloseId )->setEnabled( aHasStudy );
489 if ( action( NewWindowId ) )
490 action( NewWindowId )->setEnabled( aHasStudy );
493 SUIT_ViewManager* STD_Application::viewManager( const QString& vmType ) const
495 SUIT_ViewManager* vm = 0;
496 for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current() && !vm; ++it )
498 if ( it.current()->getType() == vmType )
504 void STD_Application::viewManagers( const QString& vmType, ViewManagerList& lst ) const
506 for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current(); ++it )
507 if ( it.current()->getType() == vmType )
508 lst.append( it.current() );
511 void STD_Application::viewManagers( ViewManagerList& lst ) const
513 for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current(); ++it )
514 lst.append( it.current() );
517 ViewManagerList STD_Application::viewManagers() const
524 SUIT_ViewManager* STD_Application::activeViewManager() const
526 return myActiveViewMgr;
529 void STD_Application::addViewManager( SUIT_ViewManager* vm )
534 if ( !containsViewManager( vm ) )
536 myViewMgrs.append( vm );
537 connect( vm, SIGNAL( activated( SUIT_ViewManager* ) ),
538 this, SLOT( onViewManagerActivated( SUIT_ViewManager* ) ) );
539 vm->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
541 emit viewManagerAdded( vm );
544 if ( !activeViewManager() && myViewMgrs.count() == 1 )
545 setActiveViewManager( vm );
549 void STD_Application::removeViewManager( SUIT_ViewManager* vm )
556 emit viewManagerRemoved( vm );
558 vm->disconnectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
560 myViewMgrs.removeRef( vm );
562 if ( myActiveViewMgr == vm )
566 void STD_Application::clearViewManagers()
571 for ( QPtrListIterator<SUIT_ViewManager> it( lst ); it.current(); ++it )
572 removeViewManager( it.current() );
575 bool STD_Application::containsViewManager( SUIT_ViewManager* vm ) const
577 return myViewMgrs.contains( vm ) > 0;
580 void STD_Application::onViewManagerActivated( SUIT_ViewManager* vm )
582 setActiveViewManager( vm );
585 void STD_Application::onViewStatusBar( bool on )
588 desktop()->statusBar()->show();
590 desktop()->statusBar()->hide();
593 void STD_Application::onHelpAbout()
595 SUIT_MessageBox::info1( desktop(), tr( "About" ), tr( "ABOUT_INFO" ), "&OK" );
598 void STD_Application::createEmptyStudy()
600 SUIT_Application::createEmptyStudy();
602 SUIT_ViewManager* vm = new SUIT_ViewManager( activeStudy(), desktop(), new SUIT_ViewModel() );
604 addViewManager( vm );
607 void STD_Application::setActiveViewManager( SUIT_ViewManager* vm )
609 if ( !containsViewManager( vm ) )
612 myActiveViewMgr = vm;
613 emit viewManagerActivated( vm );
616 void STD_Application::onConnectPopupRequest( SUIT_PopupClient* client, QContextMenuEvent* e )
618 QtxPopupMenu* popup = new QtxPopupMenu();
619 // fill popup by own items
621 contextMenuPopup( client->popupClientType(), popup, title );
622 popup->setTitleText( title );
624 popup->insertSeparator();
625 // add items from popup client
626 client->contextMenuPopup( popup );
628 SUIT_Tools::simplifySeparators( popup );
630 if ( popup->count() )
631 popup->exec( e->globalPos() );
635 QString STD_Application::getFileName( bool open, const QString& initial, const QString& filters,
636 const QString& caption, QWidget* parent )
642 return QFileDialog::getOpenFileName( initial, filters, parent, 0, caption );
648 QString anOldPath = initial;
653 // It is preferrable to use OS-specific file dialog box here !!!
654 aName = QFileDialog::getSaveFileName( anOldPath, filters, parent,
655 0, caption, &aUsedFilter);
657 if ( aName.isNull() )
661 if ( !getFileFilter().isNull() ) // check extension and add if it is necessary
663 int aStart = aUsedFilter.find( '*' );
664 int aEnd = aUsedFilter.find( ')', aStart + 1 );
665 QString aExt = aUsedFilter.mid( aStart + 1, aEnd - aStart - 1 );
666 if ( aExt.contains( '*' ) == 0 ) // if it is not *.*
668 // Check that there is an extension at the end of the name
669 QString aNameTail = aName.right( aExt.length() );
670 if ( aNameTail != aExt )
674 if ( QFileInfo( aName ).exists() )
676 int aAnswer = SUIT_MessageBox::warn3( desktop(), tr( "TIT_FILE_SAVEAS" ),
677 tr( "MSG_FILE_EXISTS" ).arg( aName ),
678 tr( "BUT_YES" ), tr( "BUT_NO" ), tr( "BUT_CANCEL" ), 1, 2, 3, 1 );
679 if ( aAnswer == 3 ) { // cancelled
680 aName = QString::null;
683 else if ( aAnswer == 2 ) // not save to this file
684 anOldPath = aName; // not to return to the same initial dir at each "while" step
685 else // overwrite the existing file
696 QString STD_Application::getDirectory( const QString& initial, const QString& caption, QWidget* parent )
700 return QFileDialog::getExistingDirectory( initial, parent, 0, caption, true );