1 #include "STD_Application.h"
3 #include "STD_MDIDesktop.h"
5 #include <SUIT_Tools.h>
6 #include <SUIT_Desktop.h>
7 #include <SUIT_Session.h>
8 #include <SUIT_ViewModel.h>
9 #include <SUIT_Operation.h>
10 #include <SUIT_MessageBox.h>
11 #include <SUIT_ResourceMgr.h>
13 #include <QtxDockAction.h>
14 #include <QtxActionMenuMgr.h>
15 #include <QtxActionToolMgr.h>
16 #include <QtxPopupMenu.h>
20 #include <qpopupmenu.h>
21 #include <qstatusbar.h>
22 #include <qfiledialog.h>
23 #include <qapplication.h>
25 extern "C" STD_EXPORT SUIT_Application* createApplication()
27 return new STD_Application();
30 STD_Application::STD_Application()
32 myEditEnabled( true ),
35 STD_MDIDesktop* desk = new STD_MDIDesktop();
37 connect( desk, SIGNAL( closing( SUIT_Desktop*, QCloseEvent* ) ),
38 this, SLOT( onDesktopClosing( SUIT_Desktop*, QCloseEvent* ) ) );
43 STD_Application::~STD_Application()
47 QString STD_Application::applicationName() const
49 return QString( "StdApplication" );
52 void STD_Application::start()
57 updateCommandsStatus();
58 setEditEnabled( myEditEnabled );
60 SUIT_Application::start();
63 void STD_Application::onDesktopClosing( SUIT_Desktop*, QCloseEvent* e )
65 if ( !isPossibleToClose() )
71 SUIT_Study* study = activeStudy();
74 study->closeDocument();
84 void STD_Application::createActions()
86 SUIT_Desktop* desk = desktop();
87 SUIT_ResourceMgr* resMgr = resourceMgr();
88 if ( !desk || !resMgr )
93 createAction( FileNewId, tr( "TOT_DESK_FILE_NEW" ),
94 resMgr->loadPixmap( "STD", tr( "ICON_FILE_NEW" ) ),
95 tr( "MEN_DESK_FILE_NEW" ), tr( "PRP_DESK_FILE_NEW" ),
96 CTRL+Key_N, desk, false, this, SLOT( onNewDoc() ) );
98 createAction( FileOpenId, tr( "TOT_DESK_FILE_OPEN" ),
99 resMgr->loadPixmap( "STD", tr( "ICON_FILE_OPEN" ) ),
100 tr( "MEN_DESK_FILE_OPEN" ), tr( "PRP_DESK_FILE_OPEN" ),
101 CTRL+Key_O, desk, false, this, SLOT( onOpenDoc() ) );
103 createAction( FileCloseId, tr( "TOT_DESK_FILE_CLOSE" ),
104 resMgr->loadPixmap( "STD", tr( "ICON_FILE_CLOSE" ) ),
105 tr( "MEN_DESK_FILE_CLOSE" ), tr( "PRP_DESK_FILE_CLOSE" ),
106 CTRL+Key_W, desk, false, this, SLOT( onCloseDoc() ) );
108 createAction( FileExitId, tr( "TOT_DESK_FILE_EXIT" ), QIconSet(),
109 tr( "MEN_DESK_FILE_EXIT" ), tr( "PRP_DESK_FILE_EXIT" ),
110 CTRL+Key_Q, desk, false, this, SLOT( onExit() ) );
112 createAction( FileSaveId, tr( "TOT_DESK_FILE_SAVE" ),
113 resMgr->loadPixmap( "STD", tr( "ICON_FILE_SAVE" ) ),
114 tr( "MEN_DESK_FILE_SAVE" ), tr( "PRP_DESK_FILE_SAVE" ),
115 CTRL+Key_S, desk, false, this, SLOT( onSaveDoc() ) );
117 createAction( FileSaveAsId, tr( "TOT_DESK_FILE_SAVEAS" ), QIconSet(),
118 tr( "MEN_DESK_FILE_SAVEAS" ), tr( "PRP_DESK_FILE_SAVEAS" ),
119 0, desk, false, this, SLOT( onSaveAsDoc() ) );
121 createAction( EditCopyId, tr( "TOT_DESK_EDIT_COPY" ),
122 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_COPY" ) ),
123 tr( "MEN_DESK_EDIT_COPY" ), tr( "PRP_DESK_EDIT_COPY" ),
124 CTRL+Key_C, desk, false, this, SLOT( onCopy() ) );
126 createAction( EditPasteId, tr( "TOT_DESK_EDIT_PASTE" ),
127 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_PASTE" ) ),
128 tr( "MEN_DESK_EDIT_PASTE" ), tr( "PRP_DESK_EDIT_PASTE" ),
129 CTRL+Key_V, desk, false, this, SLOT( onPaste() ) );
131 QAction* a = createAction( ViewStatusBarId, tr( "TOT_DESK_VIEW_STATUSBAR" ),
132 QIconSet(), tr( "MEN_DESK_VIEW_STATUSBAR" ),
133 tr( "PRP_DESK_VIEW_STATUSBAR" ), 0, desk, true );
134 a->setOn( desk->statusBar()->isVisibleTo( desk ) );
135 connect( a, SIGNAL( toggled( bool ) ), this, SLOT( onViewStatusBar( bool ) ) );
137 createAction( NewWindowId, tr( "TOT_DESK_NEWWINDOW" ), QIconSet(),
138 tr( "MEN_DESK_NEWWINDOW" ), tr( "PRP_DESK_NEWWINDOW" ), 0, desk );
140 createAction( HelpAboutId, tr( "TOT_DESK_HELP_ABOUT" ), QIconSet(),
141 tr( "MEN_DESK_HELP_ABOUT" ), tr( "PRP_DESK_HELP_ABOUT" ),
142 0, desk, false, this, SLOT( onHelpAbout() ) );
144 QtxDockAction* da = new QtxDockAction( tr( "TOT_DOCK_WINDOWS" ), tr( "MEN_DOCK_WINDOWS" ), desk );
145 registerAction( ViewWindowsId, da );
146 da->setAutoPlace( false );
150 int fileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1, -1, 0 );
151 int editMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 10 );
152 int viewMenu = createMenu( tr( "MEN_DESK_VIEW" ), -1, -1, 10 );
153 int helpMenu = createMenu( tr( "MEN_DESK_HELP" ), -1, -1, 1000 );
157 createMenu( FileNewId, fileMenu, 0 );
158 createMenu( FileOpenId, fileMenu, 0 );
159 createMenu( FileCloseId, fileMenu, 0 );
160 createMenu( separator(), fileMenu, -1, 0 );
161 createMenu( FileSaveId, fileMenu, 0 );
162 createMenu( FileSaveAsId, fileMenu, 0 );
163 createMenu( separator(), fileMenu, -1, 0 );
165 createMenu( separator(), fileMenu );
166 createMenu( FileExitId, fileMenu );
168 createMenu( EditCopyId, editMenu );
169 createMenu( EditPasteId, editMenu );
170 createMenu( separator(), editMenu );
172 createMenu( ViewWindowsId, viewMenu );
173 createMenu( ViewStatusBarId, viewMenu );
174 createMenu( separator(), viewMenu );
176 createMenu( HelpAboutId, helpMenu );
177 createMenu( separator(), helpMenu );
181 int stdTBar = createTool( tr( "INF_DESK_TOOLBAR_STANDARD" ) );
185 createTool( FileNewId, stdTBar );
186 createTool( FileOpenId, stdTBar );
187 createTool( FileSaveId, stdTBar );
188 createTool( FileCloseId, stdTBar );
189 createTool( separator(), stdTBar );
190 createTool( EditCopyId, stdTBar );
191 createTool( EditPasteId, stdTBar );
195 Opens new application
197 void STD_Application::onNewDoc()
199 if ( !activeStudy() )
202 activeStudy()->createDocument();
203 updateDesktopTitle();
204 updateCommandsStatus();
208 SUIT_Application* aApp = startApplication( 0, 0 );
209 if ( aApp->inherits( "STD_Application" ) )
210 ((STD_Application*)aApp)->onNewDoc();
212 aApp->createEmptyStudy();
213 aApp->activeStudy()->createDocument();
218 void STD_Application::onOpenDoc()
220 // It is preferrable to use OS-specific file dialog box here !!!
221 QString aName = getFileName( true, QString::null, getFileFilter(), QString::null, 0 );
222 if ( aName.isNull() )
228 bool STD_Application::onOpenDoc( const QString& aName )
231 if ( !activeStudy() )
233 // if no study - open in current desktop
234 res = useFile( aName );
238 // if study exists - open in new desktop. Check: is the same file is opened?
239 SUIT_Session* aSession = SUIT_Session::session();
240 QPtrList<SUIT_Application> aAppList = aSession->applications();
241 bool isAlreadyOpen = false;
242 SUIT_Application* aApp = 0;
243 for ( QPtrListIterator<SUIT_Application> it( aAppList ); it.current() && !isAlreadyOpen; ++it )
246 if ( aApp->activeStudy()->studyName() == aName )
247 isAlreadyOpen = true;
249 if ( !isAlreadyOpen )
251 aApp = startApplication( 0, 0 );
253 res = aApp->useFile( aName );
256 aApp->desktop()->setActiveWindow();
261 bool STD_Application::onLoadDoc( const QString& aName )
264 if ( !activeStudy() )
266 // if no study - load in current desktop
267 res = useStudy( aName );
271 // if study exists - load in new desktop. Check: is the same file is loaded?
272 SUIT_Session* aSession = SUIT_Session::session();
273 QPtrList<SUIT_Application> aAppList = aSession->applications();
274 bool isAlreadyOpen = false;
275 SUIT_Application* aApp = 0;
276 for ( QPtrListIterator<SUIT_Application> it( aAppList ); it.current() && !isAlreadyOpen; ++it )
279 if ( aApp->activeStudy()->studyName() == aName )
280 isAlreadyOpen = true;
282 if ( !isAlreadyOpen )
284 aApp = startApplication( 0, 0 );
286 res = aApp->useStudy( aName );
289 aApp->desktop()->setActiveWindow();
294 void STD_Application::beforeCloseDoc( SUIT_Study* )
298 void STD_Application::afterCloseDoc()
302 void STD_Application::onCloseDoc()
304 if ( !isPossibleToClose() )
307 SUIT_Study* study = activeStudy();
309 beforeCloseDoc( study );
312 study->closeDocument();
320 QPtrList<SUIT_Application> apps = SUIT_Session::session()->applications();
321 for ( unsigned i = 0; i < apps.count(); i++ )
322 aNbStudies += apps.at( i )->getNbStudies();
324 // STV: aNbStudies - number of currently existing studies (exclude currently closed)
325 // STV: aNbStudies should be compared with 0.
330 updateDesktopTitle();
331 updateCommandsStatus();
340 bool STD_Application::isPossibleToClose()
344 activeStudy()->abortAllOperations();
345 if ( activeStudy()->isModified() )
347 QString sName = activeStudy()->studyName().stripWhiteSpace();
348 QString msg = sName.isEmpty() ? tr( "INF_DOC_MODIFIED" ) : tr ( "INF_DOCUMENT_MODIFIED" ).arg( sName );
349 int aAnswer = SUIT_MessageBox::warn3( desktop(), tr( "TOT_DESK_FILE_CLOSE" ), msg,
350 tr( "BUT_YES" ), tr( "BUT_NO" ), tr( "BUT_CANCEL" ), 1, 2, 3, 1 );
354 if ( activeStudy()->isSaved() )
356 else if ( !onSaveAsDoc() )
370 void STD_Application::onSaveDoc()
372 if ( !activeStudy() )
376 if ( activeStudy()->isSaved() )
378 isOk = activeStudy()->saveDocument();
380 SUIT_MessageBox::error1( desktop(), tr( "TIT_FILE_SAVEAS" ),
381 tr( "MSG_CANT_SAVE" ).arg( activeStudy()->studyName() ), tr( "BUT_OK" ) );
385 updateCommandsStatus();
390 bool STD_Application::onSaveAsDoc()
392 SUIT_Study* study = activeStudy();
396 QString aName = getFileName( false, study->studyName(), getFileFilter(), QString::null, 0 );
398 if ( aName.isNull() )
400 bool isOk = study->saveDocumentAs( aName );
402 updateDesktopTitle();
403 updateCommandsStatus();
408 void STD_Application::onExit()
410 SUIT_Session::session()->closeSession();
413 void STD_Application::onCopy()
417 void STD_Application::onPaste()
421 void STD_Application::setEditEnabled( bool theEnable )
423 myEditEnabled = theEnable;
425 QtxActionMenuMgr* mMgr = desktop()->menuMgr();
426 QtxActionToolMgr* tMgr = desktop()->toolMgr();
428 for ( int i = EditCopyId; i <= EditPasteId; i++ )
430 mMgr->setShown( i, myEditEnabled );
431 tMgr->setShown( i, myEditEnabled );
435 bool STD_Application::useFile(const QString& theFileName)
437 bool res = SUIT_Application::useFile(theFileName);
438 updateDesktopTitle();
439 updateCommandsStatus();
443 void STD_Application::updateDesktopTitle()
445 QString aTitle = applicationName();
446 QString aVer = applicationVersion();
447 if ( !aVer.isEmpty() )
448 aTitle += QString( " " ) + aVer;
452 QString sName = SUIT_Tools::file( activeStudy()->studyName().stripWhiteSpace(), false );
453 if ( !sName.isEmpty() )
454 aTitle += QString( " - [%1]" ).arg( sName );
457 desktop()->setCaption( aTitle );
460 void STD_Application::updateCommandsStatus()
462 bool aHasStudy = activeStudy() != 0;
463 bool aIsNeedToSave = false;
465 aIsNeedToSave = !activeStudy()->isSaved() || activeStudy()->isModified();
467 if ( action( FileSaveId ) )
468 action( FileSaveId )->setEnabled( aIsNeedToSave );
469 if ( action( FileSaveAsId ) )
470 action( FileSaveAsId )->setEnabled( aHasStudy );
471 if ( action( FileCloseId ) )
472 action( FileCloseId )->setEnabled( aHasStudy );
473 if ( action( NewWindowId ) )
474 action( NewWindowId )->setEnabled( aHasStudy );
477 SUIT_ViewManager* STD_Application::viewManager( const QString& vmType ) const
479 SUIT_ViewManager* vm = 0;
480 for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current() && !vm; ++it )
482 if ( it.current()->getType() == vmType )
488 void STD_Application::viewManagers( const QString& vmType, ViewManagerList& lst ) const
490 for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current(); ++it )
491 if ( it.current()->getType() == vmType )
492 lst.append( it.current() );
495 void STD_Application::viewManagers( ViewManagerList& lst ) const
497 for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current(); ++it )
498 lst.append( it.current() );
501 ViewManagerList STD_Application::viewManagers() const
508 SUIT_ViewManager* STD_Application::activeViewManager() const
510 return myActiveViewMgr;
513 void STD_Application::addViewManager( SUIT_ViewManager* vm )
518 if ( !containsViewManager( vm ) )
520 myViewMgrs.append( vm );
521 connect( vm, SIGNAL( activated( SUIT_ViewManager* ) ),
522 this, SLOT( onViewManagerActivated( SUIT_ViewManager* ) ) );
523 vm->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
525 emit viewManagerAdded( vm );
528 if ( !activeViewManager() && myViewMgrs.count() == 1 )
529 setActiveViewManager( vm );
533 void STD_Application::removeViewManager( SUIT_ViewManager* vm )
540 emit viewManagerRemoved( vm );
542 vm->disconnectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
544 myViewMgrs.removeRef( vm );
546 if ( myActiveViewMgr == vm )
550 void STD_Application::clearViewManagers()
555 for ( QPtrListIterator<SUIT_ViewManager> it( lst ); it.current(); ++it )
556 removeViewManager( it.current() );
559 bool STD_Application::containsViewManager( SUIT_ViewManager* vm ) const
561 return myViewMgrs.contains( vm ) > 0;
564 void STD_Application::onViewManagerActivated( SUIT_ViewManager* vm )
566 setActiveViewManager( vm );
569 void STD_Application::onViewStatusBar( bool on )
572 desktop()->statusBar()->show();
574 desktop()->statusBar()->hide();
577 void STD_Application::onHelpAbout()
579 SUIT_MessageBox::info1( desktop(), tr( "About" ), tr( "ABOUT_INFO" ), "&OK" );
582 void STD_Application::createEmptyStudy()
584 SUIT_Application::createEmptyStudy();
586 SUIT_ViewManager* vm = new SUIT_ViewManager( activeStudy(), desktop(), new SUIT_ViewModel() );
588 addViewManager( vm );
591 void STD_Application::setActiveViewManager( SUIT_ViewManager* vm )
593 if ( !containsViewManager( vm ) )
596 myActiveViewMgr = vm;
597 emit viewManagerActivated( vm );
600 void STD_Application::onConnectPopupRequest( SUIT_PopupClient* client, QContextMenuEvent* e )
602 QtxPopupMenu* popup = new QtxPopupMenu();
603 // fill popup by own items
605 contextMenuPopup( client->popupClientType(), popup, title );
606 popup->setTitleText( title );
608 popup->insertSeparator();
609 // add items from popup client
610 client->contextMenuPopup( popup );
612 SUIT_Tools::simplifySeparators( popup );
614 if ( popup->count() )
615 popup->exec( e->globalPos() );
619 QString STD_Application::getFileName( bool open, const QString& initial, const QString& filters,
620 const QString& caption, QWidget* parent )
626 return QFileDialog::getOpenFileName( initial, filters, parent, 0, caption );
632 QString anOldPath = initial;
637 // It is preferrable to use OS-specific file dialog box here !!!
638 aName = QFileDialog::getSaveFileName( anOldPath, filters, parent,
639 0, caption, &aUsedFilter);
641 if ( aName.isNull() )
645 if ( !getFileFilter().isNull() ) // check extension and add if it is necessary
647 int aStart = aUsedFilter.find( '*' );
648 int aEnd = aUsedFilter.find( ')', aStart + 1 );
649 QString aExt = aUsedFilter.mid( aStart + 1, aEnd - aStart - 1 );
650 if ( aExt.contains( '*' ) == 0 ) // if it is not *.*
652 // Check that there is an extension at the end of the name
653 QString aNameTail = aName.right( aExt.length() );
654 if ( aNameTail != aExt )
658 if ( QFileInfo( aName ).exists() )
660 int aAnswer = SUIT_MessageBox::warn3( desktop(), tr( "TIT_FILE_SAVEAS" ),
661 tr( "MSG_FILE_EXISTS" ).arg( aName ),
662 tr( "BUT_YES" ), tr( "BUT_NO" ), tr( "BUT_CANCEL" ), 1, 2, 3, 1 );
663 if ( aAnswer == 3 ) { // cancelled
664 aName = QString::null;
667 else if ( aAnswer == 2 ) // not save to this file
668 anOldPath = aName; // not to return to the same initial dir at each "while" step
669 else // overwrite the existing file
680 QString STD_Application::getDirectory( const QString& initial, const QString& caption, QWidget* parent )
684 return QFileDialog::getExistingDirectory( initial, parent, 0, caption, true );