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( EditCutId, tr( "TOT_DESK_EDIT_CUT" ),
122 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_CUT" ) ),
123 tr( "MEN_DESK_EDIT_CUT" ), tr( "PRP_DESK_EDIT_CUT" ),
124 CTRL+Key_X, desk, false, this, SLOT( onCut() ) );
126 createAction( EditCopyId, tr( "TOT_DESK_EDIT_COPY" ),
127 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_COPY" ) ),
128 tr( "MEN_DESK_EDIT_COPY" ), tr( "PRP_DESK_EDIT_COPY" ),
129 CTRL+Key_C, desk, false, this, SLOT( onCopy() ) );
131 createAction( EditPasteId, tr( "TOT_DESK_EDIT_PASTE" ),
132 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_PASTE" ) ),
133 tr( "MEN_DESK_EDIT_PASTE" ), tr( "PRP_DESK_EDIT_PASTE" ),
134 CTRL+Key_V, desk, false, this, SLOT( onPaste() ) );
136 QAction* a = createAction( ViewStatusBarId, tr( "TOT_DESK_VIEW_STATUSBAR" ),
137 QIconSet(), tr( "MEN_DESK_VIEW_STATUSBAR" ),
138 tr( "PRP_DESK_VIEW_STATUSBAR" ), 0, desk, true );
139 a->setOn( desk->statusBar()->isVisibleTo( desk ) );
140 connect( a, SIGNAL( toggled( bool ) ), this, SLOT( onViewStatusBar( bool ) ) );
142 createAction( NewWindowId, tr( "TOT_DESK_NEWWINDOW" ), QIconSet(),
143 tr( "MEN_DESK_NEWWINDOW" ), tr( "PRP_DESK_NEWWINDOW" ), 0, desk );
145 createAction( HelpAboutId, tr( "TOT_DESK_HELP_ABOUT" ), QIconSet(),
146 tr( "MEN_DESK_HELP_ABOUT" ), tr( "PRP_DESK_HELP_ABOUT" ),
147 0, desk, false, this, SLOT( onHelpAbout() ) );
149 QtxDockAction* da = new QtxDockAction( tr( "TOT_DOCK_WINDOWS" ), tr( "MEN_DOCK_WINDOWS" ), desk );
150 registerAction( ViewWindowsId, da );
151 da->setAutoPlace( false );
155 int fileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1, -1, 0 );
156 int editMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 10 );
157 int viewMenu = createMenu( tr( "MEN_DESK_VIEW" ), -1, -1, 10 );
158 int helpMenu = createMenu( tr( "MEN_DESK_HELP" ), -1, -1, 1000 );
162 createMenu( FileNewId, fileMenu, 0 );
163 createMenu( FileOpenId, fileMenu, 0 );
164 createMenu( FileCloseId, fileMenu, 0 );
165 createMenu( separator(), fileMenu, -1, 0 );
166 createMenu( FileSaveId, fileMenu, 0 );
167 createMenu( FileSaveAsId, fileMenu, 0 );
168 createMenu( separator(), fileMenu, -1, 0 );
170 createMenu( separator(), fileMenu );
171 createMenu( FileExitId, fileMenu );
173 createMenu( EditCutId, editMenu );
174 createMenu( EditCopyId, editMenu );
175 createMenu( EditPasteId, editMenu );
176 createMenu( separator(), editMenu );
178 createMenu( ViewWindowsId, viewMenu );
179 createMenu( ViewStatusBarId, viewMenu );
180 createMenu( separator(), viewMenu );
182 createMenu( HelpAboutId, helpMenu );
183 createMenu( separator(), helpMenu );
187 int stdTBar = createTool( tr( "INF_DESK_TOOLBAR_STANDARD" ) );
191 createTool( FileNewId, stdTBar );
192 createTool( FileOpenId, stdTBar );
193 createTool( FileSaveId, stdTBar );
194 createTool( FileCloseId, stdTBar );
195 createTool( separator(), stdTBar );
196 createTool( EditCutId, stdTBar );
197 createTool( EditCopyId, stdTBar );
198 createTool( EditPasteId, stdTBar );
202 Opens new application
204 void STD_Application::onNewDoc()
206 if ( !activeStudy() )
209 activeStudy()->createDocument();
210 updateDesktopTitle();
211 updateCommandsStatus();
215 SUIT_Application* aApp = startApplication( 0, 0 );
216 if ( aApp->inherits( "STD_Application" ) )
217 ((STD_Application*)aApp)->onNewDoc();
219 aApp->createEmptyStudy();
220 aApp->activeStudy()->createDocument();
225 void STD_Application::onOpenDoc()
227 // It is preferrable to use OS-specific file dialog box here !!!
228 QString aName = QFileDialog::getOpenFileName( QString::null, getFileFilter(), desktop() );
229 if ( aName.isNull() )
232 if ( !activeStudy() )
234 // if no study - open in current desktop
236 activeStudy()->openDocument( aName );
237 updateDesktopTitle();
238 updateCommandsStatus();
242 // if study exists - open in new desktop. Check: is the same file is opened?
243 SUIT_Session* aSession = SUIT_Session::session();
244 QPtrList<SUIT_Application> aAppList = aSession->applications();
245 bool isAlreadyOpen = false;
246 SUIT_Application* aApp = 0;
247 for ( QPtrListIterator<SUIT_Application> it( aAppList ); it.current() && !isAlreadyOpen; ++it )
250 if ( aApp->activeStudy()->studyName() == aName )
251 isAlreadyOpen = true;
253 if ( !isAlreadyOpen )
255 aApp = startApplication( 0, 0 );
257 aApp->useFile( aName );
260 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 );
281 QPtrListIterator<SUIT_ViewManager> it( myViewMgrs );
283 SUIT_ViewManager* vm = 0;
284 while( ( vm = it.current()) != 0 )
291 study->closeDocument();
297 QPtrList<SUIT_Application> apps = SUIT_Session::session()->applications();
298 for ( unsigned i = 0; i < apps.count(); i++ )
299 aNbStudies += apps.at( i )->getNbStudies();
301 // STV: aNbStudies - number of currently existing studies (exclude currently closed)
302 // STV: aNbStudies should be compared with 0.
307 updateDesktopTitle();
308 updateCommandsStatus();
317 bool STD_Application::isPossibleToClose()
321 activeStudy()->abortAllOperations();
322 if ( activeStudy()->isModified() )
324 QString sName = activeStudy()->studyName().stripWhiteSpace();
325 QString msg = sName.isEmpty() ? tr( "INF_DOC_MODIFIED" ) : tr ( "INF_DOCUMENT_MODIFIED" ).arg( sName );
326 int aAnswer = SUIT_MessageBox::warn3( desktop(), tr( "TOT_DESK_FILE_CLOSE" ), msg,
327 tr( "BUT_YES" ), tr( "BUT_NO" ), tr( "BUT_CANCEL" ), 1, 2, 3, 1 );
331 if ( !activeStudy()->isSaved() )
332 if ( !onSaveAsDoc() )
348 void STD_Application::onSaveDoc()
350 if ( !activeStudy() )
353 if ( activeStudy()->isSaved() )
355 activeStudy()->saveDocument();
356 updateCommandsStatus();
362 bool STD_Application::onSaveAsDoc()
364 SUIT_Study* study = activeStudy();
368 QString aName, aUsedFilter, anOldPath = study->studyName();
369 bool isSelected = false;
372 // It is preferrable to use OS-specific file dialog box here !!!
373 aName = QFileDialog::getSaveFileName( anOldPath, getFileFilter(), desktop(),
374 0, QString::null, &aUsedFilter);
376 if ( aName.isNull() )
379 if ( !getFileFilter().isNull() )
380 { // check exstension and add if it is necessary
381 int aStart = aUsedFilter.find('*');
382 int aEnd = aUsedFilter.find(')', aStart + 1);
383 QString aExt = aUsedFilter.mid(aStart + 1, aEnd - aStart - 1);
384 if (aExt.contains('*') == 0 ) { // if it is not *.*
385 // Check that there is an extension at the end of the name
386 QString aNameTail = aName.right(aExt.length());
387 if ( aNameTail != aExt )
391 QFile aFile( aName );
392 if ( aFile.exists() )
394 int aAnswer = SUIT_MessageBox::warn2( desktop(), tr("TIT_FILE_SAVEAS"),
395 tr("MSG_FILE_EXISTS").arg(aName),
396 tr("BUT_YES"), tr("BUT_NO"), 1, 2, 2);
400 anOldPath = aName; // Not to return to the same initial dir at each "while" step
406 if ( aName.isNull() )
409 study->saveDocumentAs( aName );
410 updateDesktopTitle();
411 updateCommandsStatus();
415 void STD_Application::onExit()
417 SUIT_Session::session()->closeSession();
420 void STD_Application::onCut()
424 void STD_Application::onCopy()
428 void STD_Application::onPaste()
432 void STD_Application::setEditEnabled( bool theEnable )
434 myEditEnabled = theEnable;
436 QtxActionMenuMgr* mMgr = desktop()->menuMgr();
437 QtxActionToolMgr* tMgr = desktop()->toolMgr();
439 for ( int i = EditCutId; i <= EditPasteId; i++ )
441 mMgr->setShown( i, myEditEnabled );
442 tMgr->setShown( i, myEditEnabled );
446 void STD_Application::useFile(const QString& theFileName)
448 SUIT_Application::useFile(theFileName);
449 updateDesktopTitle();
450 updateCommandsStatus();
453 void STD_Application::updateDesktopTitle()
455 QString aTitle = applicationName();
458 QString sName = SUIT_Tools::file( activeStudy()->studyName().stripWhiteSpace(), false );
459 if ( !sName.isEmpty() )
460 aTitle += QString( " - [%1]" ).arg( sName );
463 desktop()->setCaption( aTitle );
466 void STD_Application::updateCommandsStatus()
468 bool aHasStudy = activeStudy() != 0;
469 bool aIsNeedToSave = false;
471 aIsNeedToSave = !activeStudy()->isSaved() || activeStudy()->isModified();
473 if ( action( FileSaveId ) )
474 action( FileSaveId )->setEnabled( aIsNeedToSave );
475 if ( action( FileSaveAsId ) )
476 action( FileSaveAsId )->setEnabled( aHasStudy );
477 if ( action( FileCloseId ) )
478 action( FileCloseId )->setEnabled( aHasStudy );
479 if ( action( NewWindowId ) )
480 action( NewWindowId )->setEnabled( aHasStudy );
483 SUIT_ViewManager* STD_Application::viewManager( const QString& vmType ) const
485 SUIT_ViewManager* vm = 0;
486 for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current() && !vm; ++it )
488 if ( it.current()->getType() == vmType )
494 void STD_Application::viewManagers( const QString& vmType, ViewManagerList& lst ) const
496 for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current(); ++it )
497 if ( it.current()->getType() == vmType )
498 lst.append( it.current() );
501 void STD_Application::viewManagers( ViewManagerList& lst ) const
503 for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current(); ++it )
504 lst.append( it.current() );
507 ViewManagerList STD_Application::viewManagers() const
514 SUIT_ViewManager* STD_Application::activeViewManager() const
516 return myActiveViewMgr;
519 void STD_Application::addViewManager( SUIT_ViewManager* vm )
524 if ( !containsViewManager( vm ) )
526 myViewMgrs.append( vm );
527 connect( vm, SIGNAL( activated( SUIT_ViewManager* ) ),
528 this, SLOT( onViewManagerActivated( SUIT_ViewManager* ) ) );
529 vm->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
531 emit viewManagerAdded( vm );
534 if ( !activeViewManager() && myViewMgrs.count() == 1 )
535 setActiveViewManager( vm );
539 void STD_Application::removeViewManager( SUIT_ViewManager* vm )
546 emit viewManagerRemoved( vm );
548 vm->disconnectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
550 myViewMgrs.removeRef( vm );
552 if ( myActiveViewMgr == vm )
556 void STD_Application::clearViewManagers()
561 for ( QPtrListIterator<SUIT_ViewManager> it( lst ); it.current(); ++it )
562 removeViewManager( it.current() );
565 bool STD_Application::containsViewManager( SUIT_ViewManager* vm ) const
567 return myViewMgrs.contains( vm ) > 0;
570 void STD_Application::onViewManagerActivated( SUIT_ViewManager* vm )
572 setActiveViewManager( vm );
575 void STD_Application::onViewStatusBar( bool on )
578 desktop()->statusBar()->show();
580 desktop()->statusBar()->hide();
583 void STD_Application::onHelpAbout()
585 SUIT_MessageBox::info1( desktop(), tr( "About" ), tr( "ABOUT_INFO" ), "&OK" );
588 void STD_Application::createEmptyStudy()
590 SUIT_Application::createEmptyStudy();
592 SUIT_ViewManager* vm = new SUIT_ViewManager( activeStudy(), desktop(), new SUIT_ViewModel() );
594 addViewManager( vm );
597 void STD_Application::setActiveViewManager( SUIT_ViewManager* vm )
599 if ( !containsViewManager( vm ) )
602 myActiveViewMgr = vm;
603 emit viewManagerActivated( vm );
606 void STD_Application::onConnectPopupRequest( SUIT_PopupClient* client, QContextMenuEvent* e )
608 QtxPopupMenu* popup = new QtxPopupMenu();
609 // fill popup by own items
611 contextMenuPopup( client->popupClientType(), popup, title );
612 popup->setTitleText( title );
614 popup->insertSeparator();
615 // add items from popup client
616 client->contextMenuPopup( popup );
618 SUIT_Tools::simplifySeparators( popup );
620 if ( popup->count() )
621 popup->exec( e->globalPos() );