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 /*!Create and return new instance of STD_Application*/
30 extern "C" STD_EXPORT SUIT_Application* createApplication()
32 return new STD_Application();
36 STD_Application::STD_Application()
38 myEditEnabled( true ),
41 STD_MDIDesktop* desk = new STD_MDIDesktop();
43 connect( desk, SIGNAL( closing( SUIT_Desktop*, QCloseEvent* ) ),
44 this, SLOT( onDesktopClosing( SUIT_Desktop*, QCloseEvent* ) ) );
50 STD_Application::~STD_Application()
54 /*! \retval QString "StdApplication"*/
55 QString STD_Application::applicationName() const
57 return QString( "StdApplication" );
60 /*!Start STD_Application*/
61 void STD_Application::start()
66 updateCommandsStatus();
67 setEditEnabled( myEditEnabled );
69 SUIT_Application::start();
72 /*!Event on closing desktop*/
73 void STD_Application::onDesktopClosing( SUIT_Desktop*, QCloseEvent* e )
75 if ( !isPossibleToClose() )
81 SUIT_Study* study = activeStudy();
84 study->closeDocument();
94 /*!Create actions, menus and tools*/
95 void STD_Application::createActions()
97 SUIT_Desktop* desk = desktop();
98 SUIT_ResourceMgr* resMgr = resourceMgr();
99 if ( !desk || !resMgr )
104 createAction( FileNewId, tr( "TOT_DESK_FILE_NEW" ),
105 resMgr->loadPixmap( "STD", tr( "ICON_FILE_NEW" ) ),
106 tr( "MEN_DESK_FILE_NEW" ), tr( "PRP_DESK_FILE_NEW" ),
107 CTRL+Key_N, desk, false, this, SLOT( onNewDoc() ) );
109 createAction( FileOpenId, tr( "TOT_DESK_FILE_OPEN" ),
110 resMgr->loadPixmap( "STD", tr( "ICON_FILE_OPEN" ) ),
111 tr( "MEN_DESK_FILE_OPEN" ), tr( "PRP_DESK_FILE_OPEN" ),
112 CTRL+Key_O, desk, false, this, SLOT( onOpenDoc() ) );
114 createAction( FileCloseId, tr( "TOT_DESK_FILE_CLOSE" ),
115 resMgr->loadPixmap( "STD", tr( "ICON_FILE_CLOSE" ) ),
116 tr( "MEN_DESK_FILE_CLOSE" ), tr( "PRP_DESK_FILE_CLOSE" ),
117 CTRL+Key_W, desk, false, this, SLOT( onCloseDoc() ) );
119 createAction( FileExitId, tr( "TOT_DESK_FILE_EXIT" ), QIconSet(),
120 tr( "MEN_DESK_FILE_EXIT" ), tr( "PRP_DESK_FILE_EXIT" ),
121 CTRL+Key_Q, desk, false, this, SLOT( onExit() ) );
123 createAction( FileSaveId, tr( "TOT_DESK_FILE_SAVE" ),
124 resMgr->loadPixmap( "STD", tr( "ICON_FILE_SAVE" ) ),
125 tr( "MEN_DESK_FILE_SAVE" ), tr( "PRP_DESK_FILE_SAVE" ),
126 CTRL+Key_S, desk, false, this, SLOT( onSaveDoc() ) );
128 createAction( FileSaveAsId, tr( "TOT_DESK_FILE_SAVEAS" ), QIconSet(),
129 tr( "MEN_DESK_FILE_SAVEAS" ), tr( "PRP_DESK_FILE_SAVEAS" ),
130 0, desk, false, this, SLOT( onSaveAsDoc() ) );
132 createAction( EditCopyId, tr( "TOT_DESK_EDIT_COPY" ),
133 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_COPY" ) ),
134 tr( "MEN_DESK_EDIT_COPY" ), tr( "PRP_DESK_EDIT_COPY" ),
135 CTRL+Key_C, desk, false, this, SLOT( onCopy() ) );
137 createAction( EditPasteId, tr( "TOT_DESK_EDIT_PASTE" ),
138 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_PASTE" ) ),
139 tr( "MEN_DESK_EDIT_PASTE" ), tr( "PRP_DESK_EDIT_PASTE" ),
140 CTRL+Key_V, desk, false, this, SLOT( onPaste() ) );
142 QAction* a = createAction( ViewStatusBarId, tr( "TOT_DESK_VIEW_STATUSBAR" ),
143 QIconSet(), tr( "MEN_DESK_VIEW_STATUSBAR" ),
144 tr( "PRP_DESK_VIEW_STATUSBAR" ), 0, desk, true );
145 a->setOn( desk->statusBar()->isVisibleTo( desk ) );
146 connect( a, SIGNAL( toggled( bool ) ), this, SLOT( onViewStatusBar( bool ) ) );
148 createAction( NewWindowId, tr( "TOT_DESK_NEWWINDOW" ), QIconSet(),
149 tr( "MEN_DESK_NEWWINDOW" ), tr( "PRP_DESK_NEWWINDOW" ), 0, desk );
151 createAction( HelpAboutId, tr( "TOT_DESK_HELP_ABOUT" ), QIconSet(),
152 tr( "MEN_DESK_HELP_ABOUT" ), tr( "PRP_DESK_HELP_ABOUT" ),
153 0, desk, false, this, SLOT( onHelpAbout() ) );
155 //SRN: BugID IPAL9021, add an action "Load"
156 createAction( FileLoadId, tr( "TOT_DESK_FILE_LOAD" ),
157 resMgr->loadPixmap( "STD", tr( "ICON_FILE_OPEN" ) ),
158 tr( "MEN_DESK_FILE_LOAD" ), tr( "PRP_DESK_FILE_LOAD" ),
159 CTRL+Key_L, desk, false, this, SLOT( onLoadDoc() ) );
160 //SRN: BugID IPAL9021: End
162 QtxDockAction* da = new QtxDockAction( tr( "TOT_DOCK_WINDOWS" ), tr( "MEN_DOCK_WINDOWS" ), desk );
163 registerAction( ViewWindowsId, da );
164 da->setAutoPlace( false );
168 int fileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1, -1, 0 );
169 int editMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 10 );
170 int viewMenu = createMenu( tr( "MEN_DESK_VIEW" ), -1, -1, 10 );
171 int helpMenu = createMenu( tr( "MEN_DESK_HELP" ), -1, -1, 1000 );
175 createMenu( FileNewId, fileMenu, 0 );
176 createMenu( FileOpenId, fileMenu, 0 );
177 createMenu( FileLoadId, fileMenu, 0 ); //SRN: BugID IPAL9021, add a menu item "Load"
178 createMenu( FileCloseId, fileMenu, 0 );
179 createMenu( separator(), fileMenu, -1, 0 );
180 createMenu( FileSaveId, fileMenu, 0 );
181 createMenu( FileSaveAsId, fileMenu, 0 );
182 createMenu( separator(), fileMenu, -1, 0 );
184 createMenu( separator(), fileMenu );
185 createMenu( FileExitId, fileMenu );
187 createMenu( EditCopyId, editMenu );
188 createMenu( EditPasteId, editMenu );
189 createMenu( separator(), editMenu );
191 createMenu( ViewWindowsId, viewMenu );
192 createMenu( ViewStatusBarId, viewMenu );
193 createMenu( separator(), viewMenu );
195 createMenu( HelpAboutId, helpMenu );
196 createMenu( separator(), helpMenu );
200 int stdTBar = createTool( tr( "INF_DESK_TOOLBAR_STANDARD" ) );
204 createTool( FileNewId, stdTBar );
205 createTool( FileOpenId, stdTBar );
206 createTool( FileSaveId, stdTBar );
207 createTool( FileCloseId, stdTBar );
208 createTool( separator(), stdTBar );
209 createTool( EditCopyId, stdTBar );
210 createTool( EditPasteId, stdTBar );
213 /*!Opens new application*/
214 void STD_Application::onNewDoc()
216 if ( !activeStudy() )
219 activeStudy()->createDocument();
220 updateDesktopTitle();
221 updateCommandsStatus();
225 SUIT_Application* aApp = startApplication( 0, 0 );
226 if ( aApp->inherits( "STD_Application" ) )
227 ((STD_Application*)aApp)->onNewDoc();
229 aApp->createEmptyStudy();
230 aApp->activeStudy()->createDocument();
235 /*!Put file name from file dialog to onOpenDoc(const QString&) function*/
236 void STD_Application::onOpenDoc()
238 // It is preferrable to use OS-specific file dialog box here !!!
239 QString aName = getFileName( true, QString::null, getFileFilter(), QString::null, 0 );
240 if ( aName.isNull() )
246 /*! \retval true, if document was opened successful, else false.*/
247 bool STD_Application::onOpenDoc( const QString& aName )
250 if ( !activeStudy() )
252 // if no study - open in current desktop
253 res = useFile( aName );
257 // if study exists - open in new desktop. Check: is the same file is opened?
258 SUIT_Session* aSession = SUIT_Session::session();
259 QPtrList<SUIT_Application> aAppList = aSession->applications();
260 bool isAlreadyOpen = false;
261 SUIT_Application* aApp = 0;
262 for ( QPtrListIterator<SUIT_Application> it( aAppList ); it.current() && !isAlreadyOpen; ++it )
265 if ( aApp->activeStudy()->studyName() == aName )
266 isAlreadyOpen = true;
268 if ( !isAlreadyOpen )
270 aApp = startApplication( 0, 0 );
272 res = aApp->useFile( aName );
275 aApp->desktop()->setActiveWindow();
280 /*! \retval true, if document was loaded successful, else false.*/
281 bool STD_Application::onLoadDoc( const QString& aName )
284 if ( !activeStudy() )
286 // if no study - load in current desktop
287 res = useStudy( aName );
291 // if study exists - load in new desktop. Check: is the same file is loaded?
292 SUIT_Session* aSession = SUIT_Session::session();
293 QPtrList<SUIT_Application> aAppList = aSession->applications();
294 bool isAlreadyOpen = false;
295 SUIT_Application* aApp = 0;
296 for ( QPtrListIterator<SUIT_Application> it( aAppList ); it.current() && !isAlreadyOpen; ++it )
299 if ( aApp->activeStudy()->studyName() == aName )
300 isAlreadyOpen = true;
302 if ( !isAlreadyOpen )
304 aApp = startApplication( 0, 0 );
306 res = aApp->useStudy( aName );
309 aApp->desktop()->setActiveWindow();
314 /*!Virtual function. Not implemented here.*/
315 void STD_Application::beforeCloseDoc( SUIT_Study* )
319 /*!Virtual function. Not implemented here.*/
320 void STD_Application::afterCloseDoc()
324 /*!Close document, if it's possible.*/
325 void STD_Application::onCloseDoc()
327 if ( !isPossibleToClose() )
330 SUIT_Study* study = activeStudy();
332 beforeCloseDoc( study );
335 study->closeDocument();
343 QPtrList<SUIT_Application> apps = SUIT_Session::session()->applications();
344 for ( unsigned i = 0; i < apps.count(); i++ )
345 aNbStudies += apps.at( i )->getNbStudies();
347 // STV: aNbStudies - number of currently existing studies (exclude currently closed)
348 // STV: aNbStudies should be compared with 0.
353 updateDesktopTitle();
354 updateCommandsStatus();
363 /*!Check the application on closing.
364 * \retval true if possible, else false
366 bool STD_Application::isPossibleToClose()
370 activeStudy()->abortAllOperations();
371 if ( activeStudy()->isModified() )
373 QString sName = activeStudy()->studyName().stripWhiteSpace();
374 QString msg = sName.isEmpty() ? tr( "INF_DOC_MODIFIED" ) : tr ( "INF_DOCUMENT_MODIFIED" ).arg( sName );
376 //SRN: BugID: IPAL9021: Begin
377 STD_CloseDlg dlg(desktop());
381 if ( activeStudy()->isSaved() )
383 else if ( !onSaveAsDoc() )
394 //SRN: BugID: IPAL9021: End
400 /*!Save document if all ok, else error message.*/
401 void STD_Application::onSaveDoc()
403 if ( !activeStudy() )
407 if ( activeStudy()->isSaved() )
409 isOk = activeStudy()->saveDocument();
411 SUIT_MessageBox::error1( desktop(), tr( "TIT_FILE_SAVEAS" ),
412 tr( "MSG_CANT_SAVE" ).arg( activeStudy()->studyName() ), tr( "BUT_OK" ) );
416 updateCommandsStatus();
421 /*! \retval TRUE, if doument saved successful, else FALSE.*/
422 bool STD_Application::onSaveAsDoc()
424 SUIT_Study* study = activeStudy();
428 QString aName = getFileName( false, study->studyName(), getFileFilter(), QString::null, 0 );
430 if ( aName.isNull() )
432 bool isOk = study->saveDocumentAs( aName );
434 updateDesktopTitle();
435 updateCommandsStatus();
440 /*!Closing session.*/
441 void STD_Application::onExit()
443 SUIT_Session::session()->closeSession();
446 /*!Virtual slot. Not implemented here.*/
447 void STD_Application::onCopy()
451 /*!Virtual slot. Not implemented here.*/
452 void STD_Application::onPaste()
456 /*!Sets \a theEnable for menu manager and for tool manager.*/
457 void STD_Application::setEditEnabled( bool theEnable )
459 myEditEnabled = theEnable;
461 QtxActionMenuMgr* mMgr = desktop()->menuMgr();
462 QtxActionToolMgr* tMgr = desktop()->toolMgr();
464 for ( int i = EditCopyId; i <= EditPasteId; i++ )
466 mMgr->setShown( i, myEditEnabled );
467 tMgr->setShown( i, myEditEnabled );
471 /*!\retval true, if document opened successful, else false.*/
472 bool STD_Application::useFile(const QString& theFileName)
474 bool res = SUIT_Application::useFile(theFileName);
475 updateDesktopTitle();
476 updateCommandsStatus();
480 /*!Update desktop title.*/
481 void STD_Application::updateDesktopTitle()
483 QString aTitle = applicationName();
484 QString aVer = applicationVersion();
485 if ( !aVer.isEmpty() )
486 aTitle += QString( " " ) + aVer;
490 QString sName = SUIT_Tools::file( activeStudy()->studyName().stripWhiteSpace(), false );
491 if ( !sName.isEmpty() )
492 aTitle += QString( " - [%1]" ).arg( sName );
495 desktop()->setCaption( aTitle );
498 /*!Update commands status.*/
499 void STD_Application::updateCommandsStatus()
501 bool aHasStudy = activeStudy() != 0;
502 bool aIsNeedToSave = false;
504 aIsNeedToSave = !activeStudy()->isSaved() || activeStudy()->isModified();
506 if ( action( FileSaveId ) )
507 action( FileSaveId )->setEnabled( aIsNeedToSave );
508 if ( action( FileSaveAsId ) )
509 action( FileSaveAsId )->setEnabled( aHasStudy );
510 if ( action( FileCloseId ) )
511 action( FileCloseId )->setEnabled( aHasStudy );
512 if ( action( NewWindowId ) )
513 action( NewWindowId )->setEnabled( aHasStudy );
516 /*!\retval SUIT_ViewManager by viewer manager type name.*/
517 SUIT_ViewManager* STD_Application::viewManager( const QString& vmType ) const
519 SUIT_ViewManager* vm = 0;
520 for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current() && !vm; ++it )
522 if ( it.current()->getType() == vmType )
528 /*! \param vmType - input view manager type name
529 * \param lst - output list of view managers with types \a vmType.
531 void STD_Application::viewManagers( const QString& vmType, ViewManagerList& lst ) const
533 for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current(); ++it )
534 if ( it.current()->getType() == vmType )
535 lst.append( it.current() );
538 /*!\param lst - output list of all view managers.*/
539 void STD_Application::viewManagers( ViewManagerList& lst ) const
541 for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current(); ++it )
542 lst.append( it.current() );
545 /*!\retval ViewManagerList - const list of all view managers.*/
546 ViewManagerList STD_Application::viewManagers() const
553 /*!\retval SUIT_ViewManager - return pointer to active view manager.*/
554 SUIT_ViewManager* STD_Application::activeViewManager() const
556 return myActiveViewMgr;
559 /*!Add view manager to view managers list, if it not already there.*/
560 void STD_Application::addViewManager( SUIT_ViewManager* vm )
565 if ( !containsViewManager( vm ) )
567 myViewMgrs.append( vm );
568 connect( vm, SIGNAL( activated( SUIT_ViewManager* ) ),
569 this, SLOT( onViewManagerActivated( SUIT_ViewManager* ) ) );
570 vm->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
572 emit viewManagerAdded( vm );
575 if ( !activeViewManager() && myViewMgrs.count() == 1 )
576 setActiveViewManager( vm );
580 /*!Remove view manager from view managers list.*/
581 void STD_Application::removeViewManager( SUIT_ViewManager* vm )
588 emit viewManagerRemoved( vm );
590 vm->disconnectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
592 myViewMgrs.removeRef( vm );
594 if ( myActiveViewMgr == vm )
598 /*!Remove all view managers from view managers list.*/
599 void STD_Application::clearViewManagers()
604 for ( QPtrListIterator<SUIT_ViewManager> it( lst ); it.current(); ++it )
605 removeViewManager( it.current() );
608 /*!\retval TRUE, if view manager \a vm, already in view manager list (\a myViewMgrs).*/
609 bool STD_Application::containsViewManager( SUIT_ViewManager* vm ) const
611 return myViewMgrs.contains( vm ) > 0;
614 /*!Private slot, sets active manager to \vm, if \vm in view managers list.*/
615 void STD_Application::onViewManagerActivated( SUIT_ViewManager* vm )
617 setActiveViewManager( vm );
620 /*!Sets status bar show, if \on = true, else status bar hide.*/
621 void STD_Application::onViewStatusBar( bool on )
624 desktop()->statusBar()->show();
626 desktop()->statusBar()->hide();
629 /*!Call SUIT_MessageBox::info1(...) with about information.*/
630 void STD_Application::onHelpAbout()
632 SUIT_MessageBox::info1( desktop(), tr( "About" ), tr( "ABOUT_INFO" ), "&OK" );
635 /*!Create empty study. \n
636 * Create new view manager and adding it to view managers list.
638 void STD_Application::createEmptyStudy()
640 SUIT_Application::createEmptyStudy();
642 SUIT_ViewManager* vm = new SUIT_ViewManager( activeStudy(), desktop(), new SUIT_ViewModel() );
644 addViewManager( vm );
647 /*!Sets active manager to \vm, if \vm in view managers list.*/
648 void STD_Application::setActiveViewManager( SUIT_ViewManager* vm )
650 if ( !containsViewManager( vm ) )
653 myActiveViewMgr = vm;
654 emit viewManagerActivated( vm );
658 void STD_Application::onConnectPopupRequest( SUIT_PopupClient* client, QContextMenuEvent* e )
660 QtxPopupMenu* popup = new QtxPopupMenu();
661 // fill popup by own items
663 contextMenuPopup( client->popupClientType(), popup, title );
664 popup->setTitleText( title );
666 popup->insertSeparator();
667 // add items from popup client
668 client->contextMenuPopup( popup );
670 SUIT_Tools::simplifySeparators( popup );
672 if ( popup->count() )
673 popup->exec( e->globalPos() );
677 /*!\retval QString - return file name from dialog.*/
678 QString STD_Application::getFileName( bool open, const QString& initial, const QString& filters,
679 const QString& caption, QWidget* parent )
685 return QFileDialog::getOpenFileName( initial, filters, parent, 0, caption );
691 QString anOldPath = initial;
696 // It is preferrable to use OS-specific file dialog box here !!!
697 aName = QFileDialog::getSaveFileName( anOldPath, filters, parent,
698 0, caption, &aUsedFilter);
700 if ( aName.isNull() )
704 if ( !getFileFilter().isNull() ) // check extension and add if it is necessary
706 int aStart = aUsedFilter.find( '*' );
707 int aEnd = aUsedFilter.find( ')', aStart + 1 );
708 QString aExt = aUsedFilter.mid( aStart + 1, aEnd - aStart - 1 );
709 if ( aExt.contains( '*' ) == 0 ) // if it is not *.*
711 // Check that there is an extension at the end of the name
712 QString aNameTail = aName.right( aExt.length() );
713 if ( aNameTail != aExt )
717 if ( QFileInfo( aName ).exists() )
719 int aAnswer = SUIT_MessageBox::warn3( desktop(), tr( "TIT_FILE_SAVEAS" ),
720 tr( "MSG_FILE_EXISTS" ).arg( aName ),
721 tr( "BUT_YES" ), tr( "BUT_NO" ), tr( "BUT_CANCEL" ), 1, 2, 3, 1 );
722 if ( aAnswer == 3 ) { // cancelled
723 aName = QString::null;
726 else if ( aAnswer == 2 ) // not save to this file
727 anOldPath = aName; // not to return to the same initial dir at each "while" step
728 else // overwrite the existing file
739 /*!\retval QString - return directory name from dialog.*/
740 QString STD_Application::getDirectory( const QString& initial, const QString& caption, QWidget* parent )
744 return QFileDialog::getExistingDirectory( initial, parent, 0, caption, true );