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
235 res = activeStudy()->openDocument( aName );
236 updateDesktopTitle();
237 updateCommandsStatus();
241 // if study exists - open in new desktop. Check: is the same file is opened?
242 SUIT_Session* aSession = SUIT_Session::session();
243 QPtrList<SUIT_Application> aAppList = aSession->applications();
244 bool isAlreadyOpen = false;
245 SUIT_Application* aApp = 0;
246 for ( QPtrListIterator<SUIT_Application> it( aAppList ); it.current() && !isAlreadyOpen; ++it )
249 if ( aApp->activeStudy()->studyName() == aName )
250 isAlreadyOpen = true;
252 if ( !isAlreadyOpen )
254 aApp = startApplication( 0, 0 );
256 res = aApp->useFile( aName );
259 aApp->desktop()->setActiveWindow();
264 void STD_Application::beforeCloseDoc( SUIT_Study* )
268 void STD_Application::afterCloseDoc()
272 void STD_Application::onCloseDoc()
274 if ( !isPossibleToClose() )
277 SUIT_Study* study = activeStudy();
279 beforeCloseDoc( study );
282 study->closeDocument();
290 QPtrList<SUIT_Application> apps = SUIT_Session::session()->applications();
291 for ( unsigned i = 0; i < apps.count(); i++ )
292 aNbStudies += apps.at( i )->getNbStudies();
294 // STV: aNbStudies - number of currently existing studies (exclude currently closed)
295 // STV: aNbStudies should be compared with 0.
300 updateDesktopTitle();
301 updateCommandsStatus();
310 bool STD_Application::isPossibleToClose()
314 activeStudy()->abortAllOperations();
315 if ( activeStudy()->isModified() )
317 QString sName = activeStudy()->studyName().stripWhiteSpace();
318 QString msg = sName.isEmpty() ? tr( "INF_DOC_MODIFIED" ) : tr ( "INF_DOCUMENT_MODIFIED" ).arg( sName );
319 int aAnswer = SUIT_MessageBox::warn3( desktop(), tr( "TOT_DESK_FILE_CLOSE" ), msg,
320 tr( "BUT_YES" ), tr( "BUT_NO" ), tr( "BUT_CANCEL" ), 1, 2, 3, 1 );
324 if ( !activeStudy()->isSaved() )
325 if ( !onSaveAsDoc() )
341 void STD_Application::onSaveDoc()
343 if ( !activeStudy() )
347 if ( activeStudy()->isSaved() )
349 isOk = activeStudy()->saveDocument();
351 SUIT_MessageBox::error1( desktop(), tr( "TIT_FILE_SAVEAS" ),
352 tr( "MSG_CANT_SAVE" ).arg( activeStudy()->studyName() ), tr( "BUT_OK" ) );
356 updateCommandsStatus();
361 bool STD_Application::onSaveAsDoc()
363 SUIT_Study* study = activeStudy();
367 QString aName = getFileName( false, study->studyName(), getFileFilter(), QString::null, 0 );
369 if ( aName.isNull() )
371 bool isOk = study->saveDocumentAs( aName );
373 updateDesktopTitle();
374 updateCommandsStatus();
379 void STD_Application::onExit()
381 SUIT_Session::session()->closeSession();
384 void STD_Application::onCopy()
388 void STD_Application::onPaste()
392 void STD_Application::setEditEnabled( bool theEnable )
394 myEditEnabled = theEnable;
396 QtxActionMenuMgr* mMgr = desktop()->menuMgr();
397 QtxActionToolMgr* tMgr = desktop()->toolMgr();
399 for ( int i = EditCopyId; i <= EditPasteId; i++ )
401 mMgr->setShown( i, myEditEnabled );
402 tMgr->setShown( i, myEditEnabled );
406 bool STD_Application::useFile(const QString& theFileName)
408 bool res = SUIT_Application::useFile(theFileName);
409 updateDesktopTitle();
410 updateCommandsStatus();
414 void STD_Application::updateDesktopTitle()
416 QString aTitle = applicationName();
417 QString aVer = applicationVersion();
418 if ( !aVer.isEmpty() )
419 aTitle += QString( " " ) + aVer;
423 QString sName = SUIT_Tools::file( activeStudy()->studyName().stripWhiteSpace(), false );
424 if ( !sName.isEmpty() )
425 aTitle += QString( " - [%1]" ).arg( sName );
428 desktop()->setCaption( aTitle );
431 void STD_Application::updateCommandsStatus()
433 bool aHasStudy = activeStudy() != 0;
434 bool aIsNeedToSave = false;
436 aIsNeedToSave = !activeStudy()->isSaved() || activeStudy()->isModified();
438 if ( action( FileSaveId ) )
439 action( FileSaveId )->setEnabled( aIsNeedToSave );
440 if ( action( FileSaveAsId ) )
441 action( FileSaveAsId )->setEnabled( aHasStudy );
442 if ( action( FileCloseId ) )
443 action( FileCloseId )->setEnabled( aHasStudy );
444 if ( action( NewWindowId ) )
445 action( NewWindowId )->setEnabled( aHasStudy );
448 SUIT_ViewManager* STD_Application::viewManager( const QString& vmType ) const
450 SUIT_ViewManager* vm = 0;
451 for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current() && !vm; ++it )
453 if ( it.current()->getType() == vmType )
459 void STD_Application::viewManagers( const QString& vmType, ViewManagerList& lst ) const
461 for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current(); ++it )
462 if ( it.current()->getType() == vmType )
463 lst.append( it.current() );
466 void STD_Application::viewManagers( ViewManagerList& lst ) const
468 for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current(); ++it )
469 lst.append( it.current() );
472 ViewManagerList STD_Application::viewManagers() const
479 SUIT_ViewManager* STD_Application::activeViewManager() const
481 return myActiveViewMgr;
484 void STD_Application::addViewManager( SUIT_ViewManager* vm )
489 if ( !containsViewManager( vm ) )
491 myViewMgrs.append( vm );
492 connect( vm, SIGNAL( activated( SUIT_ViewManager* ) ),
493 this, SLOT( onViewManagerActivated( SUIT_ViewManager* ) ) );
494 vm->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
496 emit viewManagerAdded( vm );
499 if ( !activeViewManager() && myViewMgrs.count() == 1 )
500 setActiveViewManager( vm );
504 void STD_Application::removeViewManager( SUIT_ViewManager* vm )
511 emit viewManagerRemoved( vm );
513 vm->disconnectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
515 myViewMgrs.removeRef( vm );
517 if ( myActiveViewMgr == vm )
521 void STD_Application::clearViewManagers()
526 for ( QPtrListIterator<SUIT_ViewManager> it( lst ); it.current(); ++it )
527 removeViewManager( it.current() );
530 bool STD_Application::containsViewManager( SUIT_ViewManager* vm ) const
532 return myViewMgrs.contains( vm ) > 0;
535 void STD_Application::onViewManagerActivated( SUIT_ViewManager* vm )
537 setActiveViewManager( vm );
540 void STD_Application::onViewStatusBar( bool on )
543 desktop()->statusBar()->show();
545 desktop()->statusBar()->hide();
548 void STD_Application::onHelpAbout()
550 SUIT_MessageBox::info1( desktop(), tr( "About" ), tr( "ABOUT_INFO" ), "&OK" );
553 void STD_Application::createEmptyStudy()
555 SUIT_Application::createEmptyStudy();
557 SUIT_ViewManager* vm = new SUIT_ViewManager( activeStudy(), desktop(), new SUIT_ViewModel() );
559 addViewManager( vm );
562 void STD_Application::setActiveViewManager( SUIT_ViewManager* vm )
564 if ( !containsViewManager( vm ) )
567 myActiveViewMgr = vm;
568 emit viewManagerActivated( vm );
571 void STD_Application::onConnectPopupRequest( SUIT_PopupClient* client, QContextMenuEvent* e )
573 QtxPopupMenu* popup = new QtxPopupMenu();
574 // fill popup by own items
576 contextMenuPopup( client->popupClientType(), popup, title );
577 popup->setTitleText( title );
579 popup->insertSeparator();
580 // add items from popup client
581 client->contextMenuPopup( popup );
583 SUIT_Tools::simplifySeparators( popup );
585 if ( popup->count() )
586 popup->exec( e->globalPos() );
590 QString STD_Application::getFileName( bool open, const QString& initial, const QString& filters,
591 const QString& caption, QWidget* parent )
597 return QFileDialog::getOpenFileName( initial, filters, parent, 0, caption );
603 QString anOldPath = initial;
608 // It is preferrable to use OS-specific file dialog box here !!!
609 aName = QFileDialog::getSaveFileName( anOldPath, filters, parent,
610 0, caption, &aUsedFilter);
612 if ( aName.isNull() )
616 if ( !getFileFilter().isNull() ) // check extension and add if it is necessary
618 int aStart = aUsedFilter.find( '*' );
619 int aEnd = aUsedFilter.find( ')', aStart + 1 );
620 QString aExt = aUsedFilter.mid( aStart + 1, aEnd - aStart - 1 );
621 if ( aExt.contains( '*' ) == 0 ) // if it is not *.*
623 // Check that there is an extension at the end of the name
624 QString aNameTail = aName.right( aExt.length() );
625 if ( aNameTail != aExt )
629 if ( QFileInfo( aName ).exists() )
631 int aAnswer = SUIT_MessageBox::warn3( desktop(), tr( "TIT_FILE_SAVEAS" ),
632 tr( "MSG_FILE_EXISTS" ).arg( aName ),
633 tr( "BUT_YES" ), tr( "BUT_NO" ), tr( "BUT_CANCEL" ), 1, 2, 3, 1 );
634 if ( aAnswer == 3 ) { // cancelled
635 aName = QString::null;
638 else if ( aAnswer == 2 ) // not save to this file
639 anOldPath = aName; // not to return to the same initial dir at each "while" step
640 else // overwrite the existing file
651 QString STD_Application::getDirectory( const QString& initial, const QString& caption, QWidget* parent )
655 return QFileDialog::getExistingDirectory( initial, parent, 0, caption, true );