Salome HOME
690b13b52ce9fa7be7a45b180ce636e13fdfec4c
[modules/gui.git] / src / SalomeApp / SalomeApp_Application.cxx
1 // File:      SalomeApp_Application.cxx
2 // Created:   10/22/2004 3:23:45 PM
3 // Author:    Sergey LITONIN
4 // Copyright (C) CEA 2004
5
6 #include "SalomeApp_PyInterp.h" // WARNING! This include must be the first!
7
8 #include "SalomeApp_Application.h"
9
10 #include "SalomeApp_Study.h"
11 #include "SalomeApp_DataModel.h"
12 #include "SalomeApp_DataObject.h"
13 #include "SalomeApp_EventFilter.h"
14
15 #include "SalomeApp_StudyPropertiesDlg.h"
16
17 #include "SalomeApp_CheckFileDlg.h"
18
19 #include "LightApp_Application.h"
20 #include "LightApp_Preferences.h"
21 #include "LightApp_WidgetContainer.h"
22 #include "LightApp_SelectionMgr.h"
23
24 #include "STD_LoadStudiesDlg.h"
25
26 #include <SUIT_Tools.h>
27 #include <SUIT_Session.h>
28
29 #include <QtxMRUAction.h>
30
31 #include <OB_Browser.h>
32 #include <OB_ListItem.h>
33
34 #include <PythonConsole_PyConsole.h>
35
36 #include <SUIT_FileDlg.h>
37 #include <SUIT_MessageBox.h>
38 #include <SUIT_ResourceMgr.h>
39 #include <SUIT_ActionOperation.h>
40
41 #include <Utils_ORB_INIT.hxx>
42 #include <Utils_SINGLETON.hxx>
43 #include <SALOME_ModuleCatalog_impl.hxx>
44 #include <SALOME_LifeCycleCORBA.hxx>
45
46 #include <qmap.h>
47 #include <qaction.h>
48 #include <qcombobox.h>
49 #include <qlistbox.h>
50 #include <qregexp.h>
51
52 #include "SALOMEDS_StudyManager.hxx"
53 #include "SALOMEDS_SObject.hxx"
54
55 #include "SALOME_ListIteratorOfListIO.hxx"
56 #include "SALOME_ListIO.hxx"
57
58 #include "ToolsGUI_CatalogGeneratorDlg.h"
59 #include "ToolsGUI_RegWidget.h"
60
61 /*!Create new instance of SalomeApp_Application.*/
62 extern "C" SALOMEAPP_EXPORT SUIT_Application* createApplication()
63 {
64   return new SalomeApp_Application();
65 }
66
67 /*
68   Class       : SalomeApp_Application
69   Description : Application containing SalomeApp module or LightApp module
70 */
71
72 /*!Constructor.*/
73 SalomeApp_Application::SalomeApp_Application()
74 : LightApp_Application()
75 {
76 }
77
78 /*!Destructor.
79  *\li Destroy event filter.
80  */
81 SalomeApp_Application::~SalomeApp_Application()
82 {
83   // Do not destroy. It's a singleton !
84   //SalomeApp_EventFilter::Destroy();
85 }
86
87 /*!Start application.*/
88 void SalomeApp_Application::start()
89 {
90   LightApp_Application::start();
91
92   SalomeApp_EventFilter::Init();
93 }
94
95 /*!Create actions:*/
96 void SalomeApp_Application::createActions()
97 {
98   LightApp_Application::createActions();
99
100   SUIT_Desktop* desk = desktop();
101   
102   //! Dump study
103   createAction( DumpStudyId, tr( "TOT_DESK_FILE_DUMP_STUDY" ), QIconSet(),
104                 tr( "MEN_DESK_FILE_DUMP_STUDY" ), tr( "PRP_DESK_FILE_DUMP_STUDY" ),
105                 CTRL+Key_D, desk, false, this, SLOT( onDumpStudy() ) );
106     
107   //! Load script
108   createAction( LoadScriptId, tr( "TOT_DESK_FILE_LOAD_SCRIPT" ), QIconSet(),
109                 tr( "MEN_DESK_FILE_LOAD_SCRIPT" ), tr( "PRP_DESK_FILE_LOAD_SCRIPT" ),
110                 CTRL+Key_T, desk, false, this, SLOT( onLoadScript() ) );
111
112   //! Properties
113   createAction( PropertiesId, tr( "TOT_DESK_PROPERTIES" ), QIconSet(),
114                 tr( "MEN_DESK_PROPERTIES" ), tr( "PRP_DESK_PROPERTIES" ),
115                 CTRL+Key_P, desk, false, this, SLOT( onProperties() ) );
116
117   //! Catalog Generator
118   createAction( CatalogGenId, tr( "TOT_DESK_CATALOG_GENERATOR" ),  QIconSet(),
119                 tr( "MEN_DESK_CATALOG_GENERATOR" ), tr( "PRP_DESK_CATALOG_GENERATOR" ),
120                 SHIFT+Key_G, desk, false, this, SLOT( onCatalogGen() ) );
121
122   //! Registry Display
123   createAction( RegDisplayId, tr( "TOT_DESK_REGISTRY_DISPLAY" ),  QIconSet(),
124                 tr( "MEN_DESK_REGISTRY_DISPLAY" ), tr( "PRP_DESK_REGISTRY_DISPLAY" ),
125                 SHIFT+Key_D, desk, false, this, SLOT( onRegDisplay() ) );
126
127   int fileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1 );
128
129   createMenu( DumpStudyId, fileMenu, 10, -1 );
130   createMenu( separator(), fileMenu, -1, 15, -1 );
131   createMenu( LoadScriptId, fileMenu, 10, -1 );
132   createMenu( separator(), fileMenu, -1, 15, -1 );
133   createMenu( PropertiesId, fileMenu, 10, -1 );
134   createMenu( separator(), fileMenu, -1, 15, -1 );
135
136   int toolsMenu = createMenu( tr( "MEN_DESK_TOOLS" ), -1, -1, 50 );
137   createMenu( CatalogGenId, toolsMenu, 10, -1 );
138   createMenu( RegDisplayId, toolsMenu, 10, -1 );
139   createMenu( separator(), toolsMenu, -1, 15, -1 );
140 }
141
142 /*! Purpose : SLOT. Open new document with \a aName.*/
143 bool SalomeApp_Application::onOpenDoc( const QString& aName )
144 {
145   bool res = false, toOpen = true, isAlreadyOpen = false;
146
147   // Look among opened studies
148   if (activeStudy()) { // at least one study is opened
149     SUIT_Session* aSession = SUIT_Session::session();
150     QPtrList<SUIT_Application> aAppList = aSession->applications();
151     QPtrListIterator<SUIT_Application> it (aAppList);
152     SUIT_Application* aApp = 0;
153     // iterate on all applications
154     for (; (aApp = it.current()) && !isAlreadyOpen; ++it) {
155       if (aApp->activeStudy()->studyName() == aName) {
156         isAlreadyOpen = true; // Already opened, ask user what to do
157
158         // The document ... is already open.
159         // Do you want to reload it?
160         int aAnswer = SUIT_MessageBox::warn2(desktop(), tr("WRN_WARNING"),
161                                              tr("QUE_DOC_ALREADYOPEN").arg(aName),
162                                              tr("BUT_YES"), tr("BUT_NO"), 1, 2, 2);
163         if (aAnswer == 1) { // reload
164           if (activeStudy()->studyName() == aName && aAppList.count() > 1) {
165             // Opened in THIS (active) application.
166             STD_Application* app1 = (STD_Application*)aAppList.at(0);
167             STD_Application* app2 = (STD_Application*)aAppList.at(1);
168             if (!app1 || !app2) {
169               // Error
170               return false;
171             }
172             if (app1->activeStudy()->studyName() == aName) {
173               // app1 is this application, we need another one
174               app1 = app2;
175             }
176             // Close document of this application. This application will be destroyed.
177             onCloseDoc(/*ask = */false);
178             // Open the file with another application, as this one will be destroyed.
179             return app1->onOpenDoc(aName);
180           } else {
181             // Opened in another application.
182             STD_Application* app = (STD_Application*)aApp;
183             if (app)
184               app->onCloseDoc(/*ask = */false);
185           }
186         } else { // do not reload
187           // OK, the study will not be reloaded, but we call
188           // CAM_Application::onOpenDoc( aName ) all the same.
189           // It will activate a desktop of the study <aName>.
190         }
191       }
192     }
193   }
194
195   // Look among unloaded studies
196   if (!isAlreadyOpen) {
197     std::vector<std::string> List = studyMgr()->GetOpenStudies();
198
199     QString studyName;
200     for (unsigned int ind = 0; ind < List.size() && !isAlreadyOpen; ind++) {
201       studyName = List[ind].c_str();
202       if (aName == studyName) {
203         // Already exists unloaded, ask user what to do
204         isAlreadyOpen = true;
205
206         // The document ... already exists in the study manager.
207         // Do you want to reload it?
208         int aAnswer = SUIT_MessageBox::warn2(desktop(), tr("WRN_WARNING"),
209                                              tr("QUE_DOC_ALREADYEXIST").arg(aName),
210                                              tr("BUT_YES"), tr("BUT_NO"), 1, 2, 2);
211         if (aAnswer == 1) {
212           _PTR(Study) aStudy = studyMgr()->GetStudyByName(aName.latin1());
213           if (aStudy)
214             studyMgr()->Close(aStudy);
215         } else {
216           toOpen = false;
217         }
218       }
219     }
220   }
221
222   if (toOpen)
223     res = CAM_Application::onOpenDoc( aName );
224
225   QAction* a = action( MRUId );
226   if ( a && a->inherits( "QtxMRUAction" ) )
227   {
228     QtxMRUAction* mru = (QtxMRUAction*)a;
229     if ( res )
230       mru->insert( aName );
231     else
232       mru->remove( aName );
233   }
234   return res;
235 }
236
237 /*!SLOT. Load document.*/
238 void SalomeApp_Application::onLoadDoc()
239 {
240   QString name, studyname, ext;
241
242   STD_LoadStudiesDlg aDlg( desktop(), TRUE);
243
244   std::vector<std::string> List = studyMgr()->GetOpenStudies();
245
246   SUIT_Session* aSession = SUIT_Session::session();
247   QPtrList<SUIT_Application> aAppList = aSession->applications();
248   SUIT_Application* aApp = 0;
249
250   for (unsigned int ind = 0; ind < List.size(); ind++) {
251      studyname = List[ind].c_str();
252      //Add to list only unloaded studies
253      bool isAlreadyOpen = false;
254      for ( QPtrListIterator<SUIT_Application> it( aAppList ); it.current() && !isAlreadyOpen; ++it )
255        {
256          aApp = it.current();
257          if(!aApp || !aApp->activeStudy()) continue;
258          if ( aApp->activeStudy()->studyName() == studyname ) isAlreadyOpen = true;
259        }
260
261      if ( !isAlreadyOpen ) aDlg.ListComponent->insertItem( studyname );
262   }
263
264   int retVal = aDlg.exec();
265   studyname = aDlg.ListComponent->currentText();
266
267   if (retVal == QDialog::Rejected)
268     return;
269
270   if ( studyname.isNull() || studyname.isEmpty() )
271     return;
272
273   name = studyname;
274   name.replace( QRegExp(":"), "/" );
275
276   if( LightApp_Application::onLoadDoc( name ) )
277   {
278      updateWindows();
279      updateViewManagers();
280      updateObjectBrowser(true);
281   }
282 }
283
284
285 /*!SLOT. Load document with \a aName.*/
286 bool SalomeApp_Application::onLoadDoc( const QString& aName )
287 {
288   return LightApp_Application::onLoadDoc( aName );
289 }
290
291 /*!SLOT. Copy objects to study maneger from selection maneger..*/
292 void SalomeApp_Application::onCopy()
293 {
294   SALOME_ListIO list;
295   LightApp_SelectionMgr* mgr = selectionMgr();
296   mgr->selectedObjects(list);
297   
298   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(activeStudy());
299   if(study == NULL) return;
300   
301   _PTR(Study) stdDS = study->studyDS();
302   if(!stdDS) return;
303
304   SALOME_ListIteratorOfListIO it( list );
305   if(it.More())
306     {
307       _PTR(SObject) so = stdDS->FindObjectID(it.Value()->getEntry());
308       try {
309         studyMgr()->Copy(so);
310         onSelectionChanged();
311       }
312       catch(...) {
313       }
314     }
315 }
316
317 /*!SLOT. Paste objects to study maneger from selection manager.*/
318 void SalomeApp_Application::onPaste()
319 {
320   SALOME_ListIO list;
321   LightApp_SelectionMgr* mgr = selectionMgr();
322   mgr->selectedObjects(list);
323
324   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(activeStudy());
325   if(study == NULL) return;
326
327   _PTR(Study) stdDS = study->studyDS();
328   if(!stdDS) return;
329
330   SALOME_ListIteratorOfListIO it( list );
331   if(it.More())
332     {
333       _PTR(SObject) so = stdDS->FindObjectID(it.Value()->getEntry());
334       try {
335         studyMgr()->Paste(so);
336         updateObjectBrowser( true );
337         updateActions(); //SRN: BugID IPAL9377, case 3
338       }
339       catch(...) {
340       }
341     }
342 }
343
344 /*!Sets enable or disable some actions on selection changed.*/
345 void SalomeApp_Application::onSelectionChanged()
346 {
347    SALOME_ListIO list;
348    LightApp_SelectionMgr* mgr = selectionMgr();
349    mgr->selectedObjects(list);
350
351    bool canCopy  = false;
352    bool canPaste = false;
353
354    SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(activeStudy());
355    if (study != NULL) {
356      _PTR(Study) stdDS = study->studyDS();
357
358      if (stdDS) {
359        SALOME_ListIteratorOfListIO it ( list );
360
361        if (it.More() && list.Extent() == 1) {
362          _PTR(SObject) so = stdDS->FindObjectID(it.Value()->getEntry());
363
364          if ( so ) {
365            SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(so.get());
366
367            if ( aSO ) {
368              canCopy = studyMgr()->CanCopy(so);
369              canPaste = studyMgr()->CanPaste(so);
370            }
371          }
372        }
373      }
374    }
375
376    action(EditCopyId)->setEnabled(canCopy);
377    action(EditPasteId)->setEnabled(canPaste);
378 }
379
380 /*!Delete references.*/
381 void SalomeApp_Application::onDeleteInvalidReferences()
382 {
383   SALOME_ListIO aList;
384   LightApp_SelectionMgr* mgr = selectionMgr();
385   mgr->selectedObjects( aList, QString::null, false );
386
387   if( aList.IsEmpty() )
388     return;
389
390   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(activeStudy());
391   _PTR(Study) aStudyDS = aStudy->studyDS();
392   _PTR(StudyBuilder) aStudyBuilder = aStudyDS->NewBuilder();
393   _PTR(SObject) anObj;
394
395   for( SALOME_ListIteratorOfListIO it( aList ); it.More(); it.Next() )
396     if ( it.Value()->hasEntry() )
397     {
398       _PTR(SObject) aSObject = aStudyDS->FindObjectID( it.Value()->getEntry() ), aRefObj = aSObject;
399       while( aRefObj && aRefObj->ReferencedObject( anObj ) )
400         aRefObj = anObj;
401
402       if( aRefObj && aRefObj!=aSObject && QString( aRefObj->GetName().c_str() ).isEmpty() )
403          aStudyBuilder->RemoveReference( aSObject );
404     }
405   updateObjectBrowser();
406 }
407
408 /*!Private SLOT. */
409 void SalomeApp_Application::onOpenWith()
410 {
411   QApplication::setOverrideCursor( Qt::waitCursor );
412   SALOME_ListIO aList;
413   LightApp_SelectionMgr* mgr = selectionMgr();
414   mgr->selectedObjects(aList);
415   if (aList.Extent() != 1)
416     {
417       QApplication::restoreOverrideCursor();
418       return;
419     }
420   Handle(SALOME_InteractiveObject) aIObj = aList.First();
421   QString aModuleName(aIObj->getComponentDataType());
422   QString aModuleTitle = moduleTitle(aModuleName);
423   activateModule(aModuleTitle);
424   QApplication::restoreOverrideCursor();
425 }
426
427 //=======================================================================
428 // name    : createNewStudy
429 /*! Purpose : Create new study*/
430 //=======================================================================
431 SUIT_Study* SalomeApp_Application::createNewStudy()
432 {
433   SalomeApp_Study* aStudy = new SalomeApp_Study( this );
434
435   // Set up processing of major study-related events
436   connect( aStudy, SIGNAL( created( SUIT_Study* ) ), this, SLOT( onStudyCreated( SUIT_Study* ) ) );
437   connect( aStudy, SIGNAL( opened ( SUIT_Study* ) ), this, SLOT( onStudyOpened ( SUIT_Study* ) ) );
438   connect( aStudy, SIGNAL( saved  ( SUIT_Study* ) ), this, SLOT( onStudySaved  ( SUIT_Study* ) ) );
439   connect( aStudy, SIGNAL( closed ( SUIT_Study* ) ), this, SLOT( onStudyClosed ( SUIT_Study* ) ) );
440
441   return aStudy;
442 }
443
444 //=======================================================================
445 // name    : updateCommandsStatus
446 /*! Purpose : Enable/Disable menu items and toolbar buttons. Rebuild menu*/
447 //=======================================================================
448 void SalomeApp_Application::updateCommandsStatus()
449 {
450   LightApp_Application::updateCommandsStatus();
451
452   // Dump study menu
453   QAction* a = action( DumpStudyId );
454   if ( a )
455     a->setEnabled( activeStudy() );
456
457   // Load script menu
458   a = action( LoadScriptId );
459   if ( a )
460     a->setEnabled( activeStudy() );
461
462   a = action( PropertiesId );
463   if( a )
464     a->setEnabled( activeStudy() );
465
466   // update state of Copy/Paste menu items
467   onSelectionChanged();
468 }
469
470 /*!Private SLOT. On dump study.*/
471 void SalomeApp_Application::onDumpStudy( )
472 {
473   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( activeStudy() );
474   if ( !appStudy ) return;
475   _PTR(Study) aStudy = appStudy->studyDS();
476
477   QStringList aFilters;
478   aFilters.append( tr( "PYTHON_FILES_FILTER" ) );
479
480   SalomeApp_CheckFileDlg* fd = new SalomeApp_CheckFileDlg( desktop(), false, tr("PUBLISH_IN_STUDY"), true, true);
481   fd->setCaption( tr( "TOT_DESK_FILE_DUMP_STUDY" ) );
482   fd->setFilters( aFilters );
483   fd->SetChecked(true);
484   fd->exec();
485   QString aFileName = fd->selectedFile();
486   bool toPublish = fd->IsChecked();
487   delete fd;
488
489   if(!aFileName.isEmpty()) {
490     QFileInfo aFileInfo(aFileName);
491     bool res = aStudy->DumpStudy( aFileInfo.dirPath( true ).latin1(), aFileInfo.baseName().latin1(), toPublish );
492     if ( !res )
493     SUIT_MessageBox::warn1 ( desktop(),
494                              QObject::tr("WRN_WARNING"),
495                              tr("WRN_DUMP_STUDY_FAILED"),
496                              QObject::tr("BUT_OK") );
497   }
498 }
499
500 /*!Private SLOT. On load script.*/
501 void SalomeApp_Application::onLoadScript( )
502 {
503   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( activeStudy() );
504   if ( !appStudy ) return;
505   _PTR(Study) aStudy = appStudy->studyDS();
506
507   if ( aStudy->GetProperties()->IsLocked() ) {
508     SUIT_MessageBox::warn1 ( desktop(),
509                              QObject::tr("WRN_WARNING"),
510                              QObject::tr("WRN_STUDY_LOCKED"),
511                              QObject::tr("BUT_OK") );
512     return;
513   }
514
515   QStringList filtersList;
516   filtersList.append(tr("PYTHON_FILES_FILTER"));
517   filtersList.append(tr("ALL_FILES_FILTER"));
518
519   QString aFile = SUIT_FileDlg::getFileName( desktop(), "", filtersList, tr( "TOT_DESK_FILE_LOAD_SCRIPT" ), true, true );
520
521   if ( !aFile.isEmpty() )
522   {
523     QString command = QString("execfile(\"%1\")").arg(aFile);
524
525     PythonConsole* pyConsole = pythonConsole();
526
527     if ( pyConsole )
528       pyConsole->exec( command );
529   }
530 }
531
532 /*!Gets file filter.
533  *\retval QString "(*.hdf)"
534  */
535 QString SalomeApp_Application::getFileFilter() const
536 {
537   return "(*.hdf)";
538 }
539
540 /*!Create window.*/
541 QWidget* SalomeApp_Application::createWindow( const int flag )
542 {
543   QWidget* wid = 0;
544   if ( flag != WT_PyConsole ) wid = LightApp_Application::createWindow(flag);
545
546   SUIT_ResourceMgr* resMgr = resourceMgr();
547
548   if ( flag == WT_ObjectBrowser )
549   {
550     OB_Browser* ob = (OB_Browser*)wid;
551     connect( ob->listView(), SIGNAL( doubleClicked( QListViewItem* ) ), this, SLOT( onDblClick( QListViewItem* ) ) );
552     bool autoSize = resMgr->booleanValue( "ObjectBrowser", "auto_size", false ),
553          autoSizeFirst = resMgr->booleanValue( "ObjectBrowser", "auto_size_first", true );
554     for ( int i = SalomeApp_DataObject::CT_Value; i <= SalomeApp_DataObject::CT_RefEntry; i++ )
555     {
556       ob->addColumn( tr( QString().sprintf( "OBJ_BROWSER_COLUMN_%d", i ) ), i );
557       ob->setColumnShown( i, resMgr->booleanValue( "ObjectBrowser",
558                                                    QString().sprintf( "visibility_column_%d", i ), true ) );
559     }
560     ob->setWidthMode( autoSize ? QListView::Maximum : QListView::Manual );
561     ob->listView()->setColumnWidthMode( 0, autoSizeFirst ? QListView::Maximum : QListView::Manual );
562     ob->resize( desktop()->width()/3, ob->height() );
563   }
564   else if ( flag == WT_PyConsole )
565   {
566     PythonConsole* pyCons = new PythonConsole( desktop(), new SalomeApp_PyInterp() );
567     pyCons->setCaption( tr( "PYTHON_CONSOLE" ) );
568     wid = pyCons;
569     pyCons->resize( pyCons->width(), desktop()->height()/4 );
570     //pyCons->connectPopupRequest(this, SLOT(onConnectPopupRequest(SUIT_PopupClient*, QContextMenuEvent*)));
571   }
572   return wid;
573 }
574
575 /*!Create preferences.*/
576 void SalomeApp_Application::createPreferences( LightApp_Preferences* pref )
577 {
578   LightApp_Application::createPreferences(pref);
579
580   if ( !pref )
581     return;
582
583   int salomeCat = pref->addPreference( tr( "PREF_CATEGORY_SALOME" ) );
584   int obTab = pref->addPreference( tr( "PREF_TAB_OBJBROWSER" ), salomeCat );
585   int defCols = pref->addPreference( tr( "PREF_GROUP_DEF_COLUMNS" ), obTab );
586   for ( int i = SalomeApp_DataObject::CT_Value; i <= SalomeApp_DataObject::CT_RefEntry; i++ )
587   {
588     pref->addPreference( tr( QString().sprintf( "OBJ_BROWSER_COLUMN_%d", i ) ), defCols,
589                          LightApp_Preferences::Bool, "ObjectBrowser", QString().sprintf( "visibility_column_%d", i ) );
590   }
591   pref->setItemProperty( defCols, "columns", 1 );
592 }
593
594 /*!Update desktop title.*/
595 void SalomeApp_Application::updateDesktopTitle() {
596   QString aTitle = applicationName();
597   QString aVer = applicationVersion();
598   if ( !aVer.isEmpty() )
599     aTitle += QString( " " ) + aVer;
600
601   if ( activeStudy() )
602   {
603     QString sName = SUIT_Tools::file( activeStudy()->studyName().stripWhiteSpace(), false );
604     if ( !sName.isEmpty() ) {
605       SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(activeStudy());
606       if ( study ) {
607         _PTR(Study) stdDS = study->studyDS();
608         if(stdDS) {
609           if ( stdDS->GetProperties()->IsLocked() ) {
610             aTitle += QString( " - [%1 (%2)]").arg( sName ).arg( tr( "STUDY_LOCKED" ) );
611           } else {
612             aTitle += QString( " - [%1]" ).arg( sName );
613           }
614         }
615       }
616     }
617   }
618
619   desktop()->setCaption( aTitle );
620 }
621
622 /*!Gets CORBA::ORB_var*/
623 CORBA::ORB_var SalomeApp_Application::orb()
624 {
625   ORB_INIT& init = *SINGLETON_<ORB_INIT>::Instance();
626   static CORBA::ORB_var _orb = init( qApp->argc(), qApp->argv() );
627   return _orb;
628 }
629
630 /*!Create and return SALOMEDS_StudyManager.*/
631 SALOMEDSClient_StudyManager* SalomeApp_Application::studyMgr()
632 {
633   static SALOMEDSClient_StudyManager* _sm = new SALOMEDS_StudyManager();
634   return _sm;
635 }
636
637 /*!Create and return SALOME_NamingService.*/
638 SALOME_NamingService* SalomeApp_Application::namingService()
639 {
640   static SALOME_NamingService* _ns = new SALOME_NamingService( orb() );
641   return _ns;
642 }
643
644 /*!Create and return SALOME_LifeCycleCORBA.*/
645 SALOME_LifeCycleCORBA* SalomeApp_Application::lcc()
646 {
647   static SALOME_LifeCycleCORBA* _lcc = new SALOME_LifeCycleCORBA( namingService() );
648   return _lcc;
649 }
650
651 /*!Return default engine IOR for light modules*/
652 QString SalomeApp_Application::defaultEngineIOR()
653 {
654   /// Look for a default module engine (needed for CORBAless modules to use SALOMEDS persistence)
655   QString anIOR( "" );
656   CORBA::Object_ptr anEngine = namingService()->Resolve( "/SalomeAppEngine" );
657   if ( !CORBA::is_nil( anEngine ) )
658     anIOR = orb()->object_to_string( anEngine );
659   return anIOR;
660 }
661
662 /*!Private SLOT. On preferences.*/
663 void SalomeApp_Application::onProperties()
664 {
665   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( activeStudy() );
666   if( !study )
667     return;
668
669   _PTR(StudyBuilder) SB = study->studyDS()->NewBuilder();
670   SB->NewCommand();
671
672   SalomeApp_StudyPropertiesDlg aDlg( desktop() );
673   int res = aDlg.exec();
674   if( res==QDialog::Accepted && aDlg.isChanged() )
675     SB->CommitCommand();
676   else
677     SB->AbortCommand();
678
679   //study->updateCaptions();
680   updateDesktopTitle();
681   updateActions();
682 }
683
684 /*!Insert items in popup, which necessary for current application*/
685 void SalomeApp_Application::contextMenuPopup( const QString& type, QPopupMenu* thePopup, QString& title )
686 {
687   LightApp_Application::contextMenuPopup( type, thePopup, title );
688
689   OB_Browser* ob = objectBrowser();
690   if ( !ob || type != ob->popupClientType() )
691     return;
692
693   // Get selected objects
694   SALOME_ListIO aList;
695   LightApp_SelectionMgr* mgr = selectionMgr();
696   mgr->selectedObjects( aList, QString::null, false );
697
698   // "Delete reference" item should appear only for invalid references
699
700   // isInvalidRefs will be true, if at least one of selected objects is invalid reference
701   bool isInvalidRefs = false;
702   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(activeStudy());
703   _PTR(Study) aStudyDS = aStudy->studyDS();
704   _PTR(SObject) anObj;
705
706   for( SALOME_ListIteratorOfListIO it( aList ); it.More() && !isInvalidRefs; it.Next() )
707     if( it.Value()->hasEntry() )
708     {
709       _PTR(SObject) aSObject = aStudyDS->FindObjectID( it.Value()->getEntry() ), aRefObj = aSObject;
710       while( aRefObj && aRefObj->ReferencedObject( anObj ) )
711         aRefObj = anObj;
712
713       if( aRefObj && aRefObj!=aSObject && QString( aRefObj->GetName().c_str() ).isEmpty() )
714         isInvalidRefs = true;
715     }
716
717   // Add "Delete reference" item to popup
718   if ( isInvalidRefs )
719   {
720     thePopup->insertSeparator();
721     thePopup->insertItem( tr( "MEN_DELETE_INVALID_REFERENCE" ), this, SLOT( onDeleteInvalidReferences() ) );
722     return;
723   }
724
725   aList.Clear();
726   mgr->selectedObjects( aList );
727
728   // "Activate module" item should appear only if it's necessary
729   if (aList.Extent() != 1)
730     return;
731   Handle(SALOME_InteractiveObject) aIObj = aList.First();
732   QString aModuleName(aIObj->getComponentDataType());
733   QString aModuleTitle = moduleTitle(aModuleName);
734   CAM_Module* currentModule = activeModule();
735   if (currentModule && currentModule->moduleName() == aModuleTitle)
736     return;
737   thePopup->insertItem( tr( "MEN_OPENWITH" ), this, SLOT( onOpenWith() ) );
738 }
739
740 /*!Update obect browser:
741  1.if 'updateModels' true, update existing data models;
742  2. update "non-existing" (not loaded yet) data models;
743  3. update object browser if it existing */
744 void SalomeApp_Application::updateObjectBrowser( const bool updateModels )
745 {
746   // update existing data models (already loaded SComponents)
747   LightApp_Application::updateObjectBrowser(updateModels);
748
749   // update "non-existing" (not loaded yet) data models
750   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(activeStudy());
751   if ( study )
752   {
753     _PTR(Study) stdDS = study->studyDS();
754     if( stdDS )
755     {
756       for ( _PTR(SComponentIterator) it ( stdDS->NewComponentIterator() ); it->More(); it->Next() ) 
757       {
758         _PTR(SComponent) aComponent ( it->Value() );
759
760         if ( aComponent->ComponentDataType() == "Interface Applicative" )
761           continue; // skip the magic "Interface Applicative" component
762
763         SalomeApp_DataModel::BuildTree( aComponent, study->root(), study, /*skipExisitng=*/true );
764       }
765     }
766   }
767
768   if ( objectBrowser() )
769   {
770     objectBrowser()->updateGeometry();
771     objectBrowser()->updateTree( 0, false );
772   }
773 }
774
775 /*!Display Catalog Genenerator dialog */
776 void SalomeApp_Application::onCatalogGen()
777 {
778   ToolsGUI_CatalogGeneratorDlg aDlg( desktop() );
779   aDlg.exec();
780 }
781
782 /*!Display Registry Display dialog */
783 void SalomeApp_Application::onRegDisplay()
784 {
785   CORBA::ORB_var anOrb = orb();
786   ToolsGUI_RegWidget* regWnd = ToolsGUI_RegWidget::GetRegWidget( anOrb, desktop(), "Registry" );
787   regWnd->show();
788   regWnd->raise();
789   regWnd->setActiveWindow();
790 }
791
792 /*!find original object by double click on item */
793 void SalomeApp_Application::onDblClick( QListViewItem* it )
794 {
795   OB_ListItem* item = dynamic_cast<OB_ListItem*>( it );
796   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( activeStudy() );
797
798   if( study && item )
799   {
800     SalomeApp_DataObject* obj = dynamic_cast<SalomeApp_DataObject*>( item->dataObject() );
801     if( !obj )
802       return;
803
804     QString entry = obj->entry();
805     _PTR(SObject) sobj = study->studyDS()->FindObjectID( entry.latin1() ), ref;
806
807     if( sobj && sobj->ReferencedObject( ref ) )
808     {
809       entry = ref->GetID().c_str();
810       QListViewItemIterator anIt( item->listView() );
811       for( ; anIt.current(); anIt++ )
812       {
813         OB_ListItem* item = dynamic_cast<OB_ListItem*>( anIt.current() );
814         if( !item )
815           continue;
816
817         SalomeApp_DataObject* original = dynamic_cast<SalomeApp_DataObject*>( item->dataObject() );
818         if( original->entry()!=entry )
819           continue;
820
821         OB_Browser* br = objectBrowser();
822         br->setSelected( original );
823         SUIT_DataObject* p = original->parent();
824         while( p )
825         {
826           br->setOpen( p );
827           p = p->parent();
828         }
829         break;
830       }
831     }
832   }
833 }