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();
78 void STD_Application::createActions()
80 SUIT_Desktop* desk = desktop();
81 SUIT_ResourceMgr* resMgr = resourceMgr();
82 if ( !desk || !resMgr )
87 createAction( FileNewId, tr( "TOT_DESK_FILE_NEW" ),
88 resMgr->loadPixmap( "STD", tr( "ICON_FILE_NEW" ) ),
89 tr( "MEN_DESK_FILE_NEW" ), tr( "PRP_DESK_FILE_NEW" ),
90 CTRL+Key_N, desk, false, this, SLOT( onNewDoc() ) );
92 createAction( FileOpenId, tr( "TOT_DESK_FILE_OPEN" ),
93 resMgr->loadPixmap( "STD", tr( "ICON_FILE_OPEN" ) ),
94 tr( "MEN_DESK_FILE_OPEN" ), tr( "PRP_DESK_FILE_OPEN" ),
95 CTRL+Key_O, desk, false, this, SLOT( onOpenDoc() ) );
97 createAction( FileCloseId, tr( "TOT_DESK_FILE_CLOSE" ),
98 resMgr->loadPixmap( "STD", tr( "ICON_FILE_CLOSE" ) ),
99 tr( "MEN_DESK_FILE_CLOSE" ), tr( "PRP_DESK_FILE_CLOSE" ),
100 CTRL+Key_W, desk, false, this, SLOT( onCloseDoc() ) );
102 createAction( FileExitId, tr( "TOT_DESK_FILE_EXIT" ), QIconSet(),
103 tr( "MEN_DESK_FILE_EXIT" ), tr( "PRP_DESK_FILE_EXIT" ),
104 CTRL+Key_Q, desk, false, this, SLOT( onExit() ) );
106 createAction( FileSaveId, tr( "TOT_DESK_FILE_SAVE" ),
107 resMgr->loadPixmap( "STD", tr( "ICON_FILE_SAVE" ) ),
108 tr( "MEN_DESK_FILE_SAVE" ), tr( "PRP_DESK_FILE_SAVE" ),
109 CTRL+Key_S, desk, false, this, SLOT( onSaveDoc() ) );
111 createAction( FileSaveAsId, tr( "TOT_DESK_FILE_SAVEAS" ), QIconSet(),
112 tr( "MEN_DESK_FILE_SAVEAS" ), tr( "PRP_DESK_FILE_SAVEAS" ),
113 0, desk, false, this, SLOT( onSaveAsDoc() ) );
115 createAction( EditCutId, tr( "TOT_DESK_EDIT_CUT" ),
116 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_CUT" ) ),
117 tr( "MEN_DESK_EDIT_CUT" ), tr( "PRP_DESK_EDIT_CUT" ),
118 CTRL+Key_X, desk, false, this, SLOT( onCut() ) );
120 createAction( EditCopyId, tr( "TOT_DESK_EDIT_COPY" ),
121 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_COPY" ) ),
122 tr( "MEN_DESK_EDIT_COPY" ), tr( "PRP_DESK_EDIT_COPY" ),
123 CTRL+Key_C, desk, false, this, SLOT( onCopy() ) );
125 createAction( EditPasteId, tr( "TOT_DESK_EDIT_PASTE" ),
126 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_PASTE" ) ),
127 tr( "MEN_DESK_EDIT_PASTE" ), tr( "PRP_DESK_EDIT_PASTE" ),
128 CTRL+Key_V, desk, false, this, SLOT( onPaste() ) );
130 QAction* a = createAction( ViewStatusBarId, tr( "TOT_DESK_VIEW_STATUSBAR" ),
131 QIconSet(), tr( "MEN_DESK_VIEW_STATUSBAR" ),
132 tr( "PRP_DESK_VIEW_STATUSBAR" ), 0, desk, true );
133 a->setOn( desk->statusBar()->isVisibleTo( desk ) );
134 connect( a, SIGNAL( toggled( bool ) ), this, SLOT( onViewStatusBar( bool ) ) );
136 createAction( NewWindowId, tr( "TOT_DESK_NEWWINDOW" ), QIconSet(),
137 tr( "MEN_DESK_NEWWINDOW" ), tr( "PRP_DESK_NEWWINDOW" ), 0, desk );
139 createAction( HelpAboutId, tr( "TOT_DESK_HELP_ABOUT" ), QIconSet(),
140 tr( "MEN_DESK_HELP_ABOUT" ), tr( "PRP_DESK_HELP_ABOUT" ),
141 0, desk, false, this, SLOT( onHelpAbout() ) );
143 QtxDockAction* da = new QtxDockAction( tr( "TOT_DOCK_WINDOWS" ), tr( "MEN_DOCK_WINDOWS" ), desk );
144 registerAction( ViewWindowsId, da );
145 da->setAutoPlace( false );
149 int fileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1, -1, 0 );
150 int editMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 10 );
151 int viewMenu = createMenu( tr( "MEN_DESK_VIEW" ), -1, -1, 20 );
152 int helpMenu = createMenu( tr( "MEN_DESK_HELP" ), -1, -1, 1000 );
156 createMenu( FileNewId, fileMenu, 0 );
157 createMenu( FileOpenId, fileMenu, 0 );
158 createMenu( FileCloseId, fileMenu, 0 );
159 createMenu( separator(), fileMenu, -1, 0 );
160 createMenu( FileSaveId, fileMenu, 0 );
161 createMenu( FileSaveAsId, fileMenu, 0 );
162 createMenu( separator(), fileMenu, -1, 0 );
164 createMenu( separator(), fileMenu );
165 createMenu( FileExitId, fileMenu );
167 createMenu( EditCutId, editMenu );
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( EditCutId, stdTBar );
191 createTool( EditCopyId, stdTBar );
192 createTool( EditPasteId, stdTBar );
196 Opens new application
198 void STD_Application::onNewDoc()
200 if ( !activeStudy() )
203 activeStudy()->createDocument();
204 updateDesktopTitle();
205 updateCommandsStatus();
209 SUIT_Application* aApp = startApplication( 0, 0 );
210 if ( aApp->inherits( "STD_Application" ) )
211 ((STD_Application*)aApp)->onNewDoc();
213 aApp->createEmptyStudy();
214 aApp->activeStudy()->createDocument();
219 void STD_Application::onOpenDoc()
221 // It is preferrable to use OS-specific file dialog box here !!!
222 QString aName = QFileDialog::getOpenFileName( QString::null, getFileFilter(), desktop() );
223 if ( aName.isNull() )
226 if ( !activeStudy() )
228 // if no study - open in current desktop
230 activeStudy()->openDocument( aName );
231 updateDesktopTitle();
232 updateCommandsStatus();
236 // if study exists - open in new desktop. Check: is the same file is opened?
237 SUIT_Session* aSession = SUIT_Session::session();
238 QPtrList<SUIT_Application> aAppList = aSession->applications();
239 bool isAlreadyOpen = false;
240 SUIT_Application* aApp = 0;
241 for ( QPtrListIterator<SUIT_Application> it( aAppList ); it.current() && !isAlreadyOpen; ++it )
244 if ( aApp->activeStudy()->studyName() == aName )
245 isAlreadyOpen = true;
247 if ( !isAlreadyOpen )
249 aApp = startApplication( 0, 0 );
251 aApp->useFile( aName );
254 aApp->desktop()->setActiveWindow();
258 void STD_Application::beforeCloseDoc( SUIT_Study* )
262 void STD_Application::afterCloseDoc()
266 void STD_Application::onCloseDoc()
268 if ( !isPossibleToClose() )
271 SUIT_Study* study = activeStudy();
273 beforeCloseDoc( study );
275 QPtrListIterator<SUIT_ViewManager> it( myViewMgrs );
277 SUIT_ViewManager* vm = 0;
278 while( ( vm = it.current()) != 0 )
285 study->closeDocument();
291 QPtrList<SUIT_Application> apps = SUIT_Session::session()->applications();
292 for ( unsigned i = 0; i < apps.count(); i++ )
293 aNbStudies += apps.at( i )->getNbStudies();
295 // STV: aNbStudies - number of currently existing studies (exclude currently closed)
296 // STV: aNbStudies should be compared with 0.
301 updateDesktopTitle();
302 updateCommandsStatus();
309 void STD_Application::closeApplication()
312 emit applicationClosed( this );
315 bool STD_Application::isPossibleToClose()
319 activeStudy()->abortAllOperations();
320 if ( activeStudy()->isModified() )
322 int aAnswer = SUIT_MessageBox::warn3( desktop(), tr( "TOT_DESK_FILE_CLOSE" ),
323 tr( "Document \"%1\" has been modified.\nDo you want to save changes?").arg( activeStudy()->studyName() ),
324 tr( "BUT_YES" ), tr( "BUT_NO" ), tr( "BUT_CANCEL" ), 1, 2, 3, 1 );
328 if ( !activeStudy()->isSaved() )
329 if ( !onSaveAsDoc() )
345 void STD_Application::onSaveDoc()
347 if ( !activeStudy() )
350 if ( activeStudy()->isSaved() )
352 activeStudy()->saveDocument();
353 updateCommandsStatus();
359 bool STD_Application::onSaveAsDoc()
361 SUIT_Study* study = activeStudy();
365 QString aName, aUsedFilter, anOldPath = study->studyName();
366 bool isSelected = false;
369 // It is preferrable to use OS-specific file dialog box here !!!
370 aName = QFileDialog::getSaveFileName( anOldPath, getFileFilter(), desktop(),
371 0, QString::null, &aUsedFilter);
373 if ( aName.isNull() )
376 if ( !getFileFilter().isNull() )
377 { // check exstension and add if it is necessary
378 int aStart = aUsedFilter.find('*');
379 int aEnd = aUsedFilter.find(')', aStart + 1);
380 QString aExt = aUsedFilter.mid(aStart + 1, aEnd - aStart - 1);
381 if (aExt.contains('*') == 0 ) { // if it is not *.*
382 // Check that there is an extension at the end of the name
383 QString aNameTail = aName.right(aExt.length());
384 if ( aNameTail != aExt )
388 QFile aFile( aName );
389 if ( aFile.exists() )
391 int aAnswer = SUIT_MessageBox::warn2( desktop(), tr("TIT_FILE_SAVEAS"),
392 tr("MSG_FILE_EXISTS").arg(aName),
393 tr("BUT_YES"), tr("BUT_NO"), 1, 2, 2);
397 anOldPath = aName; // Not to return to the same initial dir at each "while" step
403 if ( aName.isNull() )
406 study->saveDocumentAs( aName );
407 updateDesktopTitle();
408 updateCommandsStatus();
412 void STD_Application::onExit()
414 SUIT_Session::session()->closeSession();
417 void STD_Application::onCut()
421 void STD_Application::onCopy()
425 void STD_Application::onPaste()
429 void STD_Application::setEditEnabled( bool theEnable )
431 myEditEnabled = theEnable;
433 QtxActionMenuMgr* mMgr = desktop()->menuMgr();
434 QtxActionToolMgr* tMgr = desktop()->toolMgr();
436 for ( int i = EditCutId; i <= EditPasteId; i++ )
438 mMgr->setShown( i, myEditEnabled );
439 tMgr->setShown( i, myEditEnabled );
443 void STD_Application::useFile(const QString& theFileName)
445 SUIT_Application::useFile(theFileName);
446 updateDesktopTitle();
447 updateCommandsStatus();
450 void STD_Application::updateDesktopTitle()
452 QString aTitle = applicationName();
455 QString sName = SUIT_Tools::file( activeStudy()->studyName().stripWhiteSpace(), false );
456 if ( !sName.isEmpty() )
457 aTitle += QString( " - [%1]" ).arg( sName );
460 desktop()->setCaption( aTitle );
463 void STD_Application::updateCommandsStatus()
465 bool aHasStudy = activeStudy() != 0;
466 bool aIsNeedToSave = false;
468 aIsNeedToSave = !activeStudy()->isSaved() || activeStudy()->isModified();
470 if ( action( FileSaveId ) )
471 action( FileSaveId )->setEnabled( aIsNeedToSave );
472 if ( action( FileSaveAsId ) )
473 action( FileSaveAsId )->setEnabled( aHasStudy );
474 if ( action( FileCloseId ) )
475 action( FileCloseId )->setEnabled( aHasStudy );
476 if ( action( NewWindowId ) )
477 action( NewWindowId )->setEnabled( aHasStudy );
480 SUIT_ViewManager* STD_Application::viewManager( const QString& vmType ) const
482 SUIT_ViewManager* vm = 0;
483 for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current() && !vm; ++it )
485 if ( it.current()->getType() == vmType )
491 void STD_Application::viewManagers( const QString& vmType, ViewManagerList& lst ) const
493 for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current(); ++it )
494 if ( it.current()->getType() == vmType )
495 lst.append( it.current() );
498 void STD_Application::viewManagers( ViewManagerList& lst ) const
500 for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current(); ++it )
501 lst.append( it.current() );
504 ViewManagerList STD_Application::viewManagers() const
511 SUIT_ViewManager* STD_Application::activeViewManager() const
513 return myActiveViewMgr;
516 void STD_Application::addViewManager( SUIT_ViewManager* vm )
521 if ( !containsViewManager( vm ) )
523 myViewMgrs.append( vm );
524 connect( vm, SIGNAL( activated( SUIT_ViewManager* ) ),
525 this, SLOT( onViewManagerActivated( SUIT_ViewManager* ) ) );
526 vm->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
528 emit viewManagerAdded( vm );
531 if ( !activeViewManager() && myViewMgrs.count() == 1 )
532 setActiveViewManager( vm );
536 void STD_Application::removeViewManager( SUIT_ViewManager* vm )
543 emit viewManagerRemoved( vm );
545 vm->disconnectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
547 myViewMgrs.removeRef( vm );
549 if ( myActiveViewMgr == vm )
553 void STD_Application::clearViewManagers()
558 for ( QPtrListIterator<SUIT_ViewManager> it( lst ); it.current(); ++it )
559 removeViewManager( it.current() );
562 bool STD_Application::containsViewManager( SUIT_ViewManager* vm ) const
564 return myViewMgrs.contains( vm ) > 0;
567 void STD_Application::onViewManagerActivated( SUIT_ViewManager* vm )
569 setActiveViewManager( vm );
572 void STD_Application::onViewStatusBar( bool on )
575 desktop()->statusBar()->show();
577 desktop()->statusBar()->hide();
580 void STD_Application::onHelpAbout()
582 SUIT_MessageBox::info1( desktop(), tr( "About" ), tr( "ABOUT_INFO" ), "&OK" );
585 void STD_Application::createEmptyStudy()
587 SUIT_Application::createEmptyStudy();
589 SUIT_ViewManager* vm = new SUIT_ViewManager( activeStudy(), desktop(), new SUIT_ViewModel() );
591 addViewManager( vm );
594 void STD_Application::setActiveViewManager( SUIT_ViewManager* vm )
596 if ( !containsViewManager( vm ) )
599 myActiveViewMgr = vm;
600 emit viewManagerActivated( vm );
603 void STD_Application::onConnectPopupRequest( SUIT_PopupClient* client, QContextMenuEvent* e )
605 QPopupMenu* popup = new QPopupMenu();
606 // fill popup by own items
607 contextMenuPopup( client->popupClientType(), popup );
609 popup->insertSeparator();
610 // add items from popup client
611 client->contextMenuPopup( popup );
613 SUIT_Tools::simplifySeparators( popup );
615 if ( popup->count() )
616 popup->exec( e->globalPos() );