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>
19 #include <qpopupmenu.h>
20 #include <qstatusbar.h>
21 #include <qfiledialog.h>
22 #include <qapplication.h>
24 extern "C" STD_EXPORT SUIT_Application* createApplication()
26 return new STD_Application();
29 STD_Application::STD_Application()
31 myEditEnabled( true ),
34 STD_MDIDesktop* desk = new STD_MDIDesktop();
36 connect( desk, SIGNAL( closing( SUIT_Desktop*, QCloseEvent* ) ),
37 this, SLOT( onDesktopClosing( SUIT_Desktop*, QCloseEvent* ) ) );
42 STD_Application::~STD_Application()
46 QString STD_Application::applicationName() const
48 return QString( "StdApplication" );
51 void STD_Application::start()
56 updateCommandsStatus();
57 setEditEnabled( myEditEnabled );
59 SUIT_Application::start();
62 void STD_Application::onDesktopClosing( SUIT_Desktop*, QCloseEvent* e )
64 if ( !isPossibleToClose() )
70 SUIT_Study* study = activeStudy();
73 study->closeDocument();
83 void STD_Application::createActions()
85 SUIT_Desktop* desk = desktop();
86 SUIT_ResourceMgr* resMgr = resourceMgr();
87 if ( !desk || !resMgr )
92 createAction( FileNewId, tr( "TOT_DESK_FILE_NEW" ),
93 resMgr->loadPixmap( "STD", tr( "ICON_FILE_NEW" ) ),
94 tr( "MEN_DESK_FILE_NEW" ), tr( "PRP_DESK_FILE_NEW" ),
95 CTRL+Key_N, desk, false, this, SLOT( onNewDoc() ) );
97 createAction( FileOpenId, tr( "TOT_DESK_FILE_OPEN" ),
98 resMgr->loadPixmap( "STD", tr( "ICON_FILE_OPEN" ) ),
99 tr( "MEN_DESK_FILE_OPEN" ), tr( "PRP_DESK_FILE_OPEN" ),
100 CTRL+Key_O, desk, false, this, SLOT( onOpenDoc() ) );
102 createAction( FileCloseId, tr( "TOT_DESK_FILE_CLOSE" ),
103 resMgr->loadPixmap( "STD", tr( "ICON_FILE_CLOSE" ) ),
104 tr( "MEN_DESK_FILE_CLOSE" ), tr( "PRP_DESK_FILE_CLOSE" ),
105 CTRL+Key_W, desk, false, this, SLOT( onCloseDoc() ) );
107 createAction( FileExitId, tr( "TOT_DESK_FILE_EXIT" ), QIconSet(),
108 tr( "MEN_DESK_FILE_EXIT" ), tr( "PRP_DESK_FILE_EXIT" ),
109 CTRL+Key_Q, desk, false, this, SLOT( onExit() ) );
111 createAction( FileSaveId, tr( "TOT_DESK_FILE_SAVE" ),
112 resMgr->loadPixmap( "STD", tr( "ICON_FILE_SAVE" ) ),
113 tr( "MEN_DESK_FILE_SAVE" ), tr( "PRP_DESK_FILE_SAVE" ),
114 CTRL+Key_S, desk, false, this, SLOT( onSaveDoc() ) );
116 createAction( FileSaveAsId, tr( "TOT_DESK_FILE_SAVEAS" ), QIconSet(),
117 tr( "MEN_DESK_FILE_SAVEAS" ), tr( "PRP_DESK_FILE_SAVEAS" ),
118 0, desk, false, this, SLOT( onSaveAsDoc() ) );
120 createAction( EditCutId, tr( "TOT_DESK_EDIT_CUT" ),
121 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_CUT" ) ),
122 tr( "MEN_DESK_EDIT_CUT" ), tr( "PRP_DESK_EDIT_CUT" ),
123 CTRL+Key_X, desk, false, this, SLOT( onCut() ) );
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 QtxDockAction* da = new QtxDockAction( tr( "TOT_DOCK_WINDOWS" ), tr( "MEN_DOCK_WINDOWS" ), desk );
149 registerAction( ViewWindowsId, da );
150 da->setAutoPlace( false );
154 int fileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1, -1, 0 );
155 int editMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 10 );
156 int viewMenu = createMenu( tr( "MEN_DESK_VIEW" ), -1, -1, 20 );
157 int helpMenu = createMenu( tr( "MEN_DESK_HELP" ), -1, -1, 1000 );
161 createMenu( FileNewId, fileMenu, 0 );
162 createMenu( FileOpenId, fileMenu, 0 );
163 createMenu( FileCloseId, fileMenu, 0 );
164 createMenu( separator(), fileMenu, -1, 0 );
165 createMenu( FileSaveId, fileMenu, 0 );
166 createMenu( FileSaveAsId, fileMenu, 0 );
167 createMenu( separator(), fileMenu, -1, 0 );
169 createMenu( separator(), fileMenu );
170 createMenu( FileExitId, fileMenu );
172 createMenu( EditCutId, editMenu );
173 createMenu( EditCopyId, editMenu );
174 createMenu( EditPasteId, editMenu );
175 createMenu( separator(), editMenu );
177 createMenu( ViewWindowsId, viewMenu );
178 createMenu( ViewStatusBarId, viewMenu );
179 createMenu( separator(), viewMenu );
181 createMenu( HelpAboutId, helpMenu );
182 createMenu( separator(), helpMenu );
186 int stdTBar = createTool( tr( "INF_DESK_TOOLBAR_STANDARD" ) );
190 createTool( FileNewId, stdTBar );
191 createTool( FileOpenId, stdTBar );
192 createTool( FileSaveId, stdTBar );
193 createTool( FileCloseId, stdTBar );
194 createTool( separator(), stdTBar );
195 createTool( EditCutId, stdTBar );
196 createTool( EditCopyId, stdTBar );
197 createTool( EditPasteId, stdTBar );
201 Opens new application
203 void STD_Application::onNewDoc()
205 if ( !activeStudy() )
208 activeStudy()->createDocument();
209 updateDesktopTitle();
210 updateCommandsStatus();
214 SUIT_Application* aApp = startApplication( 0, 0 );
215 if ( aApp->inherits( "STD_Application" ) )
216 ((STD_Application*)aApp)->onNewDoc();
218 aApp->createEmptyStudy();
219 aApp->activeStudy()->createDocument();
224 void STD_Application::onOpenDoc()
226 // It is preferrable to use OS-specific file dialog box here !!!
227 QString aName = QFileDialog::getOpenFileName( QString::null, getFileFilter(), desktop() );
228 if ( aName.isNull() )
231 if ( !activeStudy() )
233 // if no study - open in current desktop
235 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 aApp->useFile( aName );
259 aApp->desktop()->setActiveWindow();
263 void STD_Application::beforeCloseDoc( SUIT_Study* )
267 void STD_Application::afterCloseDoc()
271 void STD_Application::onCloseDoc()
273 if ( !isPossibleToClose() )
276 SUIT_Study* study = activeStudy();
278 beforeCloseDoc( study );
280 QPtrListIterator<SUIT_ViewManager> it( myViewMgrs );
282 SUIT_ViewManager* vm = 0;
283 while( ( vm = it.current()) != 0 )
290 study->closeDocument();
296 QPtrList<SUIT_Application> apps = SUIT_Session::session()->applications();
297 for ( unsigned i = 0; i < apps.count(); i++ )
298 aNbStudies += apps.at( i )->getNbStudies();
300 // STV: aNbStudies - number of currently existing studies (exclude currently closed)
301 // STV: aNbStudies should be compared with 0.
306 updateDesktopTitle();
307 updateCommandsStatus();
316 bool STD_Application::isPossibleToClose()
320 activeStudy()->abortAllOperations();
321 if ( activeStudy()->isModified() )
323 int aAnswer = SUIT_MessageBox::warn3( desktop(), tr( "TOT_DESK_FILE_CLOSE" ),
324 tr( "Document \"%1\" has been modified.\nDo you want to save changes?").arg( activeStudy()->studyName() ),
325 tr( "BUT_YES" ), tr( "BUT_NO" ), tr( "BUT_CANCEL" ), 1, 2, 3, 1 );
329 if ( !activeStudy()->isSaved() )
330 if ( !onSaveAsDoc() )
346 void STD_Application::onSaveDoc()
348 if ( !activeStudy() )
351 if ( activeStudy()->isSaved() )
353 activeStudy()->saveDocument();
354 updateCommandsStatus();
360 bool STD_Application::onSaveAsDoc()
362 SUIT_Study* study = activeStudy();
366 QString aName, aUsedFilter, anOldPath = study->studyName();
367 bool isSelected = false;
370 // It is preferrable to use OS-specific file dialog box here !!!
371 aName = QFileDialog::getSaveFileName( anOldPath, getFileFilter(), desktop(),
372 0, QString::null, &aUsedFilter);
374 if ( aName.isNull() )
377 if ( !getFileFilter().isNull() )
378 { // check exstension and add if it is necessary
379 int aStart = aUsedFilter.find('*');
380 int aEnd = aUsedFilter.find(')', aStart + 1);
381 QString aExt = aUsedFilter.mid(aStart + 1, aEnd - aStart - 1);
382 if (aExt.contains('*') == 0 ) { // if it is not *.*
383 // Check that there is an extension at the end of the name
384 QString aNameTail = aName.right(aExt.length());
385 if ( aNameTail != aExt )
389 QFile aFile( aName );
390 if ( aFile.exists() )
392 int aAnswer = SUIT_MessageBox::warn2( desktop(), tr("TIT_FILE_SAVEAS"),
393 tr("MSG_FILE_EXISTS").arg(aName),
394 tr("BUT_YES"), tr("BUT_NO"), 1, 2, 2);
398 anOldPath = aName; // Not to return to the same initial dir at each "while" step
404 if ( aName.isNull() )
407 study->saveDocumentAs( aName );
408 updateDesktopTitle();
409 updateCommandsStatus();
413 void STD_Application::onExit()
415 SUIT_Session::session()->closeSession();
418 void STD_Application::onCut()
422 void STD_Application::onCopy()
426 void STD_Application::onPaste()
430 void STD_Application::setEditEnabled( bool theEnable )
432 myEditEnabled = theEnable;
434 QtxActionMenuMgr* mMgr = desktop()->menuMgr();
435 QtxActionToolMgr* tMgr = desktop()->toolMgr();
437 for ( int i = EditCutId; i <= EditPasteId; i++ )
439 mMgr->setShown( i, myEditEnabled );
440 tMgr->setShown( i, myEditEnabled );
444 void STD_Application::useFile(const QString& theFileName)
446 SUIT_Application::useFile(theFileName);
447 updateDesktopTitle();
448 updateCommandsStatus();
451 void STD_Application::updateDesktopTitle()
453 QString aTitle = applicationName();
456 QString sName = SUIT_Tools::file( activeStudy()->studyName().stripWhiteSpace(), false );
457 if ( !sName.isEmpty() )
458 aTitle += QString( " - [%1]" ).arg( sName );
461 desktop()->setCaption( aTitle );
464 void STD_Application::updateCommandsStatus()
466 bool aHasStudy = activeStudy() != 0;
467 bool aIsNeedToSave = false;
469 aIsNeedToSave = !activeStudy()->isSaved() || activeStudy()->isModified();
471 if ( action( FileSaveId ) )
472 action( FileSaveId )->setEnabled( aIsNeedToSave );
473 if ( action( FileSaveAsId ) )
474 action( FileSaveAsId )->setEnabled( aHasStudy );
475 if ( action( FileCloseId ) )
476 action( FileCloseId )->setEnabled( aHasStudy );
477 if ( action( NewWindowId ) )
478 action( NewWindowId )->setEnabled( aHasStudy );
481 SUIT_ViewManager* STD_Application::viewManager( const QString& vmType ) const
483 SUIT_ViewManager* vm = 0;
484 for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current() && !vm; ++it )
486 if ( it.current()->getType() == vmType )
492 void STD_Application::viewManagers( const QString& vmType, ViewManagerList& lst ) const
494 for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current(); ++it )
495 if ( it.current()->getType() == vmType )
496 lst.append( it.current() );
499 void STD_Application::viewManagers( ViewManagerList& lst ) const
501 for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current(); ++it )
502 lst.append( it.current() );
505 ViewManagerList STD_Application::viewManagers() const
512 SUIT_ViewManager* STD_Application::activeViewManager() const
514 return myActiveViewMgr;
517 void STD_Application::addViewManager( SUIT_ViewManager* vm )
522 if ( !containsViewManager( vm ) )
524 myViewMgrs.append( vm );
525 connect( vm, SIGNAL( activated( SUIT_ViewManager* ) ),
526 this, SLOT( onViewManagerActivated( SUIT_ViewManager* ) ) );
527 vm->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
529 emit viewManagerAdded( vm );
532 if ( !activeViewManager() && myViewMgrs.count() == 1 )
533 setActiveViewManager( vm );
537 void STD_Application::removeViewManager( SUIT_ViewManager* vm )
544 emit viewManagerRemoved( vm );
546 vm->disconnectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
548 myViewMgrs.removeRef( vm );
550 if ( myActiveViewMgr == vm )
554 void STD_Application::clearViewManagers()
559 for ( QPtrListIterator<SUIT_ViewManager> it( lst ); it.current(); ++it )
560 removeViewManager( it.current() );
563 bool STD_Application::containsViewManager( SUIT_ViewManager* vm ) const
565 return myViewMgrs.contains( vm ) > 0;
568 void STD_Application::onViewManagerActivated( SUIT_ViewManager* vm )
570 setActiveViewManager( vm );
573 void STD_Application::onViewStatusBar( bool on )
576 desktop()->statusBar()->show();
578 desktop()->statusBar()->hide();
581 void STD_Application::onHelpAbout()
583 SUIT_MessageBox::info1( desktop(), tr( "About" ), tr( "ABOUT_INFO" ), "&OK" );
586 void STD_Application::createEmptyStudy()
588 SUIT_Application::createEmptyStudy();
590 SUIT_ViewManager* vm = new SUIT_ViewManager( activeStudy(), desktop(), new SUIT_ViewModel() );
592 addViewManager( vm );
595 void STD_Application::setActiveViewManager( SUIT_ViewManager* vm )
597 if ( !containsViewManager( vm ) )
600 myActiveViewMgr = vm;
601 emit viewManagerActivated( vm );
604 void STD_Application::onConnectPopupRequest( SUIT_PopupClient* client, QContextMenuEvent* e )
606 QPopupMenu* popup = new QPopupMenu();
607 // fill popup by own items
608 contextMenuPopup( client->popupClientType(), popup );
610 popup->insertSeparator();
611 // add items from popup client
612 client->contextMenuPopup( popup );
614 SUIT_Tools::simplifySeparators( popup );
616 if ( popup->count() )
617 popup->exec( e->globalPos() );